forked from minetest-mods/mesecons
Simplify turnon/turnoff.
It is no longer possible for get_node_force to return nil if the target location does, in fact, exist, because a VM will always be able to load it (whereas a forceload might not, due to exhaustion of forceload resources). So it is no longer necessary to handle get_node_force returning nil by deferring processing.
This commit is contained in:
parent
bc9d4c2d5a
commit
8e6536ca2e
@ -379,42 +379,25 @@ function mesecon.turnon(pos, link)
|
|||||||
local f = frontiers[depth]
|
local f = frontiers[depth]
|
||||||
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
|
||||||
mesecon.queue:add_action(f.pos, "turnon", {f.link}, nil, true)
|
-- 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)
|
||||||
|
|
||||||
-- Success: If false, at least one neighboring node is unloaded,
|
|
||||||
-- postpone turning on action
|
|
||||||
local success = true
|
|
||||||
local neighborlinks = {}
|
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)
|
||||||
-- Neighboring node not loaded, postpone turning on current node
|
|
||||||
-- since we can't even know if neighboring node has matching rules
|
|
||||||
if not mesecon.get_node_force(np) then
|
|
||||||
success = false
|
|
||||||
break
|
|
||||||
else
|
|
||||||
neighborlinks[minetest.hash_node_position(np)] = mesecon.rules_link_rule_all(f.pos, r)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if success then
|
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
|
for npos, links in pairs(neighborlinks) do
|
||||||
-- links = all links to node, l = each single link
|
-- links = all links to node, l = each single link
|
||||||
for _, l in ipairs(links) do
|
for _, l in ipairs(links) do
|
||||||
table.insert(frontiers, {pos = minetest.get_position_from_hash(npos), link = l})
|
table.insert(frontiers, {pos = minetest.get_position_from_hash(npos), link = l})
|
||||||
end
|
|
||||||
end
|
end
|
||||||
else
|
|
||||||
mesecon.queue:add_action(f.pos, "turnon", {f.link}, nil, true)
|
|
||||||
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)
|
||||||
@ -440,40 +423,24 @@ function mesecon.turnoff(pos, link)
|
|||||||
|
|
||||||
-- area not loaded, postpone action
|
-- area not loaded, postpone action
|
||||||
if not node then
|
if not node then
|
||||||
mesecon.queue:add_action(f.pos, "turnoff", {f.link}, nil, true)
|
-- 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)
|
||||||
|
|
||||||
-- Success: If false, at least one neighboring node is unloaded,
|
|
||||||
-- postpone turning on action
|
|
||||||
local success = true
|
|
||||||
local neighborlinks = {}
|
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)
|
||||||
-- Neighboring node not loaded, postpone turning off current node
|
|
||||||
-- since we can't even know if neighboring node has matching rules
|
|
||||||
if not mesecon.get_node_force(np) then
|
|
||||||
success = false
|
|
||||||
break
|
|
||||||
else
|
|
||||||
neighborlinks[minetest.hash_node_position(np)] = mesecon.rules_link_rule_all(f.pos, r)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if success then
|
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
|
for npos, links in pairs(neighborlinks) do
|
||||||
-- links = all links to node, l = each single link
|
-- links = all links to node, l = each single link
|
||||||
for _, l in ipairs(links) do
|
for _, l in ipairs(links) do
|
||||||
table.insert(frontiers, {pos = minetest.get_position_from_hash(npos), link = l})
|
table.insert(frontiers, {pos = minetest.get_position_from_hash(npos), link = l})
|
||||||
end
|
|
||||||
end
|
end
|
||||||
else
|
|
||||||
mesecon.queue:add_action(f.pos, "turnoff", {f.link}, nil, true)
|
|
||||||
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)
|
||||||
|
Loading…
Reference in New Issue
Block a user