protected virtual bool CanDoNow(GoapAgent agent, IStateful target) { var worldState = WorldState.pool.Borrow(); worldState.Clear(); worldState[agent] = agent.GetState(); worldState[target] = target.GetState(); var conditions = GetDependentPreconditions(agent, target); bool result = GoapPlanner.DoConditionsApplyToWorld(conditions, worldState); WorldState.pool.Return(worldState); return(result); }
private void IdleState(FSM fsm, GameObject gameObj) { // GOAP planning. // Get the goal we want to plan for. var goal = CreateGoalState(); // Plan. var plan = GoapPlanner.Plan(this, availableActions, goal); if (plan != null) { // We have a plan, hooray! // Clear old plan. while (currentActions.Count > 0) { var context = currentActions.Dequeue(); GoapAction.WithContext.pool.Return(context); } currentActions = plan; PlanFound(goal, plan); #if DEBUG_PLAN currentPlan.Clear(); foreach (var action in currentActions) { currentPlan.Add(action.actionData.name + " " + (action.target as Component).name); } #endif // Move to PerformAction state. fsm.PopState(); fsm.PushState(PerformActionState); } else { // Couldn't get a plan. Debug.Log("<color=orange>Failed Plan:</color>" + PrettyPrint(goal)); PlanFailed(goal); // Move back to IdleAction state. fsm.PopState(); fsm.PushState(IdleState); } }
private void IdleState(FSM fsm) { // GOAP planning. // Get the goal we want to plan for. var goal = CreateGoalState(); // Plan. var plan = regressiveSearch ? GoapRegressiveSearchPlanner.Plan(this, goal) : GoapPlanner.Plan(this, goal); if (plan != null) { // We have a plan, hooray! // Clear old plan. while (currentActions.Count > 0) { var context = currentActions.Dequeue(); context.ReturnSelf(); } currentActions = plan; PlanFound(goal, plan); #if DEBUG_PLAN currentPlan.Clear(); foreach (GoapAction.WithContext action in currentActions) { string targetName = ""; if (action.target != null && action.target.ToString() != "Null" && action.target.ToString() != "null") { targetName = (action.target as Component).name; } currentPlan.Add(action.actionData.name + " " + targetName); } #endif // Move to PerformAction state. fsm.PopState(); fsm.PushState(PerformActionState); } else { // Couldn't get a plan. Debug.Log("<color=orange>Failed Plan:</color>" + goal); PlanFailed(goal); // Move back to IdleAction state. fsm.PopState(); fsm.PushState(IdleState); } }