Skip to content

Commit

Permalink
small fixes; fixed bug in MultipleResourcesSensor
Browse files Browse the repository at this point in the history
  • Loading branch information
luxkun committed Feb 21, 2017
1 parent dd74a62 commit 9ca2c26
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
13 changes: 0 additions & 13 deletions Unity/Editor/ReGoapNodeBaseEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@ public class ReGoapNodeBaseEditor : EditorWindow
private IReGoapAgent agent;
private GUIStyle possibleActionStyle;

private float repaintCooldown;
private float repaintDelay = 0.33f;

[MenuItem("Window/ReGoap Debugger")]
private static void OpenWindow()
{
ReGoapNodeBaseEditor window = GetWindow<ReGoapNodeBaseEditor>();
window.titleContent = new GUIContent("ReGoap Debugger");
EditorApplication.update += window.Update;
}

private void OnEnable()
Expand Down Expand Up @@ -99,15 +95,6 @@ private void OnGUI()
ProcessNodeEvents(Event.current);
ProcessEvents(Event.current);

if (GUI.changed) Repaint();
}

void Update()
{
if (Time.time < repaintCooldown)
return;
repaintCooldown = repaintDelay + Time.time;
UpdateGoapNodes(Selection.activeGameObject);
Repaint();
}

Expand Down
10 changes: 7 additions & 3 deletions Unity/FSMExample/Sensors/MultipleResourcesSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public class MultipleResourcesSensor : ResourceSensor
{
private static Dictionary<string, Dictionary<IResource, Vector3>> cachedResources;
private static float cacheUpdateDelay;
private Dictionary<string, float> cacheUpdateDelays;
private static float cacheUpdateCooldown = 1f;

public float MinResourceValue = 1f;
Expand All @@ -23,13 +23,17 @@ public override void UpdateSensor()
worldState.Set("see" + resourceManager.GetResourceName(), resourceManager.GetResourcesCount() >= MinResourceValue);

if (cachedResources == null)
{
cachedResources = new Dictionary<string, Dictionary<IResource, Vector3>>();
cacheUpdateDelays = new Dictionary<string, float>();
}
// since every agent will use same resources we cache this function
if (Time.time > cacheUpdateDelay || !cachedResources.ContainsKey(resourceManager.GetResourceName()))
float cacheUpdateDelay;
if (!cacheUpdateDelays.TryGetValue(resourceManager.GetResourceName(), out cacheUpdateDelay) || Time.time > cacheUpdateDelay)
{
UpdateResources(resourceManager);
cachedResources[resourceManager.GetResourceName()] = resourcesPosition;
cacheUpdateDelay = Time.time + cacheUpdateCooldown;
cacheUpdateDelays[resourceManager.GetResourceName()] = Time.time + cacheUpdateCooldown;
}
var nearestResource = Utilities.GetNearest(transform.position, cachedResources[resourceManager.GetResourceName()]);
worldState.Set("nearest" + resourceManager.GetResourceName(), nearestResource);
Expand Down
17 changes: 12 additions & 5 deletions Unity/GoapAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public bool IsPlanning
}

public bool WorkInFixedUpdate;
public bool ValidateActiveAction;

#region UnityFunctions
protected virtual void Awake()
Expand Down Expand Up @@ -72,9 +73,12 @@ private void Tick()
return;
}
// check if current action preconditions are still valid, else invalid action and restart planning
var state = memory.GetWorldState();
if (currentActionState.Action.GetPreconditions(state).MissingDifference(state, 1) > 0)
TryWarnActionFailure(currentActionState.Action);
if (ValidateActiveAction)
{
var state = memory.GetWorldState();
if (currentActionState.Action.GetPreconditions(state).MissingDifference(state, 1) > 0)
TryWarnActionFailure(currentActionState.Action);
}
}
#endregion

Expand Down Expand Up @@ -129,10 +133,11 @@ protected virtual bool CalculateNewGoal(bool forceStart = false)
protected virtual void OnDonePlanning(IReGoapGoal newGoal)
{
currentPlanWorker = null;
if (newGoal == null)
{
if (newGoal == null) {
if (currentGoal == null)
{
ReGoapLogger.LogWarning("GoapAgent " + this + " could not find a plan.");
}
return;
}

Expand Down Expand Up @@ -181,6 +186,8 @@ protected virtual void PushAction()
var plan = currentGoal.GetPlan();
if (plan.Count == 0)
{
currentActionState.Action.Exit(currentActionState.Action);
currentActionState = null;
CalculateNewGoal();
}
else
Expand Down
4 changes: 2 additions & 2 deletions Unity/GoapPlannerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ protected virtual void FixedUpdate()
// check all threads for done work
protected virtual void Tick()
{
lock (doneWorks)
if (doneWorks.Count > 0)
{
if (doneWorks.Count > 0)
lock (doneWorks)
{
var doneWorksCopy = doneWorks.ToArray();
doneWorks.Clear();
Expand Down

0 comments on commit 9ca2c26

Please sign in to comment.