]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/game/statbars.lua
Predict failing placement of ignore nodes
[dragonfireclient.git] / builtin / game / statbars.lua
1 -- cache setting
2 local enable_damage = core.settings:get_bool("enable_damage")
3
4 local health_bar_definition = {
5         hud_elem_type = "statbar",
6         position = {x = 0.5, y = 1},
7         text = "heart.png",
8         text2 = "heart_gone.png",
9         number = core.PLAYER_MAX_HP_DEFAULT,
10         item = core.PLAYER_MAX_HP_DEFAULT,
11         direction = 0,
12         size = {x = 24, y = 24},
13         offset = {x = (-10 * 24) - 25, y = -(48 + 24 + 16)},
14 }
15
16 local breath_bar_definition = {
17         hud_elem_type = "statbar",
18         position = {x = 0.5, y = 1},
19         text = "bubble.png",
20         text2 = "bubble_gone.png",
21         number = core.PLAYER_MAX_BREATH_DEFAULT,
22         item = core.PLAYER_MAX_BREATH_DEFAULT * 2,
23         direction = 0,
24         size = {x = 24, y = 24},
25         offset = {x = 25, y= -(48 + 24 + 16)},
26 }
27
28 local hud_ids = {}
29
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
37 end
38
39 local function update_builtin_statbars(player)
40         local name = player:get_player_name()
41
42         if name == "" then
43                 return
44         end
45
46         local flags = player:hud_get_flags()
47         if not hud_ids[name] then
48                 hud_ids[name] = {}
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)
52         end
53         local hud = hud_ids[name]
54
55         local immortal = player:get_armor_groups().immortal == 1
56
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)
63                 else
64                         player:hud_change(hud.id_healthbar, "number", number)
65                 end
66         elseif hud.id_healthbar then
67                 player:hud_remove(hud.id_healthbar)
68                 hud.id_healthbar = nil
69         end
70
71         local show_breathbar = flags.breathbar and enable_damage and not immortal
72
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)
83                 end
84         end
85
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)
89                         if player then
90                                 player:hud_remove(breath_bar)
91                         end
92                 end, name, hud.id_breathbar)
93                 hud.id_breathbar = nil
94         end
95 end
96
97 local function cleanup_builtin_statbars(player)
98         local name = player:get_player_name()
99
100         if name == "" then
101                 return
102         end
103
104         hud_ids[name] = nil
105 end
106
107 local function player_event_handler(player,eventname)
108         assert(player:is_player())
109
110         local name = player:get_player_name()
111
112         if name == "" or not hud_ids[name] then
113                 return
114         end
115
116         if eventname == "health_changed" then
117                 update_builtin_statbars(player)
118
119                 if hud_ids[name].id_healthbar then
120                         return true
121                 end
122         end
123
124         if eventname == "breath_changed" then
125                 update_builtin_statbars(player)
126
127                 if hud_ids[name].id_breathbar then
128                         return true
129                 end
130         end
131
132         if eventname == "hud_changed" or eventname == "properties_changed" then
133                 update_builtin_statbars(player)
134                 return true
135         end
136
137         return false
138 end
139
140 function core.hud_replace_builtin(hud_name, definition)
141
142         if type(definition) ~= "table" or
143                         definition.hud_elem_type ~= "statbar" then
144                 return false
145         end
146
147         if hud_name == "health" then
148                 health_bar_definition = definition
149
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)
156                         end
157                 end
158                 return true
159         end
160
161         if hud_name == "breath" then
162                 breath_bar_definition = definition
163
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)
170                         end
171                 end
172                 return true
173         end
174
175         return false
176 end
177
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)
182 end)
183 core.register_on_leaveplayer(cleanup_builtin_statbars)
184 core.register_playerevent(player_event_handler)