9 -- number of registered HUD bars
14 local health_hud_text
= {}
15 local health_hud_icon
= {}
16 local health_hud_bg
= {}
18 local air_hud_text
= {}
19 local air_hud_icon
= {}
28 HUD_HEALTH_POS
= {x
=0.5,y
=0.9}
29 HUD_HEALTH_OFFSET
= {x
=-175, y
=2}
30 HUD_AIR_POS
= {x
=0.5,y
=0.9}
31 HUD_AIR_OFFSET
= {x
=15,y
=2}
33 -- dirty way to check for new statbars
34 if dump(minetest
.hud_replace_builtin
) ~= "nil" then
36 HUD_HEALTH_POS
= {x
=0.5,y
=1}
37 HUD_HEALTH_OFFSET
= {x
=-175, y
=-70}
38 HUD_AIR_POS
= {x
=0.5,y
=1}
39 HUD_AIR_OFFSET
= {x
=15,y
=-70}
42 HUD_CUSTOM_POS_LEFT
= HUD_HEALTH_POS
43 HUD_CUSTOM_POS_RIGHT
= HUD_AIR_POS
45 HUD_CUSTOM_VMARGIN
= 24
46 if minetest
.setting_getbool("enable_damage") then
47 HUD_CUSTOM_START_OFFSET_LEFT
= {x
=HUD_HEALTH_OFFSET
.x
, y
=HUD_HEALTH_OFFSET
.y
- HUD_CUSTOM_VMARGIN
}
48 HUD_CUSTOM_START_OFFSET_RIGHT
= {x
=HUD_AIR_OFFSET
.x
, y
=HUD_AIR_OFFSET
.y
- HUD_CUSTOM_VMARGIN
}
50 HUD_CUSTOM_START_OFFSET_LEFT
= {x
=HUD_HEALTH_OFFSET
.x
, y
=HUD_HEALTH_OFFSET
.y
}
51 HUD_CUSTOM_START_OFFSET_RIGHT
= {x
=HUD_AIR_OFFSET
.x
, y
=HUD_AIR_OFFSET
.y
}
56 function hb
.value_to_barlength(value
, max)
60 return math
.ceil((value
/max) * HUD_BARLENGTH
)
64 function hb
.get_hudtable(identifier
)
65 return hb
.hudtables
[identifier
]
68 function hb
.register_hudbar(identifier
, text_color
, label
, textures
, default_start_value
, default_start_max
, start_hidden
, format_string
)
71 if hb
.hudbars_count
% 2 == 0 then
72 pos
= HUD_CUSTOM_POS_LEFT
74 x
= HUD_CUSTOM_START_OFFSET_LEFT
.x
,
75 y
= HUD_CUSTOM_START_OFFSET_LEFT
.y
- HUD_CUSTOM_VMARGIN
* math
.floor(hb
.hudbars_count
/2)
78 pos
= HUD_CUSTOM_POS_RIGHT
80 x
= HUD_CUSTOM_START_OFFSET_RIGHT
.x
,
81 y
= HUD_CUSTOM_START_OFFSET_RIGHT
.y
- HUD_CUSTOM_VMARGIN
* math
.floor((hb
.hudbars_count
-1)/2)
84 if format_string
== nil then
85 format_string
= "%s: %d/%d"
88 hudtable
.add_all
= function(player
, start_value
, start_max
)
89 if start_value
== nil then start_value
= default_start_value
end
90 if start_max
== nil then start_max
= default_start_max
end
93 local name
= player
:get_player_name()
95 if start_max
== 0 then
96 bgscale
= { x
=0, y
=0 }
98 bgscale
= { x
=1, y
=1 }
100 ids
.bg
= player
:hud_add({
101 hud_elem_type
= "image",
104 text
= "hudbars_bar_background.png",
105 alignment
= {x
=1,y
=1},
106 offset
= { x
= offset
.x
- 1, y
= offset
.y
- 1 },
108 if textures
.icon
~= nil then
109 ids
.icon
= player
:hud_add({
110 hud_elem_type
= "image",
112 scale
= { x
= 1, y
= 1 },
113 text
= textures
.icon
,
114 alignment
= {x
=-1,y
=1},
115 offset
= { x
= offset
.x
- 3, y
= offset
.y
},
118 ids
.bar
= player
:hud_add({
119 hud_elem_type
= "statbar",
122 number = hb
.value_to_barlength(start_value
, start_max
),
123 alignment
= {x
=-1,y
=-1},
126 local text
= string.format(format_string
, label
, start_value
, start_max
)
127 ids
.text
= player
:hud_add({
128 hud_elem_type
= "text",
131 alignment
= {x
=1,y
=1},
134 offset
= { x
= offset
.x
+ 2, y
= offset
.y
},
136 state
.hidden
= start_hidden
137 state
.value
= start_value
138 state
.max = start_max
140 state
.barlength
= hb
.value_to_barlength(start_value
, start_max
)
142 hb
.hudtables
[identifier
].hudids
[name
] = ids
143 hb
.hudtables
[identifier
].hudstate
[name
] = state
146 hudtable
.identifier
= identifier
147 hudtable
.format_string
= format_string
148 hudtable
.label
= label
150 hudtable
.hudstate
= {}
152 hb
.hudbars_count
= hb
.hudbars_count
+ 1
154 hb
.hudtables
[identifier
] = hudtable
157 function hb
.init_hudbar(player
, identifier
, start_value
, start_max
)
158 hb
.hudtables
[identifier
].add_all(player
, start_value
, start_max
)
161 function hb
.change_hudbar(player
, identifier
, new_value
, new_max_value
)
162 if new_value
== nil and new_max_value
== nil then
166 local name
= player
:get_player_name()
167 local hudtable
= hb
.get_hudtable(identifier
)
168 local value_changed
, max_changed
= false, false
170 if new_value
~= nil then
171 if new_value
~= hudtable
.hudstate
[name
].value
then
172 hudtable
.hudstate
[name
].value
= new_value
176 new_value
= hudtable
.hudstate
[name
].value
178 if new_max_value
~= nil then
179 if new_max_value
~= hudtable
.hudstate
[name
].max then
180 hudtable
.hudstate
[name
].max = new_max_value
184 new_max_value
= hudtable
.hudstate
[name
].max
187 if hudtable
.hudstate
[name
].hidden
== false then
189 if hudtable
.hudstate
[name
].max == 0 then
190 player
:hud_change(hudtable
.hudids
[name
].bg
, "scale", {x
=0,y
=0})
192 player
:hud_change(hudtable
.hudids
[name
].bg
, "scale", {x
=1,y
=1})
196 if value_changed
or max_changed
then
197 local new_barlength
= hb
.value_to_barlength(new_value
, new_max_value
)
198 if new_barlength
~= hudtable
.hudstate
[name
].barlength
then
199 player
:hud_change(hudtable
.hudids
[name
].bar
, "number", hb
.value_to_barlength(new_value
, new_max_value
))
200 hudtable
.hudstate
[name
].barlength
= new_barlength
203 local new_text
= string.format(hudtable
.format_string
, hudtable
.label
, new_value
, new_max_value
)
204 if new_text
~= hudtable
.hudstate
[name
].text
then
205 player
:hud_change(hudtable
.hudids
[name
].text
, "text", new_text
)
206 hudtable
.hudstate
[name
].text
= new_text
212 function hb
.hide_hudbar(player
, identifier
)
213 local name
= player
:get_player_name()
214 local hudtable
= hb
.get_hudtable(identifier
)
215 if(hudtable
.hudstate
[name
].hidden
== false) then
216 player
:hud_change(hudtable
.hudids
[name
].icon
, "scale", {x
=0,y
=0})
217 player
:hud_change(hudtable
.hudids
[name
].bg
, "scale", {x
=0,y
=0})
218 player
:hud_change(hudtable
.hudids
[name
].bar
, "number", 0)
219 player
:hud_change(hudtable
.hudids
[name
].text
, "text", "")
220 hudtable
.hudstate
[name
].hidden
= true
224 function hb
.unhide_hudbar(player
, identifier
)
225 local name
= player
:get_player_name()
226 local hudtable
= hb
.get_hudtable(identifier
)
227 if(hudtable
.hudstate
[name
].hidden
) then
228 local name
= player
:get_player_name()
229 local value
= hudtable
.hudstate
[name
].value
230 local max = hudtable
.hudstate
[name
].max
231 player
:hud_change(hudtable
.hudids
[name
].icon
, "scale", {x
=1,y
=1})
232 if hudtable
.hudstate
[name
].max ~= 0 then
233 player
:hud_change(hudtable
.hudids
[name
].bg
, "scale", {x
=1,y
=1})
235 player
:hud_change(hudtable
.hudids
[name
].bar
, "number", hb
.value_to_barlength(value
, max))
236 player
:hud_change(hudtable
.hudids
[name
].text
, "text", tostring(string.format(hudtable
.format_string
, hudtable
.label
, value
, max)))
237 hudtable
.hudstate
[name
].hidden
= false
242 --load custom settings
243 local set
= io
.open(minetest
.get_modpath("hudbars").."/hudbars.conf", "r")
245 dofile(minetest
.get_modpath("hudbars").."/hudbars.conf")
249 local function hide_builtin(player
)
250 player
:hud_set_flags({crosshair
= true, hotbar
= true, healthbar
= false, wielditem
= true, breathbar
= false})
254 local function custom_hud(player
)
255 local name
= player
:get_player_name()
257 if minetest
.setting_getbool("enable_damage") then
259 health_hud_icon
[name
] = player
:hud_add({
260 hud_elem_type
= "image",
261 position
= HUD_HEALTH_POS
,
262 scale
= { x
= 1, y
= 1 },
263 text
= "hudbars_icon_health.png",
264 alignment
= {x
=-1,y
=1},
265 offset
= { x
= HUD_HEALTH_OFFSET
.x
- 3, y
= HUD_HEALTH_OFFSET
.y
},
267 health_hud_bg
[name
] = player
:hud_add({
268 hud_elem_type
= "image",
269 position
= HUD_HEALTH_POS
,
270 scale
= { x
= 1, y
= 1 },
271 text
= "hudbars_bar_background.png",
272 alignment
= {x
=1,y
=1},
273 offset
= { x
= HUD_HEALTH_OFFSET
.x
- 1, y
= HUD_HEALTH_OFFSET
.y
- 1 },
275 health_hud
[name
] = player
:hud_add({
276 hud_elem_type
= "statbar",
277 position
= HUD_HEALTH_POS
,
278 text
= "hudbars_bar_health.png",
279 number = hb
.value_to_barlength(player
:get_hp(), 20),
280 alignment
= {x
=-1,y
=-1},
281 offset
= HUD_HEALTH_OFFSET
,
283 health_hud_text
[name
] = player
:hud_add({
284 hud_elem_type
= "text",
285 position
= HUD_HEALTH_POS
,
286 text
= tostring(string.format("Health: %d/%d", player
:get_hp(), 20)),
287 alignment
= {x
=1,y
=1},
290 offset
= { x
= HUD_HEALTH_OFFSET
.x
+ 2, y
= HUD_HEALTH_OFFSET
.y
},
294 local airnumber
, airtext
, airscale
295 local air
= player
:get_breath()
299 airscale
= {x
=0, y
=0}
301 airnumber
= hb
.value_to_barlength(math
.min(air
, 10), 10)
302 airtext
= tostring(string.format("Breath: %d/%d", math
.min(air
, 10), 10))
303 airscale
= {x
=1, y
=1}
305 air_hud_icon
[name
] = player
:hud_add({
306 hud_elem_type
= "image",
307 position
= HUD_AIR_POS
,
309 text
= "hudbars_icon_breath.png",
310 alignment
= {x
=-1,y
=1},
311 offset
= { x
= HUD_AIR_OFFSET
.x
- 3, y
= HUD_AIR_OFFSET
.y
},
313 air_hud_bg
[name
] = player
:hud_add({
314 hud_elem_type
= "image",
315 position
= HUD_AIR_POS
,
317 text
= "hudbars_bar_background.png",
318 alignment
= {x
=1,y
=1},
319 offset
= { x
= HUD_AIR_OFFSET
.x
- 1, y
= HUD_AIR_OFFSET
.y
- 1 },
321 air_hud
[name
] = player
:hud_add({
322 hud_elem_type
= "statbar",
323 position
= HUD_AIR_POS
,
324 text
= "hudbars_bar_breath.png",
326 alignment
= {x
=-1,y
=-1},
327 offset
= HUD_AIR_OFFSET
,
329 air_hud_text
[name
] = player
:hud_add({
330 hud_elem_type
= "text",
331 position
= HUD_AIR_POS
,
333 alignment
= {x
=1,y
=1},
336 offset
= { x
= HUD_AIR_OFFSET
.x
+ 2, y
= HUD_AIR_OFFSET
.y
},
343 -- update hud elemtens if value has changed
344 local function update_hud(player
)
345 local name
= player
:get_player_name()
347 local air
= tonumber(hb
.air
[name
])
348 if player
:get_breath() ~= air
then
349 air
= player
:get_breath()
351 local airnumber
, airtext
, airscale
355 airscale
= {x
=0, y
=0}
357 airnumber
= hb
.value_to_barlength(math
.min(air
, 10), 10)
358 airtext
= tostring(string.format("Breath: %d/%d", math
.min(player
:get_breath(), 10), 10))
359 airscale
= {x
=1, y
=1}
361 player
:hud_change(air_hud
[name
], "number", airnumber
)
362 player
:hud_change(air_hud_text
[name
], "text", airtext
)
363 player
:hud_change(air_hud_icon
[name
], "scale", airscale
)
364 player
:hud_change(air_hud_bg
[name
], "scale", airscale
)
367 local hp
= tonumber(hb
.health
[name
])
368 if player
:get_hp() ~= hp
then
371 player
:hud_change(health_hud
[name
], "number", hb
.value_to_barlength(hp
, 20))
372 player
:hud_change(health_hud_text
[name
], "text",
373 tostring(string.format("Health: %d/%d", hp
, 20))
379 minetest
.register_on_joinplayer(function(player
)
380 local name
= player
:get_player_name()
381 local inv
= player
:get_inventory()
382 hb
.health
[name
] = player
:get_hp()
383 local air
= player
:get_breath()
385 minetest
.after(0.5, function()
394 minetest
.after(2.5, function()
395 minetest
.register_globalstep(function(dtime
)
396 main_timer
= main_timer
+ dtime
397 timer
= timer
+ dtime
398 timer2
= timer2
+ dtime
399 if main_timer
> HUD_TICK
or timer
> 4 then
400 if main_timer
> HUD_TICK
then main_timer
= 0 end
401 for _
,player
in ipairs(minetest
.get_connected_players()) do
402 local name
= player
:get_player_name()
404 -- only proceed if damage is enabled
405 if minetest
.setting_getbool("enable_damage") then
406 local hp
= player
:get_hp()
408 -- update all hud elements
415 if timer
> 4 then timer
= 0 end