From 33d978de06b9f98da26e72dafe7a8708850f0fb4 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Sat, 20 Jan 2018 19:04:53 +0100 Subject: [PATCH] Propagate redstone power through opaque blocks --- mods/ITEMS/REDSTONE/mesecons/init.lua | 20 --------------- mods/ITEMS/REDSTONE/mesecons/internal.lua | 31 +++++++++++++++++++---- mods/ITEMS/REDSTONE/mesecons/util.lua | 7 ++++- 3 files changed, 32 insertions(+), 26 deletions(-) diff --git a/mods/ITEMS/REDSTONE/mesecons/init.lua b/mods/ITEMS/REDSTONE/mesecons/init.lua index 4ef690b8d..3286c0cef 100644 --- a/mods/ITEMS/REDSTONE/mesecons/init.lua +++ b/mods/ITEMS/REDSTONE/mesecons/init.lua @@ -81,13 +81,6 @@ mesecon.queue:add_function("receptor_on", function (pos, rules) for _, rulename in ipairs(rulenames) do mesecon.turnon(np, rulename) end - if rule.spread then - if minetest.get_item_group(minetest.get_node(np).name, "opaque") == 1 then - for _, neighbor in ipairs(mesecon.mcl_get_neighbors(np)) do - mesecon.turnon(neighbor.pos, vector.add(rule, neighbor.link)) - end - end - end end mesecon.vm_commit() @@ -117,19 +110,6 @@ mesecon.queue:add_function("receptor_off", function (pos, rules) mesecon.vm_abort() end end - if rule.spread then - if minetest.get_item_group(minetest.get_node(np).name, "opaque") == 1 then - for _, neighbor in ipairs(mesecon.mcl_get_neighbors(np)) do - mesecon.vm_begin() - mesecon.changesignal(neighbor.pos, minetest.get_node(neighbor.pos), vector.add(rule, neighbor.link), mesecon.state.off, 2) - if (mesecon.turnoff(neighbor.pos, vector.add(rule, neighbor.link))) then - mesecon.vm_commit() - else - mesecon.vm_abort() - end - end - end - end end end) diff --git a/mods/ITEMS/REDSTONE/mesecons/internal.lua b/mods/ITEMS/REDSTONE/mesecons/internal.lua index 3a75a23f6..015846721 100644 --- a/mods/ITEMS/REDSTONE/mesecons/internal.lua +++ b/mods/ITEMS/REDSTONE/mesecons/internal.lua @@ -403,12 +403,24 @@ function mesecon.turnon(pos, link) mesecon.activate(f.pos, node, f.link, depth) end end + if node and f.link.spread and minetest.get_item_group(node.name, "opaque") == 1 then + -- Call turnon on neighbors + -- Warning: A LOT of nodes need to be looked at for this to work + for _, r in ipairs(mesecon.rule2meta(f.link, mesecon.rules.mcl_alldirs_spread)) do + local np = vector.add(f.pos, r) + for _, l in ipairs(mesecon.rules_link_rule_all(f.pos, r)) do + local nlink = table.copy(l) + nlink.spread = false + table.insert(frontiers, {pos = np, link = nlink}) + end + end + end depth = depth + 1 end end --- Turn on an equipotential section starting at `pos`, which outputs in the direction of `link`. +-- Turn off an equipotential section starting at `pos`, which outputs in the direction of `link`. -- Breadth-first search. Map is abstracted away in a voxelmanip. -- Follow all all conductor paths replacing conductors that were already -- looked at, deactivating / changing all effectors along the way. @@ -464,6 +476,10 @@ function mesecon.turnoff(pos, link) depth = depth }) end + if node and f.link.spread and minetest.get_item_group(node.name, "opaque") == 1 then + -- TODO: Call turnoff on neighbors + end + depth = depth + 1 end @@ -490,8 +506,10 @@ function mesecon.rules_link_rule_all(output, rule) for _, inputrule in ipairs(mesecon.flattenrules(inputrules)) do -- Check if input accepts from output - if vector.equals(vector.add(input, inputrule), output) then - table.insert(rules, inputrule) + if vector.equals(vector.add(input, inputrule), output) then + local newrule = table.copy(inputrule) + newrule.spread = rule.spread + table.insert(rules, newrule) end end @@ -510,8 +528,11 @@ function mesecon.rules_link_rule_all_inverted(input, rule) local rules = {} for _, outputrule in ipairs(mesecon.flattenrules(outputrules)) do - if vector.equals(vector.add(output, outputrule), input) then - table.insert(rules, mesecon.invertRule(outputrule)) + if vector.equals(vector.add(output, outputrule), input) then + local newrule = table.copy(outputrule) + newrule = mesecon.invertRule(newrule) + newrule.spread = rule.spread + table.insert(rules, newrule) end end return rules diff --git a/mods/ITEMS/REDSTONE/mesecons/util.lua b/mods/ITEMS/REDSTONE/mesecons/util.lua index c848b9d3e..7fd1f6fd3 100644 --- a/mods/ITEMS/REDSTONE/mesecons/util.lua +++ b/mods/ITEMS/REDSTONE/mesecons/util.lua @@ -198,7 +198,12 @@ function mesecon.set_bit(binary,bit,value) end function mesecon.invertRule(r) - return vector.multiply(r, -1) + local spread = r.spread + r = vector.multiply(r, -1) + if spread then + r.spread = true + end + return r end function mesecon.tablecopy(table) -- deep table copy