Replace deprecated functions
[minetest_pep.git] / init.lua
blob1de223512495ab246e825a91fd23ce3aa8ea6cb8
1 local S = minetest.get_translator("pep")
2 local ppa = minetest.get_modpath("playerphysics")
4 pep = {}
5 function pep.register_potion(potiondef)
6 local on_use
7 if(potiondef.effect_type ~= nil) then
8 on_use = function(itemstack, user, pointed_thing)
9 playereffects.apply_effect_type(potiondef.effect_type, potiondef.duration, user)
10 itemstack:take_item()
11 return itemstack
12 end
13 else
14 on_use = function(itemstack, user, pointed_thing)
15 itemstack:take_item()
16 return itemstack
17 end
18 end
19 minetest.register_craftitem("pep:"..potiondef.basename, {
20 description = S("Glass Bottle (@1)", potiondef.contentstring),
21 _doc_items_longdesc = potiondef.longdesc,
22 _doc_items_usagehelp = S("Hold it in your hand, then left-click to drink it."),
23 inventory_image = "pep_"..potiondef.basename..".png",
24 wield_image = "pep_"..potiondef.basename..".png",
25 on_use = on_use,
27 end
29 pep.moles = {}
31 function pep.enable_mole_mode(playername)
32 pep.moles[playername] = true
33 end
35 function pep.disable_mole_mode(playername)
36 pep.moles[playername] = false
37 end
39 function pep.yaw_to_vector(yaw)
40 local tau = math.pi*2
42 yaw = yaw % tau
43 if yaw < tau/8 then
44 return { x=0, y=0, z=1}
45 elseif yaw < (3/8)*tau then
46 return { x=-1, y=0, z=0 }
47 elseif yaw < (5/8)*tau then
48 return { x=0, y=0, z=-1 }
49 elseif yaw < (7/8)*tau then
50 return { x=1, y=0, z=0 }
51 else
52 return { x=0, y=0, z=1}
53 end
54 end
56 function pep.moledig(playername)
57 local player = minetest.get_player_by_name(playername)
59 local yaw = player:get_look_horizontal()
61 local pos = vector.round(player:get_pos())
63 local v = pep.yaw_to_vector(yaw)
65 local digpos1 = vector.add(pos, v)
66 local digpos2 = { x = digpos1.x, y = digpos1.y+1, z = digpos1.z }
68 local try_dig = function(pos)
69 local n = minetest.get_node(pos)
70 local ndef = minetest.registered_nodes[n.name]
71 if ndef.walkable and ndef.diggable then
72 if ndef.can_dig ~= nil then
73 if ndef.can_dig() then
74 return true
75 else
76 return false
77 end
78 else
79 return true
80 end
81 else
82 return false
83 end
84 end
86 local dig = function(pos)
87 if try_dig(pos) then
88 local n = minetest.get_node(pos)
89 local ndef = minetest.registered_nodes[n.name]
90 if ndef.sounds ~= nil then
91 minetest.sound_play(ndef.sounds.dug, { pos = pos })
92 end
93 -- TODO: Replace this code as soon Minetest removes support for this function
94 local drops = minetest.get_node_drops(n.name, "default:pick_steel")
95 minetest.dig_node(pos)
96 local inv = player:get_inventory()
97 local leftovers = {}
98 for i=1,#drops do
99 table.insert(leftovers, inv:add_item("main", drops[i]))
101 for i=1,#leftovers do
102 minetest.add_item(pos, leftovers[i])
107 dig(digpos1)
108 dig(digpos2)
111 pep.timer = 0
113 minetest.register_globalstep(function(dtime)
114 pep.timer = pep.timer + dtime
115 if pep.timer > 0.5 then
116 for playername, is_mole in pairs(pep.moles) do
117 if is_mole then
118 pep.moledig(playername)
121 pep.timer = 0
123 end)
125 local add_physic = function(player, attribute, value)
126 if ppa then
127 playerphysics.add_physics_factor(player, attribute, "pep:"..attribute, value)
128 else
129 player:set_physics_override({[attribute]=value})
132 local remove_physic = function(player, attribute)
133 if ppa then
134 playerphysics.remove_physics_factor(player, attribute, "pep:"..attribute)
135 else
136 player:set_physics_override({[attribute]=1})
140 playereffects.register_effect_type("pepspeedplus", S("High speed"), "pep_speedplus.png", {"speed"},
141 function(player)
142 add_physic(player, "speed", 2)
143 end,
144 function(effect, player)
145 remove_physic(player, "speed")
148 playereffects.register_effect_type("pepspeedminus", S("Low speed"), "pep_speedminus.png", {"speed"},
149 function(player)
150 add_physic(player, "speed", 0.5)
151 end,
152 function(effect, player)
153 remove_physic(player, "speed")
156 playereffects.register_effect_type("pepspeedreset", S("Speed neutralizer"), "pep_speedreset.png", {"speed"},
157 function() end, function() end)
158 playereffects.register_effect_type("pepjumpplus", S("High jump"), "pep_jumpplus.png", {"jump"},
159 function(player)
160 add_physic(player, "jump", 2)
161 end,
162 function(effect, player)
163 remove_physic(player, "jump")
166 playereffects.register_effect_type("pepjumpminus", S("Low jump"), "pep_jumpminus.png", {"jump"},
167 function(player)
168 add_physic(player, "jump", 0.5)
169 end,
170 function(effect, player)
171 remove_physic(player, "jump")
174 playereffects.register_effect_type("pepjumpreset", S("Jump height neutralizer"), "pep_jumpreset.png", {"jump"},
175 function() end, function() end)
176 playereffects.register_effect_type("pepgrav0", S("No gravity"), "pep_grav0.png", {"gravity"},
177 function(player)
178 add_physic(player, "gravity", 0)
179 end,
180 function(effect, player)
181 remove_physic(player, "gravity")
184 playereffects.register_effect_type("pepgravreset", S("Gravity neutralizer"), "pep_gravreset.png", {"gravity"},
185 function() end, function() end)
186 playereffects.register_effect_type("pepregen", S("Regeneration"), "pep_regen.png", {"health"},
187 function(player)
188 player:set_hp(player:get_hp()+1)
189 end,
190 nil, nil, nil, 2
192 playereffects.register_effect_type("pepregen2", S("Strong regeneration"), "pep_regen2.png", {"health"},
193 function(player)
194 player:set_hp(player:get_hp()+2)
195 end,
196 nil, nil, nil, 1
199 if minetest.get_modpath("mana") ~= nil then
200 playereffects.register_effect_type("pepmanaregen", S("Weak mana boost"), "pep_manaregen.png", {"mana"},
201 function(player)
202 local name = player:get_player_name()
203 mana.setregen(name, mana.getregen(name) + 0.5)
204 end,
205 function(effect, player)
206 local name = player:get_player_name()
207 mana.setregen(name, mana.getregen(name) - 0.5)
210 playereffects.register_effect_type("pepmanaregen2", S("Strong mana boost"), "pep_manaregen2.png", {"mana"},
211 function(player)
212 local name = player:get_player_name()
213 mana.setregen(name, mana.getregen(name) + 1)
214 end,
215 function(effect, player)
216 local name = player:get_player_name()
217 mana.setregen(name, mana.getregen(name) - 1)
223 playereffects.register_effect_type("pepbreath", S("Perfect breath"), "pep_breath.png", {"breath"},
224 function(player)
225 player:set_breath(player:get_breath()+2)
226 end,
227 nil, nil, nil, 1
229 playereffects.register_effect_type("pepmole", S("Mole mode"), "pep_mole.png", {"autodig"},
230 function(player)
231 pep.enable_mole_mode(player:get_player_name())
232 end,
233 function(effect, player)
234 pep.disable_mole_mode(player:get_player_name())
237 playereffects.register_effect_type("pepglow", S("Glow"), "pep_glow.png", {"glow"},
238 function(player)
239 player:set_properties({glow=minetest.LIGHT_MAX})
240 end,
241 function(effect, player)
242 player:set_properties({glow=0})
246 pep.register_potion({
247 basename = "speedplus",
248 contentstring = S("Running Potion"),
249 longdesc = S("Drinking it will make you run faster for 30 seconds."),
250 effect_type = "pepspeedplus",
251 duration = 30,
253 pep.register_potion({
254 basename = "speedminus",
255 contentstring = S("Slug Potion"),
256 longdesc = S("Drinking it will make you walk slower for 30 seconds."),
257 effect_type = "pepspeedminus",
258 duration = 30,
260 pep.register_potion({
261 basename = "speedreset",
262 contentstring = S("Speed Neutralizer"),
263 longdesc = S("Drinking it will stop all speed effects you may currently have."),
264 effect_type = "pepspeedreset",
265 duration = 0
267 pep.register_potion({
268 basename = "breath",
269 contentstring = S("Air Potion"),
270 longdesc = S("Drinking it gives you breath underwater for 30 seconds."),
271 effect_type = "pepbreath",
272 duration = 30,
274 pep.register_potion({
275 basename = "regen",
276 contentstring = S("Weak Healing Potion"),
277 longdesc = S("Drinking it makes you regenerate health. Every 2 seconds, you get 1 HP, 10 times in total."),
278 effect_type = "pepregen",
279 duration = 10,
281 pep.register_potion({
282 basename = "regen2",
283 contentstring = S("Strong Healing Potion"),
284 longdesc = S("Drinking it makes you regenerate health quickly. Every second you get 2 HP, 10 times in total."),
285 effect_type = "pepregen2",
286 duration = 10,
288 pep.register_potion({
289 basename = "grav0",
290 contentstring = S("Non-Gravity Potion"),
291 longdesc = S("When you drink this potion, gravity stops affecting you, as if you were in space. The effect lasts for 20 seconds."),
292 effect_type = "pepgrav0",
293 duration = 20,
295 pep.register_potion({
296 basename = "gravreset",
297 contentstring = S("Gravity Neutralizer"),
298 longdesc = S("Drinking it will stop all gravity effects you currently have."),
299 effect_type = "pepgravreset",
300 duration = 0,
302 pep.register_potion({
303 basename = "jumpplus",
304 contentstring = S("High Jumping Potion"),
305 longdesc = S("Drinking it will make you jump higher for 30 seconds."),
306 effect_type = "pepjumpplus",
307 duration = 30,
309 pep.register_potion({
310 basename = "jumpminus",
311 contentstring = S("Low Jumping Potion"),
312 longdesc = S("Drinking it will make you jump lower for 30 seconds."),
313 effect_type = "pepjumpminus",
314 duration = 30,
316 pep.register_potion({
317 basename = "jumpreset",
318 contentstring = S("Jump Neutralizer"),
319 longdesc = S("Drinking it will stop all jumping effects you may currently have."),
320 effect_type = "pepjumpreset",
321 duration = 0,
323 pep.register_potion({
324 basename = "mole",
325 contentstring = S("Mole Potion"),
326 longdesc = S("Drinking it will start an effect which will magically attempt to mine any two blocks in front of you horizontally, as if you were using a steel pickaxe on them. The effect lasts for 18 seconds."),
327 effect_type = "pepmole",
328 duration = 18,
330 pep.register_potion({
331 basename = "glow",
332 contentstring = S("Glow Potion"),
333 longdesc = S("Drinking it will make you glow. The effect lasts for 60 seconds."),
334 effect_type = "pepglow",
335 duration = 60,
337 if(minetest.get_modpath("mana")~=nil) then
338 pep.register_potion({
339 basename = "manaregen",
340 contentstring = S("Weak Mana Potion"),
341 effect_type = "pepmanaregen",
342 duration = 10,
343 longdesc = S("Drinking it will increase your mana regeneration rate by 0.5 for 10 seconds."),
345 pep.register_potion({
346 basename = "manaregen2",
347 contentstring = S("Strong Mana Potion"),
348 effect_type = "pepmanaregen2",
349 duration = 10,
350 longdesc = S("Drinking it will increase your mana regeneration rate by 1 for 10 seconds."),
355 --[=[ register crafts ]=]
356 --[[ normal potions ]]
357 if(minetest.get_modpath("vessels")~=nil) then
358 if(minetest.get_modpath("default")~=nil) then
359 minetest.register_craft({
360 type = "shapeless",
361 output = "pep:breath",
362 recipe = { "default:papyrus", "default:papyrus", "default:papyrus", "default:papyrus",
363 "default:papyrus", "default:papyrus", "default:papyrus", "default:papyrus", "vessels:glass_bottle" }
365 minetest.register_craft({
366 type = "shapeless",
367 output = "pep:speedminus",
368 recipe = { "default:dry_grass_1", "default:ice", "vessels:glass_bottle" }
370 if(minetest.get_modpath("flowers") ~= nil) then
371 minetest.register_craft({
372 type = "shapeless",
373 output = "pep:jumpplus",
374 recipe = { "flowers:flower_tulip", "default:grass_1", "default:mese_crystal_fragment",
375 "default:mese_crystal_fragment", "vessels:glass_bottle" }
377 minetest.register_craft({
378 type = "shapeless",
379 output = "pep:poisoner",
380 recipe = { "flowers:mushroom_red", "flowers:mushroom_red", "flowers:mushroom_red", "vessels:glass_bottle" }
383 if(minetest.get_modpath("farming") ~= nil) then
384 minetest.register_craft({
385 type = "shapeless",
386 output = "pep:regen",
387 recipe = { "default:cactus", "farming:flour", "flowers:mushroom_brown", "vessels:glass_bottle" }
391 if(minetest.get_modpath("farming") ~= nil) then
392 minetest.register_craft({
393 type = "shapeless",
394 output = "pep:regen2",
395 recipe = { "default:gold_lump", "farming:flour", "pep:regen" }
397 if minetest.get_modpath("mana") ~= nil then
398 minetest.register_craft({
399 type = "shapeless",
400 output = "pep:manaregen",
401 recipe = { "default:dry_shrub", "default:dry_shrub", "farming:seed_cotton", "default:mese_crystal_fragment",
402 "vessels:glass_bottle" }
406 if minetest.get_modpath("mana") ~= nil then
407 minetest.register_craft({
408 type = "shapeless",
409 output = "pep:manaregen2",
410 recipe = { "default:dry_shrub", "default:dry_shrub", "default:dry_shrub", "default:dry_shrub", "default:junglesapling",
411 "default:acacia_sapling", "default:mese_crystal_fragment", "pep:manaregen" }
415 minetest.register_craft({
416 type = "shapeless",
417 output = "pep:jumpminus",
418 recipe = { "default:leaves", "default:jungleleaves", "default:iron_lump", "flowers:dandelion_yellow", "vessels:glass_bottle" }
420 minetest.register_craft({
421 type = "shapeless",
422 output = "pep:grav0",
423 recipe = { "default:mese_crystal", "vessels:glass_bottle" }
425 minetest.register_craft({
426 type = "shapeless",
427 output = "pep:mole",
428 recipe = { "default:pick_steel", "default:shovel_steel", "vessels:glass_bottle" },
430 minetest.register_craft({
431 type = "shapeless",
432 output = "pep:gravreset" ,
433 recipe = { "pep:grav0", "default:iron_lump" }
436 if(minetest.get_modpath("flowers") ~= nil) then
437 minetest.register_craft({
438 type = "shapeless",
439 output = "pep:speedplus",
440 recipe = { "default:pine_sapling", "default:cactus", "flowers:dandelion_yellow", "default:junglegrass", "vessels:glass_bottle" }
445 --[[ independent crafts ]]
447 minetest.register_craft({
448 type = "shapeless",
449 output = "pep:speedreset",
450 recipe = { "pep:speedplus", "pep:speedminus" }
452 minetest.register_craft({
453 type = "shapeless",
454 output = "pep:jumpreset",
455 recipe = { "pep:jumpplus", "pep:jumpminus" }