Fix opaque block placement propagate power too far

This commit is contained in:
Wuzzy 2018-01-17 03:54:51 +01:00
parent 2cf6332b57
commit af3db1ae21
2 changed files with 20 additions and 9 deletions

View File

@ -37,7 +37,9 @@
-- HIGH-LEVEL Internals -- HIGH-LEVEL Internals
-- mesecon.is_power_on(pos) --> Returns true if pos emits power in any way -- mesecon.is_power_on(pos) --> Returns true if pos emits power in any way
-- mesecon.is_power_off(pos) --> Returns true if pos does not emit power in any way -- mesecon.is_power_off(pos) --> Returns true if pos does not emit power in any way
-- mesecon.is_powered(pos) --> Returns true if pos is powered by a receptor, a conductor or an opaque block -- mesecon.is_powered(pos) --> Returns bool, direct_source. bool is true if pos is powered by a receptor, a conductor or an opaque block.
-- direct_source is true if it powered at least by one power source directly
-- (rather than just indirectly with a strongly-powered (opaque) block.
-- RULES ROTATION helpers -- RULES ROTATION helpers
-- mesecon.rotate_rules_right(rules) -- mesecon.rotate_rules_right(rules)
@ -536,6 +538,7 @@ function mesecon.is_powered(pos, rule, depth, sourcepos, home_pos)
end end
local function power_walk(pos, sourcepos, rulenames, rule, depth) local function power_walk(pos, sourcepos, rulenames, rule, depth)
local direct_source = false
for _, rname in ipairs(rulenames) do for _, rname in ipairs(rulenames) do
local np = vector.add(pos, rname) local np = vector.add(pos, rname)
local nn = mesecon.get_node_force(np) local nn = mesecon.get_node_force(np)
@ -543,27 +546,35 @@ function mesecon.is_powered(pos, rule, depth, sourcepos, home_pos)
or mesecon.is_receptor_on (nn.name)) then or mesecon.is_receptor_on (nn.name)) then
if not vector.equals(home_pos, np) then if not vector.equals(home_pos, np) then
table.insert(sourcepos, np) table.insert(sourcepos, np)
direct_source = true
end end
elseif depth == 0 and minetest.get_item_group(nn.name, "opaque") == 1 then elseif depth == 0 and minetest.get_item_group(nn.name, "opaque") == 1 then
local more_sourcepos = mesecon.is_powered(np, nil, depth + 1, sourcepos, home_pos) local more_sourcepos = mesecon.is_powered(np, nil, depth + 1, sourcepos, home_pos)
mesecon.mergetable(sourcepos, more_sourcepos) if more_sourcepos and #more_sourcepos > 0 then
mesecon.mergetable(sourcepos, more_sourcepos)
end
end end
end end
return sourcepos return sourcepos, direct_source
end end
local direct_source
if not rule then if not rule then
for _, rule in ipairs(mesecon.flattenrules(rules)) do for _, rule in ipairs(mesecon.flattenrules(rules)) do
local rulenames = mesecon.rules_link_rule_all_inverted(pos, rule) local rulenames = mesecon.rules_link_rule_all_inverted(pos, rule)
sourcepos = power_walk(pos, sourcepos, rulenames, rule, depth) sourcepos, direct_source = power_walk(pos, sourcepos, rulenames, rule, depth)
end end
else else
local rulenames = mesecon.rules_link_rule_all_inverted(pos, rule) local rulenames = mesecon.rules_link_rule_all_inverted(pos, rule)
sourcepos = power_walk(pos, sourcepos, rulenames, rule, depth) sourcepos, direct_source = power_walk(pos, sourcepos, rulenames, rule, depth)
end end
-- Return FALSE if not powered, return list of sources if is powered -- Return FALSE if not powered, return list of sources if is powered
if (#sourcepos == 0) then return false
else return sourcepos end if (#sourcepos == 0) then
return false
else
return sourcepos, direct_source
end
end end

View File

@ -53,8 +53,8 @@ mesecon.on_placenode = function(pos, node)
if minetest.get_item_group(node.name, "opaque") == 1 then if minetest.get_item_group(node.name, "opaque") == 1 then
local neighbors = mesecon.mcl_get_neighbors(pos) local neighbors = mesecon.mcl_get_neighbors(pos)
local is_powered = mesecon.is_powered(pos) local is_powered, direct_source = mesecon.is_powered(pos)
if is_powered then if is_powered and direct_source then
for n=1, #neighbors do for n=1, #neighbors do
local npos = neighbors[n].pos local npos = neighbors[n].pos
local nnode = minetest.get_node(npos) local nnode = minetest.get_node(npos)