init.lua 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. local S
  2. -- Intllib
  3. if minetest.get_translator ~= nil then
  4. S = minetest.get_translator("hudbars") -- 5.x translation function
  5. else
  6. if minetest.get_modpath("intllib") then
  7. dofile(minetest.get_modpath("intllib") .. "/init.lua")
  8. if intllib.make_gettext_pair then
  9. gettext, ngettext = intllib.make_gettext_pair() -- new gettext method
  10. else
  11. gettext = intllib.Getter() -- old text file method
  12. end
  13. S = gettext
  14. else -- boilerplate function
  15. S = function(str, ...)
  16. local args = {...}
  17. return str:gsub("@%d+", function(match)
  18. return args[tonumber(match:sub(2))]
  19. end)
  20. end
  21. end
  22. end
  23. local N = function(s) return s end
  24. hb = {}
  25. hb.hudtables = {}
  26. -- number of registered HUD bars
  27. hb.hudbars_count = 0
  28. -- table which records which HUD bar slots have been “registered” so far; used for automatic positioning
  29. hb.registered_slots = {}
  30. hb.settings = {}
  31. function hb.load_setting(sname, stype, defaultval, valid_values)
  32. local sval
  33. if stype == "string" then
  34. sval = minetest.settings:get(sname)
  35. elseif stype == "bool" then
  36. sval = minetest.settings:get_bool(sname)
  37. elseif stype == "number" then
  38. sval = tonumber(minetest.settings:get(sname))
  39. end
  40. if sval ~= nil then
  41. if valid_values ~= nil then
  42. local valid = false
  43. for i=1,#valid_values do
  44. if sval == valid_values[i] then
  45. valid = true
  46. end
  47. end
  48. if not valid then
  49. minetest.log("error", "[hudbars] Invalid value for "..sname.."! Using default value ("..tostring(defaultval)..").")
  50. return defaultval
  51. else
  52. return sval
  53. end
  54. else
  55. return sval
  56. end
  57. else
  58. return defaultval
  59. end
  60. end
  61. -- Load default settings
  62. dofile(minetest.get_modpath("hudbars").."/default_settings.lua")
  63. local function player_exists(player)
  64. return player ~= nil and player:is_player()
  65. end
  66. local function checksupportmax(player)
  67. local statusinfo = minetest.get_server_status()
  68. if string.find(statusinfo,"0.4.1") and not string.find(statusinfo,"0.4.18") and not string.find(statusinfo,"0.4.17.5") then
  69. hb.settings.hp_player_maximun = 20
  70. hb.settings.br_player_maximun = 10
  71. if player_exists(player) then
  72. player:set_properties({hp_max = 20})
  73. else
  74. minetest.log("error","[hudbars] WARNING! minetest version do not support customization of hp_max healt player values")
  75. end
  76. end
  77. end
  78. local function make_label(format_string, format_string_config, label, start_value, max_value)
  79. local params = {}
  80. local order = format_string_config.order
  81. for o=1, #order do
  82. if order[o] == "label" then
  83. table.insert(params, label)
  84. elseif order[o] == "value" then
  85. if format_string_config.format_value then
  86. table.insert(params, string.format(format_string_config.format_value, start_value))
  87. else
  88. table.insert(params, start_value)
  89. end
  90. elseif order[o] == "max_value" then
  91. if format_string_config.format_max_value then
  92. table.insert(params, string.format(format_string_config.format_max_value, max_value))
  93. else
  94. table.insert(params, max_value)
  95. end
  96. end
  97. end
  98. local ret
  99. if format_string_config.textdomain and minetest.translate ~= nil then
  100. ret = minetest.translate(format_string_config.textdomain, format_string, unpack(params))
  101. else
  102. ret = S(format_string, unpack(params))
  103. end
  104. return ret
  105. end
  106. -- Table which contains all players with active default HUD bars (only for internal use)
  107. hb.players = {}
  108. function hb.value_to_barlength(value, max)
  109. if max == 0 then
  110. return 0
  111. else
  112. if hb.settings.bar_type == "progress_bar" then
  113. local x
  114. if value < 0 then x=-0.5 else x = 0.5 end
  115. local ret = math.modf((value/max) * hb.settings.max_bar_length + x)
  116. return ret
  117. else
  118. local x
  119. if value < 0 then x=-0.5 else x = 0.5 end
  120. local ret = math.modf((value/max) * hb.settings.statbar_length + x)
  121. return ret
  122. end
  123. end
  124. end
  125. function hb.get_hudtable(identifier)
  126. return hb.hudtables[identifier]
  127. end
  128. function hb.get_hudbar_position_index(identifier)
  129. if hb.settings.sorting[identifier] ~= nil then
  130. return hb.settings.sorting[identifier]
  131. else
  132. local i = 0
  133. while true do
  134. if hb.registered_slots[i] ~= true and hb.settings.sorting_reverse[i] == nil then
  135. return i
  136. end
  137. i = i + 1
  138. end
  139. end
  140. end
  141. function hb.register_hudbar(identifier, text_color, label, textures, default_start_value, default_start_max, default_start_hidden, format_string, format_string_config)
  142. checksupportmax()
  143. minetest.log("action", "hb.register_hudbar: "..tostring(identifier))
  144. local hudtable = {}
  145. local pos, offset
  146. local index = math.floor(hb.get_hudbar_position_index(identifier))
  147. hb.registered_slots[index] = true
  148. if hb.settings.alignment_pattern == "stack_up" then
  149. pos = hb.settings.pos_left
  150. offset = {
  151. x = hb.settings.start_offset_left.x,
  152. y = hb.settings.start_offset_left.y - hb.settings.vmargin * index
  153. }
  154. elseif hb.settings.alignment_pattern == "stack_down" then
  155. pos = hb.settings.pos_left
  156. offset = {
  157. x = hb.settings.start_offset_left.x,
  158. y = hb.settings.start_offset_left.y + hb.settings.vmargin * index
  159. }
  160. else
  161. if index % 2 == 0 then
  162. pos = hb.settings.pos_left
  163. offset = {
  164. x = hb.settings.start_offset_left.x,
  165. y = hb.settings.start_offset_left.y - hb.settings.vmargin * (index/2)
  166. }
  167. else
  168. pos = hb.settings.pos_right
  169. offset = {
  170. x = hb.settings.start_offset_right.x,
  171. y = hb.settings.start_offset_right.y - hb.settings.vmargin * ((index-1)/2)
  172. }
  173. end
  174. end
  175. if format_string == nil then
  176. format_string = N("@1: @2/@3")
  177. end
  178. if format_string_config == nil then
  179. format_string_config = {}
  180. end
  181. if format_string_config.order == nil then
  182. format_string_config.order = { "label", "value", "max_value" }
  183. end
  184. if format_string_config.format_value == nil then
  185. format_string_config.format_value = "%02d"
  186. end
  187. if format_string_config.format_max_value == nil then
  188. format_string_config.format_max_value = "%02d"
  189. end
  190. hudtable.add_all = function(player, hudtable, start_value, start_max, start_hidden)
  191. if start_value == nil then start_value = hudtable.default_start_value end
  192. if start_max == nil then start_max = hudtable.default_start_max end
  193. if start_hidden == nil then start_hidden = hudtable.default_start_hidden end
  194. local ids = {}
  195. local state = {}
  196. local name = player:get_player_name()
  197. local bgscale, iconscale, text, barnumber, bgiconnumber
  198. if start_max == 0 or start_hidden then
  199. bgscale = { x=0, y=0 }
  200. else
  201. bgscale = { x=1, y=1 }
  202. end
  203. if start_hidden then
  204. iconscale = { x=0, y=0 }
  205. barnumber = 0
  206. bgiconnumber = 0
  207. text = ""
  208. else
  209. iconscale = { x=1, y=1 }
  210. barnumber = hb.value_to_barlength(start_value, start_max)
  211. bgiconnumber = hb.settings.statbar_length
  212. text = make_label(format_string, format_string_config, label, start_value, start_max)
  213. end
  214. if hb.settings.bar_type == "progress_bar" then
  215. ids.bg = player:hud_add({
  216. hud_elem_type = "image",
  217. position = pos,
  218. scale = bgscale,
  219. text = "hudbars_bar_background.png",
  220. alignment = {x=1,y=1},
  221. offset = { x = offset.x - 1, y = offset.y - 1 },
  222. z_index = 0,
  223. })
  224. if textures.icon ~= nil then
  225. ids.icon = player:hud_add({
  226. hud_elem_type = "image",
  227. position = pos,
  228. scale = iconscale,
  229. text = textures.icon,
  230. alignment = {x=-1,y=1},
  231. offset = { x = offset.x - 3, y = offset.y },
  232. z_index = 1,
  233. })
  234. end
  235. elseif hb.settings.bar_type == "statbar_modern" then
  236. if textures.bgicon ~= nil then
  237. ids.bg = player:hud_add({
  238. hud_elem_type = "statbar",
  239. position = pos,
  240. text = textures.bgicon,
  241. number = bgiconnumber,
  242. alignment = {x=-1,y=-1},
  243. offset = { x = offset.x, y = offset.y },
  244. direction = 0,
  245. size = {x=24, y=24},
  246. z_index = 0,
  247. })
  248. end
  249. end
  250. local bar_image, bgicon, bar_size
  251. if hb.settings.bar_type == "progress_bar" then
  252. bar_image = textures.bar
  253. -- NOTE: Intentionally set to nil. For some reason, on some systems,
  254. -- the progress bar is displaced when the bar_size is set explicitly here.
  255. -- On the other hand, setting this to nil is deprecated in MT 5.0.0 due to
  256. -- a debug log warning, but nothing is explained in lua_api.txt.
  257. -- This section is a potential bug magnet, please watch with care!
  258. -- The size of the bar image is expected to be exactly 2×16 pixels.
  259. bar_size = nil
  260. elseif hb.settings.bar_type == "statbar_classic" or hb.settings.bar_type == "statbar_modern" then
  261. bar_image = textures.icon
  262. if textures.bgicon then bgicon = textures.bgicon end
  263. bar_size = {x=24, y=24}
  264. end
  265. ids.bar = player:hud_add({
  266. hud_elem_type = "statbar",
  267. position = pos,
  268. text = bar_image,
  269. text2 = bgicon,
  270. number = barnumber,
  271. item = bgiconnumber,
  272. alignment = {x=-1,y=-1},
  273. offset = offset,
  274. direction = 0,
  275. size = bar_size,
  276. z_index = 1,
  277. })
  278. if hb.settings.bar_type == "progress_bar" then
  279. ids.text = player:hud_add({
  280. hud_elem_type = "text",
  281. position = pos,
  282. text = text,
  283. alignment = {x=1,y=1},
  284. number = text_color,
  285. direction = 0,
  286. offset = { x = offset.x + 2, y = offset.y - 1},
  287. z_index = 2,
  288. })
  289. end
  290. -- Do not forget to update hb.get_hudbar_state if you add new fields to the state table
  291. state.hidden = start_hidden
  292. state.value = start_value
  293. state.max = start_max
  294. state.text = text
  295. state.barlength = hb.value_to_barlength(start_value, start_max)
  296. local main_error_text =
  297. "[hudbars] Bad initial values of HUD bar identifier “"..tostring(identifier).."” for player "..name..". "
  298. if start_max < start_value then
  299. minetest.log("error", main_error_text.."start_max ("..start_max..") is smaller than start_value ("..start_value..")!")
  300. end
  301. if start_max < 0 then
  302. minetest.log("error", main_error_text.."start_max ("..start_max..") is smaller than 0!")
  303. end
  304. if start_value < 0 then
  305. minetest.log("error", main_error_text.."start_value ("..start_value..") is smaller than 0!")
  306. end
  307. hb.hudtables[identifier].hudids[name] = ids
  308. hb.hudtables[identifier].hudstate[name] = state
  309. end
  310. hudtable.identifier = identifier
  311. hudtable.format_string = format_string
  312. hudtable.format_string_config = format_string_config
  313. hudtable.label = label
  314. hudtable.hudids = {}
  315. hudtable.hudstate = {}
  316. hudtable.default_start_hidden = default_start_hidden
  317. hudtable.default_start_value = default_start_value
  318. hudtable.default_start_max = default_start_max
  319. hb.hudbars_count= hb.hudbars_count + 1
  320. hb.hudtables[identifier] = hudtable
  321. end
  322. function hb.init_hudbar(player, identifier, start_value, start_max, start_hidden)
  323. checksupportmax(player)
  324. if not player_exists(player) then return false end
  325. local hudtable = hb.get_hudtable(identifier)
  326. hb.hudtables[identifier].add_all(player, hudtable, start_value, start_max, start_hidden)
  327. return true
  328. end
  329. function hb.change_hudbar(player, identifier, new_value, new_max_value, new_icon, new_bgicon, new_bar, new_label, new_text_color)
  330. if new_value == nil and new_max_value == nil and new_icon == nil and new_bgicon == nil and new_bar == nil and new_label == nil and new_text_color == nil then
  331. return true
  332. end
  333. if not player_exists(player) then
  334. return false
  335. end
  336. local name = player:get_player_name()
  337. local hudtable = hb.get_hudtable(identifier)
  338. if not hudtable.hudstate[name] then
  339. return false
  340. end
  341. local value_changed, max_changed = false, false
  342. if new_value ~= nil then
  343. if new_value ~= hudtable.hudstate[name].value then
  344. hudtable.hudstate[name].value = new_value
  345. value_changed = true
  346. end
  347. else
  348. new_value = hudtable.hudstate[name].value
  349. end
  350. if new_max_value ~= nil then
  351. if new_max_value ~= hudtable.hudstate[name].max then
  352. hudtable.hudstate[name].max = new_max_value
  353. max_changed = true
  354. end
  355. else
  356. new_max_value = hudtable.hudstate[name].max
  357. end
  358. if hb.settings.bar_type == "progress_bar" then
  359. if new_icon ~= nil and hudtable.hudids[name].icon ~= nil then
  360. player:hud_change(hudtable.hudids[name].icon, "text", new_icon)
  361. end
  362. if new_bgicon ~= nil and hudtable.hudids[name].bgicon ~= nil then
  363. player:hud_change(hudtable.hudids[name].bgicon, "text", new_bgicon)
  364. end
  365. if new_bar ~= nil then
  366. player:hud_change(hudtable.hudids[name].bar , "text", new_bar)
  367. end
  368. if new_label ~= nil then
  369. hudtable.label = new_label
  370. local new_text = make_label(hudtable.format_string, hudtable.format_string_config, new_label, hudtable.hudstate[name].value, hudtable.hudstate[name].max)
  371. player:hud_change(hudtable.hudids[name].text, "text", new_text)
  372. end
  373. if new_text_color ~= nil then
  374. player:hud_change(hudtable.hudids[name].text, "number", new_text_color)
  375. end
  376. else
  377. if new_icon ~= nil and hudtable.hudids[name].bar ~= nil then
  378. player:hud_change(hudtable.hudids[name].bar, "text", new_icon)
  379. end
  380. if new_bgicon ~= nil and hudtable.hudids[name].bg ~= nil then
  381. player:hud_change(hudtable.hudids[name].bg, "text", new_bgicon)
  382. end
  383. end
  384. local main_error_text =
  385. "[hudbars] Bad call to hb.change_hudbar, identifier: “"..tostring(identifier).."”, player name: “"..name.."”. "
  386. if new_max_value < new_value then
  387. minetest.log("error", main_error_text.."new_max_value ("..new_max_value..") is smaller than new_value ("..new_value..")!")
  388. end
  389. if new_max_value < 0 then
  390. minetest.log("error", main_error_text.."new_max_value ("..new_max_value..") is smaller than 0!")
  391. end
  392. if new_value < 0 then
  393. minetest.log("error", main_error_text.."new_value ("..new_value..") is smaller than 0!")
  394. end
  395. if hudtable.hudstate[name].hidden == false then
  396. if max_changed and hb.settings.bar_type == "progress_bar" then
  397. if hudtable.hudstate[name].max == 0 then
  398. player:hud_change(hudtable.hudids[name].bg, "scale", {x=0,y=0})
  399. else
  400. player:hud_change(hudtable.hudids[name].bg, "scale", {x=1,y=1})
  401. end
  402. end
  403. if value_changed or max_changed then
  404. local new_barlength = hb.value_to_barlength(new_value, new_max_value)
  405. if new_barlength ~= hudtable.hudstate[name].barlength then
  406. player:hud_change(hudtable.hudids[name].bar, "number", hb.value_to_barlength(new_value, new_max_value))
  407. hudtable.hudstate[name].barlength = new_barlength
  408. end
  409. if hb.settings.bar_type == "progress_bar" then
  410. local new_text = make_label(hudtable.format_string, hudtable.format_string_config, hudtable.label, new_value, new_max_value)
  411. if new_text ~= hudtable.hudstate[name].text then
  412. player:hud_change(hudtable.hudids[name].text, "text", new_text)
  413. hudtable.hudstate[name].text = new_text
  414. end
  415. end
  416. end
  417. end
  418. return true
  419. end
  420. function hb.hide_hudbar(player, identifier)
  421. if not player_exists(player) then return false end
  422. local name = player:get_player_name()
  423. local hudtable = hb.get_hudtable(identifier)
  424. if hudtable == nil then return false end
  425. if hudtable.hudstate[name].hidden == true then return true end
  426. if hb.settings.bar_type == "progress_bar" then
  427. if hudtable.hudids[name].icon ~= nil then
  428. player:hud_change(hudtable.hudids[name].icon, "scale", {x=0,y=0})
  429. end
  430. player:hud_change(hudtable.hudids[name].bg, "scale", {x=0,y=0})
  431. player:hud_change(hudtable.hudids[name].text, "text", "")
  432. end
  433. player:hud_change(hudtable.hudids[name].bar, "number", 0)
  434. player:hud_change(hudtable.hudids[name].bar, "item", 0)
  435. hudtable.hudstate[name].hidden = true
  436. return true
  437. end
  438. function hb.unhide_hudbar(player, identifier)
  439. if not player_exists(player) then return false end
  440. local name = player:get_player_name()
  441. local hudtable = hb.get_hudtable(identifier)
  442. if hudtable == nil then return false end
  443. if hudtable.hudstate[name].hidden == false then return true end
  444. local value = hudtable.hudstate[name].value
  445. local max = hudtable.hudstate[name].max
  446. if hb.settings.bar_type == "progress_bar" then
  447. if hudtable.hudids[name].icon ~= nil then
  448. player:hud_change(hudtable.hudids[name].icon, "scale", {x=1,y=1})
  449. end
  450. if hudtable.hudstate[name].max ~= 0 then
  451. player:hud_change(hudtable.hudids[name].bg, "scale", {x=1,y=1})
  452. end
  453. player:hud_change(hudtable.hudids[name].text, "text", make_label(hudtable.format_string, hudtable.format_string_config, hudtable.label, value, max))
  454. elseif hb.settings.bar_type == "statbar_modern" then
  455. player:hud_change(hudtable.hudids[name].bar, "scale", {x=1,y=1})
  456. end
  457. player:hud_change(hudtable.hudids[name].bar, "number", hb.value_to_barlength(value, max))
  458. player:hud_change(hudtable.hudids[name].bar, "item", hb.value_to_barlength(max, max))
  459. hudtable.hudstate[name].hidden = false
  460. return true
  461. end
  462. function hb.get_hudbar_state(player, identifier)
  463. if not player_exists(player) then return nil end
  464. local ref = hb.get_hudtable(identifier).hudstate[player:get_player_name()]
  465. if not ref then return nil end
  466. -- Do not forget to update this chunk of code in case the state changes
  467. local copy = {
  468. hidden = ref.hidden,
  469. value = ref.value,
  470. max = ref.max,
  471. text = ref.text,
  472. barlength = ref.barlength,
  473. }
  474. return copy
  475. end
  476. function hb.get_hudbar_identifiers()
  477. local ids = {}
  478. for id, _ in pairs(hb.hudtables) do
  479. table.insert(ids, id)
  480. end
  481. return ids
  482. end
  483. --register built-in HUD bars
  484. if minetest.settings:get_bool("enable_damage") or hb.settings.forceload_default_hudbars then
  485. hb.register_hudbar("health", 0xFFFFFF, S("Health"), { bar = "hudbars_bar_health.png", icon = "hudbars_icon_health.png", bgicon = "hudbars_bgicon_health.png" }, hb.settings.hp_player_maximun, hb.settings.hp_player_maximun, false)
  486. hb.register_hudbar("breath", 0xFFFFFF, S("Breath"), { bar = "hudbars_bar_breath.png", icon = "hudbars_icon_breath.png", bgicon = "hudbars_bgicon_breath.png" }, hb.settings.br_player_maximun, hb.settings.br_player_maximun, true)
  487. end
  488. local function hide_builtin(player)
  489. local flags = player:hud_get_flags()
  490. flags.healthbar = false
  491. flags.breathbar = false
  492. player:hud_set_flags(flags)
  493. end
  494. local function custom_hud(player)
  495. if minetest.settings:get_bool("enable_damage") or hb.settings.forceload_default_hudbars then
  496. local hide
  497. if minetest.settings:get_bool("enable_damage") then
  498. hide = false
  499. else
  500. hide = true
  501. end
  502. local hp = player:get_hp()
  503. local hp_max = hb.settings.hp_player_maximun
  504. hb.init_hudbar(player, "health", math.min(hp, hp_max), hp_max, hide)
  505. local breath = player:get_breath()
  506. local breath_max = hb.settings.br_player_maximun
  507. local hide_breath
  508. -- real honoring to configuration of max hp custom heal and breath
  509. if player:get_properties().hp_max then player:set_properties({hp_max = hb.settings.hp_player_maximun}) end
  510. if player:get_properties().breath_max then player:set_properties({breath_max = hb.settings.br_player_maximun}) end
  511. -- workaround bug https://github.com/minetest/minetest/issues/12350
  512. if breath >= breath_max and hb.settings.autohide_breath == true then hide_breath = true else hide_breath = false end
  513. hb.init_hudbar(player, "breath", math.min(breath, breath_max), breath_max, hide_breath or hide)
  514. end
  515. end
  516. local function update_health(player)
  517. local hp_max = hb.settings.hp_player_maximun
  518. local hp = math.min(player:get_hp(), hp_max)
  519. hb.change_hudbar(player, "health", hp, hp_max)
  520. end
  521. -- update built-in HUD bars
  522. local function update_hud(player)
  523. if not player_exists(player) then return end
  524. if minetest.settings:get_bool("enable_damage") then
  525. if hb.settings.forceload_default_hudbars then
  526. hb.unhide_hudbar(player, "health")
  527. end
  528. --air
  529. local breath_max = player:get_properties().breath_max or hb.settings.br_player_maximun
  530. -- workaround bug https://github.com/minetest/minetest/issues/12350
  531. local breath = player:get_breath()
  532. if breath >= breath_max and hb.settings.autohide_breath == true then
  533. hb.hide_hudbar(player, "breath")
  534. else
  535. hb.unhide_hudbar(player, "breath")
  536. hb.change_hudbar(player, "breath", math.min(breath, breath_max), breath_max)
  537. end
  538. --health
  539. update_health(player)
  540. elseif hb.settings.forceload_default_hudbars then
  541. update_health(player)
  542. hb.hide_hudbar(player, "health")
  543. hb.hide_hudbar(player, "breath")
  544. end
  545. end
  546. minetest.register_on_player_hpchange(function(player)
  547. if hb.players[player:get_player_name()] ~= nil then
  548. update_health(player)
  549. end
  550. end)
  551. minetest.register_on_respawnplayer(function(player)
  552. update_health(player)
  553. hb.hide_hudbar(player, "breath")
  554. end)
  555. minetest.register_on_joinplayer(function(player)
  556. hide_builtin(player)
  557. custom_hud(player)
  558. hb.players[player:get_player_name()] = player
  559. end)
  560. minetest.register_on_leaveplayer(function(player)
  561. hb.players[player:get_player_name()] = nil
  562. end)
  563. local main_timer = 0
  564. local timer = 0
  565. minetest.register_globalstep(function(dtime)
  566. main_timer = main_timer + dtime
  567. timer = timer + dtime
  568. if main_timer > hb.settings.tick or timer > 4 then
  569. if main_timer > hb.settings.tick then main_timer = 0 end
  570. -- only proceed if damage is enabled
  571. if minetest.settings:get_bool("enable_damage") or hb.settings.forceload_default_hudbars then
  572. for _, player in pairs(hb.players) do
  573. -- update all hud elements
  574. update_hud(player)
  575. end
  576. end
  577. end
  578. if timer > 4 then timer = 0 end
  579. end)