/** * Gets the most relevant goal at the moment * * GOAPGoal GetMostRelevantGoal(bool recalculate) * { * GOAPGoal maxGoal = null; * float highestRelevancy = 0.0f; * float currentTime = Time.timeSinceLevelLoad; * float nextEvalTime; * * float goalRelevance = 0.0f; * for (int i = 0; i < m_GoalSet.Count; i++) * { //First check for timing checks?! * //Don't recalculate the goal relevancy if not asked to do so * * if (recalculate) * { * nextEvalTime = m_GoalSet[i].NextEvaluationTime; * * if (currentTime < nextEvalTime) * { //clear relevancy , we dont want to select these goals * m_GoalSet[i].ClearGoalRelevancy(); * } * else if (!m_GoalSet[i].GetReEvalOnSatisfication() && m_GoalSet[i].IsWSSatisfied(Ai.GetWorldState())) * { * m_GoalSet[i].ClearGoalRelevancy(); * } * else * {// recalculate goal relevancy !!!! * m_GoalSet[i].CalculateGoalRelevancy(); * //m_GoalSet[i].SetNewEvaluationTime(); * //set new timing check time?! * * } * } * // check all goal relevancy * goalRelevance = m_GoalSet[i].GoalRelevancy; * if (goalRelevance > highestRelevancy) * { * highestRelevancy = goalRelevance; * maxGoal = m_GoalSet[i]; * } * * } * return maxGoal; * }*/ /** * Builds a new plan for this agent * @param the agent to build the plan for * @return true if the plan builds successfully, false otherwise */ public GOAPPlan BuildPlan(GOAPGoal goal) { if (goal == null) { return(null); } //if (Owner.debugGOAP) Debug.Log(Time.timeSinceLevelLoad + " " + goal.ToString() + " - build plan"); //initialize shit Map.Initialise(Owner); Goal.Initialise(Owner, Map, goal); Storage.ResetStorage(Map); AStar.End = -1; AStar.RunAStar(Owner); AStarNode currNode = AStar.CurrentNode; if (currNode == null || currNode.NodeID == -1) { Debug.LogError(Time.timeSinceLevelLoad + " " + goal.ToString() + " - FAILED , no node "); return(null); //Building of plan failed } GOAPPlan plan = new GOAPPlan(); //create a new plan GOAPAction action; /** * We need to push each new plan step onto the list of steps until we reach the last action * Which is going to be the goal node and of no use */ //if (Owner.debugGOAP) Debug.Log(Time.timeSinceLevelLoad + " " + goal.ToString() + " current node id :" + currNode.NodeID); while (currNode.NodeID != -1) { action = Map.GetAction(currNode.NodeID); if (action == null) //If we tried to cast an node to an action that can't be done, quit out { Debug.LogError(Time.timeSinceLevelLoad + " " + goal.ToString() + ": canot find action (" + currNode.NodeID + ")"); return(null); } plan.PushBack(action); currNode = currNode.Parent; } //Finally tell the ai what its plan is if (plan.IsDone()) { Debug.LogError(Time.timeSinceLevelLoad + " " + goal.ToString() + ": plan is already done !!! (" + plan.CurrentStepIndex + "," + plan.NumberOfSteps + ")"); return(null); } return(plan); }
void CreatePlan(GOAPGoal goal) { GOAPPlan plan = BuildPlan(goal); if (plan == null) { if (Owner.debugGOAP) { Debug.Log(Time.timeSinceLevelLoad + " BUILD PLAN - " + goal.ToString() + " FAILED !!! " + Owner.WorldState.ToString(), Owner); } goal.SetDisableTime(); return; } if (CurrentGoal != null) { CurrentGoal.Deactivate(); CurrentGoal = null; } if (Owner.debugGOAP) { Debug.Log(Time.timeSinceLevelLoad + " BUILD " + goal.ToString() + " - " + plan.ToString() + " " + Owner.WorldState.ToString(), Owner); foreach (KeyValuePair <E_GOAPGoals, GOAPGoal> pair in Goals) { if (pair.Value != goal && pair.Value.GoalRelevancy > 0) { Debug.Log(pair.Value.ToString()); } } } CurrentGoal = goal; CurrentGoal.Activate(plan); }
void CreatePlan(GOAPGoal goal) { GOAPPlan plan = BuildPlan(goal); if (plan == null) { Debug.LogError(Time.timeSinceLevelLoad + " " + goal.ToString() + " - BUILD PLAN FAILED !!!"); return; } CurrentGoal = goal; Debug.Log(Time.timeSinceLevelLoad + " " + goal.ToString() + " - CurrentGoal !!!"); CurrentGoal.Activate(plan); }
void FindMostImportantGoal() { GOAPGoal newGoal = GetMostImportantGoal(CurrentGoal.GoalRelevancy); if (newGoal == null) { return; } if (newGoal == CurrentGoal) { //if (Owner.debugGOAP) Debug.Log(Time.timeSinceLevelLoad + " Current goal " + CurrentGoal.ToString() + ": " + "is most important still (" + newGoal.GoalRelevancy + ")"); return; } if (Owner.debugGOAP && CurrentGoal != null) { Debug.Log( Time.timeSinceLevelLoad + " More important goal (" + newGoal.ToString() + " >" + CurrentGoal.GoalRelevancy + ") " + Owner.WorldState.ToString(), Owner); } CreatePlan(newGoal); }
void FindNewGoal() { if (CurrentGoal != null) { CurrentGoal.Deactivate(); CurrentGoal = null; } while (CurrentGoal == null) { GOAPGoal newGoal = GetMostImportantGoal(0); if (newGoal == null) { break; } if (Owner.debugGOAP) { Debug.Log("Find new goal " + newGoal.ToString() + "WorldState - " + Owner.WorldState.ToString()); } CreatePlan(newGoal); if (CurrentGoal == null) { newGoal.SetDisableTime(); } } }
/** * Updates the current goal */ public void UpdateCurrentGoal() { if (CurrentGoal != null) { if (CurrentGoal.UpdateGoal()) { if (CurrentGoal.ReplanRequired()) { if (Owner.debugGOAP) { Debug.Log(Time.timeSinceLevelLoad + " " + CurrentGoal.ToString() + " - REPLAN required !!"); } ReplanCurrentGoal(); } if (CurrentGoal.IsPlanFinished()) { // goal is finished, so clear it and make new one if (Owner.debugGOAP) { Debug.Log(Time.timeSinceLevelLoad + " " + CurrentGoal.ToString() + " - FINISHED"); } CurrentGoal.Deactivate(); CurrentGoal = null; } } else // something bad happened, clear it { CurrentGoal.Deactivate(); CurrentGoal = null; } } }