Version 1.4.0
[minetest_pep.git] / init.lua
blob2e377fd7dabe6b3aa2eb0b1d21d5f9b242e654f9
1 -- Boilerplate to support localized strings if intllib mod is installed.
2 local S
3 if minetest.get_modpath("intllib") then
4 S = intllib.Getter()
5 else
6 S = function(s) return s end
7 end
8 local ppa = minetest.get_modpath("playerphysics")
10 pep = {}
11 function pep.register_potion(potiondef)
12 local on_use
13 if(potiondef.effect_type ~= nil) then
14 on_use = function(itemstack, user, pointed_thing)
15 playereffects.apply_effect_type(potiondef.effect_type, potiondef.duration, user)
16 itemstack:take_item()
17 return itemstack
18 end
19 else
20 on_use = function(itemstack, user, pointed_thing)
21 itemstack:take_item()
22 return itemstack
23 end
24 end
25 minetest.register_craftitem("pep:"..potiondef.basename, {
26 description = string.format(S("Glass Bottle (%s)"), potiondef.contentstring),
27 _doc_items_longdesc = potiondef.longdesc,
28 _doc_items_usagehelp = S("Hold it in your hand, then left-click to drink it."),
29 inventory_image = "pep_"..potiondef.basename..".png",
30 wield_image = "pep_"..potiondef.basename..".png",
31 on_use = on_use,
33 end
35 pep.moles = {}
37 function pep.enable_mole_mode(playername)
38 pep.moles[playername] = true
39 end
41 function pep.disable_mole_mode(playername)
42 pep.moles[playername] = false
43 end
45 function pep.yaw_to_vector(yaw)
46 local tau = math.pi*2
48 yaw = yaw % tau
49 if yaw < tau/8 then
50 return { x=0, y=0, z=1}
51 elseif yaw < (3/8)*tau then
52 return { x=-1, y=0, z=0 }
53 elseif yaw < (5/8)*tau then
54 return { x=0, y=0, z=-1 }
55 elseif yaw < (7/8)*tau then
56 return { x=1, y=0, z=0 }
57 else
58 return { x=0, y=0, z=1}
59 end
60 end
62 function pep.moledig(playername)
63 local player = minetest.get_player_by_name(playername)
65 local yaw = player:get_look_yaw()
66 -- fix stupid oddity of Minetest, adding pi/2 to the actual player's look yaw...
67 -- TODO: Remove this code as soon as Minetest fixes this.
68 yaw = yaw - math.pi/2
70 local pos = vector.round(player:getpos())
72 local v = pep.yaw_to_vector(yaw)
74 local digpos1 = vector.add(pos, v)
75 local digpos2 = { x = digpos1.x, y = digpos1.y+1, z = digpos1.z }
77 local try_dig = function(pos)
78 local n = minetest.get_node(pos)
79 local ndef = minetest.registered_nodes[n.name]
80 if ndef.walkable and ndef.diggable then
81 if ndef.can_dig ~= nil then
82 if ndef.can_dig() then
83 return true
84 else
85 return false
86 end
87 else
88 return true
89 end
90 else
91 return false
92 end
93 end
95 local dig = function(pos)
96 if try_dig(pos) then
97 local n = minetest.get_node(pos)
98 local ndef = minetest.registered_nodes[n.name]
99 if ndef.sounds ~= nil then
100 minetest.sound_play(ndef.sounds.dug, { pos = pos })
102 -- TODO: Replace this code as soon Minetest removes support for this function
103 local drops = minetest.get_node_drops(n.name, "default:pick_steel")
104 minetest.dig_node(pos)
105 local inv = player:get_inventory()
106 local leftovers = {}
107 for i=1,#drops do
108 table.insert(leftovers, inv:add_item("main", drops[i]))
110 for i=1,#leftovers do
111 minetest.add_item(pos, leftovers[i])
116 dig(digpos1)
117 dig(digpos2)
120 pep.timer = 0
122 minetest.register_globalstep(function(dtime)
123 pep.timer = pep.timer + dtime
124 if pep.timer > 0.5 then
125 for playername, is_mole in pairs(pep.moles) do
126 if is_mole then
127 pep.moledig(playername)
130 pep.timer = 0
132 end)
134 local add_physic = function(player, attribute, value)
135 if ppa then
136 playerphysics.add_physics_factor(player, attribute, "pep:"..attribute, value)
137 else
138 player:set_physics_override({[attribute]=value})
141 local remove_physic = function(player, attribute)
142 if ppa then
143 playerphysics.remove_physics_factor(player, attribute, "pep:"..attribute)
144 else
145 player:set_physics_override({[attribute]=1})
149 playereffects.register_effect_type("pepspeedplus", S("High speed"), "pep_speedplus.png", {"speed"},
150 function(player)
151 add_physic(player, "speed", 2)
152 end,
153 function(effect, player)
154 remove_physic(player, "speed")
157 playereffects.register_effect_type("pepspeedminus", S("Low speed"), "pep_speedminus.png", {"speed"},
158 function(player)
159 add_physic(player, "speed", 0.5)
160 end,
161 function(effect, player)
162 remove_physic(player, "speed")
165 playereffects.register_effect_type("pepspeedreset", S("Speed neutralizer"), "pep_speedreset.png", {"speed"},
166 function() end, function() end)
167 playereffects.register_effect_type("pepjumpplus", S("High jump"), "pep_jumpplus.png", {"jump"},
168 function(player)
169 add_physic(player, "jump", 2)
170 end,
171 function(effect, player)
172 remove_physic(player, "jump")
175 playereffects.register_effect_type("pepjumpminus", S("Low jump"), "pep_jumpminus.png", {"jump"},
176 function(player)
177 add_physic(player, "jump", 0.5)
178 end,
179 function(effect, player)
180 remove_physic(player, "jump")
183 playereffects.register_effect_type("pepjumpreset", S("Jump height neutralizer"), "pep_jumpreset.png", {"jump"},
184 function() end, function() end)
185 playereffects.register_effect_type("pepgrav0", S("No gravity"), "pep_grav0.png", {"gravity"},
186 function(player)
187 add_physic(player, "gravity", 0)
188 end,
189 function(effect, player)
190 remove_physic(player, "gravity")
193 playereffects.register_effect_type("pepgravreset", S("Gravity neutralizer"), "pep_gravreset.png", {"gravity"},
194 function() end, function() end)
195 playereffects.register_effect_type("pepregen", S("Regeneration"), "pep_regen.png", {"health"},
196 function(player)
197 player:set_hp(player:get_hp()+1)
198 end,
199 nil, nil, nil, 2
201 playereffects.register_effect_type("pepregen2", S("Strong regeneration"), "pep_regen2.png", {"health"},
202 function(player)
203 player:set_hp(player:get_hp()+2)
204 end,
205 nil, nil, nil, 1
208 if minetest.get_modpath("mana") ~= nil then
209 playereffects.register_effect_type("pepmanaregen", S("Weak mana boost"), "pep_manaregen.png", {"mana"},
210 function(player)
211 local name = player:get_player_name()
212 mana.setregen(name, mana.getregen(name) + 0.5)
213 end,
214 function(effect, player)
215 local name = player:get_player_name()
216 mana.setregen(name, mana.getregen(name) - 0.5)
219 playereffects.register_effect_type("pepmanaregen2", S("Strong mana boost"), "pep_manaregen2.png", {"mana"},
220 function(player)
221 local name = player:get_player_name()
222 mana.setregen(name, mana.getregen(name) + 1)
223 end,
224 function(effect, player)
225 local name = player:get_player_name()
226 mana.setregen(name, mana.getregen(name) - 1)
232 playereffects.register_effect_type("pepbreath", S("Perfect breath"), "pep_breath.png", {"breath"},
233 function(player)
234 player:set_breath(player:get_breath()+2)
235 end,
236 nil, nil, nil, 1
238 playereffects.register_effect_type("pepmole", S("Mole mode"), "pep_mole.png", {"autodig"},
239 function(player)
240 pep.enable_mole_mode(player:get_player_name())
241 end,
242 function(effect, player)
243 pep.disable_mole_mode(player:get_player_name())
247 pep.register_potion({
248 basename = "speedplus",
249 contentstring = S("Running Potion"),
250 longdesc = S("Drinking it will make you run faster for 30 seconds."),
251 effect_type = "pepspeedplus",
252 duration = 30,
254 pep.register_potion({
255 basename = "speedminus",
256 contentstring = S("Slug Potion"),
257 longdesc = S("Drinking it will make you walk slower for 30 seconds."),
258 effect_type = "pepspeedminus",
259 duration = 30,
261 pep.register_potion({
262 basename = "speedreset",
263 contentstring = S("Speed Neutralizer"),
264 longdesc = S("Drinking it will stop all speed effects you may currently have."),
265 effect_type = "pepspeedreset",
266 duration = 0
268 pep.register_potion({
269 basename = "breath",
270 contentstring = S("Air Potion"),
271 longdesc = S("Drinking it gives you breath underwater for 30 seconds."),
272 effect_type = "pepbreath",
273 duration = 30,
275 pep.register_potion({
276 basename = "regen",
277 contentstring = S("Weak Healing Potion"),
278 longdesc = S("Drinking it makes you regenerate health. Every 2 seconds, you get 1 HP, 10 times in total."),
279 effect_type = "pepregen",
280 duration = 10,
282 pep.register_potion({
283 basename = "regen2",
284 contentstring = S("Strong Healing Potion"),
285 longdesc = S("Drinking it makes you regenerate health quickly. Every second you get 2 HP, 10 times in total."),
286 effect_type = "pepregen2",
287 duration = 10,
289 pep.register_potion({
290 basename = "grav0",
291 contentstring = S("Non-Gravity Potion"),
292 longdesc = S("When you drink this potion, gravity stops affecting you, as if you were in space. The effect lasts for 20 seconds."),
293 effect_type = "pepgrav0",
294 duration = 20,
296 pep.register_potion({
297 basename = "gravreset",
298 contentstring = S("Gravity Neutralizer"),
299 longdesc = S("Drinking it will stop all gravity effects you currently have."),
300 effect_type = "pepgravreset",
301 duration = 0,
303 pep.register_potion({
304 basename = "jumpplus",
305 contentstring = S("High Jumping Potion"),
306 longdesc = S("Drinking it will make you jump higher for 30 seconds."),
307 effect_type = "pepjumpplus",
308 duration = 30,
310 pep.register_potion({
311 basename = "jumpminus",
312 contentstring = S("Low Jumping Potion"),
313 longdesc = S("Drinking it will make you jump lower for 30 seconds."),
314 effect_type = "pepjumpminus",
315 duration = 30,
317 pep.register_potion({
318 basename = "jumpreset",
319 contentstring = S("Jump Neutralizer"),
320 longdesc = S("Drinking it will stop all jumping effects you may currently have."),
321 effect_type = "pepjumpreset",
322 duration = 0,
324 pep.register_potion({
325 basename = "mole",
326 contentstring = S("Mole Potion"),
327 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."),
328 effect_type = "pepmole",
329 duration = 18,
331 if(minetest.get_modpath("mana")~=nil) then
332 pep.register_potion({
333 basename = "manaregen",
334 contentstring = S("Weak Mana Potion"),
335 effect_type = "pepmanaregen",
336 duration = 10,
337 longdesc = S("Drinking it will increase your mana regeneration rate by 0.5 for 10 seconds."),
339 pep.register_potion({
340 basename = "manaregen2",
341 contentstring = S("Strong Mana Potion"),
342 effect_type = "pepmanaregen2",
343 duration = 10,
344 longdesc = S("Drinking it will increase your mana regeneration rate by 1 for 10 seconds."),
349 --[=[ register crafts ]=]
350 --[[ normal potions ]]
351 if(minetest.get_modpath("vessels")~=nil) then
352 if(minetest.get_modpath("default")~=nil) then
353 minetest.register_craft({
354 type = "shapeless",
355 output = "pep:breath",
356 recipe = { "default:papyrus", "default:papyrus", "default:papyrus", "default:papyrus",
357 "default:papyrus", "default:papyrus", "default:papyrus", "default:papyrus", "vessels:glass_bottle" }
359 minetest.register_craft({
360 type = "shapeless",
361 output = "pep:speedminus",
362 recipe = { "default:dry_grass_1", "default:ice", "vessels:glass_bottle" }
364 if(minetest.get_modpath("flowers") ~= nil) then
365 minetest.register_craft({
366 type = "shapeless",
367 output = "pep:jumpplus",
368 recipe = { "flowers:flower_tulip", "default:grass_1", "default:mese_crystal_fragment",
369 "default:mese_crystal_fragment", "vessels:glass_bottle" }
371 minetest.register_craft({
372 type = "shapeless",
373 output = "pep:poisoner",
374 recipe = { "flowers:mushroom_red", "flowers:mushroom_red", "flowers:mushroom_red", "vessels:glass_bottle" }
377 if(minetest.get_modpath("farming") ~= nil) then
378 minetest.register_craft({
379 type = "shapeless",
380 output = "pep:regen",
381 recipe = { "default:cactus", "farming:flour", "flowers:mushroom_brown", "vessels:glass_bottle" }
385 if(minetest.get_modpath("farming") ~= nil) then
386 minetest.register_craft({
387 type = "shapeless",
388 output = "pep:regen2",
389 recipe = { "default:gold_lump", "farming:flour", "pep:regen" }
391 if minetest.get_modpath("mana") ~= nil then
392 minetest.register_craft({
393 type = "shapeless",
394 output = "pep:manaregen",
395 recipe = { "default:dry_shrub", "default:dry_shrub", "farming:seed_cotton", "default:mese_crystal_fragment",
396 "vessels:glass_bottle" }
400 if minetest.get_modpath("mana") ~= nil then
401 minetest.register_craft({
402 type = "shapeless",
403 output = "pep:manaregen2",
404 recipe = { "default:dry_shrub", "default:dry_shrub", "default:dry_shrub", "default:dry_shrub", "default:junglesapling",
405 "default:acacia_sapling", "default:mese_crystal_fragment", "pep:manaregen" }
409 minetest.register_craft({
410 type = "shapeless",
411 output = "pep:jumpminus",
412 recipe = { "default:leaves", "default:jungleleaves", "default:iron_lump", "flowers:dandelion_yellow", "vessels:glass_bottle" }
414 minetest.register_craft({
415 type = "shapeless",
416 output = "pep:grav0",
417 recipe = { "default:mese_crystal", "vessels:glass_bottle" }
419 minetest.register_craft({
420 type = "shapeless",
421 output = "pep:mole",
422 recipe = { "default:pick_steel", "default:shovel_steel", "vessels:glass_bottle" },
424 minetest.register_craft({
425 type = "shapeless",
426 output = "pep:gravreset" ,
427 recipe = { "pep:grav0", "default:iron_lump" }
430 if(minetest.get_modpath("flowers") ~= nil) then
431 minetest.register_craft({
432 type = "shapeless",
433 output = "pep:speedplus",
434 recipe = { "default:pine_sapling", "default:cactus", "flowers:dandelion_yellow", "default:junglegrass", "vessels:glass_bottle" }
439 --[[ independent crafts ]]
441 minetest.register_craft({
442 type = "shapeless",
443 output = "pep:speedreset",
444 recipe = { "pep:speedplus", "pep:speedminus" }
446 minetest.register_craft({
447 type = "shapeless",
448 output = "pep:jumpreset",
449 recipe = { "pep:jumpplus", "pep:jumpminus" }