init.lua 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. hunger = {}
  2. hunger.food = {}
  3. HUNGER_TICK = 200 -- time in seconds after that 1 hunger point is taken
  4. HUNGER_HEALTH_TICK = 4 -- time in seconds after player gets healed/damaged
  5. HUNGER_MOVE_TICK = 0.5 -- time in seconds after the movement is checked
  6. HUNGER_EXHAUST_DIG = 3 -- exhaustion increased this value after digged node
  7. HUNGER_EXHAUST_PLACE = 1 -- exhaustion increased this value after placed
  8. HUNGER_EXHAUST_MOVE = 1.5 -- exhaustion increased this value if player movement detected
  9. HUNGER_EXHAUST_LVL = 160 -- at what exhaustion player saturation gets lowered
  10. HUNGER_HEAL = 1 -- number of HP player gets healed after HUNGER_HEALTH_TICK
  11. HUNGER_HEAL_LVL = 15 -- lower level of saturation needed to get healed
  12. HUNGER_STARVE = 1 -- number of HP player gets damaged by hunger after HUNGER_HEALTH_TICK
  13. HUNGER_STARVE_LVL = 3 -- level of staturation that causes starving
  14. HUNGER_MAX = 30 -- maximum level of saturation
  15. local modpath = minetest.get_modpath("hunger")
  16. dofile(modpath .. "/functions.lua")
  17. dofile(modpath .. "/food.lua")
  18. dofile(modpath .. "/legacy.lua")
  19. -- Callbacks
  20. if minetest.setting_getbool("enable_damage") then
  21. minetest.register_on_joinplayer(function(player)
  22. local inv = player:get_inventory()
  23. inv:set_size("hunger", 1)
  24. local name = player:get_player_name()
  25. hunger[name] = {}
  26. hunger[name].lvl = hunger.read(player)
  27. hunger[name].exhaus = 0
  28. local lvl = hunger[name].lvl
  29. if lvl > 20 then
  30. lvl = 20
  31. end
  32. minetest.after(0.8, function()
  33. hud.swap_statbar(player, "hunger", "air")
  34. hud.change_item(player, "hunger", {number = lvl, max = 20})
  35. end)
  36. end)
  37. -- for exhaustion
  38. minetest.register_on_placenode(hunger.handle_node_actions)
  39. minetest.register_on_dignode(hunger.handle_node_actions)
  40. minetest.register_on_respawnplayer(function(player)
  41. hunger.update_hunger(player, 20)
  42. end)
  43. end