Skip to content

Commit

Permalink
Removed Update methods from ReGoapGoal, ReGoapAgent and ReGoapMemory
Browse files Browse the repository at this point in the history
Added two new classes:
- ReGoapGoalAdvanced: with update method, needed if you want automatic warnings to agent when the goal is possible / not possible anymore
- ReGoapMemoryAdvanced: with update method, needed if you use sensors and want to automatically update memory with them
- ReGoapAgentAdvanced: with updated metehod, needed if you want to validate continously current action in the agent

Small changes to FSMExample
  • Loading branch information
luxkun committed Apr 5, 2017
1 parent 7772a6e commit 063e00c
Show file tree
Hide file tree
Showing 15 changed files with 174 additions and 95 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ sysinfo.txt

# Builds
*.apk
*.unitypackage
*.unitypackage
ReGoap/Unity/SimExample/
ReGoap/Unity/SimExample.meta
50 changes: 23 additions & 27 deletions ReGoap/Unity/FSMExample/Actions/GatherResourceAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ public override ReGoapState<string, object> GetPreconditions(ReGoapState<string,
preconditions.Clear();
if (newNeededResourceName != null)
{
resource = agent.GetMemory().GetWorldState().Get("nearest" + newNeededResourceName) as IResource;
if (resource != null)
var wantedResource = agent.GetMemory().GetWorldState().Get("nearest" + newNeededResourceName) as IResource;
if (wantedResource != null)
{
resourcePosition = agent.GetMemory().GetWorldState()
.Get(string.Format("nearest{0}Position", newNeededResourceName)) as Vector3?;
preconditions.Set("isAtPosition", resourcePosition);
preconditions.Set("isAtPosition", agent.GetMemory().GetWorldState()
.Get(string.Format("nearest{0}Position", newNeededResourceName)) as Vector3?);
}
}
return preconditions;
Expand All @@ -58,10 +57,9 @@ public override ReGoapState<string, object> GetEffects(ReGoapState<string, objec
effects.Clear();
if (newNeededResourceName != null)
{
resource = agent.GetMemory().GetWorldState().Get("nearest" + newNeededResourceName) as IResource;
if (resource != null)
var wantedResource = agent.GetMemory().GetWorldState().Get("nearest" + newNeededResourceName) as IResource;
if (wantedResource != null)
{
resourcePosition = agent.GetMemory().GetWorldState().Get(string.Format("nearest{0}Position", newNeededResourceName)) as Vector3?;
effects.Set("hasResource" + newNeededResourceName, true);
}
}
Expand All @@ -71,23 +69,21 @@ public override ReGoapState<string, object> GetEffects(ReGoapState<string, objec
public override IReGoapActionSettings<string, object> GetSettings(IReGoapAgent<string, object> goapAgent, ReGoapState<string, object> goalState)
{
var newNeededResourceName = GetNeededResourceFromGoal(goalState);
settings = null;
GatherResourceSettings wantedSettings = null;
if (newNeededResourceName != null)
{
resource = agent.GetMemory().GetWorldState().Get("nearest" + newNeededResourceName) as IResource;
if (resource != null)
var wantedResource = agent.GetMemory().GetWorldState().Get("nearest" + newNeededResourceName) as IResource;
if (wantedResource != null)
{
resourcePosition = (Vector3) agent.GetMemory().GetWorldState()
.Get(string.Format("nearest{0}Position", newNeededResourceName));

settings = new GatherResourceSettings
wantedSettings = new GatherResourceSettings
{
ResourcePosition = resourcePosition,
Resource = resource
ResourcePosition = (Vector3)agent.GetMemory().GetWorldState()
.Get(string.Format("nearest{0}Position", newNeededResourceName)),
Resource = wantedResource
};
}
}
return settings;
return wantedSettings;
}

public override bool CheckProceduralCondition(IReGoapAgent<string, object> goapAgent, ReGoapState<string, object> goalState, IReGoapAction<string, object> next = null)
Expand All @@ -98,7 +94,11 @@ public override bool CheckProceduralCondition(IReGoapAgent<string, object> goapA
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);
SetNeededResources(settings);

var thisSettings = (GatherResourceSettings)settings;
resourcePosition = thisSettings.ResourcePosition;
resource = thisSettings.Resource;

if (resource == null || resource.GetCapacity() < ResourcePerAction)
failCallback(this);
else
Expand All @@ -107,18 +107,14 @@ public override void Run(IReGoapAction<string, object> previous, IReGoapAction<s
}
}

private void SetNeededResources(IReGoapActionSettings<string, object> settings)
{
var thisSettings = (GatherResourceSettings)settings;
resourcePosition = thisSettings.ResourcePosition;
resource = thisSettings.Resource;
}

protected void Update()
{
if (resource == null || resource.GetCapacity() < ResourcePerAction)
{
failCallback(this);
else if (Time.time > gatherCooldown)
return;
}
if (Time.time > gatherCooldown)
{
gatherCooldown = float.MaxValue;
ReGoapLogger.Log("[GatherResourceAction] acquired " + ResourcePerAction + " " + resource.GetName());
Expand Down
2 changes: 1 addition & 1 deletion ReGoap/Unity/FSMExample/Agents/BuilderAgent.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace ReGoap.Unity.FSMExample.Agents
{
public class BuilderAgent : ReGoapAgent<string, object>
public class BuilderAgent : ReGoapAgentAdvanced<string, object>
{
}
}
Binary file modified ReGoap/Unity/FSMExample/ExampleFSMScene.unity
Binary file not shown.
2 changes: 1 addition & 1 deletion ReGoap/Unity/FSMExample/Memories/BuilderMemory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace ReGoap.Unity.FSMExample.Memories
{
public class BuilderMemory : ReGoapMemory<string, object>
public class BuilderMemory : ReGoapMemoryAdvanced<string, object>
{
}
}
Expand Down
1 change: 1 addition & 0 deletions ReGoap/Unity/ReGoapAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public virtual void Run(IReGoapAction<T, W> previous, IReGoapAction<T, W> next,
enabled = true;
doneCallback = done;
failCallback = fail;
this.settings = settings;

previousAction = previous;
nextAction = next;
Expand Down
27 changes: 6 additions & 21 deletions ReGoap/Unity/ReGoapAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class ReGoapAgent<T, W> : MonoBehaviour, IReGoapAgent<T, W>, IReGoapAgent
public float CalculationDelay = 0.5f;
public bool BlackListGoalOnFailure;

public bool CalculateNewGoalOnStart = true;

protected float lastCalculationTime;

protected List<IReGoapGoal<T, W>> goals;
Expand All @@ -36,8 +38,6 @@ public bool IsPlanning
get { return startedPlanning && currentReGoapPlanWorker.NewGoal == null; }
}

public bool ValidateActiveAction;

#region UnityFunctions
protected virtual void Awake()
{
Expand All @@ -51,6 +51,10 @@ protected virtual void Awake()

protected virtual void Start()
{
if (CalculateNewGoalOnStart)
{
CalculateNewGoal(true);
}
}

protected virtual void OnEnable()
Expand All @@ -67,25 +71,6 @@ protected virtual void OnDisable()
currentGoal = null;
}
}

protected virtual void Update()
{
possibleGoalsDirty = true;

if (currentActionState == null)
{
if (!IsPlanning)
CalculateNewGoal();
return;
}
// check if current action preconditions are still valid, else invalid action and restart planning
if (ValidateActiveAction)
{
var state = memory.GetWorldState();
if (currentActionState.Action.GetPreconditions(state).MissingDifference(state, 1) > 0)
TryWarnActionFailure(currentActionState.Action);
}
}
#endregion

protected virtual void UpdatePossibleGoals()
Expand Down
28 changes: 28 additions & 0 deletions ReGoap/Unity/ReGoapAgentAdvanced.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace ReGoap.Unity
{
public class ReGoapAgentAdvanced<T, W> : ReGoapAgent<T, W>
{
public bool ValidateActiveAction;

#region UnityFunctions
protected virtual void Update()
{
possibleGoalsDirty = true;

if (currentActionState == null)
{
if (!IsPlanning)
CalculateNewGoal();
return;
}
// check if current action preconditions are still valid, else invalid action and restart planning
if (ValidateActiveAction)
{
var state = memory.GetWorldState();
if (currentActionState.Action.GetPreconditions(state).MissingDifference(state, 1) > 0)
TryWarnActionFailure(currentActionState.Action);
}
}
#endregion
}
}
12 changes: 12 additions & 0 deletions ReGoap/Unity/ReGoapAgentAdvanced.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 1 addition & 21 deletions ReGoap/Unity/ReGoapGoal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,20 @@ public class ReGoapGoal<T, W> : MonoBehaviour, IReGoapGoal<T, W>
protected Queue<ReGoapActionState<T, W>> plan;
protected IGoapPlanner<T, W> planner;

public float WarnDelay = 2f;
private float warnCooldown;

#region UnityFunctions
protected virtual void Awake()
{
goal = ReGoapState<T, W>.Instantiate();
}

void OnDestroy()
protected virtual void OnDestroy()
{
goal.Recycle();
}

protected virtual void Start()
{
}

protected virtual void Update()
{
if (planner != null && !planner.IsPlanning() && Time.time > warnCooldown)
{
warnCooldown = Time.time + WarnDelay;
var currentGoal = planner.GetCurrentGoal();
var plannerPlan = currentGoal == null ? null : currentGoal.GetPlan();
var equalsPlan = ReferenceEquals(plannerPlan, plan);
var isGoalPossible = IsGoalPossible();
// check if this goal is not active but CAN be activated
// or
// if this goal is active but isn't anymore possible
if ((!equalsPlan && isGoalPossible) || (equalsPlan && !isGoalPossible))
planner.GetCurrentAgent().WarnPossibleGoal(this);
}
}
#endregion

#region IReGoapGoal
Expand Down
35 changes: 35 additions & 0 deletions ReGoap/Unity/ReGoapGoalAdvanced.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using ReGoap.Core;
using ReGoap.Planner;
using UnityEngine;

// generic goal, should inherit this to do your own goal
namespace ReGoap.Unity
{
public class ReGoapGoalAdvanced<T, W> : ReGoapGoal<T, W>
{
public float WarnDelay = 2f;
private float warnCooldown;

#region UnityFunctions
protected virtual void Update()
{
if (planner != null && !planner.IsPlanning() && Time.time > warnCooldown)
{
warnCooldown = Time.time + WarnDelay;
var currentGoal = planner.GetCurrentGoal();
var plannerPlan = currentGoal == null ? null : currentGoal.GetPlan();
var equalsPlan = ReferenceEquals(plannerPlan, plan);
var isGoalPossible = IsGoalPossible();
// check if this goal is not active but CAN be activated
// or
// if this goal is active but isn't anymore possible
if ((!equalsPlan && isGoalPossible) || (equalsPlan && !isGoalPossible))
planner.GetCurrentAgent().WarnPossibleGoal(this);
}
}
#endregion
}
}
12 changes: 12 additions & 0 deletions ReGoap/Unity/ReGoapGoalAdvanced.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 1 addition & 23 deletions ReGoap/Unity/ReGoapMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,21 @@ namespace ReGoap.Unity
public class ReGoapMemory<T, W> : MonoBehaviour, IReGoapMemory<T, W>
{
protected ReGoapState<T, W> state;
private IReGoapSensor<T, W>[] sensors;

public float SensorsUpdateDelay = 0.3f;
private float sensorsUpdateCooldown;

#region UnityFunctions
protected virtual void Awake()
{
state = ReGoapState<T, W>.Instantiate();
sensors = GetComponents<IReGoapSensor<T, W>>();
foreach (var sensor in sensors)
{
sensor.Init(this);
}
}

void OnDestroy()
protected virtual void OnDestroy()
{
state.Recycle();
}

protected virtual void Start()
{
}

protected virtual void Update()
{
if (Time.time > sensorsUpdateCooldown)
{
sensorsUpdateCooldown = Time.time + SensorsUpdateDelay;

foreach (var sensor in sensors)
{
sensor.UpdateSensor();
}
}
}
#endregion

public virtual ReGoapState<T, W> GetWorldState()
Expand Down
Loading

0 comments on commit 063e00c

Please sign in to comment.