protected override void NextState()
        {
            GoToNextState();

            //if i am not done running check if we need to be close to our target
            if (!IsDone())
            {
                I_GoapAction sm = (I_GoapAction)GetCurrentState();

                bool inrange = sm.RequiresInRange();
                if (inrange)
                {
                    Vector3 target = sm.GetTargetPosition();
                    m_NavAgent.SetTarget(target);
                    currentState = State.GOTO_ACTION;
                    Debug.Log("GOTO ACTION");
                }
                else
                {
                    EnterNextState();
                    currentState = State.EXECTUE_ACTION;
                }
            }
            else
            {
                currentState = State.NONE;
            }
        }
Esempio n. 2
0
        private static void ExecuteGoapRequest(GoapRequest _request)
        {
            //I_GoapAgent _agent, HashSet< string > _worldState, HashSet<string> _goalState, I_GoapActionSet _actionSet

            //Check is there an action which can just run on the current worldstate without building a tree?


            //sort by number of effects
            List <I_GoapAction> actions = new List <I_GoapAction>(_request.actions);

            ///////TODO!!! Sort by number of preconditions


            //this is returned
            List <I_GoapAction> acitonSequence = new List <I_GoapAction>();

            //check if one action can just satisfy

            /*for (int i = 0; i < actions.Count; i++)
             * {
             *  actions[i].ResetConditionCache();
             *  if (actions[i].GetPreConditions().IsSubsetOf(_worldState) && actions[i].IsProceduralConditionValid(_agent))
             *  {
             *      HashSet<string> newWorldState = new HashSet<string>();
             *      newWorldState.UnionWith(_worldState);
             *      newWorldState.UnionWith(actions[i].GetPostEffects());
             *
             *      if (_goalState.IsSubsetOf(newWorldState))
             *      {
             *          acitonSequence.Add(actions[i]);
             *          return acitonSequence;
             *      }
             *
             *  }
             * }*/


            List <GoapNode> leaves = new List <GoapNode>(); //leaves are the solutions

            GoapNode rootNode = new GoapNode(null, null, _request.worldState, 0);

            for (int i = 0; i < actions.Count; i++)
            {
                if (actions[i] == null)
                {
                    Debug.LogError("One of the ations is null!");
                }
                actions[i].ResetConditionCache();
            }


            bool success = BuildTree(_request, rootNode, leaves, actions, _request.goalState);


            if (success)
            {
                Debug.Log("Found solution");
            }


            //return goap actions with cheapest cost
            float smallestCost = float.MaxValue;
            int   index        = -1;

            // Debug.Log("found leaves: " + leaves.Count);
            for (int i = 0; i < leaves.Count; i++)
            {
                //Debug.Log("cost: " + leaves[i].runningCost);
                if (smallestCost > leaves[i].runningCost)
                {
                    index        = i;
                    smallestCost = leaves[i].runningCost;
                }
            }

            Debug.Log("Found Solutions: " + leaves.Count);

            if (leaves.Count > 0)
            {
                GoapNode currentNode = leaves[index];
                while (currentNode != null)
                {
                    if (currentNode.GetValue() != null)
                    {
                        I_GoapAction action = (I_GoapAction)currentNode.GetValue().Clone();
                        action.ActionHasBeenPicked();
                        acitonSequence.Insert(0, action);
                    }
                    currentNode = (GoapNode)currentNode.parent;
                }
            }

            //callback -> should be executed on the main thread if this is ever threaded!
            _request.callback?.Invoke(acitonSequence);
        }