Skip to content

Commit eea1fda

Browse files
SmallJokerparamat
authored andcommittedFeb 12, 2019
Statbars: Reduce initial update interval
Centralize HUD updating in update_builtin_statbars. Fixes race condition in 'on joinplayer' that causes stat bar bugs. Correctly scales stat bars to custom max values set by mods.
1 parent ffb17f1 commit eea1fda

File tree

1 file changed

+38
-40
lines changed

1 file changed

+38
-40
lines changed
 

Diff for: ‎builtin/game/statbars.lua

+38-40
Original file line numberDiff line numberDiff line change
@@ -34,57 +34,54 @@ local function scaleToDefault(player, field)
3434
return current / max_display * nominal
3535
end
3636

37-
local function initialize_builtin_statbars(player)
38-
39-
if not player:is_player() then
40-
return
41-
end
42-
37+
local function update_builtin_statbars(player)
4338
local name = player:get_player_name()
4439

4540
if name == "" then
4641
return
4742
end
4843

44+
local flags = player:hud_get_flags()
4945
if not hud_ids[name] then
5046
hud_ids[name] = {}
5147
-- flags are not transmitted to client on connect, we need to make sure
5248
-- our current flags are transmitted by sending them actively
53-
player:hud_set_flags(player:hud_get_flags())
49+
player:hud_set_flags(flags)
5450
end
5551
local hud = hud_ids[name]
5652

57-
if player:hud_get_flags().healthbar and enable_damage then
53+
if flags.healthbar and enable_damage then
54+
local number = scaleToDefault(player, "hp")
5855
if hud.id_healthbar == nil then
5956
local hud_def = table.copy(health_bar_definition)
60-
hud_def.number = scaleToDefault(player, "hp")
57+
hud_def.number = number
6158
hud.id_healthbar = player:hud_add(hud_def)
59+
else
60+
player:hud_change(hud.id_healthbar, "number", number)
6261
end
63-
elseif hud.id_healthbar ~= nil then
62+
elseif hud.id_healthbar then
6463
player:hud_remove(hud.id_healthbar)
6564
hud.id_healthbar = nil
6665
end
6766

6867
local breath_max = player:get_properties().breath_max
69-
if player:hud_get_flags().breathbar and enable_damage and
68+
if flags.breathbar and enable_damage and
7069
player:get_breath() < breath_max then
70+
local number = 2 * scaleToDefault(player, "breath")
7171
if hud.id_breathbar == nil then
7272
local hud_def = table.copy(breath_bar_definition)
73-
hud_def.number = 2 * scaleToDefault(player, "breath")
73+
hud_def.number = number
7474
hud.id_breathbar = player:hud_add(hud_def)
75+
else
76+
player:hud_change(hud.id_breathbar, "number", number)
7577
end
76-
elseif hud.id_breathbar ~= nil then
78+
elseif hud.id_breathbar then
7779
player:hud_remove(hud.id_breathbar)
7880
hud.id_breathbar = nil
7981
end
8082
end
8183

8284
local function cleanup_builtin_statbars(player)
83-
84-
if not player:is_player() then
85-
return
86-
end
87-
8885
local name = player:get_player_name()
8986

9087
if name == "" then
@@ -99,32 +96,28 @@ local function player_event_handler(player,eventname)
9996

10097
local name = player:get_player_name()
10198

102-
if name == "" then
99+
if name == "" or not hud_ids[name] then
103100
return
104101
end
105102

106103
if eventname == "health_changed" then
107-
initialize_builtin_statbars(player)
104+
update_builtin_statbars(player)
108105

109-
if hud_ids[name].id_healthbar ~= nil then
110-
player:hud_change(hud_ids[name].id_healthbar,
111-
"number", scaleToDefault(player, "hp"))
106+
if hud_ids[name].id_healthbar then
112107
return true
113108
end
114109
end
115110

116111
if eventname == "breath_changed" then
117-
initialize_builtin_statbars(player)
112+
update_builtin_statbars(player)
118113

119-
if hud_ids[name].id_breathbar ~= nil then
120-
player:hud_change(hud_ids[name].id_breathbar,
121-
"number", 2 * scaleToDefault(player, "breath"))
114+
if hud_ids[name].id_breathbar then
122115
return true
123116
end
124117
end
125118

126119
if eventname == "hud_changed" then
127-
initialize_builtin_statbars(player)
120+
update_builtin_statbars(player)
128121
return true
129122
end
130123

@@ -133,20 +126,20 @@ end
133126

134127
function core.hud_replace_builtin(name, definition)
135128

136-
if definition == nil or
137-
type(definition) ~= "table" or
138-
definition.hud_elem_type ~= "statbar" then
129+
if type(definition) ~= "table" or
130+
definition.hud_elem_type ~= "statbar" then
139131
return false
140132
end
141133

142134
if name == "health" then
143135
health_bar_definition = definition
144136

145-
for name,ids in pairs(hud_ids) do
137+
for name, ids in pairs(hud_ids) do
146138
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)
139+
if player and ids.id_healthbar then
140+
player:hud_remove(ids.id_healthbar)
141+
ids.id_healthbar = nil
142+
update_builtin_statbars(player)
150143
end
151144
end
152145
return true
@@ -155,11 +148,12 @@ function core.hud_replace_builtin(name, definition)
155148
if name == "breath" then
156149
breath_bar_definition = definition
157150

158-
for name,ids in pairs(hud_ids) do
151+
for name, ids in pairs(hud_ids) do
159152
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)
153+
if player and ids.id_breathbar then
154+
player:hud_remove(ids.id_breathbar)
155+
ids.id_breathbar = nil
156+
update_builtin_statbars(player)
163157
end
164158
end
165159
return true
@@ -168,6 +162,10 @@ function core.hud_replace_builtin(name, definition)
168162
return false
169163
end
170164

171-
core.register_on_joinplayer(initialize_builtin_statbars)
165+
-- Append "update_builtin_statbars" as late as possible
166+
-- This ensures that the HUD is hidden when the flags are updated in this callback
167+
core.register_on_mods_loaded(function()
168+
core.register_on_joinplayer(update_builtin_statbars)
169+
end)
172170
core.register_on_leaveplayer(cleanup_builtin_statbars)
173171
core.register_playerevent(player_event_handler)

0 commit comments

Comments
 (0)
Please sign in to comment.