-
Notifications
You must be signed in to change notification settings - Fork 16
/
BehaviorTree2.lua
314 lines (252 loc) · 6.68 KB
/
BehaviorTree2.lua
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
local BehaviorTree = {} do
local function ProcessNode(node, nodes)
if node.type == "task" then
local task = {
type = "task",
status = nil,
start = node.params.start,
run = node.params.run,
finish = node.params.finish,
onsuccess = true,
onfail = false
}
--decorators
if node.params.decorator ~= nil then
if node.params.decorator == "always_succeed" then
task.task = {
running = function(self) task.status = "running" end,
success = function(self) task.status = "success" end,
fail = function(self) task.status = "success" end
}
elseif node.params.decorator == "always_fail" then
task.task = {
running = function(self) task.status = "running" end,
success = function(self) task.status = "fail" end,
fail = function(self) task.status = "fail" end
}
elseif node.params.decorator == "invert" then
task.task = {
running = function(self) task.status = "running" end,
success = function(self) task.status = "fail" end,
fail = function(self) task.status = "success" end
}
end
else
task.task = {
running = function(self) task.status = "running" end,
success = function(self) task.status = "success" end,
fail = function(self) task.status = "fail" end
}
end
nodes[#nodes + 1] = task
elseif node.type == "decorator" then
node.params.node.params.decorator = node.params.decorator
ProcessNode(node.params.node, nodes)
elseif node.type == "sequence" then
-- child.success = final ? parent.success : parent.next
-- child.fail = parent.fail (default)
for i,childNode in pairs(node.params.nodes) do
local final = i == #node.params.nodes
local start = #nodes + 1
ProcessNode(childNode, nodes)
for i = start, #nodes do
local node = nodes[i]
-- on child.success, !final ? parent.next : parent.success
if node.onsuccess == true then
node.onsuccess = not final and #nodes + 1 or true
end
if node.onfail == true then
node.onfail = not final and #nodes + 1 or true
end
end
end
elseif node.type == "priority" then
-- child.success = parent.success (default)
-- child.fail = final ? parent.fail : parent.next
for i,childNode in pairs(node.params.nodes) do
local final = i == #node.params.nodes
local start = #nodes + 1
ProcessNode(childNode, nodes)
for i = start, #nodes do
local node = nodes[i]
-- on child.fail, !final ? parent.next : parent.fail
if node.onsuccess == false then
node.onsuccess = not final and #nodes + 1 or false
end
if node.onfail == false then
node.onfail = not final and #nodes + 1 or false
end
end
end
elseif node.type == "random" then
-- child.success = parent.success (default)
-- child.fail = parent.fail (default)
local random = {
type = "random",
indices = {}
}
nodes[#nodes + 1] = random
for _,childNode in pairs(node.params.nodes) do
if childNode.params.weight then
-- childNode.weight = math.clamp(childNode.weight, 1, 200)
local base = #random.indices
local index = #nodes + 1
for i = 1, childNode.params.weight do
random.indices[base + i] = index
end
else
random.indices[#random.indices + 1] = #nodes + 1
end
ProcessNode(childNode, nodes)
end
elseif node.type == "tree" then
local start = #nodes + 1
ProcessNode(node.tree, nodes)
for i = start, #nodes do
local node = nodes[i]
if node.onsuccess == true or node.onsuccess == false then
node.onsuccess = #nodes + 1
end
if node.onfail == true or node.onfail == false then
node.onfail = #nodes + 1
end
end
else
error("ProcessNode: bad node.type " .. tostring(node.type))
end
end
local TreeProto = {}
function TreeProto:run(dt)
if self.running then
-- warn(debug.traceback("Tried to run BehaviorTree while it was already running"))
return
end
local nodes = self.nodes
local obj = self.object
local i = self.index
local nodeCount = #nodes
local didResume = self.paused
self.paused = false
self.running = true
while i <= nodeCount do
local node = nodes[i]
if node.type == "task" then
local task = node.task
if didResume then
didResume = false
elseif node.start then
node.start(task, obj, dt)
end
node.status = nil
node.run(task, obj, dt)
if not node.status then
warn("node.run did not call success, running or fail, acting as fail")
node.status = "fail"
end
if node.status == "running" then
self.paused = true
break
elseif node.status == "success" then
if node.finish then
node.finish(task, obj, dt)
end
i = node.onsuccess
elseif node.status == "fail" then
if node.finish then
node.finish(task, obj, dt)
end
i = node.onfail
else
error("bad node.status")
end
elseif node.type == "random" then
i = node.indices[math.random(1, #node.indices)]
else
error("bad node.type")
end
end
self.index = i <= nodeCount and i or 1
self.running = false
end
function TreeProto:setObject(object)
self.object = object
end
function TreeProto:clone()
return setmetatable({
nodes = self.nodes,
index = self.index,
object = self.object
}, { __index = TreeProto })
end
function BehaviorTree:new(params)
local tree = params.tree
local nodes = {}
ProcessNode({ type = "tree", tree = tree }, nodes)
return setmetatable({
nodes = nodes,
index = 1,
object = nil
}, { __index = TreeProto })
end
BehaviorTree.Task = {
new = function(_, params)
return {
type = "task",
params = params
}
end
}
BehaviorTree.Sequence = {
new = function(_, params)
return {
type = "sequence",
params = params
}
end
}
BehaviorTree.Priority = {
new = function(_, params)
return {
type = "priority",
params = params
}
end
}
BehaviorTree.Random = {
new = function(_, params)
return {
type = "random",
params = params
}
end
}
--Decorators
BehaviorTree.AlwaysSucceedDecorator = {
new = function(_, params)
params.decorator = "always_succeed"
return {
type = "decorator",
params = params
}
end
}
BehaviorTree.AlwaysFailDecorator = {
new = function(_, params)
params.decorator = "always_fail"
return {
type = "decorator",
params = params
}
end
}
BehaviorTree.InvertDecorator = {
new = function(_, params)
params.decorator = "invert"
return {
type = "decorator",
params = params
}
end
}
end
return BehaviorTree