33namespace Activ . GOAP {
44public 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} }
0 commit comments