sta_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_stamina_boost(pname)
  7. local tab = hunger.players[pname]
  8. if tab 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_stamina_boost_") then
  13. total = total * v.regen
  14. end
  15. end
  16. return total
  17. end
  18. return 1
  19. end
  20. -- Apply a stamina boost to player.
  21. -- 'data' = {regen=2, time=60}
  22. -- 'regen' is a multiplier. 1 is 100%, no change.
  23. function hunger.apply_stamina_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_stamina_boost_" .. key
  33. local datname = "effect_data_stamina_boost_" .. key
  34. local already_boosted = false
  35. if tab[keyname] then
  36. already_boosted = true
  37. end
  38. -- Boost stamina-regen for a time, time-additive.
  39. tab[keyname] = (tab[keyname] or 0) + 120
  40. tab[datname] = tab[datname] or data
  41. -- Don't stack 'minetest.after' chains.
  42. -- Also don't stack 'hp_max'.
  43. if already_boosted then
  44. return
  45. end
  46. tab[datname].hud = pref:hud_add({
  47. hud_elem_type = "image",
  48. scale = {x = -100, y = -100},
  49. alignment = {x = 1, y = 1},
  50. text = "sta_boost_effect.png",
  51. z_index = -350,
  52. })
  53. minetest.chat_send_player(pname, "# Server: Strength regen rate boosted for " .. tab[keyname] .. " seconds.")
  54. hunger.time_stamina_boost(pname, key)
  55. end
  56. -- Private function!
  57. function hunger.time_stamina_boost(pname, key)
  58. local pref = minetest.get_player_by_name(pname)
  59. if not pref then
  60. return
  61. end
  62. local tab = hunger.players[pname]
  63. if not tab then
  64. return
  65. end
  66. local keyname = "effect_time_stamina_boost_" .. key
  67. local datname = "effect_data_stamina_boost_" .. key
  68. if tab[keyname] <= 0 then
  69. if pref:get_hp() > 0 then
  70. minetest.chat_send_player(pname, "# Server: Strength regen boost expired.")
  71. end
  72. pref:hud_remove(tab[datname].hud)
  73. tab[keyname] = nil
  74. tab[datname] = nil
  75. return
  76. end
  77. -- Check again soon.
  78. tab[keyname] = tab[keyname] - 1
  79. minetest.after(1, hunger.time_stamina_boost, pname, key)
  80. end