Further simplification of turnon/turnoff

This commit is contained in:
Jeija 2016-08-30 18:24:08 +02:00
parent 8e6536ca2e
commit 89153f6909
2 changed files with 15 additions and 33 deletions

@ -87,7 +87,11 @@ minetest.register_globalstep(function (dtime)
end) end)
function mesecon.queue:execute(action) function mesecon.queue:execute(action)
mesecon.queue.funcs[action.func](action.pos, unpack(action.params)) -- ignore if action queue function name doesn't exist,
-- (e.g. in case the action queue savegame was written by an old mesecons version)
if mesecon.queue.funcs[action.func] then
mesecon.queue.funcs[action.func](action.pos, unpack(action.params))
end
end end

@ -375,30 +375,24 @@ function mesecon.turnon(pos, link)
local frontiers = {{pos = pos, link = link}} local frontiers = {{pos = pos, link = link}}
local depth = 1 local depth = 1
while frontiers[depth] do while frontiers[1] do
local f = frontiers[depth] local f = table.remove(frontiers, 1)
local node = mesecon.get_node_force(f.pos) local node = mesecon.get_node_force(f.pos)
if not node then if not node then
-- Area does not exist; do nothing -- Area does not exist; do nothing
elseif mesecon.is_conductor_off(node, f.link) then elseif mesecon.is_conductor_off(node, f.link) then
local rules = mesecon.conductor_get_rules(node) local rules = mesecon.conductor_get_rules(node)
local neighborlinks = {}
-- call turnon on neighbors -- call turnon on neighbors
for _, r in ipairs(mesecon.rule2meta(f.link, rules)) do for _, r in ipairs(mesecon.rule2meta(f.link, rules)) do
local np = vector.add(f.pos, r) local np = vector.add(f.pos, r)
neighborlinks[minetest.hash_node_position(np)] = mesecon.rules_link_rule_all(f.pos, r) for _, l in ipairs(mesecon.rules_link_rule_all(f.pos, r)) do
table.insert(frontiers, {pos = np, link = l})
end
end end
mesecon.swap_node_force(f.pos, mesecon.get_conductor_on(node, f.link)) mesecon.swap_node_force(f.pos, mesecon.get_conductor_on(node, f.link))
for npos, links in pairs(neighborlinks) do
-- links = all links to node, l = each single link
for _, l in ipairs(links) do
table.insert(frontiers, {pos = minetest.get_position_from_hash(npos), link = l})
end
end
elseif mesecon.is_effector(node.name) then elseif mesecon.is_effector(node.name) then
mesecon.changesignal(f.pos, node, f.link, mesecon.state.on, depth) mesecon.changesignal(f.pos, node, f.link, mesecon.state.on, depth)
if mesecon.is_effector_off(node.name) then if mesecon.is_effector_off(node.name) then
@ -409,39 +403,28 @@ function mesecon.turnon(pos, link)
end end
end end
mesecon.queue:add_function("turnon", function(pos, rulename, recdepth)
mesecon.turnon(pos, rulename, recdepth)
end)
function mesecon.turnoff(pos, link) function mesecon.turnoff(pos, link)
local frontiers = {{pos = pos, link = link}} local frontiers = {{pos = pos, link = link}}
local depth = 1 local depth = 1
while frontiers[depth] do while frontiers[1] do
local f = frontiers[depth] local f = table.remove(frontiers, 1)
local node = mesecon.get_node_force(f.pos) local node = mesecon.get_node_force(f.pos)
-- area not loaded, postpone action
if not node then if not node then
-- Area does not exist; do nothing -- Area does not exist; do nothing
elseif mesecon.is_conductor_on(node, f.link) then elseif mesecon.is_conductor_on(node, f.link) then
local rules = mesecon.conductor_get_rules(node) local rules = mesecon.conductor_get_rules(node)
local neighborlinks = {}
-- call turnoff on neighbors -- call turnoff on neighbors
for _, r in ipairs(mesecon.rule2meta(f.link, rules)) do for _, r in ipairs(mesecon.rule2meta(f.link, rules)) do
local np = vector.add(f.pos, r) local np = vector.add(f.pos, r)
neighborlinks[minetest.hash_node_position(np)] = mesecon.rules_link_rule_all(f.pos, r) for _, l in ipairs(mesecon.rules_link_rule_all(f.pos, r)) do
table.insert(frontiers, {pos = np, link = l})
end
end end
mesecon.swap_node_force(f.pos, mesecon.get_conductor_off(node, f.link)) mesecon.swap_node_force(f.pos, mesecon.get_conductor_off(node, f.link))
for npos, links in pairs(neighborlinks) do
-- links = all links to node, l = each single link
for _, l in ipairs(links) do
table.insert(frontiers, {pos = minetest.get_position_from_hash(npos), link = l})
end
end
elseif mesecon.is_effector(node.name) then elseif mesecon.is_effector(node.name) then
mesecon.changesignal(f.pos, node, f.link, mesecon.state.off, depth) mesecon.changesignal(f.pos, node, f.link, mesecon.state.off, depth)
if mesecon.is_effector_on(node.name) and not mesecon.is_powered(f.pos) then if mesecon.is_effector_on(node.name) and not mesecon.is_powered(f.pos) then
@ -452,11 +435,6 @@ function mesecon.turnoff(pos, link)
end end
end end
mesecon.queue:add_function("turnoff", function(pos, rulename, recdepth)
mesecon.turnoff(pos, rulename, recdepth)
end)
function mesecon.connected_to_receptor(pos, link) function mesecon.connected_to_receptor(pos, link)
local node = mesecon.get_node_force(pos) local node = mesecon.get_node_force(pos)
if not node then return false end if not node then return false end