- Use global to hold sub-modules - Move state calculation to state.lua - Do manual overriding of on_rightclick functions - Call default right-click impl when holding pin on doormain
parent
b0fc98b841
commit
04ca1950d7
6 changed files with 226 additions and 177 deletions
@ -1,73 +1,34 @@ |
||||
local minetest, nodecore |
||||
= minetest, nodecore |
||||
local minetest |
||||
= minetest |
||||
|
||||
local registry = {} |
||||
-- Table of nodes that can be rotated by the mod. |
||||
local registered_rotatables = {} |
||||
|
||||
-- Registered rotatable nodes will call this function when their `on_rightclick` |
||||
-- is triggered instead of their default implementation. Can be overwritten. |
||||
registry.custom_on_rightclick = function(pos, node, clicker, item_stack, pointed_thing) end |
||||
|
||||
local function register_rotatable(name, facedir_lookup) |
||||
local registry = {} |
||||
|
||||
function registry.register_rotatable(name, facedir_lookup) |
||||
local def = minetest.registered_nodes[name] |
||||
if not def then error("Unknown node '" .. name .. "'") end |
||||
if def.paramtype2 ~= "facedir" then error("Node '" .. name .. "' must be 'facedir'") end |
||||
def.on_rightclick = function(...) registry.custom_on_rightclick(...) end |
||||
registered_rotatables[name] = { lookup = facedir_lookup } |
||||
end |
||||
registry.register_rotatable = register_rotatable |
||||
|
||||
-- Returns whether the specified `node` is rotatable by this mod. |
||||
local function is_rotatable(node) |
||||
local name = node and node.name or "" |
||||
return not not registered_rotatables[name] |
||||
function registry.is_rotatable(node) |
||||
return not not registered_rotatables[node.name] |
||||
end |
||||
registry.is_rotatable = is_rotatable |
||||
|
||||
-- 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. |
||||
local function fix_rotatable_facedir(node, facedir) |
||||
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.lookup then return facedir end |
||||
return entry.lookup[facedir] |
||||
end |
||||
registry.fix_rotatable_facedir = fix_rotatable_facedir |
||||
|
||||
|
||||
-------------------------------------------- |
||||
-- Register rotatable nodes from NodeCore -- |
||||
-------------------------------------------- |
||||
|
||||
local function 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 |
||||
end |
||||
|
||||
local LENS_FILTERED_LOOKUP = nodecore_filtered_lookup( |
||||
function(a, b) return vector.equals(a.f, b.f) end) |
||||
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) |
||||
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) |
||||
|
||||
for _, lens_state in ipairs({ "", "_on", "_glow", "_glow_start" }) do |
||||
register_rotatable("nc_optics:lens" .. lens_state, LENS_FILTERED_LOOKUP) end |
||||
for _, prism_state in ipairs({ "", "_on", "_gated" }) do |
||||
register_rotatable("nc_optics:prism" .. prism_state, PRISM_FILTERED_LOOKUP) end |
||||
|
||||
register_rotatable("nc_doors:panel_plank" , PANEL_FILTERED_LOOKUP) |
||||
register_rotatable("nc_doors:panel_cobble", PANEL_FILTERED_LOOKUP) |
||||
|
||||
return registry |
||||
|
@ -0,0 +1,83 @@ |
||||
local math_round, minetest, vector |
||||
= math.round, minetest, vector |
||||
|
||||
local fix_rotatable_facedir = nc_extended_rotating.registry.fix_rotatable_facedir; |
||||
local is_rotatable = nc_extended_rotating.registry.is_rotatable; |
||||
local rotate_facedir = nc_extended_rotating.rotate.rotate_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. |
||||
local EDGE_DISTANCE = 2.5 / 16 -- texels (at 16² texture resolution) |
||||
|
||||
|
||||
-- Contains a per-player state that holds information, created by a raycast |
||||
-- done in `playerstep`, about the node this player might rotate. This is used |
||||
-- to update the their HUD and to decide what to do when a rightclick occurs. |
||||
local rotating_state = {} |
||||
|
||||
-- Calculates the state for the specified `player` and player `data`. |
||||
-- This state contains information about how the player would rotate a block. |
||||
local function calculate_rotating_state(player, data) |
||||
local state = {} |
||||
|
||||
local pointed_thing = data.raycast() |
||||
if (not pointed_thing) or pointed_thing.type ~= "node" then return nil end |
||||
|
||||
state.pos = pointed_thing.under |
||||
state.node = minetest.get_node(state.pos) |
||||
if not is_rotatable(state.node) then return nil end |
||||
|
||||
if vector.equals(pointed_thing.above, pointed_thing.under) then return nil end |
||||
state.face = pointed_thing.above - pointed_thing.under |
||||
|
||||
-- When player is sneaking, must be empty-handed for rotating to work. |
||||
local is_sneaking = player:get_player_control().sneak |
||||
if is_sneaking and (not player:get_wielded_item():is_empty()) then return nil end |
||||
|
||||
local degrees = 90 |
||||
local r = rotation_vector_from_lookat(state.node, pointed_thing, EDGE_DISTANCE) |
||||
state.axis = r |
||||
|
||||
local num = math_round(r.x * r.x + r.y * r.y + r.z * r.z) -- Squared length. |
||||
if num == 1 then state.mode = "face" |
||||
elseif num == 2 then state.mode = "edge"; state.axis = state.face:cross(r) |
||||
elseif num == 3 then state.mode = "mirror"; degrees = 120 |
||||
end |
||||
|
||||
-- Sneaking causes the direction of the rotation to be inverted. |
||||
if is_sneaking then degrees = -degrees end |
||||
state.invert = is_sneaking |
||||
|
||||
state.facedir = rotate_facedir(state.node.param2, state.axis, -degrees) |
||||
state.facedir = fix_rotatable_facedir(state.node, state.facedir) |
||||
state.success = state.facedir and state.facedir ~= state.node.param2 |
||||
|
||||
return state |
||||
end |
||||
|
||||
minetest.register_on_leaveplayer(function(player) |
||||
local name = player:get_player_name() |
||||
rotating_state[name] = nil |
||||
end) |
||||
|
||||
|
||||
local state = {} |
||||
|
||||
function state.update_rotating_state(player, data) |
||||
local rot_state = calculate_rotating_state(player, data) |
||||
rotating_state[player:get_player_name()] = rot_state |
||||
return rot_state |
||||
end |
||||
|
||||
function state.get_rotating_state(player, pos, node) |
||||
if not minetest.is_player(player) then return nil end |
||||
local rot_state = rotating_state[player:get_player_name()] |
||||
if not rot_state then return nil end |
||||
|
||||
if not vector.equals(pos, rot_state.pos) then return nil end |
||||
if node.name ~= rot_state.node.name or node.param2 ~= rot_state.node.param2 then return nil end |
||||
|
||||
return rot_state |
||||
end |
||||
|
||||
return state |
Loading…
Reference in new issue