2 local enable_damage = core.settings:get_bool("enable_damage")
4 local health_bar_definition =
6 hud_elem_type = "statbar",
7 position = { x=0.5, y=1 },
9 number = core.PLAYER_MAX_HP_DEFAULT,
11 size = { x=24, y=24 },
12 offset = { x=(-10*24)-25, y=-(48+24+16)},
15 local breath_bar_definition =
17 hud_elem_type = "statbar",
18 position = { x=0.5, y=1 },
20 number = core.PLAYER_MAX_BREATH_DEFAULT,
22 size = { x=24, y=24 },
23 offset = {x=25,y=-(48+24+16)},
28 local function scaleToDefault(player, field)
29 -- Scale "hp" or "breath" to the default dimensions
30 local current = player["get_" .. field](player)
31 local nominal = core["PLAYER_MAX_".. field:upper() .. "_DEFAULT"]
32 local max_display = math.max(nominal,
33 math.max(player:get_properties()[field .. "_max"], current))
34 return current / max_display * nominal
37 local function initialize_builtin_statbars(player)
39 if not player:is_player() then
43 local name = player:get_player_name()
49 if not hud_ids[name] then
51 -- flags are not transmitted to client on connect, we need to make sure
52 -- our current flags are transmitted by sending them actively
53 player:hud_set_flags(player:hud_get_flags())
55 local hud = hud_ids[name]
57 if player:hud_get_flags().healthbar and enable_damage then
58 if hud.id_healthbar == nil then
59 local hud_def = table.copy(health_bar_definition)
60 hud_def.number = scaleToDefault(player, "hp")
61 hud.id_healthbar = player:hud_add(hud_def)
63 elseif hud.id_healthbar ~= nil then
64 player:hud_remove(hud.id_healthbar)
65 hud.id_healthbar = nil
68 local breath_max = player:get_properties().breath_max
69 if player:hud_get_flags().breathbar and enable_damage and
70 player:get_breath() < breath_max then
71 if hud.id_breathbar == nil then
72 local hud_def = table.copy(breath_bar_definition)
73 hud_def.number = 2 * scaleToDefault(player, "breath")
74 hud.id_breathbar = player:hud_add(hud_def)
76 elseif hud.id_breathbar ~= nil then
77 player:hud_remove(hud.id_breathbar)
78 hud.id_breathbar = nil
82 local function cleanup_builtin_statbars(player)
84 if not player:is_player() then
88 local name = player:get_player_name()
97 local function player_event_handler(player,eventname)
98 assert(player:is_player())
100 local name = player:get_player_name()
106 if eventname == "health_changed" then
107 initialize_builtin_statbars(player)
109 if hud_ids[name].id_healthbar ~= nil then
110 player:hud_change(hud_ids[name].id_healthbar,
111 "number", scaleToDefault(player, "hp"))
116 if eventname == "breath_changed" then
117 initialize_builtin_statbars(player)
119 if hud_ids[name].id_breathbar ~= nil then
120 player:hud_change(hud_ids[name].id_breathbar,
121 "number", 2 * scaleToDefault(player, "breath"))
126 if eventname == "hud_changed" then
127 initialize_builtin_statbars(player)
134 function core.hud_replace_builtin(name, definition)
136 if definition == nil or
137 type(definition) ~= "table" or
138 definition.hud_elem_type ~= "statbar" then
142 if name == "health" then
143 health_bar_definition = definition
145 for name,ids in pairs(hud_ids) do
146 local player = core.get_player_by_name(name)
147 if player and hud_ids[name].id_healthbar then
148 player:hud_remove(hud_ids[name].id_healthbar)
149 initialize_builtin_statbars(player)
155 if name == "breath" then
156 breath_bar_definition = definition
158 for name,ids in pairs(hud_ids) do
159 local player = core.get_player_by_name(name)
160 if player and hud_ids[name].id_breathbar then
161 player:hud_remove(hud_ids[name].id_breathbar)
162 initialize_builtin_statbars(player)
171 core.register_on_joinplayer(initialize_builtin_statbars)
172 core.register_on_leaveplayer(cleanup_builtin_statbars)
173 core.register_playerevent(player_event_handler)