Exemplo n.º 1
0
        public bool IsAchievable(GOAP_State GoalState, GOAP_Agent Agent)
        {
            if (GoalState.Conditions.Count <= 0)
            {
                return(false);
            }                                                                // goalstate is already reached

            GOAP_State CopyState = new GOAP_State(GoalState);

            for (int i = 0; i < Agent.Actions.Length; i++)
            {
                if (CopyState.Count <= 0)
                {
                    return(true);
                }                                                              // if goal state count is lower the zero then its achievable

                Agent.Actions [i].UpdateEffectsAndConditions(true, CopyState); // pre calculations done before checking
                if (!Agent.Actions[i].CheckProceduralUsablity(CopyState))
                {
                    continue;
                }                                                                                // check if action is runnable or not
                CopyState.RemoveConditions(Agent.Actions[i].Effects.Conditions);                 // remove conditions of state based on effects of actions
            }
            return(CopyState.Count <= 0);
        }
Exemplo n.º 2
0
        public void Plan(GOAP_Goal Goal, GOAP_Agent Agent, System.Action <Queue <GOAP_ActionState>, GOAP_Goal> OnDone)
        {
            GOAP_State GoalState = Goal.Goal.RemoveConditions(Agent.Memory.State.Conditions); // remove already satisfied goals based on agent memory

            if (IsAchievable(GoalState, Agent))                                               // first check wheter with the given actions the disired goal can be achived before forcing it with a*
            {
                GOAP_Searcher            Searcher = new GOAP_Searcher();                      // create new A* GOAP searches
                Queue <GOAP_ActionState> NewPlan  = Searcher.Solve <GOAP_ActionState>(
                    new GOAP_Graph(),                                                         // initialize new goap graph
                    (GOAP_GraphNode Node) => {
                    return(Node.ConnectionAction);
                },
                    new GOAP_GraphNode(GoalState, Agent, null, null),          // start state is the goal cause of backwards searching
                    new GOAP_GraphNode(Agent.Memory.State, Agent, null, null), // goal state is the current state cuase of backwards searching
                    false,
                    false,                                                     // include start in the que
                    500                                                        // max 500 iterations before force exit
                    );                                                         // solve plan finding

                if (NewPlan != null)
                {
                    OnDone.Invoke(NewPlan, Goal); return;
                }                                                                            // created plan invoke
            }

            // could not find a plan
            OnDone.Invoke(null, Goal);
        }
Exemplo n.º 3
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;
        }
Exemplo n.º 4
0
        public void SetSensors(GOAP_Sensor[] Sens, GOAP_Agent Agent)
        {
            Sensors = Sens;

            if (Sens != null)               // initialize sensors if there are any
            {
                for (int i = 0; i < Sensors.Length; i++)
                {
                    Sensors [i].Initialize(Agent);
                }
            }
        }
Exemplo n.º 5
0
 public GOAP_Action(GOAP_Agent Agent)
 {
     this._Agent = Agent;
 }
Exemplo n.º 6
0
 public void Initialize(GOAP_Agent Agent)
 {
     this._Memory = Agent.Memory;
     this._Agent  = Agent;
     Awake();
 }
Exemplo n.º 7
0
 public GOAP_Goal(GOAP_Agent Agent)
 {
     this._Agent = Agent;             // set Goap agent
 }