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
4#include "spectator.h"
5
6#include <base/dbg.h>
7#include <base/log.h>
8
9#include <engine/graphics.h>
10#include <engine/shared/config.h>
11#include <engine/textrender.h>
12
13#include <generated/protocol.h>
14
15#include <game/client/animstate.h>
16#include <game/client/components/camera.h>
17#include <game/client/components/multiview.h>
18#include <game/client/gameclient.h>
19#include <game/localization.h>
20
21#include <limits>
22
23bool CSpectator::CanChangeSpectatorId()
24{
25 // don't change SpectatorId when not spectating
26 if(!GameClient()->m_Snap.m_SpecInfo.m_Active)
27 return false;
28
29 // stop follow mode from changing SpectatorId
30 if(Client()->State() == IClient::STATE_DEMOPLAYBACK && GameClient()->m_DemoSpecId == SPEC_FOLLOW)
31 return false;
32
33 return true;
34}
35
36void CSpectator::SpectateNext(bool Reverse)
37{
38 int CurIndex = -1;
39 const CNetObj_PlayerInfo **paPlayerInfos = GameClient()->m_Snap.m_apInfoByDDTeamName;
40
41 // m_SpectatorId may be uninitialized if m_Active is false
42 if(GameClient()->m_Snap.m_SpecInfo.m_Active)
43 {
44 for(int i = 0; i < MAX_CLIENTS; i++)
45 {
46 if(paPlayerInfos[i] && paPlayerInfos[i]->m_ClientId == GameClient()->m_Snap.m_SpecInfo.m_SpectatorId)
47 {
48 CurIndex = i;
49 break;
50 }
51 }
52 }
53
54 int Start;
55 if(CurIndex != -1)
56 {
57 if(Reverse)
58 Start = CurIndex - 1;
59 else
60 Start = CurIndex + 1;
61 }
62 else
63 {
64 if(Reverse)
65 Start = -1;
66 else
67 Start = 0;
68 }
69
70 int Increment = Reverse ? -1 : 1;
71
72 for(int i = 0; i < MAX_CLIENTS; i++)
73 {
74 int PlayerIndex = (Start + i * Increment) % MAX_CLIENTS;
75 // % in C++ takes the sign of the dividend, not divisor
76 if(PlayerIndex < 0)
77 PlayerIndex += MAX_CLIENTS;
78
79 const CNetObj_PlayerInfo *pPlayerInfo = paPlayerInfos[PlayerIndex];
80 if(pPlayerInfo && pPlayerInfo->m_Team != TEAM_SPECTATORS)
81 {
82 Spectate(SpectatorId: pPlayerInfo->m_ClientId);
83 break;
84 }
85 }
86}
87
88void CSpectator::ConKeySpectator(IConsole::IResult *pResult, void *pUserData)
89{
90 CSpectator *pSelf = (CSpectator *)pUserData;
91
92 if(pSelf->GameClient()->m_Scoreboard.IsActive())
93 return;
94
95 if(pSelf->GameClient()->m_Snap.m_SpecInfo.m_Active || pSelf->Client()->State() == IClient::STATE_DEMOPLAYBACK)
96 pSelf->m_Active = pResult->GetInteger(Index: 0) != 0;
97 else
98 pSelf->m_Active = false;
99}
100
101void CSpectator::ConSpectate(IConsole::IResult *pResult, void *pUserData)
102{
103 CSpectator *pSelf = (CSpectator *)pUserData;
104 if(!pSelf->CanChangeSpectatorId())
105 return;
106
107 const int SpectatorId = pResult->GetInteger(Index: 0);
108 if(SpectatorId != SPEC_FREEVIEW &&
109 SpectatorId != SPEC_FOLLOW &&
110 !in_range(a: SpectatorId, lower: 0, upper: (int)MAX_CLIENTS - 1))
111 {
112 log_error("spectator", "Invalid spectator ID: %d", SpectatorId);
113 return;
114 }
115 pSelf->Spectate(SpectatorId);
116}
117
118void CSpectator::ConSpectateNext(IConsole::IResult *pResult, void *pUserData)
119{
120 CSpectator *pSelf = (CSpectator *)pUserData;
121 if(!pSelf->CanChangeSpectatorId())
122 return;
123
124 pSelf->SpectateNext(Reverse: false);
125}
126
127void CSpectator::ConSpectatePrevious(IConsole::IResult *pResult, void *pUserData)
128{
129 CSpectator *pSelf = (CSpectator *)pUserData;
130 if(!pSelf->CanChangeSpectatorId())
131 return;
132
133 pSelf->SpectateNext(Reverse: true);
134}
135
136void CSpectator::ConSpectateClosest(IConsole::IResult *pResult, void *pUserData)
137{
138 CSpectator *pSelf = (CSpectator *)pUserData;
139 pSelf->SpectateClosest();
140}
141
142void CSpectator::ConMultiView(IConsole::IResult *pResult, void *pUserData)
143{
144 CSpectator *pSelf = (CSpectator *)pUserData;
145 int Input = pResult->GetInteger(Index: 0);
146
147 if(Input == -1)
148 pSelf->GameClient()->m_MultiView.ClearViews(); // remove everyone from multiview
149 else if(Input < MAX_CLIENTS && Input >= 0)
150 pSelf->GameClient()->m_MultiView.ToggleView(ClientId: Input); // activate or deactivate one player from multiview
151}
152
153CSpectator::CSpectator()
154{
155 m_SelectorMouse = vec2(0.0f, 0.0f);
156 OnReset();
157}
158
159void CSpectator::OnConsoleInit()
160{
161 Console()->Register(pName: "+spectate", pParams: "", Flags: CFGFLAG_CLIENT, pfnFunc: ConKeySpectator, pUser: this, pHelp: "Open spectator mode selector");
162 Console()->Register(pName: "spectate", pParams: "i[spectator-id]", Flags: CFGFLAG_CLIENT, pfnFunc: ConSpectate, pUser: this, pHelp: "Switch spectator mode");
163 Console()->Register(pName: "spectate_next", pParams: "", Flags: CFGFLAG_CLIENT, pfnFunc: ConSpectateNext, pUser: this, pHelp: "Spectate the next player");
164 Console()->Register(pName: "spectate_previous", pParams: "", Flags: CFGFLAG_CLIENT, pfnFunc: ConSpectatePrevious, pUser: this, pHelp: "Spectate the previous player");
165 Console()->Register(pName: "spectate_closest", pParams: "", Flags: CFGFLAG_CLIENT, pfnFunc: ConSpectateClosest, pUser: this, pHelp: "Spectate the closest player");
166 Console()->Register(pName: "spectate_multiview", pParams: "i[id]", Flags: CFGFLAG_CLIENT, pfnFunc: ConMultiView, pUser: this, pHelp: "Add/remove Client-IDs to spectate them exclusively (-1 to reset)");
167}
168
169bool CSpectator::OnCursorMove(float x, float y, IInput::ECursorType CursorType)
170{
171 if(!m_Active)
172 return false;
173
174 Ui()->ConvertMouseMove(pX: &x, pY: &y, CursorType);
175 m_SelectorMouse += vec2(x, y);
176 return true;
177}
178
179bool CSpectator::OnInput(const IInput::CEvent &Event)
180{
181 if(IsActive() && Event.m_Flags & IInput::FLAG_PRESS && Event.m_Key == KEY_ESCAPE)
182 {
183 OnRelease();
184 return true;
185 }
186
187 if(g_Config.m_ClSpectatorMouseclicks)
188 {
189 if(GameClient()->m_Snap.m_SpecInfo.m_Active && !IsActive() && !GameClient()->m_MultiView.IsActivated() &&
190 !Ui()->IsPopupOpen() && !GameClient()->m_GameConsole.IsActive() && !GameClient()->m_Menus.IsActive())
191 {
192 if(Event.m_Flags & IInput::FLAG_PRESS && Event.m_Key == KEY_MOUSE_1)
193 {
194 if(GameClient()->m_Snap.m_SpecInfo.m_SpectatorId != SPEC_FREEVIEW)
195 Spectate(SpectatorId: SPEC_FREEVIEW);
196 else
197 SpectateClosest();
198 return true;
199 }
200 }
201 }
202
203 if(GameClient()->m_Camera.SpectatingPlayer() && GameClient()->m_Camera.CanUseAutoSpecCamera())
204 {
205 if(Event.m_Flags & IInput::FLAG_PRESS && Event.m_Key == KEY_MOUSE_2)
206 {
207 GameClient()->m_Camera.ResetAutoSpecCamera();
208 return true;
209 }
210 }
211
212 return false;
213}
214
215void CSpectator::OnRelease()
216{
217 OnReset();
218}
219
220void CSpectator::OnRender()
221{
222 if(Client()->State() != IClient::STATE_ONLINE && Client()->State() != IClient::STATE_DEMOPLAYBACK)
223 return;
224
225 if(!GameClient()->m_MultiView.IsActivated() && m_MultiViewActivateDelay != 0.0f)
226 {
227 if(m_MultiViewActivateDelay <= Client()->LocalTime())
228 {
229 m_MultiViewActivateDelay = 0.0f;
230 GameClient()->m_MultiView.SetActivated(true);
231 }
232 }
233
234 if(!m_Active)
235 {
236 // closing the spectator menu
237 if(m_WasActive)
238 {
239 if(m_SelectedSpectatorId != NO_SELECTION)
240 {
241 if(m_SelectedSpectatorId == MULTI_VIEW)
242 GameClient()->m_MultiView.SetActivated(true);
243 else if(m_SelectedSpectatorId == SPEC_FREEVIEW || m_SelectedSpectatorId == SPEC_FOLLOW)
244 GameClient()->m_MultiView.SetActivated(false);
245
246 if(!GameClient()->m_MultiView.IsActivated())
247 Spectate(SpectatorId: m_SelectedSpectatorId);
248
249 if(GameClient()->m_MultiView.IsActivated() && m_SelectedSpectatorId != MULTI_VIEW && GameClient()->m_Teams.Team(ClientId: m_SelectedSpectatorId) != GameClient()->m_MultiView.Team())
250 {
251 GameClient()->m_MultiView.Reset();
252 Spectate(SpectatorId: m_SelectedSpectatorId);
253 m_MultiViewActivateDelay = Client()->LocalTime() + 0.3f;
254 }
255 }
256 m_WasActive = false;
257 }
258 return;
259 }
260
261 if((!GameClient()->m_Snap.m_SpecInfo.m_Active && Client()->State() != IClient::STATE_DEMOPLAYBACK) ||
262 GameClient()->m_Scoreboard.IsActive())
263 {
264 OnRelease();
265 return;
266 }
267
268 m_WasActive = true;
269 m_SelectedSpectatorId = NO_SELECTION;
270
271 // draw background
272 float Width = 400 * 3.0f * Graphics()->ScreenAspect();
273 float Height = 400 * 3.0f;
274 float ObjWidth = 300.0f;
275 float FontSize = 20.0f;
276 float BigFontSize = 20.0f;
277 float StartY = -190.0f;
278 float LineHeight = 60.0f;
279 float TeeSizeMod = 1.0f;
280 float RoundRadius = 30.0f;
281 bool MultiViewSelected = false;
282 int TotalPlayers = 0;
283 int PerLine = 8;
284 float BoxMove = -10.0f;
285 float BoxOffset = 0.0f;
286
287 for(const auto &pInfo : GameClient()->m_Snap.m_apInfoByDDTeamName)
288 {
289 if(!pInfo || pInfo->m_Team == TEAM_SPECTATORS)
290 continue;
291
292 ++TotalPlayers;
293 }
294
295 if(TotalPlayers > 96)
296 {
297 FontSize = 15.0f;
298 LineHeight = 15.0f;
299 TeeSizeMod = 0.3f;
300 PerLine = 32;
301 RoundRadius = 5.0f;
302 BoxMove = 3.0f;
303 BoxOffset = 6.0f;
304 }
305 else if(TotalPlayers > 64)
306 {
307 FontSize = 16.0f;
308 LineHeight = 19.0f;
309 TeeSizeMod = 0.45f;
310 PerLine = 24;
311 RoundRadius = 6.0f;
312 BoxMove = 3.0f;
313 BoxOffset = 6.0f;
314 }
315 else if(TotalPlayers > 32)
316 {
317 FontSize = 18.0f;
318 LineHeight = 30.0f;
319 TeeSizeMod = 0.7f;
320 PerLine = 16;
321 RoundRadius = 10.0f;
322 BoxMove = 3.0f;
323 BoxOffset = 6.0f;
324 }
325 if(TotalPlayers > 16)
326 {
327 ObjWidth = 600.0f;
328 }
329
330 const vec2 ScreenSize = vec2(Width, Height);
331 const vec2 ScreenCenter = ScreenSize / 2.0f;
332 CUIRect SpectatorRect = {.x: Width / 2.0f - ObjWidth, .y: Height / 2.0f - 300.0f, .w: ObjWidth * 2.0f, .h: 600.0f};
333 CUIRect SpectatorMouseRect;
334 SpectatorRect.Margin(Cut: 20.0f, pOtherRect: &SpectatorMouseRect);
335
336 const bool WasTouchPressed = m_TouchState.m_AnyPressed;
337 Ui()->UpdateTouchState(State&: m_TouchState);
338 if(m_TouchState.m_AnyPressed)
339 {
340 const vec2 TouchPos = (m_TouchState.m_PrimaryPosition - vec2(0.5f, 0.5f)) * ScreenSize;
341 if(SpectatorMouseRect.Inside(Point: ScreenCenter + TouchPos))
342 {
343 m_SelectorMouse = TouchPos;
344 }
345 }
346 else if(WasTouchPressed)
347 {
348 const vec2 TouchPos = (m_TouchState.m_PrimaryPosition - vec2(0.5f, 0.5f)) * ScreenSize;
349 if(!SpectatorRect.Inside(Point: ScreenCenter + TouchPos))
350 {
351 OnRelease();
352 return;
353 }
354 }
355
356 Graphics()->MapScreenToSize(Width, Height);
357
358 SpectatorRect.Draw(Color: ColorRGBA(0.0f, 0.0f, 0.0f, 0.3f), Corners: IGraphics::CORNER_ALL, Rounding: 20.0f);
359
360 // clamp mouse position to selector area
361 m_SelectorMouse.x = std::clamp(val: m_SelectorMouse.x, lo: -(ObjWidth - 20.0f), hi: ObjWidth - 20.0f);
362 m_SelectorMouse.y = std::clamp(val: m_SelectorMouse.y, lo: -280.0f, hi: 280.0f);
363
364 const bool MousePressed = Input()->KeyPress(Key: KEY_MOUSE_1) || m_TouchState.m_PrimaryPressed;
365
366 // draw selections
367 if((Client()->State() == IClient::STATE_DEMOPLAYBACK && GameClient()->m_DemoSpecId == SPEC_FREEVIEW) ||
368 (Client()->State() != IClient::STATE_DEMOPLAYBACK && GameClient()->m_Snap.m_SpecInfo.m_SpectatorId == SPEC_FREEVIEW))
369 {
370 Graphics()->DrawRect(x: Width / 2.0f - (ObjWidth - 20.0f), y: Height / 2.0f - 280.0f, w: ((ObjWidth * 2.0f) / 3.0f) - 40.0f, h: 60.0f, Color: ColorRGBA(1.0f, 1.0f, 1.0f, 0.25f), Corners: IGraphics::CORNER_ALL, Rounding: 20.0f);
371 }
372
373 if(GameClient()->m_MultiView.IsActivated())
374 {
375 Graphics()->DrawRect(x: Width / 2.0f - (ObjWidth - 20.0f) + (ObjWidth * 2.0f / 3.0f), y: Height / 2.0f - 280.0f, w: ((ObjWidth * 2.0f) / 3.0f) - 40.0f, h: 60.0f, Color: ColorRGBA(1.0f, 1.0f, 1.0f, 0.25f), Corners: IGraphics::CORNER_ALL, Rounding: 20.0f);
376 }
377
378 if(Client()->State() == IClient::STATE_DEMOPLAYBACK && GameClient()->m_Snap.m_LocalClientId >= 0 && GameClient()->m_DemoSpecId == SPEC_FOLLOW)
379 {
380 Graphics()->DrawRect(x: Width / 2.0f - (ObjWidth - 20.0f) + (ObjWidth * 2.0f * 2.0f / 3.0f), y: Height / 2.0f - 280.0f, w: ((ObjWidth * 2.0f) / 3.0f) - 40.0f, h: 60.0f, Color: ColorRGBA(1.0f, 1.0f, 1.0f, 0.25f), Corners: IGraphics::CORNER_ALL, Rounding: 20.0f);
381 }
382
383 bool FreeViewSelected = false;
384 if(m_SelectorMouse.x >= -(ObjWidth - 20.0f) && m_SelectorMouse.x <= -(ObjWidth - 20.0f) + ((ObjWidth * 2.0f) / 3.0f) - 40.0f &&
385 m_SelectorMouse.y >= -280.0f && m_SelectorMouse.y <= -220.0f)
386 {
387 m_SelectedSpectatorId = SPEC_FREEVIEW;
388 FreeViewSelected = true;
389 if(MousePressed)
390 {
391 GameClient()->m_MultiView.SetActivated(false);
392 Spectate(SpectatorId: m_SelectedSpectatorId);
393 }
394 }
395 TextRender()->TextColor(r: 1.0f, g: 1.0f, b: 1.0f, a: FreeViewSelected ? 1.0f : 0.5f);
396 TextRender()->Text(x: Width / 2.0f - (ObjWidth - 40.0f), y: Height / 2.0f - 280.f + (60.f - BigFontSize) / 2.f, Size: BigFontSize, pText: Localize(pStr: "Free-View"), LineWidth: -1.0f);
397
398 if(m_SelectorMouse.x >= -(ObjWidth - 20.0f) + (ObjWidth * 2.0f / 3.0f) && m_SelectorMouse.x <= -(ObjWidth - 20.0f) + (ObjWidth * 2.0f / 3.0f) + ((ObjWidth * 2.0f) / 3.0f) - 40.0f &&
399 m_SelectorMouse.y >= -280.0f && m_SelectorMouse.y <= -220.0f)
400 {
401 m_SelectedSpectatorId = MULTI_VIEW;
402 MultiViewSelected = true;
403 if(MousePressed)
404 {
405 GameClient()->m_MultiView.SetActivated(true);
406 }
407 }
408 TextRender()->TextColor(r: 1.0f, g: 1.0f, b: 1.0f, a: MultiViewSelected ? 1.0f : 0.5f);
409 TextRender()->Text(x: Width / 2.0f - (ObjWidth - 40.0f) + (ObjWidth * 2.0f / 3.0f), y: Height / 2.0f - 280.f + (60.f - BigFontSize) / 2.f, Size: BigFontSize, pText: Localize(pStr: "Multi-View"), LineWidth: -1.0f);
410
411 if(Client()->State() == IClient::STATE_DEMOPLAYBACK && GameClient()->m_Snap.m_LocalClientId >= 0)
412 {
413 bool FollowSelected = false;
414 if(m_SelectorMouse.x >= -(ObjWidth - 20.0f) + (ObjWidth * 2.0f * 2.0f / 3.0f) && m_SelectorMouse.x <= -(ObjWidth - 20.0f) + (ObjWidth * 2.0f * 2.0f / 3.0f) + ((ObjWidth * 2.0f) / 3.0f) - 40.0f &&
415 m_SelectorMouse.y >= -280.0f && m_SelectorMouse.y <= -220.0f)
416 {
417 m_SelectedSpectatorId = SPEC_FOLLOW;
418 FollowSelected = true;
419 if(MousePressed)
420 {
421 GameClient()->m_MultiView.SetActivated(false);
422 Spectate(SpectatorId: m_SelectedSpectatorId);
423 }
424 }
425 TextRender()->TextColor(r: 1.0f, g: 1.0f, b: 1.0f, a: FollowSelected ? 1.0f : 0.5f);
426 TextRender()->Text(x: Width / 2.0f - (ObjWidth - 40.0f) + (ObjWidth * 2.0f * 2.0f / 3.0f), y: Height / 2.0f - 280.0f + (60.f - BigFontSize) / 2.f, Size: BigFontSize, pText: Localize(pStr: "Follow"), LineWidth: -1.0f);
427 }
428
429 float x = -(ObjWidth - 35.0f), y = StartY;
430
431 int OldDDTeam = -1;
432
433 for(int i = 0, Count = 0; i < MAX_CLIENTS; ++i)
434 {
435 if(!GameClient()->m_Snap.m_apInfoByDDTeamName[i] || GameClient()->m_Snap.m_apInfoByDDTeamName[i]->m_Team == TEAM_SPECTATORS)
436 continue;
437
438 ++Count;
439
440 if(Count == PerLine + 1 || (Count > PerLine + 1 && (Count - 1) % PerLine == 0))
441 {
442 x += 290.0f;
443 y = StartY;
444 }
445
446 const CNetObj_PlayerInfo *pInfo = GameClient()->m_Snap.m_apInfoByDDTeamName[i];
447 int DDTeam = GameClient()->m_Teams.Team(ClientId: pInfo->m_ClientId);
448 int NextDDTeam = 0;
449
450 for(int j = i + 1; j < MAX_CLIENTS; j++)
451 {
452 const CNetObj_PlayerInfo *pInfo2 = GameClient()->m_Snap.m_apInfoByDDTeamName[j];
453
454 if(!pInfo2 || pInfo2->m_Team == TEAM_SPECTATORS)
455 continue;
456
457 NextDDTeam = GameClient()->m_Teams.Team(ClientId: pInfo2->m_ClientId);
458 break;
459 }
460
461 if(OldDDTeam == -1)
462 {
463 for(int j = i - 1; j >= 0; j--)
464 {
465 const CNetObj_PlayerInfo *pInfo2 = GameClient()->m_Snap.m_apInfoByDDTeamName[j];
466
467 if(!pInfo2 || pInfo2->m_Team == TEAM_SPECTATORS)
468 continue;
469
470 OldDDTeam = GameClient()->m_Teams.Team(ClientId: pInfo2->m_ClientId);
471 break;
472 }
473 }
474
475 if(DDTeam != TEAM_FLOCK)
476 {
477 const ColorRGBA Color = GameClient()->GetDDTeamColor(DDTeam).WithAlpha(alpha: 0.5f);
478 int Corners = 0;
479 if(OldDDTeam != DDTeam)
480 Corners |= IGraphics::CORNER_TL | IGraphics::CORNER_TR;
481 if(NextDDTeam != DDTeam)
482 Corners |= IGraphics::CORNER_BL | IGraphics::CORNER_BR;
483 Graphics()->DrawRect(x: Width / 2.0f + x - 10.0f + BoxOffset, y: Height / 2.0f + y + BoxMove, w: 270.0f - BoxOffset, h: LineHeight, Color, Corners, Rounding: RoundRadius);
484 }
485
486 OldDDTeam = DDTeam;
487
488 if((Client()->State() == IClient::STATE_DEMOPLAYBACK && GameClient()->m_DemoSpecId == GameClient()->m_Snap.m_apInfoByDDTeamName[i]->m_ClientId) || (Client()->State() != IClient::STATE_DEMOPLAYBACK && GameClient()->m_Snap.m_SpecInfo.m_SpectatorId == GameClient()->m_Snap.m_apInfoByDDTeamName[i]->m_ClientId))
489 {
490 Graphics()->DrawRect(x: Width / 2.0f + x - 10.0f + BoxOffset, y: Height / 2.0f + y + BoxMove, w: 270.0f - BoxOffset, h: LineHeight, Color: ColorRGBA(1.0f, 1.0f, 1.0f, 0.25f), Corners: IGraphics::CORNER_ALL, Rounding: RoundRadius);
491 }
492
493 bool PlayerSelected = false;
494 if(m_SelectorMouse.x >= x - 10.0f && m_SelectorMouse.x < x + 260.0f &&
495 m_SelectorMouse.y >= y - (LineHeight / 6.0f) && m_SelectorMouse.y < y + (LineHeight * 5.0f / 6.0f))
496 {
497 m_SelectedSpectatorId = GameClient()->m_Snap.m_apInfoByDDTeamName[i]->m_ClientId;
498 PlayerSelected = true;
499 if(MousePressed)
500 {
501 if(GameClient()->m_MultiView.IsActivated())
502 {
503 if(GameClient()->m_MultiView.Team() == DDTeam)
504 {
505 GameClient()->m_MultiView.ToggleView(ClientId: m_SelectedSpectatorId);
506 const int SpectatorId = GameClient()->m_Snap.m_SpecInfo.m_SpectatorId;
507
508 if(SpectatorId < 0 || !GameClient()->m_MultiView.IsViewed(ClientId: SpectatorId))
509 {
510 int NewClientId = GameClient()->m_MultiView.FindFirstViewed();
511 if(NewClientId != SPEC_FREEVIEW)
512 {
513 GameClient()->m_MultiView.ClearView(ClientId: NewClientId);
514 GameClient()->m_MultiView.ViewPlayer(ClientId: NewClientId);
515 Spectate(SpectatorId: NewClientId);
516 }
517 }
518 }
519 else
520 {
521 GameClient()->m_MultiView.Reset();
522 Spectate(SpectatorId: m_SelectedSpectatorId);
523 m_MultiViewActivateDelay = Client()->LocalTime() + 0.3f;
524 }
525 }
526 else
527 {
528 Spectate(SpectatorId: m_SelectedSpectatorId);
529 }
530 }
531 }
532 float TeeAlpha;
533 if(Client()->State() == IClient::STATE_DEMOPLAYBACK &&
534 !GameClient()->m_Snap.m_aCharacters[GameClient()->m_Snap.m_apInfoByDDTeamName[i]->m_ClientId].m_Active)
535 {
536 TextRender()->TextColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 0.25f);
537 TeeAlpha = 0.5f;
538 }
539 else
540 {
541 TextRender()->TextColor(r: 1.0f, g: 1.0f, b: 1.0f, a: PlayerSelected ? 1.0f : 0.5f);
542 TeeAlpha = 1.0f;
543 }
544 CTextCursor NameCursor;
545 NameCursor.SetPosition(vec2(Width / 2.0f + x + 50.0f, Height / 2.0f + y + BoxMove + (LineHeight - FontSize) / 2.f));
546 NameCursor.m_FontSize = FontSize;
547 NameCursor.m_Flags |= TEXTFLAG_ELLIPSIS_AT_END;
548 NameCursor.m_LineWidth = 180.0f;
549 if(g_Config.m_ClShowIds)
550 {
551 char aClientId[16];
552 GameClient()->FormatClientId(ClientId: GameClient()->m_Snap.m_apInfoByDDTeamName[i]->m_ClientId, aClientId, Format: EClientIdFormat::INDENT_AUTO);
553 TextRender()->TextEx(pCursor: &NameCursor, pText: aClientId);
554 }
555 TextRender()->TextEx(pCursor: &NameCursor, pText: GameClient()->m_aClients[GameClient()->m_Snap.m_apInfoByDDTeamName[i]->m_ClientId].m_aName);
556
557 if(GameClient()->m_MultiView.IsActivated())
558 {
559 if(GameClient()->m_MultiView.IsViewed(ClientId: GameClient()->m_Snap.m_apInfoByDDTeamName[i]->m_ClientId))
560 {
561 TextRender()->TextColor(r: 0.1f, g: 1.0f, b: 0.1f, a: PlayerSelected ? 1.0f : 0.5f);
562 TextRender()->Text(x: Width / 2.0f + x + 50.0f + 180.0f, y: Height / 2.0f + y + BoxMove + (LineHeight - FontSize) / 2.f, Size: FontSize - 3, pText: "⬤", LineWidth: 220.0f);
563 }
564 else if(GameClient()->m_MultiView.Team() == DDTeam)
565 {
566 TextRender()->TextColor(r: 1.0f, g: 0.1f, b: 0.1f, a: PlayerSelected ? 1.0f : 0.5f);
567 TextRender()->Text(x: Width / 2.0f + x + 50.0f + 180.0f, y: Height / 2.0f + y + BoxMove + (LineHeight - FontSize) / 2.f, Size: FontSize - 3, pText: "◯", LineWidth: 220.0f);
568 }
569 }
570
571 // flag
572 if(GameClient()->m_Snap.m_pGameInfoObj && (GameClient()->m_Snap.m_pGameInfoObj->m_GameFlags & GAMEFLAG_FLAGS) &&
573 GameClient()->m_Snap.m_pGameDataObj && (GameClient()->m_Snap.m_pGameDataObj->m_FlagCarrierRed == GameClient()->m_Snap.m_apInfoByDDTeamName[i]->m_ClientId || GameClient()->m_Snap.m_pGameDataObj->m_FlagCarrierBlue == GameClient()->m_Snap.m_apInfoByDDTeamName[i]->m_ClientId))
574 {
575 if(GameClient()->m_Snap.m_pGameDataObj->m_FlagCarrierBlue == GameClient()->m_Snap.m_apInfoByDDTeamName[i]->m_ClientId)
576 Graphics()->TextureSet(Texture: GameClient()->m_GameSkin.m_SpriteFlagBlue);
577 else
578 Graphics()->TextureSet(Texture: GameClient()->m_GameSkin.m_SpriteFlagRed);
579
580 Graphics()->QuadsBegin();
581 Graphics()->QuadsSetSubset(TopLeftU: 1, TopLeftV: 0, BottomRightU: 0, BottomRightV: 1);
582
583 float Size = LineHeight;
584 IGraphics::CQuadItem QuadItem(Width / 2.0f + x - LineHeight / 5.0f, Height / 2.0f + y - LineHeight / 3.0f, Size / 2.0f, Size);
585 Graphics()->QuadsDrawTL(pArray: &QuadItem, Num: 1);
586 Graphics()->QuadsEnd();
587 }
588
589 CTeeRenderInfo TeeInfo = GameClient()->m_aClients[GameClient()->m_Snap.m_apInfoByDDTeamName[i]->m_ClientId].m_RenderInfo;
590 TeeInfo.m_Size *= TeeSizeMod;
591
592 RenderTools()->RenderIdleTeeAtCenter(pInfo: &TeeInfo, Center: vec2(Width / 2.0f + x + 20.0f, Height / 2.0f + y + BoxMove + LineHeight / 2.0f), Emote: EMOTE_NORMAL, Dir: vec2(1.0f, 0.0f), Alpha: TeeAlpha);
593
594 if(GameClient()->m_aClients[GameClient()->m_Snap.m_apInfoByDDTeamName[i]->m_ClientId].m_Friend)
595 {
596 TextRender()->TextColor(Color: color_cast<ColorRGBA>(hsl: ColorHSLA(g_Config.m_ClMessageFriendColor)));
597 TextRender()->Text(x: Width / 2.0f + x - TeeInfo.m_Size / 2.0f, y: Height / 2.0f + y + BoxMove + (LineHeight - FontSize) / 2.f, Size: FontSize, pText: "♥", LineWidth: 220.0f);
598 TextRender()->TextColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f);
599 }
600
601 y += LineHeight;
602 }
603 TextRender()->TextColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f);
604
605 RenderTools()->RenderCursor(Center: ScreenCenter + m_SelectorMouse, Size: 48.0f);
606}
607
608void CSpectator::OnReset()
609{
610 m_WasActive = false;
611 m_Active = false;
612 m_SelectedSpectatorId = NO_SELECTION;
613 m_TouchState = CUi::CTouchState();
614 m_MultiViewActivateDelay = 0.0f;
615}
616
617void CSpectator::Spectate(int SpectatorId)
618{
619 dbg_assert(SpectatorId == SPEC_FREEVIEW || SpectatorId == SPEC_FOLLOW || in_range(SpectatorId, 0, (int)MAX_CLIENTS - 1), "Invalid SpectatorId: %d", SpectatorId);
620
621 if(Client()->State() == IClient::STATE_DEMOPLAYBACK)
622 {
623 GameClient()->m_DemoSpecId = SpectatorId;
624 // The tick must be rendered for the spectator mode to be updated, so we do it manually when demo playback is paused
625 // TODO: https://github.com/ddnet/ddnet/issues/11681
626 if(DemoPlayer()->BaseInfo()->m_Paused)
627 GameClient()->m_Menus.DemoSeekTick(TickOffset: IDemoPlayer::TICK_CURRENT);
628 return;
629 }
630
631 if(GameClient()->m_Snap.m_SpecInfo.m_SpectatorId == SpectatorId)
632 return;
633
634 if(Client()->IsSixup())
635 {
636 protocol7::CNetMsg_Cl_SetSpectatorMode Msg;
637 if(SpectatorId == SPEC_FREEVIEW)
638 {
639 Msg.m_SpecMode = protocol7::SPEC_FREEVIEW;
640 Msg.m_SpectatorId = -1;
641 }
642 else
643 {
644 Msg.m_SpecMode = protocol7::SPEC_PLAYER;
645 Msg.m_SpectatorId = SpectatorId;
646 }
647 Client()->SendPackMsgActive(pMsg: &Msg, Flags: MSGFLAG_VITAL, NoTranslate: true);
648 return;
649 }
650 CNetMsg_Cl_SetSpectatorMode Msg;
651 Msg.m_SpectatorId = SpectatorId;
652 Client()->SendPackMsgActive(pMsg: &Msg, Flags: MSGFLAG_VITAL);
653}
654
655void CSpectator::SpectateClosest()
656{
657 if(!CanChangeSpectatorId())
658 return;
659
660 const CGameClient::CSnapState &Snap = GameClient()->m_Snap;
661 int SpectatorId = Snap.m_SpecInfo.m_SpectatorId;
662
663 int NewSpectatorId = -1;
664
665 vec2 CurPosition = GameClient()->m_Camera.m_Center;
666 if(SpectatorId != SPEC_FREEVIEW && Snap.m_aCharacters[SpectatorId].m_Active)
667 {
668 const CNetObj_Character &CurCharacter = Snap.m_aCharacters[SpectatorId].m_Cur;
669 CurPosition = vec2(CurCharacter.m_X, CurCharacter.m_Y);
670 }
671
672 int ClosestDistance = std::numeric_limits<int>::max();
673 for(int ClientId = 0; ClientId < MAX_CLIENTS; ClientId++)
674 {
675 if(ClientId == SpectatorId || !Snap.m_aCharacters[ClientId].m_Active || !Snap.m_apPlayerInfos[ClientId] || Snap.m_apPlayerInfos[ClientId]->m_Team == TEAM_SPECTATORS)
676 continue;
677
678 if(Client()->State() != IClient::STATE_DEMOPLAYBACK && ClientId == Snap.m_LocalClientId)
679 continue;
680
681 const CNetObj_Character &MaybeClosestCharacter = Snap.m_aCharacters[ClientId].m_Cur;
682 int Distance = distance(a: CurPosition, b: vec2(MaybeClosestCharacter.m_X, MaybeClosestCharacter.m_Y));
683 if(NewSpectatorId == -1 || Distance < ClosestDistance)
684 {
685 NewSpectatorId = ClientId;
686 ClosestDistance = Distance;
687 }
688 }
689 if(NewSpectatorId > -1)
690 Spectate(SpectatorId: NewSpectatorId);
691}
692