-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathBTLua2.lua
More file actions
338 lines (289 loc) · 8.35 KB
/
Copy pathBTLua2.lua
File metadata and controls
338 lines (289 loc) · 8.35 KB
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
-- BEHAVIOUR TREES FOR LUA
-- code originally from http://www.blizzhackers.cc/viewtopic.php?f=236&t=490980
-- updated by Leo
if not BTLua2 then
BTLua2={}
end
function BTLua2.inheritsFrom( baseClass )
local new_class = {}
local class_mt = { __index = new_class }
function new_class:create()
local newinst = {}
setmetatable( newinst, class_mt )
return newinst
end
if baseClass then
setmetatable( new_class, { __index = baseClass } )
end
return new_class
end
local cocreate = coroutine.create
local coyield = coroutine.yield
local coresume = coroutine.resume
local codead = function(co) return co == nil or coroutine.status(co) == "dead" end
-- Sleep
function BTLua2.Sleep(timeout)
return BTLua2.WaitContinue:new(function() return false end, nil, timeout)
end
local function shuffle(t)
-- see: http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
local n = #t
while n >= 2 do
-- n is now the last pertinent index
local k = math.random(n) -- 1 <= k <= n
-- Quick swap
t[n], t[k] = t[k], t[n]
n = n - 1
end
return t
end
--- BASE NODE
BTLua2.Node = {}
function BTLua2.Node:Tick(pTreeWalker)
if codead(self.runner) then
self.runner = cocreate(self.Execute)
end
local status, rv = coresume(self.runner, self, pTreeWalker);
if codead(self.runner) then
self.last_status = rv
else
self.last_status = "Running"
end
return self.last_status
end
function BTLua2.Node:Start()
self.runner = nil
self.last_status = nil
end
BTLua2.Action = BTLua2.inheritsFrom(BTLua2.Node)
function BTLua2.Action:new(func,...)
local _func = func
if type(_func)=="string" then
_func=loadstring(_func)
end
local o = { action = _func, runner = nil, type = "Action", parent = nil,args=arg}
setmetatable(o, self)
self.__index = self
return o
end
function BTLua2.Action:Execute(pTreeWalker)
if self.args then
return self.action(pTreeWalker.object,pTreeWalker, unpack(self.args))
else
return self.action(pTreeWalker.object,pTreeWalker)
end
end
BTLua2.Condition = BTLua2.inheritsFrom(BTLua2.Node)
function BTLua2.Condition:new(func,...)
local _func = func
if type(_func)=="string" then
_func=loadstring(_func)
end
local o = { action = _func, runner = nil, type = "Action", parent = nil,args=arg}
setmetatable(o, self)
self.__index = self
return o
end
function BTLua2.Condition:Execute(pTreeWalker)
if self.args then
return self.action(pTreeWalker.object,pTreeWalker, unpack(self.args))
else
return self.action(pTreeWalker.object,pTreeWalker)
end
end
--- BASE CONTAINER
BTLua2.Container = BTLua2.inheritsFrom(BTLua2.Node)
function BTLua2.Container:new(...)
local o = { children = {}, runner = nil, type = "Container", parent = nil }
setmetatable(o, self)
self.__index = self
for i,v in ipairs(arg) do
o:Add(v)
end
return o
end
function BTLua2.Container:Add(comp)
if (type(comp) == "function") then
comp = BTLua2.Action:new(comp)
end
table.insert(self.children, comp)
comp.parent = self
return self
end
BTLua2.Sequence = BTLua2.inheritsFrom(BTLua2.Container)
function BTLua2.Sequence:Execute(pTreeWalker)
for i,comp in ipairs(self.children) do
comp:Start()
while comp:Tick(pTreeWalker) == "Running" do
coyield("Running")
end
if (comp.last_status == false) then
return false
end
end
return true
end
BTLua2.PrioritySelector = BTLua2.inheritsFrom(BTLua2.Container)
function BTLua2.PrioritySelector:Execute(pTreeWalker)
for i,comp in ipairs(self.children) do
comp:Start()
while comp:Tick(pTreeWalker) == "Running" do
coyield("Running")
end
if (comp.last_status == true) then
return true
end
end
return false
end
BTLua2.RandomSelector = BTLua2.inheritsFrom(BTLua2.Container)
function BTLua2.RandomSelector:Execute(pTreeWalker)
self.children = shuffle(self.children)
for i,comp in ipairs(self.children) do
comp:Start()
while comp:Tick(pTreeWalker) == "Running" do
coyield("Running")
end
if (comp.last_status == true) then
return true
end
end
return false
end
BTLua2.AbstractDecorator = BTLua2.inheritsFrom(BTLua2.Node)
function BTLua2.AbstractDecorator:new(predicate, child)
if (type(child) == "function") then
child = BTLua2.Action:new(child)
end
local _predicate = predicate
if type(_predicate)=="string" then
_predicate=loadstring(_predicate)
end
local o = { predicate = _predicate, child = child }
setmetatable(o, self)
self.__index = self
return o
end
-- Decorator isa AstractDecorator
BTLua2.Decorator = BTLua2.inheritsFrom(BTLua2.AbstractDecorator)
function BTLua2.Decorator:Execute(pTreeWalker)
local pred_rv = self.predicate()
self.child:Start()
if pred_rv then
while self.child:Tick(pTreeWalker) == "Running" do
coyield("Running")
end
return self.child.last_status
else
return false
end
end
-- DecoratorContinue isa AstractDecorator
BTLua2.DecoratorContinue = BTLua2.inheritsFrom(BTLua2.AbstractDecorator)
function BTLua2.DecoratorContinue:Execute(pTreeWalker)
local pred_rv = self.predicate()
if pred_rv then
self.child:Start()
while self.child:Tick(pTreeWalker) == "Running" do
coyield("Running")
end
return self.child.last_status
else
return true
end
end
-- Filter isa AstractDecorator
BTLua2.Filter = BTLua2.inheritsFrom(BTLua2.AbstractDecorator)
function BTLua2.Filter:Execute(pTreeWalker)
local pred_rv = self.predicate()
self.child:Start()
if pred_rv then
while self.child:Tick(pTreeWalker) == "Running" do
coyield("Running")
end
return self.child.last_status
else
return false
end
end
-- Wait isa AbstractDecorator
BTLua2.Wait = BTLua2.inheritsFrom(BTLua2.AbstractDecorator)
function BTLua2.Wait:new(predicate, child, timeout)
local o = AbstractDecorator.new(self, predicate, child)
o.timeout = timeout
return o
end
function BTLua2.Wait:Execute(pTreeWalker)
local time_start = GetUptimeMS()
while (GetUptimeMS() - time_start < self.timeout) do
local pred_rv = self.predicate()
if pred_rv then
self.child:Start()
while self.child:Tick(pTreeWalker) == "Running" do
coyield("Running")
end
return self.child.last_status
end
coyield("Running")
end
return false
end
-- WaitContinue isa AbstractDecorator
BTLua2.WaitContinue = BTLua2.inheritsFrom(AbstractDecorator)
function BTLua2.WaitContinue:new(predicate, child, timeout)
local o = AbstractDecorator.new(self, predicate, child)
o.timeout = timeout
return o
end
function BTLua2.WaitContinue:Execute(pTreeWalker)
local time_start = GetUptimeMS()
while (GetUptimeMS() - time_start < self.timeout) do
local pred_rv = self.predicate()
if pred_rv then
self.child:Start()
while self.child:Tick(pTreeWalker) == "Running" do
coyield("Running")
end
return self.child.last_status
end
coyield("Running")
end
return true
end
-- RepeatUntil isa AbstractDecorator
BTLua2.RepeatUntil = BTLua2.inheritsFrom(AbstractDecorator)
function BTLua2.RepeatUntil:new(predicate, child, timeout)
local o = AbstractDecorator.new(self, predicate, child)
o.timeout = timeout
return o
end
function BTLua2.RepeatUntil:Execute(pTreeWalker)
local time_start = GetUptimeMS()
while (GetUptimeMS() - time_start < self.timeout) do
local pred_rv = self.predicate()
if not pred_rv then
self.child:Start()
while self.child:Tick(pTreeWalker) == "Running" do
coyield("Running")
end
if self.child.last_status == false then return false end
coyield("Running")
else
return true
end
end
return false
end
BTLua2.TreeWalker = {}
function BTLua2.TreeWalker:new(pname,pobject,logictree)
local o = {name=pname, object=pobject, logic = logictree }
setmetatable(o, self)
self.__index = self
return o
end
function BTLua2.TreeWalker:Tick()
self.logic:Tick(self)
if (self.logic.last_status ~= "Running") then
self.logic:Start()
end
end