-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorator.go
241 lines (193 loc) · 5.73 KB
/
decorator.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
package bevtree
import "github.com/GodYY/gutils/assert"
// DecoratorNode interface indicates the functions that
// a decorator node in behavior tree must implement.
type DecoratorNode interface {
Node
// Get the child node.
Child() Node
// Set the child node.
SetChild(Node)
}
// The common part of decoratoer node.
type decoratorNode struct {
node
child Node
}
func newDecoratorNode() decoratorNode {
return decoratorNode{
node: newNode(),
}
}
func (d *decoratorNode) Child() Node { return d.child }
func (d *decoratorNode) setChild(child Node) bool {
if child == nil || child.Parent() != nil {
return false
}
if d.child != nil {
d.child.SetParent(nil)
}
d.child = child
return child != nil
}
// Inverter node runs child and returns the reverse value of
// child's result.
type InverterNode struct {
decoratorNode
}
func NewInverterNode() *InverterNode {
return &InverterNode{decoratorNode: newDecoratorNode()}
}
func (i *InverterNode) NodeType() NodeType { return inverter }
func (i *InverterNode) SetChild(child Node) {
if i.decoratorNode.setChild(child) {
child.SetParent(i)
}
}
// The inverter node task.
type inverterTask struct {
node *InverterNode
}
func (i *inverterTask) TaskType() TaskType { return Serial }
func (i *inverterTask) OnCreate(node Node) { i.node = node.(*InverterNode) }
func (i *inverterTask) OnInit(nextChildNodes NodeList, ctx Context) bool {
if i.node.Child() == nil {
return false
} else {
nextChildNodes.PushNode(i.node.Child())
return true
}
}
func (i *inverterTask) OnUpdate(ctx Context) Result { return Running }
func (i *inverterTask) OnTerminate(ctx Context) { i.node = nil }
func (i *inverterTask) OnChildTerminated(result Result, _ NodeList, ctx Context) Result {
if result == Success {
return Failure
} else {
return Success
}
}
// Succeeder node runs child node and always return success.
type SucceederNode struct {
decoratorNode
}
func NewSucceederNode() *SucceederNode {
return &SucceederNode{decoratorNode: newDecoratorNode()}
}
func (s *SucceederNode) NodeType() NodeType { return succeeder }
func (s *SucceederNode) SetChild(child Node) {
if s.decoratorNode.setChild(child) {
child.SetParent(s)
}
}
// Succeeder node task.
type succeederTask struct {
node *SucceederNode
}
func (s *succeederTask) TaskType() TaskType { return Serial }
func (s *succeederTask) OnCreate(node Node) { s.node = node.(*SucceederNode) }
func (s *succeederTask) OnInit(nextChildNodes NodeList, ctx Context) bool {
if s.node.Child() == nil {
return false
} else {
nextChildNodes.PushNode(s.node.Child())
return true
}
}
func (s *succeederTask) OnUpdate(ctx Context) Result { return Running }
func (s *succeederTask) OnTerminate(ctx Context) { s.node = nil }
func (s *succeederTask) OnChildTerminated(result Result, _ NodeList, ctx Context) Result {
return Success
}
// Repeater node runs child node in limited times until child
// returns failure. It returns the result of child directly.
type RepeaterNode struct {
decoratorNode
limited int
}
func NewRepeaterNode(limited int) *RepeaterNode {
assert.Assert(limited > 0, "invalid limited")
return &RepeaterNode{
decoratorNode: newDecoratorNode(),
limited: limited,
}
}
func (r *RepeaterNode) NodeType() NodeType { return repeater }
func (r *RepeaterNode) SetChild(child Node) {
if r.decoratorNode.setChild(child) {
child.SetParent(r)
}
}
func (r *RepeaterNode) Limited() int { return r.limited }
// Repeater node task.
type repeaterTask struct {
node *RepeaterNode
count int
}
func (r *repeaterTask) TaskType() TaskType { return Serial }
func (r *repeaterTask) OnCreate(node Node) { r.node = node.(*RepeaterNode); r.count = 0 }
func (r *repeaterTask) OnInit(nextChildNodes NodeList, ctx Context) bool {
if r.node.Child() == nil {
return false
} else {
nextChildNodes.PushNode(r.node.Child())
return true
}
}
func (r *repeaterTask) OnUpdate(ctx Context) Result { return Running }
func (r *repeaterTask) OnTerminate(ctx Context) { r.node = nil }
func (r *repeaterTask) OnChildTerminated(result Result, nextChildNodes NodeList, ctx Context) Result {
r.count++
if result != Failure && r.count < r.node.limited {
nextChildNodes.PushNode(r.node.Child())
return Running
} else {
return result
}
}
// RepeatUntilFail node runs child node until child returns
// failure. It returns success if successOnFail is true or
// failure.
type RepeatUntilFailNode struct {
decoratorNode
successOnFail bool
}
func NewRepeatUntilFailNode(successOnFail bool) *RepeatUntilFailNode {
return &RepeatUntilFailNode{
decoratorNode: newDecoratorNode(),
successOnFail: successOnFail,
}
}
func (r *RepeatUntilFailNode) NodeType() NodeType { return repeatUntilFail }
func (r *RepeatUntilFailNode) SetChild(child Node) {
if r.decoratorNode.setChild(child) {
child.SetParent(r)
}
}
func (r *RepeatUntilFailNode) SuccessOnFail() bool { return r.successOnFail }
// RepeatUntilFail node task.
type repeatUntilFailTask struct {
node *RepeatUntilFailNode
}
func (r *repeatUntilFailTask) TaskType() TaskType { return Serial }
func (r *repeatUntilFailTask) OnCreate(node Node) { r.node = node.(*RepeatUntilFailNode) }
func (r *repeatUntilFailTask) OnInit(nextChildNodes NodeList, ctx Context) bool {
if r.node.Child() == nil {
return false
} else {
nextChildNodes.PushNode(r.node.Child())
return true
}
}
func (r *repeatUntilFailTask) OnUpdate(ctx Context) Result { return Running }
func (r *repeatUntilFailTask) OnTerminate(ctx Context) { r.node = nil }
func (r *repeatUntilFailTask) OnChildTerminated(result Result, nextChildNodes NodeList, ctx Context) Result {
if result == Success {
nextChildNodes.PushNode(r.node.Child())
return Running
} else if result == Failure && r.node.successOnFail {
return Success
} else {
return result
}
}