Behavior Tree 用 Lua 实现一个最简行为树

 1 local SELECTOR = 1
 2 local SEQUENCE = 2
 3 local CONDITION = 3
 4 local ACTION = 4
 5 
 6 local function Traverse(node, ...)
 7     local t = node.type
 8     if t == SELECTOR then
 9         for i=1, #node do
10             if Traverse(node[i], ...) then
11                 return true
12             end
13         end
14         return false
15     elseif t == SEQUENCE then
16         for i=1, #node do
17             if not Traverse(node[i], ...) then
18                 return false
19             end
20         end
21         return true
22     elseif t == CONDITION then
23         for i=1, #node do
24             if not node[i](...) then
25                 return false
26             end
27         end
28         return true
29     elseif t == ACTION then
30         for i=1, #node do
31             node[i](...)
32         end
33         return true
34     end
35 end
36 
37 local root =
38 {
39     type = SELECTOR,
40     {
41         type = SEQUENCE,
42         {
43             type = CONDITION,
44             function(i,j) return math.random() > i end,
45             function(i,j) return math.random() < j end,
46         },
47         {
48             type = ACTION,
49             function() print("random") end,
50         },
51     },
52     {
53         type = ACTION,
54         function() print("idle") end,
55     },
56 }
57 
58 local input1 = 0.2
59 local input2 = 0.7
60 Traverse(root, input1, input2)

有没有比 C++ 代码简单一万倍,有没有? 

https://github.com/hangj/BehaviorTree

原文地址:https://www.cnblogs.com/hangj/p/7067945.html