public List <Action> UpdateAgent() { List <GOAPGoal> goalList = util.RunUtilityEngine(); if (goalList[0] != currentGoal) { currentGoal = goalList[0]; GOAPController.GC.EnqueueForPlanning(this); ActionPlan.Clear(); //ActionPlan.AddRange(Planner.GetActionPlan(this, WorldState, AvailableActions, currentGoal)); } if (ActionPlan != null) { if (ActionPlan.Count > 0) { if (ActionPlan[0].TestForFinished()) { ActionPlan.RemoveAt(0); } return(ActionPlan[0].effects); } } return(new List <Action>()); }
public List <GOAPAction> GetActionPlan(GOAPAgent Agent, List <GOAPState> WorldState, List <GOAPAction> AvailableActions, GOAPGoal goal) { List <GOAPAction> usable = new List <GOAPAction>(); for (int i = 0; i < AvailableActions.Count; ++i) { if (AvailableActions[i].CanRun(Agent)) { usable.Add(AvailableActions[i]); } } List <Node> leaves = new List <Node>(); Node startingNode = new Node(null, WorldState, 0, null); bool ableToGetPlan = BuildGraph(startingNode, leaves, AvailableActions, goal); if (!ableToGetPlan) { Debug.Log("Failed to get plan"); return(null); } Node cheapestAction = null; foreach (Node leaf in leaves) { if (cheapestAction == null) { cheapestAction = leaf; } else { if (leaf.Cost < cheapestAction.Cost) { cheapestAction = leaf; } } } List <GOAPAction> results = new List <GOAPAction>(); Node temp = cheapestAction; while (temp != null) { if (temp.Action != null) { results.Insert(0, temp.Action); } temp = temp.Parent; } return(results); }
bool BuildGraph(Node parent, List <Node> leaves, List <GOAPAction> usableActions, GOAPGoal goal) { bool foundLeaf = false; for (int i = 0; i < usableActions.Count; ++i) { if (StatesInGoal(usableActions[i].requiredStates, parent.State)) { List <GOAPState> currentState = CreateNewState(parent.State, usableActions[i].satisfiesStates); Node node = new Node(parent, currentState, parent.Cost + usableActions[i].Cost, usableActions[i]); if (StatesInGoal(goal.Preconditions, currentState)) { leaves.Add(node); foundLeaf = true; } else { List <GOAPAction> subset = ActionSubset(usableActions, usableActions[i]); bool found = BuildGraph(node, leaves, subset, goal); if (found) { foundLeaf = true; } } } } return(foundLeaf); }