Beispiel #1
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);
        }

        return(plan);
    }
Beispiel #2
0
    /********************************************
    *			GETTERS/SETTERS
    ********************************************/

    public override float GetHeuristicDistance(AgentHuman ai, AStarNode aStarNode, bool firstRun)
    {
        //If we are on the first run then we know that we have no node so we need to setup the first node
        AStarGOAPNode node = (AStarGOAPNode)aStarNode;

        if (firstRun)
        {
            //Copy the WS satisfaction conditions to the nodes goal state
            node.GoalState.Reset();
            Goal.SetWSSatisfactionForPlanning(node.GoalState);
            Goal.ChangeCurrentWSForPlanning(node.CurrentState);
        }
        else
        {
            //Now we know that the node being checked is an action.
            //We need to get the action and apply it
            GOAPAction action = Map.GetAction(aStarNode.NodeID);
            //if(action.ValidateWSPreconditions(node.CurrentState,node.GoalState))
            {
                //if(action.ValidateWSEffects(ai,&node.CurrentState,&node.GoalState))
                {
                    //action.AddInvalidPreconditionsToGoal(ai,&node.CurrentState,&node.GoalState);
                    //action.ApplyWSEffects(ai,&node.CurrentState,&node.GoalState);
                    action.SolvePlanWSVariable(node.CurrentState, node.GoalState);
                    action.SetPlanWSPreconditions(node.GoalState);
                }
            }
        }

        //  if (Owner.debugGOAP) Debug.Log("GetHeuristicDistance1 : goal" + node.GoalState.ToString());
        //  if (Owner.debugGOAP) Debug.Log("GetHeuristicDistance1 : current" + node.CurrentState.ToString());

        MergeStates(ai, node.CurrentState, node.GoalState);

        //       if (Owner.debugGOAP) Debug.Log("GetHeuristicDistance2 : goal" + node.GoalState.ToString());
//        if (Owner.debugGOAP) Debug.Log("GetHeuristicDistance2 : current" + node.CurrentState.ToString());

        return(node.GoalState.GetNumWorldStateDifferences(node.CurrentState));
    }