hp_regen_boost.lua 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. -- Localize for performance.
  2. local vector_round = vector.round
  3. local math_floor = math.floor
  4. local math_min = math.min
  5. -- Return the player's current HP regen boost (as a multiplier)
  6. function hunger.get_hpgen_boost(pname)
  7. local tab = hunger.players[pname]
  8. if tab and tab.effect_data_hpgen_boost then
  9. -- Calc sum of all active modifiers.
  10. local total = 1
  11. for k, v in pairs(tab) do
  12. if k:find("^effect_data_hpgen_boost_") then
  13. total = total * v.regen
  14. end
  15. end
  16. return total
  17. end
  18. return 1
  19. end
  20. -- Apply a health boost to player.
  21. -- 'data' = {regen=2, time=60}
  22. -- 'regen' is a multiplier. 1 is 100%, no change.
  23. function hunger.apply_hpgen_boost(pname, key, data)
  24. local pref = minetest.get_player_by_name(pname)
  25. if not pref then
  26. return
  27. end
  28. local tab = hunger.players[pname]
  29. if not tab then
  30. return
  31. end
  32. local keyname = "effect_time_hpgen_boost_" .. key
  33. local datname = "effect_data_hpgen_boost_" .. key
  34. local already_boosted = false
  35. if tab[keyname] then
  36. already_boosted = true
  37. end
  38. -- Boost HP-regen for several health-regain-timer ticks, time-additive.
  39. tab[keyname] = (tab[keyname] or 0) + data.time
  40. tab[datname] = tab[datname] or data
  41. -- Don't stack 'minetest.after' chains.
  42. if already_boosted then
  43. return
  44. end
  45. tab[datname].hud = pref:hud_add({
  46. hud_elem_type = "image",
  47. scale = {x = -100, y = -100},
  48. alignment = {x = 1, y = 1},
  49. text = "hp_boost_effect.png",
  50. z_index = -350,
  51. })
  52. minetest.chat_send_player(pname, "# Server: Health regen rate boosted for " .. tab[keyname] .. " seconds.")
  53. hunger.time_hpgen_boost(pname, key)
  54. end
  55. -- Private function!
  56. function hunger.time_hpgen_boost(pname, key)
  57. local pref = minetest.get_player_by_name(pname)
  58. if not pref then
  59. return
  60. end
  61. local tab = hunger.players[pname]
  62. if not tab then
  63. return
  64. end
  65. local keyname = "effect_time_hpgen_boost_" .. key
  66. local datname = "effect_data_hpgen_boost_" .. key
  67. if tab[keyname] <= 0 then
  68. if pref:get_hp() > 0 then
  69. minetest.chat_send_player(pname, "# Server: HP regen boost expired.")
  70. end
  71. pref:hud_remove(tab[datname].hud)
  72. tab[keyname] = nil
  73. tab[datname] = nil
  74. return
  75. end
  76. -- Check again soon.
  77. tab[keyname] = tab[keyname] - 1
  78. minetest.after(1, hunger.time_hpgen_boost, pname, key)
  79. end