Esempio n. 1
0
 public override bool ShouldRetry(Creature agent)
 {
     return(EntityToGather != null && !EntityToGather.IsDead && !agent.AI.GatherManager.ItemsToGather.Contains(EntityToGather) &&
            PlanAct.PathExists(EntityToGather.GetComponent <Physics>().CurrentVoxel,
                               agent.Physics.CurrentVoxel,
                               agent.AI));
 }
Esempio n. 2
0
        public override bool IsFeasible(Creature agent)
        {
            if (EntityToKill == null || EntityToKill.IsDead)
            {
                return(false);
            }
            else
            {
                Creature ai = EntityToKill.GetChildrenOfTypeRecursive <Creature>().FirstOrDefault();
                switch (Mode)
                {
                case KillType.Attack:
                {
                    if (!agent.Faction.AttackDesignations.Contains(EntityToKill))
                    {
                        return(false);
                    }
                    return(true);
                }

                case KillType.Chop:
                {
                    if (!agent.Faction.ChopDesignations.Contains(EntityToKill))
                    {
                        return(false);
                    }
                    return(true);
                }

                case KillType.Auto:
                {
                    return(true);
                }
                }

                Voxel target    = new Voxel();
                bool  voxExists = PlayState.ChunkManager.ChunkData.GetVoxel(EntityToKill.Position, ref target);
                if (!voxExists || !PlanAct.PathExists(agent.Physics.CurrentVoxel, target, agent.AI))
                {
                    return(false);
                }


                if (ai == null)
                {
                    return(true);
                }
                Relationship relation =
                    PlayState.ComponentManager.Diplomacy.GetPolitics(ai.Faction, agent.Faction).GetCurrentRelationship();
                return(relation == Relationship.Hateful || relation == Relationship.Indifferent);
            }
        }
Esempio n. 3
0
 public GoToVoxelAct(string voxel, PlanAct.PlanType planType, CreatureAI creature, float radius = 0.0f)
     : base(creature)
 {
     Name = "Go to Voxel " + voxel;
     Tree = new Sequence(
             new ForLoop(
                 new Sequence(
                               new PlanAct(Agent, "PathToVoxel", voxel, planType) { Radius = radius},
                               new FollowPathAnimationAct(Agent, "PathToVoxel")
                              )
                                , 3, true),
                               new StopAct(Agent));
 }
Esempio n. 4
0
 public GoToVoxelAct(Voxel voxel, PlanAct.PlanType planType, CreatureAI creature, float radius = 0.0f)
     : base(creature)
 {
     Voxel = voxel;
     Name = "Go to Voxel";
     if(Voxel != null)
     {
         Tree = new Sequence(
                               new SetBlackboardData<Voxel>(Agent, "TargetVoxel", Voxel),
                               new PlanAct(Agent, "PathToVoxel", "TargetVoxel", planType) { Radius = radius},
                               new FollowPathAnimationAct(Agent, "PathToVoxel"),
                               new StopAct(Agent));
     }
 }
Esempio n. 5
0
        public override IEnumerable <Status> Run()
        {
            while (true)
            {
                Creature.AI.Blackboard.Erase(PathName);
                Agent.Blackboard.SetData <bool>("NoPath", false);
                PlanAct planAct = new PlanAct(Creature.AI, PathName, VoxelName, PlanType)
                {
                    Radius = Radius, MaxTimeouts = MaxTimeouts
                };
                planAct.Initialize();

                bool planSucceeded = false;
                while (true)
                {
                    Act.Status planStatus = planAct.Tick();

                    if (planStatus == Status.Fail)
                    {
                        yield return(Act.Status.Running);

                        break;
                    }

                    else if (planStatus == Status.Running)
                    {
                        yield return(Act.Status.Running);
                    }

                    else if (planStatus == Status.Success)
                    {
                        planSucceeded = true;
                        break;
                    }
                    yield return(Act.Status.Running);
                }

                if (!planSucceeded && planAct.LastResult == AStarPlanner.PlanResultCode.MaxExpansionsReached)
                {
                    yield return(Act.Status.Running);

                    Creature.CurrentCharacterMode = CharacterMode.Idle;
                    Creature.Physics.Velocity     = Vector3.Zero;
                    Timer planTimeout = new Timer(MathFunctions.Rand(30.0f, 120.0f), false, Timer.TimerMode.Real);
                    List <VoxelHandle> exploredVoxels = new List <VoxelHandle>();
                    Color debugColor = new Color(MathFunctions.RandVector3Cube() + Vector3.One * 0.5f);
                    float debugScale = MathFunctions.Rand() * 0.5f + 0.5f;
                    while (!planTimeout.HasTriggered)
                    {
                        // In this case, try to follow a greedy path toward the entity instead of just failing.
                        var greedyPath = planAct.ComputeGreedyFallback(20, exploredVoxels);
                        var goal       = planAct.GetGoal();
                        Creature.AI.Blackboard.SetData("GreedyPath", greedyPath);
                        var greedyPathFollow = new FollowPathAct(Creature.AI, "GreedyPath")
                        {
                            //BlendEnd = true,
                            //BlendStart = false
                        };
                        greedyPathFollow.Initialize();

                        foreach (var currStatus in greedyPathFollow.Run())
                        {
                            if (Debugger.Switches.DrawPaths)
                            {
                                foreach (var voxel in exploredVoxels)
                                {
                                    Drawer3D.DrawBox(voxel.GetBoundingBox().Expand(-debugScale), debugColor, 0.05f, false);
                                }
                            }
                            if (!exploredVoxels.Contains(Agent.Physics.CurrentVoxel))
                            {
                                exploredVoxels.Add(Agent.Physics.CurrentVoxel);
                            }
                            if (Debugger.Switches.DrawPaths)
                            {
                                Drawer3D.DrawLine(Agent.Position, goal.GetVoxel().WorldPosition, debugColor, 0.1f);
                            }
                            if (goal.IsInGoalRegion(Agent.Physics.CurrentVoxel))
                            {
                                yield return(Act.Status.Success);

                                yield break;
                            }
                            yield return(Act.Status.Running);
                        }
                        planTimeout.Update(DwarfTime.LastTime);
                    }
                    continue;
                }
                else if (!planSucceeded)
                {
                    Agent.Blackboard.SetData <bool>("NoPath", true);
                    yield return(Act.Status.Fail);

                    yield break;
                }
                yield return(Act.Status.Success);

                yield break;
            }
        }
Esempio n. 6
0
        public IEnumerable <Status> TrackMovingTarget()
        {
            int maxFailures     = 10;
            int currentFailures = 0;

            while (true)
            {
                Creature.AI.Blackboard.Erase("EntityVoxel");
                Act.Status status = SetTargetVoxelFromEntityAct.SetTarget("EntityVoxel", EntityName, Creature);
                Body       entity = Agent.Blackboard.GetData <Body>(EntityName);

                if (entity == null || entity.IsDead)
                {
                    yield return(Status.Success);

                    yield break;
                }

                if (status != Status.Success)
                {
                    yield return(Act.Status.Running);
                }
                Creature.AI.Blackboard.Erase("PathToEntity");

                PlanAct planAct = new PlanAct(Creature.AI, "PathToEntity", "EntityVoxel", PlanType)
                {
                    Radius = Radius
                };
                planAct.Initialize();

                bool planSucceeded = false;
                while (true)
                {
                    Act.Status planStatus = planAct.Tick();

                    if (planStatus == Status.Fail)
                    {
                        yield return(Act.Status.Running);

                        break;
                    }

                    else if (planStatus == Status.Running)
                    {
                        yield return(Act.Status.Running);
                    }

                    else if (planStatus == Status.Success)
                    {
                        planSucceeded = true;
                        break;
                    }
                }

                if (!planSucceeded)
                {
                    currentFailures++;
                    yield return(Act.Status.Running);

                    Creature.CurrentCharacterMode = Creature.CharacterMode.Idle;
                    Creature.Physics.Velocity     = Vector3.Zero;
                    if (currentFailures > maxFailures)
                    {
                        yield return(Act.Status.Fail);

                        yield break;
                    }

                    continue;
                }


                FollowPathAct followPath = new FollowPathAct(Creature.AI, "PathToEntity");
                followPath.Initialize();

                while (true)
                {
                    if (PlanType == PlanAct.PlanType.Radius && (Creature.Physics.Position - entity.Position).Length() < Radius)
                    {
                        yield return(Act.Status.Success);
                    }

                    Act.Status pathStatus = followPath.Tick();

                    if (pathStatus == Status.Fail)
                    {
                        break;
                    }

                    else if (pathStatus == Status.Running)
                    {
                        yield return(Act.Status.Running);

                        List <Creature.MoveAction> path = Agent.Blackboard.GetData <List <Creature.MoveAction> >("PathToEntity");
                        if (path.Count > 0 && (path.Last().Voxel.Position - entity.LocalTransform.Translation).Length() > 4)
                        {
                            break;
                        }

                        if (MovingTarget && (Creature.Physics.Position - entity.Position).Length() < 2)
                        {
                            yield return(Status.Success);

                            yield break;
                        }

                        continue;
                    }

                    else if (pathStatus == Status.Success)
                    {
                        yield return(Act.Status.Success);

                        yield break;
                    }
                }

                yield return(Act.Status.Running);
            }
        }
Esempio n. 7
0
        public IEnumerable <Status> TrackMovingTarget()
        {
            while (true)
            {
                Creature.AI.Blackboard.Erase("EntityVoxel");
                Act.Status status = SetTargetVoxelFromEntityAct.SetTarget("EntityVoxel", EntityName, Creature);
                Body       entity = Agent.Blackboard.GetData <Body>(EntityName);

                if (entity == null || entity.IsDead)
                {
                    yield return(Status.Success);

                    yield break;
                }

                if (status != Status.Success)
                {
                    yield return(Act.Status.Running);
                }
                Creature.AI.Blackboard.Erase("PathToEntity");
                PlanAct planAct = new PlanAct(Creature.AI, "PathToEntity", "EntityVoxel", PlanAct.PlanType.Adjacent);
                planAct.Initialize();

                bool planSucceeded = false;
                while (true)
                {
                    Act.Status planStatus = planAct.Tick();

                    if (planStatus == Status.Fail)
                    {
                        yield return(Act.Status.Running);

                        break;
                    }

                    else if (planStatus == Status.Running)
                    {
                        yield return(Act.Status.Running);
                    }

                    else if (planStatus == Status.Success)
                    {
                        planSucceeded = true;
                        break;
                    }
                }

                if (!planSucceeded)
                {
                    yield return(Act.Status.Running);

                    continue;
                }


                FollowPathAnimationAct followPath = new FollowPathAnimationAct(Creature.AI, "PathToEntity");
                followPath.Initialize();

                while (true)
                {
                    Act.Status pathStatus = followPath.Tick();

                    if (pathStatus == Status.Fail)
                    {
                        break;
                    }

                    else if (pathStatus == Status.Running)
                    {
                        yield return(Act.Status.Running);

                        List <Creature.MoveAction> path = Agent.Blackboard.GetData <List <Creature.MoveAction> >("PathToEntity");
                        if (path.Count > 0 && (path.Last().Voxel.Position - entity.LocalTransform.Translation).Length() > 4)
                        {
                            break;
                        }

                        if (MovingTarget && (Creature.Physics.Position - entity.Position).Length() < 2)
                        {
                            yield return(Status.Success);

                            yield break;
                        }

                        continue;
                    }

                    else if (pathStatus == Status.Success)
                    {
                        yield return(Act.Status.Success);

                        yield break;
                    }
                }

                yield return(Act.Status.Running);
            }
        }