Rename “start_hide” to “start_hidden”
[minetest_hudbars.git] / init.lua
blobde37e380a5af72ae01977da02f5cd8b85fefb5b4
1 hb = {}
3 -- HUD statbar values
4 hb.health = {}
5 hb.air = {}
7 hb.hudtables = {}
9 -- number of registered HUD bars
10 hb.hudbars_count = 0
12 -- HUD item ids
13 local health_hud = {}
14 local health_hud_text = {}
15 local health_hud_icon = {}
16 local health_hud_bg = {}
17 local air_hud = {}
18 local air_hud_text = {}
19 local air_hud_icon = {}
20 local air_hud_bg = {}
22 -- default settings
24 HUD_SCALEABLE = false
25 HUD_BARLENGTH = 160
27 -- statbar positions
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
35 HUD_SCALEABLE = true
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}
40 end
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}
49 else
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 }
52 end
54 HUD_TICK = 0.1
56 function hb.value_to_barlength(value, max)
57 if max == 0 then
58 return 0
59 else
60 return math.ceil((value/max) * HUD_BARLENGTH)
61 end
62 end
64 function hb.get_hudtable(identifier)
65 return hb.hudtables[identifier]
66 end
68 function hb.register_hudbar(identifier, text_color, label, textures, default_start_value, default_start_max, start_hidden, format_string)
69 local hudtable = {}
70 local pos, offset
71 if hb.hudbars_count % 2 == 0 then
72 pos = HUD_CUSTOM_POS_LEFT
73 offset = {
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)
77 else
78 pos = HUD_CUSTOM_POS_RIGHT
79 offset = {
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)
83 end
84 if format_string == nil then
85 format_string = "%s: %d/%d"
86 end
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
91 local ids = {}
92 local state = {}
93 local name = player:get_player_name()
94 local bgscale
95 if start_max == 0 then
96 bgscale = { x=0, y=0 }
97 else
98 bgscale = { x=1, y=1 }
99 end
100 ids.bg = player:hud_add({
101 hud_elem_type = "image",
102 position = pos,
103 scale = bgscale,
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",
111 position = pos,
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",
120 position = pos,
121 text = textures.bar,
122 number = hb.value_to_barlength(start_value, start_max),
123 alignment = {x=-1,y=-1},
124 offset = offset,
126 local text = string.format(format_string, label, start_value, start_max)
127 ids.text = player:hud_add({
128 hud_elem_type = "text",
129 position = pos,
130 text = text,
131 alignment = {x=1,y=1},
132 number = text_color,
133 direction = 0,
134 offset = { x = offset.x + 2, y = offset.y },
136 state.hidden = start_hidden
137 state.value = start_value
138 state.max = start_max
139 state.text = text
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
149 hudtable.hudids = {}
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
163 return
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
173 value_changed = true
175 else
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
181 max_changed = true
183 else
184 new_max_value = hudtable.hudstate[name].max
187 if hudtable.hudstate[name].hidden == false then
188 if max_changed then
189 if hudtable.hudstate[name].max == 0 then
190 player:hud_change(hudtable.hudids[name].bg, "scale", {x=0,y=0})
191 else
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")
244 if set then
245 dofile(minetest.get_modpath("hudbars").."/hudbars.conf")
246 set:close()
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
258 --health
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},
288 number = 0xFFFFFF,
289 direction = 0,
290 offset = { x = HUD_HEALTH_OFFSET.x + 2, y = HUD_HEALTH_OFFSET.y },
293 --air
294 local airnumber, airtext, airscale
295 local air = player:get_breath()
296 if air == 11 then
297 airnumber = 0
298 airtext = ""
299 airscale = {x=0, y=0}
300 else
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,
308 scale = airscale,
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,
316 scale = airscale,
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",
325 number = airnumber,
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,
332 text = airtext,
333 alignment = {x=1,y=1},
334 number = 0xFFFFFF,
335 direction = 0,
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()
346 --air
347 local air = tonumber(hb.air[name])
348 if player:get_breath() ~= air then
349 air = player:get_breath()
350 hb.air[name] = air
351 local airnumber, airtext, airscale
352 if air == 11 then
353 airnumber = 0
354 airtext = ""
355 airscale = {x=0, y=0}
356 else
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)
366 --health
367 local hp = tonumber(hb.health[name])
368 if player:get_hp() ~= hp then
369 hp = player:get_hp()
370 hb.health[name] = hp
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()
384 hb.air[name] = air
385 minetest.after(0.5, function()
386 hide_builtin(player)
387 custom_hud(player)
388 end)
389 end)
391 local main_timer = 0
392 local timer = 0
393 local timer2 = 0
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
409 update_hud(player)
415 if timer > 4 then timer = 0 end
416 end)
417 end)