init.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. local S = minetest.get_translator("hbarmor")
  2. local N = function(s) return s end
  3. if (not armor) or (not armor.def) then
  4. minetest.log("error", "[hbarmor] Outdated 3d_armor version. Please update your version of 3d_armor!")
  5. end
  6. local hbarmor = {}
  7. -- HUD statbar values
  8. hbarmor.armor = {}
  9. -- Stores if player's HUD bar has been initialized so far.
  10. hbarmor.player_active = {}
  11. -- Time difference in seconds between updates to the HUD armor bar.
  12. -- Increase this number for slow servers.
  13. hbarmor.tick = 1.0
  14. -- If true, the armor bar is hidden when the player does not wear any armor
  15. hbarmor.autohide = false
  16. --load custom settings
  17. local set = minetest.settings:get_bool("hbarmor_autohide")
  18. if set ~= nil then
  19. hbarmor.autohide = set
  20. end
  21. set = minetest.settings:get("hbarmor_tick")
  22. if tonumber(set) ~= nil then
  23. hbarmor.tick = tonumber(set)
  24. end
  25. local must_hide = function(playername, arm)
  26. return ((not armor.def[playername].count or armor.def[playername].count == 0) and arm == 0)
  27. end
  28. local arm_printable = function(arm)
  29. return math.ceil(math.floor(arm+0.5))
  30. end
  31. local function custom_hud(player)
  32. local name = player:get_player_name()
  33. if minetest.settings:get_bool("enable_damage") then
  34. local ret = hbarmor.get_armor(player)
  35. if ret == false then
  36. minetest.log("error", "[hbarmor] Call to hbarmor.get_armor in custom_hud returned with false!")
  37. end
  38. local arm = tonumber(hbarmor.armor[name])
  39. if not arm then arm = 0 end
  40. local hide
  41. if hbarmor.autohide then
  42. hide = must_hide(name, arm)
  43. else
  44. hide = false
  45. end
  46. hb.init_hudbar(player, "armor", arm_printable(arm), nil, hide)
  47. end
  48. end
  49. --register and define armor HUD bar
  50. hb.register_hudbar("armor", 0xFFFFFF, S("Armor"), { icon = "hbarmor_icon.png", bgicon = "hbarmor_bgicon.png", bar = "hbarmor_bar.png" }, 0, 100, hbarmor.autohide, N("@1: @2%"), { order = { "label", "value" }, textdomain = "hbarmor" } )
  51. function hbarmor.get_armor(player)
  52. if not player or not armor.def then
  53. return false
  54. end
  55. local name = player:get_player_name()
  56. local def = armor.def[name] or nil
  57. if def and def.state and def.count then
  58. hbarmor.set_armor(name, def.state, def.count)
  59. else
  60. return false
  61. end
  62. return true
  63. end
  64. function hbarmor.set_armor(player_name, ges_state, items)
  65. local max_items = 4
  66. if items == 5 then
  67. max_items = items
  68. end
  69. local max = max_items * 65535
  70. local lvl = max - ges_state
  71. lvl = lvl/max
  72. if ges_state == 0 and items == 0 then
  73. lvl = 0
  74. end
  75. hbarmor.armor[player_name] = math.max(0, math.min(lvl* (items * (100 / max_items)), 100))
  76. end
  77. -- update hud elemtens if value has changed
  78. local function update_hud(player)
  79. local name = player:get_player_name()
  80. --armor
  81. local arm = tonumber(hbarmor.armor[name])
  82. if not arm then
  83. arm = 0
  84. hbarmor.armor[name] = 0
  85. end
  86. if hbarmor.autohide then
  87. -- hide armor bar completely when there is none
  88. if must_hide(name, arm) then
  89. hb.hide_hudbar(player, "armor")
  90. else
  91. hb.change_hudbar(player, "armor", arm_printable(arm))
  92. hb.unhide_hudbar(player, "armor")
  93. end
  94. else
  95. hb.change_hudbar(player, "armor", arm_printable(arm))
  96. end
  97. end
  98. minetest.register_on_joinplayer(function(player)
  99. local name = player:get_player_name()
  100. custom_hud(player)
  101. hbarmor.player_active[name] = true
  102. end)
  103. minetest.register_on_leaveplayer(function(player)
  104. local name = player:get_player_name()
  105. hbarmor.player_active[name] = false
  106. end)
  107. local main_timer = 0
  108. local timer = 0
  109. minetest.register_globalstep(function(dtime)
  110. main_timer = main_timer + dtime
  111. timer = timer + dtime
  112. if main_timer > hbarmor.tick or timer > 4 then
  113. if minetest.settings:get_bool("enable_damage") then
  114. if main_timer > hbarmor.tick then main_timer = 0 end
  115. for _,player in ipairs(minetest.get_connected_players()) do
  116. local name = player:get_player_name()
  117. if hbarmor.player_active[name] == true then
  118. local ret = hbarmor.get_armor(player)
  119. if ret == false then
  120. minetest.log("error", "[hbarmor] Call to hbarmor.get_armor in globalstep returned with false!")
  121. end
  122. -- update all hud elements
  123. update_hud(player)
  124. end
  125. end
  126. end
  127. end
  128. if timer > 4 then timer = 0 end
  129. end)