Zefram
2014-07-07 5e4a87b92599aa0fc9a56081209c930d08a2c3bd
commit | author | age
e748af 1 local max_charge = {50000, 200000, 650000}
Z 2 local power_usage_per_node = {200, 500, 800}
82cba9 3
be2f30 4 local S = technic.getter
S 5
82cba9 6 minetest.register_craft({
R 7     output = 'technic:mining_drill',
8     recipe = {
5e4a87 9         {'moreores:tin_ingot',            'technic:diamond_drill_head', 'moreores:tin_ingot'},
eac484 10         {'technic:stainless_steel_ingot', 'technic:motor',              'technic:stainless_steel_ingot'},
S 11         {'',                              'technic:red_energy_crystal', 'moreores:copper_ingot'},
82cba9 12     }
R 13 })
e23f87 14 minetest.register_craft({
R 15     output = 'technic:mining_drill_mk2',
16     recipe = {
eac484 17         {'technic:diamond_drill_head',    'technic:diamond_drill_head',   'technic:diamond_drill_head'},
S 18         {'technic:stainless_steel_ingot', 'technic:mining_drill',         'technic:stainless_steel_ingot'},
19         {'',                              'technic:green_energy_crystal', ''},
e23f87 20     }
R 21 })
22 minetest.register_craft({
23     output = 'technic:mining_drill_mk3',
24     recipe = {
eac484 25         {'technic:diamond_drill_head',    'technic:diamond_drill_head',  'technic:diamond_drill_head'},
S 26         {'technic:stainless_steel_ingot', 'technic:mining_drill_mk2',    'technic:stainless_steel_ingot'},
27         {'',                              'technic:blue_energy_crystal', ''},
e23f87 28     }
R 29 })
eac484 30 for i = 1, 4 do
ee0765 31     minetest.register_craft({
S 32         output = 'technic:mining_drill_mk3',
33         recipe = {
34             {'technic:diamond_drill_head',    'technic:diamond_drill_head',   'technic:diamond_drill_head'},
35             {'technic:stainless_steel_ingot', 'technic:mining_drill_mk2_'..i, 'technic:stainless_steel_ingot'},
36             {'',                              'technic:blue_energy_crystal',  ''},
37         }
38     })
91329b 39 end
82cba9 40
5cf765 41 local mining_drill_mode_text = {
S 42     {S("Single node.")},
43     {S("3 nodes deep.")},
44     {S("3 nodes wide.")},
45     {S("3 nodes tall.")},
46     {S("3x3 nodes.")},
47 }
48
49 local function drill_dig_it0 (pos,player)
bab851 50     if minetest.is_protected(pos, player:get_player_name()) then
S 51         minetest.record_protection_violation(pos, player:get_player_name())
52         return
53     end
5cf765 54     local node=minetest.env:get_node(pos)
S 55     if node.name == "air" or node.name == "ignore" then return end
56     if node.name == "default:lava_source" then return end
57     if node.name == "default:lava_flowing" then return end
58     if node.name == "default:water_source" then minetest.env:remove_node(pos) return end
59     if node.name == "default:water_flowing" then minetest.env:remove_node(pos) return end
60     minetest.node_dig(pos,node,player)
61 end
62
63 local function drill_dig_it1 (player)
64     local dir=player:get_look_dir()
65     if math.abs(dir.x)>math.abs(dir.z) then 
66         if dir.x>0 then return 0 end
67         return 1
68     end
69     if dir.z>0 then return 2 end
70     return 3
71 end
72
73 local function drill_dig_it2 (pos,player)
74     drill_dig_it0 (pos,player)
75     pos.z=pos.z+1
76     drill_dig_it0 (pos,player)
77     pos.z=pos.z-2
78     drill_dig_it0 (pos,player)
79     pos.z=pos.z+1
80     pos.y=pos.y+1
81     drill_dig_it0 (pos,player)
82     pos.z=pos.z+1
83     drill_dig_it0 (pos,player)
84     pos.z=pos.z-2
85     drill_dig_it0 (pos,player)
86     pos.z=pos.z+1
87     pos.y=pos.y-2
88     drill_dig_it0 (pos,player)
89     pos.z=pos.z+1
90     drill_dig_it0 (pos,player)
91     pos.z=pos.z-2
92     drill_dig_it0 (pos,player)
93 end
94
95 local function drill_dig_it3 (pos,player)
96     drill_dig_it0 (pos,player)
97     pos.x=pos.x+1
98     drill_dig_it0 (pos,player)
99     pos.x=pos.x-2
100     drill_dig_it0 (pos,player)
101     pos.x=pos.x+1
102     pos.y=pos.y+1
103     drill_dig_it0 (pos,player)
104     pos.x=pos.x+1
105     drill_dig_it0 (pos,player)
106     pos.x=pos.x-2
107     drill_dig_it0 (pos,player)
108     pos.x=pos.x+1
109     pos.y=pos.y-2
110     drill_dig_it0 (pos,player)
111     pos.x=pos.x+1
112     drill_dig_it0 (pos,player)
113     pos.x=pos.x-2
114     drill_dig_it0 (pos,player)
115 end
116
117 local function drill_dig_it4 (pos,player)
118     drill_dig_it0 (pos,player)
119     pos.x=pos.x+1
120     drill_dig_it0 (pos,player)
121     pos.x=pos.x-2
122     drill_dig_it0 (pos,player)
123     pos.x=pos.x+1
124     pos.z=pos.z+1
125     drill_dig_it0 (pos,player)
126     pos.x=pos.x+1
127     drill_dig_it0 (pos,player)
128     pos.x=pos.x-2
129     drill_dig_it0 (pos,player)
130     pos.x=pos.x+1
131     pos.z=pos.z-2
132     drill_dig_it0 (pos,player)
133     pos.x=pos.x+1
134     drill_dig_it0 (pos,player)
135     pos.x=pos.x-2
136     drill_dig_it0 (pos,player)
137 end
138
e748af 139 local function cost_to_use(drill_type, mode)
Z 140     local mult
141     if mode == 1 then
142         mult = 1
143     elseif mode <= 4 then
144         mult = 3
145     else
146         mult = 9
147     end
148     return power_usage_per_node[drill_type] * mult
149 end
5cf765 150
e748af 151 local function drill_dig_it(pos, player, mode)
eac484 152     if mode == 1 then
S 153         drill_dig_it0(pos, player)
e23f87 154     end
R 155     
eac484 156     if mode == 2 then -- 3 deep
S 157         dir = drill_dig_it1(player)
158         if dir == 0 then -- x+
159             drill_dig_it0(pos, player)
160             pos.x = pos.x + 1
161             drill_dig_it0(pos, player)
162             pos.x = pos.x + 1
163             drill_dig_it0(pos, player)
e23f87 164         end
eac484 165         if dir == 1 then  -- x-
S 166             drill_dig_it0(pos, player)
e23f87 167             pos.x=pos.x-1
R 168             drill_dig_it0 (pos,player)
169             pos.x=pos.x-1
170             drill_dig_it0 (pos,player)
171         end
172         if dir==2 then  -- z+
173             drill_dig_it0 (pos,player)
174             pos.z=pos.z+1
175             drill_dig_it0 (pos,player)
176             pos.z=pos.z+1
177             drill_dig_it0 (pos,player)
178         end
179         if dir==3 then  -- z-
180             drill_dig_it0 (pos,player)
8e2cb0 181             pos.z=pos.z-1
e23f87 182             drill_dig_it0 (pos,player)
8e2cb0 183             pos.z=pos.z-1
e23f87 184             drill_dig_it0 (pos,player)
R 185         end
186     end
187     
188     if mode==3 then -- 3 wide
189         dir=drill_dig_it1(player)
190         if dir==0 or dir==1 then -- x
191             drill_dig_it0 (pos,player)
192             pos.z=pos.z+1
193             drill_dig_it0 (pos,player)
194             pos.z=pos.z-2
195             drill_dig_it0 (pos,player)
196         end
197         if dir==2 or dir==3 then  -- z
198             drill_dig_it0 (pos,player)
199             pos.x=pos.x+1
200             drill_dig_it0 (pos,player)
201             pos.x=pos.x-2
202             drill_dig_it0 (pos,player)
203         end
204     end
205     
206     if mode==4 then -- 3 tall, selected in the middle
207         drill_dig_it0 (pos,player)
5cf765 208         pos.y=pos.y-1
e23f87 209         drill_dig_it0 (pos,player)
5cf765 210         pos.y=pos.y-1
e23f87 211         drill_dig_it0 (pos,player)
R 212     end
82cba9 213
e23f87 214     if mode==5 then -- 3 x 3
R 215         local dir=player:get_look_dir()
216         if math.abs(dir.y)<0.5 then
217             dir=drill_dig_it1(player)
218                 if dir==0 or dir==1 then -- x
219                     drill_dig_it2(pos,player)
220                 end
221                 if dir==2 or dir==3 then  -- z
222                     drill_dig_it3(pos,player)
223                 end
224         else
225         drill_dig_it4(pos,player)
226         end
227     end
228     
229     minetest.sound_play("mining_drill", {pos = pos, gain = 1.0, max_hear_distance = 10,})
230 end
82cba9 231
c39498 232 local function pos_is_pointable(pos)
Z 233     local node = minetest.env:get_node(pos)
234     local nodedef = minetest.registered_nodes[node.name]
235     return nodedef and nodedef.pointable
236 end
237
5cf765 238 local function mining_drill_mk2_setmode(user,itemstack)
S 239     local player_name=user:get_player_name()
240     local item=itemstack:to_table()
241     local meta=minetest.deserialize(item["metadata"])
242     if meta==nil then
243         meta={}
244         mode=0
e23f87 245     end
5cf765 246     if meta["mode"]==nil then
04c6f5 247         minetest.chat_send_player(player_name, S("Use while sneaking to change Mining Drill Mk%d modes."):format(2))
5cf765 248         meta["mode"]=0
S 249         mode=0
250     end
251     mode=(meta["mode"])
252     mode=mode+1
253     if mode>=5 then mode=1 end
254     minetest.chat_send_player(player_name, S("Mining Drill Mk%d Mode %d"):format(2, mode)..": "..mining_drill_mode_text[mode][1])
255     item["name"]="technic:mining_drill_mk2_"..mode
256     meta["mode"]=mode
257     item["metadata"]=minetest.serialize(meta)
258     itemstack:replace(item)
259     return itemstack
e23f87 260 end
R 261
5cf765 262 local function mining_drill_mk3_setmode(user,itemstack)
S 263     local player_name=user:get_player_name()
264     local item=itemstack:to_table()
265     local meta=minetest.deserialize(item["metadata"])
266     if meta==nil then
267         meta={}
268         mode=0
269     end
270     if meta["mode"]==nil then
04c6f5 271         minetest.chat_send_player(player_name, S("Use while sneaking to change Mining Drill Mk%d modes."):format(3))
5cf765 272         meta["mode"]=0
S 273         mode=0
274     end
275     mode=(meta["mode"])
276     mode=mode+1
277     if mode>=6 then mode=1 end
278     minetest.chat_send_player(player_name, S("Mining Drill Mk%d Mode %d"):format(3, mode)..": "..mining_drill_mode_text[mode][1])
279     item["name"]="technic:mining_drill_mk3_"..mode
280     meta["mode"]=mode
281     item["metadata"]=minetest.serialize(meta)
282     itemstack:replace(item)
283     return itemstack
e23f87 284 end
R 285
5cf765 286
S 287 local function mining_drill_mk2_handler(itemstack, user, pointed_thing)
288     local keys = user:get_player_control()
289     local player_name = user:get_player_name()
290     local meta = minetest.deserialize(itemstack:get_metadata())
291     if not meta or not meta.mode or keys.sneak then
292         return mining_drill_mk2_setmode(user, itemstack)
293     end
c39498 294     if pointed_thing.type ~= "node" or not pos_is_pointable(pointed_thing.under) or not meta.charge then
5cf765 295         return
S 296     end
e748af 297     local charge_to_take = cost_to_use(2, meta.mode)
Z 298     if meta.charge >= charge_to_take then
5cf765 299         local pos = minetest.get_pointed_thing_position(pointed_thing, above)
e748af 300         drill_dig_it(pos, user, meta.mode)
5cf765 301         meta.charge = meta.charge - charge_to_take
S 302         itemstack:set_metadata(minetest.serialize(meta))
e748af 303         technic.set_RE_wear(itemstack, meta.charge, max_charge[2])
5cf765 304     end
S 305     return itemstack
306 end
307
308 local function mining_drill_mk3_handler(itemstack, user, pointed_thing)
309     local keys = user:get_player_control()
310     local player_name = user:get_player_name()
311     local meta = minetest.deserialize(itemstack:get_metadata())
312     if not meta or not meta.mode or keys.sneak then
313         return mining_drill_mk3_setmode(user, itemstack)
314     end
c39498 315     if pointed_thing.type ~= "node" or not pos_is_pointable(pointed_thing.under) or not meta.charge then
5cf765 316         return
S 317     end
e748af 318     local charge_to_take = cost_to_use(3, meta.mode)
Z 319     if meta.charge >= charge_to_take then
5cf765 320         local pos = minetest.get_pointed_thing_position(pointed_thing, above)
e748af 321         drill_dig_it(pos, user, meta.mode)
5cf765 322         meta.charge = meta.charge - charge_to_take
S 323         itemstack:set_metadata(minetest.serialize(meta))
e748af 324         technic.set_RE_wear(itemstack, meta.charge, max_charge[3])
5cf765 325     end
S 326     return itemstack
e23f87 327 end
R 328
e748af 329 technic.register_power_tool("technic:mining_drill", max_charge[1])
5cf765 330
e23f87 331 minetest.register_tool("technic:mining_drill", {
f90915 332     description = S("Mining Drill Mk%d"):format(1),
e23f87 333     inventory_image = "technic_mining_drill.png",
R 334     stack_max = 1,
99fd5d 335     wear_represents = "technic_RE_charge",
00d7c9 336     on_refill = technic.refill_RE_charge,
e23f87 337     on_use = function(itemstack, user, pointed_thing)
c39498 338         if pointed_thing.type ~= "node" or not pos_is_pointable(pointed_thing.under) then
eac484 339             return itemstack
e23f87 340         end
5cf765 341         local meta = minetest.deserialize(itemstack:get_metadata())
eac484 342         if not meta or not meta.charge then
S 343             return
344         end
e748af 345         local charge_to_take = cost_to_use(1, 1)
Z 346         if meta.charge >= charge_to_take then
eac484 347             local pos = minetest.get_pointed_thing_position(pointed_thing, above)
e748af 348             drill_dig_it(pos, user, 1)
Z 349             meta.charge = meta.charge - charge_to_take
5cf765 350             itemstack:set_metadata(minetest.serialize(meta))
e748af 351             technic.set_RE_wear(itemstack, meta.charge, max_charge[1])
eac484 352         end
S 353         return itemstack
e23f87 354     end,
R 355 })
356
357 minetest.register_tool("technic:mining_drill_mk2", {
f90915 358     description = S("Mining Drill Mk%d"):format(2),
e23f87 359     inventory_image = "technic_mining_drill_mk2.png",
99fd5d 360     wear_represents = "technic_RE_charge",
00d7c9 361     on_refill = technic.refill_RE_charge,
e23f87 362     on_use = function(itemstack, user, pointed_thing)
eac484 363         mining_drill_mk2_handler(itemstack, user, pointed_thing)
S 364         return itemstack
e23f87 365     end,
R 366 })
ee0765 367
e748af 368 technic.register_power_tool("technic:mining_drill_mk2", max_charge[2])
e23f87 369
eac484 370 for i = 1, 4 do
e748af 371     technic.register_power_tool("technic:mining_drill_mk2_"..i, max_charge[2])
ee0765 372     minetest.register_tool("technic:mining_drill_mk2_"..i, {
be2f30 373         description = S("Mining Drill Mk%d Mode %d"):format(2, i),
ee0765 374         inventory_image = "technic_mining_drill_mk2.png^technic_tool_mode"..i..".png",
S 375         wield_image = "technic_mining_drill_mk2.png",
99fd5d 376         wear_represents = "technic_RE_charge",
00d7c9 377         on_refill = technic.refill_RE_charge,
ee0765 378         groups = {not_in_creative_inventory=1},
S 379         on_use = function(itemstack, user, pointed_thing)
380             mining_drill_mk2_handler(itemstack, user, pointed_thing)
381             return itemstack
382         end,
383     })
e23f87 384 end
R 385
386 minetest.register_tool("technic:mining_drill_mk3", {
f90915 387     description = S("Mining Drill Mk%d"):format(3),
e23f87 388     inventory_image = "technic_mining_drill_mk3.png",
99fd5d 389     wear_represents = "technic_RE_charge",
00d7c9 390     on_refill = technic.refill_RE_charge,
e23f87 391     on_use = function(itemstack, user, pointed_thing)
R 392     mining_drill_mk3_handler(itemstack,user,pointed_thing)
393     return itemstack
394     end,
395 })
ee0765 396
e748af 397 technic.register_power_tool("technic:mining_drill_mk3", max_charge[3])
e23f87 398
R 399 for i=1,5,1 do
e748af 400     technic.register_power_tool("technic:mining_drill_mk3_"..i, max_charge[3])
ee0765 401     minetest.register_tool("technic:mining_drill_mk3_"..i, {
be2f30 402         description = S("Mining Drill Mk%d Mode %d"):format(3, i),
ee0765 403         inventory_image = "technic_mining_drill_mk3.png^technic_tool_mode"..i..".png",
S 404         wield_image = "technic_mining_drill_mk3.png",
99fd5d 405         wear_represents = "technic_RE_charge",
00d7c9 406         on_refill = technic.refill_RE_charge,
ee0765 407         groups = {not_in_creative_inventory=1},
S 408         on_use = function(itemstack, user, pointed_thing)
409         mining_drill_mk3_handler(itemstack,user,pointed_thing)
410         return itemstack
411         end,
412     })
e23f87 413 end