resistance.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 damage resistance (as a multiplier).
  6. function hunger.get_damage_resistance(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_damage_resistance_") then
  13. total = total * v.resistance
  14. end
  15. end
  16. return total
  17. end
  18. return 1
  19. end
  20. -- Apply damage resistance to player.
  21. -- 'data' = {resistance=0.8, time=30}
  22. -- 'resistance' is a multiplier, 1 is 100%, no change. Values LESS than 1 increase resitance.
  23. function hunger.apply_damage_resistance(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_damage_resistance_" .. key
  33. local datname = "effect_data_damage_resistance_" .. key
  34. local already_boosted = false
  35. if tab[keyname] then
  36. already_boosted = true
  37. end
  38. -- Boost damage resistance, time-additive.
  39. -- This is a multiplier to regular punch/arrow damage.
  40. tab[keyname] = (tab[keyname] or 0) + 30
  41. tab[datname] = tab[datname] or data
  42. -- Don't stack 'minetest.after' chains.
  43. -- Also don't stack 'hp_max'.
  44. if already_boosted then
  45. return
  46. end
  47. tab[datname].hud = pref:hud_add({
  48. hud_elem_type = "image",
  49. scale = {x = -100, y = -100},
  50. alignment = {x = 1, y = 1},
  51. text = "dmg_boost_effect.png",
  52. z_index = -350,
  53. })
  54. minetest.chat_send_player(pname, "# Server: Damage resistance increased for " .. tab[keyname] .. " seconds.")
  55. hunger.time_damage_resistance(pname, key)
  56. end
  57. -- Private function!
  58. function hunger.time_damage_resistance(pname, key)
  59. local pref = minetest.get_player_by_name(pname)
  60. if not pref then
  61. return
  62. end
  63. local tab = hunger.players[pname]
  64. if not tab then
  65. return
  66. end
  67. local keyname = "effect_time_damage_resistance_" .. key
  68. local datname = "effect_data_damage_resistance_" .. key
  69. if tab[keyname] <= 0 then
  70. if pref:get_hp() > 0 then
  71. minetest.chat_send_player(pname, "# Server: Damage resistance expired.")
  72. end
  73. pref:hud_remove(tab[datname].hud)
  74. tab[keyname] = nil
  75. tab[datname] = nil
  76. return
  77. end
  78. -- Check again soon.
  79. tab[keyname] = tab[keyname] - 1
  80. minetest.after(1, hunger.time_damage_resistance, pname, key)
  81. end