1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#include "hud.h"
4
5#include <base/color.h>
6#include <base/time.h>
7
8#include <engine/font_icons.h>
9#include <engine/graphics.h>
10#include <engine/shared/config.h>
11#include <engine/textrender.h>
12
13#include <generated/client_data.h>
14#include <generated/protocol.h>
15
16#include <game/client/animstate.h>
17#include <game/client/components/camera.h>
18#include <game/client/components/controls.h>
19#include <game/client/components/scoreboard.h>
20#include <game/client/components/voting.h>
21#include <game/client/gameclient.h>
22#include <game/client/prediction/entities/character.h>
23#include <game/layers.h>
24#include <game/localization.h>
25
26#include <cmath>
27
28CHud::CHud()
29{
30 m_FPSTextContainerIndex.Reset();
31 m_DDRaceEffectsTextContainerIndex.Reset();
32 m_PlayerAngleTextContainerIndex.Reset();
33 m_PlayerPrevAngle = -INFINITY;
34
35 for(int i = 0; i < 2; i++)
36 {
37 m_aPlayerSpeedTextContainers[i].Reset();
38 m_aPlayerPrevSpeed[i] = -INFINITY;
39 m_aPlayerPositionContainers[i].Reset();
40 m_aPlayerPrevPosition[i] = -INFINITY;
41 }
42}
43
44void CHud::ResetHudContainers()
45{
46 for(auto &ScoreInfo : m_aScoreInfo)
47 {
48 TextRender()->DeleteTextContainer(TextContainerIndex&: ScoreInfo.m_OptionalNameTextContainerIndex);
49 TextRender()->DeleteTextContainer(TextContainerIndex&: ScoreInfo.m_TextRankContainerIndex);
50 TextRender()->DeleteTextContainer(TextContainerIndex&: ScoreInfo.m_TextScoreContainerIndex);
51 Graphics()->DeleteQuadContainer(ContainerIndex&: ScoreInfo.m_RoundRectQuadContainerIndex);
52
53 ScoreInfo.Reset();
54 }
55
56 TextRender()->DeleteTextContainer(TextContainerIndex&: m_FPSTextContainerIndex);
57 TextRender()->DeleteTextContainer(TextContainerIndex&: m_DDRaceEffectsTextContainerIndex);
58 TextRender()->DeleteTextContainer(TextContainerIndex&: m_PlayerAngleTextContainerIndex);
59 m_PlayerPrevAngle = -INFINITY;
60 for(int i = 0; i < 2; i++)
61 {
62 TextRender()->DeleteTextContainer(TextContainerIndex&: m_aPlayerSpeedTextContainers[i]);
63 m_aPlayerPrevSpeed[i] = -INFINITY;
64 TextRender()->DeleteTextContainer(TextContainerIndex&: m_aPlayerPositionContainers[i]);
65 m_aPlayerPrevPosition[i] = -INFINITY;
66 }
67}
68
69void CHud::OnWindowResize()
70{
71 ResetHudContainers();
72}
73
74void CHud::OnReset()
75{
76 m_TimeCpDiff = 0.0f;
77 m_DDRaceTime = 0;
78 m_FinishTimeLastReceivedTick = 0;
79 m_TimeCpLastReceivedTick = 0;
80 m_ShowFinishTime = false;
81 m_aPlayerRecord[0] = -1.0f;
82 m_aPlayerRecord[1] = -1.0f;
83 m_aPlayerSpeed[0] = 0;
84 m_aPlayerSpeed[1] = 0;
85 m_aLastPlayerSpeedChange[0] = ESpeedChange::NONE;
86 m_aLastPlayerSpeedChange[1] = ESpeedChange::NONE;
87 m_LastSpectatorCountTick = 0;
88
89 ResetHudContainers();
90}
91
92void CHud::OnInit()
93{
94 OnReset();
95
96 Graphics()->SetColor(r: 1.0, g: 1.0, b: 1.0, a: 1.0);
97
98 m_HudQuadContainerIndex = Graphics()->CreateQuadContainer(AutomaticUpload: false);
99 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 0, BottomRightU: 1, BottomRightV: 1);
100 PrepareAmmoHealthAndArmorQuads();
101
102 // all cursors for the different weapons
103 for(int i = 0; i < NUM_WEAPONS; ++i)
104 {
105 float ScaleX, ScaleY;
106 Graphics()->GetSpriteScale(pSprite: g_pData->m_Weapons.m_aId[i].m_pSpriteCursor, ScaleX, ScaleY);
107 m_aCursorOffset[i] = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_HudQuadContainerIndex, Width: 64.f * ScaleX, Height: 64.f * ScaleY);
108 }
109
110 // the flags
111 m_FlagOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_HudQuadContainerIndex, X: 0.f, Y: 0.f, Width: 8.f, Height: 16.f);
112
113 PreparePlayerStateQuads();
114
115 Graphics()->QuadContainerUpload(ContainerIndex: m_HudQuadContainerIndex);
116}
117
118void CHud::RenderGameTimer()
119{
120 float Half = m_Width / 2.0f;
121
122 if(!(GameClient()->m_Snap.m_pGameInfoObj->m_GameStateFlags & GAMESTATEFLAG_SUDDENDEATH))
123 {
124 char aBuf[32];
125 int Time = 0;
126 if(GameClient()->m_Snap.m_pGameInfoObj->m_TimeLimit && (GameClient()->m_Snap.m_pGameInfoObj->m_WarmupTimer <= 0))
127 {
128 Time = GameClient()->m_Snap.m_pGameInfoObj->m_TimeLimit * 60 - ((Client()->GameTick(Conn: g_Config.m_ClDummy) - GameClient()->m_Snap.m_pGameInfoObj->m_RoundStartTick) / Client()->GameTickSpeed());
129
130 if(GameClient()->m_Snap.m_pGameInfoObj->m_GameStateFlags & GAMESTATEFLAG_GAMEOVER)
131 Time = 0;
132 }
133 else if(GameClient()->m_Snap.m_pGameInfoObj->m_GameStateFlags & GAMESTATEFLAG_RACETIME)
134 {
135 // The Warmup timer is negative in this case to make sure that incompatible clients will not see a warmup timer
136 Time = (Client()->GameTick(Conn: g_Config.m_ClDummy) + GameClient()->m_Snap.m_pGameInfoObj->m_WarmupTimer) / Client()->GameTickSpeed();
137 }
138 else
139 {
140 Time = (Client()->GameTick(Conn: g_Config.m_ClDummy) - GameClient()->m_Snap.m_pGameInfoObj->m_RoundStartTick) / Client()->GameTickSpeed();
141 }
142
143 str_time(centisecs: (int64_t)Time * 100, format: ETimeFormat::DAYS, buffer: aBuf, buffer_size: sizeof(aBuf));
144 float FontSize = 10.0f;
145 static float s_TextWidthM = TextRender()->TextWidth(Size: FontSize, pText: "00:00", StrLength: -1, LineWidth: -1.0f);
146 static float s_TextWidthH = TextRender()->TextWidth(Size: FontSize, pText: "00:00:00", StrLength: -1, LineWidth: -1.0f);
147 static float s_TextWidth0D = TextRender()->TextWidth(Size: FontSize, pText: "0d 00:00:00", StrLength: -1, LineWidth: -1.0f);
148 static float s_TextWidth00D = TextRender()->TextWidth(Size: FontSize, pText: "00d 00:00:00", StrLength: -1, LineWidth: -1.0f);
149 static float s_TextWidth000D = TextRender()->TextWidth(Size: FontSize, pText: "000d 00:00:00", StrLength: -1, LineWidth: -1.0f);
150 float w = Time >= 3600 * 24 * 100 ? s_TextWidth000D : (Time >= 3600 * 24 * 10 ? s_TextWidth00D : (Time >= 3600 * 24 ? s_TextWidth0D : (Time >= 3600 ? s_TextWidthH : s_TextWidthM)));
151 // last 60 sec red, last 10 sec blink
152 if(GameClient()->m_Snap.m_pGameInfoObj->m_TimeLimit && Time <= 60 && (GameClient()->m_Snap.m_pGameInfoObj->m_WarmupTimer <= 0))
153 {
154 float Alpha = Time <= 10 && (2 * time() / time_freq()) % 2 ? 0.5f : 1.0f;
155 TextRender()->TextColor(r: 1.0f, g: 0.25f, b: 0.25f, a: Alpha);
156 }
157 TextRender()->Text(x: Half - w / 2, y: 2, Size: FontSize, pText: aBuf, LineWidth: -1.0f);
158 TextRender()->TextColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f);
159 }
160}
161
162void CHud::RenderPauseNotification()
163{
164 if(GameClient()->m_Snap.m_pGameInfoObj->m_GameStateFlags & GAMESTATEFLAG_PAUSED &&
165 !(GameClient()->m_Snap.m_pGameInfoObj->m_GameStateFlags & GAMESTATEFLAG_GAMEOVER))
166 {
167 const char *pText = Localize(pStr: "Game paused");
168 float FontSize = 20.0f;
169 float w = TextRender()->TextWidth(Size: FontSize, pText, StrLength: -1, LineWidth: -1.0f);
170 TextRender()->Text(x: 150.0f * Graphics()->ScreenAspect() + -w / 2.0f, y: 50.0f, Size: FontSize, pText, LineWidth: -1.0f);
171 }
172}
173
174void CHud::RenderSuddenDeath()
175{
176 if(GameClient()->m_Snap.m_pGameInfoObj->m_GameStateFlags & GAMESTATEFLAG_SUDDENDEATH)
177 {
178 float Half = m_Width / 2.0f;
179 const char *pText = Localize(pStr: "Sudden Death");
180 float FontSize = 12.0f;
181 float w = TextRender()->TextWidth(Size: FontSize, pText, StrLength: -1, LineWidth: -1.0f);
182 TextRender()->Text(x: Half - w / 2, y: 2, Size: FontSize, pText, LineWidth: -1.0f);
183 }
184}
185
186void CHud::RenderScoreHud()
187{
188 // render small score hud
189 if(!(GameClient()->m_Snap.m_pGameInfoObj->m_GameStateFlags & GAMESTATEFLAG_GAMEOVER))
190 {
191 float StartY = 229.0f; // the height of this display is 56, so EndY is 285
192
193 const float ScoreSingleBoxHeight = 18.0f;
194
195 bool ForceScoreInfoInit = !m_aScoreInfo[0].m_Initialized || !m_aScoreInfo[1].m_Initialized;
196 m_aScoreInfo[0].m_Initialized = m_aScoreInfo[1].m_Initialized = true;
197
198 if(GameClient()->IsTeamPlay() && GameClient()->m_Snap.m_pGameDataObj)
199 {
200 char aScoreTeam[2][16];
201 str_format(buffer: aScoreTeam[TEAM_RED], buffer_size: sizeof(aScoreTeam[TEAM_RED]), format: "%d", GameClient()->m_Snap.m_pGameDataObj->m_TeamscoreRed);
202 str_format(buffer: aScoreTeam[TEAM_BLUE], buffer_size: sizeof(aScoreTeam[TEAM_BLUE]), format: "%d", GameClient()->m_Snap.m_pGameDataObj->m_TeamscoreBlue);
203
204 bool aRecreateTeamScore[2] = {str_comp(a: aScoreTeam[0], b: m_aScoreInfo[0].m_aScoreText) != 0, str_comp(a: aScoreTeam[1], b: m_aScoreInfo[1].m_aScoreText) != 0};
205
206 const int aFlagCarrier[2] = {
207 GameClient()->m_Snap.m_pGameDataObj->m_FlagCarrierRed,
208 GameClient()->m_Snap.m_pGameDataObj->m_FlagCarrierBlue};
209
210 bool RecreateRect = ForceScoreInfoInit;
211 for(int t = 0; t < 2; t++)
212 {
213 if(aRecreateTeamScore[t])
214 {
215 m_aScoreInfo[t].m_ScoreTextWidth = TextRender()->TextWidth(Size: 14.0f, pText: aScoreTeam[t == 0 ? TEAM_RED : TEAM_BLUE], StrLength: -1, LineWidth: -1.0f);
216 str_copy(dst&: m_aScoreInfo[t].m_aScoreText, src: aScoreTeam[t == 0 ? TEAM_RED : TEAM_BLUE]);
217 RecreateRect = true;
218 }
219 }
220
221 static float s_TextWidth100 = TextRender()->TextWidth(Size: 14.0f, pText: "100", StrLength: -1, LineWidth: -1.0f);
222 float ScoreWidthMax = std::max(l: {m_aScoreInfo[0].m_ScoreTextWidth, m_aScoreInfo[1].m_ScoreTextWidth, s_TextWidth100});
223 float Split = 3.0f;
224 float ImageSize = (GameClient()->m_Snap.m_pGameInfoObj->m_GameFlags & GAMEFLAG_FLAGS) ? 16.0f : Split;
225 for(int t = 0; t < 2; t++)
226 {
227 // draw box
228 if(RecreateRect)
229 {
230 Graphics()->DeleteQuadContainer(ContainerIndex&: m_aScoreInfo[t].m_RoundRectQuadContainerIndex);
231
232 if(t == 0)
233 Graphics()->SetColor(r: 0.975f, g: 0.17f, b: 0.17f, a: 0.3f);
234 else
235 Graphics()->SetColor(r: 0.17f, g: 0.46f, b: 0.975f, a: 0.3f);
236 m_aScoreInfo[t].m_RoundRectQuadContainerIndex = Graphics()->CreateRectQuadContainer(x: m_Width - ScoreWidthMax - ImageSize - 2 * Split, y: StartY + t * 20, w: ScoreWidthMax + ImageSize + 2 * Split, h: ScoreSingleBoxHeight, r: 5.0f, Corners: IGraphics::CORNER_L);
237 }
238 Graphics()->TextureClear();
239 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f);
240 if(m_aScoreInfo[t].m_RoundRectQuadContainerIndex != -1)
241 Graphics()->RenderQuadContainer(ContainerIndex: m_aScoreInfo[t].m_RoundRectQuadContainerIndex, QuadDrawNum: -1);
242
243 // draw score
244 if(aRecreateTeamScore[t])
245 {
246 CTextCursor Cursor;
247 Cursor.SetPosition(vec2(m_Width - ScoreWidthMax + (ScoreWidthMax - m_aScoreInfo[t].m_ScoreTextWidth) / 2 - Split, StartY + t * 20 + (18.f - 14.f) / 2.f));
248 Cursor.m_FontSize = 14.0f;
249 TextRender()->RecreateTextContainer(TextContainerIndex&: m_aScoreInfo[t].m_TextScoreContainerIndex, pCursor: &Cursor, pText: aScoreTeam[t]);
250 }
251 if(m_aScoreInfo[t].m_TextScoreContainerIndex.Valid())
252 {
253 ColorRGBA TColor(1.f, 1.f, 1.f, 1.f);
254 ColorRGBA TOutlineColor(0.f, 0.f, 0.f, 0.3f);
255 TextRender()->RenderTextContainer(TextContainerIndex: m_aScoreInfo[t].m_TextScoreContainerIndex, TextColor: TColor, TextOutlineColor: TOutlineColor);
256 }
257
258 if(GameClient()->m_Snap.m_pGameInfoObj->m_GameFlags & GAMEFLAG_FLAGS)
259 {
260 int BlinkTimer = (GameClient()->m_aFlagDropTick[t] != 0 &&
261 (Client()->GameTick(Conn: g_Config.m_ClDummy) - GameClient()->m_aFlagDropTick[t]) / Client()->GameTickSpeed() >= 25) ?
262 10 :
263 20;
264 if(aFlagCarrier[t] == FLAG_ATSTAND || (aFlagCarrier[t] == FLAG_TAKEN && ((Client()->GameTick(Conn: g_Config.m_ClDummy) / BlinkTimer) & 1)))
265 {
266 // draw flag
267 Graphics()->TextureSet(Texture: t == 0 ? GameClient()->m_GameSkin.m_SpriteFlagRed : GameClient()->m_GameSkin.m_SpriteFlagBlue);
268 Graphics()->SetColor(r: 1.f, g: 1.f, b: 1.f, a: 1.f);
269 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_FlagOffset, X: m_Width - ScoreWidthMax - ImageSize, Y: StartY + 1.0f + t * 20);
270 }
271 else if(aFlagCarrier[t] >= 0)
272 {
273 // draw name of the flag holder
274 int Id = aFlagCarrier[t] % MAX_CLIENTS;
275 const char *pName = GameClient()->m_aClients[Id].m_aName;
276 if(str_comp(a: pName, b: m_aScoreInfo[t].m_aPlayerNameText) != 0 || RecreateRect)
277 {
278 str_copy(dst&: m_aScoreInfo[t].m_aPlayerNameText, src: pName);
279
280 float w = TextRender()->TextWidth(Size: 8.0f, pText: pName, StrLength: -1, LineWidth: -1.0f);
281
282 CTextCursor Cursor;
283 Cursor.SetPosition(vec2(std::min(a: m_Width - w - 1.0f, b: m_Width - ScoreWidthMax - ImageSize - 2 * Split), StartY + (t + 1) * 20.0f - 2.0f));
284 Cursor.m_FontSize = 8.0f;
285 TextRender()->RecreateTextContainer(TextContainerIndex&: m_aScoreInfo[t].m_OptionalNameTextContainerIndex, pCursor: &Cursor, pText: pName);
286 }
287
288 if(m_aScoreInfo[t].m_OptionalNameTextContainerIndex.Valid())
289 {
290 ColorRGBA TColor(1.f, 1.f, 1.f, 1.f);
291 ColorRGBA TOutlineColor(0.f, 0.f, 0.f, 0.3f);
292 TextRender()->RenderTextContainer(TextContainerIndex: m_aScoreInfo[t].m_OptionalNameTextContainerIndex, TextColor: TColor, TextOutlineColor: TOutlineColor);
293 }
294
295 // draw tee of the flag holder
296 CTeeRenderInfo TeeInfo = GameClient()->m_aClients[Id].m_RenderInfo;
297 TeeInfo.m_Size = ScoreSingleBoxHeight;
298
299 RenderTools()->RenderIdleTeeAtCenter(pInfo: &TeeInfo, Center: vec2(m_Width - ScoreWidthMax - TeeInfo.m_Size / 2 - Split, StartY + (t * 20) + ScoreSingleBoxHeight / 2.0f));
300 }
301 }
302 StartY += 8.0f;
303 }
304 }
305 else
306 {
307 int Local = -1;
308 int aPos[2] = {1, 2};
309 const CNetObj_PlayerInfo *apPlayerInfo[2] = {nullptr, nullptr};
310 int i = 0;
311 for(int t = 0; t < 2 && i < MAX_CLIENTS && GameClient()->m_Snap.m_apInfoByScore[i]; ++i)
312 {
313 if(GameClient()->m_Snap.m_apInfoByScore[i]->m_Team != TEAM_SPECTATORS)
314 {
315 apPlayerInfo[t] = GameClient()->m_Snap.m_apInfoByScore[i];
316 if(apPlayerInfo[t]->m_ClientId == GameClient()->m_Snap.m_LocalClientId)
317 Local = t;
318 ++t;
319 }
320 }
321 // search local player info if not a spectator, nor within top2 scores
322 if(Local == -1 && GameClient()->m_Snap.m_pLocalInfo && GameClient()->m_Snap.m_pLocalInfo->m_Team != TEAM_SPECTATORS)
323 {
324 for(; i < MAX_CLIENTS && GameClient()->m_Snap.m_apInfoByScore[i]; ++i)
325 {
326 if(GameClient()->m_Snap.m_apInfoByScore[i]->m_Team != TEAM_SPECTATORS)
327 ++aPos[1];
328 if(GameClient()->m_Snap.m_apInfoByScore[i]->m_ClientId == GameClient()->m_Snap.m_LocalClientId)
329 {
330 apPlayerInfo[1] = GameClient()->m_Snap.m_apInfoByScore[i];
331 Local = 1;
332 break;
333 }
334 }
335 }
336 char aScore[2][16];
337 for(int t = 0; t < 2; ++t)
338 {
339 if(apPlayerInfo[t])
340 {
341 if(Client()->IsSixup() && GameClient()->m_Snap.m_pGameInfoObj->m_GameFlags & protocol7::GAMEFLAG_RACE)
342 {
343 str_time(centisecs: absolute(a: static_cast<int64_t>(apPlayerInfo[t]->m_Score)) / 10, format: ETimeFormat::MINS_CENTISECS, buffer: aScore[t], buffer_size: sizeof(aScore[t]));
344 }
345 else if(GameClient()->m_GameInfo.m_TimeScore)
346 {
347 const CGameClient::CClientData &ClientData = GameClient()->m_aClients[apPlayerInfo[t]->m_ClientId];
348 if(GameClient()->m_ReceivedDDNetPlayerFinishTimes && ClientData.m_FinishTimeSeconds != FinishTime::NOT_FINISHED_MILLIS)
349 {
350 const int64_t TimeMillis = static_cast<int64_t>(ClientData.m_FinishTimeSeconds) * 1000 + ClientData.m_FinishTimeMillis % 1000;
351 str_time(centisecs: TimeMillis / 10, format: ETimeFormat::HOURS, buffer: aScore[t], buffer_size: sizeof(aScore[t]));
352 }
353 else if(apPlayerInfo[t]->m_Score != FinishTime::NOT_FINISHED_TIMESCORE)
354 {
355 str_time(centisecs: absolute(a: static_cast<int64_t>(apPlayerInfo[t]->m_Score)) * 100, format: ETimeFormat::HOURS, buffer: aScore[t], buffer_size: sizeof(aScore[t]));
356 }
357 else
358 {
359 aScore[t][0] = 0;
360 }
361 }
362 else
363 {
364 str_format(buffer: aScore[t], buffer_size: sizeof(aScore[t]), format: "%d", apPlayerInfo[t]->m_Score);
365 }
366 }
367 else
368 {
369 aScore[t][0] = 0;
370 }
371 }
372
373 bool RecreateScores = str_comp(a: aScore[0], b: m_aScoreInfo[0].m_aScoreText) != 0 || str_comp(a: aScore[1], b: m_aScoreInfo[1].m_aScoreText) != 0 || m_LastLocalClientId != GameClient()->m_Snap.m_LocalClientId;
374 m_LastLocalClientId = GameClient()->m_Snap.m_LocalClientId;
375
376 bool RecreateRect = ForceScoreInfoInit;
377 for(int t = 0; t < 2; t++)
378 {
379 if(RecreateScores)
380 {
381 m_aScoreInfo[t].m_ScoreTextWidth = TextRender()->TextWidth(Size: 14.0f, pText: aScore[t], StrLength: -1, LineWidth: -1.0f);
382 str_copy(dst&: m_aScoreInfo[t].m_aScoreText, src: aScore[t]);
383 RecreateRect = true;
384 }
385
386 if(apPlayerInfo[t])
387 {
388 int Id = apPlayerInfo[t]->m_ClientId;
389 if(Id >= 0 && Id < MAX_CLIENTS)
390 {
391 const char *pName = GameClient()->m_aClients[Id].m_aName;
392 if(str_comp(a: pName, b: m_aScoreInfo[t].m_aPlayerNameText) != 0)
393 RecreateRect = true;
394 }
395 }
396 else
397 {
398 if(m_aScoreInfo[t].m_aPlayerNameText[0] != 0)
399 RecreateRect = true;
400 }
401
402 char aBuf[16];
403 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%d.", aPos[t]);
404 if(str_comp(a: aBuf, b: m_aScoreInfo[t].m_aRankText) != 0)
405 RecreateRect = true;
406 }
407
408 static float s_TextWidth10 = TextRender()->TextWidth(Size: 14.0f, pText: "10", StrLength: -1, LineWidth: -1.0f);
409 float ScoreWidthMax = std::max(l: {m_aScoreInfo[0].m_ScoreTextWidth, m_aScoreInfo[1].m_ScoreTextWidth, s_TextWidth10});
410 float Split = 3.0f, ImageSize = 16.0f, PosSize = 16.0f;
411
412 for(int t = 0; t < 2; t++)
413 {
414 // draw box
415 if(RecreateRect)
416 {
417 Graphics()->DeleteQuadContainer(ContainerIndex&: m_aScoreInfo[t].m_RoundRectQuadContainerIndex);
418
419 if(t == Local)
420 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 0.25f);
421 else
422 Graphics()->SetColor(r: 0.0f, g: 0.0f, b: 0.0f, a: 0.25f);
423 m_aScoreInfo[t].m_RoundRectQuadContainerIndex = Graphics()->CreateRectQuadContainer(x: m_Width - ScoreWidthMax - ImageSize - 2 * Split - PosSize, y: StartY + t * 20, w: ScoreWidthMax + ImageSize + 2 * Split + PosSize, h: ScoreSingleBoxHeight, r: 5.0f, Corners: IGraphics::CORNER_L);
424 }
425 Graphics()->TextureClear();
426 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f);
427 if(m_aScoreInfo[t].m_RoundRectQuadContainerIndex != -1)
428 Graphics()->RenderQuadContainer(ContainerIndex: m_aScoreInfo[t].m_RoundRectQuadContainerIndex, QuadDrawNum: -1);
429
430 if(RecreateScores)
431 {
432 CTextCursor Cursor;
433 Cursor.SetPosition(vec2(m_Width - ScoreWidthMax + (ScoreWidthMax - m_aScoreInfo[t].m_ScoreTextWidth) - Split, StartY + t * 20 + (18.f - 14.f) / 2.f));
434 Cursor.m_FontSize = 14.0f;
435 TextRender()->RecreateTextContainer(TextContainerIndex&: m_aScoreInfo[t].m_TextScoreContainerIndex, pCursor: &Cursor, pText: aScore[t]);
436 }
437 // draw score
438 if(m_aScoreInfo[t].m_TextScoreContainerIndex.Valid())
439 {
440 ColorRGBA TColor(1.f, 1.f, 1.f, 1.f);
441 ColorRGBA TOutlineColor(0.f, 0.f, 0.f, 0.3f);
442 TextRender()->RenderTextContainer(TextContainerIndex: m_aScoreInfo[t].m_TextScoreContainerIndex, TextColor: TColor, TextOutlineColor: TOutlineColor);
443 }
444
445 if(apPlayerInfo[t])
446 {
447 // draw name
448 int Id = apPlayerInfo[t]->m_ClientId;
449 if(Id >= 0 && Id < MAX_CLIENTS)
450 {
451 const char *pName = GameClient()->m_aClients[Id].m_aName;
452 if(RecreateRect)
453 {
454 str_copy(dst&: m_aScoreInfo[t].m_aPlayerNameText, src: pName);
455
456 CTextCursor Cursor;
457 Cursor.SetPosition(vec2(std::min(a: m_Width - TextRender()->TextWidth(Size: 8.0f, pText: pName) - 1.0f, b: m_Width - ScoreWidthMax - ImageSize - 2 * Split - PosSize), StartY + (t + 1) * 20.0f - 2.0f));
458 Cursor.m_FontSize = 8.0f;
459 TextRender()->RecreateTextContainer(TextContainerIndex&: m_aScoreInfo[t].m_OptionalNameTextContainerIndex, pCursor: &Cursor, pText: pName);
460 }
461
462 if(m_aScoreInfo[t].m_OptionalNameTextContainerIndex.Valid())
463 {
464 ColorRGBA TColor(1.f, 1.f, 1.f, 1.f);
465 ColorRGBA TOutlineColor(0.f, 0.f, 0.f, 0.3f);
466 TextRender()->RenderTextContainer(TextContainerIndex: m_aScoreInfo[t].m_OptionalNameTextContainerIndex, TextColor: TColor, TextOutlineColor: TOutlineColor);
467 }
468
469 // draw tee
470 CTeeRenderInfo TeeInfo = GameClient()->m_aClients[Id].m_RenderInfo;
471 TeeInfo.m_Size = ScoreSingleBoxHeight;
472
473 RenderTools()->RenderIdleTeeAtCenter(pInfo: &TeeInfo, Center: vec2(m_Width - ScoreWidthMax - TeeInfo.m_Size / 2 - Split, StartY + (t * 20) + ScoreSingleBoxHeight / 2.0f));
474 }
475 }
476 else
477 {
478 m_aScoreInfo[t].m_aPlayerNameText[0] = 0;
479 }
480
481 // draw position
482 char aBuf[16];
483 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%d.", aPos[t]);
484 if(RecreateRect)
485 {
486 str_copy(dst&: m_aScoreInfo[t].m_aRankText, src: aBuf);
487
488 CTextCursor Cursor;
489 Cursor.SetPosition(vec2(m_Width - ScoreWidthMax - ImageSize - Split - PosSize, StartY + t * 20 + (18.f - 10.f) / 2.f));
490 Cursor.m_FontSize = 10.0f;
491 TextRender()->RecreateTextContainer(TextContainerIndex&: m_aScoreInfo[t].m_TextRankContainerIndex, pCursor: &Cursor, pText: aBuf);
492 }
493 if(m_aScoreInfo[t].m_TextRankContainerIndex.Valid())
494 {
495 ColorRGBA TColor(1.f, 1.f, 1.f, 1.f);
496 ColorRGBA TOutlineColor(0.f, 0.f, 0.f, 0.3f);
497 TextRender()->RenderTextContainer(TextContainerIndex: m_aScoreInfo[t].m_TextRankContainerIndex, TextColor: TColor, TextOutlineColor: TOutlineColor);
498 }
499
500 StartY += 8.0f;
501 }
502 }
503 }
504}
505
506void CHud::RenderWarmupTimer()
507{
508 if(GameClient()->m_Snap.m_pGameInfoObj->m_WarmupTimer <= 0 ||
509 (GameClient()->m_Snap.m_pGameInfoObj->m_GameStateFlags & GAMESTATEFLAG_RACETIME) != 0)
510 {
511 return;
512 }
513
514 const float FontSize = 20.0f;
515 const char *pTitle = Localize(pStr: "Warmup");
516 TextRender()->Text(x: 150.0f * Graphics()->ScreenAspect() - TextRender()->TextWidth(Size: FontSize, pText: pTitle) / 2.0f, y: 50.0f, Size: FontSize, pText: pTitle);
517
518 const int Seconds = GameClient()->m_Snap.m_pGameInfoObj->m_WarmupTimer / Client()->GameTickSpeed();
519 char aWarmupTime[16];
520 float TextWidth;
521 if(Seconds < 5)
522 {
523 str_format(buffer: aWarmupTime, buffer_size: sizeof(aWarmupTime), format: "%d.%d", Seconds, (GameClient()->m_Snap.m_pGameInfoObj->m_WarmupTimer * 10 / Client()->GameTickSpeed()) % 10);
524 TextWidth = TextRender()->TextWidth(Size: FontSize, pText: "0.0"); // Calculate width with fixed string to avoid slight changes when using aWarmupTime
525 }
526 else
527 {
528 str_format(buffer: aWarmupTime, buffer_size: sizeof(aWarmupTime), format: "%d", Seconds);
529 TextWidth = TextRender()->TextWidth(Size: FontSize, pText: aWarmupTime);
530 }
531 TextRender()->Text(x: 150.0f * Graphics()->ScreenAspect() - TextWidth / 2.0f, y: 75.0f, Size: FontSize, pText: aWarmupTime);
532}
533
534void CHud::RenderTextInfo()
535{
536 int Showfps = g_Config.m_ClShowfps;
537#if defined(CONF_VIDEORECORDER)
538 if(IVideo::Current())
539 Showfps = 0;
540#endif
541 if(Showfps)
542 {
543 char aBuf[16];
544 const int FramesPerSecond = round_to_int(f: 1.0f / Client()->FrameTimeAverage());
545 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%d", FramesPerSecond);
546
547 static float s_TextWidth0 = TextRender()->TextWidth(Size: 12.f, pText: "0", StrLength: -1, LineWidth: -1.0f);
548 static float s_TextWidth00 = TextRender()->TextWidth(Size: 12.f, pText: "00", StrLength: -1, LineWidth: -1.0f);
549 static float s_TextWidth000 = TextRender()->TextWidth(Size: 12.f, pText: "000", StrLength: -1, LineWidth: -1.0f);
550 static float s_TextWidth0000 = TextRender()->TextWidth(Size: 12.f, pText: "0000", StrLength: -1, LineWidth: -1.0f);
551 static float s_TextWidth00000 = TextRender()->TextWidth(Size: 12.f, pText: "00000", StrLength: -1, LineWidth: -1.0f);
552 static const float s_aTextWidth[5] = {s_TextWidth0, s_TextWidth00, s_TextWidth000, s_TextWidth0000, s_TextWidth00000};
553
554 int DigitIndex = GetDigitsIndex(Value: FramesPerSecond, Max: 4);
555
556 CTextCursor Cursor;
557 Cursor.SetPosition(vec2(m_Width - 10 - s_aTextWidth[DigitIndex], 5));
558 Cursor.m_FontSize = 12.0f;
559 auto OldFlags = TextRender()->GetRenderFlags();
560 TextRender()->SetRenderFlags(OldFlags | TEXT_RENDER_FLAG_ONE_TIME_USE);
561 if(m_FPSTextContainerIndex.Valid())
562 TextRender()->RecreateTextContainerSoft(TextContainerIndex&: m_FPSTextContainerIndex, pCursor: &Cursor, pText: aBuf);
563 else
564 TextRender()->CreateTextContainer(TextContainerIndex&: m_FPSTextContainerIndex, pCursor: &Cursor, pText: "0");
565 TextRender()->SetRenderFlags(OldFlags);
566 if(m_FPSTextContainerIndex.Valid())
567 {
568 TextRender()->RenderTextContainer(TextContainerIndex: m_FPSTextContainerIndex, TextColor: TextRender()->DefaultTextColor(), TextOutlineColor: TextRender()->DefaultTextOutlineColor());
569 }
570 }
571 if(g_Config.m_ClShowpred && Client()->State() != IClient::STATE_DEMOPLAYBACK)
572 {
573 char aBuf[64];
574 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%d", Client()->GetPredictionTime());
575 TextRender()->Text(x: m_Width - 10 - TextRender()->TextWidth(Size: 12, pText: aBuf, StrLength: -1, LineWidth: -1.0f), y: Showfps ? 20 : 5, Size: 12, pText: aBuf, LineWidth: -1.0f);
576 }
577}
578
579void CHud::RenderConnectionWarning()
580{
581 if(Client()->ConnectionProblems())
582 {
583 const char *pText = Localize(pStr: "Connection Problems…");
584 float w = TextRender()->TextWidth(Size: 24, pText, StrLength: -1, LineWidth: -1.0f);
585 TextRender()->Text(x: 150 * Graphics()->ScreenAspect() - w / 2, y: 50, Size: 24, pText, LineWidth: -1.0f);
586 }
587}
588
589void CHud::RenderTeambalanceWarning()
590{
591 // render prompt about team-balance
592 bool Flash = time() / (time_freq() / 2) % 2 == 0;
593 if(GameClient()->IsTeamPlay())
594 {
595 int TeamDiff = GameClient()->m_Snap.m_aTeamSize[TEAM_RED] - GameClient()->m_Snap.m_aTeamSize[TEAM_BLUE];
596 if(g_Config.m_ClWarningTeambalance && (TeamDiff >= 2 || TeamDiff <= -2))
597 {
598 const char *pText = Localize(pStr: "Please balance teams!");
599 if(Flash)
600 TextRender()->TextColor(r: 1, g: 1, b: 0.5f, a: 1);
601 else
602 TextRender()->TextColor(r: 0.7f, g: 0.7f, b: 0.2f, a: 1.0f);
603 TextRender()->Text(x: 5, y: 50, Size: 6, pText, LineWidth: -1.0f);
604 TextRender()->TextColor(Color: TextRender()->DefaultTextColor());
605 }
606 }
607}
608
609void CHud::RenderCursor()
610{
611 int CurWeapon = 0;
612 vec2 TargetPos;
613 float Alpha = 1.0f;
614
615 const vec2 Center = GameClient()->m_Camera.m_Center;
616 CScreenRect ScreenRect = Graphics()->MapScreenToWorld(CenterX: Center.x, CenterY: Center.y, ParallaxX: 100.0f, ParallaxY: 100.0f, ParallaxZoom: 100.0f, OffsetX: 0, OffsetY: 0, Aspect: Graphics()->ScreenAspect(), Zoom: 1.0f);
617 Graphics()->MapScreen(ScreenRect);
618
619 if(Client()->State() != IClient::STATE_DEMOPLAYBACK && GameClient()->m_Snap.m_pLocalCharacter)
620 {
621 // Render local cursor
622 CurWeapon = std::max(a: 0, b: GameClient()->m_aClients[GameClient()->m_Snap.m_LocalClientId].m_Predicted.m_ActiveWeapon);
623 TargetPos = GameClient()->m_Controls.m_aTargetPos[g_Config.m_ClDummy];
624 }
625 else
626 {
627 // Render spec cursor
628 if(!g_Config.m_ClSpecCursor || !GameClient()->m_CursorInfo.IsAvailable())
629 return;
630
631 bool RenderSpecCursor = (GameClient()->m_Snap.m_SpecInfo.m_Active && GameClient()->m_Snap.m_SpecInfo.m_SpectatorId != SPEC_FREEVIEW) || Client()->State() == IClient::STATE_DEMOPLAYBACK;
632
633 if(!RenderSpecCursor)
634 return;
635
636 // Calculate factor to keep cursor on screen
637 const vec2 HalfSize = Center - ScreenRect.m_TopLeft;
638 const vec2 ScreenPos = (GameClient()->m_CursorInfo.WorldTarget() - Center) / GameClient()->m_Camera.m_Zoom;
639 const float ClampFactor = std::max(l: {
640 1.0f,
641 absolute(a: ScreenPos.x / HalfSize.x),
642 absolute(a: ScreenPos.y / HalfSize.y),
643 });
644
645 CurWeapon = std::max(a: 0, b: GameClient()->m_CursorInfo.Weapon() % NUM_WEAPONS);
646 TargetPos = ScreenPos / ClampFactor + Center;
647 if(ClampFactor != 1.0f)
648 Alpha /= 2.0f;
649 }
650
651 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: Alpha);
652 Graphics()->TextureSet(Texture: GameClient()->m_GameSkin.m_aSpriteWeaponCursors[CurWeapon]);
653 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_aCursorOffset[CurWeapon], X: TargetPos.x, Y: TargetPos.y);
654}
655
656void CHud::PrepareAmmoHealthAndArmorQuads()
657{
658 float x = 5;
659 float y = 5;
660 IGraphics::CQuadItem Array[10];
661
662 // ammo of the different weapons
663 for(int i = 0; i < NUM_WEAPONS; ++i)
664 {
665 // 0.6
666 for(int n = 0; n < 10; n++)
667 Array[n] = IGraphics::CQuadItem(x + n * 12, y, 10, 10);
668
669 m_aAmmoOffset[i] = Graphics()->QuadContainerAddQuads(ContainerIndex: m_HudQuadContainerIndex, pArray: Array, Num: 10);
670
671 // 0.7
672 if(i == WEAPON_GRENADE)
673 {
674 // special case for 0.7 grenade
675 for(int n = 0; n < 10; n++)
676 Array[n] = IGraphics::CQuadItem(1 + x + n * 12, y, 10, 10);
677 }
678 else
679 {
680 for(int n = 0; n < 10; n++)
681 Array[n] = IGraphics::CQuadItem(x + n * 12, y, 12, 12);
682 }
683
684 Graphics()->QuadContainerAddQuads(ContainerIndex: m_HudQuadContainerIndex, pArray: Array, Num: 10);
685 }
686
687 // health
688 for(int i = 0; i < 10; ++i)
689 Array[i] = IGraphics::CQuadItem(x + i * 12, y, 10, 10);
690 m_HealthOffset = Graphics()->QuadContainerAddQuads(ContainerIndex: m_HudQuadContainerIndex, pArray: Array, Num: 10);
691
692 // 0.7
693 for(int i = 0; i < 10; ++i)
694 Array[i] = IGraphics::CQuadItem(x + i * 12, y, 12, 12);
695 Graphics()->QuadContainerAddQuads(ContainerIndex: m_HudQuadContainerIndex, pArray: Array, Num: 10);
696
697 // empty health
698 for(int i = 0; i < 10; ++i)
699 Array[i] = IGraphics::CQuadItem(x + i * 12, y, 10, 10);
700 m_EmptyHealthOffset = Graphics()->QuadContainerAddQuads(ContainerIndex: m_HudQuadContainerIndex, pArray: Array, Num: 10);
701
702 // 0.7
703 for(int i = 0; i < 10; ++i)
704 Array[i] = IGraphics::CQuadItem(x + i * 12, y, 12, 12);
705 Graphics()->QuadContainerAddQuads(ContainerIndex: m_HudQuadContainerIndex, pArray: Array, Num: 10);
706
707 // armor meter
708 for(int i = 0; i < 10; ++i)
709 Array[i] = IGraphics::CQuadItem(x + i * 12, y + 12, 10, 10);
710 m_ArmorOffset = Graphics()->QuadContainerAddQuads(ContainerIndex: m_HudQuadContainerIndex, pArray: Array, Num: 10);
711
712 // 0.7
713 for(int i = 0; i < 10; ++i)
714 Array[i] = IGraphics::CQuadItem(x + i * 12, y + 12, 12, 12);
715 Graphics()->QuadContainerAddQuads(ContainerIndex: m_HudQuadContainerIndex, pArray: Array, Num: 10);
716
717 // empty armor meter
718 for(int i = 0; i < 10; ++i)
719 Array[i] = IGraphics::CQuadItem(x + i * 12, y + 12, 10, 10);
720 m_EmptyArmorOffset = Graphics()->QuadContainerAddQuads(ContainerIndex: m_HudQuadContainerIndex, pArray: Array, Num: 10);
721
722 // 0.7
723 for(int i = 0; i < 10; ++i)
724 Array[i] = IGraphics::CQuadItem(x + i * 12, y + 12, 12, 12);
725 Graphics()->QuadContainerAddQuads(ContainerIndex: m_HudQuadContainerIndex, pArray: Array, Num: 10);
726}
727
728void CHud::RenderAmmoHealthAndArmor(const CNetObj_Character *pCharacter)
729{
730 if(!pCharacter)
731 return;
732
733 bool IsSixupGameSkin = GameClient()->m_GameSkin.IsSixup();
734 int QuadOffsetSixup = (IsSixupGameSkin ? 10 : 0);
735
736 if(GameClient()->m_GameInfo.m_HudAmmo)
737 {
738 // ammo display
739 float AmmoOffsetY = GameClient()->m_GameInfo.m_HudHealthArmor ? 24 : 0;
740 int CurWeapon = pCharacter->m_Weapon % NUM_WEAPONS;
741 // 0.7 only
742 if(CurWeapon == WEAPON_NINJA)
743 {
744 if(!GameClient()->m_GameInfo.m_HudDDRace && Client()->IsSixup())
745 {
746 const int Max = g_pData->m_Weapons.m_Ninja.m_Duration * Client()->GameTickSpeed() / 1000;
747 float NinjaProgress = std::clamp(val: pCharacter->m_AmmoCount - Client()->GameTick(Conn: g_Config.m_ClDummy), lo: 0, hi: Max) / (float)Max;
748 RenderNinjaBarPos(x: 5 + 10 * 12, y: 5, Width: 6.f, Height: 24.f, Progress: NinjaProgress);
749 }
750 }
751 else if(CurWeapon >= 0 && GameClient()->m_GameSkin.m_aSpriteWeaponProjectiles[CurWeapon].IsValid())
752 {
753 Graphics()->TextureSet(Texture: GameClient()->m_GameSkin.m_aSpriteWeaponProjectiles[CurWeapon]);
754 if(AmmoOffsetY > 0)
755 {
756 Graphics()->RenderQuadContainerEx(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_aAmmoOffset[CurWeapon] + QuadOffsetSixup, QuadDrawNum: std::clamp(val: pCharacter->m_AmmoCount, lo: 0, hi: 10), X: 0, Y: AmmoOffsetY);
757 }
758 else
759 {
760 Graphics()->RenderQuadContainer(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_aAmmoOffset[CurWeapon] + QuadOffsetSixup, QuadDrawNum: std::clamp(val: pCharacter->m_AmmoCount, lo: 0, hi: 10));
761 }
762 }
763 }
764
765 if(GameClient()->m_GameInfo.m_HudHealthArmor)
766 {
767 // health display
768 const int DisplayHealth = std::min(a: pCharacter->m_Health, b: 10);
769 Graphics()->TextureSet(Texture: GameClient()->m_GameSkin.m_SpriteHealthFull);
770 Graphics()->RenderQuadContainer(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_HealthOffset + QuadOffsetSixup, QuadDrawNum: DisplayHealth);
771 Graphics()->TextureSet(Texture: GameClient()->m_GameSkin.m_SpriteHealthEmpty);
772 Graphics()->RenderQuadContainer(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_EmptyHealthOffset + QuadOffsetSixup + DisplayHealth, QuadDrawNum: 10 - DisplayHealth);
773
774 // armor display
775 const int DisplayArmor = std::min(a: pCharacter->m_Armor, b: 10);
776 Graphics()->TextureSet(Texture: GameClient()->m_GameSkin.m_SpriteArmorFull);
777 Graphics()->RenderQuadContainer(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_ArmorOffset + QuadOffsetSixup, QuadDrawNum: DisplayArmor);
778 Graphics()->TextureSet(Texture: GameClient()->m_GameSkin.m_SpriteArmorEmpty);
779 Graphics()->RenderQuadContainer(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_ArmorOffset + QuadOffsetSixup + DisplayArmor, QuadDrawNum: 10 - DisplayArmor);
780 }
781}
782
783void CHud::PreparePlayerStateQuads()
784{
785 float x = 5;
786 float y = 5 + 24;
787 IGraphics::CQuadItem Array[10];
788
789 // Quads for displaying the available and used jumps
790 for(int i = 0; i < 10; ++i)
791 Array[i] = IGraphics::CQuadItem(x + i * 12, y, 12, 12);
792 m_AirjumpOffset = Graphics()->QuadContainerAddQuads(ContainerIndex: m_HudQuadContainerIndex, pArray: Array, Num: 10);
793
794 for(int i = 0; i < 10; ++i)
795 Array[i] = IGraphics::CQuadItem(x + i * 12, y, 12, 12);
796 m_AirjumpEmptyOffset = Graphics()->QuadContainerAddQuads(ContainerIndex: m_HudQuadContainerIndex, pArray: Array, Num: 10);
797
798 // Quads for displaying weapons
799 for(int Weapon = 0; Weapon < NUM_WEAPONS; ++Weapon)
800 {
801 const CDataWeaponspec &WeaponSpec = g_pData->m_Weapons.m_aId[Weapon];
802 float ScaleX, ScaleY;
803 Graphics()->GetSpriteScale(pSprite: WeaponSpec.m_pSpriteBody, ScaleX, ScaleY);
804 constexpr float HudWeaponScale = 0.25f;
805 float Width = WeaponSpec.m_VisualSize * ScaleX * HudWeaponScale;
806 float Height = WeaponSpec.m_VisualSize * ScaleY * HudWeaponScale;
807 m_aWeaponOffset[Weapon] = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_HudQuadContainerIndex, Width, Height);
808 }
809
810 // Quads for displaying capabilities
811 m_EndlessJumpOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_HudQuadContainerIndex, X: 0.f, Y: 0.f, Width: 12.f, Height: 12.f);
812 m_EndlessHookOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_HudQuadContainerIndex, X: 0.f, Y: 0.f, Width: 12.f, Height: 12.f);
813 m_JetpackOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_HudQuadContainerIndex, X: 0.f, Y: 0.f, Width: 12.f, Height: 12.f);
814 m_TeleportGrenadeOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_HudQuadContainerIndex, X: 0.f, Y: 0.f, Width: 12.f, Height: 12.f);
815 m_TeleportGunOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_HudQuadContainerIndex, X: 0.f, Y: 0.f, Width: 12.f, Height: 12.f);
816 m_TeleportLaserOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_HudQuadContainerIndex, X: 0.f, Y: 0.f, Width: 12.f, Height: 12.f);
817
818 // Quads for displaying prohibited capabilities
819 m_SoloOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_HudQuadContainerIndex, X: 0.f, Y: 0.f, Width: 12.f, Height: 12.f);
820 m_CollisionDisabledOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_HudQuadContainerIndex, X: 0.f, Y: 0.f, Width: 12.f, Height: 12.f);
821 m_HookHitDisabledOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_HudQuadContainerIndex, X: 0.f, Y: 0.f, Width: 12.f, Height: 12.f);
822 m_HammerHitDisabledOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_HudQuadContainerIndex, X: 0.f, Y: 0.f, Width: 12.f, Height: 12.f);
823 m_GunHitDisabledOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_HudQuadContainerIndex, X: 0.f, Y: 0.f, Width: 12.f, Height: 12.f);
824 m_ShotgunHitDisabledOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_HudQuadContainerIndex, X: 0.f, Y: 0.f, Width: 12.f, Height: 12.f);
825 m_GrenadeHitDisabledOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_HudQuadContainerIndex, X: 0.f, Y: 0.f, Width: 12.f, Height: 12.f);
826 m_LaserHitDisabledOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_HudQuadContainerIndex, X: 0.f, Y: 0.f, Width: 12.f, Height: 12.f);
827
828 // Quads for displaying freeze status
829 m_DeepFrozenOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_HudQuadContainerIndex, X: 0.f, Y: 0.f, Width: 12.f, Height: 12.f);
830 m_LiveFrozenOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_HudQuadContainerIndex, X: 0.f, Y: 0.f, Width: 12.f, Height: 12.f);
831
832 // Quads for displaying dummy actions
833 m_DummyHammerOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_HudQuadContainerIndex, X: 0.f, Y: 0.f, Width: 12.f, Height: 12.f);
834 m_DummyCopyOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_HudQuadContainerIndex, X: 0.f, Y: 0.f, Width: 12.f, Height: 12.f);
835
836 // Quads for displaying team modes
837 m_PracticeModeOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_HudQuadContainerIndex, X: 0.f, Y: 0.f, Width: 12.f, Height: 12.f);
838 m_LockModeOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_HudQuadContainerIndex, X: 0.f, Y: 0.f, Width: 12.f, Height: 12.f);
839 m_Team0ModeOffset = Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_HudQuadContainerIndex, X: 0.f, Y: 0.f, Width: 12.f, Height: 12.f);
840}
841
842void CHud::RenderPlayerState(const int ClientId)
843{
844 Graphics()->SetColor(r: 1.f, g: 1.f, b: 1.f, a: 1.f);
845
846 // pCharacter contains the predicted character for local players or the last snap for players who are spectated
847 CCharacterCore *pCharacter = &GameClient()->m_aClients[ClientId].m_Predicted;
848 CNetObj_Character *pPlayer = &GameClient()->m_aClients[ClientId].m_RenderCur;
849 int TotalJumpsToDisplay = 0;
850 if(g_Config.m_ClShowhudJumpsIndicator)
851 {
852 int AvailableJumpsToDisplay;
853 if(GameClient()->m_Snap.m_aCharacters[ClientId].m_HasExtendedDisplayInfo)
854 {
855 const bool Grounded = Collision()->IsOnGround(Pos: vec2(pPlayer->m_X, pPlayer->m_Y), Size: CCharacterCore::PhysicalSize());
856 int UsedJumps = pCharacter->m_JumpedTotal;
857 if(pCharacter->m_Jumps > 1)
858 {
859 UsedJumps += !Grounded;
860 }
861 else if(pCharacter->m_Jumps == 1)
862 {
863 // If the player has only one jump, each jump is the last one
864 UsedJumps = pPlayer->m_Jumped & 2;
865 }
866 else if(pCharacter->m_Jumps == -1)
867 {
868 // The player has only one ground jump
869 UsedJumps = !Grounded;
870 }
871
872 if(pCharacter->m_EndlessJump && UsedJumps >= absolute(a: pCharacter->m_Jumps))
873 {
874 UsedJumps = absolute(a: pCharacter->m_Jumps) - 1;
875 }
876
877 int UnusedJumps = absolute(a: pCharacter->m_Jumps) - UsedJumps;
878 if(!(pPlayer->m_Jumped & 2) && UnusedJumps <= 0)
879 {
880 // In some edge cases when the player just got another number of jumps, UnusedJumps is not correct
881 UnusedJumps = 1;
882 }
883 TotalJumpsToDisplay = std::clamp(val: absolute(a: pCharacter->m_Jumps), lo: 0, hi: 10);
884 AvailableJumpsToDisplay = std::clamp(val: UnusedJumps, lo: 0, hi: TotalJumpsToDisplay);
885 }
886 else
887 {
888 TotalJumpsToDisplay = AvailableJumpsToDisplay = std::clamp(val: absolute(a: GameClient()->m_Snap.m_aCharacters[ClientId].m_ExtendedData.m_Jumps), lo: 0, hi: 10);
889 }
890
891 // render available and used jumps
892 int JumpsOffsetY = ((GameClient()->m_GameInfo.m_HudHealthArmor && g_Config.m_ClShowhudHealthAmmo ? 24 : 0) +
893 (GameClient()->m_GameInfo.m_HudAmmo && g_Config.m_ClShowhudHealthAmmo ? 12 : 0));
894 if(JumpsOffsetY > 0)
895 {
896 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudAirjump);
897 Graphics()->RenderQuadContainerEx(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_AirjumpOffset, QuadDrawNum: AvailableJumpsToDisplay, X: 0, Y: JumpsOffsetY);
898 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudAirjumpEmpty);
899 Graphics()->RenderQuadContainerEx(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_AirjumpEmptyOffset + AvailableJumpsToDisplay, QuadDrawNum: TotalJumpsToDisplay - AvailableJumpsToDisplay, X: 0, Y: JumpsOffsetY);
900 }
901 else
902 {
903 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudAirjump);
904 Graphics()->RenderQuadContainer(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_AirjumpOffset, QuadDrawNum: AvailableJumpsToDisplay);
905 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudAirjumpEmpty);
906 Graphics()->RenderQuadContainer(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_AirjumpEmptyOffset + AvailableJumpsToDisplay, QuadDrawNum: TotalJumpsToDisplay - AvailableJumpsToDisplay);
907 }
908 }
909
910 float x = 5 + 12;
911 float y = (5 + 12 + (GameClient()->m_GameInfo.m_HudHealthArmor && g_Config.m_ClShowhudHealthAmmo ? 24 : 0) +
912 (GameClient()->m_GameInfo.m_HudAmmo && g_Config.m_ClShowhudHealthAmmo ? 12 : 0));
913
914 // render weapons
915 {
916 constexpr float aWeaponWidth[NUM_WEAPONS] = {16, 12, 12, 12, 12, 12};
917 constexpr float aWeaponInitialOffset[NUM_WEAPONS] = {-3, -4, -1, -1, -2, -4};
918 bool InitialOffsetAdded = false;
919 for(int Weapon = 0; Weapon < NUM_WEAPONS; ++Weapon)
920 {
921 if(!pCharacter->m_aWeapons[Weapon].m_Got)
922 continue;
923 if(!InitialOffsetAdded)
924 {
925 x += aWeaponInitialOffset[Weapon];
926 InitialOffsetAdded = true;
927 }
928 if(pPlayer->m_Weapon != Weapon)
929 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 0.4f);
930 Graphics()->QuadsSetRotation(Angle: pi * 7 / 4);
931 Graphics()->TextureSet(Texture: GameClient()->m_GameSkin.m_aSpritePickupWeapons[Weapon]);
932 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_aWeaponOffset[Weapon], X: x, Y: y);
933 Graphics()->QuadsSetRotation(Angle: 0);
934 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f);
935 x += aWeaponWidth[Weapon];
936 }
937 if(pCharacter->m_aWeapons[WEAPON_NINJA].m_Got)
938 {
939 const int Max = g_pData->m_Weapons.m_Ninja.m_Duration * Client()->GameTickSpeed() / 1000;
940 float NinjaProgress = std::clamp(val: pCharacter->m_Ninja.m_ActivationTick + g_pData->m_Weapons.m_Ninja.m_Duration * Client()->GameTickSpeed() / 1000 - Client()->GameTick(Conn: g_Config.m_ClDummy), lo: 0, hi: Max) / (float)Max;
941 if(NinjaProgress > 0.0f && GameClient()->m_Snap.m_aCharacters[ClientId].m_HasExtendedDisplayInfo)
942 {
943 RenderNinjaBarPos(x, y: y - 12, Width: 6.f, Height: 24.f, Progress: NinjaProgress);
944 }
945 }
946 }
947
948 // render capabilities
949 x = 5;
950 y += 12;
951 if(TotalJumpsToDisplay > 0)
952 {
953 y += 12;
954 }
955 bool HasCapabilities = false;
956 if(pCharacter->m_EndlessJump)
957 {
958 HasCapabilities = true;
959 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudEndlessJump);
960 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_EndlessJumpOffset, X: x, Y: y);
961 x += 12;
962 }
963 if(pCharacter->m_EndlessHook)
964 {
965 HasCapabilities = true;
966 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudEndlessHook);
967 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_EndlessHookOffset, X: x, Y: y);
968 x += 12;
969 }
970 if(pCharacter->m_Jetpack)
971 {
972 HasCapabilities = true;
973 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudJetpack);
974 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_JetpackOffset, X: x, Y: y);
975 x += 12;
976 }
977 if(pCharacter->m_HasTelegunGun && pCharacter->m_aWeapons[WEAPON_GUN].m_Got)
978 {
979 HasCapabilities = true;
980 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudTeleportGun);
981 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_TeleportGunOffset, X: x, Y: y);
982 x += 12;
983 }
984 if(pCharacter->m_HasTelegunGrenade && pCharacter->m_aWeapons[WEAPON_GRENADE].m_Got)
985 {
986 HasCapabilities = true;
987 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudTeleportGrenade);
988 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_TeleportGrenadeOffset, X: x, Y: y);
989 x += 12;
990 }
991 if(pCharacter->m_HasTelegunLaser && pCharacter->m_aWeapons[WEAPON_LASER].m_Got)
992 {
993 HasCapabilities = true;
994 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudTeleportLaser);
995 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_TeleportLaserOffset, X: x, Y: y);
996 }
997
998 // render prohibited capabilities
999 x = 5;
1000 if(HasCapabilities)
1001 {
1002 y += 12;
1003 }
1004 bool HasProhibitedCapabilities = false;
1005 if(pCharacter->m_Solo)
1006 {
1007 HasProhibitedCapabilities = true;
1008 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudSolo);
1009 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_SoloOffset, X: x, Y: y);
1010 x += 12;
1011 }
1012 if(pCharacter->m_CollisionDisabled)
1013 {
1014 HasProhibitedCapabilities = true;
1015 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudCollisionDisabled);
1016 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_CollisionDisabledOffset, X: x, Y: y);
1017 x += 12;
1018 }
1019 if(pCharacter->m_HookHitDisabled)
1020 {
1021 HasProhibitedCapabilities = true;
1022 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudHookHitDisabled);
1023 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_HookHitDisabledOffset, X: x, Y: y);
1024 x += 12;
1025 }
1026 if(pCharacter->m_HammerHitDisabled)
1027 {
1028 HasProhibitedCapabilities = true;
1029 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudHammerHitDisabled);
1030 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_HammerHitDisabledOffset, X: x, Y: y);
1031 x += 12;
1032 }
1033 if((pCharacter->m_GrenadeHitDisabled && pCharacter->m_HasTelegunGun && pCharacter->m_aWeapons[WEAPON_GUN].m_Got))
1034 {
1035 HasProhibitedCapabilities = true;
1036 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudGunHitDisabled);
1037 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_LaserHitDisabledOffset, X: x, Y: y);
1038 x += 12;
1039 }
1040 if((pCharacter->m_ShotgunHitDisabled && pCharacter->m_aWeapons[WEAPON_SHOTGUN].m_Got))
1041 {
1042 HasProhibitedCapabilities = true;
1043 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudShotgunHitDisabled);
1044 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_ShotgunHitDisabledOffset, X: x, Y: y);
1045 x += 12;
1046 }
1047 if((pCharacter->m_GrenadeHitDisabled && pCharacter->m_aWeapons[WEAPON_GRENADE].m_Got))
1048 {
1049 HasProhibitedCapabilities = true;
1050 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudGrenadeHitDisabled);
1051 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_GrenadeHitDisabledOffset, X: x, Y: y);
1052 x += 12;
1053 }
1054 if((pCharacter->m_LaserHitDisabled && pCharacter->m_aWeapons[WEAPON_LASER].m_Got))
1055 {
1056 HasProhibitedCapabilities = true;
1057 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudLaserHitDisabled);
1058 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_LaserHitDisabledOffset, X: x, Y: y);
1059 }
1060
1061 // render dummy actions and freeze state
1062 x = 5;
1063 if(HasProhibitedCapabilities)
1064 {
1065 y += 12;
1066 }
1067 if(GameClient()->m_Snap.m_aCharacters[ClientId].m_HasExtendedDisplayInfo && GameClient()->m_Snap.m_aCharacters[ClientId].m_ExtendedData.m_Flags & CHARACTERFLAG_LOCK_MODE)
1068 {
1069 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudLockMode);
1070 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_LockModeOffset, X: x, Y: y);
1071 x += 12;
1072 }
1073 if(GameClient()->m_Snap.m_aCharacters[ClientId].m_HasExtendedDisplayInfo && GameClient()->m_Snap.m_aCharacters[ClientId].m_ExtendedData.m_Flags & CHARACTERFLAG_PRACTICE_MODE)
1074 {
1075 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudPracticeMode);
1076 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_PracticeModeOffset, X: x, Y: y);
1077 x += 12;
1078 }
1079 if(GameClient()->m_Snap.m_aCharacters[ClientId].m_HasExtendedDisplayInfo && GameClient()->m_Snap.m_aCharacters[ClientId].m_ExtendedData.m_Flags & CHARACTERFLAG_TEAM0_MODE)
1080 {
1081 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudTeam0Mode);
1082 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_Team0ModeOffset, X: x, Y: y);
1083 x += 12;
1084 }
1085 if(pCharacter->m_DeepFrozen)
1086 {
1087 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudDeepFrozen);
1088 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_DeepFrozenOffset, X: x, Y: y);
1089 x += 12;
1090 }
1091 if(pCharacter->m_LiveFrozen)
1092 {
1093 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudLiveFrozen);
1094 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_LiveFrozenOffset, X: x, Y: y);
1095 }
1096}
1097
1098void CHud::RenderNinjaBarPos(const float x, float y, const float Width, const float Height, float Progress, const float Alpha)
1099{
1100 Progress = std::clamp(val: Progress, lo: 0.0f, hi: 1.0f);
1101
1102 // what percentage of the end pieces is used for the progress indicator and how much is the rest
1103 // half of the ends are used for the progress display
1104 const float RestPct = 0.5f;
1105 const float ProgPct = 0.5f;
1106
1107 const float EndHeight = Width; // to keep the correct scale - the width of the sprite is as long as the height
1108 const float BarWidth = Width;
1109 const float WholeBarHeight = Height;
1110 const float MiddleBarHeight = WholeBarHeight - (EndHeight * 2.0f);
1111 const float EndProgressHeight = EndHeight * ProgPct;
1112 const float EndRestHeight = EndHeight * RestPct;
1113 const float ProgressBarHeight = WholeBarHeight - (EndProgressHeight * 2.0f);
1114 const float EndProgressProportion = EndProgressHeight / ProgressBarHeight;
1115 const float MiddleProgressProportion = MiddleBarHeight / ProgressBarHeight;
1116
1117 // beginning piece
1118 float BeginningPieceProgress = 1;
1119 if(Progress <= 1)
1120 {
1121 if(Progress <= (EndProgressProportion + MiddleProgressProportion))
1122 {
1123 BeginningPieceProgress = 0;
1124 }
1125 else
1126 {
1127 BeginningPieceProgress = (Progress - EndProgressProportion - MiddleProgressProportion) / EndProgressProportion;
1128 }
1129 }
1130 // empty
1131 Graphics()->WrapClamp();
1132 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudNinjaBarEmptyRight);
1133 Graphics()->QuadsBegin();
1134 Graphics()->SetColor(r: 1.f, g: 1.f, b: 1.f, a: Alpha);
1135 // Subset: btm_r, top_r, top_m, btm_m | it is mirrored on the horizontal axe and rotated 90 degrees counterclockwise
1136 Graphics()->QuadsSetSubsetFree(x0: 1, y0: 1, x1: 1, y1: 0, x2: ProgPct - ProgPct * (1.0f - BeginningPieceProgress), y2: 0, x3: ProgPct - ProgPct * (1.0f - BeginningPieceProgress), y3: 1);
1137 IGraphics::CQuadItem QuadEmptyBeginning(x, y, BarWidth, EndRestHeight + EndProgressHeight * (1.0f - BeginningPieceProgress));
1138 Graphics()->QuadsDrawTL(pArray: &QuadEmptyBeginning, Num: 1);
1139 Graphics()->QuadsEnd();
1140 // full
1141 if(BeginningPieceProgress > 0.0f)
1142 {
1143 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudNinjaBarFullLeft);
1144 Graphics()->QuadsBegin();
1145 Graphics()->SetColor(r: 1.f, g: 1.f, b: 1.f, a: Alpha);
1146 // Subset: btm_m, top_m, top_r, btm_r | it is rotated 90 degrees clockwise
1147 Graphics()->QuadsSetSubsetFree(x0: RestPct + ProgPct * (1.0f - BeginningPieceProgress), y0: 1, x1: RestPct + ProgPct * (1.0f - BeginningPieceProgress), y1: 0, x2: 1, y2: 0, x3: 1, y3: 1);
1148 IGraphics::CQuadItem QuadFullBeginning(x, y + (EndRestHeight + EndProgressHeight * (1.0f - BeginningPieceProgress)), BarWidth, EndProgressHeight * BeginningPieceProgress);
1149 Graphics()->QuadsDrawTL(pArray: &QuadFullBeginning, Num: 1);
1150 Graphics()->QuadsEnd();
1151 }
1152
1153 // middle piece
1154 y += EndHeight;
1155
1156 float MiddlePieceProgress = 1;
1157 if(Progress <= EndProgressProportion + MiddleProgressProportion)
1158 {
1159 if(Progress <= EndProgressProportion)
1160 {
1161 MiddlePieceProgress = 0;
1162 }
1163 else
1164 {
1165 MiddlePieceProgress = (Progress - EndProgressProportion) / MiddleProgressProportion;
1166 }
1167 }
1168
1169 const float FullMiddleBarHeight = MiddleBarHeight * MiddlePieceProgress;
1170 const float EmptyMiddleBarHeight = MiddleBarHeight - FullMiddleBarHeight;
1171
1172 // empty ninja bar
1173 if(EmptyMiddleBarHeight > 0.0f)
1174 {
1175 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudNinjaBarEmpty);
1176 Graphics()->QuadsBegin();
1177 Graphics()->SetColor(r: 1.f, g: 1.f, b: 1.f, a: Alpha);
1178 // select the middle portion of the sprite so we don't get edge bleeding
1179 if(EmptyMiddleBarHeight <= EndHeight)
1180 {
1181 // prevent pixel puree, select only a small slice
1182 // Subset: btm_r, top_r, top_m, btm_m | it is mirrored on the horizontal axe and rotated 90 degrees counterclockwise
1183 Graphics()->QuadsSetSubsetFree(x0: 1, y0: 1, x1: 1, y1: 0, x2: 1.0f - (EmptyMiddleBarHeight / EndHeight), y2: 0, x3: 1.0f - (EmptyMiddleBarHeight / EndHeight), y3: 1);
1184 }
1185 else
1186 {
1187 // Subset: btm_r, top_r, top_l, btm_l | it is mirrored on the horizontal axe and rotated 90 degrees counterclockwise
1188 Graphics()->QuadsSetSubsetFree(x0: 1, y0: 1, x1: 1, y1: 0, x2: 0, y2: 0, x3: 0, y3: 1);
1189 }
1190 IGraphics::CQuadItem QuadEmpty(x, y, BarWidth, EmptyMiddleBarHeight);
1191 Graphics()->QuadsDrawTL(pArray: &QuadEmpty, Num: 1);
1192 Graphics()->QuadsEnd();
1193 }
1194
1195 // full ninja bar
1196 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudNinjaBarFull);
1197 Graphics()->QuadsBegin();
1198 Graphics()->SetColor(r: 1.f, g: 1.f, b: 1.f, a: Alpha);
1199 // select the middle portion of the sprite so we don't get edge bleeding
1200 if(FullMiddleBarHeight <= EndHeight)
1201 {
1202 // prevent pixel puree, select only a small slice
1203 // Subset: btm_m, top_m, top_r, btm_r | it is rotated 90 degrees clockwise
1204 Graphics()->QuadsSetSubsetFree(x0: 1.0f - (FullMiddleBarHeight / EndHeight), y0: 1, x1: 1.0f - (FullMiddleBarHeight / EndHeight), y1: 0, x2: 1, y2: 0, x3: 1, y3: 1);
1205 }
1206 else
1207 {
1208 // Subset: btm_l, top_l, top_r, btm_r | it is rotated 90 degrees clockwise
1209 Graphics()->QuadsSetSubsetFree(x0: 0, y0: 1, x1: 0, y1: 0, x2: 1, y2: 0, x3: 1, y3: 1);
1210 }
1211 IGraphics::CQuadItem QuadFull(x, y + EmptyMiddleBarHeight, BarWidth, FullMiddleBarHeight);
1212 Graphics()->QuadsDrawTL(pArray: &QuadFull, Num: 1);
1213 Graphics()->QuadsEnd();
1214
1215 // ending piece
1216 y += MiddleBarHeight;
1217 float EndingPieceProgress = 1;
1218 if(Progress <= EndProgressProportion)
1219 {
1220 EndingPieceProgress = Progress / EndProgressProportion;
1221 }
1222 // empty
1223 if(EndingPieceProgress < 1.0f)
1224 {
1225 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudNinjaBarEmptyRight);
1226 Graphics()->QuadsBegin();
1227 Graphics()->SetColor(r: 1.f, g: 1.f, b: 1.f, a: Alpha);
1228 // Subset: btm_l, top_l, top_m, btm_m | it is rotated 90 degrees clockwise
1229 Graphics()->QuadsSetSubsetFree(x0: 0, y0: 1, x1: 0, y1: 0, x2: ProgPct - ProgPct * EndingPieceProgress, y2: 0, x3: ProgPct - ProgPct * EndingPieceProgress, y3: 1);
1230 IGraphics::CQuadItem QuadEmptyEnding(x, y, BarWidth, EndProgressHeight * (1.0f - EndingPieceProgress));
1231 Graphics()->QuadsDrawTL(pArray: &QuadEmptyEnding, Num: 1);
1232 Graphics()->QuadsEnd();
1233 }
1234 // full
1235 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudNinjaBarFullLeft);
1236 Graphics()->QuadsBegin();
1237 Graphics()->SetColor(r: 1.f, g: 1.f, b: 1.f, a: Alpha);
1238 // Subset: btm_m, top_m, top_l, btm_l | it is mirrored on the horizontal axe and rotated 90 degrees counterclockwise
1239 Graphics()->QuadsSetSubsetFree(x0: RestPct + ProgPct * EndingPieceProgress, y0: 1, x1: RestPct + ProgPct * EndingPieceProgress, y1: 0, x2: 0, y2: 0, x3: 0, y3: 1);
1240 IGraphics::CQuadItem QuadFullEnding(x, y + (EndProgressHeight * (1.0f - EndingPieceProgress)), BarWidth, EndRestHeight + EndProgressHeight * EndingPieceProgress);
1241 Graphics()->QuadsDrawTL(pArray: &QuadFullEnding, Num: 1);
1242 Graphics()->QuadsEnd();
1243
1244 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 0, BottomRightU: 1, BottomRightV: 1);
1245 Graphics()->SetColor(r: 1.f, g: 1.f, b: 1.f, a: 1.f);
1246 Graphics()->WrapNormal();
1247}
1248
1249void CHud::RenderSpectatorCount()
1250{
1251 if(!g_Config.m_ClShowhudSpectatorCount)
1252 {
1253 return;
1254 }
1255
1256 int Count = 0;
1257 if(Client()->IsSixup())
1258 {
1259 for(int i = 0; i < MAX_CLIENTS; i++)
1260 {
1261 if(i == GameClient()->m_aLocalIds[0] || (GameClient()->Client()->DummyConnected() && i == GameClient()->m_aLocalIds[1]))
1262 continue;
1263
1264 if(Client()->m_TranslationContext.m_aClients[i].m_PlayerFlags7 & protocol7::PLAYERFLAG_WATCHING)
1265 {
1266 Count++;
1267 }
1268 }
1269 }
1270 else
1271 {
1272 const CNetObj_SpectatorCount *pSpectatorCount = GameClient()->m_Snap.m_pSpectatorCount;
1273 if(!pSpectatorCount)
1274 {
1275 m_LastSpectatorCountTick = Client()->GameTick(Conn: g_Config.m_ClDummy);
1276 return;
1277 }
1278 Count = pSpectatorCount->m_NumSpectators;
1279 }
1280
1281 if(Count == 0)
1282 {
1283 m_LastSpectatorCountTick = Client()->GameTick(Conn: g_Config.m_ClDummy);
1284 return;
1285 }
1286
1287 // 1 second delay
1288 if(Client()->GameTick(Conn: g_Config.m_ClDummy) < m_LastSpectatorCountTick + Client()->GameTickSpeed())
1289 return;
1290
1291 char aBuf[16];
1292 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%d", Count);
1293
1294 const float Fontsize = 6.0f;
1295 const float BoxHeight = 14.f;
1296 const float BoxWidth = 13.f + TextRender()->TextWidth(Size: Fontsize, pText: aBuf);
1297
1298 float StartX = m_Width - BoxWidth;
1299 float StartY = 285.0f - BoxHeight - 4; // 4 units distance to the next display;
1300 if(g_Config.m_ClShowhudPlayerPosition || g_Config.m_ClShowhudPlayerSpeed || g_Config.m_ClShowhudPlayerAngle)
1301 {
1302 StartY -= 4;
1303 }
1304 StartY -= GetMovementInformationBoxHeight();
1305
1306 if(g_Config.m_ClShowhudScore)
1307 {
1308 StartY -= 56;
1309 }
1310
1311 if(g_Config.m_ClShowhudDummyActions && !(GameClient()->m_Snap.m_pGameInfoObj->m_GameStateFlags & GAMESTATEFLAG_GAMEOVER) && Client()->DummyConnected())
1312 {
1313 StartY = StartY - 29.0f - 4; // dummy actions height and padding
1314 }
1315
1316 Graphics()->DrawRect(x: StartX, y: StartY, w: BoxWidth, h: BoxHeight, Color: ColorRGBA(0.0f, 0.0f, 0.0f, 0.4f), Corners: IGraphics::CORNER_L, Rounding: 5.0f);
1317
1318 float y = StartY + BoxHeight / 3;
1319 float x = StartX + 2;
1320
1321 TextRender()->SetFontPreset(EFontPreset::ICON_FONT);
1322 TextRender()->Text(x, y, Size: Fontsize, pText: FontIcon::EYE, LineWidth: -1.0f);
1323 TextRender()->SetFontPreset(EFontPreset::DEFAULT_FONT);
1324 TextRender()->Text(x: x + Fontsize + 3.f, y, Size: Fontsize, pText: aBuf, LineWidth: -1.0f);
1325}
1326
1327void CHud::RenderDummyActions()
1328{
1329 if(!g_Config.m_ClShowhudDummyActions || (GameClient()->m_Snap.m_pGameInfoObj->m_GameStateFlags & GAMESTATEFLAG_GAMEOVER) || !Client()->DummyConnected())
1330 {
1331 return;
1332 }
1333 // render small dummy actions hud
1334 const float BoxHeight = 29.0f;
1335 const float BoxWidth = 16.0f;
1336
1337 float StartX = m_Width - BoxWidth;
1338 float StartY = 285.0f - BoxHeight - 4; // 4 units distance to the next display;
1339 if(g_Config.m_ClShowhudPlayerPosition || g_Config.m_ClShowhudPlayerSpeed || g_Config.m_ClShowhudPlayerAngle)
1340 {
1341 StartY -= 4;
1342 }
1343 StartY -= GetMovementInformationBoxHeight();
1344
1345 if(g_Config.m_ClShowhudScore)
1346 {
1347 StartY -= 56;
1348 }
1349
1350 Graphics()->DrawRect(x: StartX, y: StartY, w: BoxWidth, h: BoxHeight, Color: ColorRGBA(0.0f, 0.0f, 0.0f, 0.4f), Corners: IGraphics::CORNER_L, Rounding: 5.0f);
1351
1352 float y = StartY + 2;
1353 float x = StartX + 2;
1354 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 0.4f);
1355 if(g_Config.m_ClDummyHammer)
1356 {
1357 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f);
1358 }
1359 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudDummyHammer);
1360 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_DummyHammerOffset, X: x, Y: y);
1361 y += 13;
1362 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 0.4f);
1363 if(g_Config.m_ClDummyCopyMoves)
1364 {
1365 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f);
1366 }
1367 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudDummyCopy);
1368 Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_HudQuadContainerIndex, QuadOffset: m_DummyCopyOffset, X: x, Y: y);
1369}
1370
1371inline int CHud::GetDigitsIndex(int Value, int Max)
1372{
1373 if(Value < 0)
1374 {
1375 Value *= -1;
1376 }
1377 int DigitsIndex = std::log10(x: (Value ? Value : 1));
1378 if(DigitsIndex > Max)
1379 {
1380 DigitsIndex = Max;
1381 }
1382 if(DigitsIndex < 0)
1383 {
1384 DigitsIndex = 0;
1385 }
1386 return DigitsIndex;
1387}
1388
1389inline float CHud::GetMovementInformationBoxHeight()
1390{
1391 if(GameClient()->m_Snap.m_SpecInfo.m_Active && (GameClient()->m_Snap.m_SpecInfo.m_SpectatorId == SPEC_FREEVIEW || GameClient()->m_aClients[GameClient()->m_Snap.m_SpecInfo.m_SpectatorId].m_SpecCharPresent))
1392 return g_Config.m_ClShowhudPlayerPosition ? 3.0f * MOVEMENT_INFORMATION_LINE_HEIGHT + 2.0f : 0.0f;
1393 float BoxHeight = 3.0f * MOVEMENT_INFORMATION_LINE_HEIGHT * (g_Config.m_ClShowhudPlayerPosition + g_Config.m_ClShowhudPlayerSpeed) + 2.0f * MOVEMENT_INFORMATION_LINE_HEIGHT * g_Config.m_ClShowhudPlayerAngle;
1394 if(g_Config.m_ClShowhudPlayerPosition || g_Config.m_ClShowhudPlayerSpeed || g_Config.m_ClShowhudPlayerAngle)
1395 {
1396 BoxHeight += 2.0f;
1397 }
1398 return BoxHeight;
1399}
1400
1401void CHud::UpdateMovementInformationTextContainer(STextContainerIndex &TextContainer, float FontSize, float Value, float &PrevValue)
1402{
1403 Value = std::round(x: Value * 100.0f) / 100.0f; // Round to 2dp
1404 if(TextContainer.Valid() && PrevValue == Value)
1405 return;
1406 PrevValue = Value;
1407
1408 char aBuf[128];
1409 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%.2f", Value);
1410
1411 CTextCursor Cursor;
1412 Cursor.m_FontSize = FontSize;
1413 TextRender()->RecreateTextContainer(TextContainerIndex&: TextContainer, pCursor: &Cursor, pText: aBuf);
1414}
1415
1416void CHud::RenderMovementInformationTextContainer(STextContainerIndex &TextContainer, const ColorRGBA &Color, float X, float Y)
1417{
1418 if(TextContainer.Valid())
1419 {
1420 TextRender()->RenderTextContainer(TextContainerIndex: TextContainer, TextColor: Color, TextOutlineColor: TextRender()->DefaultTextOutlineColor(), X: X - TextRender()->GetBoundingBoxTextContainer(TextContainerIndex: TextContainer).m_W, Y);
1421 }
1422}
1423
1424CHud::CMovementInformation CHud::GetMovementInformation(int ClientId, int Conn) const
1425{
1426 CMovementInformation Out;
1427 if(ClientId == SPEC_FREEVIEW)
1428 {
1429 Out.m_Pos = GameClient()->m_Camera.m_Center / 32.0f;
1430 }
1431 else if(GameClient()->m_aClients[ClientId].m_SpecCharPresent)
1432 {
1433 Out.m_Pos = GameClient()->m_aClients[ClientId].m_SpecChar / 32.0f;
1434 }
1435 else
1436 {
1437 const CNetObj_Character *pPrevChar = &GameClient()->m_Snap.m_aCharacters[ClientId].m_Prev;
1438 const CNetObj_Character *pCurChar = &GameClient()->m_Snap.m_aCharacters[ClientId].m_Cur;
1439 const float IntraTick = Client()->IntraGameTick(Conn);
1440
1441 // To make the player position relative to blocks we need to divide by the block size
1442 Out.m_Pos = mix(a: vec2(pPrevChar->m_X, pPrevChar->m_Y), b: vec2(pCurChar->m_X, pCurChar->m_Y), amount: IntraTick) / 32.0f;
1443
1444 const vec2 Vel = mix(a: vec2(pPrevChar->m_VelX, pPrevChar->m_VelY), b: vec2(pCurChar->m_VelX, pCurChar->m_VelY), amount: IntraTick);
1445
1446 float VelspeedX = Vel.x / 256.0f * Client()->GameTickSpeed();
1447 if(Vel.x >= -1.0f && Vel.x <= 1.0f)
1448 {
1449 VelspeedX = 0.0f;
1450 }
1451 float VelspeedY = Vel.y / 256.0f * Client()->GameTickSpeed();
1452 if(Vel.y >= -128.0f && Vel.y <= 128.0f)
1453 {
1454 VelspeedY = 0.0f;
1455 }
1456 // We show the speed in Blocks per Second (Bps) and therefore have to divide by the block size
1457 Out.m_Speed.x = VelspeedX / 32.0f;
1458 float VelspeedLength = length(a: vec2(Vel.x, Vel.y) / 256.0f) * Client()->GameTickSpeed();
1459 // Todo: Use Velramp tuning of each individual player
1460 // Since these tuning parameters are almost never changed, the default values are sufficient in most cases
1461 float Ramp = VelocityRamp(Value: VelspeedLength, Start: GameClient()->m_aTuning[Conn].m_VelrampStart, Range: GameClient()->m_aTuning[Conn].m_VelrampRange, Curvature: GameClient()->m_aTuning[Conn].m_VelrampCurvature);
1462 Out.m_Speed.x *= Ramp;
1463 Out.m_Speed.y = VelspeedY / 32.0f;
1464
1465 float Angle = GameClient()->m_Players.GetPlayerTargetAngle(pPrevChar, pPlayerChar: pCurChar, ClientId, Intra: IntraTick);
1466 if(Angle < 0.0f)
1467 {
1468 Angle += 2.0f * pi;
1469 }
1470 Out.m_Angle = Angle * 180.0f / pi;
1471 }
1472 return Out;
1473}
1474
1475void CHud::RenderMovementInformation()
1476{
1477 const int ClientId = GameClient()->m_Snap.m_SpecInfo.m_Active ? GameClient()->m_Snap.m_SpecInfo.m_SpectatorId : GameClient()->m_Snap.m_LocalClientId;
1478 const bool PosOnly = ClientId == SPEC_FREEVIEW || (GameClient()->m_aClients[ClientId].m_SpecCharPresent);
1479 // Draw the information depending on settings: Position, speed and target angle
1480 // This display is only to present the available information from the last snapshot, not to interpolate or predict
1481 if(!g_Config.m_ClShowhudPlayerPosition && (PosOnly || (!g_Config.m_ClShowhudPlayerSpeed && !g_Config.m_ClShowhudPlayerAngle)))
1482 {
1483 return;
1484 }
1485 const float LineSpacer = 1.0f; // above and below each entry
1486 const float Fontsize = 6.0f;
1487
1488 float BoxHeight = GetMovementInformationBoxHeight();
1489 const float BoxWidth = 62.0f;
1490
1491 float StartX = m_Width - BoxWidth;
1492 float StartY = 285.0f - BoxHeight - 4.0f; // 4 units distance to the next display;
1493 if(g_Config.m_ClShowhudScore)
1494 {
1495 StartY -= 56.0f;
1496 }
1497
1498 Graphics()->DrawRect(x: StartX, y: StartY, w: BoxWidth, h: BoxHeight, Color: ColorRGBA(0.0f, 0.0f, 0.0f, 0.4f), Corners: IGraphics::CORNER_L, Rounding: 5.0f);
1499
1500 const CMovementInformation Info = GetMovementInformation(ClientId, Conn: g_Config.m_ClDummy);
1501
1502 float y = StartY + LineSpacer * 2.0f;
1503 const float LeftX = StartX + 2.0f;
1504 const float RightX = m_Width - 2.0f;
1505
1506 if(g_Config.m_ClShowhudPlayerPosition)
1507 {
1508 TextRender()->Text(x: LeftX, y, Size: Fontsize, pText: Localize(pStr: "Position:"), LineWidth: -1.0f);
1509 y += MOVEMENT_INFORMATION_LINE_HEIGHT;
1510
1511 TextRender()->Text(x: LeftX, y, Size: Fontsize, pText: "X:", LineWidth: -1.0f);
1512 UpdateMovementInformationTextContainer(TextContainer&: m_aPlayerPositionContainers[0], FontSize: Fontsize, Value: Info.m_Pos.x, PrevValue&: m_aPlayerPrevPosition[0]);
1513 RenderMovementInformationTextContainer(TextContainer&: m_aPlayerPositionContainers[0], Color: TextRender()->DefaultTextColor(), X: RightX, Y: y);
1514 y += MOVEMENT_INFORMATION_LINE_HEIGHT;
1515
1516 TextRender()->Text(x: LeftX, y, Size: Fontsize, pText: "Y:", LineWidth: -1.0f);
1517 UpdateMovementInformationTextContainer(TextContainer&: m_aPlayerPositionContainers[1], FontSize: Fontsize, Value: Info.m_Pos.y, PrevValue&: m_aPlayerPrevPosition[1]);
1518 RenderMovementInformationTextContainer(TextContainer&: m_aPlayerPositionContainers[1], Color: TextRender()->DefaultTextColor(), X: RightX, Y: y);
1519 y += MOVEMENT_INFORMATION_LINE_HEIGHT;
1520 }
1521
1522 if(PosOnly)
1523 return;
1524
1525 if(g_Config.m_ClShowhudPlayerSpeed)
1526 {
1527 TextRender()->Text(x: LeftX, y, Size: Fontsize, pText: Localize(pStr: "Speed:"), LineWidth: -1.0f);
1528 y += MOVEMENT_INFORMATION_LINE_HEIGHT;
1529
1530 const char aaCoordinates[][4] = {"X:", "Y:"};
1531 for(int i = 0; i < 2; i++)
1532 {
1533 ColorRGBA Color(1.0f, 1.0f, 1.0f, 1.0f);
1534 if(m_aLastPlayerSpeedChange[i] == ESpeedChange::INCREASE)
1535 Color = ColorRGBA(0.0f, 1.0f, 0.0f, 1.0f);
1536 if(m_aLastPlayerSpeedChange[i] == ESpeedChange::DECREASE)
1537 Color = ColorRGBA(1.0f, 0.5f, 0.5f, 1.0f);
1538 TextRender()->Text(x: LeftX, y, Size: Fontsize, pText: aaCoordinates[i], LineWidth: -1.0f);
1539 UpdateMovementInformationTextContainer(TextContainer&: m_aPlayerSpeedTextContainers[i], FontSize: Fontsize, Value: i == 0 ? Info.m_Speed.x : Info.m_Speed.y, PrevValue&: m_aPlayerPrevSpeed[i]);
1540 RenderMovementInformationTextContainer(TextContainer&: m_aPlayerSpeedTextContainers[i], Color, X: RightX, Y: y);
1541 y += MOVEMENT_INFORMATION_LINE_HEIGHT;
1542 }
1543
1544 TextRender()->TextColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f);
1545 }
1546
1547 if(g_Config.m_ClShowhudPlayerAngle)
1548 {
1549 TextRender()->Text(x: LeftX, y, Size: Fontsize, pText: Localize(pStr: "Angle:"), LineWidth: -1.0f);
1550 y += MOVEMENT_INFORMATION_LINE_HEIGHT;
1551
1552 UpdateMovementInformationTextContainer(TextContainer&: m_PlayerAngleTextContainerIndex, FontSize: Fontsize, Value: Info.m_Angle, PrevValue&: m_PlayerPrevAngle);
1553 RenderMovementInformationTextContainer(TextContainer&: m_PlayerAngleTextContainerIndex, Color: TextRender()->DefaultTextColor(), X: RightX, Y: y);
1554 }
1555}
1556
1557void CHud::RenderSpectatorHud()
1558{
1559 if(!g_Config.m_ClShowhudSpectator)
1560 return;
1561
1562 // draw the box
1563 Graphics()->DrawRect(x: m_Width - 180.0f, y: m_Height - 15.0f, w: 180.0f, h: 15.0f, Color: ColorRGBA(0.0f, 0.0f, 0.0f, 0.4f), Corners: IGraphics::CORNER_TL, Rounding: 5.0f);
1564
1565 // draw the text
1566 char aBuf[128];
1567 if(GameClient()->m_MultiView.IsActivated())
1568 {
1569 str_copy(dst&: aBuf, src: Localize(pStr: "Multi-View"));
1570 }
1571 else if(GameClient()->m_Snap.m_SpecInfo.m_SpectatorId != SPEC_FREEVIEW)
1572 {
1573 const auto &Player = GameClient()->m_aClients[GameClient()->m_Snap.m_SpecInfo.m_SpectatorId];
1574 if(g_Config.m_ClShowIds)
1575 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: Localize(pStr: "Following %d: %s", pContext: "Spectating"), Player.ClientId(), Player.m_aName);
1576 else
1577 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: Localize(pStr: "Following %s", pContext: "Spectating"), Player.m_aName);
1578 }
1579 else
1580 {
1581 str_copy(dst&: aBuf, src: Localize(pStr: "Free-View"));
1582 }
1583 TextRender()->Text(x: m_Width - 174.0f, y: m_Height - 15.0f + (15.f - 8.f) / 2.f, Size: 8.0f, pText: aBuf, LineWidth: -1.0f);
1584
1585 // draw the camera info
1586 if(Client()->State() != IClient::STATE_DEMOPLAYBACK && GameClient()->m_Camera.SpectatingPlayer() && GameClient()->m_Camera.CanUseAutoSpecCamera() && g_Config.m_ClSpecAutoSync)
1587 {
1588 bool AutoSpecCameraEnabled = GameClient()->m_Camera.m_AutoSpecCamera;
1589 const char *pLabelText = Localize(pStr: "AUTO", pContext: "Spectating Camera Mode Icon");
1590 const float TextWidth = TextRender()->TextWidth(Size: 6.0f, pText: pLabelText);
1591
1592 constexpr float RightMargin = 4.0f;
1593 constexpr float IconWidth = 6.0f;
1594 constexpr float Padding = 3.0f;
1595 const float TagWidth = IconWidth + TextWidth + Padding * 3.0f;
1596 const float TagX = m_Width - RightMargin - TagWidth;
1597 Graphics()->DrawRect(x: TagX, y: m_Height - 12.0f, w: TagWidth, h: 10.0f, Color: ColorRGBA(1.0f, 1.0f, 1.0f, AutoSpecCameraEnabled ? 0.50f : 0.10f), Corners: IGraphics::CORNER_ALL, Rounding: 2.5f);
1598 TextRender()->TextColor(r: 1, g: 1, b: 1, a: AutoSpecCameraEnabled ? 1.0f : 0.65f);
1599 TextRender()->SetFontPreset(EFontPreset::ICON_FONT);
1600 TextRender()->Text(x: TagX + Padding, y: m_Height - 10.0f, Size: 6.0f, pText: FontIcon::CAMERA, LineWidth: -1.0f);
1601 TextRender()->SetFontPreset(EFontPreset::DEFAULT_FONT);
1602 TextRender()->Text(x: TagX + Padding + IconWidth + Padding, y: m_Height - 10.0f, Size: 6.0f, pText: pLabelText, LineWidth: -1.0f);
1603 TextRender()->TextColor(r: 1, g: 1, b: 1, a: 1);
1604 }
1605}
1606
1607void CHud::RenderLocalTime(float x)
1608{
1609 if(!g_Config.m_ClShowLocalTimeAlways && !GameClient()->m_Scoreboard.IsActive())
1610 return;
1611
1612 // draw the box
1613 Graphics()->DrawRect(x: x - 30.0f, y: 0.0f, w: 25.0f, h: 12.5f, Color: ColorRGBA(0.0f, 0.0f, 0.0f, 0.4f), Corners: IGraphics::CORNER_B, Rounding: 3.75f);
1614
1615 // draw the text
1616 char aTimeStr[6];
1617 str_timestamp_format(buffer: aTimeStr, buffer_size: sizeof(aTimeStr), format: "%H:%M");
1618 TextRender()->Text(x: x - 25.0f, y: (12.5f - 5.f) / 2.f, Size: 5.0f, pText: aTimeStr, LineWidth: -1.0f);
1619}
1620
1621void CHud::OnNewSnapshot()
1622{
1623 if(Client()->State() != IClient::STATE_ONLINE && Client()->State() != IClient::STATE_DEMOPLAYBACK)
1624 return;
1625 if(!GameClient()->m_Snap.m_pGameInfoObj)
1626 return;
1627
1628 int ClientId = -1;
1629 if(GameClient()->m_Snap.m_pLocalCharacter && !GameClient()->m_Snap.m_SpecInfo.m_Active && !(GameClient()->m_Snap.m_pGameInfoObj->m_GameStateFlags & GAMESTATEFLAG_GAMEOVER))
1630 ClientId = GameClient()->m_Snap.m_LocalClientId;
1631 else if(GameClient()->m_Snap.m_SpecInfo.m_Active)
1632 ClientId = GameClient()->m_Snap.m_SpecInfo.m_SpectatorId;
1633
1634 if(ClientId == -1)
1635 return;
1636
1637 const CNetObj_Character *pPrevChar = &GameClient()->m_Snap.m_aCharacters[ClientId].m_Prev;
1638 const CNetObj_Character *pCurChar = &GameClient()->m_Snap.m_aCharacters[ClientId].m_Cur;
1639 const float IntraTick = Client()->IntraGameTick(Conn: g_Config.m_ClDummy);
1640 ivec2 Vel = mix(a: ivec2(pPrevChar->m_VelX, pPrevChar->m_VelY), b: ivec2(pCurChar->m_VelX, pCurChar->m_VelY), amount: IntraTick);
1641
1642 CCharacter *pChar = GameClient()->m_PredictedWorld.GetCharacterById(Id: ClientId);
1643 if(pChar && pChar->IsGrounded())
1644 Vel.y = 0;
1645
1646 int aVels[2] = {Vel.x, Vel.y};
1647
1648 for(int i = 0; i < 2; i++)
1649 {
1650 int AbsVel = abs(x: aVels[i]);
1651 if(AbsVel > m_aPlayerSpeed[i])
1652 {
1653 m_aLastPlayerSpeedChange[i] = ESpeedChange::INCREASE;
1654 }
1655 if(AbsVel < m_aPlayerSpeed[i])
1656 {
1657 m_aLastPlayerSpeedChange[i] = ESpeedChange::DECREASE;
1658 }
1659 if(AbsVel < 2)
1660 {
1661 m_aLastPlayerSpeedChange[i] = ESpeedChange::NONE;
1662 }
1663 m_aPlayerSpeed[i] = AbsVel;
1664 }
1665}
1666
1667void CHud::OnRender()
1668{
1669 if(Client()->State() != IClient::STATE_ONLINE && Client()->State() != IClient::STATE_DEMOPLAYBACK)
1670 return;
1671
1672 if(!GameClient()->m_Snap.m_pGameInfoObj)
1673 return;
1674
1675 m_Width = 300.0f * Graphics()->ScreenAspect();
1676 m_Height = 300.0f;
1677 Graphics()->MapScreenToSize(Width: m_Width, Height: m_Height);
1678
1679#if defined(CONF_VIDEORECORDER)
1680 if((IVideo::Current() && g_Config.m_ClVideoShowhud) || (!IVideo::Current() && g_Config.m_ClShowhud))
1681#else
1682 if(g_Config.m_ClShowhud)
1683#endif
1684 {
1685 if(GameClient()->m_Snap.m_pLocalCharacter && !GameClient()->m_Snap.m_SpecInfo.m_Active && !(GameClient()->m_Snap.m_pGameInfoObj->m_GameStateFlags & GAMESTATEFLAG_GAMEOVER))
1686 {
1687 if(g_Config.m_ClShowhudHealthAmmo)
1688 {
1689 RenderAmmoHealthAndArmor(pCharacter: GameClient()->m_Snap.m_pLocalCharacter);
1690 }
1691 if(GameClient()->m_Snap.m_aCharacters[GameClient()->m_Snap.m_LocalClientId].m_HasExtendedData && g_Config.m_ClShowhudDDRace && GameClient()->m_GameInfo.m_HudDDRace)
1692 {
1693 RenderPlayerState(ClientId: GameClient()->m_Snap.m_LocalClientId);
1694 }
1695 RenderSpectatorCount();
1696 RenderMovementInformation();
1697 RenderDDRaceEffects();
1698 }
1699 else if(GameClient()->m_Snap.m_SpecInfo.m_Active)
1700 {
1701 int SpectatorId = GameClient()->m_Snap.m_SpecInfo.m_SpectatorId;
1702 if(SpectatorId != SPEC_FREEVIEW && g_Config.m_ClShowhudHealthAmmo)
1703 {
1704 RenderAmmoHealthAndArmor(pCharacter: &GameClient()->m_Snap.m_aCharacters[SpectatorId].m_Cur);
1705 }
1706 if(SpectatorId != SPEC_FREEVIEW &&
1707 GameClient()->m_Snap.m_aCharacters[SpectatorId].m_HasExtendedData &&
1708 g_Config.m_ClShowhudDDRace &&
1709 (!GameClient()->m_MultiView.IsActivated() || GameClient()->m_MultiView.ShowHud()) &&
1710 GameClient()->m_GameInfo.m_HudDDRace)
1711 {
1712 RenderPlayerState(ClientId: SpectatorId);
1713 }
1714 RenderMovementInformation();
1715 RenderSpectatorHud();
1716 }
1717
1718 if(g_Config.m_ClShowhudTimer)
1719 RenderGameTimer();
1720 RenderPauseNotification();
1721 RenderSuddenDeath();
1722 if(g_Config.m_ClShowhudScore)
1723 RenderScoreHud();
1724 RenderDummyActions();
1725 RenderWarmupTimer();
1726 RenderTextInfo();
1727 RenderLocalTime(x: (m_Width / 7) * 3);
1728 if(Client()->State() != IClient::STATE_DEMOPLAYBACK)
1729 RenderConnectionWarning();
1730 RenderTeambalanceWarning();
1731 GameClient()->m_Voting.Render();
1732 if(g_Config.m_ClShowRecord)
1733 RenderRecord();
1734 }
1735 RenderCursor();
1736}
1737
1738void CHud::OnMessage(int MsgType, void *pRawMsg)
1739{
1740 if(MsgType == NETMSGTYPE_SV_DDRACETIME || MsgType == NETMSGTYPE_SV_DDRACETIMELEGACY)
1741 {
1742 CNetMsg_Sv_DDRaceTime *pMsg = (CNetMsg_Sv_DDRaceTime *)pRawMsg;
1743
1744 m_DDRaceTime = pMsg->m_Time;
1745
1746 m_ShowFinishTime = pMsg->m_Finish != 0;
1747
1748 if(!m_ShowFinishTime)
1749 {
1750 m_TimeCpDiff = (float)pMsg->m_Check / 100;
1751 m_TimeCpLastReceivedTick = Client()->GameTick(Conn: g_Config.m_ClDummy);
1752 }
1753 else
1754 {
1755 m_FinishTimeDiff = (float)pMsg->m_Check / 100;
1756 m_FinishTimeLastReceivedTick = Client()->GameTick(Conn: g_Config.m_ClDummy);
1757 }
1758 }
1759 else if(MsgType == NETMSGTYPE_SV_RECORD || MsgType == NETMSGTYPE_SV_RECORDLEGACY)
1760 {
1761 CNetMsg_Sv_Record *pMsg = (CNetMsg_Sv_Record *)pRawMsg;
1762
1763 // NETMSGTYPE_SV_RACETIME on old race servers
1764 if(MsgType == NETMSGTYPE_SV_RECORDLEGACY && GameClient()->m_GameInfo.m_DDRaceRecordMessage)
1765 {
1766 m_DDRaceTime = pMsg->m_ServerTimeBest; // First value: m_Time
1767
1768 m_FinishTimeLastReceivedTick = Client()->GameTick(Conn: g_Config.m_ClDummy);
1769
1770 if(pMsg->m_PlayerTimeBest) // Second value: m_Check
1771 {
1772 m_TimeCpDiff = (float)pMsg->m_PlayerTimeBest / 100;
1773 m_TimeCpLastReceivedTick = Client()->GameTick(Conn: g_Config.m_ClDummy);
1774 }
1775 }
1776 else if(MsgType == NETMSGTYPE_SV_RECORD || GameClient()->m_GameInfo.m_RaceRecordMessage)
1777 {
1778 // ignore m_ServerTimeBest, it's handled by the game client
1779 m_aPlayerRecord[g_Config.m_ClDummy] = (float)pMsg->m_PlayerTimeBest / 100;
1780 }
1781 }
1782}
1783
1784void CHud::RenderDDRaceEffects()
1785{
1786 if(m_DDRaceTime)
1787 {
1788 char aBuf[64];
1789 char aTime[32];
1790 if(m_ShowFinishTime && m_FinishTimeLastReceivedTick + Client()->GameTickSpeed() * 6 > Client()->GameTick(Conn: g_Config.m_ClDummy))
1791 {
1792 str_time(centisecs: m_DDRaceTime, format: ETimeFormat::HOURS_CENTISECS, buffer: aTime, buffer_size: sizeof(aTime));
1793 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "Finish time: %s", aTime);
1794
1795 // calculate alpha (4 sec 1 than get lower the next 2 sec)
1796 float Alpha = 1.0f;
1797 if(m_FinishTimeLastReceivedTick + Client()->GameTickSpeed() * 4 < Client()->GameTick(Conn: g_Config.m_ClDummy) && m_FinishTimeLastReceivedTick + Client()->GameTickSpeed() * 6 > Client()->GameTick(Conn: g_Config.m_ClDummy))
1798 {
1799 // lower the alpha slowly to blend text out
1800 Alpha = ((float)(m_FinishTimeLastReceivedTick + Client()->GameTickSpeed() * 6) - (float)Client()->GameTick(Conn: g_Config.m_ClDummy)) / (float)(Client()->GameTickSpeed() * 2);
1801 }
1802
1803 TextRender()->TextColor(r: 1, g: 1, b: 1, a: Alpha);
1804 CTextCursor Cursor;
1805 Cursor.SetPosition(vec2(150 * Graphics()->ScreenAspect() - TextRender()->TextWidth(Size: 12, pText: aBuf) / 2, 20));
1806 Cursor.m_FontSize = 12.0f;
1807 TextRender()->RecreateTextContainer(TextContainerIndex&: m_DDRaceEffectsTextContainerIndex, pCursor: &Cursor, pText: aBuf);
1808 if(m_FinishTimeDiff != 0.0f && m_DDRaceEffectsTextContainerIndex.Valid())
1809 {
1810 if(m_FinishTimeDiff < 0)
1811 {
1812 str_time_float(secs: -m_FinishTimeDiff, format: ETimeFormat::HOURS_CENTISECS, buffer: aTime, buffer_size: sizeof(aTime));
1813 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "-%s", aTime);
1814 TextRender()->TextColor(r: 0.5f, g: 1.0f, b: 0.5f, a: Alpha); // green
1815 }
1816 else
1817 {
1818 str_time_float(secs: m_FinishTimeDiff, format: ETimeFormat::HOURS_CENTISECS, buffer: aTime, buffer_size: sizeof(aTime));
1819 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "+%s", aTime);
1820 TextRender()->TextColor(r: 1.0f, g: 0.5f, b: 0.5f, a: Alpha); // red
1821 }
1822 CTextCursor DiffCursor;
1823 DiffCursor.SetPosition(vec2(150 * Graphics()->ScreenAspect() - TextRender()->TextWidth(Size: 10, pText: aBuf) / 2, 34));
1824 DiffCursor.m_FontSize = 10.0f;
1825 TextRender()->AppendTextContainer(TextContainerIndex: m_DDRaceEffectsTextContainerIndex, pCursor: &DiffCursor, pText: aBuf);
1826 }
1827 if(m_DDRaceEffectsTextContainerIndex.Valid())
1828 {
1829 auto OutlineColor = TextRender()->DefaultTextOutlineColor();
1830 OutlineColor.a *= Alpha;
1831 TextRender()->RenderTextContainer(TextContainerIndex: m_DDRaceEffectsTextContainerIndex, TextColor: TextRender()->DefaultTextColor(), TextOutlineColor: OutlineColor);
1832 }
1833 TextRender()->TextColor(Color: TextRender()->DefaultTextColor());
1834 }
1835 else if(g_Config.m_ClShowhudTimeCpDiff && !m_ShowFinishTime && m_TimeCpLastReceivedTick + Client()->GameTickSpeed() * 6 > Client()->GameTick(Conn: g_Config.m_ClDummy))
1836 {
1837 if(m_TimeCpDiff < 0)
1838 {
1839 str_time_float(secs: -m_TimeCpDiff, format: ETimeFormat::HOURS_CENTISECS, buffer: aTime, buffer_size: sizeof(aTime));
1840 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "-%s", aTime);
1841 }
1842 else
1843 {
1844 str_time_float(secs: m_TimeCpDiff, format: ETimeFormat::HOURS_CENTISECS, buffer: aTime, buffer_size: sizeof(aTime));
1845 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "+%s", aTime);
1846 }
1847
1848 // calculate alpha (4 sec 1 than get lower the next 2 sec)
1849 float Alpha = 1.0f;
1850 if(m_TimeCpLastReceivedTick + Client()->GameTickSpeed() * 4 < Client()->GameTick(Conn: g_Config.m_ClDummy) && m_TimeCpLastReceivedTick + Client()->GameTickSpeed() * 6 > Client()->GameTick(Conn: g_Config.m_ClDummy))
1851 {
1852 // lower the alpha slowly to blend text out
1853 Alpha = ((float)(m_TimeCpLastReceivedTick + Client()->GameTickSpeed() * 6) - (float)Client()->GameTick(Conn: g_Config.m_ClDummy)) / (float)(Client()->GameTickSpeed() * 2);
1854 }
1855
1856 if(m_TimeCpDiff > 0)
1857 TextRender()->TextColor(r: 1.0f, g: 0.5f, b: 0.5f, a: Alpha); // red
1858 else if(m_TimeCpDiff < 0)
1859 TextRender()->TextColor(r: 0.5f, g: 1.0f, b: 0.5f, a: Alpha); // green
1860 else if(!m_TimeCpDiff)
1861 TextRender()->TextColor(r: 1, g: 1, b: 1, a: Alpha); // white
1862
1863 CTextCursor Cursor;
1864 Cursor.SetPosition(vec2(150 * Graphics()->ScreenAspect() - TextRender()->TextWidth(Size: 10, pText: aBuf) / 2, 20));
1865 Cursor.m_FontSize = 10.0f;
1866 TextRender()->RecreateTextContainer(TextContainerIndex&: m_DDRaceEffectsTextContainerIndex, pCursor: &Cursor, pText: aBuf);
1867
1868 if(m_DDRaceEffectsTextContainerIndex.Valid())
1869 {
1870 auto OutlineColor = TextRender()->DefaultTextOutlineColor();
1871 OutlineColor.a *= Alpha;
1872 TextRender()->RenderTextContainer(TextContainerIndex: m_DDRaceEffectsTextContainerIndex, TextColor: TextRender()->DefaultTextColor(), TextOutlineColor: OutlineColor);
1873 }
1874 TextRender()->TextColor(Color: TextRender()->DefaultTextColor());
1875 }
1876 }
1877}
1878
1879void CHud::RenderRecord()
1880{
1881 if(GameClient()->m_MapBestTimeSeconds != FinishTime::UNSET && GameClient()->m_MapBestTimeSeconds != FinishTime::NOT_FINISHED_MILLIS)
1882 {
1883 char aBuf[64];
1884 TextRender()->Text(x: 5, y: 75, Size: 6, pText: Localize(pStr: "Server best:"), LineWidth: -1.0f);
1885 char aTime[32];
1886 int64_t TimeCentiseconds = static_cast<int64_t>(GameClient()->m_MapBestTimeSeconds) * 100 + static_cast<int64_t>(GameClient()->m_MapBestTimeMillis) / 10;
1887 str_time(centisecs: TimeCentiseconds, format: ETimeFormat::HOURS_CENTISECS, buffer: aTime, buffer_size: sizeof(aTime));
1888 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s%s", GameClient()->m_MapBestTimeSeconds > 3600 ? "" : "   ", aTime);
1889 TextRender()->Text(x: 53, y: 75, Size: 6, pText: aBuf, LineWidth: -1.0f);
1890 }
1891
1892 if(GameClient()->m_ReceivedDDNetPlayerFinishTimes)
1893 {
1894 const int PlayerTimeSeconds = GameClient()->m_aClients[GameClient()->m_aLocalIds[g_Config.m_ClDummy]].m_FinishTimeSeconds;
1895 if(PlayerTimeSeconds != FinishTime::NOT_FINISHED_MILLIS)
1896 {
1897 char aBuf[64];
1898 TextRender()->Text(x: 5, y: 82, Size: 6, pText: Localize(pStr: "Personal best:"), LineWidth: -1.0f);
1899 char aTime[32];
1900 const int PlayerTimeMillis = GameClient()->m_aClients[GameClient()->m_aLocalIds[g_Config.m_ClDummy]].m_FinishTimeMillis;
1901 int64_t TimeCentiseconds = static_cast<int64_t>(PlayerTimeSeconds) * 100 + static_cast<int64_t>(PlayerTimeMillis) / 10;
1902 str_time(centisecs: TimeCentiseconds, format: ETimeFormat::HOURS_CENTISECS, buffer: aTime, buffer_size: sizeof(aTime));
1903 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s%s", PlayerTimeSeconds > 3600 ? "" : "   ", aTime);
1904 TextRender()->Text(x: 53, y: 82, Size: 6, pText: aBuf, LineWidth: -1.0f);
1905 }
1906 }
1907 else
1908 {
1909 const float PlayerRecord = m_aPlayerRecord[g_Config.m_ClDummy];
1910 if(PlayerRecord > 0.0f)
1911 {
1912 char aBuf[64];
1913 TextRender()->Text(x: 5, y: 82, Size: 6, pText: Localize(pStr: "Personal best:"), LineWidth: -1.0f);
1914 char aTime[32];
1915 str_time_float(secs: PlayerRecord, format: ETimeFormat::HOURS_CENTISECS, buffer: aTime, buffer_size: sizeof(aTime));
1916 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s%s", PlayerRecord > 3600 ? "" : "   ", aTime);
1917 TextRender()->Text(x: 53, y: 82, Size: 6, pText: aBuf, LineWidth: -1.0f);
1918 }
1919 }
1920}
1921