Implement mesecon:register_receptor(onstate, offstate, rules, get_rules) and make add_receptor_node(_off) deprecated

This commit is contained in:
Jeija 2012-09-06 09:53:30 +02:00
parent 3c90f36a75
commit 3928d54d0c
4 changed files with 33 additions and 31 deletions

@ -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");

@ -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

13
mesecons/legacy.lua Normal file

@ -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

@ -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