-
Notifications
You must be signed in to change notification settings - Fork 0
/
bevtree.go
466 lines (362 loc) · 10.3 KB
/
bevtree.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
package bevtree
import (
"path"
"sync"
"sync/atomic"
"unsafe"
"github.com/GodYY/gutils/assert"
"github.com/pkg/errors"
)
// Node type.
type NodeType string
func (t NodeType) Valid() bool { return string(t) != "" }
func (t NodeType) String() string { return string(t) }
// Node represents the structure portion of behavior tree.
// Node defines the basic function of node of behavior tree.
// Nodes must implement the Node interface to be considered
// behavior tree nodes.
type Node interface {
// Node type.
NodeType() NodeType
// Get the parent node.
Parent() Node
// Set the parent node.
SetParent(Node)
// Get the comment.
Comment() string
// Set the comment.
SetComment(string)
}
// The common part of node.
type node struct {
parent Node
comment string
}
func newNode() node {
return node{}
}
func (n *node) Parent() Node { return n.parent }
func (n *node) SetParent(parent Node) {
n.parent = parent
}
func (n *node) Comment() string { return n.comment }
func (n *node) SetComment(comment string) { n.comment = comment }
// status indicate the status of node's runtime.
type status int8
const (
// The initail state.
sNone = status(iota)
// Running.
sRunning
// Terminated.
sTerminated
// Was stopped.
sStopped
// Was destroyed.
sDestroyed
)
// The strings represent the status values.
var statusStrings = [...]string{
sNone: "none",
sRunning: "running",
sTerminated: "terminated",
sStopped: "stopped",
sDestroyed: "destroyed",
}
func (s status) String() string { return statusStrings[s] }
// lazyStop indicate node's runtime how to stop.
type lazyStop int8
const (
// Don't need to stop.
lzsNone = lazyStop(iota)
// Stop before update.
lzsBeforeUpdate
// Stop after update.
lzsAfterUpdate
)
// The strings represent the lazyStop values.
var lazyStopStrings = [...]string{
lzsNone: "none",
lzsBeforeUpdate: "before-Update",
lzsAfterUpdate: "after-Update",
}
func (l lazyStop) String() string { return lazyStopStrings[l] }
// Result represents the running results of node's runtime and even behavior trees.
type Result int8
const (
// Success can indicate that the behavior ran successfully,
// or the node made a decision successfully, or the behavior
// tree ran successfully.
Success = Result(iota)
// Failure can indicate that the behavior fails to run, or
// the node fails to make a decision, or the behavior tree
// fails to run.
Failure
// Running can indicate that a behavior run is running, or
// that a node is making a decision, or that the behavior
// tree is running.
Running
)
// The strings repesents the Result values.
var resultStrings = [...]string{
Success: "success",
Failure: "failure",
Running: "running",
}
func (r Result) String() string { return resultStrings[r] }
// TaskType indicate how the task will run.
type TaskType int8
const (
// Single task, no any subtask.
Single = TaskType(iota)
// Serial task, there are subtasks and the subtasks run one
// by one.
Serial
// Parallel task, there are subtasks and the subtasks run
// together.
Parallel
)
var taskTypeStrings = [...]string{
Single: "single",
Serial: "serial",
Parallel: "parallel",
}
func (tt TaskType) String() string { return taskTypeStrings[tt] }
// Task represents the independent parts of behavir tree node.
// Task maintains runtime data and implements the logic of the
// corresponding node.
type Task interface {
// Get the TaskType.
TaskType() TaskType
// OnCreate is called immediately after the Task is created.
// node indicates the node on which the Task is created.
OnCreate(node Node)
// OnInit is called before the first update of the Task.
// nextChildNodes is used to return the child nodes that need
// to run next. ctx represents the running context of the
// behavior tree.
OnInit(nextChildNodes NodeList, ctx Context) bool
// OnUpdate is called until the Task is terminated.
OnUpdate(ctx Context) Result
// OnTerminate is called either after ths last update or when
// stopping it.
OnTerminate(ctx Context)
// OnChildTerminated is called when a sub Task is terminated.
//
// result Indicates the running result of the subtask.
// nextChildNodes is used to return the child nodes that need to
// run next.
//
// OnChildTerminated returns the decision result.
OnChildTerminated(result Result, nextChildNodes NodeList, ctx Context) Result
}
// Root node, a special node in behavior tree. it has
// only one child and no parent. It returns result of
// child directly.
type rootNode struct {
child Node
}
func newRootNode() *rootNode {
return &rootNode{}
}
func (rootNode) NodeType() NodeType { return root }
func (rootNode) Parent() Node { return nil }
func (rootNode) SetParent(Node) {}
func (rootNode) Comment() string { return "" }
func (rootNode) SetComment(string) {}
func (r *rootNode) Child() Node { return r.child }
func (r *rootNode) SetChild(child Node) {
assert.Assert(child == nil || child.Parent() == nil, "child already has parent")
if r.child != nil {
r.child.SetParent(nil)
r.child = nil
}
if child != nil {
child.SetParent(r)
r.child = child
}
}
// rootNode Task.
type rootTask struct {
node *rootNode
}
// rootNode Task is serail task.
func (r *rootTask) TaskType() TaskType { return Serial }
func (r *rootTask) OnCreate(node Node) { r.node = node.(*rootNode) }
func (r *rootTask) OnInit(nextChildNodes NodeList, ctx Context) bool {
if r.node.Child() == nil {
return false
} else {
nextChildNodes.PushNode(r.node.Child())
return true
}
}
func (r *rootTask) OnUpdate(ctx Context) Result { return Running }
func (r *rootTask) OnTerminate(ctx Context) { r.node = nil }
func (r *rootTask) OnChildTerminated(result Result, nextChildNodes NodeList, ctx Context) Result {
// Returns result of child directly.
return result
}
// Tree interface, make the tree readonly while in use.
type Tree interface {
// Get tree anme.
Name() string
// Get tree comment.
Comment() string
// Get root node.
root() *rootNode
internal
}
// tree, contains the structure data
type tree struct {
// The name of the behavior tree.
name string
// The comment of the behavior tree.
comment string
// The _root node of behavior tree.
_root *rootNode
internalImpl
}
func NewTree(name string) *tree {
assert.AssertF(name != "", "invalid name \"%s\"", name)
tree := &tree{
name: name,
_root: newRootNode(),
}
return tree
}
func (t *tree) Name() string { return t.name }
func (t *tree) SetName(name string) {
assert.AssertF(name != "", "invalid name \"%s\"", name)
t.name = name
}
func (t *tree) Comment() string { return t.comment }
func (t *tree) SetComment(comment string) { t.comment = comment }
func (t *tree) Root() *rootNode { return t._root }
func (t *tree) root() *rootNode { return t._root }
type treeAsset struct {
entry *TreeEntry
once *sync.Once
tree *tree
}
type Framework struct {
*meta
initialized bool
loadAll bool
configPathRoot string
treeAssets map[string]*treeAsset
}
func NewFramework() *Framework {
return &Framework{
meta: newMeta(),
}
}
func (s *Framework) RegsiterNodeType(nodeType NodeType, nodeCreator func() Node, taskCreator func() Task) {
if s.initialized {
panic("bevtree framework initialized")
}
s.meta.RegisterNodeType(nodeType, nodeCreator, taskCreator)
}
func (s *Framework) RegisterBevType(bevType BevType, creator func() Bev) {
if s.initialized {
panic("bevtree framework initialized")
}
s.meta.RegisterBevType(bevType, creator)
}
func (s *Framework) Init(cfgPath string) error {
if s.initialized {
return errors.New("bevtree framework repeated initialization")
}
config, err := loadConfig(cfgPath)
if err != nil {
return errors.WithMessagef(err, "bevtree framework init")
}
s.configPathRoot = path.Dir(cfgPath)
s.treeAssets = make(map[string]*treeAsset, len(config.TreeEntries))
s.loadAll = config.LoadAll
if len(config.TreeEntries) > 0 {
for _, entry := range config.TreeEntries {
var ta *treeAsset
if s.loadAll {
tree := new(tree)
path := path.Join(s.configPathRoot, entry.Path)
if err = s.DecodeXMLTreeFile(path, tree); err == nil {
if tree.Name() != entry.Name {
err = errors.Errorf("load tree \"%s\": name don't match config name \"%s\"", tree.Name(), entry.Name)
}
}
if err != nil {
return errors.WithMessage(err, "bevtree framework init")
}
ta = &treeAsset{entry: entry, tree: tree}
} else {
ta = &treeAsset{entry: entry, once: new(sync.Once)}
}
if s.treeAssets[entry.Name] != nil {
return errors.Errorf("bevtree framework init: duplcate tree name \"%s\"", entry.Name)
}
s.treeAssets[entry.Name] = ta
}
}
s.initialized = true
return nil
}
func (s *Framework) loadTree(ta *treeAsset) (*tree, error) {
var err error
ta.once.Do(func() {
tree := new(tree)
path := path.Join(s.configPathRoot, ta.entry.Path)
if err = s.DecodeXMLTreeFile(path, tree); err != nil {
return
}
if tree.Name() != ta.entry.Name {
err = errors.Errorf("loadTree \"%s\": tree name don't match config name \"%s\"", tree.Name(), ta.entry.Name)
}
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&ta.tree)), unsafe.Pointer(tree))
})
if err != nil {
return nil, err
} else if ta.tree == nil {
return nil, errors.Errorf("loadTree \"%s\" failed", ta.entry.Name)
} else {
return ta.tree, nil
}
}
func (s *Framework) GetOrLoadTree(name string) (Tree, error) {
if !s.initialized {
return nil, errors.New("bevtree framework uninitialized")
}
return s.getOrLoadTree(name)
}
func (s *Framework) getOrLoadTree(name string) (*tree, error) {
ta := s.treeAssets[name]
if ta == nil {
return nil, nil
}
if s.loadAll {
return ta.tree, nil
} else {
tree := (*tree)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&ta.tree))))
if tree != nil {
return tree, nil
}
var err error
if tree, err = s.loadTree(ta); err != nil {
return nil, errors.WithMessage(err, "bevtree framework GetOrLoadTree")
}
return tree, nil
}
}
func (s *Framework) CreateEntity(treeName string, userData interface{}) (Entity, error) {
if !s.initialized {
return nil, errors.New("bevtree framework uninitialized")
}
tree, err := s.getOrLoadTree(treeName)
if err != nil {
return nil, err
} else if tree == nil {
return nil, errors.Errorf("bevtree framework CreateEntity: tree \"%s\" not exist", treeName)
} else {
return newEntity(newContext(s, tree, userData)), nil
}
}