Beispiel #1
0
        public Queue <ReGoapNode> Plan(ReGoapAgent agent)
        {
            Queue <ReGoapNode> result = new Queue <ReGoapNode>();

            ReGoapGoal currentGoal = null;

            List <ReGoapGoal> possibleGoals = new List <ReGoapGoal>();

            foreach (var goal in agent.GetGoalsSet())
            {
                possibleGoals.Add(goal);
            }

            possibleGoals.Sort((x, y) => x.GetPriority().CompareTo(y.GetPriority()));

            for (int i = 0; i < possibleGoals.Count; ++i)
            {
                currentGoal = possibleGoals[i];
                ReGoapState goalState = currentGoal.GetGoalState();

                ReGoapNode reGoapNode = new ReGoapNode(agent, goalState, null, null);
                ReGoapNode leaf       = nodeManager.Run(reGoapNode);
                if (leaf == null)
                {
                    currentGoal = null;
                    continue;
                }

                result = leaf.CalculatePath();
                if (result.Count == 0)
                {
                    currentGoal = null;
                    continue;
                }
                break;
            }

            return(result);
        }
Beispiel #2
0
        private void Init(ReGoapAgent agent, ReGoapState newGoalState, ReGoapNode parent, ReGoapAction action)
        {
            expandList.Clear();

            ReGoapState goal = null;

            this.reGoapAgent = agent;
            this.parentNode  = parent;
            this.action      = action;
            if (action != null)
            {
                actionSettings = action.GetSettings(newGoalState);
            }

            if (parentNode != null)
            {
                agentReGoapState = parentNode.GetState().Clone();
                g = parentNode.GetPathCost();
            }
            else
            {
                ReGoapState reGoapState = agent.GetWorldState();
                agentReGoapState = reGoapState.Clone();
            }

            if (action != null)
            {
                // create a new instance of the goal based on the paren't goal
                goal = ReGoapState.Instantiate(newGoalState);

                var preconditions = action.GetPreconditions(goal);
                var effects       = action.GetEffects(goal);
                // adding the action's effects to the current node's state
                agentReGoapState.AddFromState(effects);
                // addding the action's cost to the node's total cost
                g += action.GetCost();
                // add all preconditions of the current action to the goal
                goal.AddFromState(preconditions);
                // removes from goal all the conditions that are now fullfiled in the node's state
                goal.ReplaceWithMissingDifference(agentReGoapState);
            }
            else
            {
                goal = newGoalState.MissingDifference(agentReGoapState);
            }
            h    = goal.Count;
            cost = g + h;

            //Expand(goal);

            expandList.Clear();

            List <ReGoapAction> actionsList = reGoapAgent.GetActionsSet();

            for (var index = actionsList.Count - 1; index >= 0; index--)
            {
                ReGoapAction possibleAction = actionsList[index];

                if (!possibleAction.CheckProceduralCondition())  // 执行条件不满足排除掉
                {
                    continue;
                }

                ReGoapState precond = possibleAction.GetPreconditions(goal);
                ReGoapState effects = possibleAction.GetEffects(goal);

                if (!ReGoapState.HasAny(effects, goal)) // any effect is the current goal
                {
                    continue;
                }

                if (!ReGoapState.HasAnyConflict(precond, goal))
                {
                    ReGoapNode reGoapNode = new ReGoapNode(reGoapAgent, goal, this, possibleAction);
                    expandList.Add(reGoapNode);
                }
            }
        }
Beispiel #3
0
 public ReGoapNode(ReGoapAgent agent, ReGoapState newGoalState, ReGoapNode parent, ReGoapAction action)
 {
     Init(agent, newGoalState, parent, action);
 }