-
Notifications
You must be signed in to change notification settings - Fork 2
/
bt_common.lua
75 lines (65 loc) · 1.71 KB
/
bt_common.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
local Const = require "const"
local M = {}
local function st2str(status)
if status == Const.SUCCESS then
return "SUCCESS"
elseif status == Const.FAIL then
return "FAIL"
else
return "RUNNING"
end
end
function M.execute(node, tick, level)
local node_data = tick[node] or {}
node_data.__level = level
tick[node] = node_data
if tick.log then
tick.exepath:on_exe_node(tick, level, node)
end
-- open callback
if not node_data.is_open then
if node.open then
local ret = node:open(tick, node_data)
if ret then
if tick.log then
tick.exepath:on_node_status(node, st2str(ret))
end
return ret
end
end
node_data.is_open = true
end
-- run callback, get status
local status = node:run(tick, node_data)
if tick.log then
tick.exepath:on_node_status(tick, node, st2str(status))
end
-- close callback
if status == Const.RUNNING then
tick.open_nodes[node] = true
return status
else
node_data.is_open = false
if node.close then
node:close(tick, node_data)
end
return status
end
end
-- 根据权重决定子节点索引的顺序
function M.reorder(indexes, weight, total)
for i = 1, #indexes do
local rnd = math.random(total)
local acc = 0
for j = i, #indexes do
local w = weight[indexes[j]]
acc = acc + w
if rnd <= acc then
indexes[i], indexes[j] = indexes[j], indexes[i]
total = total - w
break
end
end
end
end
return M