-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticker.go
511 lines (375 loc) · 11.1 KB
/
ticker.go
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
package behaviortree
import (
"errors"
"fmt"
)
/* ============================== TickerType ============================== */
//
type TickerType byte
const (
TickerTypeUnknown TickerType = 0
// Leaf node - no child node
TickerTypeAction TickerType = 1 // Action node
TickerTypeCondition TickerType = 2 // Condition node
// Composite node - at least one child node
TickerTypeSequence TickerType = 50 // Sequence node
TickerTypeSelector TickerType = 51 // Selector node
TickerTypeParallel TickerType = 52 // Parallel node
// Decorative node - only one child node
TickerTypeSucceeder TickerType = 100 // Succeeder node
TickerTypeFailure TickerType = 101 // Failure node
TickerTypeInverter TickerType = 102 // Inverter node
TickerTypeRepeater TickerType = 103 // Repeater node
TickerTypeRepeatUntilFail TickerType = 104 // RepeatUntilFail node
TickerTypeRepeatUntilSucceed TickerType = 105 // RepeatUntilSucceed node
// Node enumeration maximum
TickerTypeMax
)
func (t TickerType) String() string {
switch t {
case TickerTypeAction:
return "Action"
case TickerTypeCondition:
return "Condition"
case TickerTypeSequence:
return "Sequence"
case TickerTypeSelector:
return "Selector"
case TickerTypeParallel:
return "Parallel"
case TickerTypeSucceeder:
return "Succeeder"
case TickerTypeFailure:
return "Failure"
case TickerTypeInverter:
return "Inverter"
case TickerTypeRepeater:
return "Repeater"
case TickerTypeRepeatUntilFail:
return "RepeatUntilFail"
case TickerTypeRepeatUntilSucceed:
return "RepeatUntilSucceed"
}
panic(errors.New("unknown node type"))
}
func (t TickerType) IsLeafNode() bool {
return t >= TickerTypeAction && t < TickerTypeSequence
}
func (t TickerType) IsCompositeNode() bool {
return t >= TickerTypeSequence && t < TickerTypeSucceeder
}
func (t TickerType) IsDecoratorNode() bool {
return t >= TickerTypeSucceeder && t < TickerTypeMax
}
/* ============================== TickStatus ============================== */
//
type TickStatus byte
const (
TickStatusNotTick TickStatus = iota
TickStatusSuccess
TickStatusFailure
TickStatusRunning
)
func (s TickStatus) String() string {
switch s {
case TickStatusNotTick:
return "NotTick"
case TickStatusSuccess:
return "Success"
case TickStatusFailure:
return "Failure"
case TickStatusRunning:
return "Running"
}
panic(errors.New("unknown node status"))
}
func (s TickStatus) IsFinal() bool {
return s == TickStatusSuccess || s == TickStatusFailure
}
func (s TickStatus) IsTickValid() bool {
return s == TickStatusSuccess || s == TickStatusFailure || s == TickStatusRunning
}
func (s TickStatus) Invert() TickStatus {
switch s {
case TickStatusSuccess:
return TickStatusFailure
case TickStatusFailure:
return TickStatusSuccess
default:
return s
}
}
/* ============================== Ticker ============================== */
//
type Ticker interface {
Index() int
Type() TickerType
Tick() TickStatus
}
/* ============================== action Ticker ============================== */
//
type actionTicker struct {
bt *builtTree
index int
}
var _ Ticker = (*actionTicker)(nil)
func (t *actionTicker) Type() TickerType { return TickerTypeAction }
func (t *actionTicker) Index() int { return t.index }
func (t *actionTicker) Tick() TickStatus {
t.bt.setTickerTickTime(t.index)
if status := t.bt.getTickerStatus(t.index); status.IsFinal() {
return status
}
t.bt.setTickerTicked(t.index)
status := t.bt.getLeafNode(t.index).(ActionNode).Action(t.bt.getContext(), t.bt.getBlackboards(t.index))
if !status.IsTickValid() {
panic(fmt.Errorf("invalid tick status: action name: %s", t.bt.getTickerName(t.index)))
}
t.bt.setTickerStatus(t.index, status)
return status
}
/* ============================== condition Ticker ============================== */
//
type conditionTicker struct {
bt *builtTree
index int
}
var _ Ticker = (*conditionTicker)(nil)
func (t *conditionTicker) Type() TickerType { return TickerTypeCondition }
func (t *conditionTicker) Index() int { return t.index }
func (t *conditionTicker) Tick() TickStatus {
t.bt.setTickerTickTime(t.index)
if status := t.bt.getTickerStatus(t.index); status.IsFinal() {
return status
}
t.bt.setTickerTicked(t.index)
status := TickStatusFailure
if t.bt.getLeafNode(t.index).(ConditionNode).Condition(t.bt.getContext(), t.bt.getBlackboards(t.index)) {
status = TickStatusSuccess
}
t.bt.setTickerStatus(t.index, status)
return status
}
/* ============================== sequence Ticker ============================== */
//
type sequenceTicker struct {
bt *builtTree
index int
children []int
}
var _ Ticker = (*sequenceTicker)(nil)
func (t *sequenceTicker) Type() TickerType { return TickerTypeSequence }
func (t *sequenceTicker) Index() int { return t.index }
func (t *sequenceTicker) Tick() TickStatus {
t.bt.setTickerTickTime(t.index)
if status := t.bt.getTickerStatus(t.index); status.IsFinal() {
return status
}
t.bt.setTickerTicked(t.index)
var status TickStatus
for _, child := range t.children {
status = t.bt.getTicker(child).Tick()
if status == TickStatusFailure || status == TickStatusRunning {
break
}
}
t.bt.setTickerStatus(t.index, status)
return status
}
/* ============================== selector Ticker ============================== */
//
type selectorTicker struct {
bt *builtTree
index int
children []int
}
var _ Ticker = (*selectorTicker)(nil)
func (t *selectorTicker) Type() TickerType { return TickerTypeSelector }
func (t *selectorTicker) Index() int { return t.index }
func (t *selectorTicker) Tick() TickStatus {
t.bt.setTickerTickTime(t.index)
if status := t.bt.getTickerStatus(t.index); status.IsFinal() {
return status
}
t.bt.setTickerTicked(t.index)
var status TickStatus
for _, child := range t.children {
status = t.bt.getTicker(child).Tick()
if status == TickStatusSuccess || status == TickStatusRunning {
break
}
}
t.bt.setTickerStatus(t.index, status)
return status
}
/* ============================== parallel Ticker ============================== */
//
type parallelTicker struct {
bt *builtTree
index int
children []int
}
var _ Ticker = (*parallelTicker)(nil)
func (t *parallelTicker) Type() TickerType { return TickerTypeParallel }
func (t *parallelTicker) Index() int { return t.index }
func (t *parallelTicker) Tick() TickStatus {
t.bt.setTickerTickTime(t.index)
if status := t.bt.getTickerStatus(t.index); status.IsFinal() {
return status
}
t.bt.setTickerTicked(t.index)
var hasSuccess, hasFailure bool
for _, child := range t.children {
switch t.bt.getTicker(child).Tick() {
case TickStatusSuccess:
hasSuccess = true
case TickStatusFailure:
hasFailure = true
}
}
var status TickStatus
if hasSuccess {
status = TickStatusSuccess
} else if hasFailure {
status = TickStatusFailure
} else {
status = TickStatusRunning
}
t.bt.setTickerStatus(t.index, status)
return status
}
/* ============================== succeeder Ticker ============================== */
//
type succeederTicker struct {
bt *builtTree
index int
child int
}
var _ Ticker = (*succeederTicker)(nil)
func (t *succeederTicker) Type() TickerType { return TickerTypeSucceeder }
func (t *succeederTicker) Index() int { return t.index }
func (t *succeederTicker) Tick() TickStatus {
t.bt.setTickerTickTime(t.index)
if status := t.bt.getTickerStatus(t.index); status.IsFinal() {
return status
}
t.bt.setTickerTicked(t.index)
t.bt.getTicker(t.child).Tick()
t.bt.setTickerStatus(t.index, TickStatusSuccess)
return TickStatusSuccess
}
/* ============================== failure Ticker ============================== */
//
type failureTicker struct {
bt *builtTree
index int
child int
}
var _ Ticker = (*failureTicker)(nil)
func (t *failureTicker) Type() TickerType { return TickerTypeFailure }
func (t *failureTicker) Index() int { return t.index }
func (t *failureTicker) Tick() TickStatus {
t.bt.setTickerTickTime(t.index)
if status := t.bt.getTickerStatus(t.index); status.IsFinal() {
return status
}
t.bt.setTickerTicked(t.index)
t.bt.getTicker(t.child).Tick()
t.bt.setTickerStatus(t.index, TickStatusFailure)
return TickStatusFailure
}
/* ============================== inverter Ticker ============================== */
//
type inverterTicker struct {
bt *builtTree
index int
child int
}
var _ Ticker = (*inverterTicker)(nil)
func (t *inverterTicker) Type() TickerType { return TickerTypeInverter }
func (t *inverterTicker) Index() int { return t.index }
func (t *inverterTicker) Tick() TickStatus {
t.bt.setTickerTickTime(t.index)
if status := t.bt.getTickerStatus(t.index); status.IsFinal() {
return status
}
t.bt.setTickerTicked(t.index)
status := t.bt.getTicker(t.child).Tick().Invert()
t.bt.setTickerStatus(t.index, status)
return status
}
/* ============================== repeater Ticker ============================== */
//
type repeaterTicker struct {
bt *builtTree
index int
child int
}
var _ Ticker = (*repeaterTicker)(nil)
func (t *repeaterTicker) Type() TickerType { return TickerTypeRepeater }
func (t *repeaterTicker) Index() int { return t.index }
func (t *repeaterTicker) Tick() TickStatus {
t.bt.setTickerTickTime(t.index)
// if status := t.bt.getTickerStatus(t.index); status.IsFinal() {
// return status
// }
t.bt.setTickerTicked(t.index)
if status := t.bt.getTickerStatus(t.index + 1); status.IsFinal() {
t.bt.resetChildren(t.index)
}
t.bt.getTicker(t.child).Tick()
t.bt.setTickerStatus(t.index, TickStatusRunning)
return TickStatusRunning
}
/* ============================== repeatUntilFail Ticker ============================== */
//
type repeatUntilFailTicker struct {
bt *builtTree
index int
child int
}
var _ Ticker = (*repeatUntilFailTicker)(nil)
func (t *repeatUntilFailTicker) Type() TickerType { return TickerTypeRepeatUntilFail }
func (t *repeatUntilFailTicker) Index() int { return t.index }
func (t *repeatUntilFailTicker) Tick() TickStatus {
t.bt.setTickerTickTime(t.index)
if status := t.bt.getTickerStatus(t.index); status.IsFinal() {
return status
}
t.bt.setTickerTicked(t.index)
if status := t.bt.getTickerStatus(t.index + 1); status.IsFinal() {
t.bt.resetChildren(t.index)
}
status := t.bt.getTicker(t.child).Tick()
if status != TickStatusFailure {
status = TickStatusRunning
}
t.bt.setTickerStatus(t.index, status)
return status
}
/* ============================== repeatUntilSucceed Ticker ============================== */
//
type repeatUntilSucceedTicker struct {
bt *builtTree
index int
child int
}
var _ Ticker = (*repeatUntilSucceedTicker)(nil)
func (t *repeatUntilSucceedTicker) Type() TickerType { return TickerTypeRepeatUntilSucceed }
func (t *repeatUntilSucceedTicker) Index() int { return t.index }
func (t *repeatUntilSucceedTicker) Tick() TickStatus {
t.bt.setTickerTickTime(t.index)
if status := t.bt.getTickerStatus(t.index); status.IsFinal() {
return status
}
t.bt.setTickerTicked(t.index)
if status := t.bt.getTickerStatus(t.index + 1); status.IsFinal() {
t.bt.resetChildren(t.index)
}
status := t.bt.getTicker(t.child).Tick()
if status != TickStatusSuccess {
status = TickStatusRunning
}
t.bt.setTickerStatus(t.index, status)
return status
}