init.lua 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. hunger = {}
  2. hunger.players = {}
  3. hunger.food = {}
  4. HUNGER_TICK = 60*3 -- time in seconds after that 1 hunger point is taken
  5. HUNGER_HEALTH_TICK = 30 -- time in seconds after player gets healed/damaged
  6. HUNGER_MOVE_TICK = 5 -- time in seconds after the movement is checked
  7. HUNGER_EXHAUST_DIG = 6 -- exhaustion increased this value after digged node
  8. HUNGER_EXHAUST_PLACE = 3 -- exhaustion increased this value after placed
  9. HUNGER_EXHAUST_MOVE = 0.5 -- exhaustion increased this value if player movement detected
  10. HUNGER_EXHAUST_LVL = 160 -- at what exhaustion player saturation gets lowered
  11. HUNGER_HEAL = 1 -- number of HP player gets healed after HUNGER_HEALTH_TICK
  12. HUNGER_HEAL_LVL = 10 -- lower level of saturation needed to get healed
  13. HUNGER_STARVE = 1 -- number of HP player gets damaged by hunger after HUNGER_HEALTH_TICK
  14. HUNGER_STARVE_LVL = 3 -- level of staturation that causes starving
  15. HUNGER_MAX = 30 -- maximum level of saturation
  16. local modpath = minetest.get_modpath("hunger")
  17. dofile(modpath .. "/functions.lua")
  18. dofile(modpath .. "/food.lua")
  19. dofile(modpath .. "/legacy.lua")
  20. -- Callbacks
  21. -- initialization
  22. minetest.register_on_joinplayer(function(player)
  23. local inv = player:get_inventory()
  24. inv:set_size("hunger", 1)
  25. local name = player:get_player_name()
  26. hunger.players[name] = {}
  27. hunger.players[name].lvl = hunger.read(player)
  28. hunger.players[name].exhaus = 0
  29. local lvl = hunger.players[name].lvl
  30. if lvl > 30 then
  31. lvl = 30
  32. end
  33. minetest.after(0.8, function()
  34. hud.change_item(player, "hunger", {number = lvl, max = HUNGER_MAX})
  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. return true
  43. end)
  44. -- clean up
  45. minetest.register_on_leaveplayer(function(player, timeout)
  46. local pname = player:get_player_name()
  47. hunger.players[pname] = nil
  48. end)