fix broken screwdriver mode2

This commit is contained in:
MetaDucky 2014-04-24 20:30:07 +02:00
parent 15740ffd3d
commit 639ac00dc6

View file

@ -6,13 +6,26 @@ local mode_text = {
{"Bring top in front then rotate it."}, {"Bring top in front then rotate it."},
} }
-- Use Lua-friendly tables to avoid hash-lookups:
local opposite_faces = { local opposite_faces = {
[0] = 5, -- 0 1 2 3 4 5
[1] = 2, 5, 2, 1, 4, 3, 0
[2] = 1, }
[3] = 4,
[4] = 3, -- Rotational order for a given rotation axis as primary index:
[5] = 0, -- Negative values indicate X rotation adjustments (rotate to opposite side),
-- 00 is just a visual indicator for invalid/unused axisdir indices.
-- Rotation order is always clockwise.
-- Layout: Row is selected by the face pointed at (the rotation axis),
-- current axisdir selects column, value is next axisdir:
local axis_rotations = {
-- 0 1 2 3 4 5
{00, 3, 4, 2, 1, 00}, -- Y+
{ 4, 00, 00, 0, 5, 3}, -- Z+
{ 3, 00, 00, 5, 0, 4}, -- Z-
{ 1, -5, 0, 00, 00, -2}, -- X+
{ 2, 0, -5, 00, 00, -1}, -- X-
{00, 4, 3, 1, 2, 00}, -- Y-
} }
local function screwdriver_setmode(user,itemstack) local function screwdriver_setmode(user,itemstack)
@ -47,14 +60,6 @@ local function get_node_face(pointed_thing)
end end
end end
local function nextrange(x, max)
x = x + 1
if x > max then
x = 0
end
return x
end
local function screwdriver_handler(itemstack, user, pointed_thing) local function screwdriver_handler(itemstack, user, pointed_thing)
if pointed_thing.type ~= "node" then if pointed_thing.type ~= "node" then
return return
@ -78,45 +83,53 @@ local function screwdriver_handler(itemstack, user, pointed_thing)
node.param2 == nil then node.param2 == nil then
return return
end end
-- Get ready to set the param2
local n = node.param2 -- Split current param2 facedir value into axisdir and rotation:
local axisdir = math.floor(n / 4) local rotation = node.param2 % 4
local rotation = n - axisdir * 4 local axisdir = math.floor(node.param2 / 4) % 6
if mode == 1 then if mode == 1 then
n = axisdir * 4 + nextrange(rotation, 3) rotation = rotation + 1
elseif mode == 2 then elseif mode == 2 then
-- If you are pointing at the axisdir face or the -- rotates the pointed face clockwise:
-- opposite one then you can just rotate the node.
-- Otherwise change the axisdir, avoiding the facing
-- and opposite axes.
local face = get_node_face(pointed_thing) local face = get_node_face(pointed_thing)
if axisdir == face or axisdir == opposite_faces[face] then -- when rotating the top/bottom faces, only change rotation:
n = axisdir * 4 + nextrange(rotation, 3) if axisdir == face then
rotation = rotation + 1
elseif axisdir == opposite_faces[face+1] then
rotation = rotation - 1
else else
axisdir = nextrange(axisdir, 5) -- rotate side faces: get next axisdir from table:
-- This is repeated because switching from the face axisdir = axis_rotations[face+1][axisdir+1]
-- can move to to the opposite and vice-versa
if axisdir == face or axisdir == opposite_faces[face] then -- handle rotation adjustment (to keep face in front):
axisdir = nextrange(axisdir, 5) if face == 0 then -- Y+ (top) axis rotation fix
rotation = rotation + 1
elseif face == 5 then -- Y- (bottom)
rotation = rotation - 1
elseif axisdir < 0 then -- X axis rotation fix
axisdir = -axisdir
rotation = rotation + 2
end end
if axisdir == face or axisdir == opposite_faces[face] then
axisdir = nextrange(axisdir, 5)
end
n = axisdir * 4
end end
elseif mode == 3 then elseif mode == 3 then
n = nextrange(axisdir, 5) * 4 axisdir = axisdir + 1
rotation = 0
elseif mode == 4 then elseif mode == 4 then
local face = get_node_face(pointed_thing) local face = get_node_face(pointed_thing)
if axisdir == face then if axisdir == face then
n = axisdir * 4 + nextrange(rotation, 3) rotation = rotation + 1
else else
n = face * 4 axisdir = face
rotation = 0
end end
end end
--print (dump(axisdir..", "..rotation))
node.param2 = n -- recombine axisdir and rotation to facedir value:
node.param2 = (axisdir%6) * 4 + (rotation%4)
minetest.swap_node(pos, node) minetest.swap_node(pos, node)
if not minetest.setting_getbool("creative_mode") then
local item_wear = tonumber(itemstack:get_wear()) local item_wear = tonumber(itemstack:get_wear())
item_wear = item_wear + 327 item_wear = item_wear + 327
if item_wear > 65535 then if item_wear > 65535 then
@ -124,6 +137,7 @@ local function screwdriver_handler(itemstack, user, pointed_thing)
return itemstack return itemstack
end end
itemstack:set_wear(item_wear) itemstack:set_wear(item_wear)
end
return itemstack return itemstack
end end