init_old.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. local max = tonumber(hbhunger.max[name])
  39. if h_out ~= h then
  40. hbhunger.hunger_out[name] = h
  41. hb.change_hudbar(player, "satiation", h, max)
  42. end
  43. end
  44. hbhunger.get_hunger_raw = function(player)
  45. local inv = player:get_inventory()
  46. if not inv then return nil end
  47. local hgp = inv:get_stack("hunger", 1):get_count()
  48. if hgp == 0 then
  49. hgp = 21
  50. inv:set_stack("hunger", 1, ItemStack({name=":", count=hgp}))
  51. else
  52. hgp = hgp
  53. end
  54. return hgp-1
  55. end
  56. hbhunger.set_hunger_raw = function(player)
  57. local inv = player:get_inventory()
  58. local name = player:get_player_name()
  59. local value = hbhunger.hunger[name]
  60. if not inv or not value then return nil end
  61. if value > 30 then value = 30 end
  62. if value < 0 then value = 0 end
  63. inv:set_stack("hunger", 1, ItemStack({name=":", count=value+1}))
  64. return true
  65. end
  66. minetest.register_on_joinplayer(function(player)
  67. local name = player:get_player_name()
  68. local inv = player:get_inventory()
  69. inv:set_size("hunger",1)
  70. hbhunger.hunger[name] = hbhunger.get_hunger_raw(player)
  71. hbhunger.hunger_out[name] = hbhunger.hunger[name]
  72. hbhunger.exhaustion[name] = 0
  73. hbhunger.poisonings[name] = 0
  74. custom_hud(player)
  75. hbhunger.set_hunger_raw(player)
  76. end)
  77. minetest.register_on_respawnplayer(function(player)
  78. -- reset hunger (and save)
  79. local name = player:get_player_name()
  80. hbhunger.hunger[name] = 20
  81. hbhunger.set_hunger_raw(player)
  82. hbhunger.exhaustion[name] = 0
  83. end)
  84. local main_timer = 0
  85. local timer = 0
  86. local timer2 = 0
  87. minetest.register_globalstep(function(dtime)
  88. main_timer = main_timer + dtime
  89. timer = timer + dtime
  90. timer2 = timer2 + dtime
  91. if main_timer > hbhunger.HUD_TICK or timer > 4 or timer2 > hbhunger.HUNGER_TICK then
  92. if main_timer > hbhunger.HUD_TICK then main_timer = 0 end
  93. for _,player in ipairs(minetest.get_connected_players()) do
  94. local name = player:get_player_name()
  95. local h = tonumber(hbhunger.hunger[name])
  96. local hp = player:get_hp()
  97. if timer > 4 then
  98. -- heal player by 1 hp if not dead and satiation is > 15 (of 30)
  99. if h > 15 and hp > 0 and player:get_breath() > 0 then
  100. player:set_hp(hp+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.set_hunger_raw(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