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); }
// When done with planning private void OnDonePlanning(Queue <GOAP_ActionState> Plan, GOAP_Goal Goal) { IsPlanning = false; if (Plan != null && Plan.Count > 0 && Goal != null) { SetNewPlan(Plan, Goal); return; // find a purpose } SetNewPlan(null, null); }
// finds the most valid goal at the moment and creates a plan to complete it private void CreateNewPlan() { if (_CurrentPlan != null && _CurrentPlan.Count > 0) { _CurrentPlan.Peek().Action.ExitAction(_CurrentPlan.Peek().GoalState); } List <GOAP_Goal> PriorGoals = PrioritizedGoals; // get current available goals sorted by priority if (PriorGoals != null && PriorGoals.Count > 0) // there are available goals found { PlanGoal(PriorGoals[0]); // get first prior goal and plan it } else // no available goals are found { _CurrentGoal = null; _CurrentPlan = null; #if DEBUG Debug.LogError("No available goals found in agent: " + this.gameObject.name); #endif NoAvailableGoals(); // run no available goals function } }
private void SetNewPlan(Queue <GOAP_ActionState> Plan, GOAP_Goal Goal) { _CurrentGoal = Goal; // set current goal _CurrentPlan = Plan; // set current plan OnEnterPlan(); }
// plan a new plan private void PlanGoal(GOAP_Goal TargetGoal) { IsPlanning = true; // set isplanning to true cause the Planner is planning a plan TargetGoal.PreCalculations(); // do pre calculations before calculating path to goal state GOAP_Planner.I.Plan(TargetGoal, this, OnDonePlanning); // plan a new plan }