示例#1
0
        // pursues the completion of the current goal and plan
        private void PursuePlan()
        {
            if (PursueCurrentPlan)               // there is a plan and a goal to pursue
            {
                if (!CheckGoalValidation())
                {
                    return;
                }                                                                       // if validation is not true stop here dont update

                _CurrentPlan.Peek().Action.UpdateAction(_CurrentPlan.Peek().GoalState); // update
                if (_CurrentPlan.Peek().Action.IsDone(_CurrentPlan.Peek().GoalState))   // if action is done or actions can not be
                {
                    GOAP_ActionState DequeAction = _CurrentPlan.Dequeue();
                    DequeAction.Action.ExitAction(_CurrentGoal.Goal);    // exit action

                    bool EnterPlan = OnEnterPlan();                      // call on enter of current plan
                    if (!EnterPlan)
                    {
                        SetNewPlan(null, null);
                    }                                                          // set empty plan
                }
            }
            else                 // there is no goal and plan to pursue
            {
                                #if DEBUG
                Debug.LogError("Trying to pursue a plan but there isnt any: " + this.gameObject.name + ". Create a new goal and plan. CurrentGoal: " + _CurrentGoal);
                                #endif
            }
        }
示例#2
0
        public GOAP_GraphNode(GOAP_State Left, GOAP_Agent Agent, GOAP_GraphNode Parent, GOAP_Action ConnectionAction, List <KeyValuePair <string, GOAP_State.PriorityValue> > LeftPreConditions = default(List <KeyValuePair <string, GOAP_State.PriorityValue> >))
        {
            this._Agent             = Agent;                // the agent of which to take its actions
            this._LeftConditions    = new GOAP_State(Left); // the goal state represents the state you want to achieve using the actions of the agent.
            this._Parent            = Parent;               // the parents effect is used to meet the preconditions of this node. If parent is equal to null that means its the beginning point of the search.
            this._LeftPreConditions = LeftPreConditions;

            // temp
            float G = 0;

            if (Parent != null && ConnectionAction != null)
            {
                G = ConnectionAction.Cost;                                             // set new cost
                _LeftConditions.RemoveConditions(ConnectionAction.Effects.Conditions); // remove effects from left state
                _LeftConditions += ConnectionAction.PreConditions;                     // add the pre conditions of the connection action

                _State = Parent.State + ConnectionAction.Effects;                      // the new state is the state of the parrent with the connection action effects applied

                if (Parent.ConnectionAction != null && Parent.ConnectionAction.Action != null)
                {
                    _State.ReplaceGenData(Parent.ConnectionAction.Action.PreConditions);                     // replace pre conditions gen data
                }
            }
            else
            {
                _State = Agent.Memory.State;                 // if parent is equal to null. It means this is the start node. The state of the start node is always the memory of the agent.
            }

            this._ConnectionAction = new GOAP_ActionState(ConnectionAction, new GOAP_State(this._State)); // the connection action is the action used to get from the parent state to this state.

            _H    = _LeftConditions.Count;                                                                // the heuristic is the count of conditions left to solve
            _Cost = G + _H;
        }