Avoid most node copies

This commit is contained in:
Jude Melton-Houghton 2022-05-19 21:17:48 -04:00
parent 468ec521ea
commit d4acc6a8a6
15 changed files with 72 additions and 22 deletions

View File

@ -59,6 +59,10 @@ end
local function get_rules(def, node)
local rules = def.rules
if type(rules) == 'function' then
if not def.const_node 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
@ -336,7 +340,7 @@ 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
@ -344,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
@ -410,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
@ -475,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
@ -492,7 +496,7 @@ function mesecon.turnoff(pos, link)
-- abort this turnoff process by returning false. `receptor_off` will
-- discard all the changes that we made in the voxelmanip:
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_nocopy(np).name) then
return false
end
end
@ -538,7 +542,7 @@ end
-- 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(input)
local inputnode = mesecon.get_node_force_nocopy(input)
local inputrules = mesecon.get_any_inputrules(inputnode)
if not inputrules then return end
@ -554,7 +558,7 @@ end
-- 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(output)
local outputnode = mesecon.get_node_force_nocopy(output)
local outputrules = mesecon.get_any_outputrules(outputnode)
if not outputrules then return end
@ -567,7 +571,7 @@ function mesecon.link_inverted(input, output)
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
@ -579,7 +583,7 @@ function mesecon.is_powered(pos, rule)
local rname = mesecon.link_inverted(pos, vector.add(pos, rule))
if rname then
local np = vector.add(pos, rname)
local nn = mesecon.get_node_force(np)
local nn = mesecon.get_node_force_nocopy(np)
if (mesecon.is_conductor_on(nn, mesecon.invertRule(rname))
or mesecon.is_receptor_on(nn.name)) then
@ -591,7 +595,7 @@ function mesecon.is_powered(pos, rule)
local rname = mesecon.link_inverted(pos, vector.add(pos, rule))
if rname then
local np = vector.add(pos, rname)
local nn = mesecon.get_node_force(np)
local nn = mesecon.get_node_force_nocopy(np)
if (mesecon.is_conductor_on (nn, mesecon.invertRule(rname))
or mesecon.is_receptor_on (nn.name)) then
table.insert(sourcepos, np)

View File

@ -390,16 +390,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.
@ -424,6 +429,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.
--
@ -435,14 +452,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

View File

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

View File

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

View File

@ -34,6 +34,7 @@ minetest.register_node("mesecons_extrawires:corner_on", {
mesecons = {conductor =
{
state = mesecon.state.on,
const_node = true,
rules = corner_get_rules,
offstate = "mesecons_extrawires:corner_off"
}},
@ -60,6 +61,7 @@ minetest.register_node("mesecons_extrawires:corner_off", {
mesecons = {conductor =
{
state = mesecon.state.off,
const_node = true,
rules = corner_get_rules,
onstate = "mesecons_extrawires:corner_on"
}},

View File

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

View File

@ -46,6 +46,7 @@ minetest.register_node("mesecons_extrawires:tjunction_on", {
mesecons = {conductor =
{
state = mesecon.state.on,
const_node = true,
rules = tjunction_get_rules,
offstate = "mesecons_extrawires:tjunction_off"
}},
@ -76,6 +77,7 @@ minetest.register_node("mesecons_extrawires:tjunction_off", {
mesecons = {conductor =
{
state = mesecon.state.off,
const_node = true,
rules = tjunction_get_rules,
onstate = "mesecons_extrawires:tjunction_on"
}},

View File

@ -99,8 +99,10 @@ local function register_gate(name, inputnumber, assess, recipe, description)
groups = {dig_immediate = 2, overheat = 1},
mesecons = { receptor = {
state = "off",
const_node = true,
rules = gate_get_output_rules
}, effector = {
const_node = true,
rules = get_inputrules,
action_change = update_gate
}}
@ -118,8 +120,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",
const_node = true,
rules = gate_get_output_rules
}, effector = {
const_node = true,
rules = get_inputrules,
action_change = update_gate
}}

View File

@ -38,6 +38,7 @@ minetest.register_node("mesecons_insulated:insulated_on", {
mesecons = {conductor = {
state = mesecon.state.on,
offstate = "mesecons_insulated:insulated_off",
const_node = true,
rules = insulated_wire_get_rules
}},
on_blast = mesecon.on_blastnode,
@ -74,6 +75,7 @@ minetest.register_node("mesecons_insulated:insulated_off", {
mesecons = {conductor = {
state = mesecon.state.off,
onstate = "mesecons_insulated:insulated_on",
const_node = true,
rules = insulated_wire_get_rules
}},
on_blast = mesecon.on_blastnode,

View File

@ -31,6 +31,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,
const_node = true,
rules = mesecon.rules.wallmounted_get,
}},
on_blast = mesecon.on_blastnode,
@ -56,6 +57,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,
const_node = true,
rules = mesecon.rules.wallmounted_get,
}},
on_blast = mesecon.on_blastnode,

View File

@ -281,6 +281,7 @@ minetest.register_node("mesecons_pistons:piston_normal_off", {
sounds = mesecon.node_sound.wood,
mesecons = {effector={
action_on = piston_on,
const_node = true,
rules = piston_get_rules,
}},
on_punch = piston_punch,
@ -311,6 +312,7 @@ minetest.register_node("mesecons_pistons:piston_normal_on", {
sounds = mesecon.node_sound.wood,
mesecons = {effector={
action_off = piston_off,
const_node = true,
rules = piston_get_rules,
}},
on_rotate = piston_rotate_on,
@ -360,6 +362,7 @@ minetest.register_node("mesecons_pistons:piston_sticky_off", {
sounds = mesecon.node_sound.wood,
mesecons = {effector={
action_on = piston_on,
const_node = true,
rules = piston_get_rules,
}},
on_punch = piston_punch,
@ -390,6 +393,7 @@ minetest.register_node("mesecons_pistons:piston_sticky_on", {
sounds = mesecon.node_sound.wood,
mesecons = {effector={
action_off = piston_off,
const_node = true,
rules = piston_get_rules,
}},
on_rotate = piston_rotate_on,

View File

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

View File

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

View File

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

View File

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