From 05cd360029ee151e5413e2b119dbd813c1184395 Mon Sep 17 00:00:00 2001 From: entuland Date: Wed, 20 Jun 2018 15:02:14 +0200 Subject: [PATCH] Support wallmounted nodes with default screwdriver behavior --- README.md | 1 + init.lua | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/README.md b/README.md index 1700051..414def5 100644 --- a/README.md +++ b/README.md @@ -85,5 +85,6 @@ Here are possible messages you can receive: - Pulled closest edge (sneak + left click) - Cannot rotate node with paramtype2 == glasslikeliquidlevel - Unsupported node type: modname:nodename +- Wallmounted node rotated with default screwdriver behavior plus some more messages warning about protected areas or rotations performed or prevented by custom on_rotate() handlers. diff --git a/init.lua b/init.lua index e0db91f..ecbeeef 100644 --- a/init.lua +++ b/init.lua @@ -284,6 +284,47 @@ end handlers.colorfacedir = handlers.facedir +-- ============================================================ +-- Replicate default screwdriver behavior for wallmounted nodes + +-- For attached wallmounted nodes: returns true if rotation is valid +-- simplified version of minetest:builtin/game/falling.lua#L148. +local function check_attached_node(pos, rotation) + local d = minetest.wallmounted_to_dir(rotation) + local p2 = vector.add(pos, d) + local n = minetest.get_node(p2).name + local def2 = minetest.registered_nodes[n] + if def2 and not def2.walkable then + return false + end + return true +end + +local wallmounted_tbl = { + [PRIMARY_BTN] = {[2] = 5, [3] = 4, [4] = 2, [5] = 3, [1] = 0, [0] = 1}, + [SECONDARY_BTN] = {[2] = 5, [3] = 4, [4] = 2, [5] = 1, [1] = 0, [0] = 3} +} + +function handlers.wallmounted(node, player, pointed_thing, click) + local pos = pointed_thing.under + local rotation = node.param2 % 8 -- get first 3 bits + local other = node.param2 - rotation + rotation = wallmounted_tbl[click][rotation] or 0 + if minetest.get_item_group(node.name, "attached_node") ~= 0 then + -- find an acceptable orientation + for i = 1, 5 do + if not check_attached_node(pos, rotation) then + rotation = wallmounted_tbl[click][rotation] or 0 + else + break + end + end + end + return rotation + other, "Wallmounted node rotated with default screwdriver behavior" +end + +handlers.colorwallmounted = handlers.wallmounted + -- ============================================================ -- interaction