From fee53f38d5f089754e364a388c1fdf710d8c25c1 Mon Sep 17 00:00:00 2001 From: Jude Melton-Houghton Date: Thu, 19 May 2022 18:36:26 -0400 Subject: [PATCH 1/7] Speed up internal info getters a little bit --- mesecons/internal.lua | 96 ++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 57 deletions(-) diff --git a/mesecons/internal.lua b/mesecons/internal.lua index 6f52b44..4a9c63d 100644 --- a/mesecons/internal.lua +++ b/mesecons/internal.lua @@ -50,48 +50,55 @@ mesecon.fifo_queue = dofile(minetest.get_modpath("mesecons").."/fifo_queue.lua") -- General -function mesecon.get_effector(nodename) - if minetest.registered_nodes[nodename] - and minetest.registered_nodes[nodename].mesecons - and minetest.registered_nodes[nodename].mesecons.effector then - return minetest.registered_nodes[nodename].mesecons.effector + +local function get_mesecons_spec(nodename) + local def = minetest.registered_nodes[nodename] + return def and def.mesecons +end + +local function get_rules(def, node) + local rules = def.rules + if type(rules) == 'function' then + return rules(node) + elseif rules then + return rules end + return mesecon.rules.default +end + +function mesecon.get_effector(nodename) + local mesecons_spec = get_mesecons_spec(nodename) + return mesecons_spec and mesecons_spec.effector end function mesecon.get_receptor(nodename) - if minetest.registered_nodes[nodename] - and minetest.registered_nodes[nodename].mesecons - and minetest.registered_nodes[nodename].mesecons.receptor then - return minetest.registered_nodes[nodename].mesecons.receptor - end + local mesecons_spec = get_mesecons_spec(nodename) + return mesecons_spec and mesecons_spec.receptor end function mesecon.get_conductor(nodename) - if minetest.registered_nodes[nodename] - and minetest.registered_nodes[nodename].mesecons - and minetest.registered_nodes[nodename].mesecons.conductor then - return minetest.registered_nodes[nodename].mesecons.conductor - end + local mesecons_spec = get_mesecons_spec(nodename) + return mesecons_spec and mesecons_spec.conductor end function mesecon.get_any_outputrules(node) - if not node then return nil end - - if mesecon.is_conductor(node.name) then - return mesecon.conductor_get_rules(node) - elseif mesecon.is_receptor(node.name) then - return mesecon.receptor_get_rules(node) - end + if not node then return end + local mesecons_spec = get_mesecons_spec(node.name) + if not mesecons_spec then return end + local conductor = mesecons_spec.conductor + if conductor then return get_rules(conductor, node) end + local receptor = mesecons_spec.receptor + if receptor then return get_rules(receptor, node) end end function mesecon.get_any_inputrules(node) - if not node then return nil end - - if mesecon.is_conductor(node.name) then - return mesecon.conductor_get_rules(node) - elseif mesecon.is_effector(node.name) then - return mesecon.effector_get_rules(node) - end + if not node then return end + local mesecons_spec = get_mesecons_spec(node.name) + if not mesecons_spec then return end + local conductor = mesecons_spec.conductor + if conductor then return get_rules(conductor, node) end + local effector = mesecons_spec.effector + if effector then return get_rules(effector, node) end end function mesecon.get_any_rules(node) @@ -127,16 +134,7 @@ end function mesecon.receptor_get_rules(node) local receptor = mesecon.get_receptor(node.name) - if receptor then - local rules = receptor.rules - if type(rules) == 'function' then - return rules(node) - elseif rules then - return rules - end - end - - return mesecon.rules.default + return receptor and get_rules(receptor, node) or mesecon.rules.default end -- Effectors @@ -167,15 +165,7 @@ end function mesecon.effector_get_rules(node) local effector = mesecon.get_effector(node.name) - if effector then - local rules = effector.rules - if type(rules) == 'function' then - return rules(node) - elseif rules then - return rules - end - end - return mesecon.rules.default + return effector and get_rules(effector, node) or mesecon.rules.default end -- ####################### @@ -340,15 +330,7 @@ end function mesecon.conductor_get_rules(node) local conductor = mesecon.get_conductor(node.name) - if conductor then - local rules = conductor.rules - if type(rules) == 'function' then - return rules(node) - elseif rules then - return rules - end - end - return mesecon.rules.default + return conductor and get_rules(conductor, node) or mesecon.rules.default end -- some more general high-level stuff From 468ec521eab7d0ddbfc65c7f373d9d1b66a21639 Mon Sep 17 00:00:00 2001 From: Jude Melton-Houghton Date: Thu, 19 May 2022 20:33:09 -0400 Subject: [PATCH 2/7] Improve linking --- mesecons/init.lua | 8 +++---- mesecons/internal.lua | 53 +++++++++++++++++++------------------------ 2 files changed, 27 insertions(+), 34 deletions(-) diff --git a/mesecons/init.lua b/mesecons/init.lua index 83e611b..0b6c23d 100644 --- a/mesecons/init.lua +++ b/mesecons/init.lua @@ -77,8 +77,8 @@ mesecon.queue:add_function("receptor_on", function (pos, rules) -- Call turnon on all linking positions for _, rule in ipairs(mesecon.flattenrules(rules)) do local np = vector.add(pos, rule) - local rulenames = mesecon.rules_link_rule_all(pos, rule) - for _, rulename in ipairs(rulenames) do + local rulename = mesecon.link(pos, np) + if rulename then mesecon.turnon(np, rulename) end end @@ -96,8 +96,8 @@ mesecon.queue:add_function("receptor_off", function (pos, rules) -- Call turnoff on all linking positions for _, rule in ipairs(mesecon.flattenrules(rules)) do local np = vector.add(pos, rule) - local rulenames = mesecon.rules_link_rule_all(pos, rule) - for _, rulename in ipairs(rulenames) do + local rulename = mesecon.link(pos, np) + if rulename then mesecon.vm_begin() mesecon.changesignal(np, minetest.get_node(np), rulename, mesecon.state.off, 2) diff --git a/mesecons/internal.lua b/mesecons/internal.lua index 4a9c63d..a3980ea 100644 --- a/mesecons/internal.lua +++ b/mesecons/internal.lua @@ -423,7 +423,8 @@ function mesecon.turnon(pos, link) for _, r in ipairs(mesecon.rule2meta(f.link, rules)) do local np = vector.add(f.pos, r) if not pos_can_be_skipped[minetest.hash_node_position(np)] then - for _, l in ipairs(mesecon.rules_link_rule_all(f.pos, r)) do + local l = mesecon.link(f.pos, np) + if l then frontiers:add({pos = np, link = l}) end end @@ -490,14 +491,15 @@ function mesecon.turnoff(pos, link) -- Check if an onstate receptor is connected. If that is the case, -- abort this turnoff process by returning false. `receptor_off` will -- discard all the changes that we made in the voxelmanip: - if mesecon.rules_link_rule_all_inverted(f.pos, r)[1] then + if mesecon.link_inverted(f.pos, np) then if mesecon.is_receptor_on(mesecon.get_node_force(np).name) then return false end end -- Call turnoff on neighbors - for _, l in ipairs(mesecon.rules_link_rule_all(f.pos, r)) do + local l = mesecon.link(f.pos, np) + if l then frontiers:add({pos = np, link = l}) end end @@ -533,44 +535,35 @@ function mesecon.turnoff(pos, link) return true end --- Get all linking inputrules of inputnode (effector or conductor) that is connected to --- outputnode (receptor or conductor) at position `output` and has an output in direction `rule` -function mesecon.rules_link_rule_all(output, rule) - local input = vector.add(output, rule) +-- Get the linking inputrule of inputnode (effector or conductor) that is connected to +-- outputnode (receptor or conductor) between positions `output` and `input` +function mesecon.link(output, input) local inputnode = mesecon.get_node_force(input) local inputrules = mesecon.get_any_inputrules(inputnode) - if not inputrules then - return {} - end - local rules = {} + if not inputrules then return end + local dx, dy, dz = output.x - input.x, output.y - input.y, output.z - input.z for _, inputrule in ipairs(mesecon.flattenrules(inputrules)) do -- Check if input accepts from output - if vector.equals(vector.add(input, inputrule), output) then - table.insert(rules, inputrule) + if inputrule.x == dx and inputrule.y == dy and inputrule.z == dz then + return inputrule end end - - return rules end --- Get all linking outputnodes of outputnode (receptor or conductor) that is connected to --- inputnode (effector or conductor) at position `input` and has an input in direction `rule` -function mesecon.rules_link_rule_all_inverted(input, rule) - local output = vector.add(input, rule) +-- Get the linking outputrule of outputnode (receptor or conductor) that is connected to +-- inputnode (effector or conductor) between positions `input` and `output` +function mesecon.link_inverted(input, output) local outputnode = mesecon.get_node_force(output) local outputrules = mesecon.get_any_outputrules(outputnode) - if not outputrules then - return {} - end - local rules = {} + if not outputrules then return end + local dx, dy, dz = input.x - output.x, input.y - output.y, input.z - output.z for _, outputrule in ipairs(mesecon.flattenrules(outputrules)) do - if vector.equals(vector.add(output, outputrule), input) then - table.insert(rules, mesecon.invertRule(outputrule)) + if outputrule.x == dx and outputrule.y == dy and outputrule.z == dz then + return outputrule end end - return rules end function mesecon.is_powered(pos, rule) @@ -583,8 +576,8 @@ function mesecon.is_powered(pos, rule) if not rule then for _, rule in ipairs(mesecon.flattenrules(rules)) do - local rulenames = mesecon.rules_link_rule_all_inverted(pos, rule) - for _, rname in ipairs(rulenames) do + local rname = mesecon.link_inverted(pos, vector.add(pos, rule)) + if rname then local np = vector.add(pos, rname) local nn = mesecon.get_node_force(np) @@ -595,8 +588,8 @@ function mesecon.is_powered(pos, rule) end end else - local rulenames = mesecon.rules_link_rule_all_inverted(pos, rule) - for _, rname in ipairs(rulenames) do + local rname = mesecon.link_inverted(pos, vector.add(pos, rule)) + if rname then local np = vector.add(pos, rname) local nn = mesecon.get_node_force(np) if (mesecon.is_conductor_on (nn, mesecon.invertRule(rname)) From d4acc6a8a61cbead8f9b33ac6dbd09d98a6884a4 Mon Sep 17 00:00:00 2001 From: Jude Melton-Houghton Date: Thu, 19 May 2022 21:17:48 -0400 Subject: [PATCH 3/7] Avoid most node copies --- mesecons/internal.lua | 24 +++++++++------- mesecons/util.lua | 43 ++++++++++++++++++++-------- mesecons_button/init.lua | 2 ++ mesecons_delayer/init.lua | 2 ++ mesecons_extrawires/corner.lua | 2 ++ mesecons_extrawires/doublecorner.lua | 1 + mesecons_extrawires/tjunction.lua | 2 ++ mesecons_gates/init.lua | 4 +++ mesecons_insulated/init.lua | 2 ++ mesecons_lamp/init.lua | 2 ++ mesecons_pistons/init.lua | 4 +++ mesecons_receiver/init.lua | 1 + mesecons_solarpanel/init.lua | 2 ++ mesecons_torch/init.lua | 1 + mesecons_walllever/init.lua | 2 ++ 15 files changed, 72 insertions(+), 22 deletions(-) diff --git a/mesecons/internal.lua b/mesecons/internal.lua index a3980ea..5fb5cc7 100644 --- a/mesecons/internal.lua +++ b/mesecons/internal.lua @@ -59,6 +59,10 @@ end local function get_rules(def, node) local rules = def.rules if type(rules) == 'function' then + if not def.const_node then + -- Copy the node to avoid overwriting data in the cache + node = {name = node.name, param1 = node.param1, param2 = node.param2} + end return rules(node) elseif rules then return rules @@ -336,7 +340,7 @@ end -- some more general high-level stuff function mesecon.is_power_on(pos, rulename) - local node = mesecon.get_node_force(pos) + local node = mesecon.get_node_force_nocopy(pos) if node and (mesecon.is_conductor_on(node, rulename) or mesecon.is_receptor_on(node.name)) then return true end @@ -344,7 +348,7 @@ function mesecon.is_power_on(pos, rulename) end function mesecon.is_power_off(pos, rulename) - local node = mesecon.get_node_force(pos) + local node = mesecon.get_node_force_nocopy(pos) if node and (mesecon.is_conductor_off(node, rulename) or mesecon.is_receptor_off(node.name)) then return true end @@ -410,7 +414,7 @@ function mesecon.turnon(pos, link) local depth = 1 for f in frontiers:iter() do - local node = mesecon.get_node_force(f.pos) + local node = mesecon.get_node_force_nocopy(f.pos) if not node then -- Area does not exist; do nothing @@ -475,7 +479,7 @@ function mesecon.turnoff(pos, link) local depth = 1 for f in frontiers:iter() do - local node = mesecon.get_node_force(f.pos) + local node = mesecon.get_node_force_nocopy(f.pos) if not node then -- Area does not exist; do nothing @@ -492,7 +496,7 @@ function mesecon.turnoff(pos, link) -- abort this turnoff process by returning false. `receptor_off` will -- discard all the changes that we made in the voxelmanip: if mesecon.link_inverted(f.pos, np) then - if mesecon.is_receptor_on(mesecon.get_node_force(np).name) then + if mesecon.is_receptor_on(mesecon.get_node_force_nocopy(np).name) then return false end end @@ -538,7 +542,7 @@ end -- Get the linking inputrule of inputnode (effector or conductor) that is connected to -- outputnode (receptor or conductor) between positions `output` and `input` function mesecon.link(output, input) - local inputnode = mesecon.get_node_force(input) + local inputnode = mesecon.get_node_force_nocopy(input) local inputrules = mesecon.get_any_inputrules(inputnode) if not inputrules then return end @@ -554,7 +558,7 @@ end -- Get the linking outputrule of outputnode (receptor or conductor) that is connected to -- inputnode (effector or conductor) between positions `input` and `output` function mesecon.link_inverted(input, output) - local outputnode = mesecon.get_node_force(output) + local outputnode = mesecon.get_node_force_nocopy(output) local outputrules = mesecon.get_any_outputrules(outputnode) if not outputrules then return end @@ -567,7 +571,7 @@ function mesecon.link_inverted(input, output) end function mesecon.is_powered(pos, rule) - local node = mesecon.get_node_force(pos) + local node = mesecon.get_node_force_nocopy(pos) local rules = mesecon.get_any_inputrules(node) if not rules then return false end @@ -579,7 +583,7 @@ function mesecon.is_powered(pos, rule) local rname = mesecon.link_inverted(pos, vector.add(pos, rule)) if rname then local np = vector.add(pos, rname) - local nn = mesecon.get_node_force(np) + local nn = mesecon.get_node_force_nocopy(np) if (mesecon.is_conductor_on(nn, mesecon.invertRule(rname)) or mesecon.is_receptor_on(nn.name)) then @@ -591,7 +595,7 @@ function mesecon.is_powered(pos, rule) local rname = mesecon.link_inverted(pos, vector.add(pos, rule)) if rname then local np = vector.add(pos, rname) - local nn = mesecon.get_node_force(np) + local nn = mesecon.get_node_force_nocopy(np) if (mesecon.is_conductor_on (nn, mesecon.invertRule(rname)) or mesecon.is_receptor_on (nn.name)) then table.insert(sourcepos, np) diff --git a/mesecons/util.lua b/mesecons/util.lua index b87dffd..c43fcb3 100644 --- a/mesecons/util.lua +++ b/mesecons/util.lua @@ -390,16 +390,21 @@ local function vm_get_or_create_entry(pos) return tbl end --- Gets the node at a given position during a VoxelManipulator-based --- transaction. -function mesecon.vm_get_node(pos) +local function vm_get_node_nocopy(pos) local hash = minetest.hash_node_position(pos) local node = vm_node_cache[hash] if not node then node = vm_get_or_create_entry(pos).vm:get_node_at(pos) vm_node_cache[hash] = node end - return node.name ~= "ignore" and {name = node.name, param1 = node.param1, param2 = node.param2} or nil + return node.name ~= "ignore" and node or nil +end + +-- Gets the node at a given position during a VoxelManipulator-based +-- transaction. +function mesecon.vm_get_node(pos) + local node = vm_get_node_nocopy(pos) + return node and {name = node.name, param1 = node.param1, param2 = node.param2} end -- Sets a node’s name during a VoxelManipulator-based transaction. @@ -424,6 +429,18 @@ function mesecon.vm_swap_node(pos, name, update_light) tbl.dirty = true end +-- Get node, loading map if necessary. +local function get_node_load(pos) + local node = minetest.get_node_or_nil(pos) + if node == nil then + -- Node is not currently loaded; use a VoxelManipulator to prime + -- the mapblock cache and try again. + minetest.get_voxel_manip(pos, pos) + node = minetest.get_node_or_nil(pos) + end + return node +end + -- Gets the node at a given position, regardless of whether it is loaded or -- not, respecting a transaction if one is in progress. -- @@ -435,14 +452,16 @@ function mesecon.get_node_force(pos) if vm_cache then return mesecon.vm_get_node(pos) else - local node = minetest.get_node_or_nil(pos) - if node == nil then - -- Node is not currently loaded; use a VoxelManipulator to prime - -- the mapblock cache and try again. - minetest.get_voxel_manip(pos, pos) - node = minetest.get_node_or_nil(pos) - end - return node + return get_node_load(pos) + end +end + +-- Same without copying the internal node. Not part of public API. +function mesecon.get_node_force_nocopy(pos) + if vm_cache then + return vm_get_node_nocopy(pos) + else + return get_node_load(pos) end end diff --git a/mesecons_button/init.lua b/mesecons_button/init.lua index 7c70923..df2f12d 100644 --- a/mesecons_button/init.lua +++ b/mesecons_button/init.lua @@ -55,6 +55,7 @@ minetest.register_node("mesecons_button:button_off", { sounds = mesecon.node_sound.stone, mesecons = {receptor = { state = mesecon.state.off, + const_node = true, rules = mesecon.rules.buttonlike_get }}, on_blast = mesecon.on_blastnode, @@ -96,6 +97,7 @@ minetest.register_node("mesecons_button:button_on", { sounds = mesecon.node_sound.stone, mesecons = {receptor = { state = mesecon.state.on, + const_node = true, rules = mesecon.rules.buttonlike_get }}, on_timer = mesecon.button_turnoff, diff --git a/mesecons_delayer/init.lua b/mesecons_delayer/init.lua index bc95806..15186a4 100644 --- a/mesecons_delayer/init.lua +++ b/mesecons_delayer/init.lua @@ -108,10 +108,12 @@ local off_state = { receptor = { state = mesecon.state.off, + const_node = true, rules = delayer_get_output_rules }, effector = { + const_node = true, rules = delayer_get_input_rules, action_on = delayer_activate } diff --git a/mesecons_extrawires/corner.lua b/mesecons_extrawires/corner.lua index 3aca209..ac082a4 100644 --- a/mesecons_extrawires/corner.lua +++ b/mesecons_extrawires/corner.lua @@ -34,6 +34,7 @@ minetest.register_node("mesecons_extrawires:corner_on", { mesecons = {conductor = { state = mesecon.state.on, + const_node = true, rules = corner_get_rules, offstate = "mesecons_extrawires:corner_off" }}, @@ -60,6 +61,7 @@ minetest.register_node("mesecons_extrawires:corner_off", { mesecons = {conductor = { state = mesecon.state.off, + const_node = true, rules = corner_get_rules, onstate = "mesecons_extrawires:corner_on" }}, diff --git a/mesecons_extrawires/doublecorner.lua b/mesecons_extrawires/doublecorner.lua index de6051f..0742da6 100644 --- a/mesecons_extrawires/doublecorner.lua +++ b/mesecons_extrawires/doublecorner.lua @@ -62,6 +62,7 @@ for k, state in ipairs(doublecorner_states) do mesecons = { conductor = { states = doublecorner_states, + const_node = true, rules = doublecorner_get_rules, }, }, diff --git a/mesecons_extrawires/tjunction.lua b/mesecons_extrawires/tjunction.lua index ba68f75..b065a47 100644 --- a/mesecons_extrawires/tjunction.lua +++ b/mesecons_extrawires/tjunction.lua @@ -46,6 +46,7 @@ minetest.register_node("mesecons_extrawires:tjunction_on", { mesecons = {conductor = { state = mesecon.state.on, + const_node = true, rules = tjunction_get_rules, offstate = "mesecons_extrawires:tjunction_off" }}, @@ -76,6 +77,7 @@ minetest.register_node("mesecons_extrawires:tjunction_off", { mesecons = {conductor = { state = mesecon.state.off, + const_node = true, rules = tjunction_get_rules, onstate = "mesecons_extrawires:tjunction_on" }}, diff --git a/mesecons_gates/init.lua b/mesecons_gates/init.lua index 51c29b3..e36f798 100644 --- a/mesecons_gates/init.lua +++ b/mesecons_gates/init.lua @@ -99,8 +99,10 @@ local function register_gate(name, inputnumber, assess, recipe, description) groups = {dig_immediate = 2, overheat = 1}, mesecons = { receptor = { state = "off", + const_node = true, rules = gate_get_output_rules }, effector = { + const_node = true, rules = get_inputrules, action_change = update_gate }} @@ -118,8 +120,10 @@ local function register_gate(name, inputnumber, assess, recipe, description) groups = {dig_immediate = 2, not_in_creative_inventory = 1, overheat = 1}, mesecons = { receptor = { state = "on", + const_node = true, rules = gate_get_output_rules }, effector = { + const_node = true, rules = get_inputrules, action_change = update_gate }} diff --git a/mesecons_insulated/init.lua b/mesecons_insulated/init.lua index c641e94..d2555cf 100644 --- a/mesecons_insulated/init.lua +++ b/mesecons_insulated/init.lua @@ -38,6 +38,7 @@ minetest.register_node("mesecons_insulated:insulated_on", { mesecons = {conductor = { state = mesecon.state.on, offstate = "mesecons_insulated:insulated_off", + const_node = true, rules = insulated_wire_get_rules }}, on_blast = mesecon.on_blastnode, @@ -74,6 +75,7 @@ minetest.register_node("mesecons_insulated:insulated_off", { mesecons = {conductor = { state = mesecon.state.off, onstate = "mesecons_insulated:insulated_on", + const_node = true, rules = insulated_wire_get_rules }}, on_blast = mesecon.on_blastnode, diff --git a/mesecons_lamp/init.lua b/mesecons_lamp/init.lua index aff1f26..c89e42c 100644 --- a/mesecons_lamp/init.lua +++ b/mesecons_lamp/init.lua @@ -31,6 +31,7 @@ minetest.register_node("mesecons_lamp:lamp_on", { action_off = function (pos, node) minetest.swap_node(pos, {name = "mesecons_lamp:lamp_off", param2 = node.param2}) end, + const_node = true, rules = mesecon.rules.wallmounted_get, }}, on_blast = mesecon.on_blastnode, @@ -56,6 +57,7 @@ minetest.register_node("mesecons_lamp:lamp_off", { action_on = function (pos, node) minetest.swap_node(pos, {name = "mesecons_lamp:lamp_on", param2 = node.param2}) end, + const_node = true, rules = mesecon.rules.wallmounted_get, }}, on_blast = mesecon.on_blastnode, diff --git a/mesecons_pistons/init.lua b/mesecons_pistons/init.lua index bc940d0..16b9633 100644 --- a/mesecons_pistons/init.lua +++ b/mesecons_pistons/init.lua @@ -281,6 +281,7 @@ minetest.register_node("mesecons_pistons:piston_normal_off", { sounds = mesecon.node_sound.wood, mesecons = {effector={ action_on = piston_on, + const_node = true, rules = piston_get_rules, }}, on_punch = piston_punch, @@ -311,6 +312,7 @@ minetest.register_node("mesecons_pistons:piston_normal_on", { sounds = mesecon.node_sound.wood, mesecons = {effector={ action_off = piston_off, + const_node = true, rules = piston_get_rules, }}, on_rotate = piston_rotate_on, @@ -360,6 +362,7 @@ minetest.register_node("mesecons_pistons:piston_sticky_off", { sounds = mesecon.node_sound.wood, mesecons = {effector={ action_on = piston_on, + const_node = true, rules = piston_get_rules, }}, on_punch = piston_punch, @@ -390,6 +393,7 @@ minetest.register_node("mesecons_pistons:piston_sticky_on", { sounds = mesecon.node_sound.wood, mesecons = {effector={ action_off = piston_off, + const_node = true, rules = piston_get_rules, }}, on_rotate = piston_rotate_on, diff --git a/mesecons_receiver/init.lua b/mesecons_receiver/init.lua index 301dbc4..4077fcb 100644 --- a/mesecons_receiver/init.lua +++ b/mesecons_receiver/init.lua @@ -68,6 +68,7 @@ mesecon.register_node("mesecons_receiver:receiver", { }, mesecons = {conductor = { state = mesecon.state.off, + const_node = true, rules = receiver_get_rules, onstate = "mesecons_receiver:receiver_on" }} diff --git a/mesecons_solarpanel/init.lua b/mesecons_solarpanel/init.lua index 84bbfd3..1c09889 100644 --- a/mesecons_solarpanel/init.lua +++ b/mesecons_solarpanel/init.lua @@ -21,12 +21,14 @@ mesecon.register_node("mesecons_solarpanel:solar_panel", { groups = {dig_immediate = 3}, mesecons = {receptor = { state = mesecon.state.off, + const_node = true, rules = mesecon.rules.wallmounted_get }} },{ groups = {dig_immediate = 3, not_in_creative_inventory = 1}, mesecons = {receptor = { state = mesecon.state.on, + const_node = true, rules = mesecon.rules.wallmounted_get }}, }) diff --git a/mesecons_torch/init.lua b/mesecons_torch/init.lua index 34f3c4d..7d05d43 100644 --- a/mesecons_torch/init.lua +++ b/mesecons_torch/init.lua @@ -67,6 +67,7 @@ minetest.register_node("mesecons_torch:mesecon_torch_off", { sounds = mesecon.node_sound.default, mesecons = {receptor = { state = mesecon.state.off, + const_node = true, rules = torch_get_output_rules }}, on_blast = mesecon.on_blastnode, diff --git a/mesecons_walllever/init.lua b/mesecons_walllever/init.lua index 670dab5..18c6c89 100644 --- a/mesecons_walllever/init.lua +++ b/mesecons_walllever/init.lua @@ -34,6 +34,7 @@ mesecon.register_node("mesecons_walllever:wall_lever", { mesh="jeija_wall_lever_off.obj", on_rotate = mesecon.buttonlike_onrotate, mesecons = {receptor = { + const_node = true, rules = mesecon.rules.buttonlike_get, state = mesecon.state.off }}, @@ -48,6 +49,7 @@ mesecon.register_node("mesecons_walllever:wall_lever", { mesh="jeija_wall_lever_on.obj", on_rotate = false, mesecons = {receptor = { + const_node = true, rules = mesecon.rules.buttonlike_get, state = mesecon.state.on }}, From 569677dee4c4755dbd13ec37399de90b2c015f75 Mon Sep 17 00:00:00 2001 From: Jude Melton-Houghton Date: Thu, 19 May 2022 22:31:01 -0400 Subject: [PATCH 4/7] Fix use of link_inverted The function was supposed to return an outputrule, but before it inverted the returned rules, so these usages of the function expected inverted rules. --- mesecons/internal.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/mesecons/internal.lua b/mesecons/internal.lua index 5fb5cc7..ebac27b 100644 --- a/mesecons/internal.lua +++ b/mesecons/internal.lua @@ -580,23 +580,23 @@ function mesecon.is_powered(pos, rule) if not rule then for _, rule in ipairs(mesecon.flattenrules(rules)) do - local rname = mesecon.link_inverted(pos, vector.add(pos, rule)) + local np = vector.add(pos, rule) + local rname = mesecon.link_inverted(pos, np) if rname then - local np = vector.add(pos, rname) local nn = mesecon.get_node_force_nocopy(np) - if (mesecon.is_conductor_on(nn, mesecon.invertRule(rname)) + if (mesecon.is_conductor_on(nn, rname) or mesecon.is_receptor_on(nn.name)) then table.insert(sourcepos, np) end end end else - local rname = mesecon.link_inverted(pos, vector.add(pos, rule)) + local np = vector.add(pos, rule) + local rname = mesecon.link_inverted(pos, np) if rname then - local np = vector.add(pos, rname) local nn = mesecon.get_node_force_nocopy(np) - if (mesecon.is_conductor_on (nn, mesecon.invertRule(rname)) + if (mesecon.is_conductor_on (nn, rname) or mesecon.is_receptor_on (nn.name)) then table.insert(sourcepos, np) end From 49645b10b5e1c8dfbdd921a93e1bd9922836b211 Mon Sep 17 00:00:00 2001 From: Jude Melton-Houghton Date: Sun, 29 May 2022 16:24:25 -0400 Subject: [PATCH 5/7] Rename const_node to rule_node_nocopy --- mesecons/internal.lua | 2 +- mesecons_button/init.lua | 4 ++-- mesecons_delayer/init.lua | 4 ++-- mesecons_extrawires/corner.lua | 4 ++-- mesecons_extrawires/doublecorner.lua | 2 +- mesecons_extrawires/tjunction.lua | 4 ++-- mesecons_gates/init.lua | 8 ++++---- mesecons_insulated/init.lua | 4 ++-- mesecons_lamp/init.lua | 4 ++-- mesecons_pistons/init.lua | 8 ++++---- mesecons_receiver/init.lua | 2 +- mesecons_solarpanel/init.lua | 4 ++-- mesecons_torch/init.lua | 2 +- mesecons_walllever/init.lua | 4 ++-- 14 files changed, 28 insertions(+), 28 deletions(-) diff --git a/mesecons/internal.lua b/mesecons/internal.lua index ebac27b..7ddbe8c 100644 --- a/mesecons/internal.lua +++ b/mesecons/internal.lua @@ -59,7 +59,7 @@ end local function get_rules(def, node) local rules = def.rules if type(rules) == 'function' then - if not def.const_node then + if not def.rule_node_nocopy then -- Copy the node to avoid overwriting data in the cache node = {name = node.name, param1 = node.param1, param2 = node.param2} end diff --git a/mesecons_button/init.lua b/mesecons_button/init.lua index df2f12d..40eb927 100644 --- a/mesecons_button/init.lua +++ b/mesecons_button/init.lua @@ -55,7 +55,7 @@ minetest.register_node("mesecons_button:button_off", { sounds = mesecon.node_sound.stone, mesecons = {receptor = { state = mesecon.state.off, - const_node = true, + rule_node_nocopy = true, rules = mesecon.rules.buttonlike_get }}, on_blast = mesecon.on_blastnode, @@ -97,7 +97,7 @@ minetest.register_node("mesecons_button:button_on", { sounds = mesecon.node_sound.stone, mesecons = {receptor = { state = mesecon.state.on, - const_node = true, + rule_node_nocopy = true, rules = mesecon.rules.buttonlike_get }}, on_timer = mesecon.button_turnoff, diff --git a/mesecons_delayer/init.lua b/mesecons_delayer/init.lua index 15186a4..0fa0695 100644 --- a/mesecons_delayer/init.lua +++ b/mesecons_delayer/init.lua @@ -108,12 +108,12 @@ local off_state = { receptor = { state = mesecon.state.off, - const_node = true, + rule_node_nocopy = true, rules = delayer_get_output_rules }, effector = { - const_node = true, + rule_node_nocopy = true, rules = delayer_get_input_rules, action_on = delayer_activate } diff --git a/mesecons_extrawires/corner.lua b/mesecons_extrawires/corner.lua index ac082a4..cc6e817 100644 --- a/mesecons_extrawires/corner.lua +++ b/mesecons_extrawires/corner.lua @@ -34,7 +34,7 @@ minetest.register_node("mesecons_extrawires:corner_on", { mesecons = {conductor = { state = mesecon.state.on, - const_node = true, + rule_node_nocopy = true, rules = corner_get_rules, offstate = "mesecons_extrawires:corner_off" }}, @@ -61,7 +61,7 @@ minetest.register_node("mesecons_extrawires:corner_off", { mesecons = {conductor = { state = mesecon.state.off, - const_node = true, + rule_node_nocopy = true, rules = corner_get_rules, onstate = "mesecons_extrawires:corner_on" }}, diff --git a/mesecons_extrawires/doublecorner.lua b/mesecons_extrawires/doublecorner.lua index 0742da6..884dca6 100644 --- a/mesecons_extrawires/doublecorner.lua +++ b/mesecons_extrawires/doublecorner.lua @@ -62,7 +62,7 @@ for k, state in ipairs(doublecorner_states) do mesecons = { conductor = { states = doublecorner_states, - const_node = true, + rule_node_nocopy = true, rules = doublecorner_get_rules, }, }, diff --git a/mesecons_extrawires/tjunction.lua b/mesecons_extrawires/tjunction.lua index b065a47..a9328bc 100644 --- a/mesecons_extrawires/tjunction.lua +++ b/mesecons_extrawires/tjunction.lua @@ -46,7 +46,7 @@ minetest.register_node("mesecons_extrawires:tjunction_on", { mesecons = {conductor = { state = mesecon.state.on, - const_node = true, + rule_node_nocopy = true, rules = tjunction_get_rules, offstate = "mesecons_extrawires:tjunction_off" }}, @@ -77,7 +77,7 @@ minetest.register_node("mesecons_extrawires:tjunction_off", { mesecons = {conductor = { state = mesecon.state.off, - const_node = true, + rule_node_nocopy = true, rules = tjunction_get_rules, onstate = "mesecons_extrawires:tjunction_on" }}, diff --git a/mesecons_gates/init.lua b/mesecons_gates/init.lua index e36f798..e5d81ac 100644 --- a/mesecons_gates/init.lua +++ b/mesecons_gates/init.lua @@ -99,10 +99,10 @@ local function register_gate(name, inputnumber, assess, recipe, description) groups = {dig_immediate = 2, overheat = 1}, mesecons = { receptor = { state = "off", - const_node = true, + rule_node_nocopy = true, rules = gate_get_output_rules }, effector = { - const_node = true, + rule_node_nocopy = true, rules = get_inputrules, action_change = update_gate }} @@ -120,10 +120,10 @@ local function register_gate(name, inputnumber, assess, recipe, description) groups = {dig_immediate = 2, not_in_creative_inventory = 1, overheat = 1}, mesecons = { receptor = { state = "on", - const_node = true, + rule_node_nocopy = true, rules = gate_get_output_rules }, effector = { - const_node = true, + rule_node_nocopy = true, rules = get_inputrules, action_change = update_gate }} diff --git a/mesecons_insulated/init.lua b/mesecons_insulated/init.lua index d2555cf..45162e7 100644 --- a/mesecons_insulated/init.lua +++ b/mesecons_insulated/init.lua @@ -38,7 +38,7 @@ minetest.register_node("mesecons_insulated:insulated_on", { mesecons = {conductor = { state = mesecon.state.on, offstate = "mesecons_insulated:insulated_off", - const_node = true, + rule_node_nocopy = true, rules = insulated_wire_get_rules }}, on_blast = mesecon.on_blastnode, @@ -75,7 +75,7 @@ minetest.register_node("mesecons_insulated:insulated_off", { mesecons = {conductor = { state = mesecon.state.off, onstate = "mesecons_insulated:insulated_on", - const_node = true, + rule_node_nocopy = true, rules = insulated_wire_get_rules }}, on_blast = mesecon.on_blastnode, diff --git a/mesecons_lamp/init.lua b/mesecons_lamp/init.lua index c89e42c..a41c600 100644 --- a/mesecons_lamp/init.lua +++ b/mesecons_lamp/init.lua @@ -31,7 +31,7 @@ minetest.register_node("mesecons_lamp:lamp_on", { action_off = function (pos, node) minetest.swap_node(pos, {name = "mesecons_lamp:lamp_off", param2 = node.param2}) end, - const_node = true, + rule_node_nocopy = true, rules = mesecon.rules.wallmounted_get, }}, on_blast = mesecon.on_blastnode, @@ -57,7 +57,7 @@ minetest.register_node("mesecons_lamp:lamp_off", { action_on = function (pos, node) minetest.swap_node(pos, {name = "mesecons_lamp:lamp_on", param2 = node.param2}) end, - const_node = true, + rule_node_nocopy = true, rules = mesecon.rules.wallmounted_get, }}, on_blast = mesecon.on_blastnode, diff --git a/mesecons_pistons/init.lua b/mesecons_pistons/init.lua index 16b9633..39220dc 100644 --- a/mesecons_pistons/init.lua +++ b/mesecons_pistons/init.lua @@ -281,7 +281,7 @@ minetest.register_node("mesecons_pistons:piston_normal_off", { sounds = mesecon.node_sound.wood, mesecons = {effector={ action_on = piston_on, - const_node = true, + rule_node_nocopy = true, rules = piston_get_rules, }}, on_punch = piston_punch, @@ -312,7 +312,7 @@ minetest.register_node("mesecons_pistons:piston_normal_on", { sounds = mesecon.node_sound.wood, mesecons = {effector={ action_off = piston_off, - const_node = true, + rule_node_nocopy = true, rules = piston_get_rules, }}, on_rotate = piston_rotate_on, @@ -362,7 +362,7 @@ minetest.register_node("mesecons_pistons:piston_sticky_off", { sounds = mesecon.node_sound.wood, mesecons = {effector={ action_on = piston_on, - const_node = true, + rule_node_nocopy = true, rules = piston_get_rules, }}, on_punch = piston_punch, @@ -393,7 +393,7 @@ minetest.register_node("mesecons_pistons:piston_sticky_on", { sounds = mesecon.node_sound.wood, mesecons = {effector={ action_off = piston_off, - const_node = true, + rule_node_nocopy = true, rules = piston_get_rules, }}, on_rotate = piston_rotate_on, diff --git a/mesecons_receiver/init.lua b/mesecons_receiver/init.lua index 4077fcb..e275c4e 100644 --- a/mesecons_receiver/init.lua +++ b/mesecons_receiver/init.lua @@ -68,7 +68,7 @@ mesecon.register_node("mesecons_receiver:receiver", { }, mesecons = {conductor = { state = mesecon.state.off, - const_node = true, + rule_node_nocopy = true, rules = receiver_get_rules, onstate = "mesecons_receiver:receiver_on" }} diff --git a/mesecons_solarpanel/init.lua b/mesecons_solarpanel/init.lua index 1c09889..4de69fa 100644 --- a/mesecons_solarpanel/init.lua +++ b/mesecons_solarpanel/init.lua @@ -21,14 +21,14 @@ mesecon.register_node("mesecons_solarpanel:solar_panel", { groups = {dig_immediate = 3}, mesecons = {receptor = { state = mesecon.state.off, - const_node = true, + rule_node_nocopy = true, rules = mesecon.rules.wallmounted_get }} },{ groups = {dig_immediate = 3, not_in_creative_inventory = 1}, mesecons = {receptor = { state = mesecon.state.on, - const_node = true, + rule_node_nocopy = true, rules = mesecon.rules.wallmounted_get }}, }) diff --git a/mesecons_torch/init.lua b/mesecons_torch/init.lua index 7d05d43..78592b9 100644 --- a/mesecons_torch/init.lua +++ b/mesecons_torch/init.lua @@ -67,7 +67,7 @@ minetest.register_node("mesecons_torch:mesecon_torch_off", { sounds = mesecon.node_sound.default, mesecons = {receptor = { state = mesecon.state.off, - const_node = true, + rule_node_nocopy = true, rules = torch_get_output_rules }}, on_blast = mesecon.on_blastnode, diff --git a/mesecons_walllever/init.lua b/mesecons_walllever/init.lua index 18c6c89..1ae25e4 100644 --- a/mesecons_walllever/init.lua +++ b/mesecons_walllever/init.lua @@ -34,7 +34,7 @@ mesecon.register_node("mesecons_walllever:wall_lever", { mesh="jeija_wall_lever_off.obj", on_rotate = mesecon.buttonlike_onrotate, mesecons = {receptor = { - const_node = true, + rule_node_nocopy = true, rules = mesecon.rules.buttonlike_get, state = mesecon.state.off }}, @@ -49,7 +49,7 @@ mesecon.register_node("mesecons_walllever:wall_lever", { mesh="jeija_wall_lever_on.obj", on_rotate = false, mesecons = {receptor = { - const_node = true, + rule_node_nocopy = true, rules = mesecon.rules.buttonlike_get, state = mesecon.state.on }}, From 55cc5076dcd80a619e58f364e04746b33d2835ff Mon Sep 17 00:00:00 2001 From: Jude Melton-Houghton Date: Sun, 29 May 2022 19:21:36 -0400 Subject: [PATCH 6/7] Improve non-VM swap_node_force --- mesecons/util.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mesecons/util.lua b/mesecons/util.lua index c43fcb3..7d93abe 100644 --- a/mesecons/util.lua +++ b/mesecons/util.lua @@ -483,7 +483,7 @@ function mesecon.swap_node_force(pos, name, update_light) else -- This serves to both ensure the mapblock is loaded and also hand us -- the old node table so we can preserve param2. - local node = mesecon.get_node_force(pos) + local node = get_node_load(pos) node.name = name minetest.swap_node(pos, node) end From e4f2f8420320aedfdfa4af7dcdaca053fe0a4d08 Mon Sep 17 00:00:00 2001 From: Jude Melton-Houghton Date: Fri, 22 Jul 2022 21:57:51 -0400 Subject: [PATCH 7/7] Implement legacy linking functions --- mesecons/legacy.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/mesecons/legacy.lua b/mesecons/legacy.lua index 2a8eae6..5d7e6ef 100644 --- a/mesecons/legacy.lua +++ b/mesecons/legacy.lua @@ -12,3 +12,15 @@ for hash, _ in pairs(old_forceloaded_blocks) do minetest.forceload_free_block(unhash_blockpos(hash)) end os.remove(minetest.get_worldpath().."/mesecon_forceloaded") + +-- Implement mesecon.rules_link_rule_all and mesecon.rules_link_rule_all_inverted +-- for mods that use them, even though they were internal functions. + +function mesecon.rules_link_rule_all(output, rule) + return {mesecon.link(output, vector.add(output, rule))} +end + +function mesecon.rules_link_rule_all_inverted(input, rule) + local r = mesecon.link_inverted(input, vector.add(input, rule)) + return {r and mesecon.invertRule(r)} +end