-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathBTLua.lua
More file actions
883 lines (860 loc) · 24.1 KB
/
Copy pathBTLua.lua
File metadata and controls
883 lines (860 loc) · 24.1 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
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
--- BTLua
if not BTLua then
BTLua={}
end
BTLua.BTree = {}
function BTLua.BTree:new(...)
local _o = {}
_o.name=""
_o.tree=nil
_o.object=nil
_o.laststatus=nil
_o.Runningnode=nil
_o.initialized=false
_o.ticknum=0
setmetatable(_o, self)
self.__index = self
_o:init(...)
return _o
end
--------------- UTILS -------------------
local function inheritsFrom( baseClass )
local new_class = {}
local class_mt = { __index = new_class }
if baseClass then
setmetatable( new_class, { __index = baseClass } )
end
return new_class
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
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
--------------- NODE --------------------
BTLua.node = {}
function BTLua.node:new(...)
local _o = {}
setmetatable(_o, self)
self.__index = self
_o:init(...)
return _o
end
--------------- SEQUENCE ----------------
BTLua.Sequence = inheritsFrom(BTLua.node)
function BTLua.Sequence:init(...)
self.s = ""
self.n = -1
self.c = {}
local arg = { ... }
for i,v in ipairs(arg) do
table.insert(self.c,v)
end
end
function BTLua.Sequence:run(pbehavtree)
--debugprint("BTLua.Sequence:run")
--if pbehavtree.startNode then pbehavtree.startNode(pbehavtree,self) end
local _s, _child
local _ticknum = pbehavtree.ticknum
-- children loop
for i=1,#self.c do
_child = self.c[i]
--debugprint("BTLua.Sequence:run "..i)
-- if I was Running then I'll launch only childs Running or not yet launched
if self.s == "Running" and self.n == _ticknum-1 and _child.s~="Running" and _child.n == _ticknum-1 then
_s = _child.s
else
_s = _child:run(pbehavtree)
end
-- stop execution on first false or running (sequence loops children on true return)
if _s==false or _s=="Running" then
--debugprint("BTLua.Sequence ends2")
--debugprint(_s)
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
end
--debugprint("BTLua.Sequence ends")
--debugprint(_s)
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
--------------- SELECTOR ----------------
BTLua.Selector = inheritsFrom(BTLua.node)
function BTLua.Selector:init(...)
self.s = ""
self.n = -1
self.c = {}
local arg = { ... }
for i,v in ipairs(arg) do
table.insert(self.c,v)
end
end
function BTLua.Selector:run(pbehavtree)
--debugprint("BTLua.Selector:run")
--if pbehavtree.startNode then pbehavtree.startNode(pbehavtree,self) end
local _s, _child
local _ticknum = pbehavtree.ticknum
-- children loop
for i=1,#self.c do
--debugprint("BTLua.Selector "..i)
_child = self.c[i]
-- if I was Running then I'll launch only childs Running or not yet launched
if self.s == "Running" and self.n == _ticknum-1 and _child.s~="Running" and _child.n== _ticknum-1 then
_s = _child.s
else
_s = _child:run(pbehavtree)
end
-- stop execution on first true or running (selector loops children on false return)
if _s==true or _s=="Running" then
--debugprint("BTLua.Selector ends2")
--debugprint(_s)
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
end
--debugprint("BTLua.Selector ends")
--debugprint(_s)
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
--------------- RANDOMSELECTOR ----------------
BTLua.RandomSelector = inheritsFrom(BTLua.node)
function BTLua.RandomSelector:init(...)
self.s = ""
self.n = -1
self.c = {}
local arg = { ... }
for i,v in ipairs(arg) do
table.insert(self.c,v)
end
end
function BTLua.RandomSelector:run(pbehavtree)
--debugprint("BTLua.RandomSelector:run")
--if pbehavtree.startNode then pbehavtree.startNode(pbehavtree,self) end
local _s, _child
local _ticknum = pbehavtree.ticknum
-- on new run shuffle children
if self.s ~= "Running" or self.n ~= _ticknum-1 then
shuffle(self.c)
end
-- children loop
for i=1,#self.c do
_child = self.c[i]
if self.s == "Running" and self.n == _ticknum-1 and _child.s~="Running" and _child.n== _ticknum-1 then
_s = _child.s
else
_s = _child:run(pbehavtree)
end
-- stop execution on first false or running (sequence loops children on true return)
if _s==true or _s=="Running" then
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
end
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
--------------- FILTER ----------------
BTLua.Filter = inheritsFrom(BTLua.node)
function BTLua.Filter:init(pcondition,pchild,...)
self.s = ""
self.n = -1
self.c = {pchild}
self.a = pcondition
if select("#",...)>0 then
self.a2 = {...}
end
end
function BTLua.Filter:run(pbehavtree)
--debugprint("BTLua.Filter:run "..type(self.a))
--if pbehavtree.startNode then pbehavtree.startNode(pbehavtree,self) end
local _s, _child
local _ticknum = pbehavtree.ticknum
local _object, _btree = pbehavtree.object, pbehavtree
if self.s ~= "Running" or self.n ~= _ticknum-1 then
-- evaluate function
if type(self.a) == "function" then
if self.a2 then
_s = self.a(pbehavtree.object,pbehavtree, unpack(self.a2))
else
_s = self.a(pbehavtree.object,pbehavtree)
end
-- if function returns false stop and don't call children
if _s == false then
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
end
end
-- children loop
for i=1,#self.c do
_child = self.c[i]
_s = _child:run(pbehavtree)
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
--debugprint("BTLua.Filter:run result ")
--debugprint(_s)
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
--------------- DECORATOR ----------------
BTLua.Decorator = inheritsFrom(BTLua.node,...)
function BTLua.Decorator:init(pcondition,pchild,...)
self.s = ""
self.n = -1
self.c = {pchild}
self.a = pcondition
if select("#",...)>0 then
self.a2 = {...}
end
end
function BTLua.Decorator:run(pbehavtree)
--debugprint("BTLua.Decorator:run "..type(self.a))
--if pbehavtree.startNode then pbehavtree.startNode(pbehavtree,self) end
local _s, _child
local _ticknum = pbehavtree.ticknum
local _object, _btree = pbehavtree.object, pbehavtree
if self.s ~= "Running" or self.n ~= _ticknum-1 then
-- evaluate function
if type(self.a) == "function" then
if self.a2 then
_s = self.a(pbehavtree.object,pbehavtree, unpack(self.a2))
else
_s = self.a(pbehavtree.object,pbehavtree)
end
-- if function returns false stop and don't call children
if _s == false then
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
end
end
-- children loop
for i=1,#self.c do
_child = self.c[i]
_s = _child:run(pbehavtree)
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
--debugprint("BTLua.Decorator:run result ")
--debugprint(_s)
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
--------------- DECORATORCONTINUE ----------------
BTLua.DecoratorContinue = inheritsFrom(BTLua.node)
function BTLua.DecoratorContinue:init(pcondition,pchild,...)
self.s = ""
self.n = -1
self.c = {pchild}
self.a = pcondition
if select("#",...)>0 then
self.a2 = {...}
end
end
function BTLua.DecoratorContinue:run(pbehavtree)
--debugprint("BTLua.DecoratorContinue:run "..type(self.a))
--if pbehavtree.startNode then pbehavtree.startNode(pbehavtree,self) end
local _s, _child
local _ticknum = pbehavtree.ticknum
local _object, _btree = pbehavtree.object, pbehavtree
if self.s ~= "Running" or self.n ~= _ticknum-1 then
if type(self.a) == "function" then
if self.a2 then
_s = self.a(pbehavtree.object,pbehavtree, unpack(self.a2))
else
_s = self.a(pbehavtree.object,pbehavtree)
end
-- if function returns false stop, return TRUE, and don't call children
if _s == false then
_s = true
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
end
end
-- children loop
for i=1,#self.c do
_child = self.c[i]
_s = _child:run(pbehavtree)
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
--debugprint("BTLua.DecoratorContinue:run result ")
--debugprint(_s)
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
--------------- WAIT ----------------
BTLua.Wait = inheritsFrom(BTLua.node)
function BTLua.Wait:init(ptimeout,pcondition,pchild,...)
self.s = ""
self.n = -1
self.c = {pchild}
self.t = ptimeout
if self.t == nil then
self.t = 1
end
self.a = pcondition
if select("#",...)>0 then
self.a2 = {...}
end
self.t1 = os.clock()
self.s1 = false
end
function BTLua.Wait:run(pbehavtree)
--debugprint("BTLua.Wait:run "..type(self.a))
--if pbehavtree.startNode then pbehavtree.startNode(pbehavtree,self) end
local _s,_child
local _ticknum = pbehavtree.ticknum
local _object, _btree = pbehavtree.object, pbehavtree
-- on first run gets clock to check timeout
if self.s ~= "Running" or self.n ~= _ticknum-1 then
self.t1 = os.clock()
if self.a then
self.s1 = false
else
self.s1 = true
end
end
-- on timeout return false
if os.clock()-self.t1>self.t then
_s = false
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
-- if condition is false then try it
if self.s1==false then
if type(self.a) == "function" then
if self.a2 then
_s = self.a(pbehavtree.object,pbehavtree, unpack(self.a2))
else
_s = self.a(pbehavtree.object,pbehavtree)
end
self.s1 = _s
end
end
-- if condition is true launch children
if self.s1==true then
-- children loop
for i=1,#self.c do
_child = self.c[i]
_s = _child:run(pbehavtree)
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
end
-- no return so... Running
_s = "Running"
--debugprint("BTLua.Wait:run result ")
--debugprint(_s)
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
--------------- WAITContinue ----------------
BTLua.WaitContinue = inheritsFrom(BTLua.node)
function BTLua.WaitContinue:init(ptimeout,pcondition,pchild,...)
self.s = ""
self.n = -1
self.c = {pchild}
self.t = ptimeout
if self.t == nil then
self.t = 1
end
self.a = pcondition
if select("#",...)>0 then
self.a2 = {...}
end
self.t1 = os.clock()
self.s1 = false
end
function BTLua.WaitContinue:run(pbehavtree)
--debugprint("BTLua.WaitContinue:run "..type(self.a))
--if pbehavtree.startNode then pbehavtree.startNode(pbehavtree,self) end
local _s, _child
local _ticknum = pbehavtree.ticknum
local _object, _btree = pbehavtree.object, pbehavtree
-- on first run gets clock to check timeout
if self.s ~= "Running" or self.n ~= _ticknum-1 then
self.t1 = os.clock()
if self.a then
self.s1 = false
else
self.s1 = true
end
end
if os.clock()-self.t1>self.t then
_s = true
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
if self.s1==false then
if type(self.a) == "function" then
if self.a2 then
_s = self.a(pbehavtree.object,pbehavtree, unpack(self.a2))
else
_s = self.a(pbehavtree.object,pbehavtree)
end
self.s1 = _s
end
end
if self.s1==true then
for i=1,#self.c do
_child = self.c[i]
_s = _child:run(pbehavtree)
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
end
_s = "Running"
--debugprint("BTLua.WaitContinue:run result ")
--debugprint(_s)
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
--------------- REPEATUNTIL ----------------
BTLua.RepeatUntil = inheritsFrom(BTLua.node)
function BTLua.RepeatUntil:init(ptimeout,pcondition,pchild,...)
self.s = ""
self.n = -1
self.c = {pchild}
self.t = ptimeout
if self.t == nil then
self.t = 1
end
self.a = pcondition
if select("#",...)>0 then
self.a2 = {...}
end
self.t1 = os.clock()
self.s1 = false
end
function BTLua.RepeatUntil:run(pbehavtree)
--debugprint("BTLua.RepeatUntil:run "..type(self.a))
--if pbehavtree.startNode then pbehavtree.startNode(pbehavtree,self) end
local _s, _child
local _ticknum = pbehavtree.ticknum
local _object, _btree = pbehavtree.object, pbehavtree
-- on first run gets clock to check timeout
if self.s ~= "Running" or self.n ~= _ticknum-1 then
self.t1 = os.clock()
if self.a then
self.s1 = false
else
self.s1 = true
end
end
if os.clock()-self.t1>self.t then
_s = false
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
if self.s1==false then
if type(self.a) == "function" then
if self.a2 then
_s = self.a(pbehavtree.object,pbehavtree, unpack(self.a2))
else
_s = self.a(pbehavtree.object,pbehavtree)
end
self.s1 = _s
if _s == true then
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
end
end
if self.s1==false then
for i=1,#self.c do
_child = self.c[i]
_s = _child:run(pbehavtree)
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
end
_s = "Running"
--debugprint("BTLua.RepeatUntil:run result ")
--debugprint(_s)
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
--------------- SLEEP --------------------
function BTLua.Sleep(ptimeout)
return BTLua.WaitContinue:new(ptimeout,BTLua.ReturnFalse, nil)
end
--------------- CONDITION ----------------
BTLua.Condition = inheritsFrom(BTLua.node)
function BTLua.Condition:init(pcondition,...)
self.s = ""
self.n = -1
self.a = pcondition
if select("#",...)>0 then
self.a2 = {...}
end
end
function BTLua.Condition:run(pbehavtree)
--debugprint("BTLua.Condition:run "..type(self.a))
--if pbehavtree.startNode then pbehavtree.startNode(pbehavtree,self) end
local _s
local _ticknum = pbehavtree.ticknum
local _object, _btree = pbehavtree.object, pbehavtree
if type(self.a) == "function" then
if self.a2 then
_s = self.a(pbehavtree.object,pbehavtree,unpack(self.a2))
else
_s = self.a(pbehavtree.object,pbehavtree)
end
end
--debugprint("BTLua.Condition:run result ")
--debugprint(_s)
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
--------------- ACTION ----------------
BTLua.Action = inheritsFrom(BTLua.node)
function BTLua.Action:init(paction,...)
self.s = ""
self.n = -1
self.a = paction
if select("#",...)>0 then
self.a2 = {...}
end
self.r = nil
end
function BTLua.Action:run(pbehavtree)
--debugprint("BTLua.Action:run")
--if pbehavtree.startNode then pbehavtree.startNode(pbehavtree,self) end
local _s
local _ticknum = pbehavtree.ticknum
local _object, _btree = pbehavtree.object, pbehavtree
if type(self.a) == "function" then
if self.a2 then
_s = self.a(pbehavtree.object,pbehavtree,unpack(self.a2))
else
_s = self.a(pbehavtree.object,pbehavtree)
end
end
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
--------------- ActionResume ----------------
BTLua.ActionResume = inheritsFrom(BTLua.node)
function BTLua.ActionResume:init(paction,...)
self.s = ""
self.n = -1
self.a = paction
if select("#",...)>0 then
self.a2 = {...}
end
self.r = nil
end
function BTLua.ActionResume:run(pbehavtree)
--debugprint("BTLua.ActionResume:run")
--if pbehavtree.startNode then pbehavtree.startNode(pbehavtree,self) end
local _status, _s
local _ticknum = pbehavtree.ticknum
local _object, _btree = pbehavtree.object, pbehavtree
if type(self.a) == "function" then
if self.s ~= "Running" or self.n ~= _ticknum-1 then
if self.a2 then
self.r = cocreate(self.a,unpack(self.a2))
else
self.r = cocreate(self.a)
end
end
if codead(self.r) then
self.r = cocreate(self.a)
end
_status,_s = coresume(self.r, pbehavtree.object,pbehavtree)
end
self.n,self.s = _ticknum, _s
--if pbehavtree.endNode then pbehavtree.endNode(pbehavtree,self) end
return _s
end
--------------- RETURNTRUE ---------------
function BTLua.ReturnTrue()
return true
end
--------------- RETURNFALSE ---------------
function BTLua.ReturnFalse()
return false
end
--------------- RETURNRUNNING ---------------
function BTLua.ReturnRunning()
return "Running"
end
--------------- BEHAVTREE ----------------
function BTLua.BTree:init(pname,pobject,ptree,pfunctionTreeStart,pfunctionTreeEnd)
--debugprint("BTLua.BTree:init")
self.name=pname
self.object=pobject
self.tree={ptree}
self.treeStart = pfunctionTreeStart
self.treeEnd = pfunctionTreeEnd
self.initialized = false
self:initialize()
end
function BTLua.BTree:run()
--debugprint("BTLua.BTree:run "..self.name)
if (self.initialized==false) then
self:initialize()
end
if self.treeStart then
self.treeStart(self.object,self)
end
self.ticknum = self.ticknum + 1
if self.ticknum > 30000 then
for i=1,#self.tree do
self:resetTicknumChildren(self.tree[i])
end
self.ticknum = 1
end
self.laststatus = nil
local _s
for i=1,#self.tree do
--debugprint(i.."/"..#self.tree)
_s = self.tree[i]:run(self)
end
self.laststatus = _s
if self.treeEnd then
self.treeEnd(self.object,self)
end
--debugprint(_s)
return self.laststatus
end
function BTLua.BTree:initialize()
for i=1,#self.tree do
self:initializeChildren(self.tree[i],nil)
end
self.initialized=true
end
function BTLua.BTree:initializeChildren(pnode,pparent)
if pnode then
pnode.p =pparent
if type(pnode.a)=="string" then
pnode.a = self:parseFunc(pnode.a)
end
if pnode.c then
for i=1,#pnode.c do
self:initializeChildren(pnode.c[i],pnode)
end
end
end
end
function BTLua.BTree:resetTicknumChildren(pbehavtree,pnode)
if pnode then
local _ticknum = pbehavtree.ticknum
pnode.ticknum =pnode.ticknum-_ticknum
if pnode.c then
for i=1,#pnode.c do
self:resetTicknumChildren(pbehavtree,pnode.c[i])
end
end
end
end
function BTLua.BTree:addNode(pparent,pnode)
if pparent then
table.insert(pparent.c,pnode)
else
table.insert(self.tree,pnode)
end
self.initialized = false
end
function BTLua.BTree:parseTable(pparent,pexternaltable,pattributes)
if pparent == nil then
self.name = pexternaltable.name or pexternaltable.title or self.name
self.tree=nil
self.laststatus=nil
self.Runningnode=nil
self.ticknum=0
self.tree={}
end
if pexternaltable.nodes then
if pexternaltable.nodes.children then
for i = 1,#pexternaltable.nodes.children do
self:parseNodeAndAdd(nil,pexternaltable.nodes.children[i],pattributes)
end
else
for i = 1,#pexternaltable.nodes do
self:parseNodeAndAdd(nil,pexternaltable.nodes[i],pattributes)
end
end
end
self.initialized=false
end
function BTLua.BTree:parseNodeAndAdd(pparent,pnode,pattributes)
local _node = self:parseNode(pnode,pattributes)
if _node then
self:addNode(pparent,_node)
end
if pnode.children then
for i = 1,#pnode.children do
self:parseNodeAndAdd(_node,pnode.children[i],pattributes)
end
end
end
function BTLua.BTree:parseFunc(pfunc)
local _function
print(pfunc)
local _object, _btree = self.object, self
if string.sub(pfunc,1,1)=="#" then
_function = _object[string.sub(pfunc,2,-1)]
elseif string.sub(pfunc,1,1)=="@" then
_function = _btree[string.sub(pfunc,2,-1)]
elseif string.sub(pfunc,1,1)=="!" then
_function = _G[string.sub(pfunc,2,-1)]
elseif pfunc~=nil and pfunc~="" then
_function = loadstring("return "..pfunc)()
end
print(_function)
return _function
end
function BTLua.BTree:parseFuncs(pfunc)
-- Compatibility: Lua-5.0
local function split(str, delim, maxNb)
-- Eliminate bad cases...
if string.find(str, delim) == nil then
return { str }
end
if maxNb == nil or maxNb < 1 then
maxNb = 0 -- No limit
end
local result = {}
local pat = "(.-)" .. delim .. "()"
local nb = 0
local lastPos
for part, pos in string.gfind(str, pat) do
nb = nb + 1
result[nb] = part
lastPos = pos
if nb == maxNb then break end
end
-- Handle the last field
if nb ~= maxNb then
result[nb + 1] = string.sub(str, lastPos)
end
return result
end
if pfunc==nil or pfunc=="" then
return {}
end
local _funcs = split(pfunc,"|")
local _return ={}
for i,v in ipairs(_funcs) do
if v ~= "" then
local _function
local _strfunc = v
if (string.sub(_strfunc,1,1)=="'" or string.sub(_strfunc,-1)=='"') and string.sub(_strfunc,1,1)==string.sub(_strfunc,-1) then
-- string
table.insert(_return,string.sub(_strfunc,2,-2))
elseif tonumber(_strfunc)~=nil then
-- number
table.insert(_return,tonumber(_strfunc))
else
_function = self:parseFunc(_strfunc)
table.insert(_return,_function)
end
end
end
return _return
end
function BTLua.BTree:parseNode(pnode,pattributes)
local _node = nil
local _type = string.upper(pnode.type)
local _func = nil
if pnode.func then
_func = self:parseFuncs(pnode.func)
end
if _type =="START" then
return nil
end
if _type =="ACTION" then
_node = BTLua.Action:new(unpack(_func))
end
if _type =="ACTIONRESUME" then
_node = BTLua.Action:new(unpack(_func))
end
if _type =="CONDITION" then
_node = BTLua.Condition:new(unpack(_func))
end
if _type =="SELECTOR" then
_node = BTLua.Selector:new()
end
if _type =="RANDOMSELECTOR" then
_node = BTLua.RandomSelector:new()
end
if _type =="SEQUENCE" then
_node = BTLua.Sequence:new()
end
if _type =="FILTER" then
_node = BTLua.Filter:new(unpack(_func))
end
if _type =="DECORATOR" then
_node = BTLua.Decorator:new(unpack(_func))
end
if _type =="DECORATORCONTINUE" then
_node = BTLua.DecoratorContinue:new(unpack(_func))
end
if _type =="WAIT" then
_node = BTLua.Wait:new(_func[1],_func[2],nil,unpack(_func,3,table.maxn(_func)))
end
if _type =="WAITCONTINUE" then
_node = BTLua.WaitContinue:new(_func[1],_func[2],nil,unpack(_func,3,table.maxn(_func)))
end
if _type =="REPEATUNTIL" then
_node = BTLua.RepeatUntil:new(_func[1],_func[2],nil,unpack(_func,3,table.maxn(_func)))
end
if _type =="SLEEP" then
_node = BTLua.Sleep(unpack(_func))
end
if _node==nil then
error("BTLua : node type '"..pnode.type.."' unrecognized!")
end
if pattributes then
for i,v in ipairs(pattributes) do
_node[v]=pnode[v]
end
end
return _node
end
--debugprint=print