Fix a lot of bugs in PartsManager
This commit is contained in:
parent
0ca5905ab6
commit
4a19ddf72b
75
script.lua
75
script.lua
@ -78,6 +78,15 @@ function filter(func, table)
|
|||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param tbl table
|
||||||
|
---@param val any
|
||||||
|
function has_value(tbl, val)
|
||||||
|
for _, v in pairs(tbl) do
|
||||||
|
if v==val then return true end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
--- Unordered reduction, only use when working with dictionaries and
|
--- Unordered reduction, only use when working with dictionaries and
|
||||||
--- execution order does not matter
|
--- execution order does not matter
|
||||||
---@param tbl table Table to reduce
|
---@param tbl table Table to reduce
|
||||||
@ -86,8 +95,8 @@ end
|
|||||||
function reduce(func, tbl, init)
|
function reduce(func, tbl, init)
|
||||||
local result = init
|
local result = init
|
||||||
local first_loop = true
|
local first_loop = true
|
||||||
for _, v in pairs(table) do
|
for _, v in pairs(tbl) do
|
||||||
if first_loop and init ~= nil then
|
if first_loop and init == nil then
|
||||||
result=v
|
result=v
|
||||||
else
|
else
|
||||||
result = func(result, v)
|
result = func(result, v)
|
||||||
@ -104,8 +113,8 @@ end
|
|||||||
function ireduce(func, tbl, init)
|
function ireduce(func, tbl, init)
|
||||||
local result = init
|
local result = init
|
||||||
local first_loop = true
|
local first_loop = true
|
||||||
for _, v in ipairs(table) do
|
for _, v in ipairs(tbl) do
|
||||||
if first_loop and init ~= nil then
|
if first_loop and init == nil then
|
||||||
result=v
|
result=v
|
||||||
else
|
else
|
||||||
result = func(result, v)
|
result = func(result, v)
|
||||||
@ -178,41 +187,61 @@ end
|
|||||||
|
|
||||||
-- Parts management -- {{{
|
-- Parts management -- {{{
|
||||||
do
|
do
|
||||||
|
PartsManager={}
|
||||||
local pm={}
|
local pm={}
|
||||||
--- Add function to part in Parts Manager
|
|
||||||
--- @
|
local function initPart(part)
|
||||||
function addPartFunction(part, func)
|
|
||||||
local part_key=tostring(part)
|
local part_key=tostring(part)
|
||||||
if pm[part_key] == nil then
|
if pm[part_key] == nil then
|
||||||
pm[part_key]={}
|
pm[part_key]={}
|
||||||
end
|
end
|
||||||
table.insert(pm[part_key]["functions"], func)
|
pm[part_key].part=part
|
||||||
pm[part_key]["part"]=part
|
if pm[part_key].functions == nil then
|
||||||
end
|
pm[part_key].functions = {}
|
||||||
|
end
|
||||||
function addGroupFunction(group, func)
|
if pm[part_key].eval_mode==nil then
|
||||||
for _, v in pairs(group) do
|
pm[part_key].eval_mode="chain"
|
||||||
addPartFunction(v, func)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
--- Add function to part in Parts Manager
|
||||||
function evaluatePart(part)
|
--- @
|
||||||
|
function PartsManager.addPartFunction(part, func)
|
||||||
|
initPart(part)
|
||||||
local part_key=tostring(part)
|
local part_key=tostring(part)
|
||||||
return ireduce(function(x, y) return x() and y() end,
|
table.insert(pm[part_key]["functions"], func)
|
||||||
pm[part_key].functions, true)
|
end
|
||||||
|
function PartsManager.setPartEvalMode(part, mode)
|
||||||
|
initPart(part)
|
||||||
|
local part_key=tostring(part)
|
||||||
|
mode=((mode=="and" or mode=="or" or mode=="chain") and
|
||||||
|
mode or "chain")
|
||||||
|
pm[part_key].eval_mode=mode
|
||||||
end
|
end
|
||||||
|
|
||||||
function refreshPart(part)
|
function PartsManager.evaluatePart(part)
|
||||||
|
local part_key=tostring(part)
|
||||||
|
local evalFunc=function(x, y) return x() and y() end
|
||||||
|
if pm[part_key].eval_mode=="or" then
|
||||||
|
evalFunc=function(x, y) return x() or y() end
|
||||||
|
elseif pm[part_key].eval_mode=="chain" then
|
||||||
|
evalFunc=function(x, y) return y(x) end
|
||||||
|
return ireduce(evalFunc, pm[part_key].functions, true)
|
||||||
|
end
|
||||||
|
return ireduce(evalFunc, pm[part_key].functions)
|
||||||
|
end
|
||||||
|
local evaluatePart=PartsManager.evaluatePart
|
||||||
|
|
||||||
|
function PartsManager.refreshPart(part)
|
||||||
local part_enabled=evaluatePart(part)
|
local part_enabled=evaluatePart(part)
|
||||||
part.setEnabled(part_enabled)
|
part.setEnabled(part_enabled)
|
||||||
return part_enabled
|
return part_enabled
|
||||||
end
|
end
|
||||||
|
function PartsManager.refreshAll()
|
||||||
function refreshGroup(group)
|
for k, v in pairs(pm) do
|
||||||
for _, v in pairs(group) do
|
PartsManager.refreshPart(v.part)
|
||||||
refreshPart(v)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
-- }}}
|
-- }}}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user