Skip to content

Commit 9921d30

Browse files
committed
Defer GetMethod in ExpandActions
1 parent 02f8764 commit 9921d30

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

Runtime/Details/Node.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,41 @@
33
namespace Activ.GOAP{
44
public class Node<T> : Base{
55

6-
public readonly Node<T> prev;
7-
public readonly object action;
8-
public readonly T state;
9-
public float value;
6+
public readonly Node<T> prev;
7+
public readonly Func<Cost> source;
8+
public readonly T state;
9+
public float value;
1010
public float cost{ get; private set; }
11+
readonly object effect;
1112

12-
public Node(object action, T result, Node<T> prev = null,
13+
public Node(Func<Cost> planningAction, T result,
14+
Node<T> prev = null, float cost = 0f){
15+
this.source = Assert(planningAction, "Action");;
16+
this.state = Assert(result, "Result");
17+
this.prev = prev;
18+
this.cost = cost + (prev?.cost ?? 0f);
19+
}
20+
21+
// TODO - object should be System.Action and separate
22+
// constructor for init state if wanted
23+
public Node(object effect, T result, Node<T> prev = null,
1324
float cost = 0f){
14-
this.action = Assert(action, "action");
15-
this.state = Assert(result, "result");
25+
this.effect = Assert(effect, "Action");
26+
this.state = Assert(result, "Result");
1627
this.prev = prev;
1728
this.cost = cost + (prev?.cost ?? 0f);
1829
}
1930

31+
public object action => effect ?? source.Method.Name;
32+
2033
public static implicit operator string(Node<T> x)
2134
=> (string)(x.Head());
2235

2336
public static implicit operator Delegate(Node<T> x)
2437
=> (Delegate)(x.Head());
2538

26-
// Regress to the next action; root (init state) does not count.
27-
public object Head()
28-
=> prev?.prev == null ? action : prev.Head();
39+
// Regress to first applicable action; root (init state) excl.
40+
public object Head() => prev?.prev==null ? action : prev.Head();
2941

3042
public Node<T>[] Path(int n = 1){
3143
Node<T>[] @out;
@@ -46,7 +58,7 @@ public string PathToString(){
4658
}
4759

4860
override public string ToString()
49-
=> $"[{value:0.0} :: {action} => {state}]"
61+
=> $"[{value:0.0} :: {effect} => {state}]"
5062
.Replace("System.Object", "object");
5163

5264
}}

Runtime/Solver.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public Node<T> Iterate(int iter=-1){
6060

6161
void ExpandActions(Node<T> x, NodeSet<T> @out){
6262
if(!(x.state is Agent p)) return;
63-
var count = p.Actions()?.Length ?? 0;
63+
var actions = p.Actions();
64+
var count = actions?.Length ?? 0;
6465
if(count == 0) return;
6566
dish.Init(x.state);
6667
T y = null;
@@ -70,11 +71,9 @@ void ExpandActions(Node<T> x, NodeSet<T> @out){
7071
var r = q.Actions()[i]();
7172
if(r.done){
7273
dish.Invalidate();
73-
if(!brfs && (r.cost <= 0))
74-
throw new Ex(ZERO_COST_ERR);
75-
var name = p.Actions()[i].Method.Name;
76-
if(@out.Insert(new Node<T>(name, y, x, r.cost)))
77-
dish.Consume();
74+
if(!brfs && (r.cost<=0)) throw new Ex(ZERO_COST_ERR);
75+
if(@out.Insert(new Node<T>(
76+
actions[i], y, x, r.cost))) dish.Consume();
7877
}
7978
}
8079
}

Tests/Editor/NodeTest.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ [Test] public void Path1(){
5656

5757
[Test] public void Head2(){
5858
var x = new Node<object>(INITIAL_STATE, new object());
59-
var y = new Node<object>(ACTION_1, new object(), x);
60-
o( y.Head(), ACTION_1);
59+
var y = new Node<object>(Costly, new object(), x);
60+
o( y.Head(), "Costly");
6161
}
6262

63+
Cost Costly() => true;
64+
6365
[Test] public void Path2(){
6466
var x = new Node<object>(INITIAL_STATE, new object());
6567
var y = new Node<object>(ACTION_1, new object(), x);

0 commit comments

Comments
 (0)