]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/game/statbars.lua
Fix core.download_file() creating empty files on HTTP error
[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 {
6         hud_elem_type = "statbar",
7         position = { x=0.5, y=1 },
8         text = "heart.png",
9         number = core.PLAYER_MAX_HP_DEFAULT,
10         direction = 0,
11         size = { x=24, y=24 },
12         offset = { x=(-10*24)-25, y=-(48+24+16)},
13 }
14
15 local breath_bar_definition =
16 {
17         hud_elem_type = "statbar",
18         position = { x=0.5, y=1 },
19         text = "bubble.png",
20         number = core.PLAYER_MAX_BREATH_DEFAULT,
21         direction = 0,
22         size = { x=24, y=24 },
23         offset = {x=25,y=-(48+24+16)},
24 }
25
26 local hud_ids = {}
27
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 
35 end
36
37 local function initialize_builtin_statbars(player)
38
39         if not player:is_player() then
40                 return
41         end
42
43         local name = player:get_player_name()
44
45         if name == "" then
46                 return
47         end
48
49         if not hud_ids[name] then
50                 hud_ids[name] = {}
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())
54         end
55         local hud = hud_ids[name]
56
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)
62                 end
63         elseif hud.id_healthbar ~= nil then
64                 player:hud_remove(hud.id_healthbar)
65                 hud.id_healthbar = nil
66         end
67
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)
75                 end
76         elseif hud.id_breathbar ~= nil then
77                 player:hud_remove(hud.id_breathbar)
78                 hud.id_breathbar = nil
79         end
80 end
81
82 local function cleanup_builtin_statbars(player)
83
84         if not player:is_player() then
85                 return
86         end
87
88         local name = player:get_player_name()
89
90         if name == "" then
91                 return
92         end
93
94         hud_ids[name] = nil
95 end
96
97 local function player_event_handler(player,eventname)
98         assert(player:is_player())
99
100         local name = player:get_player_name()
101
102         if name == "" then
103                 return
104         end
105
106         if eventname == "health_changed" then
107                 initialize_builtin_statbars(player)
108
109                 if hud_ids[name].id_healthbar ~= nil then
110                         player:hud_change(hud_ids[name].id_healthbar,
111                                 "number", scaleToDefault(player, "hp"))
112                         return true
113                 end
114         end
115
116         if eventname == "breath_changed" then
117                 initialize_builtin_statbars(player)
118
119                 if hud_ids[name].id_breathbar ~= nil then
120                         player:hud_change(hud_ids[name].id_breathbar,
121                                 "number", 2 * scaleToDefault(player, "breath"))
122                         return true
123                 end
124         end
125
126         if eventname == "hud_changed" then
127                 initialize_builtin_statbars(player)
128                 return true
129         end
130
131         return false
132 end
133
134 function core.hud_replace_builtin(name, definition)
135
136         if definition == nil or
137                 type(definition) ~= "table" or
138                 definition.hud_elem_type ~= "statbar" then
139                 return false
140         end
141
142         if name == "health" then
143                 health_bar_definition = definition
144
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)
150                         end
151                 end
152                 return true
153         end
154
155         if name == "breath" then
156                 breath_bar_definition = definition
157
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)
163                         end
164                 end
165                 return true
166         end
167
168         return false
169 end
170
171 core.register_on_joinplayer(initialize_builtin_statbars)
172 core.register_on_leaveplayer(cleanup_builtin_statbars)
173 core.register_playerevent(player_event_handler)