1 local S
= minetest
.get_translator("hbarmor")
2 local N
= function(s
) return s
end
4 if (not armor
) or (not armor
.def
) then
5 minetest
.log("error", "[hbarmor] Outdated 3d_armor version. Please update your version of 3d_armor!")
13 -- Stores if player's HUD bar has been initialized so far.
14 hbarmor
.player_active
= {}
16 -- Time difference in seconds between updates to the HUD armor bar.
17 -- Increase this number for slow servers.
20 -- If true, the armor bar is hidden when the player does not wear any armor
21 hbarmor
.autohide
= true
23 --load custom settings
24 local set
= minetest
.settings
:get_bool("hbarmor_autohide")
26 hbarmor
.autohide
= set
29 set
= minetest
.settings
:get("hbarmor_tick")
30 if tonumber(set
) ~= nil then
31 hbarmor
.tick
= tonumber(set
)
35 local must_hide
= function(playername
, arm
)
36 return ((not armor
.def
[playername
].count
or armor
.def
[playername
].count
== 0) and arm
== 0)
39 local arm_printable
= function(arm
)
40 return math
.ceil(math
.floor(arm
+0.5))
43 local function custom_hud(player
)
44 local name
= player
:get_player_name()
46 if minetest
.settings
:get_bool("enable_damage") then
47 local ret
= hbarmor
.get_armor(player
)
49 minetest
.log("error", "[hbarmor] Call to hbarmor.get_armor in custom_hud returned with false!")
51 local arm
= tonumber(hbarmor
.armor
[name
])
52 if not arm
then arm
= 0 end
54 if hbarmor
.autohide
then
55 hide
= must_hide(name
, arm
)
59 hb
.init_hudbar(player
, "armor", arm_printable(arm
), nil, hide
)
63 --register and define armor HUD bar
64 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" } )
66 function hbarmor
.get_armor(player
)
67 if not player
or not armor
.def
then
70 local name
= player
:get_player_name()
71 local def
= armor
.def
[name
] or nil
72 if def
and def
.state
and def
.count
then
73 hbarmor
.set_armor(name
, def
.state
, def
.count
)
80 function hbarmor
.set_armor(player_name
, ges_state
, items
)
85 local max = max_items
* 65535
86 local lvl
= max - ges_state
88 if ges_state
== 0 and items
== 0 then
92 hbarmor
.armor
[player_name
] = math
.max(0, math
.min(lvl
* (items
* (100 / max_items
)), 100))
95 -- update hud elemtens if value has changed
96 local function update_hud(player
)
97 local name
= player
:get_player_name()
99 local arm
= tonumber(hbarmor
.armor
[name
])
102 hbarmor
.armor
[name
] = 0
104 if hbarmor
.autohide
then
105 -- hide armor bar completely when there is none
106 if must_hide(name
, arm
) then
107 hb
.hide_hudbar(player
, "armor")
109 hb
.change_hudbar(player
, "armor", arm_printable(arm
))
110 hb
.unhide_hudbar(player
, "armor")
113 hb
.change_hudbar(player
, "armor", arm_printable(arm
))
117 minetest
.register_on_joinplayer(function(player
)
118 local name
= player
:get_player_name()
120 hbarmor
.player_active
[name
] = true
123 minetest
.register_on_leaveplayer(function(player
)
124 local name
= player
:get_player_name()
125 hbarmor
.player_active
[name
] = false
130 minetest
.register_globalstep(function(dtime
)
131 main_timer
= main_timer
+ dtime
132 timer
= timer
+ dtime
133 if main_timer
> hbarmor
.tick
or timer
> 4 then
134 if minetest
.settings
:get_bool("enable_damage") then
135 if main_timer
> hbarmor
.tick
then main_timer
= 0 end
136 for _
,player
in ipairs(minetest
.get_connected_players()) do
137 local name
= player
:get_player_name()
138 if hbarmor
.player_active
[name
] == true then
139 local ret
= hbarmor
.get_armor(player
)
141 minetest
.log("error", "[hbarmor] Call to hbarmor.get_armor in globalstep returned with false!")
143 -- update all hud elements
149 if timer
> 4 then timer
= 0 end