public IGoapGoal Plan(IGoapAgent agent) { m_agent = agent; m_currentGoal = null; List <IGoapGoal> possibleGoals = GetPossibleGoals(agent); if (possibleGoals.Count == 0) { GoapLogger.LogWarning("[ReGoapPlanner] Agent does not have any Goals to perform. " + m_agent.GetName()); } while (possibleGoals.Count > 0) { m_currentGoal = possibleGoals[possibleGoals.Count - 1]; possibleGoals.RemoveAt(possibleGoals.Count - 1); if (CanFullfillWithActions(m_agent, m_currentGoal) == false) { //No actions can't handle this goal GoapLogger.LogWarning("GoalPlanner :: No Actions to handle Goal (" + m_currentGoal.GetName() + ")"); m_currentGoal = null; continue; } GoapState targetState = m_currentGoal.GetGoalState(agent); GoapNode <GoapState> leaf = (GoapNode <GoapState>)m_aStar.Run(GoapNode <GoapState> .Instantiate(this, targetState, null, null), targetState); if (leaf == null) { GoapLogger.LogWarning("GoapPlanner :: Pathfinding failed!"); m_currentGoal = null; continue; } Queue <IGoapAction> actions = leaf.CalculatePath(); if (actions.Count == 0) { GoapLogger.LogWarning("GoapPlanner :: Calculating Path failed!"); m_currentGoal = null; continue; } m_currentGoal.SetPlan(actions); break; } if (m_currentGoal != null) { GoapLogger.Log(string.Format("[ReGoapPlanner] Calculated plan for goal '{0}', plan length: {1}", m_currentGoal, m_currentGoal.GetPlan().Count)); } else { GoapLogger.LogWarning("[ReGoapPlanner] Error while calculating plan."); } return(m_currentGoal); }
public GoapPlanner() { GoapNode <GoapState> .Warmup(100); GoapState.Warmup(100); m_aStar = new AStar <GoapState>(100); }
public void CalculatePath(ref Queue <IGoapAction> container) { GoapNode <T> node = this; while (node.GetParent() != null) { container.Enqueue(node.m_action); node = (GoapNode <T>)node.GetParent(); } }
public void Init(IGoapPlanner planner, GoapState goalState, GoapNode <T> parent, IGoapAction action) { m_expandList.Clear(); m_planner = planner; m_parent = parent; m_action = action; if (m_parent != null) { m_currentState = parent.GetState().Clone(); m_gCost = parent.GetCost(); } else { m_currentState = m_planner.GetAgent().GetMemory().GetWorldState().Clone(); } if (action != null) { m_gCost += action.GetCost(); GoapState preconditions = action.GetPreConditions(goalState); m_targetState = goalState + preconditions; GoapState effects = action.GetPostEffects(goalState); m_currentState.AddFromState(effects); //Did this action's effect fulfill any of the goals? m_targetState.RemoveCompletedConditions(effects); //Did the world fulfill any of the goals? m_targetState.RemoveCompletedConditions(m_planner.GetAgent().GetMemory().GetWorldState()); } else { var diff = GoapState.Instantiate(); goalState.CreateStateWithMissingDifferences(m_currentState, ref diff); m_targetState = diff; } //Cost is equal to the amount of extra actions m_hCost = m_targetState.Count; }
public static GoapNode <T> Instantiate(IGoapPlanner planner, GoapState goalState, GoapNode <T> parent, IGoapAction action) { GoapNode <T> node; if (m_cachedNodes == null) { m_cachedNodes = new Stack <GoapNode <T> >(); } node = (m_cachedNodes.Count > 0) ? m_cachedNodes.Pop() : new GoapNode <T>(); node.Init(planner, goalState, parent, action); return(node); }