5 -- number of registered HUD bars
11 hb
.settings
.max_bar_length
= 160
14 hb
.settings
.pos_left
= { x
=0.5, y
=1 }
15 hb
.settings
.pos_right
= { x
= 0.5, y
= 1 }
16 hb
.settings
.start_offset_left
= { x
= -175, y
= -70 }
17 hb
.settings
.start_offset_right
= { x
= 15, y
= -70 }
19 hb
.settings
.vmargin
= 24
20 hb
.settings
.tick
= 0.1
22 -- Table which contains all players with active default HUD bars (only for internal use)
25 function hb
.value_to_barlength(value
, max)
29 return math
.ceil((value
/max) * hb
.settings
.max_bar_length
)
33 function hb
.get_hudtable(identifier
)
34 return hb
.hudtables
[identifier
]
37 function hb
.register_hudbar(identifier
, text_color
, label
, textures
, default_start_value
, default_start_max
, default_start_hidden
, format_string
)
40 if hb
.hudbars_count
% 2 == 0 then
41 pos
= hb
.settings
.pos_left
43 x
= hb
.settings
.start_offset_left
.x
,
44 y
= hb
.settings
.start_offset_left
.y
- hb
.settings
.vmargin
* math
.floor(hb
.hudbars_count
/2)
47 pos
= hb
.settings
.pos_right
49 x
= hb
.settings
.start_offset_right
.x
,
50 y
= hb
.settings
.start_offset_right
.y
- hb
.settings
.vmargin
* math
.floor((hb
.hudbars_count
-1)/2)
53 if format_string
== nil then
54 format_string
= "%s: %d/%d"
57 hudtable
.add_all
= function(player
, hudtable
, start_value
, start_max
, start_hidden
)
58 if start_value
== nil then start_value
= hudtable
.default_start_value
end
59 if start_max
== nil then start_max
= hudtable
.default_start_max
end
60 if start_hidden
== nil then start_hidden
= hudtable
.default_start_hidden
end
63 local name
= player
:get_player_name()
64 local bgscale
, iconscale
, text
, barnumber
65 if start_max
== 0 or start_hidden
then
66 bgscale
= { x
=0, y
=0 }
68 bgscale
= { x
=1, y
=1 }
71 iconscale
= { x
=0, y
=0 }
75 iconscale
= { x
=1, y
=1 }
76 barnumber
= hb
.value_to_barlength(start_value
, start_max
)
77 text
= string.format(format_string
, label
, start_value
, start_max
)
79 ids
.bg
= player
:hud_add({
80 hud_elem_type
= "image",
83 text
= "hudbars_bar_background.png",
84 alignment
= {x
=1,y
=1},
85 offset
= { x
= offset
.x
- 1, y
= offset
.y
- 1 },
87 if textures
.icon
~= nil then
88 ids
.icon
= player
:hud_add({
89 hud_elem_type
= "image",
93 alignment
= {x
=-1,y
=1},
94 offset
= { x
= offset
.x
- 3, y
= offset
.y
},
97 ids
.bar
= player
:hud_add({
98 hud_elem_type
= "statbar",
102 alignment
= {x
=-1,y
=-1},
105 ids
.text
= player
:hud_add({
106 hud_elem_type
= "text",
109 alignment
= {x
=1,y
=1},
112 offset
= { x
= offset
.x
+ 2, y
= offset
.y
},
114 -- Do not forget to update hb.get_hudbar_state if you add new fields to the state table
115 state
.hidden
= start_hidden
116 state
.value
= start_value
117 state
.max = start_max
119 state
.barlength
= hb
.value_to_barlength(start_value
, start_max
)
121 local main_error_text
=
122 "[hudbars] Bad initial values of HUD bar identifier “"..tostring(identifier
).."” for player "..name
..". "
124 if start_max
< start_value
then
125 minetest
.log("error", main_error_text
.."start_max ("..start_max
..") is smaller than start_value ("..start_value
..")!")
127 if start_max
< 0 then
128 minetest
.log("error", main_error_text
.."start_max ("..start_max
..") is smaller than 0!")
130 if start_value
< 0 then
131 minetest
.log("error", main_error_text
.."start_value ("..start_value
..") is smaller than 0!")
134 hb
.hudtables
[identifier
].hudids
[name
] = ids
135 hb
.hudtables
[identifier
].hudstate
[name
] = state
138 hudtable
.identifier
= identifier
139 hudtable
.format_string
= format_string
140 hudtable
.label
= label
142 hudtable
.hudstate
= {}
143 hudtable
.default_start_hidden
= default_start_hidden
144 hudtable
.default_start_value
= default_start_value
145 hudtable
.default_start_max
= default_start_max
147 hb
.hudbars_count
= hb
.hudbars_count
+ 1
149 hb
.hudtables
[identifier
] = hudtable
152 function hb
.init_hudbar(player
, identifier
, start_value
, start_max
, start_hidden
)
153 local hudtable
= hb
.get_hudtable(identifier
)
154 hb
.hudtables
[identifier
].add_all(player
, hudtable
, start_value
, start_max
, start_hidden
)
157 function hb
.change_hudbar(player
, identifier
, new_value
, new_max_value
)
158 if new_value
== nil and new_max_value
== nil then
162 local name
= player
:get_player_name()
163 local hudtable
= hb
.get_hudtable(identifier
)
164 local value_changed
, max_changed
= false, false
166 if new_value
~= nil then
167 if new_value
~= hudtable
.hudstate
[name
].value
then
168 hudtable
.hudstate
[name
].value
= new_value
172 new_value
= hudtable
.hudstate
[name
].value
174 if new_max_value
~= nil then
175 if new_max_value
~= hudtable
.hudstate
[name
].max then
176 hudtable
.hudstate
[name
].max = new_max_value
180 new_max_value
= hudtable
.hudstate
[name
].max
183 local main_error_text
=
184 "[hudbars] Bad call to hb.change_hudbar, identifier: “"..tostring(identifier
).."”, player name: “"..name
.."”. "
185 if new_max_value
< new_value
then
186 minetest
.log("error", main_error_text
.."new_max_value ("..new_max_value
..") is smaller than new_value ("..new_value
..")!")
188 if new_max_value
< 0 then
189 minetest
.log("error", main_error_text
.."new_max_value ("..new_max_value
..") is smaller than 0!")
191 if new_value
< 0 then
192 minetest
.log("error", main_error_text
.."new_value ("..new_value
..") is smaller than 0!")
195 if hudtable
.hudstate
[name
].hidden
== false then
197 if hudtable
.hudstate
[name
].max == 0 then
198 player
:hud_change(hudtable
.hudids
[name
].bg
, "scale", {x
=0,y
=0})
200 player
:hud_change(hudtable
.hudids
[name
].bg
, "scale", {x
=1,y
=1})
204 if value_changed
or max_changed
then
205 local new_barlength
= hb
.value_to_barlength(new_value
, new_max_value
)
206 if new_barlength
~= hudtable
.hudstate
[name
].barlength
then
207 player
:hud_change(hudtable
.hudids
[name
].bar
, "number", hb
.value_to_barlength(new_value
, new_max_value
))
208 hudtable
.hudstate
[name
].barlength
= new_barlength
211 local new_text
= string.format(hudtable
.format_string
, hudtable
.label
, new_value
, new_max_value
)
212 if new_text
~= hudtable
.hudstate
[name
].text
then
213 player
:hud_change(hudtable
.hudids
[name
].text
, "text", new_text
)
214 hudtable
.hudstate
[name
].text
= new_text
220 function hb
.hide_hudbar(player
, identifier
)
221 local name
= player
:get_player_name()
222 local hudtable
= hb
.get_hudtable(identifier
)
223 if(hudtable
.hudstate
[name
].hidden
== false) then
224 if hudtable
.hudids
[name
].icon
~= nil then
225 player
:hud_change(hudtable
.hudids
[name
].icon
, "scale", {x
=0,y
=0})
227 player
:hud_change(hudtable
.hudids
[name
].bg
, "scale", {x
=0,y
=0})
228 player
:hud_change(hudtable
.hudids
[name
].bar
, "number", 0)
229 player
:hud_change(hudtable
.hudids
[name
].text
, "text", "")
230 hudtable
.hudstate
[name
].hidden
= true
234 function hb
.unhide_hudbar(player
, identifier
)
235 local name
= player
:get_player_name()
236 local hudtable
= hb
.get_hudtable(identifier
)
237 if(hudtable
.hudstate
[name
].hidden
) then
238 local name
= player
:get_player_name()
239 local value
= hudtable
.hudstate
[name
].value
240 local max = hudtable
.hudstate
[name
].max
241 if hudtable
.hudids
[name
].icon
~= nil then
242 player
:hud_change(hudtable
.hudids
[name
].icon
, "scale", {x
=1,y
=1})
244 if hudtable
.hudstate
[name
].max ~= 0 then
245 player
:hud_change(hudtable
.hudids
[name
].bg
, "scale", {x
=1,y
=1})
247 player
:hud_change(hudtable
.hudids
[name
].bar
, "number", hb
.value_to_barlength(value
, max))
248 player
:hud_change(hudtable
.hudids
[name
].text
, "text", tostring(string.format(hudtable
.format_string
, hudtable
.label
, value
, max)))
249 hudtable
.hudstate
[name
].hidden
= false
253 function hb
.get_hudbar_state(player
, identifier
)
254 local ref
= hb
.get_hudtable(identifier
).hudstate
[player
:get_player_name()]
255 -- Do not forget to update this chunk of code in case the state changes
261 barlength
= ref
.barlength
,
266 --register built-in HUD bars
267 if minetest
.setting_getbool("enable_damage") then
268 hb
.register_hudbar("health", 0xFFFFFF, "Health", { bar
= "hudbars_bar_health.png", icon
= "hudbars_icon_health.png" }, 20, 20, false)
269 hb
.register_hudbar("breath", 0xFFFFFF, "Breath", { bar
= "hudbars_bar_breath.png", icon
= "hudbars_icon_breath.png" }, 10, 10, true)
272 --load custom settings
273 local set
= io
.open(minetest
.get_modpath("hudbars").."/hudbars.conf", "r")
275 dofile(minetest
.get_modpath("hudbars").."/hudbars.conf")
279 local function hide_builtin(player
)
280 local flags
= player
:hud_get_flags()
281 flags
.healthbar
= false
282 flags
.breathbar
= false
283 player
:hud_set_flags(flags
)
287 local function custom_hud(player
)
288 if minetest
.setting_getbool("enable_damage") then
289 hb
.init_hudbar(player
, "health", player
:get_hp())
290 local breath
= player
:get_breath()
292 if breath
== 11 then hide_breath
= true else hide_breath
= false end
293 hb
.init_hudbar(player
, "breath", math
.min(breath
, 10), nil, hide_breath
)
298 -- update built-in HUD bars
299 local function update_hud(player
)
300 if minetest
.setting_getbool("enable_damage") then
302 local breath
= player
:get_breath()
305 hb
.hide_hudbar(player
, "breath")
307 hb
.unhide_hudbar(player
, "breath")
308 hb
.change_hudbar(player
, "breath", math
.min(breath
, 10))
312 hb
.change_hudbar(player
, "health", player
:get_hp())
316 minetest
.register_on_joinplayer(function(player
)
319 hb
.players
[player
:get_player_name()] = player
322 minetest
.register_on_leaveplayer(function(player
)
323 hb
.players
[player
:get_player_name()] = nil
328 minetest
.register_globalstep(function(dtime
)
329 main_timer
= main_timer
+ dtime
330 timer
= timer
+ dtime
331 if main_timer
> hb
.settings
.tick
or timer
> 4 then
332 if main_timer
> hb
.settings
.tick
then main_timer
= 0 end
333 for playername
, player
in pairs(hb
.players
) do
334 -- only proceed if damage is enabled
335 if minetest
.setting_getbool("enable_damage") then
336 -- update all hud elements
341 if timer
> 4 then timer
= 0 end