init.lua 4.9 KB

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