init.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. local S = minetest.get_translator("hbhunger")
  2. if minetest.settings:get_bool("enable_damage") then
  3. hbhunger = {}
  4. hbhunger.food = {}
  5. -- HUD statbar values
  6. hbhunger.hunger = {}
  7. hbhunger.hunger_out = {}
  8. -- Count number of poisonings a player has at once
  9. hbhunger.poisonings = {}
  10. -- HUD item ids
  11. local hunger_hud = {}
  12. hbhunger.HUD_TICK = 0.1
  13. --Some hunger settings
  14. hbhunger.exhaustion = {} -- Exhaustion is experimental!
  15. hbhunger.HUNGER_TICK = 800 -- time in seconds after that 1 hunger point is taken
  16. hbhunger.EXHAUST_DIG = 3 -- exhaustion increased this value after digged node
  17. hbhunger.EXHAUST_PLACE = 1 -- exhaustion increased this value after placed
  18. hbhunger.EXHAUST_MOVE = 0.3 -- exhaustion increased this value if player movement detected
  19. hbhunger.EXHAUST_LVL = 160 -- at what exhaustion player satiation gets lowerd
  20. --load custom settings
  21. local set = io.open(minetest.get_modpath("hbhunger").."/hbhunger.conf", "r")
  22. if set then
  23. dofile(minetest.get_modpath("hbhunger").."/hbhunger.conf")
  24. set:close()
  25. end
  26. local function custom_hud(player)
  27. hb.init_hudbar(player, "satiation", hbhunger.get_hunger_raw(player))
  28. end
  29. dofile(minetest.get_modpath("hbhunger").."/hunger.lua")
  30. -- register satiation hudbar
  31. hb.register_hudbar("satiation", 0xFFFFFF, S("Satiation"), { icon = "hbhunger_icon.png", bgicon = "hbhunger_bgicon.png", bar = "hbhunger_bar.png" }, 20, 30, false, nil, { format_value = "%.1f", format_max_value = "%d" })
  32. -- update hud elemtens if value has changed
  33. local function update_hud(player)
  34. local name = player:get_player_name()
  35. --hunger
  36. local h_out = tonumber(hbhunger.hunger_out[name])
  37. local h = tonumber(hbhunger.hunger[name])
  38. if h_out ~= h then
  39. hbhunger.hunger_out[name] = h
  40. hb.change_hudbar(player, "satiation", h)
  41. end
  42. end
  43. hbhunger.get_hunger_raw = function(player)
  44. local inv = player:get_inventory()
  45. if not inv then return nil end
  46. local hgp = inv:get_stack("hunger", 1):get_count()
  47. if hgp == 0 then
  48. hgp = 21
  49. inv:set_stack("hunger", 1, ItemStack({name=":", count=hgp}))
  50. else
  51. hgp = hgp
  52. end
  53. return hgp-1
  54. end
  55. hbhunger.set_hunger_raw = function(player)
  56. local inv = player:get_inventory()
  57. local name = player:get_player_name()
  58. local value = hbhunger.hunger[name]
  59. if not inv or not value then return nil end
  60. if value > 30 then value = 30 end
  61. if value < 0 then value = 0 end
  62. inv:set_stack("hunger", 1, ItemStack({name=":", count=value+1}))
  63. return true
  64. end
  65. minetest.register_on_joinplayer(function(player)
  66. local name = player:get_player_name()
  67. local inv = player:get_inventory()
  68. inv:set_size("hunger",1)
  69. hbhunger.hunger[name] = hbhunger.get_hunger_raw(player)
  70. hbhunger.hunger_out[name] = hbhunger.hunger[name]
  71. hbhunger.exhaustion[name] = 0
  72. hbhunger.poisonings[name] = 0
  73. custom_hud(player)
  74. hbhunger.set_hunger_raw(player)
  75. end)
  76. minetest.register_on_respawnplayer(function(player)
  77. -- reset hunger (and save)
  78. local name = player:get_player_name()
  79. hbhunger.hunger[name] = 20
  80. hbhunger.set_hunger_raw(player)
  81. hbhunger.exhaustion[name] = 0
  82. end)
  83. local main_timer = 0
  84. local timer = 0
  85. local timer2 = 0
  86. minetest.register_globalstep(function(dtime)
  87. main_timer = main_timer + dtime
  88. timer = timer + dtime
  89. timer2 = timer2 + dtime
  90. if main_timer > hbhunger.HUD_TICK or timer > 4 or timer2 > hbhunger.HUNGER_TICK then
  91. if main_timer > hbhunger.HUD_TICK then main_timer = 0 end
  92. for _,player in ipairs(minetest.get_connected_players()) do
  93. local name = player:get_player_name()
  94. local h = tonumber(hbhunger.hunger[name])
  95. local hp = player:get_hp()
  96. if timer > 4 then
  97. -- heal player by 1 hp if not dead and satiation is > 15 (of 30)
  98. if h > 15 and hp > 0 and player:get_breath() > 0 then
  99. player:set_hp(hp+1)
  100. -- or damage player by 1 hp if satiation is < 2 (of 30)
  101. elseif h <= 1 then
  102. if hp-1 >= 0 then player:set_hp(hp-1) end
  103. end
  104. end
  105. -- lower satiation by 1 point after xx seconds
  106. if timer2 > hbhunger.HUNGER_TICK then
  107. if h > 0 then
  108. h = h-1
  109. hbhunger.hunger[name] = h
  110. hbhunger.set_hunger_raw(player)
  111. end
  112. end
  113. -- update all hud elements
  114. update_hud(player)
  115. local controls = player:get_player_control()
  116. -- Determine if the player is walking
  117. if controls.up or controls.down or controls.left or controls.right then
  118. hbhunger.handle_node_actions(nil, nil, player)
  119. end
  120. end
  121. end
  122. if timer > 4 then timer = 0 end
  123. if timer2 > hbhunger.HUNGER_TICK then timer2 = 0 end
  124. end)
  125. end