Skip to content

Commit

Permalink
added action continuation for actions that are in Executing status
Browse files Browse the repository at this point in the history
  • Loading branch information
caesuric committed Jun 5, 2023
1 parent f108392 commit 4e2f94f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
9 changes: 7 additions & 2 deletions MountainGoap/Action.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,20 @@ public float GetCost() {
/// Executes a step of work for the agent.
/// </summary>
/// <param name="agent">Agent executing the action.</param>
internal void Execute(Agent agent) {
/// <returns>The execution status of the action.</returns>
internal ExecutionStatus Execute(Agent agent) {
OnBeginExecuteAction(agent, this, parameters);
if (IsPossible(agent.State)) {
var newState = executor(agent, this);
if (newState == ExecutionStatus.Succeeded) ApplyEffects(agent.State);
ExecutionStatus = newState;
OnFinishExecuteAction(agent, this, ExecutionStatus, parameters);
return newState;
}
else {
OnFinishExecuteAction(agent, this, ExecutionStatus.NotPossible, parameters);
return ExecutionStatus.NotPossible;
}
else OnFinishExecuteAction(agent, this, ExecutionStatus.NotPossible, parameters);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions MountainGoap/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ private void Execute() {
List<List<Action>> cullableSequences = new();
foreach (var sequence in CurrentActionSequences) {
if (sequence.Count > 0) {
sequence[0].Execute(this);
sequence.RemoveAt(0);
var executionStatus = sequence[0].Execute(this);
if (executionStatus != ExecutionStatus.Executing) sequence.RemoveAt(0);
}
else cullableSequences.Add(sequence);
}
Expand Down
54 changes: 54 additions & 0 deletions MountainGoapTest/ActionContinuationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
namespace MountainGoapTest {
using System.Collections.Generic;

public class ActionContinuationTests {
[Fact]
public void ItCanContinueActions() {
var timesExecuted = 0;
var agent = new Agent(
state: new() {
{ "key", false },
{ "progress", 0 }
},
goals: new List<BaseGoal> {
new Goal(
desiredState: new() {
{ "key", true }
}
)
},
actions: new List<Action> {
new Action(
preconditions: new() {
{ "key", false }
},
postconditions: new() {
{ "key", true }
},
executor: (Agent agent, Action action) => {
timesExecuted++;
if (agent.State["progress"] is int progress && progress < 3) {
agent.State["progress"] = progress + 1;
return ExecutionStatus.Executing;
}
else return ExecutionStatus.Succeeded;
}
)
}
);
agent.Step(StepMode.OneAction);
if (agent.State["key"] is bool value) Assert.False(value);
else Assert.False(true);
agent.Step(StepMode.OneAction);
if (agent.State["key"] is bool value2) Assert.False(value2);
else Assert.False(true);
agent.Step(StepMode.OneAction);
if (agent.State["key"] is bool value3) Assert.False(value3);
else Assert.False(true);
agent.Step(StepMode.OneAction);
if (agent.State["key"] is bool value4) Assert.True(value4);
else Assert.False(true);
Assert.Equal(4, timesExecuted);
}
}
}

0 comments on commit 4e2f94f

Please sign in to comment.