1#include "freezebars.h"
2
3#include <game/client/gameclient.h>
4
5void CFreezeBars::RenderFreezeBar(const int ClientId)
6{
7 const float FreezeBarWidth = 64.0f;
8 const float FreezeBarHalfWidth = 32.0f;
9 const float FreezeBarHeight = 16.0f;
10
11 // pCharacter contains the predicted character for local players or the last snap for players who are spectated
12 CCharacterCore *pCharacter = &GameClient()->m_aClients[ClientId].m_Predicted;
13
14 if(pCharacter->m_FreezeEnd <= 0 || pCharacter->m_FreezeStart == 0 || pCharacter->m_FreezeEnd <= pCharacter->m_FreezeStart || !GameClient()->m_Snap.m_aCharacters[ClientId].m_HasExtendedDisplayInfo || (pCharacter->m_IsInFreeze && g_Config.m_ClFreezeBarsAlphaInsideFreeze == 0))
15 {
16 return;
17 }
18
19 // only show the freeze bar once the server confirmed the freeze, so mispredicted freezes are not shown
20 if(GameClient()->m_Snap.m_aCharacters[ClientId].m_ExtendedData.m_FreezeEnd <= 0)
21 {
22 return;
23 }
24
25 // the freeze ticks of predicted characters are in predicted time, so compare them against the tick they were predicted to
26 int Tick = Client()->GameTick(Conn: g_Config.m_ClDummy);
27 if(GameClient()->m_aClients[ClientId].m_IsPredictedLocal)
28 {
29 Tick = Client()->PredGameTick(Conn: g_Config.m_ClDummy);
30 }
31 else if(GameClient()->m_aClients[ClientId].m_IsPredicted)
32 {
33 Tick = Client()->GetPredictionTick();
34 }
35
36 const int64_t Max = (int64_t)pCharacter->m_FreezeEnd - pCharacter->m_FreezeStart;
37 float FreezeProgress = std::clamp<int64_t>(val: Max - ((int64_t)Tick - pCharacter->m_FreezeStart), lo: 0, hi: Max) / (float)Max;
38 if(FreezeProgress <= 0.0f)
39 {
40 return;
41 }
42
43 vec2 Position = GameClient()->m_aClients[ClientId].m_RenderPos;
44 Position.x -= FreezeBarHalfWidth;
45 Position.y += 32;
46
47 float Alpha = GameClient()->IsOtherTeam(ClientId) ? g_Config.m_ClShowOthersAlpha / 100.0f : 1.0f;
48 if(pCharacter->m_IsInFreeze)
49 {
50 Alpha *= g_Config.m_ClFreezeBarsAlphaInsideFreeze / 100.0f;
51 }
52
53 RenderFreezeBarPos(x: Position.x, y: Position.y, Width: FreezeBarWidth, Height: FreezeBarHeight, Progress: FreezeProgress, Alpha);
54}
55
56void CFreezeBars::RenderFreezeBarPos(float x, const float y, const float Width, const float Height, float Progress, const float Alpha)
57{
58 Progress = std::clamp(val: Progress, lo: 0.0f, hi: 1.0f);
59
60 // what percentage of the end pieces is used for the progress indicator and how much is the rest
61 // half of the ends are used for the progress display
62 const float RestPct = 0.5f;
63 const float ProgPct = 0.5f;
64
65 const float EndWidth = Height; // to keep the correct scale - the height of the sprite is as long as the width
66 const float BarHeight = Height;
67 const float WholeBarWidth = Width;
68 const float MiddleBarWidth = WholeBarWidth - (EndWidth * 2.0f);
69 const float EndProgressWidth = EndWidth * ProgPct;
70 const float EndRestWidth = EndWidth * RestPct;
71 const float ProgressBarWidth = WholeBarWidth - (EndProgressWidth * 2.0f);
72 const float EndProgressProportion = EndProgressWidth / ProgressBarWidth;
73 const float MiddleProgressProportion = MiddleBarWidth / ProgressBarWidth;
74
75 // beginning piece
76 float BeginningPieceProgress = 1;
77 if(Progress <= EndProgressProportion)
78 {
79 BeginningPieceProgress = Progress / EndProgressProportion;
80 }
81
82 // full
83 Graphics()->WrapClamp();
84 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudFreezeBarFullLeft);
85 Graphics()->QuadsBegin();
86 Graphics()->SetColor(r: 1.f, g: 1.f, b: 1.f, a: Alpha);
87 // Subset: top_l, top_m, btm_m, btm_l
88 Graphics()->QuadsSetSubsetFree(x0: 0, y0: 0, x1: RestPct + ProgPct * BeginningPieceProgress, y1: 0, x2: RestPct + ProgPct * BeginningPieceProgress, y2: 1, x3: 0, y3: 1);
89 IGraphics::CQuadItem QuadFullBeginning(x, y, EndRestWidth + EndProgressWidth * BeginningPieceProgress, BarHeight);
90 Graphics()->QuadsDrawTL(pArray: &QuadFullBeginning, Num: 1);
91 Graphics()->QuadsEnd();
92
93 // empty
94 if(BeginningPieceProgress < 1.0f)
95 {
96 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudFreezeBarEmptyRight);
97 Graphics()->QuadsBegin();
98 Graphics()->SetColor(r: 1.f, g: 1.f, b: 1.f, a: Alpha);
99 // Subset: top_m, top_l, btm_l, btm_m | it is mirrored on the horizontal axe and rotated 180 degrees
100 Graphics()->QuadsSetSubsetFree(x0: ProgPct - ProgPct * BeginningPieceProgress, y0: 0, x1: 0, y1: 0, x2: 0, y2: 1, x3: ProgPct - ProgPct * BeginningPieceProgress, y3: 1);
101 IGraphics::CQuadItem QuadEmptyBeginning(x + EndRestWidth + EndProgressWidth * BeginningPieceProgress, y, EndProgressWidth * (1.0f - BeginningPieceProgress), BarHeight);
102 Graphics()->QuadsDrawTL(pArray: &QuadEmptyBeginning, Num: 1);
103 Graphics()->QuadsEnd();
104 }
105
106 // middle piece
107 x += EndWidth;
108
109 float MiddlePieceProgress = 1;
110 if(Progress <= EndProgressProportion + MiddleProgressProportion)
111 {
112 if(Progress <= EndProgressProportion)
113 {
114 MiddlePieceProgress = 0;
115 }
116 else
117 {
118 MiddlePieceProgress = (Progress - EndProgressProportion) / MiddleProgressProportion;
119 }
120 }
121
122 const float FullMiddleBarWidth = MiddleBarWidth * MiddlePieceProgress;
123 const float EmptyMiddleBarWidth = MiddleBarWidth - FullMiddleBarWidth;
124
125 // full freeze bar
126 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudFreezeBarFull);
127 Graphics()->QuadsBegin();
128 Graphics()->SetColor(r: 1.f, g: 1.f, b: 1.f, a: Alpha);
129 // select the middle portion of the sprite so we don't get edge bleeding
130 if(FullMiddleBarWidth <= EndWidth)
131 {
132 // prevent pixel puree, select only a small slice
133 // Subset: top_l, top_m, btm_m, btm_l
134 Graphics()->QuadsSetSubsetFree(x0: 0, y0: 0, x1: FullMiddleBarWidth / EndWidth, y1: 0, x2: FullMiddleBarWidth / EndWidth, y2: 1, x3: 0, y3: 1);
135 }
136 else
137 {
138 // Subset: top_l, top_r, btm_r, btm_l
139 Graphics()->QuadsSetSubsetFree(x0: 0, y0: 0, x1: 1, y1: 0, x2: 1, y2: 1, x3: 0, y3: 1);
140 }
141 IGraphics::CQuadItem QuadFull(x, y, FullMiddleBarWidth, BarHeight);
142 Graphics()->QuadsDrawTL(pArray: &QuadFull, Num: 1);
143 Graphics()->QuadsEnd();
144
145 // empty freeze bar
146 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudFreezeBarEmpty);
147 Graphics()->QuadsBegin();
148 Graphics()->SetColor(r: 1.f, g: 1.f, b: 1.f, a: Alpha);
149 // select the middle portion of the sprite so we don't get edge bleeding
150 if(EmptyMiddleBarWidth <= EndWidth)
151 {
152 // prevent pixel puree, select only a small slice
153 // Subset: top_m, top_l, btm_l, btm_m | it is mirrored on the horizontal axe and rotated 180 degrees
154 Graphics()->QuadsSetSubsetFree(x0: EmptyMiddleBarWidth / EndWidth, y0: 0, x1: 0, y1: 0, x2: 0, y2: 1, x3: EmptyMiddleBarWidth / EndWidth, y3: 1);
155 }
156 else
157 {
158 // Subset: top_r, top_l, btm_l, btm_r | it is mirrored on the horizontal axe and rotated 180 degrees
159 Graphics()->QuadsSetSubsetFree(x0: 1, y0: 0, x1: 0, y1: 0, x2: 0, y2: 1, x3: 1, y3: 1);
160 }
161
162 IGraphics::CQuadItem QuadEmpty(x + FullMiddleBarWidth, y, EmptyMiddleBarWidth, BarHeight);
163 Graphics()->QuadsDrawTL(pArray: &QuadEmpty, Num: 1);
164 Graphics()->QuadsEnd();
165
166 // end piece
167 x += MiddleBarWidth;
168 float EndingPieceProgress = 1;
169 if(Progress <= 1)
170 {
171 if(Progress <= (EndProgressProportion + MiddleProgressProportion))
172 {
173 EndingPieceProgress = 0;
174 }
175 else
176 {
177 EndingPieceProgress = (Progress - EndProgressProportion - MiddleProgressProportion) / EndProgressProportion;
178 }
179 }
180 if(EndingPieceProgress > 0.0f)
181 {
182 // full
183 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudFreezeBarFullLeft);
184 Graphics()->QuadsBegin();
185 Graphics()->SetColor(r: 1.f, g: 1.f, b: 1.f, a: Alpha);
186 // Subset: top_r, top_m, btm_m, btm_r | it is mirrored on the horizontal axe and rotated 180 degrees
187 Graphics()->QuadsSetSubsetFree(x0: 1, y0: 0, x1: 1.0f - ProgPct * EndingPieceProgress, y1: 0, x2: 1.0f - ProgPct * EndingPieceProgress, y2: 1, x3: 1, y3: 1);
188 IGraphics::CQuadItem QuadFullEnding(x, y, EndProgressWidth * EndingPieceProgress, BarHeight);
189 Graphics()->QuadsDrawTL(pArray: &QuadFullEnding, Num: 1);
190 Graphics()->QuadsEnd();
191 }
192 // empty
193 Graphics()->TextureSet(Texture: GameClient()->m_HudSkin.m_SpriteHudFreezeBarEmptyRight);
194 Graphics()->QuadsBegin();
195 Graphics()->SetColor(r: 1.f, g: 1.f, b: 1.f, a: Alpha);
196 // Subset: top_m, top_r, btm_r, btm_m
197 Graphics()->QuadsSetSubsetFree(x0: ProgPct - ProgPct * (1.0f - EndingPieceProgress), y0: 0, x1: 1, y1: 0, x2: 1, y2: 1, x3: ProgPct - ProgPct * (1.0f - EndingPieceProgress), y3: 1);
198 IGraphics::CQuadItem QuadEmptyEnding(x + EndProgressWidth * EndingPieceProgress, y, EndProgressWidth * (1.0f - EndingPieceProgress) + EndRestWidth, BarHeight);
199 Graphics()->QuadsDrawTL(pArray: &QuadEmptyEnding, Num: 1);
200 Graphics()->QuadsEnd();
201
202 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 0, BottomRightU: 1, BottomRightV: 1);
203 Graphics()->SetColor(r: 1.f, g: 1.f, b: 1.f, a: 1.f);
204 Graphics()->WrapNormal();
205}
206
207inline bool CFreezeBars::IsPlayerInfoAvailable(int ClientId) const
208{
209 return GameClient()->m_Snap.m_aCharacters[ClientId].m_Active &&
210 GameClient()->m_Snap.m_apPrevPlayerInfos[ClientId] != nullptr &&
211 GameClient()->m_Snap.m_apPlayerInfos[ClientId] != nullptr;
212}
213
214void CFreezeBars::OnRender()
215{
216 if(Client()->State() != IClient::STATE_ONLINE && Client()->State() != IClient::STATE_DEMOPLAYBACK)
217 return;
218
219 if(!g_Config.m_ClShowFreezeBars)
220 {
221 return;
222 }
223 // get screen edges to avoid rendering offscreen
224 CScreenRect ScreenRect = Graphics()->GetScreen();
225 // expand the edges to prevent popping in/out onscreen
226 //
227 // it is assumed that the tee with the freeze bar fit into a 240x240 box centered on the tee
228 // this may need to be changed or calculated differently in the future
229 constexpr float BorderBuffer = 120.0f;
230 ScreenRect.Expand(Size: BorderBuffer);
231
232 int LocalClientId = GameClient()->m_Snap.m_LocalClientId;
233
234 // render everyone else's freeze bar, then our own
235 for(int ClientId = 0; ClientId < MAX_CLIENTS; ClientId++)
236 {
237 if(ClientId == LocalClientId || !IsPlayerInfoAvailable(ClientId))
238 {
239 continue;
240 }
241
242 //don't render if the tee is offscreen
243 if(!ScreenRect.Inside(Position: GameClient()->m_aClients[ClientId].m_RenderPos))
244 {
245 continue;
246 }
247
248 RenderFreezeBar(ClientId);
249 }
250 if(LocalClientId != -1 && IsPlayerInfoAvailable(ClientId: LocalClientId))
251 {
252 RenderFreezeBar(ClientId: LocalClientId);
253 }
254}
255