/// <summary>
        /// A* forward search for a plan that satisfies the given goal.
        /// </summary>
        /// <returns>Returns null if a plan could not be found, or a list of the
        /// actions that must be performed, in order.</returns>
        public static Queue <ITransition> Plan(
            GoapAgent agent,
            WorldGoal goal)
        {
            var worldState = WorldState.Borrow();

            worldState[agent] = agent.GetState();
            var regressiveSearchGoal = RegressiveSearchWorldGoal.Borrow(goal);

            DebugUtils.Assert(worldState[agent].ContainsKey("x") &&
                              worldState[agent].ContainsKey("y") &&
                              worldState[agent].ContainsKey("z"),
                              "Agent's state must contain his position as 'x' and 'y' keys");

            var path = AStarSearch.Search(agent, regressiveSearchGoal, worldState, true);

            worldState.ReturnSelf();

            GoapAction.WithContext.ReportLeaks();
            State.ReportLeaks();
            WorldState.ReportLeaks();
            WorldGoal.ReportLeaks();
            RegressiveSearchWorldGoal.ReportLeaks();
            WorldEffects.ReportLeaks();

            return(path);
        }
Exemplo n.º 2
0
Arquivo: Goal.cs Projeto: yagelch/ex_4
        /// <summary>
        /// Regress worldGoal to before effects
        /// </summary>
        public RegressiveSearchWorldGoal RegressWorldGoal(WorldEffects worldEffects, bool inPlace = false)
        {
            var worldGoal = this;

            if (!inPlace)
            {
                worldGoal = RegressiveSearchWorldGoal.Borrow();
                foreach (var goal in this)
                {
                    worldGoal[goal.Key] = goal.Value;
                }
            }
            // Reverse change.
            foreach (var effects in worldEffects)
            {
                if (worldGoal.ContainsKey(effects.Key))
                {
                    worldGoal[effects.Key] = worldGoal[effects.Key].RegressGoal(effects.Value);
                }
            }
            return(worldGoal);
        }