diff --git a/api.lua b/api.lua index 3d234cf..cc88c8c 100644 --- a/api.lua +++ b/api.lua @@ -117,16 +117,8 @@ local function add_stage(p_stage) end -- Add to lbm_nodenames - if ((stage.restart == false) or (stage.restart == nil)) then - --[[ TODO: Make add and delete into a function: - for i=#yl_api_nodestages.lbm_nodenames,1,-1 do - if (yl_api_nodestages.lbm_nodenames[i] == modname .. ":" .. nodename) then - table.remove(yl_api_nodestages.lbm_nodenames, i) - break - end - end - table.insert(yl_api_nodestages.lbm_nodenames, stage.stage_name) - ]]-- + if ((stage.restart == true) or (stage.restart == nil)) then + yl_api_nodestages.lbm_add_to_restart(stage.stage_name) end target_def._stage = stage @@ -208,9 +200,11 @@ local function overwrite_stage(p_stage) end end + -- Remove from lbm_nodenames + yl_api_nodestages.lbm_remove_from_restart(stage.stage_name) -- Add to lbm_nodenames - if ((stage.restart == false) or (stage.restart == nil)) then - table.insert(yl_api_nodestages.lbm_nodenames, stage.stage_name) + if ((stage.restart == true) or (stage.restart == nil)) then + yl_api_nodestages.lbm_add_to_restart(stage.stage_name) end -- Assign the new values @@ -275,15 +269,10 @@ local function rollback_stage(modname, nodename) end -- Remove current from lbm_names - for i=#yl_api_nodestages.lbm_nodenames,1,-1 do - if (yl_api_nodestages.lbm_nodenames[i] == modname .. ":" .. nodename) then - table.remove(yl_api_nodestages.lbm_nodenames, i) - break - end - end + yl_api_nodestages.lbm_remove_from_restart(modname .. ":" .. nodename) -- Add to lbm_nodenames - if ((previous.stage.restart == false) or (previous.stage.restart == nil)) then - table.insert(yl_api_nodestages.lbm_nodenames, previous.stage.stage_name) + if ((previous.stage.restart == true) or (previous.stage.restart == nil)) then + yl_api_nodestages.lbm_add_to_restart(previous.stage.stage_name) end def._stage = previous.stage @@ -327,12 +316,7 @@ local function delete_stage(modname, nodename) end -- Remove current from lbm_names - for i=#yl_api_nodestages.lbm_nodenames,1,-1 do - if (yl_api_nodestages.lbm_nodenames[i] == modname .. ":" .. nodename) then - table.remove(yl_api_nodestages.lbm_nodenames, i) - break - end - end + yl_api_nodestages.lbm_remove_from_restart(modname .. ":" .. nodename) def._stage = nil def.on_timer = nil diff --git a/internal.lua b/internal.lua index 9814833..a83f96d 100644 --- a/internal.lua +++ b/internal.lua @@ -13,3 +13,4 @@ function yl_api_nodestages.log(text) return log(text) end dofile(yl_api_nodestages.modpath .. "internal_nodefunctions.lua") dofile(yl_api_nodestages.modpath .. "internal_validation.lua") +dofile(yl_api_nodestages.modpath .. "internal_lbm.lua") diff --git a/internal_lbm.lua b/internal_lbm.lua new file mode 100644 index 0000000..0b444c6 --- /dev/null +++ b/internal_lbm.lua @@ -0,0 +1,54 @@ +-- lbm_remove_from_restart +-- + +local function lbm_remove_from_restart(target) + for i=#yl_api_nodestages.lbm_nodenames,1,-1 do + if (yl_api_nodestages.lbm_nodenames[i] == target) then + table.remove(yl_api_nodestages.lbm_nodenames, i) + return true + end + end + return false +end + +function yl_api_nodestages.lbm_remove_from_restart(target) + return lbm_remove_from_restart(target) +end + +-- lbm_add_to_restart +-- + +local function lbm_add_to_restart(target) + local found = false + for _, nodename in ipairs(yl_api_nodestages.lbm_nodenames) do + if (nodename == target) then + found = true + end + end + if (found == false) then + table.insert(yl_api_nodestages.lbm_nodenames, target) + return true + end + return false +end + +function yl_api_nodestages.lbm_add_to_restart(target) + return lbm_add_to_restart(target) +end + +-- lbm_action +-- + +local function lbm_action(pos, node, _dtime_s) + local t = minetest.get_node_timer(pos) + if t:is_started() == false then + local stage = minetest.registered_nodes[node.name]._stage + if (stage and stage.duration and (type(stage.duration) == "number") and (stage.duration > 0)) then + t:start(stage.duration) + end + end +end + +function yl_api_nodestages.lbm_action(pos, node, dtime_s) + return lbm_action(pos, node, dtime_s) +end \ No newline at end of file diff --git a/lbm.lua b/lbm.lua index 8da04b3..339b636 100644 --- a/lbm.lua +++ b/lbm.lua @@ -1,5 +1,5 @@ local function action(pos, node, dtime_s) - + return yl_api_nodestages.lbm_action(pos, node, dtime_s) end local def = {