tactree/init.lua

209 lines
4.6 KiB
Lua

local Tactree = {}
local Nodes = {}
local function NodeStart(node, args)
-- abort if started
if node.data then return end
node.data = node.parent and node.parent.data or args
node.data[node] = node.data[node] or {}
node:_start()
end
local function NodeUpdate(node)
-- abort if not started
if not node.data then return end
local result = node:_tick()
if type(result) == 'boolean' then
node:_finish()
node.data = nil
end
return result
end
local function NodePrivateData(node)
return node.data[node]
end
local function default_impl(node) end
function Tactree.Leaf(name)
return function(t)
if Nodes[name] then
error('node with name \'' .. name .. '\' already exists.', 2)
else
Nodes[name] = {}
end
local node_type = Nodes[name]
local tick = t.tick or t[1]
if type(tick) ~= 'function' then error('no tick function supplied', 2) end
node_type._tick = tick
node_type._start = type(t.start) == 'function' and t.start or default_impl
node_type._finish = type(t.finish) == 'function' and t.finish or default_impl
end
end
function Tactree.Composite(name)
return function(parent_type_name)
return function(children)
if Nodes[name] then
error('node with name \'' .. name .. '\' already exists.', 2)
else
if not Nodes[parent_type_name] then error('no such node \'' .. name .. '\'', 2) end
Nodes[name] = setmetatable({}, { __index = Nodes[parent_type_name] })
end
local node_type = Nodes[name]
node_type.children = {}
for _, child in ipairs(children) do
if type(child) ~= 'string' then error('invalid child node', 2) end
table.insert(node_type.children, child)
end
end
end
end
function Tactree.Tree(name)
if not Nodes[name] then error('no such node \'' .. name .. '\'', 2) end
local node = {}
node.children = {}
node.start = NodeStart
node.update = NodeUpdate
node.private = NodePrivateData
-- this will crash if there is a recursive structure
-- find a way to prevent this?
if Nodes[name].children then
for _, child in ipairs(Nodes[name].children) do
local cn = Tactree.Tree(child)
cn.parent = node
table.insert(node.children, cn)
end
end
return setmetatable(node, { __index = Nodes[name] })
end
Tactree.Leaf 'Sequence'
{
start = function(node)
node:private().current_child = 1
node.children[node:private().current_child]:start()
end,
tick = function(node)
local result = node.children[node:private().current_child]:update()
if type(result) == 'boolean' then
if result then
if node:private().current_child == #node.children then
return true
else
node:private().current_child = node:private().current_child + 1
node.children[node:private().current_child]:start()
end
else
return false
end
end
end
}
Tactree.Leaf 'Selector'
{
start = function(node)
node:private().current_child = 1
node.children[node:private().current_child]:start()
end,
tick = function(node)
local result = node.children[node:private().current_child]:update()
if type(result) == 'boolean' then
if not result then
if node:private().current_child == #node.children then
return false
else
node:private().current_child = node:private().current_child + 1
node.children[node:private().current_child]:start()
end
else
return true
end
end
end
}
Tactree.Leaf 'RepeatUntilFail'
{
start = function(node)
node.children[1]:start()
end,
tick = function(node)
local result = node.children[1]:update()
if type(result) == 'boolean' then
if not result then
return true
else
node.children[1]:start()
end
end
end
}
Tactree.Leaf 'RepeatUntilPass'
{
start = function(node)
node.children[1]:start()
end,
tick = function(node)
local result = node.children[1]:update()
if type(result) == 'boolean' then
if result then
return true
else
node.children[1]:start()
end
end
end
}
Tactree.Leaf 'RepeatForever'
{
start = function(node)
node.children[1]:start()
end,
tick = function(node)
local result = node.children[1]:update()
if type(result) == 'boolean' then node.children[1]:start() end
end
}
--[[
Example usage
-------------
Tactree.Leaf 'Find nearest object' { function (node) ... end }
Tactree.Leaf 'Pick up object' { function (node) ... end}
Tactree.Leaf 'Move' { function (node) return node.data.creature:move(node.data.tx, node.data.ty, dt) end }
Tactree.Composite 'Pick up nearest object' 'Sequence' { 'Find nearest object', 'Move', 'Pick up object' }
local tr = Tactree.Tree 'Pick up nearest object'
tr:start{ ... } ( or, if you already have a table, tr:start(existing_table) )
t:update()
]]--
return Tactree