Skip to content

Commit 0b514a9

Browse files
committed
Qualify safe alloc mode
1 parent 75a93e6 commit 0b514a9

File tree

8 files changed

+74
-11
lines changed

8 files changed

+74
-11
lines changed

Runtime/Details/Dish.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ internal abstract class Dish<T> where T : class{
1313
public virtual void Invalidate (){}
1414
public void Consume () => state = null;
1515

16+
public static Dish<T> Create(T s, bool safe)
17+
=> (s is Clonable<T> && !safe) ? (Dish<T>)new DirtyDish<T>()
18+
: new PolyDish<T>();
19+
1620
}
1721

1822
// Dirty dish assumes failing actions do not mutate model state

Runtime/Details/Strings.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/GameAI.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,14 @@ public abstract partial class GameAI<T> : SolverOwner
2929
// Decide a goal and (optionally) a heuristic
3030
public abstract Goal<T> Goal();
3131

32-
// Instantiates or updates the model using relevant world states
32+
// Instantiate a model from relevant world states
3333
public abstract T Model();
3434

3535
// Override if you have a behavior while idle
3636
virtual public void Idle(){ }
3737

38-
// This method should return false to indicate that the actor
39-
// is available for planning, true while the actor is effecting
40-
// game a-ctions
38+
// Return false to indicate the actor are available for
39+
// planning, true while the actor are effecting game actions
4140
virtual public bool IsActing() => false;
4241

4342
public virtual void Update(){
@@ -48,14 +47,13 @@ public virtual void Update(){
4847
solver.maxNodes = config.maxNodes;
4948
solver.maxIter = config.maxIter;
5049
solver.tolerance = config.tolerance;
51-
solver.cleanActions = config.safeActions;
50+
solver.safe = config.safe;
5251
if(handler is ActionMap m) m.verbose = verbose;
5352
handler.Effect( solver.isRunning
5453
? solver.Iterate(config.frameBudget)?.Head()
5554
: solver.Next(model, Goal(), config.frameBudget)?.Head(),
5655
this);
5756
if(policies.OnResult(solver.status, ObjectName())){
58-
//ebug.Log($"Clear solver - {solver.state}");
5957
solver = null;
6058
}
6159
}

Runtime/Solver.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ public class Solver<T> : SolverStats where T : class{
1010

1111
public int maxNodes = 1000, maxIter = 1000;
1212
public float tolerance;
13-
public bool brfs, cleanActions = true;
13+
public bool brfs, safe = true;
1414
public PlanningState status { get; private set; }
15+
// TODO terrible name
1516
public int fxMaxNodes { get; private set; }
1617
public int I { get; private set; }
17-
//
1818
T initialState;
1919
Dish<T> dish;
2020
Goal<T> goal;
@@ -24,8 +24,7 @@ public class Solver<T> : SolverStats where T : class{
2424

2525
public Node<T> Next(T s, in Goal<T> g, int iter=-1){
2626
if(s == null) throw new NullRef(NO_INIT_ERR);
27-
if(dish == null) dish = (s is Clonable<T> && cleanActions)
28-
? (Dish<T>)new DirtyDish<T>() : new PolyDish<T>();
27+
dish = dish ?? Dish<T>.Create(s, safe);
2928
initialState = s;
3029
goal = g;
3130
I = 0;

Runtime/SolverParams.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ [System.Serializable] public class SolverParams{
55
public int maxNodes = 1000;
66
public int maxIter = 1000;
77
public float tolerance = 0;
8-
public bool safeActions = false;
8+
public bool safe = true;
99

1010
}}

Tests/Editor/DishTest.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using NUnit.Framework;
2+
using Activ.GOAP;
3+
using ArgNull = System.ArgumentNullException;
4+
5+
namespace Activ.GOAP{
6+
public class DishTest : TestBase{
7+
8+
T clonable = new T();
9+
10+
[Test] public void CreateSafeDishWithClonable()
11+
=> o( Dish<T>.Create(clonable, safe: true) is PolyDish<T> );
12+
13+
[Test] public void CreateUnsafeDishWithClonable()
14+
=> o( Dish<T>.Create(clonable, safe: false) is DirtyDish<T> );
15+
16+
[Test] public void CreateWithSerializable(
17+
[Values(true, false)] bool safe){
18+
o( Dish<U>.Create(new U(), safe) is PolyDish<U> );
19+
}
20+
21+
class T : Clonable<T>{
22+
public T Allocate() => new T();
23+
public T Clone(T t) => t;
24+
}
25+
26+
[System.Serializable] class U{}
27+
28+
}}

Tests/Editor/DishTest.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tests/Editor/SolverTest.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ [SetUp] public void Setup(){
1818
x.maxIter = x.maxNodes = 100;
1919
}
2020

21+
// Defaults ----------------------------------------------------
22+
23+
// Important because tolerance > 0 may affect search result
24+
[Test] public void ZeroToleranceDefault() => x.tolerance = 0;
25+
26+
// Unsafe mode should be enabled by user once they figure out
27+
// what it means
28+
[Test] public void SafeDefault() => x.safe = true;
29+
30+
// Default to A*, not breadth-first
31+
[Test] public void BRFSDefault() => x.brfs = false;
32+
2133
// isRunning ---------------------------------------------------
2234

2335
[Test] public void IsRunning_false_on_construct()

0 commit comments

Comments
 (0)