public override void Run(IReGoapAction <string, object> previous, IReGoapAction <string, object> next, ReGoapState <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); model.movementSpeed = 10f; if (model.Target != null) { doneCallback(this); } else { failCallback(this); } }
public bool IsGoal(ReGoapState <T, W> goal) { return(goalMergedWithWorld.Count <= 0); }
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(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, ReGoapState <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); // do your own game logic here if (GetComponent <BuilderMemory>().remainingBlocks <= 0) { agent.GetMemory().GetWorldState().Set("BlockAvailable", false); failCallback(this); } else { GetComponent <BuilderMemory>().remainingBlocks--; doneCallback(this); // this will tell the ReGoapAgent that the action is successfully done and go ahead in the action plan } }
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 IReGoapGoal <T, W> Plan(IReGoapAgent <T, W> agent, IReGoapGoal <T, W> blacklistGoal = null, Queue <ReGoapActionState <T, W> > currentPlan = null, Action <IReGoapGoal <T, W> > callback = null) { if (ReGoapLogger.Level == ReGoapLogger.DebugLevel.Full) { ReGoapLogger.Log("[ReGoalPlanner] Starting planning calculation for agent: " + agent); } goapAgent = agent; Calculated = false; currentGoal = null; var possibleGoals = new List <IReGoapGoal <T, W> >(); foreach (var goal in goapAgent.GetGoalsSet()) { if (goal == blacklistGoal) { continue; } goal.Precalculations(this); if (goal.IsGoalPossible()) { possibleGoals.Add(goal); } } possibleGoals.Sort((x, y) => x.GetPriority().CompareTo(y.GetPriority())); var currentState = agent.GetMemory().GetWorldState(); while (possibleGoals.Count > 0) { currentGoal = possibleGoals[possibleGoals.Count - 1]; possibleGoals.RemoveAt(possibleGoals.Count - 1); var goalState = currentGoal.GetGoalState(); // can't work with dynamic actions, of course if (!settings.UsingDynamicActions) { var wantedGoalCheck = currentGoal.GetGoalState(); GoapActionStackData <T, W> stackData; stackData.agent = goapAgent; stackData.currentState = currentState; stackData.goalState = goalState; stackData.next = null; stackData.settings = null; // we check if the goal can be archived through actions first, so we don't brute force it with A* if we can't foreach (var action in goapAgent.GetActionsSet()) { action.Precalculations(stackData); if (!action.CheckProceduralCondition(stackData)) { continue; } // check if the effects of all actions can archieve currentGoal var previous = wantedGoalCheck; wantedGoalCheck = ReGoapState <T, W> .Instantiate(); previous.MissingDifference(action.GetEffects(stackData), ref wantedGoalCheck); } // finally push the current world state var current = wantedGoalCheck; wantedGoalCheck = ReGoapState <T, W> .Instantiate(); current.MissingDifference(GetCurrentAgent().GetMemory().GetWorldState(), ref wantedGoalCheck); // can't validate goal if (wantedGoalCheck.Count > 0) { currentGoal = null; continue; } } goalState = goalState.Clone(); var leaf = (ReGoapNode <T, W>)astar.Run( ReGoapNode <T, W> .Instantiate(this, goalState, null, null, null), goalState, settings.MaxIterations, settings.PlanningEarlyExit); if (leaf == null) { currentGoal = null; continue; } var result = leaf.CalculatePath(); if (currentPlan != null && currentPlan == result) { currentGoal = null; break; } if (result.Count == 0) { currentGoal = null; continue; } currentGoal.SetPlan(result); break; } Calculated = true; if (callback != null) { callback(currentGoal); } if (currentGoal != null) { ReGoapLogger.Log(string.Format("[ReGoapPlanner] Calculated plan for goal '{0}', plan length: {1}", currentGoal, currentGoal.GetPlan().Count)); if (ReGoapLogger.Level == ReGoapLogger.DebugLevel.Full) { int i = 0; GoapActionStackData <T, W> stackData; stackData.agent = agent; stackData.currentState = currentState; stackData.goalState = currentGoal.GetGoalState(); stackData.next = null; foreach (var action in currentGoal.GetPlan()) { stackData.settings = action.Settings; ReGoapLogger.Log(string.Format("[ReGoapPlanner] {0}) {1}", i++, action.Action.ToString(stackData))); } } } else { ReGoapLogger.LogWarning("[ReGoapPlanner] Error while calculating plan."); } return(currentGoal); }
public override ReGoapState <string, object> GetPreconditions(ReGoapState <string, object> goalState, IReGoapAction <string, object> next = null) { preconditions.Set("isAtPosition", agent.GetMemory().GetWorldState().Get("nearestWorkstationPosition") as Vector3?); return(preconditions); }
protected virtual void Awake() { reGoapState = ReGoapState.Instantiate(); }
public void SetPreconditions(ReGoapState <string, object> preconditions) { this.preconditions = preconditions; }
public override void Run(IReGoapAction <string, object> previous, IReGoapAction <string, object> next, ReGoapState <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); Debug.Log("Find a Tree"); transform.position = treePos.position; StartCoroutine(WaitASec()); }
public void SetEffects(ReGoapState <string, object> effects) { this.effects = effects; }
public override void Run(IReGoapAction <string, object> previous, IReGoapAction <string, object> next, ReGoapState <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); print("grass"); doneCallback(this); }
protected virtual void Awake() { goal = new ReGoapState(); }
public virtual bool CheckProceduralCondition(IReGoapAgent goapAgent, ReGoapState goalState, IReGoapAction next = null) { return(true); }
public override void Run(IReGoapAction <string, object> previous, IReGoapAction <string, object> next, ReGoapState <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 thisSettings = settings; resourcePosition = (Vector3)thisSettings.Get("ResourcePosition"); resource = (IResource)thisSettings.Get("Resource"); if (resource == null || resource.GetCapacity() < ResourcePerAction) { failCallback(this); } else { gatherCooldown = Time.time + TimeToGather; } }
public override bool CheckProceduralCondition(IReGoapAgent goapAgent, ReGoapState goalState, IReGoapAction next = null) { return(base.CheckProceduralCondition(goapAgent, goalState) && bag != null); }
public override void Run(IReGoapAction <string, object> previous, IReGoapAction <string, object> next, ReGoapState <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 = settings.Get("workstation") as Workstation; if (workstation != null && workstation.CraftResource(resourcesBag, recipe)) { ReGoapLogger.Log("[CraftRecipeAction] crafted recipe " + recipe.GetCraftedResource()); done(this); } else { fail(this); } }
public override void Run(IReGoapAction <string, object> previous, IReGoapAction <string, object> next, ReGoapState <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); Vector3 inRangePosition = lastKnownPlayerPosition.position * .5f; blackboard.currentTarget = lastKnownPlayerPosition.position; if (blackboard.targetReachedStatus) { //Debug.Log("Chase success"); done(this); } fail(this); }
public virtual void Precalculations(IReGoapAgent goapAgent, ReGoapState goalState) { agent = goapAgent; }
public override IReGoapActionSettings <string, object> GetSettings(IReGoapAgent <string, object> goapAgent, ReGoapState <string, object> goalState) { settings = null; foreach (var pair in goalState.GetValues()) { if (pair.Key.StartsWith("collectedResource")) { var resourceName = pair.Key.Substring(17); settings = new AddResourceToBankSettings { ResourceName = resourceName }; break; } } return(settings); }
public virtual IReGoapActionSettings GetSettings(IReGoapAgent goapAgent, ReGoapState goalState) { return(settings); }
public virtual ReGoapState <T, W> InstantiateNewState() { return(ReGoapState <T, W> .Instantiate()); }
public virtual ReGoapState GetPreconditions(ReGoapState goalState, IReGoapAction next = null) { return(preconditions); }
public static ReGoapNode <T, W> Instantiate(IGoapPlanner <T, W> planner, ReGoapState <T, W> newGoal, ReGoapNode <T, W> parent, IReGoapAction <T, W> action, ReGoapState <T, W> actionSettings) { ReGoapNode <T, W> node; if (cachedNodes == null) { cachedNodes = new Stack <ReGoapNode <T, W> >(); } lock (cachedNodes) { node = cachedNodes.Count > 0 ? cachedNodes.Pop() : new ReGoapNode <T, W>(); } node.Init(planner, newGoal, parent, action, actionSettings); return(node); }
public virtual ReGoapState GetEffects(ReGoapState goalState, IReGoapAction next = null) { return(effects); }
private void Init(IGoapPlanner <T, W> planner, ReGoapState <T, W> newGoal, ReGoapNode <T, W> parent, IReGoapAction <T, W> action, ReGoapState <T, W> settings) { expandList.Clear(); this.planner = planner; this.parent = parent; this.action = action; if (settings != null) { this.actionSettings = settings.Clone(); } 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); GoapActionStackData <T, W> stackData; stackData.currentState = state; stackData.goalState = Goal; stackData.next = action; stackData.agent = planner.GetCurrentAgent(); stackData.settings = actionSettings; Preconditions = action.GetPreconditions(stackData); Effects = action.GetEffects(stackData); // addding the action's cost to the node's total cost g += action.GetCost(stackData); // adding the action's effects to the current node's state state.AddFromState(Effects); // removes from goal all the conditions that are now fullfiled in the action's effects Goal.ReplaceWithMissingDifference(Effects); // add all preconditions of the current action to the goal Goal.AddFromState(Preconditions); } else { Goal = newGoal; } h = Goal.Count; // f(node) = g(node) + h(node) cost = g + h * heuristicMultiplier; // additionally calculate the goal without any world effect to understand if we are done var diff = ReGoapState <T, W> .Instantiate(); Goal.MissingDifference(planner.GetCurrentAgent().GetMemory().GetWorldState(), ref diff); goalMergedWithWorld = diff; }
public virtual float GetCost(ReGoapState goalState, IReGoapAction next = null) { return(Cost); }
public override void Run(IReGoapAction <string, object> previous, IReGoapAction <string, object> next, ReGoapState <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); // do your own game logic here Debug.Log("BLOCK PLACED"); // when done, in this function or outside this function, call the done or fail callback, automatically saved to doneCallback and failCallback by ReGoapAction doneCallback(this); // this will tell the ReGoapAgent that the action is successfully done and go ahead in the action plan // if the action has failed then run failCallback(this), the ReGoapAgent will automatically invalidate the whole plan and ask the ReGoapPlannerManager to create a new plan }
public override void Run(IReGoapAction <string, object> previous, IReGoapAction <string, object> next, ReGoapState <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); spinner.movementSpeed = 0f; Health health = spinner.Target.GetComponent <Health>(); while (health.Amount != 0) { health.Change(-damageOverTime * Time.deltaTime, this.gameObject); doneCallback(this); } health.OnDeathEvent += SpawnSoul; }