forked from Mirrorlandia_minetest/mesecons
Fixes with multi-conductors that have special connects
This commit is contained in:
parent
d5b39951a4
commit
ae557d1cd4
@ -429,7 +429,7 @@ function mesecon:turnon(pos, rulename)
|
|||||||
|
|
||||||
for _, rule in ipairs(mesecon:rule2meta(rulename, rules)) do
|
for _, rule in ipairs(mesecon:rule2meta(rulename, rules)) do
|
||||||
local np = mesecon:addPosRule(pos, rule)
|
local np = mesecon:addPosRule(pos, rule)
|
||||||
local link, rulename = mesecon:rules_link(pos, np)
|
local link, rulename = mesecon:rules_link_rule(pos, rule)
|
||||||
|
|
||||||
if link then
|
if link then
|
||||||
mesecon:turnon(np, rulename)
|
mesecon:turnon(np, rulename)
|
||||||
@ -462,7 +462,7 @@ function mesecon:turnoff(pos, rulename)
|
|||||||
|
|
||||||
for _, rule in ipairs(mesecon:rule2meta(rulename, rules)) do
|
for _, rule in ipairs(mesecon:rule2meta(rulename, rules)) do
|
||||||
local np = mesecon:addPosRule(pos, rule)
|
local np = mesecon:addPosRule(pos, rule)
|
||||||
local link, rulename = mesecon:rules_link(pos, np)
|
local link, rulename = mesecon:rules_link_rule(pos, rule)
|
||||||
|
|
||||||
if link then
|
if link then
|
||||||
mesecon:turnoff(np, rulename)
|
mesecon:turnoff(np, rulename)
|
||||||
@ -498,23 +498,25 @@ function mesecon:connected_to_receptor(pos, rulename)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function mesecon:find_receptor_on(pos, checked, rulename)
|
function mesecon:find_receptor_on(pos, checked, rulename)
|
||||||
-- find out if node has already been checked (to prevent from endless loop)
|
|
||||||
for _, cp in ipairs(checked) do
|
|
||||||
if mesecon:cmpPos(cp, pos) then
|
|
||||||
return false, checked
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- add current position to checked
|
|
||||||
table.insert(checked, {x=pos.x, y=pos.y, z=pos.z})
|
|
||||||
local node = minetest.get_node(pos)
|
local node = minetest.get_node(pos)
|
||||||
|
|
||||||
if mesecon:is_receptor_on(node.name) then
|
if mesecon:is_receptor_on(node.name) then
|
||||||
|
-- add current position to checked
|
||||||
|
table.insert(checked, {x=pos.x, y=pos.y, z=pos.z})
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
if mesecon:is_conductor(node.name) then
|
if mesecon:is_conductor(node.name) then
|
||||||
local rules = mesecon:conductor_get_rules(node)
|
local rules = mesecon:conductor_get_rules(node)
|
||||||
|
local metaindex = mesecon:rule2metaindex(rulename, rules)
|
||||||
|
-- find out if node has already been checked (to prevent from endless loop)
|
||||||
|
for _, cp in ipairs(checked) do
|
||||||
|
if mesecon:cmpPos(cp, pos) and cp.metaindex == metaindex then
|
||||||
|
return false, checked
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-- add current position to checked
|
||||||
|
table.insert(checked, {x=pos.x, y=pos.y, z=pos.z, metaindex = metaindex})
|
||||||
for _, rule in ipairs(mesecon:rule2meta(rulename, rules)) do
|
for _, rule in ipairs(mesecon:rule2meta(rulename, rules)) do
|
||||||
local np = mesecon:addPosRule(pos, rule)
|
local np = mesecon:addPosRule(pos, rule)
|
||||||
if mesecon:rules_link(np, pos) then
|
if mesecon:rules_link(np, pos) then
|
||||||
@ -523,6 +525,14 @@ function mesecon:find_receptor_on(pos, checked, rulename)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
-- find out if node has already been checked (to prevent from endless loop)
|
||||||
|
for _, cp in ipairs(checked) do
|
||||||
|
if mesecon:cmpPos(cp, pos) then
|
||||||
|
return false, checked
|
||||||
|
end
|
||||||
|
end
|
||||||
|
table.insert(checked, {x=pos.x, y=pos.y, z=pos.z})
|
||||||
end
|
end
|
||||||
|
|
||||||
return false
|
return false
|
||||||
@ -543,9 +553,7 @@ function mesecon:rules_link(output, input, dug_outputrules) --output/input are p
|
|||||||
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 mesecon:cmpPos(mesecon:addPosRule(input, inputrule), output) then
|
if mesecon:cmpPos(mesecon:addPosRule(input, inputrule), output) then
|
||||||
if inputrule.sx == nil or outputrule.sx == nil or
|
if inputrule.sx == nil or outputrule.sx == nil or mesecon:cmpSpecial(inputrule, outputrule) then
|
||||||
(inputrule.sx == outputrule.sx and inputrule.sy == outputrule.sy
|
|
||||||
and inputrule.sz == outputrule.sz) then
|
|
||||||
return true, inputrule
|
return true, inputrule
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -555,6 +563,25 @@ function mesecon:rules_link(output, input, dug_outputrules) --output/input are p
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function mesecon:rules_link_rule(output, rule) --output/input are positions (outputrules optional, used if node has been dug), second return value: the name of the affected input rule
|
||||||
|
local input = mesecon:addPosRule(output, rule)
|
||||||
|
local inputnode = minetest.get_node(input)
|
||||||
|
local inputrules = mesecon:get_any_inputrules (inputnode)
|
||||||
|
if not inputrules then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
for _, inputrule in ipairs(mesecon:flattenrules(inputrules)) do
|
||||||
|
-- Check if input accepts from output
|
||||||
|
if mesecon:cmpPos(mesecon:addPosRule(input, inputrule), output) then
|
||||||
|
if inputrule.sx == nil or rule.sx == nil or mesecon:cmpSpecial(inputrule, rule) then
|
||||||
|
return true, inputrule
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
function mesecon:rules_link_anydir(pos1, pos2)
|
function mesecon:rules_link_anydir(pos1, pos2)
|
||||||
return mesecon:rules_link(pos1, pos2) or mesecon:rules_link(pos2, pos1)
|
return mesecon:rules_link(pos1, pos2) or mesecon:rules_link(pos2, pos1)
|
||||||
end
|
end
|
||||||
|
@ -62,18 +62,18 @@ function mesecon:rule2bit(findrule, allrules)
|
|||||||
end
|
end
|
||||||
for m,metarule in ipairs( allrules) do
|
for m,metarule in ipairs( allrules) do
|
||||||
for _, rule in ipairs(metarule ) do
|
for _, rule in ipairs(metarule ) do
|
||||||
if mesecon:cmpPos(findrule, rule) then
|
if mesecon:cmpPos(findrule, rule) and mesecon:cmpSpecial(findrule, rule) then
|
||||||
return m
|
return m
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function mesecon:rule2meta(findrule, allrules)
|
function mesecon:rule2metaindex(findrule, allrules)
|
||||||
--get the metarule the rule is in, or allrules
|
--get the metarule the rule is in, or allrules
|
||||||
|
|
||||||
if allrules[1].x then
|
if allrules[1].x then
|
||||||
return allrules
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
if not(findrule) then
|
if not(findrule) then
|
||||||
@ -82,13 +82,19 @@ function mesecon:rule2meta(findrule, allrules)
|
|||||||
|
|
||||||
for m, metarule in ipairs( allrules) do
|
for m, metarule in ipairs( allrules) do
|
||||||
for _, rule in ipairs(metarule ) do
|
for _, rule in ipairs(metarule ) do
|
||||||
if mesecon:cmpPos(findrule, rule) then
|
if mesecon:cmpPos(findrule, rule) and mesecon:cmpSpecial(findrule, rule) then
|
||||||
return metarule
|
return m
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function mesecon:rule2meta(findrule, allrules)
|
||||||
|
local index = mesecon:rule2metaindex(findrule, allrules)
|
||||||
|
if index == nil then return allrules end
|
||||||
|
return allrules[index]
|
||||||
|
end
|
||||||
|
|
||||||
if convert_base then
|
if convert_base then
|
||||||
print(
|
print(
|
||||||
"base2dec is tonumber(num,base1)\n"..
|
"base2dec is tonumber(num,base1)\n"..
|
||||||
@ -141,7 +147,7 @@ function mesecon:set_bit(binary,bit,value)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function mesecon:invertRule(r)
|
function mesecon:invertRule(r)
|
||||||
return {x = -r.x, y = -r.y, z = -r.z}
|
return {x = -r.x, y = -r.y, z = -r.z, sx = r.sx, sy = r.sy, sz = r.sz}
|
||||||
end
|
end
|
||||||
|
|
||||||
function mesecon:addPosRule(p, r)
|
function mesecon:addPosRule(p, r)
|
||||||
@ -152,6 +158,10 @@ function mesecon:cmpPos(p1, p2)
|
|||||||
return (p1.x == p2.x and p1.y == p2.y and p1.z == p2.z)
|
return (p1.x == p2.x and p1.y == p2.y and p1.z == p2.z)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function mesecon:cmpSpecial(r1, r2)
|
||||||
|
return (r1.sx == r2.sx and r1.sy == r2.sy and r1.sz == r2.sz)
|
||||||
|
end
|
||||||
|
|
||||||
function mesecon:tablecopy(table) -- deep table copy
|
function mesecon:tablecopy(table) -- deep table copy
|
||||||
local newtable = {}
|
local newtable = {}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user