Beispiel #1
0
    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);
    }
Beispiel #2
0
    /**
     * 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);
        }

        Debug.LogError(goal);
        Debug.LogError(plan.ToString());
        return(plan);
    }