Screwdriver: use table lookup for facedir rotations.

The table was generated using the old code. A table lookup
should be faster than lots of math and branches.

Allows us to drop `nextrange()` as well.
This commit is contained in:
Auke Kok 2017-02-25 00:27:19 -08:00 committed by paramat
parent f14b0a6ff5
commit 297192dd39

@ -1,13 +1,5 @@
screwdriver = {} screwdriver = {}
local function nextrange(x, max)
x = x + 1
if x > max then
x = 0
end
return x
end
screwdriver.ROTATE_FACE = 1 screwdriver.ROTATE_FACE = 1
screwdriver.ROTATE_AXIS = 2 screwdriver.ROTATE_AXIS = 2
screwdriver.disallow = function(pos, node, user, mode, new_param2) screwdriver.disallow = function(pos, node, user, mode, new_param2)
@ -21,19 +13,27 @@ end
screwdriver.rotate = {} screwdriver.rotate = {}
screwdriver.rotate.facedir = function(node, mode) local facedir_tbl = {
-- Compute param2 [screwdriver.ROTATE_FACE] = {
local rotationPart = node.param2 % 32 -- get first 4 bits [0] = 1, [1] = 2, [2] = 3, [3] = 0,
local preservePart = node.param2 - rotationPart [4] = 5, [5] = 6, [6] = 7, [7] = 4,
local axisdir = math.floor(rotationPart / 4) [8] = 9, [9] = 10, [10] = 11, [11] = 8,
local rotation = rotationPart - axisdir * 4 [12] = 13, [13] = 14, [14] = 15, [15] = 12,
if mode == screwdriver.ROTATE_FACE then [16] = 17, [17] = 18, [18] = 19, [19] = 16,
rotationPart = axisdir * 4 + nextrange(rotation, 3) [20] = 21, [21] = 22, [22] = 23, [23] = 20,
elseif mode == screwdriver.ROTATE_AXIS then },
rotationPart = nextrange(axisdir, 5) * 4 [screwdriver.ROTATE_AXIS] = {
end [0] = 4, [1] = 4, [2] = 4, [3] = 4,
[4] = 8, [5] = 8, [6] = 8, [7] = 8,
[8] = 12, [9] = 12, [10] = 12, [11] = 12,
[12] = 16, [13] = 16, [14] = 16, [15] = 16,
[16] = 20, [17] = 20, [18] = 20, [19] = 20,
[20] = 0, [21] = 0, [22] = 0, [23] = 0,
},
}
return preservePart + rotationPart screwdriver.rotate.facedir = function(node, mode)
return facedir_tbl[mode][node.param2]
end end
local wallmounted_tbl = { local wallmounted_tbl = {