hot.lua 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. -- HOT = Heal Over Time
  2. -- Localize for performance.
  3. local vector_round = vector.round
  4. local math_floor = math.floor
  5. local math_min = math.min
  6. -- Apply a HOT modifier to player.
  7. -- HOT modifier shall have a name ('key') which keeps separate from other HOTs.
  8. -- A HoT with particular name can only have one active instance per player, so
  9. -- they don't stack, UNLESS they are different types.
  10. --
  11. -- 'data' = {heal=500, time=60}
  12. -- 'heal' is amount of HP to heal per second.
  13. function hunger.apply_hot(pname, key, data)
  14. local pref = minetest.get_player_by_name(pname)
  15. if not pref then
  16. return
  17. end
  18. local tab = hunger.players[pname]
  19. if not tab then
  20. return
  21. end
  22. local hotname = "effect_time_hot_" .. key
  23. local datname = "effect_data_hot_" .. key
  24. local already_boosted = false
  25. if tab[hotname] then
  26. already_boosted = true
  27. end
  28. -- HOT for several seconds, time-additive.
  29. tab[hotname] = (tab[hotname] or 0) + data.time
  30. tab[datname] = tab[datname] or data
  31. -- Don't stack 'minetest.after' chains.
  32. if already_boosted then
  33. return
  34. end
  35. hunger.time_hot(pname, key)
  36. end
  37. -- Private function!
  38. function hunger.time_hot(pname, key)
  39. local pref = minetest.get_player_by_name(pname)
  40. if not pref then
  41. return
  42. end
  43. local tab = hunger.players[pname]
  44. if not tab then
  45. return
  46. end
  47. local hotname = "effect_time_hot_" .. key
  48. local datname = "effect_data_hot_" .. key
  49. if tab[hotname] <= 0 then
  50. tab[hotname] = nil
  51. tab[datname] = nil
  52. return
  53. end
  54. -- Cancel if health reached full
  55. if pref:get_hp() == pova.get_active_modifier(pref, "properties").hp_max then
  56. tab[hotname] = nil
  57. tab[datname] = nil
  58. return
  59. end
  60. local data = tab[datname]
  61. pref:set_hp(pref:get_hp() + data.heal)
  62. -- Check again soon.
  63. tab[hotname] = tab[hotname] - 1
  64. minetest.after(1, hunger.time_hot, pname, key)
  65. end