Added support for vitamin mod
[minetest_hbhunger/mttechnic_game.git] / hunger.lua
blobf14c2513797f0434cd90d9c82dc3ff10df39ac9f
1 -- Keep these for backwards compatibility
2 function hbhunger.save_hunger(player)
3 hbhunger.set_hunger_raw(player)
4 end
5 function hbhunger.load_hunger(player)
6 hbhunger.get_hunger_raw(player)
7 end
9 -- wrapper for minetest.item_eat (this way we make sure other mods can't break this one)
10 local org_eat = core.do_item_eat
11 core.do_item_eat = function(hp_change, replace_with_item, itemstack, user, pointed_thing)
12 local old_itemstack = itemstack
13 itemstack = hbhunger.eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
14 for _, callback in pairs(core.registered_on_item_eats) do
15 local result = callback(hp_change, replace_with_item, itemstack, user, pointed_thing, old_itemstack)
16 if result then
17 return result
18 end
19 end
20 return itemstack
21 end
23 -- food functions
24 local food = hbhunger.food
26 function hbhunger.register_food(name, hunger_change, replace_with_item, poisen, heal, sound)
27 food[name] = {}
28 food[name].saturation = hunger_change -- hunger points added
29 food[name].replace = replace_with_item -- what item is given back after eating
30 food[name].poisen = poisen -- time its poisening
31 food[name].healing = heal -- amount of HP
32 food[name].sound = sound -- special sound that is played when eating
33 end
35 function hbhunger.eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
36 local item = itemstack:get_name()
37 local def = food[item]
38 if not def then
39 def = {}
40 if type(hp_change) ~= "number" then
41 hp_change = 1
42 core.log("error", "Wrong on_use() definition for item '" .. item .. "'")
43 end
44 def.saturation = hp_change * 1.3
45 def.replace = replace_with_item
46 end
47 local func = hbhunger.item_eat(def.saturation, def.replace, def.poisen, def.healing, def.sound)
48 return func(itemstack, user, pointed_thing)
49 end
51 -- Poison player
52 local function poisenp(tick, time, time_left, player)
53 -- First check if player is still there
54 if not player:is_player() then
55 return
56 end
57 time_left = time_left + tick
58 if time_left < time then
59 minetest.after(tick, poisenp, tick, time, time_left, player)
60 else
61 hbhunger.poisonings[player:get_player_name()] = hbhunger.poisonings[player:get_player_name()] - 1
62 if hbhunger.poisonings[player:get_player_name()] <= 0 then
63 -- Reset HUD bar color
64 hb.change_hudbar(player, "health", nil, nil, "hudbars_icon_health.png", nil, "hudbars_bar_health.png")
65 end
66 end
67 if player:get_hp()-1 > 0 then
68 player:set_hp(player:get_hp()-1)
69 end
71 end
73 function hbhunger.item_eat(hunger_change, replace_with_item, poisen, heal, sound)
74 return function(itemstack, user, pointed_thing)
75 if itemstack:take_item() ~= nil and user ~= nil then
76 local name = user:get_player_name()
77 local h = tonumber(hbhunger.hunger[name])
78 local hp = user:get_hp()
79 minetest.sound_play({name = sound or "hbhunger_eat_generic", gain = 1}, {pos=user:getpos(), max_hear_distance = 16})
81 -- Saturation
82 if h < 30 and hunger_change then
83 h = h + hunger_change
84 if h > 30 then h = 30 end
85 hbhunger.hunger[name] = h
86 hbhunger.set_hunger_raw(user)
87 end
88 -- Healing
89 if hp < 20 and heal then
90 hp = hp + heal
91 if hp > 20 then hp = 20 end
92 user:set_hp(hp)
93 end
94 -- Poison
95 if poisen then
96 -- Set poison bar
97 hb.change_hudbar(user, "health", nil, nil, "hbhunger_icon_health_poison.png", nil, "hbhunger_bar_health_poison.png")
98 hbhunger.poisonings[name] = hbhunger.poisonings[name] + 1
99 poisenp(1, poisen, 0, user)
101 -- Calc vitamins
102 vitamins.eat(user, itemstack)
104 if itemstack:get_count() == 0 then
105 itemstack:add_item(replace_with_item)
106 else
107 local inv = user:get_inventory()
108 if inv:room_for_item("main", replace_with_item) then
109 inv:add_item("main", replace_with_item)
110 else
111 minetest.add_item(user:getpos(), replace_with_item)
115 return itemstack
119 if minetest.get_modpath("default") ~= nil then
120 hbhunger.register_food("default:apple", 2)
122 if minetest.get_modpath("flowers") ~= nil then
123 hbhunger.register_food("flowers:mushroom_brown", 1)
124 hbhunger.register_food("flowers:mushroom_red", 1, "", 3)
126 if minetest.get_modpath("farming") ~= nil then
127 hbhunger.register_food("farming:bread", 4)
130 if minetest.get_modpath("mobs") ~= nil then
131 if mobs.mod ~= nil and mobs.mod == "redo" then
132 hbhunger.register_food("mobs:cheese", 4)
133 hbhunger.register_food("mobs:meat", 8)
134 hbhunger.register_food("mobs:meat_raw", 4)
135 hbhunger.register_food("mobs:rat_cooked", 4)
136 hbhunger.register_food("mobs:honey", 2)
137 hbhunger.register_food("mobs:pork_raw", 3, "", 3)
138 hbhunger.register_food("mobs:pork_cooked", 8)
139 hbhunger.register_food("mobs:chicken_cooked", 6)
140 hbhunger.register_food("mobs:chicken_raw", 2, "", 3)
141 hbhunger.register_food("mobs:chicken_egg_fried", 2)
142 if minetest.get_modpath("bucket") then
143 hbhunger.register_food("mobs:bucket_milk", 3, "bucket:bucket_empty")
145 else
146 hbhunger.register_food("mobs:meat", 6)
147 hbhunger.register_food("mobs:meat_raw", 3)
148 hbhunger.register_food("mobs:rat_cooked", 5)
152 if minetest.get_modpath("moretrees") ~= nil then
153 hbhunger.register_food("moretrees:coconut_milk", 1)
154 hbhunger.register_food("moretrees:raw_coconut", 2)
155 hbhunger.register_food("moretrees:acorn_muffin", 3)
156 hbhunger.register_food("moretrees:spruce_nuts", 1)
157 hbhunger.register_food("moretrees:pine_nuts", 1)
158 hbhunger.register_food("moretrees:fir_nuts", 1)
161 if minetest.get_modpath("dwarves") ~= nil then
162 hbhunger.register_food("dwarves:beer", 2)
163 hbhunger.register_food("dwarves:apple_cider", 1)
164 hbhunger.register_food("dwarves:midus", 2)
165 hbhunger.register_food("dwarves:tequila", 2)
166 hbhunger.register_food("dwarves:tequila_with_lime", 2)
167 hbhunger.register_food("dwarves:sake", 2)
170 if minetest.get_modpath("animalmaterials") ~= nil then
171 hbhunger.register_food("animalmaterials:milk", 2)
172 hbhunger.register_food("animalmaterials:meat_raw", 3)
173 hbhunger.register_food("animalmaterials:meat_pork", 3)
174 hbhunger.register_food("animalmaterials:meat_beef", 3)
175 hbhunger.register_food("animalmaterials:meat_chicken", 3)
176 hbhunger.register_food("animalmaterials:meat_lamb", 3)
177 hbhunger.register_food("animalmaterials:meat_venison", 3)
178 hbhunger.register_food("animalmaterials:meat_undead", 3, "", 3)
179 hbhunger.register_food("animalmaterials:meat_toxic", 3, "", 5)
180 hbhunger.register_food("animalmaterials:meat_ostrich", 3)
181 hbhunger.register_food("animalmaterials:fish_bluewhite", 2)
182 hbhunger.register_food("animalmaterials:fish_clownfish", 2)
185 if minetest.get_modpath("fishing") ~= nil then
186 hbhunger.register_food("fishing:fish_raw", 2)
187 hbhunger.register_food("fishing:fish_cooked", 5)
188 hbhunger.register_food("fishing:sushi", 6)
189 hbhunger.register_food("fishing:shark", 4)
190 hbhunger.register_food("fishing:shark_cooked", 8)
191 hbhunger.register_food("fishing:pike", 4)
192 hbhunger.register_food("fishing:pike_cooked", 8)
195 if minetest.get_modpath("glooptest") ~= nil then
196 hbhunger.register_food("glooptest:kalite_lump", 1)
199 if minetest.get_modpath("bushes") ~= nil then
200 hbhunger.register_food("bushes:sugar", 1)
201 hbhunger.register_food("bushes:strawberry", 2)
202 hbhunger.register_food("bushes:berry_pie_raw", 3)
203 hbhunger.register_food("bushes:berry_pie_cooked", 4)
204 hbhunger.register_food("bushes:basket_pies", 15)
207 if minetest.get_modpath("bushes_classic") then
208 -- bushes_classic mod, as found in the plantlife modpack
209 local berries = {
210 "strawberry",
211 "blackberry",
212 "blueberry",
213 "raspberry",
214 "gooseberry",
215 "mixed_berry"}
216 for _, berry in ipairs(berries) do
217 if berry ~= "mixed_berry" then
218 hbhunger.register_food("bushes:"..berry, 1)
220 hbhunger.register_food("bushes:"..berry.."_pie_raw", 2)
221 hbhunger.register_food("bushes:"..berry.."_pie_cooked", 5)
222 hbhunger.register_food("bushes:basket_"..berry, 15)
226 if minetest.get_modpath("mushroom") ~= nil then
227 hbhunger.register_food("mushroom:brown", 1)
228 hbhunger.register_food("mushroom:red", 1, "", 3)
229 -- mushroom potions: red = strong poison, brown = light restorative
230 if minetest.get_modpath("vessels") then
231 hbhunger.register_food("mushroom:brown_essence", 1, "vessels:glass_bottle", nil, 4)
232 hbhunger.register_food("mushroom:poison", 1, "vessels:glass_bottle", 10)
236 if minetest.get_modpath("docfarming") ~= nil then
237 hbhunger.register_food("docfarming:carrot", 3)
238 hbhunger.register_food("docfarming:cucumber", 2)
239 hbhunger.register_food("docfarming:corn", 3)
240 hbhunger.register_food("docfarming:potato", 4)
241 hbhunger.register_food("docfarming:bakedpotato", 5)
242 hbhunger.register_food("docfarming:raspberry", 3)
245 if minetest.get_modpath("farming_plus") ~= nil then
246 hbhunger.register_food("farming_plus:carrot_item", 3)
247 hbhunger.register_food("farming_plus:banana", 2)
248 hbhunger.register_food("farming_plus:orange_item", 2)
249 hbhunger.register_food("farming:pumpkin_bread", 4)
250 hbhunger.register_food("farming_plus:strawberry_item", 2)
251 hbhunger.register_food("farming_plus:tomato_item", 2)
252 hbhunger.register_food("farming_plus:potato_item", 4)
253 hbhunger.register_food("farming_plus:rhubarb_item", 2)
256 if minetest.get_modpath("mtfoods") ~= nil then
257 hbhunger.register_food("mtfoods:dandelion_milk", 1)
258 hbhunger.register_food("mtfoods:sugar", 1)
259 hbhunger.register_food("mtfoods:short_bread", 4)
260 hbhunger.register_food("mtfoods:cream", 1)
261 hbhunger.register_food("mtfoods:chocolate", 2)
262 hbhunger.register_food("mtfoods:cupcake", 2)
263 hbhunger.register_food("mtfoods:strawberry_shortcake", 2)
264 hbhunger.register_food("mtfoods:cake", 3)
265 hbhunger.register_food("mtfoods:chocolate_cake", 3)
266 hbhunger.register_food("mtfoods:carrot_cake", 3)
267 hbhunger.register_food("mtfoods:pie_crust", 3)
268 hbhunger.register_food("mtfoods:apple_pie", 3)
269 hbhunger.register_food("mtfoods:rhubarb_pie", 2)
270 hbhunger.register_food("mtfoods:banana_pie", 3)
271 hbhunger.register_food("mtfoods:pumpkin_pie", 3)
272 hbhunger.register_food("mtfoods:cookies", 2)
273 hbhunger.register_food("mtfoods:mlt_burger", 5)
274 hbhunger.register_food("mtfoods:potato_slices", 2)
275 hbhunger.register_food("mtfoods:potato_chips", 3)
276 --mtfoods:medicine
277 hbhunger.register_food("mtfoods:casserole", 3)
278 hbhunger.register_food("mtfoods:glass_flute", 2)
279 hbhunger.register_food("mtfoods:orange_juice", 2)
280 hbhunger.register_food("mtfoods:apple_juice", 2)
281 hbhunger.register_food("mtfoods:apple_cider", 2)
282 hbhunger.register_food("mtfoods:cider_rack", 2)
285 if minetest.get_modpath("fruit") ~= nil then
286 hbhunger.register_food("fruit:apple", 2)
287 hbhunger.register_food("fruit:pear", 2)
288 hbhunger.register_food("fruit:bananna", 3)
289 hbhunger.register_food("fruit:orange", 2)
292 if minetest.get_modpath("mush45") ~= nil then
293 hbhunger.register_food("mush45:meal", 4)
296 if minetest.get_modpath("seaplants") ~= nil then
297 hbhunger.register_food("seaplants:kelpgreen", 1)
298 hbhunger.register_food("seaplants:kelpbrown", 1)
299 hbhunger.register_food("seaplants:seagrassgreen", 1)
300 hbhunger.register_food("seaplants:seagrassred", 1)
301 hbhunger.register_food("seaplants:seasaladmix", 6)
302 hbhunger.register_food("seaplants:kelpgreensalad", 1)
303 hbhunger.register_food("seaplants:kelpbrownsalad", 1)
304 hbhunger.register_food("seaplants:seagrassgreensalad", 1)
305 hbhunger.register_food("seaplants:seagrassgreensalad", 1)
308 if minetest.get_modpath("mobfcooking") ~= nil then
309 hbhunger.register_food("mobfcooking:cooked_pork", 6)
310 hbhunger.register_food("mobfcooking:cooked_ostrich", 6)
311 hbhunger.register_food("mobfcooking:cooked_beef", 6)
312 hbhunger.register_food("mobfcooking:cooked_chicken", 6)
313 hbhunger.register_food("mobfcooking:cooked_lamb", 6)
314 hbhunger.register_food("mobfcooking:cooked_venison", 6)
315 hbhunger.register_food("mobfcooking:cooked_fish", 6)
318 if minetest.get_modpath("creatures") ~= nil then
319 hbhunger.register_food("creatures:meat", 6)
320 hbhunger.register_food("creatures:flesh", 3)
321 hbhunger.register_food("creatures:rotten_flesh", 3, "", 3)
324 if minetest.get_modpath("ethereal") then
325 hbhunger.register_food("ethereal:strawberry", 1)
326 hbhunger.register_food("ethereal:banana", 4)
327 hbhunger.register_food("ethereal:pine_nuts", 1)
328 hbhunger.register_food("ethereal:bamboo_sprout", 0, "", 3)
329 hbhunger.register_food("ethereal:fern_tubers", 1)
330 hbhunger.register_food("ethereal:banana_bread", 7)
331 hbhunger.register_food("ethereal:mushroom_plant", 2)
332 hbhunger.register_food("ethereal:coconut_slice", 2)
333 hbhunger.register_food("ethereal:golden_apple", 4, "", nil, 10)
334 hbhunger.register_food("ethereal:wild_onion_plant", 2)
335 hbhunger.register_food("ethereal:mushroom_soup", 4, "ethereal:bowl")
336 hbhunger.register_food("ethereal:mushroom_soup_cooked", 6, "ethereal:bowl")
337 hbhunger.register_food("ethereal:hearty_stew", 6, "ethereal:bowl", 3)
338 hbhunger.register_food("ethereal:hearty_stew_cooked", 10, "ethereal:bowl")
339 if minetest.get_modpath("bucket") then
340 hbhunger.register_food("ethereal:bucket_cactus", 2, "bucket:bucket_empty")
342 hbhunger.register_food("ethereal:fish_raw", 2)
343 hbhunger.register_food("ethereal:fish_cooked", 5)
344 hbhunger.register_food("ethereal:seaweed", 1)
345 hbhunger.register_food("ethereal:yellowleaves", 1, "", nil, 1)
346 hbhunger.register_food("ethereal:sashimi", 4)
347 hbhunger.register_food("ethereal:orange", 2)
350 if minetest.get_modpath("farming") and farming.mod == "redo" then
351 hbhunger.register_food("farming:bread", 6)
352 hbhunger.register_food("farming:potato", 1)
353 hbhunger.register_food("farming:baked_potato", 6)
354 hbhunger.register_food("farming:cucumber", 4)
355 hbhunger.register_food("farming:tomato", 4)
356 hbhunger.register_food("farming:carrot", 3)
357 hbhunger.register_food("farming:carrot_gold", 6, "", nil, 8)
358 hbhunger.register_food("farming:corn", 3)
359 hbhunger.register_food("farming:corn_cob", 5)
360 hbhunger.register_food("farming:melon_slice", 2)
361 hbhunger.register_food("farming:pumpkin_slice", 1)
362 hbhunger.register_food("farming:pumpkin_bread", 9)
363 hbhunger.register_food("farming:coffee_cup", 2, "farming:drinking_cup")
364 hbhunger.register_food("farming:coffee_cup_hot", 3, "farming:drinking_cup", nil, 2)
365 hbhunger.register_food("farming:cookie", 2)
366 hbhunger.register_food("farming:chocolate_dark", 3)
367 hbhunger.register_food("farming:donut", 4)
368 hbhunger.register_food("farming:donut_chocolate", 6)
369 hbhunger.register_food("farming:donut_apple", 6)
370 hbhunger.register_food("farming:raspberries", 1)
371 hbhunger.register_food("farming:blueberries", 1)
372 hbhunger.register_food("farming:muffin_blueberry", 4)
373 if minetest.get_modpath("vessels") then
374 hbhunger.register_food("farming:smoothie_raspberry", 2, "vessels:drinking_glass")
376 hbhunger.register_food("farming:rhubarb", 1)
377 hbhunger.register_food("farming:rhubarb_pie", 6)
378 hbhunger.register_food("farming:beans", 1)
381 if minetest.get_modpath("kpgmobs") ~= nil then
382 hbhunger.register_food("kpgmobs:uley", 3)
383 hbhunger.register_food("kpgmobs:meat", 6)
384 hbhunger.register_food("kpgmobs:rat_cooked", 5)
385 hbhunger.register_food("kpgmobs:med_cooked", 4)
386 if minetest.get_modpath("bucket") then
387 hbhunger.register_food("kpgmobs:bucket_milk", 4, "bucket:bucket_empty")
391 if minetest.get_modpath("jkfarming") ~= nil then
392 hbhunger.register_food("jkfarming:carrot", 3)
393 hbhunger.register_food("jkfarming:corn", 3)
394 hbhunger.register_food("jkfarming:melon_part", 2)
395 hbhunger.register_food("jkfarming:cake", 3)
398 if minetest.get_modpath("jkanimals") ~= nil then
399 hbhunger.register_food("jkanimals:meat", 6)
402 if minetest.get_modpath("jkwine") ~= nil then
403 hbhunger.register_food("jkwine:grapes", 2)
404 hbhunger.register_food("jkwine:winebottle", 1)
407 if minetest.get_modpath("cooking") ~= nil then
408 hbhunger.register_food("cooking:meat_beef_cooked", 4)
409 hbhunger.register_food("cooking:fish_bluewhite_cooked", 3)
410 hbhunger.register_food("cooking:fish_clownfish_cooked", 1)
411 hbhunger.register_food("cooking:meat_chicken_cooked", 2)
412 hbhunger.register_food("cooking:meat_cooked", 2)
413 hbhunger.register_food("cooking:meat_pork_cooked", 3)
414 hbhunger.register_food("cooking:meat_toxic_cooked", -3)
415 hbhunger.register_food("cooking:meat_venison_cooked", 3)
416 hbhunger.register_food("cooking:meat_undead_cooked", 1)
419 -- ferns mod of plantlife_modpack
420 if minetest.get_modpath("ferns") ~= nil then
421 hbhunger.register_food("ferns:fiddlehead", 1, "", 1)
422 hbhunger.register_food("ferns:fiddlehead_roasted", 3)
423 hbhunger.register_food("ferns:ferntuber_roasted", 3)
424 hbhunger.register_food("ferns:horsetail_01", 1)
427 if minetest.get_modpath("pizza") ~= nil then
428 hbhunger.register_food("pizza:pizza", 30, "", nil, 30)
429 hbhunger.register_food("pizza:pizzaslice", 5, "", nil, 5)
432 if minetest.get_modpath("nssm") then
433 hbhunger.register_food("nssm:werewolf_leg", 3)
434 hbhunger.register_food("nssm:heron_leg", 2)
435 hbhunger.register_food("nssm:chichibios_heron_leg", 4)
436 hbhunger.register_food("nssm:crocodile_tail", 3)
437 hbhunger.register_food("nssm:duck_legs", 1)
438 hbhunger.register_food("nssm:ant_leg", 1)
439 hbhunger.register_food("nssm:spider_leg", 1)
440 hbhunger.register_food("nssm:tentacle", 2)
441 hbhunger.register_food("nssm:worm_flesh", 2, "", 2) -- poisonous
442 hbhunger.register_food("nssm:amphibian_heart", 1)
443 hbhunger.register_food("nssm:raw_scrausics_wing", 1)
444 -- superfoods
445 hbhunger.register_food("nssm:phoenix_nuggets", 20, "", nil, 20)
446 hbhunger.register_food("nssm:phoenix_tear", 20, "", nil, 20)
449 -- player-action based hunger changes
450 function hbhunger.handle_node_actions(pos, oldnode, player, ext)
451 -- is_fake_player comes from the pipeworks, we are not interested in those
452 if not player or not player:is_player() or player.is_fake_player == true then
453 return
455 local name = player:get_player_name()
456 local exhaus = hbhunger.exhaustion[name]
457 if exhaus == nil then return end
458 local new = hbhunger.EXHAUST_PLACE
459 -- placenode event
460 if not ext then
461 new = hbhunger.EXHAUST_DIG
463 -- assume its send by main timer when movement detected
464 if not pos and not oldnode then
465 new = hbhunger.EXHAUST_MOVE
467 exhaus = exhaus + new
468 if exhaus > hbhunger.EXHAUST_LVL then
469 exhaus = 0
470 local h = tonumber(hbhunger.hunger[name])
471 h = h - 1
472 if h < 0 then h = 0 end
473 hbhunger.hunger[name] = h
474 hbhunger.set_hunger_raw(player)
476 hbhunger.exhaustion[name] = exhaus
479 minetest.register_on_placenode(hbhunger.handle_node_actions)
480 minetest.register_on_dignode(hbhunger.handle_node_actions)