From dfe88b4aecc104c755a9a239cbcfb4221db04f03 Mon Sep 17 00:00:00 2001 From: copygirl Date: Tue, 31 Oct 2023 10:05:38 +0100 Subject: [PATCH] Use NodeCore's new spindata property - Remove nodecore_filtered_lookup NodeCore now exposes this information, and turns out there was a bug in it, anyway. - Move fix_rotatable_facedir to utility.lua - Add sanity check to ensure spindata exists - Depends on NodeCore `966ed012` --- README.md | 4 ---- init.lua | 27 +++++++++++++-------------- registry.lua | 15 +-------------- state.lua | 2 +- utility.lua | 19 ++++++++----------- 5 files changed, 23 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 2df596b..b2e67a8 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,6 @@ Rotating a node can be done in three ways: Sneaking while empty-handed inverts the direction of the rotation. -At this time, nodes that can be rotated are hardcoded into the mod, since -their valid orientations can't be determined just by their node definition. -A function mimicking NodeCore's `node_spin_filtered` has to be provided. - [Minetest]: https://www.minetest.net/ [NodeCore]: https://content.minetest.net/packages/Warr1024/nodecore/ [Extended Placement]: https://content.minetest.net/packages/gamefreq0/extended_placement/ diff --git a/init.lua b/init.lua index 05cc293..1fa2cec 100755 --- a/init.lua +++ b/init.lua @@ -1,5 +1,5 @@ -local ipairs, ItemStack, vector, include, nodecore - = ipairs, ItemStack, vector, include, nodecore +local ipairs, ItemStack, nodecore, include + = ipairs, ItemStack, nodecore, include rawset(_G, "nc_extended_rotating", {}) nc_extended_rotating.hud = include("hud") @@ -9,6 +9,12 @@ nc_extended_rotating.entity = include("entity") -- Depends on `utility`. nc_extended_rotating.registry = include("registry") -- Depends on `rotate` and `state` (late). nc_extended_rotating.state = include("state") -- Depends on `registry`, `rotate` and `utility`. +do + -- Do a quick check if we're using a compatible version of NodeCore. + local lens_def = minetest.registered_nodes["nc_optics:lens"] + if not lens_def.spindata then error("Requires NodeCore 966ed012 or later") end +end + ------------------------------------- -- Register Player Update Function -- ------------------------------------- @@ -30,24 +36,17 @@ nodecore.register_playerstep({ -- Register Rotatable NodeCore Nodes -- --------------------------------------- -local nodecore_filtered_lookup = nc_extended_rotating.utility.nodecore_filtered_lookup -local register_rotatable = nc_extended_rotating.registry.register_rotatable; +local register_rotatable = nc_extended_rotating.registry.register_rotatable; -- Register lenses. -local LENS_FILTERED_LOOKUP = nodecore_filtered_lookup(function(a, b) - return vector.equals(a.f, b.f) end) for _, lens_state in ipairs({ "", "_on", "_glow", "_glow_start" }) do - register_rotatable("nc_optics:lens" .. lens_state, nil, LENS_FILTERED_LOOKUP) end + register_rotatable("nc_optics:lens" .. lens_state) end -- Register prisms. -local PRISM_FILTERED_LOOKUP = nodecore_filtered_lookup(function(a, b) - return vector.equals(a.f, b.r) and vector.equals(a.r, b.f) end) for _, prism_state in ipairs({ "", "_on", "_gated" }) do - register_rotatable("nc_optics:prism" .. prism_state, nil, PRISM_FILTERED_LOOKUP) end + register_rotatable("nc_optics:prism" .. prism_state) end -- Register all existing and to-be-registered door panels. -local PANEL_FILTERED_LOOKUP = nodecore_filtered_lookup(function(a, b) - return vector.equals(a.f, b.r) and vector.equals(a.r, b.f) end) nodecore.register_on_register_item({ retroactive = true, func = function(name, def) @@ -57,8 +56,8 @@ nodecore.register_on_register_item({ if def.groups and def.groups.door then local panel = def.drop_in_place.name -- The panel version of the door. local pin = def.drop_non_silktouch or def.drop -- Dropped item is the door's pin. - register_rotatable(panel, nil, PANEL_FILTERED_LOOKUP, - function(_, _, _, item_stack) return ItemStack(item_stack):get_name() == pin end) + register_rotatable(panel, nil, function(_, _, _, item_stack) + return ItemStack(item_stack):get_name() == pin end) end end, }) diff --git a/registry.lua b/registry.lua index f9958c0..7c80e40 100644 --- a/registry.lua +++ b/registry.lua @@ -29,7 +29,7 @@ end local rotate_node = nc_extended_rotating.rotate.rotate_node -- local get_rotating_state = nc_extended_rotating.state.get_rotating_state -function registry.register_rotatable(name, def, facedir_lookup, rightclick_filter_func) +function registry.register_rotatable(name, def, rightclick_filter_func) def = def or minetest.registered_nodes[name] or error("Unknown node '" .. name .. "'") if def.paramtype2 ~= "facedir" then error("Node '" .. name .. "' must be 'facedir'") end @@ -49,7 +49,6 @@ function registry.register_rotatable(name, def, facedir_lookup, rightclick_filte end registered_rotatables[name] = { - facedir_lookup = facedir_lookup, rightclick_filter_func = rightclick_filter_func, } end @@ -67,16 +66,4 @@ function registry.is_rightclick_filtered(pos, node, player, item_stack, pointed_ and entry.rightclick_filter_func(pos, node, player, item_stack, pointed_thing) end --- Fixes facedir for the specified `node` when rotated to `facedir`. --- * Returns `nil` if the node can't rotate this way. --- * Returns `facedir` when the node can rotate this way. --- * Returns another facedir when the node should rotate another equivalent way. -function registry.fix_rotatable_facedir(node, facedir) - local name = node and node.name or "" - local entry = registered_rotatables[name] - if not entry then return nil end - if not entry.facedir_lookup then return facedir end - return entry.facedir_lookup[facedir] -end - return registry diff --git a/state.lua b/state.lua index 04f9e5e..1f92ed7 100755 --- a/state.lua +++ b/state.lua @@ -1,10 +1,10 @@ local math_round, minetest, vector = math.round, minetest, vector -local fix_rotatable_facedir = nc_extended_rotating.registry.fix_rotatable_facedir; local is_rightclick_filtered = nc_extended_rotating.registry.is_rightclick_filtered; local is_rotatable = nc_extended_rotating.registry.is_rotatable; local rotate_facedir = nc_extended_rotating.rotate.rotate_facedir; +local fix_rotatable_facedir = nc_extended_rotating.utility.fix_rotatable_facedir; local rotation_vector_from_lookat = nc_extended_rotating.utility.rotation_vector_from_lookat; -- Distance at which we want to rotate by "pushing" on an edge. diff --git a/utility.lua b/utility.lua index 1a41d1e..a08f079 100644 --- a/utility.lua +++ b/utility.lua @@ -44,17 +44,14 @@ function utility.rotation_vector_from_lookat(node, pointed_thing, edge_distance) return b - math_abs(p) <= edge_distance and math_sign(p) or 0 end) end -function utility.nodecore_filtered_lookup(eq_func) - local lookup = {} - for i = 0, 23 do - local facedir = nodecore.facedirs[i] - for j = 0, #lookup - 1 do - local other = nodecore.facedirs[lookup[j]] - if eq_func(facedir, other) then lookup[i] = j; break end - end - lookup[i] = lookup[i] or i - end - return lookup +-- NodeCore only has a couple of "canonical" orientations for its nodes. +-- This returns a canonical facedir equivalent to the one passed in. +function utility.fix_rotatable_facedir(node, facedir) + local name = node and node.name or "" + local def = minetest.registered_nodes[name] + if not def then return nil end + if not def.spindata then return facedir end + return def.spindata.equiv[facedir] end return utility