protected virtual void PushAction() { if (interruptOnNextTransistion) { CalculateNewGoal(); return; } var plan = currentGoal.GetPlan(); if (plan.Count == 0) { if (currentActionState != null) { currentActionState.Action.Exit(currentActionState.Action); } currentActionState = null; CalculateNewGoal(); } else { var previous = currentActionState; currentActionState = plan.Dequeue(); IReGoapAction next = null; if (plan.Count > 0) { next = plan.Peek().Action; } if (previous != null) { previous.Action.Exit(currentActionState.Action); } StartCoroutine(currentActionState.Action.Run(previous != null ? previous.Action : null, next, currentActionState.Settings, currentGoal.GetGoalState(), WarnActionEnd, WarnActionFailure)); } }
protected virtual void OnDonePlanning(IReGoapGoal newGoal) { currentPlanWorker = null; if (newGoal == null) { if (currentGoal == null) { ReGoapLogger.LogWarning("GoapAgent " + this + " could not find a plan."); } return; } if (currentActionState != null) { currentActionState.Action.Exit(null); } currentActionState = null; currentGoal = newGoal; var plan = currentGoal.GetPlan(); startingPlan = plan.ToList(); ClearPlanValues(); foreach (var actionState in startingPlan) { actionState.Action.PostPlanCalculations(this); } currentGoal.Run(WarnGoalEnd); PushAction(); }
protected virtual void OnDonePlanning(IReGoapGoal <T, W> newGoal) { startedPlanning = false; currentReGoapPlanWorker = default(ReGoapPlanWork <T, W>); if (newGoal == null) { if (currentGoal == null) { ReGoapLogger.LogWarning("GoapAgent " + this + " could not find a plan."); } return; } if (currentActionState != null) { currentActionState.Action.Exit(null); } currentActionState = null; currentGoal = newGoal; if (startingPlan != null) { for (int i = 0; i < startingPlan.Count; i++) { startingPlan[i].Action.PlanExit(i > 0 ? startingPlan[i - 1].Action : null, i + 1 < startingPlan.Count ? startingPlan[i + 1].Action : null, startingPlan[i].Settings, currentGoal.GetGoalState()); } } startingPlan = currentGoal.GetPlan().ToList(); ClearPlanValues(); for (int i = 0; i < startingPlan.Count; i++) { startingPlan[i].Action.PlanEnter(i > 0 ? startingPlan[i - 1].Action : null, i + 1 < startingPlan.Count ? startingPlan[i + 1].Action : null, startingPlan[i].Settings, currentGoal.GetGoalState()); } currentGoal.Run(WarnGoalEnd); PushAction(); }
public BGoapNode(IGoapPlanner planner, BGoapState parentGoal, BGoapNode parent, ReGoapActionState actionState) { this.planner = planner; this.parent = parent; if(actionState != null) { this.action = actionState.Action; this.actionSettings = actionState.Settings; } if (this.parent != null){ g = parent.GetPathCost(); } var nextAction = parent == null ? null : parent.action; if(action != null) { //first step - subtract effects of action var effects = action.GetEffects( parentGoal, actionSettings, nextAction ); try { goal = parentGoal.Difference( effects, false ); //dont use defaults here, only subtract what really is in the effect } catch(ArgumentException e) { Debug.Log( e ); } //then add preconditions to the current goal state var preconditions = action.GetPreconditions( parentGoal, actionSettings, nextAction ); goal = goal.Union( preconditions ); g += action.GetCost( parentGoal, actionSettings, nextAction ); } else goal = parentGoal; h = goal.Distance( planner.GetCurrentAgent().GetMemory().GetWorldState() ); // f(node) = g(node) + h(node) cost = g + h * heuristicMultiplier; }
protected virtual void OnDisable() { if (currentActionState != null) { currentActionState.Action.Exit(null); currentActionState = null; currentGoal = null; } }
protected virtual void TryWarnActionFailure(ReGoapActionState actionState) { if (actionState.Action.IsInterruptable()) { WarnActionFailure(actionState.Action); } else { actionState.Action.AskForInterruption(actionState.Settings); } }