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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值