Refactor some redstone internal code

This commit is contained in:
Wuzzy 2018-01-17 20:23:43 +01:00
parent e85a16f70a
commit 5d913b6d29

View file

@ -37,9 +37,8 @@
-- 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 bool, direct_source. bool is true if pos is powered by a receptor, a conductor or an opaque block. -- mesecon.is_powered(pos) --> Returns bool, spread. 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 -- spread is true if it is powered AND also transmits its power one block further.
-- (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)
@ -538,7 +537,7 @@ function mesecon.is_powered(pos, rule, depth, sourcepos, home_pos)
end end
local function power_walk(pos, home_pos, sourcepos, rulenames, rule, depth) local function power_walk(pos, home_pos, sourcepos, rulenames, rule, depth)
local direct_source = false local spread = 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)
@ -546,18 +545,18 @@ 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
local rulez = mesecon.get_any_outputrules(nn) local rulez = mesecon.get_any_outputrules(nn)
local ds_tmp = false local spread_tmp = false
for r=1, #rulez do for r=1, #rulez do
if vector.equals(mesecon.invertRule(rname), rulez[r]) then if vector.equals(mesecon.invertRule(rname), rulez[r]) then
if rulez[r].spread then if rulez[r].spread then
ds_tmp = true spread_tmp = true
end end
end end
end end
if depth == 0 or ds_tmp then if depth == 0 or spread_tmp then
table.insert(sourcepos, np) table.insert(sourcepos, np)
if ds_tmp then if spread_tmp then
direct_source = true spread = true
end end
end end
end end
@ -568,22 +567,22 @@ function mesecon.is_powered(pos, rule, depth, sourcepos, home_pos)
end end
end end
end end
return sourcepos, direct_source return sourcepos, spread
end end
local direct_source = false local spread = false
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 direct_source_temp local spread_temp
local rulenames = mesecon.rules_link_rule_all_inverted(pos, rule) local rulenames = mesecon.rules_link_rule_all_inverted(pos, rule)
sourcepos, direct_source_temp = power_walk(pos, home_pos, sourcepos, rulenames, rule, depth) sourcepos, spread_temp = power_walk(pos, home_pos, sourcepos, rulenames, rule, depth)
if direct_source_temp then if spread_temp then
direct_source = true spread = true
end end
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, direct_source = power_walk(pos, home_pos, sourcepos, rulenames, rule, depth) sourcepos, spread = power_walk(pos, home_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
@ -591,7 +590,7 @@ function mesecon.is_powered(pos, rule, depth, sourcepos, home_pos)
if (#sourcepos == 0) then if (#sourcepos == 0) then
return false, false return false, false
else else
return sourcepos, direct_source return sourcepos, spread
end end
end end