Improve linking

This commit is contained in:
Jude Melton-Houghton 2022-05-19 20:33:09 -04:00
parent fee53f38d5
commit 468ec521ea
2 changed files with 27 additions and 34 deletions

View File

@ -77,8 +77,8 @@ mesecon.queue:add_function("receptor_on", function (pos, rules)
-- Call turnon on all linking positions -- Call turnon on all linking positions
for _, rule in ipairs(mesecon.flattenrules(rules)) do for _, rule in ipairs(mesecon.flattenrules(rules)) do
local np = vector.add(pos, rule) local np = vector.add(pos, rule)
local rulenames = mesecon.rules_link_rule_all(pos, rule) local rulename = mesecon.link(pos, np)
for _, rulename in ipairs(rulenames) do if rulename then
mesecon.turnon(np, rulename) mesecon.turnon(np, rulename)
end end
end end
@ -96,8 +96,8 @@ mesecon.queue:add_function("receptor_off", function (pos, rules)
-- Call turnoff on all linking positions -- Call turnoff on all linking positions
for _, rule in ipairs(mesecon.flattenrules(rules)) do for _, rule in ipairs(mesecon.flattenrules(rules)) do
local np = vector.add(pos, rule) local np = vector.add(pos, rule)
local rulenames = mesecon.rules_link_rule_all(pos, rule) local rulename = mesecon.link(pos, np)
for _, rulename in ipairs(rulenames) do if rulename then
mesecon.vm_begin() mesecon.vm_begin()
mesecon.changesignal(np, minetest.get_node(np), rulename, mesecon.state.off, 2) mesecon.changesignal(np, minetest.get_node(np), rulename, mesecon.state.off, 2)

View File

@ -423,7 +423,8 @@ function mesecon.turnon(pos, link)
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)
if not pos_can_be_skipped[minetest.hash_node_position(np)] then if not pos_can_be_skipped[minetest.hash_node_position(np)] then
for _, l in ipairs(mesecon.rules_link_rule_all(f.pos, r)) do local l = mesecon.link(f.pos, np)
if l then
frontiers:add({pos = np, link = l}) frontiers:add({pos = np, link = l})
end end
end end
@ -490,14 +491,15 @@ function mesecon.turnoff(pos, link)
-- Check if an onstate receptor is connected. If that is the case, -- Check if an onstate receptor is connected. If that is the case,
-- abort this turnoff process by returning false. `receptor_off` will -- abort this turnoff process by returning false. `receptor_off` will
-- discard all the changes that we made in the voxelmanip: -- discard all the changes that we made in the voxelmanip:
if mesecon.rules_link_rule_all_inverted(f.pos, r)[1] then if mesecon.link_inverted(f.pos, np) then
if mesecon.is_receptor_on(mesecon.get_node_force(np).name) then if mesecon.is_receptor_on(mesecon.get_node_force(np).name) then
return false return false
end end
end end
-- Call turnoff on neighbors -- Call turnoff on neighbors
for _, l in ipairs(mesecon.rules_link_rule_all(f.pos, r)) do local l = mesecon.link(f.pos, np)
if l then
frontiers:add({pos = np, link = l}) frontiers:add({pos = np, link = l})
end end
end end
@ -533,44 +535,35 @@ function mesecon.turnoff(pos, link)
return true return true
end end
-- Get all linking inputrules of inputnode (effector or conductor) that is connected to -- Get the linking inputrule of inputnode (effector or conductor) that is connected to
-- outputnode (receptor or conductor) at position `output` and has an output in direction `rule` -- outputnode (receptor or conductor) between positions `output` and `input`
function mesecon.rules_link_rule_all(output, rule) function mesecon.link(output, input)
local input = vector.add(output, rule)
local inputnode = mesecon.get_node_force(input) local inputnode = mesecon.get_node_force(input)
local inputrules = mesecon.get_any_inputrules(inputnode) local inputrules = mesecon.get_any_inputrules(inputnode)
if not inputrules then if not inputrules then return end
return {}
end
local rules = {}
local dx, dy, dz = output.x - input.x, output.y - input.y, output.z - input.z
for _, inputrule in ipairs(mesecon.flattenrules(inputrules)) do for _, inputrule in ipairs(mesecon.flattenrules(inputrules)) do
-- Check if input accepts from output -- Check if input accepts from output
if vector.equals(vector.add(input, inputrule), output) then if inputrule.x == dx and inputrule.y == dy and inputrule.z == dz then
table.insert(rules, inputrule) return inputrule
end
end end
end end
return rules -- Get the linking outputrule of outputnode (receptor or conductor) that is connected to
end -- inputnode (effector or conductor) between positions `input` and `output`
function mesecon.link_inverted(input, output)
-- Get all linking outputnodes of outputnode (receptor or conductor) that is connected to
-- inputnode (effector or conductor) at position `input` and has an input in direction `rule`
function mesecon.rules_link_rule_all_inverted(input, rule)
local output = vector.add(input, rule)
local outputnode = mesecon.get_node_force(output) local outputnode = mesecon.get_node_force(output)
local outputrules = mesecon.get_any_outputrules(outputnode) local outputrules = mesecon.get_any_outputrules(outputnode)
if not outputrules then if not outputrules then return end
return {}
end
local rules = {}
local dx, dy, dz = input.x - output.x, input.y - output.y, input.z - output.z
for _, outputrule in ipairs(mesecon.flattenrules(outputrules)) do for _, outputrule in ipairs(mesecon.flattenrules(outputrules)) do
if vector.equals(vector.add(output, outputrule), input) then if outputrule.x == dx and outputrule.y == dy and outputrule.z == dz then
table.insert(rules, mesecon.invertRule(outputrule)) return outputrule
end end
end end
return rules
end end
function mesecon.is_powered(pos, rule) function mesecon.is_powered(pos, rule)
@ -583,8 +576,8 @@ function mesecon.is_powered(pos, rule)
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 rname = mesecon.link_inverted(pos, vector.add(pos, rule))
for _, rname in ipairs(rulenames) do if rname then
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)
@ -595,8 +588,8 @@ function mesecon.is_powered(pos, rule)
end end
end end
else else
local rulenames = mesecon.rules_link_rule_all_inverted(pos, rule) local rname = mesecon.link_inverted(pos, vector.add(pos, rule))
for _, rname in ipairs(rulenames) do if rname then
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)
if (mesecon.is_conductor_on (nn, mesecon.invertRule(rname)) if (mesecon.is_conductor_on (nn, mesecon.invertRule(rname))