Skip to content

Commit 85e342c

Browse files
Sonic Screwdriver: Support every paramtype2 (#662)
1 parent 0ff7cb7 commit 85e342c

File tree

1 file changed

+71
-27
lines changed

1 file changed

+71
-27
lines changed

technic/tools/sonic_screwdriver.lua

Lines changed: 71 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@ technic.register_power_tool("technic:sonic_screwdriver", sonic_screwdriver_max_c
88
local ROTATE_FACE = 1
99
local ROTATE_AXIS = 2
1010

11-
local function nextrange(x, max)
12-
x = x + 1
13-
if x > max then
14-
x = 0
15-
end
16-
return x
17-
end
18-
1911
-- Handles rotation
2012
local function screwdriver_handler(itemstack, user, pointed_thing, mode)
2113
if pointed_thing.type ~= "node" then
@@ -31,16 +23,80 @@ local function screwdriver_handler(itemstack, user, pointed_thing, mode)
3123

3224
local node = minetest.get_node(pos)
3325
local ndef = minetest.registered_nodes[node.name]
34-
if not ndef or not ndef.paramtype2 == "facedir" or
35-
(ndef.drawtype == "nodebox" and
36-
not ndef.node_box.type == "fixed") or
37-
node.param2 == nil then
38-
return
39-
end
26+
27+
if not ndef then return end
28+
29+
local paramtype2 = ndef.paramtype2
4030

4131
-- contrary to the default screwdriver, do not check for can_dig, to allow rotating machines with CLU's in them
4232
-- this is consistent with the previous sonic screwdriver
4333

34+
-- Set param2
35+
local new_param2
36+
local param2 = node.param2
37+
38+
local dirs_per_axis = 4
39+
40+
local dir_components = {
41+
-- 2^5, 5 bits
42+
facedir = 32,
43+
colorfacedir = 32,
44+
colordegrotate = 32,
45+
46+
-- 2^2, 2 bits
47+
["4dir"] = 4, -- lua doesn't like it when vars start with a digit
48+
color4dir = 4,
49+
}
50+
51+
local dir_component = dir_components[paramtype2]
52+
53+
local floor = math.floor
54+
-- non-direction data is preserved whether it be color or otherwise
55+
if (paramtype2 == "facedir") or (paramtype2 == "colorfacedir") then
56+
local aux = floor(param2 / dir_component)
57+
local dir = param2 % dir_component
58+
59+
if mode == ROTATE_FACE then
60+
dir = (floor(param2 / dirs_per_axis)) * dirs_per_axis + ((dir + 1) % dirs_per_axis)
61+
elseif mode == ROTATE_AXIS then
62+
dir = ((floor(param2 / dirs_per_axis) + 1) * dirs_per_axis) % 24
63+
end
64+
65+
new_param2 = aux * dir_component + dir
66+
elseif (paramtype2 == "4dir") or (paramtype2 == "color4dir") then
67+
local aux = floor(param2 / dir_component)
68+
local dir = param2 % dir_component
69+
70+
if mode == ROTATE_FACE then
71+
dir = (dir + 1) % dirs_per_axis
72+
elseif mode == ROTATE_AXIS then
73+
dir = 0
74+
end
75+
76+
new_param2 = aux * dir_component + dir
77+
elseif (paramtype2 == "degrotate") then
78+
if mode == ROTATE_FACE then
79+
new_param2 = param2 + 1
80+
elseif mode == ROTATE_AXIS then
81+
new_param2 = param2 + 20
82+
end
83+
new_param2 = new_param2 % 240
84+
elseif (paramtype2 == "colordegrotate") then
85+
local aux = floor(param2 / dir_component)
86+
local rotation = param2 % dir_component
87+
88+
if mode == ROTATE_FACE then
89+
rotation = rotation + 1
90+
elseif mode == ROTATE_AXIS then
91+
rotation = rotation + 4
92+
end
93+
rotation = rotation % 24
94+
95+
new_param2 = aux * dir_component + rotation
96+
else
97+
return
98+
end
99+
44100
local meta = technic.get_stack_meta(itemstack)
45101
local charge = meta:get_int("technic:charge")
46102
if charge < 100 then
@@ -49,19 +105,7 @@ local function screwdriver_handler(itemstack, user, pointed_thing, mode)
49105

50106
minetest.sound_play("technic_sonic_screwdriver", {pos = pos, gain = 0.3, max_hear_distance = 10})
51107

52-
-- Set param2
53-
local rotationPart = node.param2 % 32 -- get first 4 bits
54-
local preservePart = node.param2 - rotationPart
55-
56-
local axisdir = math.floor(rotationPart / 4)
57-
local rotation = rotationPart - axisdir * 4
58-
if mode == ROTATE_FACE then
59-
rotationPart = axisdir * 4 + nextrange(rotation, 3)
60-
elseif mode == ROTATE_AXIS then
61-
rotationPart = nextrange(axisdir, 5) * 4
62-
end
63-
64-
node.param2 = preservePart + rotationPart
108+
node.param2 = new_param2
65109
minetest.swap_node(pos, node)
66110

67111
if not technic.creative_mode then

0 commit comments

Comments
 (0)