1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
using System;
using UnityEngine;
using System.Linq;
using Pathfinding.RVO;
namespace Pathfinding.Examples.RTS {
public enum Status {
Invalid,
Failure,
Success,
Running
};
public class BTContext {
public RTSUnit unit;
public Transform transform;
public Animator animator;
}
/// <summary>Implements a simple behavior tree. This is the base class for all nodes in the tree.</summary>
public abstract class BTNode {
protected Status lastStatus;
public Status Tick (BTContext ctx) {
if (lastStatus == Status.Invalid) OnInit(ctx);
lastStatus = DoTick(ctx);
if (lastStatus == Status.Invalid) throw new System.Exception();
return lastStatus;
}
public void Terminate (BTContext ctx) {
OnTerminate(ctx);
lastStatus = Status.Invalid;
}
protected virtual void OnInit (BTContext ctx) {
}
protected virtual void OnTerminate (BTContext ctx) {
}
protected abstract Status DoTick(BTContext ctx);
}
public class BTTransparent : BTNode {
public BTNode child;
protected override void OnTerminate (BTContext ctx) {
child.Terminate(ctx);
}
protected override Status DoTick (BTContext ctx) {
return child.Tick(ctx);
}
}
public class Once : BTNode {
public BTNode child;
public Once (BTNode child) { this.child = child; }
protected override void OnTerminate (BTContext ctx) {
if (lastStatus == Status.Running) child.Terminate(ctx);
}
protected override Status DoTick (BTContext ctx) {
if (lastStatus == Status.Success) return Status.Success;
var s = child.Tick(ctx);
if (s == Status.Success) {
child.Terminate(ctx);
}
return s;
}
}
public class SimpleAction : BTNode {
public System.Action<BTContext> action;
public SimpleAction (System.Action<BTContext> action) { this.action = action; }
protected override Status DoTick (BTContext ctx) {
action(ctx);
return Status.Success;
}
}
public class Condition : BTNode {
public System.Func<BTContext, bool> predicate;
public Condition (System.Func<BTContext, bool> predicate) { this.predicate = predicate; }
protected override Status DoTick (BTContext ctx) {
return predicate(ctx) ? Status.Success : Status.Failure;
}
}
public class BTSequence : BTNode {
public BTNode[] children;
int childIndex = -1;
public BTSequence (BTNode[] children) {
this.children = children;
}
protected override void OnInit (BTContext ctx) {
childIndex = 0;
}
protected override void OnTerminate (BTContext ctx) {
for (int i = 0; i <= childIndex; i++) {
children[i].Terminate(ctx);
}
childIndex = -1;
}
protected override Status DoTick (BTContext ctx) {
int i;
for (i = 0; i < children.Length; i++) {
var s = children[i].Tick(ctx);
if (s != Status.Success) {
// Terminate all nodes that executed the last frame, but did not execute this frame
for (int j = i + 1; j <= childIndex; j++) children[j].Terminate(ctx);
childIndex = i;
return s;
}
}
childIndex = i - 1;
return Status.Success;
}
}
public class BTSelector : BTNode {
public BTNode[] children;
int childIndex = -1;
public BTSelector (BTNode[] children) {
this.children = children;
}
protected override void OnInit (BTContext ctx) {
}
protected override void OnTerminate (BTContext ctx) {
for (int i = 0; i <= childIndex; i++) {
children[i].Terminate(ctx);
}
childIndex = -1;
}
protected override Status DoTick (BTContext ctx) {
int i;
for (i = 0; i < children.Length; i++) {
var s = children[i].Tick(ctx);
if (s != Status.Failure) {
// Terminate all nodes that executed the last frame, but did not execute this frame
for (int j = i + 1; j <= childIndex; j++) children[j].Terminate(ctx);
childIndex = i;
return s;
}
}
childIndex = i - 1;
return Status.Failure;
}
}
class Binding<T> {
T val;
public System.Func<T> getter;
public System.Action<T> setter;
public T value {
get {
if (getter != null) return getter();
return val;
}
set {
if (setter != null) setter(value);
val = value;
}
}
}
public struct Value<T> {
T val;
Binding<T> binding;
public T value {
get {
if (binding != null) return binding.value;
return val;
}
set {
if (binding != null) binding.value = value;
val = value;
}
}
public void Bind (ref Value<T> other) {
if (other.binding != null && binding == null) binding = other.binding;
else if (binding == null && other.binding != null) other.binding = binding;
else if (binding == null) binding = other.binding = new Binding<T>();
else throw new System.Exception("Too complex binding");
}
public void Bind (System.Func<T> other) {
if (binding != null) throw new System.Exception("Already has a binding");
binding = new Binding<T>();
binding.getter = other;
binding.setter = _ => {
throw new System.InvalidOperationException("Trying to assign a value which has been bound as read-only (using a delegate)");
};
}
public Value<T> Bound {
get {
var r = this;
if (binding == null) Bind(ref r);
return r;
}
}
public Value (System.Func<T> getter) {
val = default(T);
binding = null;
Bind(getter);
}
public static implicit operator Value<T>(System.Func<T> getter) {
var val = new Value<T>();
val.Bind(getter);
return val;
}
}
public class BTMove : BTNode {
public Value<Vector3> destination;
public BTMove (Value<Vector3> destination) {
this.destination = destination;
}
protected override void OnInit (BTContext ctx) {
ctx.unit.SetDestination(destination.value, MovementMode.Move);
}
protected override Status DoTick (BTContext ctx) {
var dest = destination.value;
if ((Time.frameCount % 100) == 0) ctx.unit.SetDestination(dest, MovementMode.Move);
if (VectorMath.SqrDistanceXZ(ctx.transform.position, dest) < 0.5f * 0.5f) {
return Status.Success;
} else {
return Status.Running;
}
}
}
public class FindClosestUnit : BTNode {
public Value<RTSUnit> target;
public bool reserve;
RTSUnit.Type type;
public FindClosestUnit (RTSUnit.Type type) {
this.type = type;
}
protected override void OnTerminate (BTContext ctx) {
if (reserve && target.value != null) {
if (target.value.reservedBy != ctx.unit) throw new System.Exception();
target.value.reservedBy = null;
}
target.value = null;
}
RTSUnit FindClosest (Vector3 point) {
var units = RTSManager.instance.units.units;
RTSUnit closest = null;
var dist = float.PositiveInfinity;
for (int i = 0; i < units.Count; i++) {
var unit = units[i];
if (unit.type != type || (reserve && unit.reservedBy != null)) {
continue;
}
if (unit.resource != null && !unit.resource.harvestable) {
continue;
}
var d = (unit.transform.position - point).sqrMagnitude;
if (d < dist) {
dist = d;
closest = unit;
}
}
return closest;
}
protected override Status DoTick (BTContext ctx) {
if (target.value != null) {
return Status.Success;
}
target.value = FindClosest(ctx.transform.position);
if (target.value != null) {
if (reserve) target.value.reservedBy = ctx.unit;
return Status.Success;
}
return Status.Failure;
}
}
static class Behaviors {
public static BTNode HarvestBehavior () {
var reserve = new FindClosestUnit(RTSUnit.Type.ResourceCrystal) { reserve = true };
var dropoff = new FindClosestUnit(RTSUnit.Type.HarvesterDropoff) { reserve = true };
var dropoffQueue = new FindClosestUnit(RTSUnit.Type.HarvesterDropoffQueue);
return new BTSelector(new BTNode[] {
new HarvestMode() {
child = new BTSelector(new BTNode[] {
new BTSequence(new BTNode[] {
new Condition(ctx => ctx.unit.storedCrystals > 0),
new BTSequence(new BTNode[] {
dropoff,
new BTMove(new Value<Vector3>(() => dropoff.target.value.transform.position)),
new SimpleAction(ctx => {
ctx.unit.owner.resources.AddResource(RTSUnit.Type.ResourceCrystal, ctx.unit.storedCrystals);
ctx.unit.storedCrystals = 0;
}),
})
//new Deposit(move1),
}),
new BTSequence(new BTNode[] {
new Condition(ctx => ctx.unit.storedCrystals == 0),
new BTSequence(new BTNode[] {
reserve,
new BTMove(new Value<Vector3>(() => reserve.target.value.transform.position)),
new Harvest { resource = new Value<RTSHarvestableResource>(() => reserve.target.value.resource), duration = 5 },
}),
})
})
},
new BTSequence(new BTNode[] {
dropoffQueue,
new BTMove(new Value<Vector3>(() => dropoffQueue.target.value.transform.position)),
})
});
}
}
public class HarvestMode : BTTransparent {
protected override void OnTerminate (BTContext ctx) {
ctx.unit.GetComponent<RVOController>().layer = RVO.RVOLayer.Layer2;
base.OnTerminate(ctx);
}
protected override Status DoTick (BTContext ctx) {
var s = base.DoTick(ctx);
ctx.unit.GetComponent<RVOController>().layer = s == Status.Running ? RVO.RVOLayer.Layer3 : RVO.RVOLayer.Layer2;
return s;
}
}
public class Harvest : BTNode {
public Value<RTSHarvestableResource> resource;
public float duration = 5;
float time;
protected override void OnInit (BTContext ctx) {
ctx.animator.SetBool("harvesting", true);
//ctx.unit.locked = true;
}
protected override void OnTerminate (BTContext ctx) {
Debug.Log("Terminated harvesting");
ctx.animator.SetBool("harvesting", false);
}
protected override Status DoTick (BTContext ctx) {
time += Time.deltaTime;
if (time > duration) {
ctx.animator.SetBool("harvesting", false);
if (ctx.animator.GetCurrentAnimatorStateInfo(0).IsName("RTSHarvesterHarvesting") || ctx.animator.GetCurrentAnimatorStateInfo(0).IsName("RTSHarvesterHarvestingExit")) {
return Status.Running;
} else {
ctx.unit.storedCrystals += 50;
resource.value.value -= 50;
time = 0;
//ctx.unit.locked = false;
return Status.Success;
}
} else {
return Status.Running;
}
}
}
public class BTHarvest {
/*RTSCommandMove moveToDepositPoint;
RTSCommandMove moveToHarvestPoint;
void Deposit () {
}
public override void Tick () {
if (HasResources()) {
if (moveToDepositPoint.Tick() == Success) {
Deposit();
}
} else {
var target = ReserveTarget();
moveToHarvestPoint.target = target;
if (moveToHarvestPoint.Tick() == Success) {
Harvest(target);
}
}
}*/
}
}
|