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; }
private bool CanFullfillWithActions(IGoapAgent agent, IGoapGoal goal) { GoapState goalState = goal.GetGoalState(agent).Clone(); foreach (IGoapAction action in agent.GetActions()) { if (CanRunAction(agent, action) == false) { continue; } goalState.RemoveCompletedConditions(action.GetContextPostEffects(goalState));; } goalState.RemoveCompletedConditions(m_agent.GetMemory().GetWorldState()); if (goalState.Count > 0) { return(false); } return(true); }
public bool CanRunGoal(IGoapAgent agent, IGoapGoal goal) { if (agent.GetCurrentGoal() == goal) { return(false); } if (goal.CanRun(agent) == false) { return(false); } GoapState differenceState = goal.GetGoalState(agent).Clone(); differenceState.RemoveCompletedConditions(m_agent.GetMemory().GetWorldState()); if (differenceState.Count == 0) { return(false); } return(true); }