示例#1
0
            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 GoalRegion GetGoal()
        {
            GoalRegion goal = null;

            switch (Type)
            {
            case PlanType.Radius:
                goal = new SphereGoalRegion(Target, Radius);
                break;

            case PlanType.Into:
                goal = new VoxelGoalRegion(Target);
                break;

            case PlanType.Adjacent:
                goal = new AdjacentVoxelGoalRegion2D(Target);
                break;

            case PlanType.Edge:
                goal = new EdgeGoalRegion();
                break;
            }
            return(goal);
        }
示例#3
0
        public IEnumerable <Act.Status> GreedyFallbackBehavior(Creature agent)
        {
            var edgeGoal = new EdgeGoalRegion();

            while (true)
            {
                DieTimer.Update(DwarfTime.LastTime);
                if (DieTimer.HasTriggered)
                {
                    foreach (var status in Die(agent))
                    {
                        continue;
                    }
                    yield break;
                }
                var creatureVoxel      = agent.Physics.CurrentVoxel;
                List <MoveAction> path = new List <MoveAction>();
                var storage            = new MoveActionTempStorage();
                for (int i = 0; i < 10; i++)
                {
                    if (edgeGoal.IsInGoalRegion(creatureVoxel))
                    {
                        foreach (var status in Die(agent))
                        {
                            continue;
                        }
                        yield return(Act.Status.Success);

                        yield break;
                    }

                    var actions = agent.AI.Movement.GetMoveActions(new MoveState {
                        Voxel = creatureVoxel
                    }, new List <GameComponent>(), storage);

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

                        float cost = edgeGoal.Heuristic(vox) * 10 + MathFunctions.Rand(0.0f, 0.1f) + agent.AI.Movement.Cost(action.MoveType);

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

                    if (hasMinAction)
                    {
                        path.Add(minAction);
                        creatureVoxel = minAction.DestinationVoxel;
                    }
                    else
                    {
                        foreach (var status in Die(agent))
                        {
                            continue;
                        }
                        yield return(Act.Status.Success);

                        yield break;
                    }
                }
                if (path.Count == 0)
                {
                    foreach (var status in Die(agent))
                    {
                        continue;
                    }
                    yield return(Act.Status.Success);

                    yield break;
                }
                agent.AI.Blackboard.SetData("GreedyPath", path);
                var pathAct = new FollowPathAct(agent.AI, "GreedyPath");
                pathAct.Initialize();

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