6 -- Stores if player's HUD bar has been initialized so far.
7 hbarmor
.player_active
= {}
14 --load custom settings
15 local set
= io
.open(minetest
.get_modpath("hbarmor").."/hbarmor.conf", "r")
17 dofile(minetest
.get_modpath("hbarmor").."/hbarmor.conf")
21 local must_hide
= function(playername
, arm
)
22 return ((not armor
.def
[playername
].count
or armor
.def
[playername
].count
== 0) and arm
== 0)
25 local arm_printable
= function(arm
)
26 return math
.ceil(math
.floor(arm
+0.5))
29 local function custom_hud(player
)
30 local name
= player
:get_player_name()
32 if minetest
.setting_getbool("enable_damage") then
33 local arm
= tonumber(hbarmor
.armor
[name
])
34 if not arm
then arm
= 0 end
35 local hide
= must_hide(name
, arm
)
36 hb
.init_hudbar(player
, "armor", arm_printable(arm
), nil, hide
)
40 --register and define armor HUD bar
41 hb
.register_hudbar("armor", 0xFFFFFF, "Armor", { icon
= "hbarmor_icon.png", bar
= "hbarmor_bar.png" }, 0, 100, true, "%s: %d%%")
43 dofile(minetest
.get_modpath("hbarmor").."/armor.lua")
46 -- update hud elemtens if value has changed
47 local function update_hud(player
)
48 local name
= player
:get_player_name()
50 local arm
= tonumber(hbarmor
.armor
[name
])
53 hbarmor
.armor
[name
] = 0
55 -- hide armor bar completely when there is none
56 if must_hide(name
, arm
) then
57 hb
.hide_hudbar(player
, "armor")
59 hb
.change_hudbar(player
, "armor", arm_printable(arm
))
60 hb
.unhide_hudbar(player
, "armor")
64 minetest
.register_on_joinplayer(function(player
)
65 local name
= player
:get_player_name()
66 hbarmor
.armor
[name
] = 0
68 hbarmor
.player_active
[name
] = true
71 minetest
.register_on_leaveplayer(function(player
)
72 local name
= player
:get_player_name()
73 hbarmor
.player_active
[name
] = false
78 minetest
.register_globalstep(function(dtime
)
79 main_timer
= main_timer
+ dtime
81 if main_timer
> hbarmor
.tick
or timer
> 4 then
82 if minetest
.setting_getbool("enable_damage") then
83 if main_timer
> hbarmor
.tick
then main_timer
= 0 end
84 for _
,player
in ipairs(minetest
.get_connected_players()) do
85 local name
= player
:get_player_name()
86 if hbarmor
.player_active
[name
] == true then
87 hbarmor
.get_armor(player
)
89 -- update all hud elements
95 if timer
> 4 then timer
= 0 end