2 local health_bar_definition =
4 hud_elem_type = "statbar",
5 position = { x=0.5, y=1 },
10 offset = { x=(-10*24)-25, y=-(48+24+16)},
13 local breath_bar_definition =
15 hud_elem_type = "statbar",
16 position = { x=0.5, y=1 },
20 size = { x=24, y=24 },
21 offset = {x=25,y=-(48+24+16)},
26 local function initialize_builtin_statbars(player)
28 if not player:is_player() then
32 local name = player:get_player_name()
38 if (hud_ids[name] == nil) then
40 -- flags are not transmitted to client on connect, we need to make sure
41 -- our current flags are transmitted by sending them actively
42 player:hud_set_flags(player:hud_get_flags())
45 if player:hud_get_flags().healthbar and
46 core.is_yes(core.setting_get("enable_damage")) then
47 if hud_ids[name].id_healthbar == nil then
48 health_bar_definition.number = player:get_hp()
49 hud_ids[name].id_healthbar = player:hud_add(health_bar_definition)
52 if hud_ids[name].id_healthbar ~= nil then
53 player:hud_remove(hud_ids[name].id_healthbar)
54 hud_ids[name].id_healthbar = nil
58 if (player:get_breath() < 11) then
59 if player:hud_get_flags().breathbar and
60 core.is_yes(core.setting_get("enable_damage")) then
61 if hud_ids[name].id_breathbar == nil then
62 hud_ids[name].id_breathbar = player:hud_add(breath_bar_definition)
65 if hud_ids[name].id_breathbar ~= nil then
66 player:hud_remove(hud_ids[name].id_breathbar)
67 hud_ids[name].id_breathbar = nil
70 elseif hud_ids[name].id_breathbar ~= nil then
71 player:hud_remove(hud_ids[name].id_breathbar)
72 hud_ids[name].id_breathbar = nil
76 local function cleanup_builtin_statbars(player)
78 if not player:is_player() then
82 local name = player:get_player_name()
91 local function player_event_handler(player,eventname)
92 assert(player:is_player())
94 local name = player:get_player_name()
100 if eventname == "health_changed" then
101 initialize_builtin_statbars(player)
103 if hud_ids[name].id_healthbar ~= nil then
104 player:hud_change(hud_ids[name].id_healthbar,"number",player:get_hp())
109 if eventname == "breath_changed" then
110 initialize_builtin_statbars(player)
112 if hud_ids[name].id_breathbar ~= nil then
113 player:hud_change(hud_ids[name].id_breathbar,"number",player:get_breath()*2)
118 if eventname == "hud_changed" then
119 initialize_builtin_statbars(player)
126 function core.hud_replace_builtin(name, definition)
128 if definition == nil or
129 type(definition) ~= "table" or
130 definition.hud_elem_type ~= "statbar" then
134 if name == "health" then
135 health_bar_definition = definition
137 for name,ids in pairs(hud_ids) do
138 local player = core.get_player_by_name(name)
139 if player and hud_ids[name].id_healthbar then
140 player:hud_remove(hud_ids[name].id_healthbar)
141 initialize_builtin_statbars(player)
147 if name == "breath" then
148 breath_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 hud_ids[name].id_breathbar then
153 player:hud_remove(hud_ids[name].id_breathbar)
154 initialize_builtin_statbars(player)
163 core.register_on_joinplayer(initialize_builtin_statbars)
164 core.register_on_leaveplayer(cleanup_builtin_statbars)
165 core.register_playerevent(player_event_handler)