This commit is contained in:
Jude Melton-Houghton 2023-12-26 03:04:11 -07:00 committed by GitHub
commit fc9fff24bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 155 additions and 118 deletions

View File

@ -77,8 +77,8 @@ mesecon.queue:add_function("receptor_on", function (pos, rules)
-- Call turnon on all linking positions
for _, rule in ipairs(mesecon.flattenrules(rules)) do
local np = vector.add(pos, rule)
local rulenames = mesecon.rules_link_rule_all(pos, rule)
for _, rulename in ipairs(rulenames) do
local rulename = mesecon.link(pos, np)
if rulename then
mesecon.turnon(np, rulename)
end
end
@ -96,8 +96,8 @@ mesecon.queue:add_function("receptor_off", function (pos, rules)
-- Call turnoff on all linking positions
for _, rule in ipairs(mesecon.flattenrules(rules)) do
local np = vector.add(pos, rule)
local rulenames = mesecon.rules_link_rule_all(pos, rule)
for _, rulename in ipairs(rulenames) do
local rulename = mesecon.link(pos, np)
if rulename then
mesecon.vm_begin()
-- Turnoff returns true if turnoff process was successful, no onstate receptor

View File

@ -50,48 +50,59 @@
mesecon.fifo_queue = dofile(minetest.get_modpath("mesecons").."/fifo_queue.lua")
-- General
function mesecon.get_effector(nodename)
if minetest.registered_nodes[nodename]
and minetest.registered_nodes[nodename].mesecons
and minetest.registered_nodes[nodename].mesecons.effector then
return minetest.registered_nodes[nodename].mesecons.effector
local function get_mesecons_spec(nodename)
local def = minetest.registered_nodes[nodename]
return def and def.mesecons
end
local function get_rules(def, node)
local rules = def.rules
if type(rules) == 'function' then
if not def.rule_node_nocopy then
-- Copy the node to avoid overwriting data in the cache
node = {name = node.name, param1 = node.param1, param2 = node.param2}
end
return rules(node)
elseif rules then
return rules
end
return mesecon.rules.default
end
function mesecon.get_effector(nodename)
local mesecons_spec = get_mesecons_spec(nodename)
return mesecons_spec and mesecons_spec.effector
end
function mesecon.get_receptor(nodename)
if minetest.registered_nodes[nodename]
and minetest.registered_nodes[nodename].mesecons
and minetest.registered_nodes[nodename].mesecons.receptor then
return minetest.registered_nodes[nodename].mesecons.receptor
end
local mesecons_spec = get_mesecons_spec(nodename)
return mesecons_spec and mesecons_spec.receptor
end
function mesecon.get_conductor(nodename)
if minetest.registered_nodes[nodename]
and minetest.registered_nodes[nodename].mesecons
and minetest.registered_nodes[nodename].mesecons.conductor then
return minetest.registered_nodes[nodename].mesecons.conductor
end
local mesecons_spec = get_mesecons_spec(nodename)
return mesecons_spec and mesecons_spec.conductor
end
function mesecon.get_any_outputrules(node)
if not node then return nil end
if mesecon.is_conductor(node.name) then
return mesecon.conductor_get_rules(node)
elseif mesecon.is_receptor(node.name) then
return mesecon.receptor_get_rules(node)
end
if not node then return end
local mesecons_spec = get_mesecons_spec(node.name)
if not mesecons_spec then return end
local conductor = mesecons_spec.conductor
if conductor then return get_rules(conductor, node) end
local receptor = mesecons_spec.receptor
if receptor then return get_rules(receptor, node) end
end
function mesecon.get_any_inputrules(node)
if not node then return nil end
if mesecon.is_conductor(node.name) then
return mesecon.conductor_get_rules(node)
elseif mesecon.is_effector(node.name) then
return mesecon.effector_get_rules(node)
end
if not node then return end
local mesecons_spec = get_mesecons_spec(node.name)
if not mesecons_spec then return end
local conductor = mesecons_spec.conductor
if conductor then return get_rules(conductor, node) end
local effector = mesecons_spec.effector
if effector then return get_rules(effector, node) end
end
function mesecon.get_any_rules(node)
@ -127,16 +138,7 @@ end
function mesecon.receptor_get_rules(node)
local receptor = mesecon.get_receptor(node.name)
if receptor then
local rules = receptor.rules
if type(rules) == 'function' then
return rules(node)
elseif rules then
return rules
end
end
return mesecon.rules.default
return receptor and get_rules(receptor, node) or mesecon.rules.default
end
-- Effectors
@ -167,15 +169,7 @@ end
function mesecon.effector_get_rules(node)
local effector = mesecon.get_effector(node.name)
if effector then
local rules = effector.rules
if type(rules) == 'function' then
return rules(node)
elseif rules then
return rules
end
end
return mesecon.rules.default
return effector and get_rules(effector, node) or mesecon.rules.default
end
-- #######################
@ -340,21 +334,13 @@ end
function mesecon.conductor_get_rules(node)
local conductor = mesecon.get_conductor(node.name)
if conductor then
local rules = conductor.rules
if type(rules) == 'function' then
return rules(node)
elseif rules then
return rules
end
end
return mesecon.rules.default
return conductor and get_rules(conductor, node) or mesecon.rules.default
end
-- some more general high-level stuff
function mesecon.is_power_on(pos, rulename)
local node = mesecon.get_node_force(pos)
local node = mesecon.get_node_force_nocopy(pos)
if node and (mesecon.is_conductor_on(node, rulename) or mesecon.is_receptor_on(node.name)) then
return true
end
@ -362,7 +348,7 @@ function mesecon.is_power_on(pos, rulename)
end
function mesecon.is_power_off(pos, rulename)
local node = mesecon.get_node_force(pos)
local node = mesecon.get_node_force_nocopy(pos)
if node and (mesecon.is_conductor_off(node, rulename) or mesecon.is_receptor_off(node.name)) then
return true
end
@ -428,7 +414,7 @@ function mesecon.turnon(pos, link)
local depth = 1
for f in frontiers:iter() do
local node = mesecon.get_node_force(f.pos)
local node = mesecon.get_node_force_nocopy(f.pos)
if not node then
-- Area does not exist; do nothing
@ -441,7 +427,8 @@ function mesecon.turnon(pos, link)
for _, r in ipairs(mesecon.rule2meta(f.link, rules)) do
local np = vector.add(f.pos, r)
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})
end
end
@ -492,7 +479,7 @@ function mesecon.turnoff(pos, link)
local depth = 1
for f in frontiers:iter() do
local node = mesecon.get_node_force(f.pos)
local node = mesecon.get_node_force_nocopy(f.pos)
if not node then
-- Area does not exist; do nothing
@ -508,14 +495,15 @@ function mesecon.turnoff(pos, link)
-- Check if an onstate receptor is connected. If that is the case,
-- abort this turnoff process by returning false. `receptor_off` will
-- discard all the changes that we made in the voxelmanip:
if mesecon.rules_link_rule_all_inverted(f.pos, r)[1] then
if mesecon.is_receptor_on(mesecon.get_node_force(np).name) then
if mesecon.link_inverted(f.pos, np) then
if mesecon.is_receptor_on(mesecon.get_node_force_nocopy(np).name) then
return false
end
end
-- 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})
end
end
@ -554,48 +542,39 @@ function mesecon.turnoff(pos, link)
return true
end
-- Get all linking inputrules of inputnode (effector or conductor) that is connected to
-- outputnode (receptor or conductor) at position `output` and has an output in direction `rule`
function mesecon.rules_link_rule_all(output, rule)
local input = vector.add(output, rule)
local inputnode = mesecon.get_node_force(input)
-- Get the linking inputrule of inputnode (effector or conductor) that is connected to
-- outputnode (receptor or conductor) between positions `output` and `input`
function mesecon.link(output, input)
local inputnode = mesecon.get_node_force_nocopy(input)
local inputrules = mesecon.get_any_inputrules(inputnode)
if not inputrules then
return {}
end
local rules = {}
if not inputrules then return end
local dx, dy, dz = output.x - input.x, output.y - input.y, output.z - input.z
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 inputrule.x == dx and inputrule.y == dy and inputrule.z == dz then
return inputrule
end
end
return rules
end
-- 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)
-- Get the linking outputrule of outputnode (receptor or conductor) that is connected to
-- inputnode (effector or conductor) between positions `input` and `output`
function mesecon.link_inverted(input, output)
local outputnode = mesecon.get_node_force_nocopy(output)
local outputrules = mesecon.get_any_outputrules(outputnode)
if not outputrules then
return {}
end
local rules = {}
if not outputrules then return end
local dx, dy, dz = input.x - output.x, input.y - output.y, input.z - output.z
for _, outputrule in ipairs(mesecon.flattenrules(outputrules)) do
if vector.equals(vector.add(output, outputrule), input) then
table.insert(rules, mesecon.invertRule(outputrule))
if outputrule.x == dx and outputrule.y == dy and outputrule.z == dz then
return outputrule
end
end
return rules
end
function mesecon.is_powered(pos, rule)
local node = mesecon.get_node_force(pos)
local node = mesecon.get_node_force_nocopy(pos)
local rules = mesecon.get_any_inputrules(node)
if not rules then return false end
@ -604,23 +583,23 @@ function mesecon.is_powered(pos, rule)
if not rule then
for _, rule in ipairs(mesecon.flattenrules(rules)) do
local rulenames = mesecon.rules_link_rule_all_inverted(pos, rule)
for _, rname in ipairs(rulenames) do
local np = vector.add(pos, rname)
local nn = mesecon.get_node_force(np)
local np = vector.add(pos, rule)
local rname = mesecon.link_inverted(pos, np)
if rname then
local nn = mesecon.get_node_force_nocopy(np)
if (mesecon.is_conductor_on(nn, mesecon.invertRule(rname))
if (mesecon.is_conductor_on(nn, rname)
or mesecon.is_receptor_on(nn.name)) then
table.insert(sourcepos, np)
end
end
end
else
local rulenames = mesecon.rules_link_rule_all_inverted(pos, rule)
for _, rname in ipairs(rulenames) do
local np = vector.add(pos, rname)
local nn = mesecon.get_node_force(np)
if (mesecon.is_conductor_on (nn, mesecon.invertRule(rname))
local np = vector.add(pos, rule)
local rname = mesecon.link_inverted(pos, np)
if rname then
local nn = mesecon.get_node_force_nocopy(np)
if (mesecon.is_conductor_on (nn, rname)
or mesecon.is_receptor_on (nn.name)) then
table.insert(sourcepos, np)
end

View File

@ -12,3 +12,15 @@ for hash, _ in pairs(old_forceloaded_blocks) do
minetest.forceload_free_block(unhash_blockpos(hash))
end
os.remove(minetest.get_worldpath().."/mesecon_forceloaded")
-- Implement mesecon.rules_link_rule_all and mesecon.rules_link_rule_all_inverted
-- for mods that use them, even though they were internal functions.
function mesecon.rules_link_rule_all(output, rule)
return {mesecon.link(output, vector.add(output, rule))}
end
function mesecon.rules_link_rule_all_inverted(input, rule)
local r = mesecon.link_inverted(input, vector.add(input, rule))
return {r and mesecon.invertRule(r)}
end

View File

@ -411,16 +411,21 @@ local function vm_get_or_create_entry(pos)
return tbl
end
-- Gets the node at a given position during a VoxelManipulator-based
-- transaction.
function mesecon.vm_get_node(pos)
local function vm_get_node_nocopy(pos)
local hash = minetest.hash_node_position(pos)
local node = vm_node_cache[hash]
if not node then
node = vm_get_or_create_entry(pos).vm:get_node_at(pos)
vm_node_cache[hash] = node
end
return node.name ~= "ignore" and {name = node.name, param1 = node.param1, param2 = node.param2} or nil
return node.name ~= "ignore" and node or nil
end
-- Gets the node at a given position during a VoxelManipulator-based
-- transaction.
function mesecon.vm_get_node(pos)
local node = vm_get_node_nocopy(pos)
return node and {name = node.name, param1 = node.param1, param2 = node.param2}
end
-- Sets a nodes name during a VoxelManipulator-based transaction.
@ -445,6 +450,18 @@ function mesecon.vm_swap_node(pos, name, update_light)
tbl.dirty = true
end
-- Get node, loading map if necessary.
local function get_node_load(pos)
local node = minetest.get_node_or_nil(pos)
if node == nil then
-- Node is not currently loaded; use a VoxelManipulator to prime
-- the mapblock cache and try again.
minetest.get_voxel_manip(pos, pos)
node = minetest.get_node_or_nil(pos)
end
return node
end
-- Gets the node at a given position, regardless of whether it is loaded or
-- not, respecting a transaction if one is in progress.
--
@ -456,14 +473,16 @@ function mesecon.get_node_force(pos)
if vm_cache then
return mesecon.vm_get_node(pos)
else
local node = minetest.get_node_or_nil(pos)
if node == nil then
-- Node is not currently loaded; use a VoxelManipulator to prime
-- the mapblock cache and try again.
minetest.get_voxel_manip(pos, pos)
node = minetest.get_node_or_nil(pos)
end
return node
return get_node_load(pos)
end
end
-- Same without copying the internal node. Not part of public API.
function mesecon.get_node_force_nocopy(pos)
if vm_cache then
return vm_get_node_nocopy(pos)
else
return get_node_load(pos)
end
end
@ -485,7 +504,7 @@ function mesecon.swap_node_force(pos, name, update_light)
else
-- This serves to both ensure the mapblock is loaded and also hand us
-- the old node table so we can preserve param2.
local node = mesecon.get_node_force(pos)
local node = get_node_load(pos)
node.name = name
minetest.swap_node(pos, node)
end

View File

@ -56,6 +56,7 @@ minetest.register_node("mesecons_button:button_off", {
sounds = mesecon.node_sound.stone,
mesecons = {receptor = {
state = mesecon.state.off,
rule_node_nocopy = true,
rules = mesecon.rules.buttonlike_get
}},
on_blast = mesecon.on_blastnode,
@ -97,6 +98,7 @@ minetest.register_node("mesecons_button:button_on", {
sounds = mesecon.node_sound.stone,
mesecons = {receptor = {
state = mesecon.state.on,
rule_node_nocopy = true,
rules = mesecon.rules.buttonlike_get
}},
on_timer = mesecon.button_turnoff,

View File

@ -98,10 +98,12 @@ local off_state = {
receptor =
{
state = mesecon.state.off,
rule_node_nocopy = true,
rules = delayer_get_output_rules
},
effector =
{
rule_node_nocopy = true,
rules = delayer_get_input_rules,
action_on = delayer_activate
}

View File

@ -29,6 +29,7 @@ minetest.register_node("mesecons_extrawires:corner_on", {
mesecons = {conductor =
{
state = mesecon.state.on,
rule_node_nocopy = true,
rules = corner_get_rules,
offstate = "mesecons_extrawires:corner_off"
}},
@ -55,6 +56,7 @@ minetest.register_node("mesecons_extrawires:corner_off", {
mesecons = {conductor =
{
state = mesecon.state.off,
rule_node_nocopy = true,
rules = corner_get_rules,
onstate = "mesecons_extrawires:corner_on"
}},

View File

@ -52,6 +52,7 @@ for k, state in ipairs(doublecorner_states) do
mesecons = {
conductor = {
states = doublecorner_states,
rule_node_nocopy = true,
rules = doublecorner_get_rules,
},
},

View File

@ -41,6 +41,7 @@ minetest.register_node("mesecons_extrawires:tjunction_on", {
mesecons = {conductor =
{
state = mesecon.state.on,
rule_node_nocopy = true,
rules = tjunction_get_rules,
offstate = "mesecons_extrawires:tjunction_off"
}},
@ -71,6 +72,7 @@ minetest.register_node("mesecons_extrawires:tjunction_off", {
mesecons = {conductor =
{
state = mesecon.state.off,
rule_node_nocopy = true,
rules = tjunction_get_rules,
onstate = "mesecons_extrawires:tjunction_on"
}},

View File

@ -115,8 +115,10 @@ local function register_gate(name, inputnumber, assess, recipe, description)
groups = {dig_immediate = 2, overheat = 1},
mesecons = { receptor = {
state = "off",
rule_node_nocopy = true,
rules = gate_get_output_rules
}, effector = {
rule_node_nocopy = true,
rules = get_inputrules,
action_change = update_gate
}}
@ -134,8 +136,10 @@ local function register_gate(name, inputnumber, assess, recipe, description)
groups = {dig_immediate = 2, not_in_creative_inventory = 1, overheat = 1},
mesecons = { receptor = {
state = "on",
rule_node_nocopy = true,
rules = gate_get_output_rules
}, effector = {
rule_node_nocopy = true,
rules = get_inputrules,
action_change = update_gate
}}

View File

@ -36,6 +36,7 @@ minetest.register_node("mesecons_insulated:insulated_on", {
mesecons = {conductor = {
state = mesecon.state.on,
offstate = "mesecons_insulated:insulated_off",
rule_node_nocopy = true,
rules = insulated_wire_get_rules
}},
on_blast = mesecon.on_blastnode,
@ -72,6 +73,7 @@ minetest.register_node("mesecons_insulated:insulated_off", {
mesecons = {conductor = {
state = mesecon.state.off,
onstate = "mesecons_insulated:insulated_on",
rule_node_nocopy = true,
rules = insulated_wire_get_rules
}},
on_blast = mesecon.on_blastnode,

View File

@ -33,6 +33,7 @@ minetest.register_node("mesecons_lamp:lamp_on", {
action_off = function (pos, node)
minetest.swap_node(pos, {name = "mesecons_lamp:lamp_off", param2 = node.param2})
end,
rule_node_nocopy = true,
rules = mesecon.rules.wallmounted_get,
}},
on_blast = mesecon.on_blastnode,
@ -58,6 +59,7 @@ minetest.register_node("mesecons_lamp:lamp_off", {
action_on = function (pos, node)
minetest.swap_node(pos, {name = "mesecons_lamp:lamp_on", param2 = node.param2})
end,
rule_node_nocopy = true,
rules = mesecon.rules.wallmounted_get,
}},
on_blast = mesecon.on_blastnode,

View File

@ -283,6 +283,7 @@ minetest.register_node("mesecons_pistons:piston_normal_off", {
sounds = mesecon.node_sound.wood,
mesecons = {effector={
action_on = piston_on,
rule_node_nocopy = true,
rules = piston_get_rules,
}},
on_punch = piston_punch,
@ -313,6 +314,7 @@ minetest.register_node("mesecons_pistons:piston_normal_on", {
sounds = mesecon.node_sound.wood,
mesecons = {effector={
action_off = piston_off,
rule_node_nocopy = true,
rules = piston_get_rules,
}},
on_rotate = piston_rotate_on,
@ -362,6 +364,7 @@ minetest.register_node("mesecons_pistons:piston_sticky_off", {
sounds = mesecon.node_sound.wood,
mesecons = {effector={
action_on = piston_on,
rule_node_nocopy = true,
rules = piston_get_rules,
}},
on_punch = piston_punch,
@ -392,6 +395,7 @@ minetest.register_node("mesecons_pistons:piston_sticky_on", {
sounds = mesecon.node_sound.wood,
mesecons = {effector={
action_off = piston_off,
rule_node_nocopy = true,
rules = piston_get_rules,
}},
on_rotate = piston_rotate_on,

View File

@ -60,6 +60,7 @@ mesecon.register_node("mesecons_receiver:receiver", {
},
mesecons = {conductor = {
state = mesecon.state.off,
rule_node_nocopy = true,
rules = receiver_get_rules,
onstate = "mesecons_receiver:receiver_on"
}}

View File

@ -23,12 +23,14 @@ mesecon.register_node("mesecons_solarpanel:solar_panel", {
groups = {dig_immediate = 3},
mesecons = {receptor = {
state = mesecon.state.off,
rule_node_nocopy = true,
rules = mesecon.rules.wallmounted_get
}}
},{
groups = {dig_immediate = 3, not_in_creative_inventory = 1},
mesecons = {receptor = {
state = mesecon.state.on,
rule_node_nocopy = true,
rules = mesecon.rules.wallmounted_get
}},
})

View File

@ -69,6 +69,7 @@ minetest.register_node("mesecons_torch:mesecon_torch_off", {
sounds = mesecon.node_sound.default,
mesecons = {receptor = {
state = mesecon.state.off,
rule_node_nocopy = true,
rules = torch_get_output_rules
}},
on_blast = mesecon.on_blastnode,

View File

@ -36,6 +36,7 @@ mesecon.register_node("mesecons_walllever:wall_lever", {
mesh="jeija_wall_lever_off.obj",
on_rotate = mesecon.buttonlike_onrotate,
mesecons = {receptor = {
rule_node_nocopy = true,
rules = mesecon.rules.buttonlike_get,
state = mesecon.state.off
}},
@ -50,6 +51,7 @@ mesecon.register_node("mesecons_walllever:wall_lever", {
mesh="jeija_wall_lever_on.obj",
on_rotate = false,
mesecons = {receptor = {
rule_node_nocopy = true,
rules = mesecon.rules.buttonlike_get,
state = mesecon.state.on
}},