Vanessa Dannenberg
2018-10-31 44cb8df048e09b64214f59db73a3fd23cfe12e77
commit | author | age
636b0f 1 local S = technic.getter
Z 2
871ded 3 technic.register_power_tool("technic:prospector", 300000)
636b0f 4
Z 5 local function get_metadata(toolstack)
6     local m = minetest.deserialize(toolstack:get_metadata())
7     if not m then m = {} end
8     if not m.charge then m.charge = 0 end
9     if not m.target then m.target = "" end
10     if not m.look_depth then m.look_depth = 7 end
11     if not m.look_radius then m.look_radius = 1 end
12     return m
13 end
14
15 minetest.register_tool("technic:prospector", {
16     description = S("Prospector"),
17     inventory_image = "technic_prospector.png",
18     wear_represents = "technic_RE_charge",
19     on_refill = technic.refill_RE_charge,
20     on_use = function(toolstack, user, pointed_thing)
21         if not user or not user:is_player() or user.is_fake_player then return end
22         if pointed_thing.type ~= "node" then return end
23         local toolmeta = get_metadata(toolstack)
24         local look_diameter = toolmeta.look_radius * 2 + 1
25         local charge_to_take = toolmeta.look_depth * (toolmeta.look_depth + 1) * look_diameter * look_diameter
26         if toolmeta.charge < charge_to_take then return end
27         if toolmeta.target == "" then
28             minetest.chat_send_player(user:get_player_name(), "Right-click to set target block type")
29             return
30         end
b8c902 31         if not technic.creative_mode then
M 32             toolmeta.charge = toolmeta.charge - charge_to_take
33             toolstack:set_metadata(minetest.serialize(toolmeta))
34             technic.set_RE_wear(toolstack, toolmeta.charge, technic.power_tools[toolstack:get_name()])
35         end
636b0f 36         local start_pos = pointed_thing.under
Z 37         local forward = minetest.facedir_to_dir(minetest.dir_to_facedir(user:get_look_dir(), true))
38         local right = forward.x ~= 0 and { x=0, y=1, z=0 } or (forward.y ~= 0 and { x=0, y=0, z=1 } or { x=1, y=0, z=0 })
39         local up = forward.x ~= 0 and { x=0, y=0, z=1 } or (forward.y ~= 0 and { x=1, y=0, z=0 } or { x=0, y=1, z=0 })
40         local base_pos = vector.add(start_pos, vector.multiply(vector.add(right, up), - toolmeta.look_radius))
41         local found = false
42         for f = 0, toolmeta.look_depth-1 do
43             for r = 0, look_diameter-1 do
44                 for u = 0, look_diameter-1 do
45                     if minetest.get_node(vector.add(vector.add(vector.add(base_pos, vector.multiply(forward, f)), vector.multiply(right, r)), vector.multiply(up, u))).name == toolmeta.target then found = true end
46                 end
47             end
48         end
49         if math.random() < 0.02 then found = not found end
50         minetest.chat_send_player(user:get_player_name(), minetest.registered_nodes[toolmeta.target].description.." is "..(found and "present" or "absent").." in "..look_diameter.."x"..look_diameter.."x"..toolmeta.look_depth.." region")
51         minetest.sound_play("technic_prospector_"..(found and "hit" or "miss"), { pos = vector.add(user:getpos(), { x = 0, y = 1, z = 0 }), gain = 1.0, max_hear_distance = 10 })
52         return toolstack
53     end,
54     on_place = function(toolstack, user, pointed_thing)
55         if not user or not user:is_player() or user.is_fake_player then return end
56         local toolmeta = get_metadata(toolstack)
57         local pointed
58         if pointed_thing.type == "node" then
59             local pname = minetest.get_node(pointed_thing.under).name
60             local pdef = minetest.registered_nodes[pname]
61             if pdef and (pdef.groups.not_in_creative_inventory or 0) == 0 and pname ~= toolmeta.target then
62                 pointed = pname
63             end
64         end
65         local look_diameter = toolmeta.look_radius * 2 + 1
66         minetest.show_formspec(user:get_player_name(), "technic:prospector_control",
67             "size[7,8.5]"..
68             "item_image[0,0;1,1;"..toolstack:get_name().."]"..
69             "label[1,0;"..minetest.formspec_escape(toolstack:get_definition().description).."]"..
70             (toolmeta.target ~= "" and
71                 "label[0,1.5;Current target:]"..
72                 "label[0,2;"..minetest.formspec_escape(minetest.registered_nodes[toolmeta.target].description).."]"..
73                 "item_image[0,2.5;1,1;"..toolmeta.target.."]" or
74                 "label[0,1.5;No target set]")..
75             (pointed and
76                 "label[3.5,1.5;May set new target:]"..
77                 "label[3.5,2;"..minetest.formspec_escape(minetest.registered_nodes[pointed].description).."]"..
78                 "item_image[3.5,2.5;1,1;"..pointed.."]"..
79                 "button_exit[3.5,3.65;2,0.5;target_"..pointed..";Set target]" or
80                 "label[3.5,1.5;No new target available]")..
81             "label[0,4.5;Region cross section:]"..
82             "label[0,5;"..look_diameter.."x"..look_diameter.."]"..
83             "label[3.5,4.5;Set region cross section:]"..
84             "button_exit[3.5,5.15;1,0.5;look_radius_0;1x1]"..
85             "button_exit[4.5,5.15;1,0.5;look_radius_1;3x3]"..
86             "button_exit[5.5,5.15;1,0.5;look_radius_3;7x7]"..
87             "label[0,6;Region depth:]"..
88             "label[0,6.5;"..toolmeta.look_depth.."]"..
89             "label[3.5,6;Set region depth:]"..
90             "button_exit[3.5,6.65;1,0.5;look_depth_7;7]"..
91             "button_exit[4.5,6.65;1,0.5;look_depth_14;14]"..
92             "button_exit[5.5,6.65;1,0.5;look_depth_21;21]"..
93             "label[0,7.5;Accuracy:]"..
94             "label[0,8;98%]")
95         return
96     end, 
97 })
98
99 minetest.register_on_player_receive_fields(function(user, formname, fields)
100         if formname ~= "technic:prospector_control" then return false end
101     if not user or not user:is_player() or user.is_fake_player then return end
102     local toolstack = user:get_wielded_item()
103     if toolstack:get_name() ~= "technic:prospector" then return true end
104     local toolmeta = get_metadata(toolstack)
105     for field, value in pairs(fields) do
106         if field:sub(1, 7) == "target_" then
107             toolmeta.target = field:sub(8)
108         end
109         if field:sub(1, 12) == "look_radius_" then
110             toolmeta.look_radius = field:sub(13)
111         end
112         if field:sub(1, 11) == "look_depth_" then
113             toolmeta.look_depth = field:sub(12)
114         end
115     end
116     toolstack:set_metadata(minetest.serialize(toolmeta))
117     user:set_wielded_item(toolstack)
118     return true
119 end)
120  
121 minetest.register_craft({
122     output = "technic:prospector",
123     recipe = {
124         {"moreores:pick_silver", "moreores:mithril_block", "pipeworks:teleport_tube_1"},
44cb8d 125         {"basic_materials:brass_ingot", "technic:control_logic_unit", "basic_materials:brass_ingot"},
871ded 126         {"", "technic:blue_energy_crystal", ""},
636b0f 127     }
Z 128 })