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; }
private void SetNeededResources(IReGoapActionSettings <string, object> settings) { var thisSettings = (GatherResourceSettings)settings; resourcePosition = thisSettings.ResourcePosition; resource = thisSettings.Resource; }
public virtual void Run(IReGoapActionSettings settings, Action <ReGoapAction> done, Action <ReGoapAction> fail) { enabled = true; doneCallback = done; failCallback = fail; this.settings = settings; }
public ReGoapActionState(IReGoapAction action, IReGoapActionSettings settings) { Action = action; Settings = settings; #if DEBUG isValid = true; #endif }
public ReGoapNode(IGoapPlanner planner, ReGoapState newGoal, ReGoapNode parent, IReGoapAction action) { this.planner = planner; this.parent = parent; this.action = action; if (action != null) { actionSettings = action.GetSettings(planner.GetCurrentAgent(), goal); } if (this.parent != null) { state = this.parent.GetState(); // g(node) g = parent.GetPathCost(); } else { state = planner.GetCurrentAgent().GetMemory().GetWorldState(); } var nextAction = parent == null ? null : parent.action; if (action != null) { // backward search does NOT support negative preconditions // since in backward search we relax the problem all preconditions are valid but are added to the current goal var preconditions = action.GetPreconditions(newGoal, nextAction); goal = newGoal + preconditions; var effects = action.GetEffects(newGoal, nextAction); state += effects; g += action.GetCost(newGoal, nextAction); // removing current action effects from goal, no need to do with to the whole state // since the state is the sum of all the previous actions's effects. var missingState = new ReGoapState(); goal.MissingDifference(effects, ref missingState); goal = missingState; // this is needed every step to make sure that any precondition is not already satisfied // by the world state var worldMissingState = new ReGoapState(); goal.MissingDifference(planner.GetCurrentAgent().GetMemory().GetWorldState(), ref worldMissingState); goal = worldMissingState; } else { var diff = new ReGoapState(); newGoal.MissingDifference(state, ref diff); goal = diff; } h = goal.Count; // f(node) = g(node) + h(node) cost = g + h * heuristicMultiplier; }
public virtual void Run(IReGoapAction previous, IReGoapAction next, IReGoapActionSettings settings, ReGoapState goalState, Action <IReGoapAction> done, Action <IReGoapAction> fail) { interruptWhenPossible = false; enabled = true; doneCallback = done; failCallback = fail; previousAction = previous; nextAction = next; }
private void Init(IGoapPlanner <T, W> planner, ReGoapState <T, W> newGoal, ReGoapNode <T, W> parent, IReGoapAction <T, W> action) { expandList.Clear(); this.planner = planner; this.parent = parent; this.action = action; if (action != null) { actionSettings = action.GetSettings(planner.GetCurrentAgent(), newGoal); } if (parent != null) { state = parent.GetState().Clone(); // g(node) g = parent.GetPathCost(); } else { state = planner.GetCurrentAgent().GetMemory().GetWorldState().Clone(); } var nextAction = parent == null ? null : parent.action; if (action != null) { // since in backward search we relax the problem all preconditions are valid but are added to the current goal var preconditions = action.GetPreconditions(newGoal, nextAction); goal = newGoal + preconditions; var effects = action.GetEffects(newGoal, nextAction); state.AddFromState(effects); g += action.GetCost(newGoal, nextAction); // removing current action effects from goal, no need to do with to the whole state // since the state is the sum of all the previous actions's effects. goal.ReplaceWithMissingDifference(effects); // this is needed every step to make sure that any precondition is not already satisfied // by the world state goal.ReplaceWithMissingDifference(planner.GetCurrentAgent().GetMemory().GetWorldState()); } else { var diff = ReGoapState <T, W> .Instantiate(); newGoal.MissingDifference(state, ref diff); goal = diff; } h = goal.Count; // f(node) = g(node) + h(node) cost = g + h * heuristicMultiplier; }
public virtual IEnumerator Run(IReGoapAction previous, IReGoapAction next, IReGoapActionSettings settings, BGoapState goalState, Action <IReGoapAction> done, Action <IReGoapAction> fail) { interruptWhenPossible = false; enabled = true; doneCallback = done; failCallback = fail; previousAction = previous; nextAction = next; yield return(null); }
private void Init(IGoapPlanner <T, W> planner, ReGoapState <T, W> newGoal, ReGoapNode <T, W> parent, IReGoapAction <T, W> action) { expandList.Clear(); this.planner = planner; this.parent = parent; this.action = action; if (action != null) { actionSettings = action.GetSettings(planner.GetCurrentAgent(), newGoal); } if (parent != null) { state = parent.GetState().Clone(); // g(node) g = parent.GetPathCost(); } else { state = planner.GetCurrentAgent().GetMemory().GetWorldState().Clone(); } var nextAction = parent == null ? null : parent.action; if (action != null) { // create a new instance of the goal based on the paren't goal goal = ReGoapState <T, W> .Instantiate(newGoal); var preconditions = action.GetPreconditions(goal, nextAction); var effects = action.GetEffects(goal, nextAction); // adding the action's effects to the current node's state state.AddFromState(effects); // addding the action's cost to the node's total cost g += action.GetCost(goal, nextAction); // add all preconditions of the current action to the goal goal.AddFromState(preconditions); // removes from goal all the conditions that are now fullfiled in the node's state goal.ReplaceWithMissingDifference(state); } else { var diff = ReGoapState <T, W> .Instantiate(); newGoal.MissingDifference(state, ref diff); goal = diff; } h = goal.Count; // f(node) = g(node) + h(node) cost = g + h * heuristicMultiplier; }
public virtual void Run(IReGoapAction <T, W> previous, IReGoapAction <T, W> next, IReGoapActionSettings <T, W> settings, ReGoapState <T, W> goalState, Action <IReGoapAction <T, W> > done, Action <IReGoapAction <T, W> > fail) { interruptWhenPossible = false; enabled = true; doneCallback = done; failCallback = fail; this.settings = settings; previousAction = previous; nextAction = next; }
public override sealed BGoapState GetPreconditions(BGoapState goalState, IReGoapActionSettings settings, IReGoapAction next = null) { BGoapState variablePreconditions = GetPreconditionsFromGoal(goalState, settings as Settings); if (variablePreconditions == null || variablePreconditions.IsEmpty()) { return(staticPreconditions); } else { return(staticPreconditions.Union(variablePreconditions)); } }
public override void Run(IReGoapAction previous, IReGoapAction next, IReGoapActionSettings settings, ReGoapState goalState, Action <IReGoapAction> done, Action <IReGoapAction> fail) { base.Run(previous, next, settings, goalState, done, fail); SetNeededResources(settings); if (resource == null || resource.GetCapacity() < ResourcePerAction) { failCallback(this); } else { gatherCooldown = Time.time + TimeToGather; } }
public override void Run(IReGoapAction previous, IReGoapAction next, IReGoapActionSettings settings, ReGoapState goalState, Action <IReGoapAction> done, Action <IReGoapAction> fail) { base.Run(previous, next, settings, goalState, done, fail); SetObjective(settings); if (objectivePosition != default(Vector3)) { smsGoto.GoTo(objectivePosition, OnDoneMovement, OnFailureMovement); } else { failCallback(this); } }
public override void Run(IReGoapAction previous, IReGoapAction next, IReGoapActionSettings settings, ReGoapState goalState, Action <IReGoapAction> done, Action <IReGoapAction> fail) { base.Run(previous, next, settings, goalState, done, fail); var workstation = agent.GetMemory().GetWorldState().Get <Workstation>("nearestWorkstation"); if (workstation.CraftResource(resourcesBag, recipe)) { ReGoapLogger.Log("[CraftRecipeAction] crafted recipe " + recipe.GetCraftedResource()); done(this); } else { fail(this); } }
public override void Run(IReGoapAction previous, IReGoapAction next, IReGoapActionSettings settings, ReGoapState goalState, Action <IReGoapAction> done, Action <IReGoapAction> fail) { base.Run(previous, next, settings, goalState, done, fail); this.settings = (AddResourceToBankSettings)settings; var bank = agent.GetMemory().GetWorldState().Get <Bank>("nearestBank"); if (bank.AddResource(resourcesBag, ((AddResourceToBankSettings)settings).ResourceName)) { done(this); } else { fail(this); } }
public override void Run(IReGoapActionSettings settings, Action <ReGoapAction> done, Action <ReGoapAction> fail) { base.Run(settings, done, fail); GenericGoToSettings localSettings = (GenericGoToSettings)settings; if (localSettings.ObjectivePosition.HasValue) { smsGoto.GoTo(localSettings.ObjectivePosition, OnDoneMovement, OnFailureMovement); } else { failCallback(this); } }
public override void Run(IReGoapAction previous, IReGoapAction next, IReGoapActionSettings settings, ReGoapState goalState, Action <IReGoapAction> done, Action <IReGoapAction> fail) { base.Run(previous, next, settings, goalState, done, fail); SetNeededResources(settings); if (resource == null || resource.GetCapacity() < ResourcePerAction) { failCallback(this); } else { ReGoapLogger.Log("[GatherResourceAction] acquired " + ResourcePerAction + " " + resource.GetName()); resource.RemoveResource(ResourcePerAction); bag.AddResource(resource.GetName(), ResourcePerAction); doneCallback(this); } }
public override void Run(IReGoapActionSettings settings, Action <ReGoapAction> done, Action <ReGoapAction> fail) { base.Run(settings, done, fail); ReGoapState reGoapState = reGoapAgent.GetWorldState(); var workstation = reGoapState.Get("nearestWorkstation") as Workstation; if (workstation != null && workstation.CraftResource(resourcesBag, recipe)) { done(this); } else { fail(this); } }
public override void Run(IReGoapActionSettings settings, Action <ReGoapAction> done, Action <ReGoapAction> fail) { base.Run(settings, done, fail); this.settings = (AddResourceToBankSettings)settings; ReGoapState reGoapState = reGoapAgent.GetWorldState(); Bank bank = reGoapState.Get("nearestBank") as Bank; if (bank != null && bank.AddResource(resourcesBag, ((AddResourceToBankSettings)settings).ResourceName)) { done(this); } else { fail(this); } }
public override void Run(IReGoapActionSettings settings, Action <ReGoapAction> done, Action <ReGoapAction> fail) { base.Run(settings, done, fail); var thisSettings = (GatherResourceSettings)settings; resourcePosition = thisSettings.ResourcePosition; resource = thisSettings.Resource; if (resource == null || resource.GetCapacity() < ResourcePerAction) { failCallback(this); } else { gatherCooldown = Time.time + TimeToGather; } }
public override void Run(IReGoapAction <string, object> previous, IReGoapAction <string, object> next, IReGoapActionSettings <string, object> settings, ReGoapState <string, object> goalState, Action <IReGoapAction <string, object> > done, Action <IReGoapAction <string, object> > fail) { base.Run(previous, next, settings, goalState, done, fail); this.settings = (AddResourceToBankSettings)settings; var bank = agent.GetMemory().GetWorldState().Get("nearestBank") as Bank; if (bank != null && bank.AddResource(resourcesBag, ((AddResourceToBankSettings)settings).ResourceName)) { done(this); } else { fail(this); } }
public override void Run(IReGoapAction <string, object> previous, IReGoapAction <string, object> next, IReGoapActionSettings <string, object> settings, ReGoapState <string, object> goalState, Action <IReGoapAction <string, object> > done, Action <IReGoapAction <string, object> > fail) { base.Run(previous, next, settings, goalState, done, fail); var localSettings = (GenericGoToSettings)settings; if (localSettings.ObjectivePosition.HasValue) { smsGoto.GoTo(localSettings.ObjectivePosition, OnDoneMovement, OnFailureMovement); } else { failCallback(this); } }
public override sealed IEnumerator Run(IReGoapAction previousAction, IReGoapAction nextAction, IReGoapActionSettings settingsParam, BGoapState goalState, Action <IReGoapAction> done, Action <IReGoapAction> fail) { Settings settings = settingsParam as Settings; IEnumerator <SimpleActionExecutionControlElements> progress = Execute(settings, () => { fail(this); }); while (progress.MoveNext()) { if (settings.interruptNextChanceYouHave && progress.Current != SimpleActionExecutionControlElements.CANNOT_INTERRUPT) { done(this); yield break; } if (progress.Current == SimpleActionExecutionControlElements.WAIT_NEXT_FRAME) { yield return(new WaitForFixedUpdate()); } yield return(progress.Current); } done(this); }
private void Init(IGoapPlanner <T, W> planner, ReGoapState <T, W> newGoal, ReGoapNode <T, W> parent, IReGoapAction <T, W> action) { expandList.Clear(); tmpKeys.Clear(); this.planner = planner; this.parent = parent; this.action = action; if (action != null) { actionSettings = action.GetSettings(planner.GetCurrentAgent(), newGoal); } if (parent != null) { state = parent.GetState().Clone(); // g(node) g = parent.GetPathCost(); } else { state = planner.GetCurrentAgent().GetMemory().GetWorldState().Clone(); } var nextAction = parent == null ? null : parent.action; if (action != null) { // create a new instance of the goal based on the paren't goal goal = ReGoapState <T, W> .Instantiate(); var tmpGoal = ReGoapState <T, W> .Instantiate(newGoal); var preconditions = action.GetPreconditions(tmpGoal, nextAction); var effects = action.GetEffects(tmpGoal, nextAction); // adding the action's effects to the current node's state state.AddFromState(effects); // addding the action's cost to the node's total cost g += action.GetCost(tmpGoal, nextAction); //// add all preconditions of the current action to the goal //tmpGoal.AddFromState(preconditions); //// removes from goal all the conditions that are now fulfilled in the node's state //tmpGoal.ReplaceWithMissingDifference(state); ////goal.ReplaceWithMissingDifference(effects); // collect all keys from goal & precondition, unique-ed foreach (var pr in tmpGoal.GetValues()) { var k = pr.Key; if (!tmpKeys.Contains(k)) { tmpKeys.Add(k); } } foreach (var pr in preconditions.GetValues()) { var k = pr.Key; if (!tmpKeys.Contains(k)) { tmpKeys.Add(k); } } // process each keys foreach (var k in tmpKeys) { StructValue goalValue, effectValue, precondValue, stateValue, protoValue; tmpGoal.GetValues().TryGetValue(k, out goalValue); effects.GetValues().TryGetValue(k, out effectValue); preconditions.GetValues().TryGetValue(k, out precondValue); state.GetValues().TryGetValue(k, out stateValue); StructValue.EValueType valueType; _GetValueType(ref goalValue, ref effectValue, ref precondValue, ref stateValue, out valueType, out protoValue); if (valueType == StructValue.EValueType.Arithmetic) { //_EnsureArithStructValueInited(ref goalValue, ref protoValue); _EnsureArithStructValueInited(ref effectValue, ref protoValue); _EnsureArithStructValueInited(ref precondValue, ref protoValue); _EnsureArithStructValueInited(ref stateValue, ref protoValue); if (!goalValue.Inited) { goalValue = StructValue.CopyCreate(ref stateValue, -(Convert.ToSingle(stateValue.v) - Convert.ToSingle(effectValue.v))); } float fGoal = Convert.ToSingle(goalValue.v); float fEffect = Convert.ToSingle(effectValue.v); float fPrecond = Convert.ToSingle(precondValue.v); float fState = Convert.ToSingle(stateValue.v); float finalV = Math.Max( fGoal - fEffect, Math.Min(fPrecond, fPrecond - fState) ); var sv = StructValue.CopyCreate(ref protoValue, finalV); goal.SetStructValue(k, sv); } else if (valueType == StructValue.EValueType.Other) { //ReplaceWithMissingDifference if (stateValue.Inited && goalValue.Inited && goalValue.IsFulfilledBy(stateValue)) { goalValue.Invalidate(); } // AddFromPrecond // 1. if the precond is satisfied by the memory start state, then discard // 2. else this newly added goal from precond, should not be removed due to fulfilled by curStateValue if (precondValue.Inited) { bool preCondfulfilledByMem = false; var startMemoryState = planner.GetCurrentAgent().GetMemory().GetWorldState(); StructValue startMemoryValue; if (startMemoryState.GetValues().TryGetValue(k, out startMemoryValue)) { if (startMemoryValue.Inited && precondValue.IsFulfilledBy(startMemoryValue)) { preCondfulfilledByMem = true; } } if (!preCondfulfilledByMem) { if (goalValue.Inited) { goalValue = goalValue.MergeWith(precondValue); } else { goalValue = precondValue; } } } if (goalValue.Inited) { goal.SetStructValue(k, goalValue); } } else { UnityEngine.Debug.LogError("Unexpected StructValue type: " + valueType); } }// foreach (var k in tmpKeys) tmpGoal.Recycle(); } else { var diff = ReGoapState <T, W> .Instantiate(); newGoal.MissingDifference(state, ref diff); goal = diff; } h = _CalculateH(); // f(node) = g(node) + h(node) cost = g + h * planner.GetSettings().HeuristicMultiplier; }
public override void Run(IReGoapAction <string, object> previous, IReGoapAction <string, object> next, IReGoapActionSettings <string, object> settings, ReGoapState <string, object> goalState, Action <IReGoapAction <string, object> > done, Action <IReGoapAction <string, object> > fail) { base.Run(previous, next, settings, goalState, done, fail); var workstation = agent.GetMemory().GetWorldState().Get("nearestWorkstation") as Workstation; if (workstation != null && workstation.CraftResource(resourcesBag, recipe)) { ReGoapLogger.Log("[CraftRecipeAction] crafted recipe " + recipe.GetCraftedResource()); done(this); } else { fail(this); } }
public override sealed void AskForInterruption(IReGoapActionSettings settings) { (settings as SimpleActionSettings).interruptNextChanceYouHave = true; }
public abstract void AskForInterruption(IReGoapActionSettings settings);
private void Init(ReGoapAgent agent, ReGoapState newGoalState, ReGoapNode parent, ReGoapAction action) { expandList.Clear(); ReGoapState goal = null; this.reGoapAgent = agent; this.parentNode = parent; this.action = action; if (action != null) { actionSettings = action.GetSettings(newGoalState); } if (parentNode != null) { agentReGoapState = parentNode.GetState().Clone(); g = parentNode.GetPathCost(); } else { ReGoapState reGoapState = agent.GetWorldState(); agentReGoapState = reGoapState.Clone(); } if (action != null) { // create a new instance of the goal based on the paren't goal goal = ReGoapState.Instantiate(newGoalState); var preconditions = action.GetPreconditions(goal); var effects = action.GetEffects(goal); // adding the action's effects to the current node's state agentReGoapState.AddFromState(effects); // addding the action's cost to the node's total cost g += action.GetCost(); // add all preconditions of the current action to the goal goal.AddFromState(preconditions); // removes from goal all the conditions that are now fullfiled in the node's state goal.ReplaceWithMissingDifference(agentReGoapState); } else { goal = newGoalState.MissingDifference(agentReGoapState); } h = goal.Count; cost = g + h; //Expand(goal); expandList.Clear(); List <ReGoapAction> actionsList = reGoapAgent.GetActionsSet(); for (var index = actionsList.Count - 1; index >= 0; index--) { ReGoapAction possibleAction = actionsList[index]; if (!possibleAction.CheckProceduralCondition()) // 执行条件不满足排除掉 { continue; } ReGoapState precond = possibleAction.GetPreconditions(goal); ReGoapState effects = possibleAction.GetEffects(goal); if (!ReGoapState.HasAny(effects, goal)) // any effect is the current goal { continue; } if (!ReGoapState.HasAnyConflict(precond, goal)) { ReGoapNode reGoapNode = new ReGoapNode(reGoapAgent, goal, this, possibleAction); expandList.Add(reGoapNode); } } }
public abstract bool CheckProceduralCondition(IReGoapAgent goapAgent, IReGoapActionSettings settings, BGoapState goalState, IReGoapAction nextAction = null);
public override void Run(IReGoapAction <string, object> previous, IReGoapAction <string, object> next, IReGoapActionSettings <string, object> settings, ReGoapState <string, object> goalState, Action <IReGoapAction <string, object> > done, Action <IReGoapAction <string, object> > fail) { base.Run(previous, next, settings, goalState, done, fail); done(this); }