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 List <INode <GoapState> > GetNeighbours() { m_expandList.Clear(); IGoapAgent agent = m_planner.GetAgent(); List <IGoapAction> actions = agent.GetActions(); for (int i = actions.Count - 1; i >= 0; i--) { IGoapAction possibleAction = actions[i]; if (possibleAction == m_action) { continue; } if (GoapPlannerManager.s_Instance.GetPlanner().CanRunAction(agent, possibleAction) == false) { continue; } GoapState preConditions = possibleAction.GetContextPreConditions(m_targetState); GoapState postEffects = possibleAction.GetContextPostEffects(m_targetState); bool isValid = (postEffects.HasAny(m_targetState)) && (!m_targetState.HasAnyConflict(preConditions)) && (!m_targetState.HasAnyConflict(postEffects)); if (isValid) { GoapState targetState = m_targetState; m_expandList.Add(Instantiate(m_planner, targetState, this, possibleAction)); } } return(m_expandList); }