コード例 #1
0
ファイル: CreatureAI.cs プロジェクト: KingMoo1213/dwarfcorp
            public IEnumerable<Act.Status> GreedyFallbackBehavior(Creature agent)
            {
                EdgeGoalRegion edgeGoal = new EdgeGoalRegion();

                while (true)
                {
                    Voxel creatureVoxel = agent.Physics.CurrentVoxel;

                    if (edgeGoal.IsInGoalRegion(creatureVoxel))
                    {
                        yield return Act.Status.Success;
                        yield break;
                    }

                    List<Creature.MoveAction> actions = agent.AI.Movement.GetMoveActions(creatureVoxel);

                    float minCost = float.MaxValue;
                    Creature.MoveAction minAction = new Creature.MoveAction();
                    bool hasMinAction = false;
                    foreach (Creature.MoveAction action in actions)
                    {
                        Voxel vox = action.Voxel;

                        float cost = edgeGoal.Heuristic(vox) + MathFunctions.Rand(0.0f, 5.0f);

                        if (cost < minCost)
                        {
                            minAction = action;
                            minCost = cost;
                            hasMinAction = true;
                        }
                    }

                    if (hasMinAction)
                    {
                        Creature.MoveAction nullAction = new Creature.MoveAction()
                        {
                            Diff = minAction.Diff,
                            MoveType = Creature.MoveType.Walk,
                            Voxel = creatureVoxel
                        };

                        agent.AI.Blackboard.SetData("GreedyPath", new List<Creature.MoveAction>(){nullAction, minAction});
                        FollowPathAnimationAct pathAct = new FollowPathAnimationAct(agent.AI, "GreedyPath");
                        pathAct.Initialize();

                        foreach (Act.Status status in pathAct.Run())
                        {
                            yield return Act.Status.Running;
                        }
                    }

                    yield return Act.Status.Running;
                }
            }
コード例 #2
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);
            }
        }