From 3928d54d0cb9992c1aca8d86c77b10a5a77083f8 Mon Sep 17 00:00:00 2001 From: Jeija Date: Thu, 6 Sep 2012 09:53:30 +0200 Subject: [PATCH] Implement mesecon:register_receptor(onstate, offstate, rules, get_rules) and make add_receptor_node(_off) deprecated --- mesecons/init.lua | 28 +++++++++++++++------------- mesecons/internal.lua | 20 ++++---------------- mesecons/legacy.lua | 13 +++++++++++++ mesecons_switch/init.lua | 3 +-- 4 files changed, 33 insertions(+), 31 deletions(-) create mode 100644 mesecons/legacy.lua diff --git a/mesecons/init.lua b/mesecons/init.lua index 467ebf1..babfca4 100644 --- a/mesecons/init.lua +++ b/mesecons/init.lua @@ -20,7 +20,6 @@ mesecon.actions_on={} -- Saves registered function callbacks for mesecon on mesecon.actions_off={} -- Saves registered function callbacks for mesecon off mesecon.actions_change={} -- Saves registered function callbacks for mesecon change mesecon.receptors={} -mesecon.receptors_off={} mesecon.effectors={} mesecon.rules={} mesecon.conductors={} @@ -33,25 +32,26 @@ dofile(minetest.get_modpath("mesecons").."/internal.lua"); -- API API API API API API API API API API API API API API API API API API -function mesecon:add_receptor_node(name, rules, get_rules) --rules table is optional; if rules depend on param2 pass (nodename, nil, function get_rules) - if get_rules==nil and rules==nil then - rules=mesecon:get_rules("default") +function mesecon:register_receptor(onstate, offstate, rules, get_rules) + if get_rules == nil and rules == nil then + rules = mesecon:get_rules("default") end - table.insert(mesecon.receptors, {name = name, rules = rules, get_rules = get_rules}) -end - -function mesecon:add_receptor_node_off(name, rules, get_rules) - if get_rules==nil and rules==nil then - rules=mesecon:get_rules("default") - end - table.insert(mesecon.receptors_off, {name = name, rules = rules, get_rules = get_rules}) + table.insert(mesecon.receptors, + {onstate = onstate, + offstate = offstate, + rules = rules, + get_rules = get_rules}) end function mesecon:register_effector(onstate, offstate, input_rules, get_input_rules) if get_input_rules==nil and input_rules==nil then rules=mesecon:get_rules("default") end - table.insert(mesecon.effectors, {onstate = onstate, offstate = offstate, input_rules = input_rules, get_input_rules = get_input_rules}) + table.insert(mesecon.effectors, + {onstate = onstate, + offstate = offstate, + input_rules = input_rules, + get_input_rules = get_input_rules}) end function mesecon:receptor_on(pos, rules) @@ -130,3 +130,5 @@ dofile(minetest.get_modpath("mesecons").."/wires.lua"); --Services like turnoff receptor on dignode and so on dofile(minetest.get_modpath("mesecons").."/services.lua"); +--Deprecated stuff +dofile(minetest.get_modpath("mesecons").."/legacy.lua"); diff --git a/mesecons/internal.lua b/mesecons/internal.lua index 4270a22..71b5172 100644 --- a/mesecons/internal.lua +++ b/mesecons/internal.lua @@ -4,7 +4,7 @@ function mesecon:is_receptor_node(nodename) local i = 1 for i, receptor in ipairs(mesecon.receptors) do - if receptor.name == nodename then + if receptor.onstate == nodename then return true end i = i + 1 @@ -14,8 +14,8 @@ end function mesecon:is_receptor_node_off(nodename, pos, ownpos) local i = 1 - for i, receptor in ipairs(mesecon.receptors_off) do - if receptor.name == nodename then + for i, receptor in ipairs(mesecon.receptors) do + if receptor.offstate == nodename then return true end i = i + 1 @@ -25,7 +25,7 @@ end function mesecon:receptor_get_rules(node) for i, receptor in ipairs(mesecon.receptors) do - if receptor.name == node.name then + if receptor.onstate == node.name or receptor.offstate == node.name then if receptor.get_rules ~= nil then return receptor.get_rules(node.param2) elseif mesecon.receptors[i].rules ~=nil then @@ -35,18 +35,6 @@ function mesecon:receptor_get_rules(node) end end end - - for i, receptor in ipairs(mesecon.receptors_off) do - if receptor.name == node.name then - if receptor.get_rules ~= nil then - return receptor.get_rules(node.param2) - elseif mesecon.receptors_off[i].rules ~=nil then - return receptor.rules - else - return mesecon:get_rules("default") - end - end - end return nil end diff --git a/mesecons/legacy.lua b/mesecons/legacy.lua new file mode 100644 index 0000000..4fd73dd --- /dev/null +++ b/mesecons/legacy.lua @@ -0,0 +1,13 @@ +function mesecon:add_receptor_node(name, rules, get_rules) + if get_rules==nil and rules==nil then + rules=mesecon:get_rules("default") + end + table.insert(mesecon.receptors, {onstate = name, rules = rules, get_rules = get_rules}) +end + +function mesecon:add_receptor_node_off(name, rules, get_rules) + if get_rules==nil and rules==nil then + rules=mesecon:get_rules("default") + end + table.insert(mesecon.receptors, {offstate = name, rules = rules, get_rules = get_rules}) +end diff --git a/mesecons_switch/init.lua b/mesecons_switch/init.lua index 1213f6e..a8b3415 100644 --- a/mesecons_switch/init.lua +++ b/mesecons_switch/init.lua @@ -15,8 +15,7 @@ minetest.register_node("mesecons_switch:mesecon_switch_on", { description="Switch", }) -mesecon:add_receptor_node("mesecons_switch:mesecon_switch_on") -mesecon:add_receptor_node_off("mesecons_switch:mesecon_switch_off") +mesecon:register_receptor("mesecons_switch:mesecon_switch_on", "mesecons_switch:mesecon_switch_off") minetest.register_on_punchnode(function(pos, node, puncher) if node.name == "mesecons_switch:mesecon_switch_on" then