2 if (minetest
.get_modpath("intllib")) then
5 S
= function ( s
) return s
end
8 if (not armor
) or (not armor
.def
) then
9 minetest
.log("error", "[hbarmor] Outdated 3d_armor version. Please update your version of 3d_armor!")
17 -- Stores if player's HUD bar has been initialized so far.
18 hbarmor
.player_active
= {}
20 -- Time difference in seconds between updates to the HUD armor bar.
21 -- Increase this number for slow servers.
24 -- If true, the armor bar is hidden when the player does not wear any armor
25 hbarmor
.autohide
= true
27 --load custom settings
28 local set
= minetest
.settings
:get_bool("hbarmor_autohide")
30 hbarmor
.autohide
= set
33 set
= minetest
.settings
:get("hbarmor_tick")
34 if tonumber(set
) ~= nil then
35 hbarmor
.tick
= tonumber(set
)
39 local must_hide
= function(playername
, arm
)
40 return ((not armor
.def
[playername
].count
or armor
.def
[playername
].count
== 0) and arm
== 0)
43 local arm_printable
= function(arm
)
44 return math
.ceil(math
.floor(arm
+0.5))
47 local function custom_hud(player
)
48 local name
= player
:get_player_name()
50 if minetest
.settings
:get_bool("enable_damage") then
51 local ret
= hbarmor
.get_armor(player
)
53 minetest
.log("error", "[hbarmor] Call to hbarmor.get_armor in custom_hud returned with false!")
55 local arm
= tonumber(hbarmor
.armor
[name
])
56 if not arm
then arm
= 0 end
58 if hbarmor
.autohide
then
59 hide
= must_hide(name
, arm
)
63 hb
.init_hudbar(player
, "armor", arm_printable(arm
), nil, hide
)
67 --register and define armor HUD bar
68 hb
.register_hudbar("armor", 0xFFFFFF, S("Armor"), { icon
= "hbarmor_icon.png", bgicon
= "hbarmor_bgicon.png", bar
= "hbarmor_bar.png" }, 0, 100, hbarmor
.autohide
, S("%s: %d%%"))
70 function hbarmor
.get_armor(player
)
71 if not player
or not armor
.def
then
74 local name
= player
:get_player_name()
75 local def
= armor
.def
[name
] or nil
76 if def
and def
.state
and def
.count
then
77 hbarmor
.set_armor(name
, def
.state
, def
.count
)
84 function hbarmor
.set_armor(player_name
, ges_state
, items
)
89 local max = max_items
* 65535
90 local lvl
= max - ges_state
92 if ges_state
== 0 and items
== 0 then
96 hbarmor
.armor
[player_name
] = math
.max(0, math
.min(lvl
* (items
* (100 / max_items
)), 100))
99 -- update hud elemtens if value has changed
100 local function update_hud(player
)
101 local name
= player
:get_player_name()
103 local arm
= tonumber(hbarmor
.armor
[name
])
106 hbarmor
.armor
[name
] = 0
108 if hbarmor
.autohide
then
109 -- hide armor bar completely when there is none
110 if must_hide(name
, arm
) then
111 hb
.hide_hudbar(player
, "armor")
113 hb
.change_hudbar(player
, "armor", arm_printable(arm
))
114 hb
.unhide_hudbar(player
, "armor")
117 hb
.change_hudbar(player
, "armor", arm_printable(arm
))
121 minetest
.register_on_joinplayer(function(player
)
122 local name
= player
:get_player_name()
124 hbarmor
.player_active
[name
] = true
127 minetest
.register_on_leaveplayer(function(player
)
128 local name
= player
:get_player_name()
129 hbarmor
.player_active
[name
] = false
134 minetest
.register_globalstep(function(dtime
)
135 main_timer
= main_timer
+ dtime
136 timer
= timer
+ dtime
137 if main_timer
> hbarmor
.tick
or timer
> 4 then
138 if minetest
.settings
:get_bool("enable_damage") then
139 if main_timer
> hbarmor
.tick
then main_timer
= 0 end
140 for _
,player
in ipairs(minetest
.get_connected_players()) do
141 local name
= player
:get_player_name()
142 if hbarmor
.player_active
[name
] == true then
143 local ret
= hbarmor
.get_armor(player
)
145 minetest
.log("error", "[hbarmor] Call to hbarmor.get_armor in globalstep returned with false!")
147 -- update all hud elements
153 if timer
> 4 then timer
= 0 end