Ejemplo n.º 1
0
        public override float Heuristic(Voxel voxel)
        {
            BoundingBox worldBounds = voxel.Chunk.Manager.Bounds;
            Vector3     pos         = voxel.Position;
            float       value       = MathFunctions.Dist2D(worldBounds, pos);

            return(value);
        }
Ejemplo n.º 2
0
        public override float Heuristic(VoxelHandle voxel)
        {
            if (!voxel.IsValid)
            {
                return(float.MaxValue);
            }
            BoundingBox worldBounds = voxel.Chunk.Manager.Bounds;
            Vector3     pos         = voxel.WorldPosition;
            float       value       = MathFunctions.Dist2D(worldBounds, pos);

            return(value);
        }
Ejemplo n.º 3
0
        public void UpdateTradeEnvoys(Faction faction)
        {
            foreach (TradeEnvoy envoy in faction.TradeEnvoys)
            {
                if (envoy.ExpiditionState == Expedition.State.Trading)
                {
                    if (envoy.UpdateWaitTimer(World.Time.CurrentDate))
                    {
                        World.MakeAnnouncement(String.Format("The envoy from {0} is leaving.", envoy.OwnerFaction.Name));
                        RecallEnvoy(envoy);
                    }
                }

                envoy.Creatures.RemoveAll(creature => creature.IsDead);
                if (envoy.DeathTimer.Update(faction.World.Time.CurrentDate))
                {
                    envoy.Creatures.ForEach((creature) => creature.GetRoot().Die());
                }

                Diplomacy.Politics politics = faction.World.Diplomacy.GetPolitics(faction, envoy.OtherFaction);
                if (politics.GetCurrentRelationship() == Relationship.Hateful)
                {
                    World.MakeAnnouncement(String.Format("The envoy from {0} is leaving.", envoy.OwnerFaction.Name));
                    RecallEnvoy(envoy);
                }
                else
                {
                    if (envoy.Creatures.Any(
                            // TODO (mklingen) why do I need this null check?
                            creature => creature.Creature != null &&
                            envoy.OtherFaction.Designations.IsDesignation(creature.Physics, DesignationType.Attack)))
                    {
                        if (!politics.HasEvent("You attacked our trade delegates"))
                        {
                            politics.RecentEvents.Add(new Diplomacy.PoliticalEvent()
                            {
                                Change      = -1.0f,
                                Description = "You attacked our trade delegates",
                                Duration    = new TimeSpan(1, 0, 0, 0),
                                Time        = faction.World.Time.CurrentDate
                            });
                        }
                        else
                        {
                            politics.RecentEvents.Add(new Diplomacy.PoliticalEvent()
                            {
                                Change      = -2.0f,
                                Description = "You attacked our trade delegates more than once",
                                Duration    = new TimeSpan(1, 0, 0, 0),
                                Time        = faction.World.Time.CurrentDate
                            });
                        }
                    }
                }

                if (!envoy.ShouldRemove && envoy.ExpiditionState == Expedition.State.Arriving)
                {
                    foreach (CreatureAI creature in envoy.Creatures)
                    {
                        Room tradePort = envoy.OtherFaction.GetNearestRoomOfType(BalloonPort.BalloonPortName,
                                                                                 creature.Position);

                        if (tradePort == null)
                        {
                            World.MakeAnnouncement("We need a balloon trade port to trade.", null, () =>
                            {
                                return(envoy.OtherFaction.GetNearestRoomOfType(BalloonPort.BalloonPortName, creature.Position) == null);
                            });
                            World.Tutorial("trade");
                            SoundManager.PlaySound(ContentPaths.Audio.Oscar.sfx_gui_negative_generic, 0.5f);
                            RecallEnvoy(envoy);
                            break;
                        }

                        if (creature.Tasks.Count == 0)
                        {
                            TradeEnvoy envoy1 = envoy;
                            creature.Tasks.Add(new TradeTask(tradePort, envoy1));
                        }

                        if (!tradePort.IsRestingOnZone(creature.Position))
                        {
                            continue;
                        }

                        if (envoy.ExpiditionState != Expedition.State.Trading ||
                            !envoy.IsTradeWidgetValid())
                        {
                            envoy.MakeTradeWidget(World);
                        }
                        envoy.ExpiditionState = Expedition.State.Trading;
                        break;
                    }
                }
                else if (envoy.ExpiditionState == Expedition.State.Leaving)
                {
                    BoundingBox worldBBox = faction.World.ChunkManager.Bounds;

                    foreach (CreatureAI creature in envoy.Creatures)
                    {
                        if (creature.Tasks.Count == 0)
                        {
                            creature.LeaveWorld();
                        }
                    }

                    foreach (CreatureAI creature in envoy.Creatures)
                    {
                        if (MathFunctions.Dist2D(worldBBox, creature.Position) < 2.0f)
                        {
                            creature.GetRoot().Delete();
                        }
                    }
                }
                else
                {
                    if (!envoy.IsTradeWidgetValid())
                    {
                        envoy.MakeTradeWidget(World);
                    }
                }
                if (envoy.Creatures.All(creature => creature.IsDead))
                {
                    envoy.ShouldRemove = true;

                    World.GoalManager.OnGameEvent(new Goals.Triggers.TradeEnvoyKilled
                    {
                        PlayerFaction = envoy.OtherFaction,
                        OtherFaction  = envoy.OwnerFaction
                    });
                }
            }

            bool hadFactions = faction.TradeEnvoys.Count > 0;

            faction.TradeEnvoys.RemoveAll(t => t == null || t.ShouldRemove);

            if (hadFactions && faction.TradeEnvoys.Count == 0)
            {
                var music = World.Time.IsDay() ? "main_theme_day" : "main_theme_night";
                SoundManager.PlayMusic(music);
            }
        }
Ejemplo n.º 4
0
        public void OverheadUpdate(DwarfTime time, ChunkManager chunks, float diffPhi, float diffTheta, float diffRadius)
        {
            Vector3 forward = (Target - Position);

            forward.Normalize();
            Vector3 right = Vector3.Cross(forward, UpVector);
            Vector3 up    = Vector3.Cross(right, forward);

            right.Normalize();
            up.Normalize();
            MouseState    mouse  = Mouse.GetState();
            KeyboardState keys   = Keyboard.GetState();
            var           bounds = new BoundingBox(World.ChunkManager.Bounds.Min, World.ChunkManager.Bounds.Max + Vector3.UnitY * 20);

            if (ZoomTargets.Count > 0)
            {
                Vector3 currTarget = MathFunctions.Clamp(ProjectToSurface(ZoomTargets.First()), bounds);
                if (MathFunctions.Dist2D(Target, currTarget) > 5 && _zoomTime < 3)
                {
                    Vector3 newTarget = 0.8f * Target + 0.2f * currTarget;
                    Vector3 d         = newTarget - Target;
                    if (bounds.Contains(Target + d) != ContainmentType.Contains)
                    {
                        _zoomTime = 0;
                        ZoomTargets.RemoveAt(0);
                    }
                    else
                    {
                        Target    += d;
                        Position  += d;
                        _zoomTime += (float)time.ElapsedRealTime.TotalSeconds;
                    }
                }
                else
                {
                    _zoomTime = 0;
                    ZoomTargets.RemoveAt(0);
                }
            }

            Target = MathFunctions.Clamp(Target, bounds);
            int edgePadding = -10000;

            if (GameSettings.Default.EnableEdgeScroll)
            {
                edgePadding = 100;
            }

            float diffX, diffY = 0;
            float dt = (float)time.ElapsedRealTime.TotalSeconds;

            SnapToBounds(new BoundingBox(World.ChunkManager.Bounds.Min, World.ChunkManager.Bounds.Max + Vector3.UnitY * 20));
            if (KeyManager.RotationEnabled(this))
            {
                World.UserInterface.Gui.MouseVisible = false;
                if (!shiftPressed)
                {
                    shiftPressed   = true;
                    mouseOnRotate  = new Point(mouse.X, mouse.Y);
                    mousePrerotate = new Point(mouse.X, mouse.Y);
                }

                if (!isLeftPressed && mouse.LeftButton == ButtonState.Pressed)
                {
                    isLeftPressed = true;
                }
                else if (mouse.LeftButton == ButtonState.Released)
                {
                    isLeftPressed = false;
                }

                if (!isRightPressed && mouse.RightButton == ButtonState.Pressed)
                {
                    isRightPressed = true;
                }
                else if (mouse.RightButton == ButtonState.Released)
                {
                    isRightPressed = false;
                }

                Mouse.SetPosition(mouseOnRotate.X, mouseOnRotate.Y);

                diffX = mouse.X - mouseOnRotate.X;
                diffY = mouse.Y - mouseOnRotate.Y;


                if (!isRightPressed)
                {
                    float filterDiffX = (float)(diffX * dt);
                    float filterDiffY = (float)(diffY * dt);

                    diffTheta = (filterDiffX);
                    diffPhi   = -(filterDiffY);
                }
                KeyManager.TrueMousePos = mousePrerotate;
            }
            else
            {
                if (shiftPressed)
                {
                    Mouse.SetPosition(mousePrerotate.X, mousePrerotate.Y);
                    KeyManager.TrueMousePos = new Point(mousePrerotate.X, mousePrerotate.Y);
                }
                else
                {
                    KeyManager.TrueMousePos = new Point(mouse.X, mouse.Y);
                }
                shiftPressed = false;
                World.UserInterface.Gui.MouseVisible = true;
            }

            Vector3 velocityToSet = Vector3.Zero;

            //if (EnableControl)
            {
                if (keys.IsKeyDown(ControlSettings.Mappings.Forward) || keys.IsKeyDown(Keys.Up))
                {
                    Vector3 mov = forward;
                    mov.Y = 0;
                    mov.Normalize();
                    velocityToSet += mov * CameraMoveSpeed * dt;
                }
                else if (keys.IsKeyDown(ControlSettings.Mappings.Back) || keys.IsKeyDown(Keys.Down))
                {
                    Vector3 mov = forward;
                    mov.Y = 0;
                    mov.Normalize();
                    velocityToSet += -mov * CameraMoveSpeed * dt;
                }

                if (keys.IsKeyDown(ControlSettings.Mappings.Left) || keys.IsKeyDown(Keys.Left))
                {
                    Vector3 mov = right;
                    mov.Y = 0;
                    mov.Normalize();
                    velocityToSet += -mov * CameraMoveSpeed * dt;
                }
                else if (keys.IsKeyDown(ControlSettings.Mappings.Right) || keys.IsKeyDown(Keys.Right))
                {
                    Vector3 mov = right;
                    mov.Y = 0;
                    mov.Normalize();
                    velocityToSet += mov * CameraMoveSpeed * dt;
                }
            }
            //else
            if (FollowAutoTarget)
            {
                Vector3 prevTarget = Target;
                float   damper     = MathFunctions.Clamp((Target - AutoTarget).Length() - 5, 0, 1);
                float   smooth     = 0.1f * damper;
                Target    = AutoTarget * (smooth) + Target * (1.0f - smooth);
                Position += (Target - prevTarget);
            }

            if (velocityToSet.LengthSquared() > 0)
            {
                World.Tutorial("camera");
                Velocity = velocityToSet;
            }


            if (!KeyManager.RotationEnabled(this))
            {
                if (!World.UserInterface.IsMouseOverGui)
                {
                    if (mouse.X < edgePadding || mouse.X > GameState.Game.GraphicsDevice.Viewport.Width - edgePadding)
                    {
                        moveTimer.Update(time);
                        if (moveTimer.HasTriggered)
                        {
                            float dir = 0.0f;

                            if (mouse.X < edgePadding)
                            {
                                dir = edgePadding - mouse.X;
                            }
                            else
                            {
                                dir = (GameState.Game.GraphicsDevice.Viewport.Width - edgePadding) - mouse.X;
                            }

                            dir *= 0.01f;
                            Vector3 delta = right * CameraMoveSpeed * dir * dt;
                            delta.Y  = 0;
                            Velocity = -delta;
                        }
                    }
                    else if (mouse.Y < edgePadding ||
                             mouse.Y > GameState.Game.GraphicsDevice.Viewport.Height - edgePadding)
                    {
                        moveTimer.Update(time);
                        if (moveTimer.HasTriggered)
                        {
                            float dir = 0.0f;

                            if (mouse.Y < edgePadding)
                            {
                                dir = -(edgePadding - mouse.Y);
                            }
                            else
                            {
                                dir = -((GameState.Game.GraphicsDevice.Viewport.Height - edgePadding) - mouse.Y);
                            }

                            dir *= 0.01f;

                            Vector3 delta = up * CameraMoveSpeed * dir * dt;
                            delta.Y  = 0;
                            Velocity = -delta;
                        }
                    }
                    else
                    {
                        moveTimer.Reset(moveTimer.TargetTimeSeconds);
                    }
                }
            }

            int scroll = mouse.ScrollWheelValue;

            if (isRightPressed && KeyManager.RotationEnabled(this))
            {
                scroll = (int)(diffY * 10) + LastWheel;
            }

            if (scroll != LastWheel && !World.UserInterface.IsMouseOverGui)
            {
                int change = scroll - LastWheel;

                if (!(keys.IsKeyDown(Keys.LeftAlt) || keys.IsKeyDown(Keys.RightAlt)))
                {
                    if (!keys.IsKeyDown(Keys.LeftControl))
                    {
                        var delta = change * -1;

                        if (GameSettings.Default.InvertZoom)
                        {
                            delta *= -1;
                        }

                        diffRadius = delta * CameraZoomSpeed * dt;

                        if (diffRadius < 0 && !FollowAutoTarget && GameSettings.Default.ZoomCameraTowardMouse && !shiftPressed)
                        {
                            float diffxy =
                                (new Vector3(Target.X, 0, Target.Z) -
                                 new Vector3(World.Renderer.CursorLightPos.X, 0, World.Renderer.CursorLightPos.Z)).Length();

                            if (diffxy > 5)
                            {
                                Vector3 slewTarget = Target * 0.9f + World.Renderer.CursorLightPos * 0.1f;
                                Vector3 slewDiff   = slewTarget - Target;
                                Target   += slewDiff;
                                Position += slewDiff;
                            }
                        }
                    }
                    else
                    {
                        World.Renderer.SetMaxViewingLevel(World.Renderer.PersistentSettings.MaxViewingLevel + (int)((float)change * 0.01f));
                    }
                }
            }

            LastWheel = mouse.ScrollWheelValue;

            if (!CollidesWithChunks(World.ChunkManager, Position + Velocity, false, false, 0.5f, 1.0f))
            {
                MoveTarget(Velocity);
                PushVelocity = Vector3.Zero;
            }
            else
            {
                PushVelocity += Vector3.Up * 0.1f;
                Position     += PushVelocity;
            }


            Velocity *= 0.8f;
            UpdateBasisVectors();

            bool    projectTarget   = GameSettings.Default.CameraFollowSurface || (!GameSettings.Default.CameraFollowSurface && (keys.IsKeyDown(Keys.LeftControl) || keys.IsKeyDown(Keys.RightControl)));
            Vector3 projectedTarget = projectTarget ? ProjectToSurface(Target) : Target;
            Vector3 diffTarget      = projectedTarget - Target;

            if (diffTarget.LengthSquared() > 25)
            {
                diffTarget.Normalize();
                diffTarget *= 5;
            }
            Position = (Position + diffTarget) * 0.05f + Position * 0.95f;
            Target   = (Target + diffTarget) * 0.05f + Target * 0.95f;
            float currRadius = (Position - Target).Length();
            float newRadius  = Math.Max(currRadius + diffRadius, 3.0f);

            Position = MathFunctions.ProjectOutOfHalfPlane(MathFunctions.ProjectOutOfCylinder(MathFunctions.ProjectToSphere(Position - right * diffTheta * 2 - up * diffPhi * 2, newRadius, Target), Target, 3.0f), Target, 2.0f);
            UpdateViewMatrix();
        }
Ejemplo n.º 5
0
        public void Update(WorldManager World)
        {
            if (ExpiditionState == Expedition.State.Trading)
            {
                if (UpdateWaitTimer(World.Time.CurrentDate))
                {
                    World.MakeAnnouncement(String.Format("The envoy from {0} is leaving.", OwnerFaction.ParentFaction.Name));
                    RecallEnvoy();
                }
            }

            Creatures.RemoveAll(creature => creature.IsDead);
            if (DeathTimer.Update(World.Time.CurrentDate))
            {
                Creatures.ForEach((creature) => creature.GetRoot().Die());
            }

            var politics = World.Overworld.GetPolitics(OwnerFaction.ParentFaction, OtherFaction.ParentFaction);

            if (politics.GetCurrentRelationship() == Relationship.Hateful)
            {
                World.MakeAnnouncement(String.Format("The envoy from {0} left: we are at war with them.", OwnerFaction.ParentFaction.Name));
                RecallEnvoy();
            }
            else
            {
                if (Creatures.Any(
                        // TODO (mklingen) why do I need this null check?
                        creature => creature.Creature != null &&
                        World.PersistentData.Designations.IsDesignation(creature.Physics, DesignationType.Attack)))
                {
                    if (!politics.HasEvent("You attacked our trade delegates"))
                    {
                        politics.AddEvent(new PoliticalEvent()
                        {
                            Change      = -1.0f,
                            Description = "You attacked our trade delegates",
                        });
                    }
                    else
                    {
                        politics.AddEvent(new PoliticalEvent()
                        {
                            Change      = -2.0f,
                            Description = "You attacked our trade delegates more than once",
                        });
                    }
                }
            }

            if (!ShouldRemove && ExpiditionState == Expedition.State.Arriving)
            {
                foreach (var creature in Creatures)
                {
                    var tradePort = World.GetNearestRoomOfType("Balloon Port", creature.Position);

                    if (tradePort == null)
                    {
                        World.MakeAnnouncement("We need a balloon trade port to trade.", null, () =>
                        {
                            return(World.GetNearestRoomOfType("Balloon Port", creature.Position) == null);
                        });
                        World.Tutorial("trade");
                        SoundManager.PlaySound(ContentPaths.Audio.Oscar.sfx_gui_negative_generic, 0.5f);
                        RecallEnvoy();
                        break;
                    }

                    if (creature.Tasks.Count == 0)
                    {
                        creature.Tasks.Add(new TradeTask(tradePort, this));
                    }

                    if (!tradePort.IsRestingOnZone(creature.Position))
                    {
                        continue;
                    }

                    if (ExpiditionState != Expedition.State.Trading || !IsTradeWidgetValid())
                    {
                        MakeTradeWidget(World);
                    }

                    StartTrading(World.Time.CurrentDate);
                    ExpiditionState = Expedition.State.Trading;
                    break;
                }
            }
            else if (ExpiditionState == Expedition.State.Leaving)
            {
                BoundingBox worldBBox = World.ChunkManager.Bounds;

                foreach (CreatureAI creature in Creatures)
                {
                    if (creature.Tasks.Count == 0)
                    {
                        creature.LeaveWorld();
                    }
                }

                foreach (var creature in Creatures)
                {
                    if (MathFunctions.Dist2D(worldBBox, creature.Position) < 2.0f)
                    {
                        creature.GetRoot().Delete();
                    }
                }
            }
            else if (!IsTradeWidgetValid())
            {
                MakeTradeWidget(World);
            }

            if (!OwnerFaction.ParentFaction.IsCorporate && Creatures.All(creature => creature.IsDead))
            {
                ShouldRemove = true;
            }
        }
Ejemplo n.º 6
0
        public void UpdateTradeEnvoys(Faction faction)
        {
            foreach (Faction.TradeEnvoy envoy in faction.TradeEnvoys)
            {
                if (envoy.DeathTimer.Update(PlayState.Time.CurrentDate))
                {
                    envoy.Creatures.ForEach((creature) => creature.GetRootComponent().Die());
                }

                Diplomacy.Politics politics = PlayState.ComponentManager.Diplomacy.GetPolitics(faction, envoy.OtherFaction);
                if (politics.GetCurrentRelationship() == Relationship.Hateful)
                {
                    RecallEnvoy(envoy);
                }
                else
                {
                    if (envoy.Creatures.Any(
                            creature => envoy.OtherFaction.AttackDesignations.Contains(creature.Physics)))
                    {
                        if (!politics.HasEvent("You attacked our trade delegates"))
                        {
                            politics.RecentEvents.Add(new Diplomacy.PoliticalEvent()
                            {
                                Change      = -1.0f,
                                Description = "You attacked our trade delegates",
                                Duration    = new TimeSpan(1, 0, 0, 0),
                                Time        = PlayState.Time.CurrentDate
                            });
                        }
                        else
                        {
                            politics.RecentEvents.Add(new Diplomacy.PoliticalEvent()
                            {
                                Change      = -2.0f,
                                Description = "You attacked our trade delegates more than once",
                                Duration    = new TimeSpan(1, 0, 0, 0),
                                Time        = PlayState.Time.CurrentDate
                            });
                        }
                    }
                }

                if (!envoy.ShouldRemove && envoy.ExpiditionState == Faction.Expidition.State.Arriving)
                {
                    foreach (CreatureAI creature in envoy.Creatures)
                    {
                        Room tradePort = envoy.OtherFaction.GetNearestRoomOfType(BalloonPort.BalloonPortName,
                                                                                 creature.Position);

                        if (creature.Tasks.Count == 0)
                        {
                            creature.Tasks.Add(new ActWrapperTask(new GoToZoneAct(creature, tradePort))
                            {
                                Name = "Go to trade port.", Priority = Task.PriorityType.Urgent
                            });
                        }

                        if (!tradePort.IsRestingOnZone(creature.Position))
                        {
                            continue;
                        }

                        envoy.ExpiditionState = Faction.Expidition.State.Trading;
                        if (GameState.Game.StateManager.States.ContainsKey("DiplomacyState_" + faction.Name))
                        {
                            DiplomacyState state = GameState.Game.StateManager.States["DiplomacyState_" + faction.Name] as DiplomacyState;
                            if (state != null)
                            {
                                state.Envoy     = envoy;
                                state.Resources = envoy.TradeGoods;
                            }
                            GameState.Game.StateManager.PushState("DiplomacyState_" + faction.Name);
                        }
                        else
                        {
                            GameState.Game.StateManager.PushState(new DiplomacyState(GameState.Game,
                                                                                     GameState.Game.StateManager,
                                                                                     (PlayState)GameState.Game.StateManager.GetState <PlayState>("PlayState"), envoy)
                            {
                                Name  = "DiplomacyState_" + faction.Name,
                                Envoy = envoy
                            });
                        }
                        break;
                    }
                }
                else if (envoy.ExpiditionState == Faction.Expidition.State.Leaving)
                {
                    BoundingBox worldBBox = PlayState.ChunkManager.Bounds;

                    foreach (CreatureAI creature in envoy.Creatures)
                    {
                        if (creature.Tasks.Count == 0)
                        {
                            creature.LeaveWorld();
                        }
                    }

                    foreach (CreatureAI creature in envoy.Creatures)
                    {
                        if (MathFunctions.Dist2D(worldBBox, creature.Position) < 2.0f)
                        {
                            creature.GetRootComponent().Delete();
                            creature.IsDead = true;
                        }
                    }
                }

                if (envoy.Creatures.All(creature => creature.IsDead))
                {
                    envoy.ShouldRemove = true;
                }
            }

            faction.TradeEnvoys.RemoveAll(t => t.ShouldRemove);
        }