Remove pointless delays and useless code
[minetest_hudbars.git] / init.lua
blobf2689217a8db853b0845333691de28db96654d3f
1 hb = {}
3 hb.hudtables = {}
5 -- number of registered HUD bars
6 hb.hudbars_count = 0
8 hb.settings = {}
10 -- default settings
11 hb.settings.max_bar_length = 160
13 -- statbar positions
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)
23 hb.players = {}
25 function hb.value_to_barlength(value, max)
26 if max == 0 then
27 return 0
28 else
29 return math.ceil((value/max) * hb.settings.max_bar_length)
30 end
31 end
33 function hb.get_hudtable(identifier)
34 return hb.hudtables[identifier]
35 end
37 function hb.register_hudbar(identifier, text_color, label, textures, default_start_value, default_start_max, default_start_hidden, format_string)
38 local hudtable = {}
39 local pos, offset
40 if hb.hudbars_count % 2 == 0 then
41 pos = hb.settings.pos_left
42 offset = {
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)
46 else
47 pos = hb.settings.pos_right
48 offset = {
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)
52 end
53 if format_string == nil then
54 format_string = "%s: %d/%d"
55 end
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
61 local ids = {}
62 local state = {}
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 }
67 else
68 bgscale = { x=1, y=1 }
69 end
70 if start_hidden then
71 iconscale = { x=0, y=0 }
72 barnumber = 0
73 text = ""
74 else
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)
78 end
79 ids.bg = player:hud_add({
80 hud_elem_type = "image",
81 position = pos,
82 scale = bgscale,
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",
90 position = pos,
91 scale = iconscale,
92 text = textures.icon,
93 alignment = {x=-1,y=1},
94 offset = { x = offset.x - 3, y = offset.y },
96 end
97 ids.bar = player:hud_add({
98 hud_elem_type = "statbar",
99 position = pos,
100 text = textures.bar,
101 number = barnumber,
102 alignment = {x=-1,y=-1},
103 offset = offset,
105 ids.text = player:hud_add({
106 hud_elem_type = "text",
107 position = pos,
108 text = text,
109 alignment = {x=1,y=1},
110 number = text_color,
111 direction = 0,
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
118 state.text = text
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
141 hudtable.hudids = {}
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
159 return
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
169 value_changed = true
171 else
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
177 max_changed = true
179 else
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
196 if max_changed then
197 if hudtable.hudstate[name].max == 0 then
198 player:hud_change(hudtable.hudids[name].bg, "scale", {x=0,y=0})
199 else
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
256 local copy = {
257 hidden = ref.hidden,
258 value = ref.value,
259 max = ref.max,
260 text = ref.text,
261 barlength = ref.barlength,
263 return copy
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")
274 if set then
275 dofile(minetest.get_modpath("hudbars").."/hudbars.conf")
276 set:close()
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()
291 local hide_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
301 --air
302 local breath = player:get_breath()
304 if breath == 11 then
305 hb.hide_hudbar(player, "breath")
306 else
307 hb.unhide_hudbar(player, "breath")
308 hb.change_hudbar(player, "breath", math.min(breath, 10))
311 --health
312 hb.change_hudbar(player, "health", player:get_hp())
316 minetest.register_on_joinplayer(function(player)
317 hide_builtin(player)
318 custom_hud(player)
319 hb.players[player:get_player_name()] = player
320 end)
322 minetest.register_on_leaveplayer(function(player)
323 hb.players[player:get_player_name()] = nil
324 end)
326 local main_timer = 0
327 local timer = 0
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
337 update_hud(player)
341 if timer > 4 then timer = 0 end
342 end)