Reduce redundancy in minetest.registered_nodes lookups

This commit is contained in:
SmallJoker 2023-12-27 10:20:04 +01:00 committed by sfan5
parent 6b42419828
commit 7418d5cb61
4 changed files with 55 additions and 59 deletions

View File

@ -51,27 +51,18 @@ mesecon.fifo_queue = dofile(minetest.get_modpath("mesecons").."/fifo_queue.lua")
-- General -- General
function mesecon.get_effector(nodename) function mesecon.get_effector(nodename)
if minetest.registered_nodes[nodename] local def = minetest.registered_nodes[nodename]
and minetest.registered_nodes[nodename].mesecons return def and def.mesecons and def.mesecons.effector
and minetest.registered_nodes[nodename].mesecons.effector then
return minetest.registered_nodes[nodename].mesecons.effector
end
end end
function mesecon.get_receptor(nodename) function mesecon.get_receptor(nodename)
if minetest.registered_nodes[nodename] local def = minetest.registered_nodes[nodename]
and minetest.registered_nodes[nodename].mesecons return def and def.mesecons and def.mesecons.receptor
and minetest.registered_nodes[nodename].mesecons.receptor then
return minetest.registered_nodes[nodename].mesecons.receptor
end
end end
function mesecon.get_conductor(nodename) function mesecon.get_conductor(nodename)
if minetest.registered_nodes[nodename] local def = minetest.registered_nodes[nodename]
and minetest.registered_nodes[nodename].mesecons return def and def.mesecons and def.mesecons.conductor
and minetest.registered_nodes[nodename].mesecons.conductor then
return minetest.registered_nodes[nodename].mesecons.conductor
end
end end
function mesecon.get_any_outputrules(node) function mesecon.get_any_outputrules(node)

View File

@ -40,35 +40,39 @@ local bottom_rules = {
{x=0, y=2, z=0} -- receive power from pressure plate / detector / ... 2 nodes above {x=0, y=2, z=0} -- receive power from pressure plate / detector / ... 2 nodes above
} }
local function is_vertical_conductor(nodename)
local def = minetest.registered_nodes[nodename]
return def and def.is_vertical_conductor
end
local vertical_updatepos = function (pos) local vertical_updatepos = function (pos)
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
if minetest.registered_nodes[node.name] if not is_vertical_conductor(node.name) then
and minetest.registered_nodes[node.name].is_vertical_conductor then return
local node_above = minetest.get_node(vector.add(pos, vertical_rules[1]))
local node_below = minetest.get_node(vector.add(pos, vertical_rules[2]))
local above = minetest.registered_nodes[node_above.name]
and minetest.registered_nodes[node_above.name].is_vertical_conductor
local below = minetest.registered_nodes[node_below.name]
and minetest.registered_nodes[node_below.name].is_vertical_conductor
mesecon.on_dignode(pos, node)
-- Always place offstate conductor and let mesecon.on_placenode take care
local newname = "mesecons_extrawires:vertical_"
if above and below then -- above and below: vertical mesecon
newname = newname .. "off"
elseif above and not below then -- above only: bottom
newname = newname .. "bottom_off"
elseif not above and below then -- below only: top
newname = newname .. "top_off"
else -- no vertical wire above, no vertical wire below: use bottom
newname = newname .. "bottom_off"
end
minetest.set_node(pos, {name = newname})
mesecon.on_placenode(pos, {name = newname})
end end
local node_above = minetest.get_node(vector.add(pos, vertical_rules[1]))
local node_below = minetest.get_node(vector.add(pos, vertical_rules[2]))
local above = is_vertical_conductor(node_above.name)
local below = is_vertical_conductor(node_below.name)
mesecon.on_dignode(pos, node)
-- Always place offstate conductor and let mesecon.on_placenode take care
local newname = "mesecons_extrawires:vertical_"
if above and below then -- above and below: vertical mesecon
newname = newname .. "off"
elseif above and not below then -- above only: bottom
newname = newname .. "bottom_off"
elseif not above and below then -- below only: top
newname = newname .. "top_off"
else -- no vertical wire above, no vertical wire below: use bottom
newname = newname .. "bottom_off"
end
minetest.set_node(pos, {name = newname})
mesecon.on_placenode(pos, {name = newname})
end end
local vertical_update = function (pos) local vertical_update = function (pos)

View File

@ -81,9 +81,9 @@ function mesecon.mvps_get_stack(pos, dir, maximum, all_pull_sticky)
if #nodes > maximum then return nil end if #nodes > maximum then return nil end
-- add connected nodes to frontiers -- add connected nodes to frontiers
if minetest.registered_nodes[nn.name] local nndef = minetest.registered_nodes[nn.name]
and minetest.registered_nodes[nn.name].mvps_sticky then if nndef and nndef.mvps_sticky then
local connected = minetest.registered_nodes[nn.name].mvps_sticky(np, nn) local connected = nndef.mvps_sticky(np, nn)
for _, cp in ipairs(connected) do for _, cp in ipairs(connected) do
frontiers:add(cp) frontiers:add(cp)
end end
@ -96,10 +96,9 @@ function mesecon.mvps_get_stack(pos, dir, maximum, all_pull_sticky)
for _, r in ipairs(mesecon.rules.alldirs) do for _, r in ipairs(mesecon.rules.alldirs) do
local adjpos = vector.add(np, r) local adjpos = vector.add(np, r)
local adjnode = minetest.get_node(adjpos) local adjnode = minetest.get_node(adjpos)
if minetest.registered_nodes[adjnode.name] local adjdef = minetest.registered_nodes[adjnode.name]
and minetest.registered_nodes[adjnode.name].mvps_sticky then if adjdef and adjdef.mvps_sticky then
local sticksto = minetest.registered_nodes[adjnode.name] local sticksto = adjdef.mvps_sticky(adjpos, adjnode)
.mvps_sticky(adjpos, adjnode)
-- connects to this position? -- connects to this position?
for _, link in ipairs(sticksto) do for _, link in ipairs(sticksto) do

View File

@ -13,11 +13,11 @@ local S = minetest.get_translator(minetest.get_current_modname())
-- self_pos = pos of any mesecon node, from_pos = pos of conductor to getconnect for -- self_pos = pos of any mesecon node, from_pos = pos of conductor to getconnect for
local wire_getconnect = function (from_pos, self_pos) local wire_getconnect = function (from_pos, self_pos)
local node = minetest.get_node(self_pos) local node = minetest.get_node(self_pos)
if minetest.registered_nodes[node.name] local def = minetest.registered_nodes[node.name]
and minetest.registered_nodes[node.name].mesecons then if def and def.mesecons then
-- rules of node to possibly connect to -- rules of node to possibly connect to
local rules local rules
if (minetest.registered_nodes[node.name].mesecon_wire) then if def.mesecon_wire then
rules = mesecon.rules.default rules = mesecon.rules.default
else else
rules = mesecon.get_any_rules(node) rules = mesecon.get_any_rules(node)
@ -68,16 +68,18 @@ end
local update_on_place_dig = function (pos, node) local update_on_place_dig = function (pos, node)
-- Update placed node (get_node again as it may have been dug) -- Update placed node (get_node again as it may have been dug)
local nn = minetest.get_node(pos) do
if (minetest.registered_nodes[nn.name]) local nn = minetest.get_node(pos)
and (minetest.registered_nodes[nn.name].mesecon_wire) then local def = minetest.registered_nodes[nn.name]
wire_updateconnect(pos) if def and def.mesecon_wire then
wire_updateconnect(pos)
end
end end
-- Update nodes around it -- Update nodes around it
local rules local rules
if minetest.registered_nodes[node.name] local ndef = minetest.registered_nodes[node.name]
and minetest.registered_nodes[node.name].mesecon_wire then if ndef and ndef.mesecon_wire then
rules = mesecon.rules.default rules = mesecon.rules.default
else else
rules = mesecon.get_any_rules(node) rules = mesecon.get_any_rules(node)
@ -86,8 +88,8 @@ local update_on_place_dig = function (pos, node)
for _, r in ipairs(mesecon.flattenrules(rules)) do for _, r in ipairs(mesecon.flattenrules(rules)) do
local np = vector.add(pos, r) local np = vector.add(pos, r)
if minetest.registered_nodes[minetest.get_node(np).name] local rdef = minetest.registered_nodes[minetest.get_node(np).name]
and minetest.registered_nodes[minetest.get_node(np).name].mesecon_wire then if rdef and rdef.mesecon_wire then
wire_updateconnect(np) wire_updateconnect(np)
end end
end end