Version 1.5.0
[minetest_pep.git] / init.lua
blob0b9a2c6afd598e2fb3223015c1cf5b6eaca4053d
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())
238 pep.register_potion({
239 basename = "speedplus",
240 contentstring = S("Running Potion"),
241 longdesc = S("Drinking it will make you run faster for 30 seconds."),
242 effect_type = "pepspeedplus",
243 duration = 30,
245 pep.register_potion({
246 basename = "speedminus",
247 contentstring = S("Slug Potion"),
248 longdesc = S("Drinking it will make you walk slower for 30 seconds."),
249 effect_type = "pepspeedminus",
250 duration = 30,
252 pep.register_potion({
253 basename = "speedreset",
254 contentstring = S("Speed Neutralizer"),
255 longdesc = S("Drinking it will stop all speed effects you may currently have."),
256 effect_type = "pepspeedreset",
257 duration = 0
259 pep.register_potion({
260 basename = "breath",
261 contentstring = S("Air Potion"),
262 longdesc = S("Drinking it gives you breath underwater for 30 seconds."),
263 effect_type = "pepbreath",
264 duration = 30,
266 pep.register_potion({
267 basename = "regen",
268 contentstring = S("Weak Healing Potion"),
269 longdesc = S("Drinking it makes you regenerate health. Every 2 seconds, you get 1 HP, 10 times in total."),
270 effect_type = "pepregen",
271 duration = 10,
273 pep.register_potion({
274 basename = "regen2",
275 contentstring = S("Strong Healing Potion"),
276 longdesc = S("Drinking it makes you regenerate health quickly. Every second you get 2 HP, 10 times in total."),
277 effect_type = "pepregen2",
278 duration = 10,
280 pep.register_potion({
281 basename = "grav0",
282 contentstring = S("Non-Gravity Potion"),
283 longdesc = S("When you drink this potion, gravity stops affecting you, as if you were in space. The effect lasts for 20 seconds."),
284 effect_type = "pepgrav0",
285 duration = 20,
287 pep.register_potion({
288 basename = "gravreset",
289 contentstring = S("Gravity Neutralizer"),
290 longdesc = S("Drinking it will stop all gravity effects you currently have."),
291 effect_type = "pepgravreset",
292 duration = 0,
294 pep.register_potion({
295 basename = "jumpplus",
296 contentstring = S("High Jumping Potion"),
297 longdesc = S("Drinking it will make you jump higher for 30 seconds."),
298 effect_type = "pepjumpplus",
299 duration = 30,
301 pep.register_potion({
302 basename = "jumpminus",
303 contentstring = S("Low Jumping Potion"),
304 longdesc = S("Drinking it will make you jump lower for 30 seconds."),
305 effect_type = "pepjumpminus",
306 duration = 30,
308 pep.register_potion({
309 basename = "jumpreset",
310 contentstring = S("Jump Neutralizer"),
311 longdesc = S("Drinking it will stop all jumping effects you may currently have."),
312 effect_type = "pepjumpreset",
313 duration = 0,
315 pep.register_potion({
316 basename = "mole",
317 contentstring = S("Mole Potion"),
318 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."),
319 effect_type = "pepmole",
320 duration = 18,
322 if(minetest.get_modpath("mana")~=nil) then
323 pep.register_potion({
324 basename = "manaregen",
325 contentstring = S("Weak Mana Potion"),
326 effect_type = "pepmanaregen",
327 duration = 10,
328 longdesc = S("Drinking it will increase your mana regeneration rate by 0.5 for 10 seconds."),
330 pep.register_potion({
331 basename = "manaregen2",
332 contentstring = S("Strong Mana Potion"),
333 effect_type = "pepmanaregen2",
334 duration = 10,
335 longdesc = S("Drinking it will increase your mana regeneration rate by 1 for 10 seconds."),
340 --[=[ register crafts ]=]
341 --[[ normal potions ]]
342 if(minetest.get_modpath("vessels")~=nil) then
343 if(minetest.get_modpath("default")~=nil) then
344 minetest.register_craft({
345 type = "shapeless",
346 output = "pep:breath",
347 recipe = { "default:coral_cyan", "default:coral_green", "default:coral_pink", "vessels:glass_bottle" }
349 minetest.register_craft({
350 type = "shapeless",
351 output = "pep:speedminus",
352 recipe = { "default:dry_grass_1", "default:ice", "vessels:glass_bottle" }
354 if(minetest.get_modpath("flowers") ~= nil) then
355 minetest.register_craft({
356 type = "shapeless",
357 output = "pep:jumpplus",
358 recipe = { "flowers:flower_tulip", "default:grass_1", "default:mese_crystal_fragment",
359 "default:mese_crystal_fragment", "vessels:glass_bottle" }
361 minetest.register_craft({
362 type = "shapeless",
363 output = "pep:poisoner",
364 recipe = { "flowers:mushroom_red", "flowers:mushroom_red", "flowers:mushroom_red", "vessels:glass_bottle" }
367 if(minetest.get_modpath("farming") ~= nil) then
368 minetest.register_craft({
369 type = "shapeless",
370 output = "pep:regen",
371 recipe = { "default:cactus", "farming:flour", "flowers:mushroom_brown", "vessels:glass_bottle" }
375 if(minetest.get_modpath("farming") ~= nil) then
376 minetest.register_craft({
377 type = "shapeless",
378 output = "pep:regen2",
379 recipe = { "default:gold_lump", "farming:flour", "pep:regen" }
381 if minetest.get_modpath("mana") ~= nil then
382 minetest.register_craft({
383 type = "shapeless",
384 output = "pep:manaregen",
385 recipe = { "default:dry_shrub", "default:dry_shrub", "farming:seed_cotton", "default:mese_crystal_fragment",
386 "vessels:glass_bottle" }
390 if minetest.get_modpath("mana") ~= nil then
391 minetest.register_craft({
392 type = "shapeless",
393 output = "pep:manaregen2",
394 recipe = { "default:dry_shrub", "default:dry_shrub", "default:dry_shrub", "default:dry_shrub", "default:junglesapling",
395 "default:marram_grass_1", "default:mese_crystal_fragment", "pep:manaregen" }
399 minetest.register_craft({
400 type = "shapeless",
401 output = "pep:jumpminus",
402 recipe = { "default:leaves", "default:jungleleaves", "default:iron_lump", "flowers:dandelion_yellow", "vessels:glass_bottle" }
404 minetest.register_craft({
405 type = "shapeless",
406 output = "pep:grav0",
407 recipe = { "default:mese_crystal", "vessels:glass_bottle" }
409 minetest.register_craft({
410 type = "shapeless",
411 output = "pep:mole",
412 recipe = { "default:pick_steel", "default:shovel_steel", "vessels:glass_bottle" },
414 minetest.register_craft({
415 type = "shapeless",
416 output = "pep:gravreset" ,
417 recipe = { "pep:grav0", "default:iron_lump" }
420 if(minetest.get_modpath("flowers") ~= nil) then
421 minetest.register_craft({
422 type = "shapeless",
423 output = "pep:speedplus",
424 recipe = { "default:pine_sapling", "default:cactus", "flowers:dandelion_yellow", "default:junglegrass", "vessels:glass_bottle" }
429 --[[ independent crafts ]]
431 minetest.register_craft({
432 type = "shapeless",
433 output = "pep:speedreset",
434 recipe = { "pep:speedplus", "pep:speedminus" }
436 minetest.register_craft({
437 type = "shapeless",
438 output = "pep:jumpreset",
439 recipe = { "pep:jumpplus", "pep:jumpminus" }