Beispiel #1
0
        /// <summary>
        /// Gets the possible action branches that arrive at the goals required state.
        /// </summary>
        /// <param name="goal"></param>
        /// <returns></returns>
        List <GoapNode> GetGoalTree(GoapGoal goal)
        {
            List <GoapNode> EndNodes = new List <GoapNode>();
            GoapNode        RootNode = new GoapNode(null, 0, goal.RequiredWorldState, null);

            // check if the world state already matches this goal.
            if (TestGoalStateAgainstWorldState(goal))
            {
                return(new List <GoapNode>());
            }

            List <GoapAction> ImmedeateActions = GetGoalActions(goal);

            foreach (GoapAction action in ImmedeateActions)
            {
                // test each immediate action's pre-requisite states against the world states.
                if (TestActionAgainstWorldState(action))
                {
                    EndNodes.Add(new GoapNode(RootNode, RootNode.CumulativeCost + action.Cost, action.SatisfiesStates, action));
                }
                else
                {
                    // recurse through this action's state requirements until it either matches the world state or has no prerequisites.
                    EndNodes.AddRange(GetActionTree(action, new GoapNode(RootNode, RootNode.CumulativeCost + action.Cost, action.SatisfiesStates, action)));
                }
            }

            return(EndNodes);
        }
Beispiel #2
0
 public GoapNode(GoapNode parent, float cumulativeCost, List <GoapState> satisfiedStates, GoapAction action)
 {
     Parent          = parent;
     CumulativeCost  = cumulativeCost;
     SatisfiedStates = satisfiedStates;
     Action          = action;
 }
Beispiel #3
0
        // takes the node path that is passed in and turns it into a queue of actions.
        Queue <GoapAction> ProccessNodeListIntoQueue(GoapNode StartNode)
        {
            Queue <GoapAction> ReturnQueue = new Queue <GoapAction>();
            GoapNode           tempNode    = StartNode;

            while (tempNode.Parent != null)
            {
                ReturnQueue.Enqueue(tempNode.Action);
                tempNode = tempNode.Parent;
            }
            return(ReturnQueue);
        }
Beispiel #4
0
        List <GoapNode> GetActionTree(GoapAction action, GoapNode RootNode)
        {
            List <GoapNode>   returnList   = new List <GoapNode>();
            List <GoapAction> ChildActions = PreCursorActions(action);

            // if this action has pre-requisite requirements that are not fulfilled, then find the actions that satisfy the required worls states.
            if (ChildActions.Count > 0)
            {
                foreach (GoapAction ChildAction in ChildActions)
                {
                    returnList.AddRange(GetActionTree(ChildAction, new GoapNode(RootNode, RootNode.CumulativeCost + action.Cost, action.SatisfiesStates, action)));
                }
            }
            else
            {
                // otherwise if the action has no pre-requisite states return this action.
                if (action.RequiredStates.Count == 0)
                {
                    returnList.Add(new GoapNode(RootNode, RootNode.CumulativeCost + action.Cost, action.SatisfiesStates, action));
                }
            }
            return(returnList);
        }