Skip to content

Commit

Permalink
Merge pull request #82 from crashkonijn/feature/allow-to-requeue-agent
Browse files Browse the repository at this point in the history
Allow to re-queue action
  • Loading branch information
crashkonijn authored Oct 4, 2023
2 parents 38cca16 + b053d6a commit df4cd1f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ private void Run(GlobalWorldData globalData, IMonoAgent agent)
if (agent.CurrentGoal == null)
return;

if (agent.CurrentAction != null)
return;

var localData = this.goapSet.SensorRunner.SenseLocal(globalData, agent);

if (this.IsGoalCompleted(localData, agent))
Expand Down Expand Up @@ -139,8 +136,11 @@ public void Complete()
resolveHandle.Agent.Events.NoActionFound(resolveHandle.Agent.CurrentGoal);
continue;
}

resolveHandle.Agent.SetAction(action, result, resolveHandle.Agent.WorldData.GetTarget(action));

if (action != resolveHandle.Agent.CurrentAction)
{
resolveHandle.Agent.SetAction(action, result, resolveHandle.Agent.WorldData.GetTarget(action));
}
}

this.resolveHandles.Clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,25 @@ public void Run_AgentHasCurrentGoalAndNoAction_Runs()
sensorRunner.Received(1).SenseLocal(Arg.Any<GlobalWorldData>(), agent);
resolver.Received(1).StartResolve(Arg.Any<RunData>());
}

[Test]
public void Run_AgentHasCurrentGoalAndAction_DoesNotRun()
public void Run_AgentHasCurrentGoalAndDoesHaveAction_Runs()
{
// Arrange
var goal = Substitute.For<IGoalBase>();
goal.Conditions.Returns(new CrashKonijn.Goap.Resolver.Interfaces.ICondition[] { Substitute.For<ICondition>() });

var agent = Substitute.For<IMonoAgent>();
agent.CurrentGoal.Returns(Substitute.For<IGoalBase>());
agent.CurrentGoal.Returns(goal);
agent.CurrentAction.Returns(Substitute.For<IActionBase>());

var sensorRunner = Substitute.For<ISensorRunner>();
sensorRunner.SenseGlobal().Returns(new GlobalWorldData());

var goapSet = Substitute.For<IGoapSet>();
goapSet.SensorRunner.Returns(sensorRunner);
goapSet.GetAllNodes().Returns(new List<IAction> { });
goapSet.GoapConfig.Returns(GoapConfig.Default);
goapSet.GetAllNodes().Returns(new List<IAction> { goal });
goapSet.Agents.GetQueue().Returns(new []{ agent });
goapSet.GetActions().Returns(new List<IActionBase>());

var resolver = Substitute.For<IGraphResolver>();

Expand All @@ -144,8 +146,8 @@ public void Run_AgentHasCurrentGoalAndAction_DoesNotRun()
runner.Dispose();

// Assert
sensorRunner.Received(0).SenseLocal(Arg.Any<GlobalWorldData>(), agent);
resolver.Received(0).StartResolve(Arg.Any<RunData>());
sensorRunner.Received(1).SenseLocal(Arg.Any<GlobalWorldData>(), agent);
resolver.Received(1).StartResolve(Arg.Any<RunData>());
}

[Test]
Expand Down Expand Up @@ -217,5 +219,79 @@ public void Run_AgentHasNotCompletedGoal_DoesntCallGoalCompleteEvent()
resolver.Received(1).StartResolve(Arg.Any<RunData>());
agent.Events.Received(0).GoalCompleted(agent.CurrentGoal);
}

[Test]
public void Run_AgentHasCurrentGoalAndNoAction_SetsTheActionOnAgent()
{
// Arrange
var goal = Substitute.For<IGoalBase>();
goal.Conditions.Returns(new CrashKonijn.Goap.Resolver.Interfaces.ICondition[] { Substitute.For<ICondition>() });

var action = Substitute.For<IActionBase>();

var agent = Substitute.For<IMonoAgent>();
agent.CurrentGoal.Returns(goal);
agent.CurrentAction.ReturnsNull();

var sensorRunner = Substitute.For<ISensorRunner>();

var goapSet = Substitute.For<IGoapSet>();
goapSet.SensorRunner.Returns(sensorRunner);
goapSet.GetAllNodes().Returns(new List<IAction> { goal });
goapSet.Agents.GetQueue().Returns(new []{ agent });
goapSet.GetActions().Returns(new List<IActionBase>());

var handle = Substitute.For<IResolveHandle>();
handle.Complete().Returns(new IAction[] { action });

var resolver = Substitute.For<IGraphResolver>();
resolver.StartResolve(Arg.Any<RunData>()).Returns(handle);

var runner = new GoapSetJobRunner(goapSet, resolver);

// Act
runner.Run();
runner.Complete();

// Assert
agent.Received(1).SetAction(action, Arg.Any<List<IActionBase>>(), Arg.Any<ITarget>());
}

[Test]
public void Run_AgentHasCurrentGoalAndAction_ResolvingSameActionDoesntCallSet()
{
// Arrange
var goal = Substitute.For<IGoalBase>();
goal.Conditions.Returns(new CrashKonijn.Goap.Resolver.Interfaces.ICondition[] { Substitute.For<ICondition>() });

var action = Substitute.For<IActionBase>();

var agent = Substitute.For<IMonoAgent>();
agent.CurrentGoal.Returns(goal);
agent.CurrentAction.Returns(action);

var sensorRunner = Substitute.For<ISensorRunner>();

var goapSet = Substitute.For<IGoapSet>();
goapSet.SensorRunner.Returns(sensorRunner);
goapSet.GetAllNodes().Returns(new List<IAction> { goal });
goapSet.Agents.GetQueue().Returns(new []{ agent });
goapSet.GetActions().Returns(new List<IActionBase>());

var handle = Substitute.For<IResolveHandle>();
handle.Complete().Returns(new IAction[] { action });

var resolver = Substitute.For<IGraphResolver>();
resolver.StartResolve(Arg.Any<RunData>()).Returns(handle);

var runner = new GoapSetJobRunner(goapSet, resolver);

// Act
runner.Run();
runner.Complete();

// Assert
agent.Received(0).SetAction(Arg.Any<IActionBase>(), Arg.Any<List<IActionBase>>(), Arg.Any<ITarget>());
}
}
}

0 comments on commit df4cd1f

Please sign in to comment.