Screwdriver: allow simple wallmounted rotation.

Allows rotating things like signs and torches. Axis rotation
rotates over all 6 faces, face rotation flips upside down to
flat on floor only, and of course in the 4 horizontal directions.

Made the code a bit more modular to account for different rotation
schemes. Should be easier to extend from here on to other needs,
and the functions can be reused by other mods for convenience.
This commit is contained in:
Auke Kok 2017-02-24 22:44:52 -08:00 committed by paramat
parent 0850314d74
commit f14b0a6ff5

View File

@ -19,6 +19,31 @@ screwdriver.rotate_simple = function(pos, node, user, mode, new_param2)
end end
end end
screwdriver.rotate = {}
screwdriver.rotate.facedir = function(node, mode)
-- Compute param2
local rotationPart = node.param2 % 32 -- get first 4 bits
local preservePart = node.param2 - rotationPart
local axisdir = math.floor(rotationPart / 4)
local rotation = rotationPart - axisdir * 4
if mode == screwdriver.ROTATE_FACE then
rotationPart = axisdir * 4 + nextrange(rotation, 3)
elseif mode == screwdriver.ROTATE_AXIS then
rotationPart = nextrange(axisdir, 5) * 4
end
return preservePart + rotationPart
end
local wallmounted_tbl = {
[screwdriver.ROTATE_FACE] = {[2] = 5, [3] = 4, [4] = 2, [5] = 3, [1] = 0, [0] = 1},
[screwdriver.ROTATE_AXIS] = {[2] = 5, [3] = 4, [4] = 2, [5] = 1, [1] = 0, [0] = 3}
}
screwdriver.rotate.wallmounted = function(node, mode)
return wallmounted_tbl[mode][node.param2]
end
-- Handles rotation -- Handles rotation
screwdriver.handler = function(itemstack, user, pointed_thing, mode, uses) screwdriver.handler = function(itemstack, user, pointed_thing, mode, uses)
if pointed_thing.type ~= "node" then if pointed_thing.type ~= "node" then
@ -34,23 +59,14 @@ screwdriver.handler = function(itemstack, user, pointed_thing, mode, uses)
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
local ndef = minetest.registered_nodes[node.name] local ndef = minetest.registered_nodes[node.name]
-- verify node is facedir (expected to be rotatable) -- can we rotate this paramtype2?
if not ndef or ndef.paramtype2 ~= "facedir" then local fn = screwdriver.rotate[ndef.paramtype2]
if not fn then
return return
end end
-- Compute param2
local rotationPart = node.param2 % 32 -- get first 4 bits
local preservePart = node.param2 - rotationPart
local axisdir = math.floor(rotationPart / 4)
local rotation = rotationPart - axisdir * 4
if mode == screwdriver.ROTATE_FACE then
rotationPart = axisdir * 4 + nextrange(rotation, 3)
elseif mode == screwdriver.ROTATE_AXIS then
rotationPart = nextrange(axisdir, 5) * 4
end
local new_param2 = preservePart + rotationPart
local should_rotate = true local should_rotate = true
local new_param2 = fn(node, mode)
-- Node provides a handler, so let the handler decide instead if the node can be rotated -- Node provides a handler, so let the handler decide instead if the node can be rotated
if ndef and ndef.on_rotate then if ndef and ndef.on_rotate then