Gábriel
2024-07-07 6731db14e580ddccace186f5a8ac03dad0661e0c
commit | author | age
ee0765 1 local sonic_screwdriver_max_charge = 15000
be2f30 2
S 3 local S = technic.getter
4
ee0765 5 technic.register_power_tool("technic:sonic_screwdriver", sonic_screwdriver_max_charge)
e23f87 6
fe4372 7 -- screwdriver handler code reused from minetest/minetest_game screwdriver @a9ac480
T 8 local ROTATE_FACE = 1
9 local ROTATE_AXIS = 2
10
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
19 -- Handles rotation
20 local function screwdriver_handler(itemstack, user, pointed_thing, mode)
21     if pointed_thing.type ~= "node" then
22         return
23     end
24
25     local pos = pointed_thing.under
26
27     if minetest.is_protected(pos, user:get_player_name()) then
28         minetest.record_protection_violation(pos, user:get_player_name())
29         return
30     end
31
32     local node = minetest.get_node(pos)
33     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
40
41     -- contrary to the default screwdriver, do not check for can_dig, to allow rotating machines with CLU's in them
42     -- this is consistent with the previous sonic screwdriver
43
a08ba2 44     local meta = technic.get_stack_meta(itemstack)
C 45     local charge = meta:get_int("technic:charge")
46     if charge < 100 then
fe4372 47         return
T 48     end
49
50     minetest.sound_play("technic_sonic_screwdriver", {pos = pos, gain = 0.3, max_hear_distance = 10})
51
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
65     minetest.swap_node(pos, node)
66
b8c902 67     if not technic.creative_mode then
a08ba2 68         charge = charge - 100
C 69         meta:set_int("technic:charge", charge)
70         technic.set_RE_wear(itemstack, charge, sonic_screwdriver_max_charge)
fe4372 71     end
T 72
73     return itemstack
74 end
75
e23f87 76 minetest.register_tool("technic:sonic_screwdriver", {
fe4372 77     description = S("Sonic Screwdriver (left-click rotates face, right-click rotates axis)"),
eac484 78     inventory_image = "technic_sonic_screwdriver.png",
99fd5d 79     wear_represents = "technic_RE_charge",
00d7c9 80     on_refill = technic.refill_RE_charge,
eac484 81     on_use = function(itemstack, user, pointed_thing)
fe4372 82         screwdriver_handler(itemstack, user, pointed_thing, ROTATE_FACE)
eac484 83         return itemstack
fe4372 84     end,
T 85     on_place = function(itemstack, user, pointed_thing)
86         screwdriver_handler(itemstack, user, pointed_thing, ROTATE_AXIS)
87         return itemstack
88     end,
eac484 89 })
4f78a6 90
e23f87 91 minetest.register_craft({
R 92     output = "technic:sonic_screwdriver",
93     recipe = {
5e4a87 94         {"",                         "default:diamond",        ""},
Z 95         {"mesecons_materials:fiber", "technic:battery",        "mesecons_materials:fiber"},
96         {"mesecons_materials:fiber", "moreores:mithril_ingot", "mesecons_materials:fiber"}
f3d8b4 97     }
e23f87 98 })
f3d8b4 99