2 local enable_damage = core.settings:get_bool("enable_damage")
4 local health_bar_definition = {
5 hud_elem_type = "statbar",
6 position = {x = 0.5, y = 1},
8 text2 = "heart_gone.png",
9 number = core.PLAYER_MAX_HP_DEFAULT,
10 item = core.PLAYER_MAX_HP_DEFAULT,
12 size = {x = 24, y = 24},
13 offset = {x = (-10 * 24) - 25, y = -(48 + 24 + 16)},
16 local breath_bar_definition = {
17 hud_elem_type = "statbar",
18 position = {x = 0.5, y = 1},
20 text2 = "bubble_gone.png",
21 number = core.PLAYER_MAX_BREATH_DEFAULT,
22 item = core.PLAYER_MAX_BREATH_DEFAULT * 2,
24 size = {x = 24, y = 24},
25 offset = {x = 25, y= -(48 + 24 + 16)},
30 local function scaleToDefault(player, field)
31 -- Scale "hp" or "breath" to the default dimensions
32 local current = player["get_" .. field](player)
33 local nominal = core["PLAYER_MAX_" .. field:upper() .. "_DEFAULT"]
34 local max_display = math.max(nominal,
35 math.max(player:get_properties()[field .. "_max"], current))
36 return current / max_display * nominal
39 local function update_builtin_statbars(player)
40 local name = player:get_player_name()
46 local flags = player:hud_get_flags()
47 if not hud_ids[name] then
49 -- flags are not transmitted to client on connect, we need to make sure
50 -- our current flags are transmitted by sending them actively
51 player:hud_set_flags(flags)
53 local hud = hud_ids[name]
55 local immortal = player:get_armor_groups().immortal == 1
57 if flags.healthbar and enable_damage and not immortal then
58 local number = scaleToDefault(player, "hp")
59 if hud.id_healthbar == nil then
60 local hud_def = table.copy(health_bar_definition)
61 hud_def.number = number
62 hud.id_healthbar = player:hud_add(hud_def)
64 player:hud_change(hud.id_healthbar, "number", number)
66 elseif hud.id_healthbar then
67 player:hud_remove(hud.id_healthbar)
68 hud.id_healthbar = nil
71 local show_breathbar = flags.breathbar and enable_damage and not immortal
73 local breath = player:get_breath()
74 local breath_max = player:get_properties().breath_max
75 if show_breathbar and breath <= breath_max then
76 local number = 2 * scaleToDefault(player, "breath")
77 if not hud.id_breathbar and breath < breath_max then
78 local hud_def = table.copy(breath_bar_definition)
79 hud_def.number = number
80 hud.id_breathbar = player:hud_add(hud_def)
81 elseif hud.id_breathbar then
82 player:hud_change(hud.id_breathbar, "number", number)
86 if hud.id_breathbar and (not show_breathbar or breath == breath_max) then
87 core.after(1, function(player_name, breath_bar)
88 local player = core.get_player_by_name(player_name)
90 player:hud_remove(breath_bar)
92 end, name, hud.id_breathbar)
93 hud.id_breathbar = nil
97 local function cleanup_builtin_statbars(player)
98 local name = player:get_player_name()
107 local function player_event_handler(player,eventname)
108 assert(player:is_player())
110 local name = player:get_player_name()
112 if name == "" or not hud_ids[name] then
116 if eventname == "health_changed" then
117 update_builtin_statbars(player)
119 if hud_ids[name].id_healthbar then
124 if eventname == "breath_changed" then
125 update_builtin_statbars(player)
127 if hud_ids[name].id_breathbar then
132 if eventname == "hud_changed" or eventname == "properties_changed" then
133 update_builtin_statbars(player)
140 function core.hud_replace_builtin(hud_name, definition)
142 if type(definition) ~= "table" or
143 definition.hud_elem_type ~= "statbar" then
147 if hud_name == "health" then
148 health_bar_definition = definition
150 for name, ids in pairs(hud_ids) do
151 local player = core.get_player_by_name(name)
152 if player and ids.id_healthbar then
153 player:hud_remove(ids.id_healthbar)
154 ids.id_healthbar = nil
155 update_builtin_statbars(player)
161 if hud_name == "breath" then
162 breath_bar_definition = definition
164 for name, ids in pairs(hud_ids) do
165 local player = core.get_player_by_name(name)
166 if player and ids.id_breathbar then
167 player:hud_remove(ids.id_breathbar)
168 ids.id_breathbar = nil
169 update_builtin_statbars(player)
178 -- Append "update_builtin_statbars" as late as possible
179 -- This ensures that the HUD is hidden when the flags are updated in this callback
180 core.register_on_mods_loaded(function()
181 core.register_on_joinplayer(update_builtin_statbars)
183 core.register_on_leaveplayer(cleanup_builtin_statbars)
184 core.register_playerevent(player_event_handler)