1#include "multiview.h"
2
3#include <base/vmath.h>
4
5#include <engine/client.h>
6#include <engine/graphics.h>
7#include <engine/shared/config.h>
8#include <engine/shared/protocol.h>
9
10#include <game/client/components/camera.h>
11#include <game/client/components/spectator.h>
12#include <game/client/gameclient.h>
13
14#include <algorithm>
15#include <limits>
16
17static float MapValue(float MaxValue, float MinValue, float MaxRange, float MinRange, float Value)
18{
19 return (MaxRange - MinRange) / (MaxValue - MinValue) * (Value - MinValue) + MinRange;
20}
21
22CMultiView::CMultiView()
23{
24 m_Team = 0;
25 m_ShowHud = false;
26 m_Solo = false;
27 m_IsInit = false;
28 m_Teleported = false;
29 m_OldPos = vec2(0.0f, 0.0f);
30 m_OldPersonalZoom = 0.0f;
31 m_SecondChance = 0.0f;
32 m_OldCameraDistance = 0.0f;
33 OnReset();
34}
35
36void CMultiView::OnReset()
37{
38 m_PersonalZoom = 0.0f;
39 m_Activated = false;
40 m_IsInit = false;
41}
42
43void CMultiView::Reset()
44{
45 GameClient()->m_Camera.SetZoom(Target: CCamera::ZoomStepsToValue(Steps: g_Config.m_ClDefaultZoom - 10), Smoothness: g_Config.m_ClSmoothZoomTime, IsUser: true);
46 m_PersonalZoom = 0.0f;
47 m_Teleported = false;
48 m_OldCameraDistance = 0.0f;
49 OnReset();
50}
51
52void CMultiView::ViewPlayer(int ClientId)
53{
54 if(ClientId < 0 || ClientId >= MAX_CLIENTS)
55 return;
56
57 m_aViewedClients[ClientId].m_IsViewed = true;
58}
59
60void CMultiView::ToggleView(int ClientId)
61{
62 if(ClientId < 0 || ClientId >= MAX_CLIENTS)
63 return;
64
65 bool &Id = m_aViewedClients[ClientId].m_IsViewed;
66 Id = !Id;
67}
68
69void CMultiView::ClearViews()
70{
71 for(auto &ViewedClient : m_aViewedClients)
72 {
73 ViewedClient.m_IsViewed = false;
74 }
75}
76
77bool CMultiView::IsAnyViewed() const
78{
79 return std::any_of(first: std::begin(arr: m_aViewedClients), last: std::end(arr: m_aViewedClients), pred: [](const CViewedClient &ViewedClient) { return ViewedClient.m_IsViewed; });
80}
81
82int CMultiView::FindFirstViewed() const
83{
84 const auto *It = std::find_if(first: std::begin(arr: m_aViewedClients), last: std::end(arr: m_aViewedClients), pred: [](const CViewedClient &ViewedClient) { return ViewedClient.m_IsViewed && !ViewedClient.m_Vanish; });
85 if(It == std::end(arr: m_aViewedClients))
86 return SPEC_FREEVIEW;
87 return It - std::begin(arr: m_aViewedClients);
88}
89
90void CMultiView::ClearView(int ClientId)
91{
92 if(ClientId >= MAX_CLIENTS || ClientId < 0)
93 return;
94
95 m_aViewedClients[ClientId].Reset();
96}
97
98void CMultiView::ClearAll()
99{
100 for(auto &ViewedClient : m_aViewedClients)
101 {
102 ViewedClient.Reset();
103 }
104}
105
106void CMultiView::OnKillMessage(const CNetMsg_Sv_KillMsg *pKillMessage)
107{
108 // if we are spectating a static id set (team 0) and somebody killed, and its not a guy in solo, we remove them from the list
109 // never remove players from the list if it is a pvp server
110 if(IsAnyViewed() && m_Team == 0 && m_aViewedClients[pKillMessage->m_Victim].m_IsViewed && !GameClient()->m_aClients[pKillMessage->m_Victim].m_Spec && !m_Solo && !GameClient()->m_GameInfo.m_Pvp)
111 {
112 m_aViewedClients[pKillMessage->m_Victim].m_IsViewed = false;
113
114 // if everyone of a team killed, we have no ids to spectate anymore, so we disable multi view
115 if(!IsAnyViewed())
116 {
117 Reset();
118 }
119 else
120 {
121 // the "main" tee killed, search a new one
122 if(GameClient()->m_Snap.m_SpecInfo.m_SpectatorId == pKillMessage->m_Victim)
123 {
124 int NewClientId = FindFirstViewed();
125 if(NewClientId != SPEC_FREEVIEW)
126 {
127 CViewedClient &Client = m_aViewedClients[NewClientId];
128 Client.Reset();
129 Client.m_IsViewed = true;
130 GameClient()->m_Spectator.Spectate(SpectatorId: NewClientId);
131 }
132 }
133 }
134 }
135}
136
137void CMultiView::OnMessage(int MsgType, void *pRawMsg)
138{
139 switch(MsgType)
140 {
141 case NETMSGTYPE_SV_KILLMSG:
142 OnKillMessage(pKillMessage: static_cast<CNetMsg_Sv_KillMsg *>(pRawMsg));
143 break;
144 }
145}
146
147void CMultiView::UpdateActivation()
148{
149 // check if multi view got activated
150 if(m_IsInit || !m_Activated)
151 return;
152
153 int TeamId = 0;
154 if(GameClient()->m_Snap.m_SpecInfo.m_SpectatorId >= 0)
155 TeamId = GameClient()->m_Teams.Team(ClientId: GameClient()->m_Snap.m_SpecInfo.m_SpectatorId);
156
157 if(TeamId > MAX_CLIENTS || TeamId < 0)
158 TeamId = 0;
159
160 if(!Init(Team: TeamId))
161 {
162 Reset();
163 }
164}
165
166void CMultiView::CheckReset()
167{
168 if(!m_Activated && m_IsInit)
169 Reset();
170}
171
172bool CMultiView::Init(int Team)
173{
174 float Width, Height;
175 ClearAll();
176 m_IsInit = true;
177
178 // get the current view coordinates
179 Graphics()->CalcScreenParams(Aspect: Graphics()->ScreenAspect(), Zoom: GameClient()->m_Camera.m_Zoom, pWidth: &Width, pHeight: &Height);
180 vec2 AxisX = vec2(GameClient()->m_Camera.m_Center.x - (Width / 2.0f), GameClient()->m_Camera.m_Center.x + (Width / 2.0f));
181 vec2 AxisY = vec2(GameClient()->m_Camera.m_Center.y - (Height / 2.0f), GameClient()->m_Camera.m_Center.y + (Height / 2.0f));
182
183 if(Team > 0)
184 {
185 m_Team = Team;
186 for(int ClientId = 0; ClientId < MAX_CLIENTS; ClientId++)
187 {
188 if(GameClient()->m_Teams.Team(ClientId) == Team)
189 {
190 ViewPlayer(ClientId);
191 }
192 }
193 }
194 else
195 {
196 // we want to allow spectating players in teams directly if there is no other team on screen
197 // to do that, -1 is used temporarily for "we don't know which team to spectate yet"
198 m_Team = -1;
199
200 int Count = 0;
201 for(int ClientId = 0; ClientId < MAX_CLIENTS; ClientId++)
202 {
203 vec2 PlayerPos;
204
205 // get the position of the player
206 if(GameClient()->m_Snap.m_aCharacters[ClientId].m_Active)
207 PlayerPos = vec2(GameClient()->m_Snap.m_aCharacters[ClientId].m_Cur.m_X, GameClient()->m_Snap.m_aCharacters[ClientId].m_Cur.m_Y);
208 else if(GameClient()->m_aClients[ClientId].m_Spec)
209 PlayerPos = GameClient()->m_aClients[ClientId].m_SpecChar;
210 else
211 continue;
212
213 // skip players that aren't in view
214 if(PlayerPos.x <= AxisX.x || PlayerPos.x >= AxisX.y || PlayerPos.y <= AxisY.x || PlayerPos.y >= AxisY.y)
215 continue;
216
217 if(m_Team == -1)
218 {
219 // use the current player's team for now, but it might switch to team 0 if any other team is found
220 m_Team = GameClient()->m_Teams.Team(ClientId);
221 }
222 else if(m_Team != 0 && GameClient()->m_Teams.Team(ClientId) != m_Team)
223 {
224 // mismatched teams; remove all previously added players again and switch to team 0 instead
225 for(int PreviousClientId = 0; PreviousClientId < ClientId; ++PreviousClientId)
226 {
227 m_aViewedClients[PreviousClientId].m_IsViewed = false;
228 }
229 m_Team = 0;
230 }
231
232 ViewPlayer(ClientId);
233 Count++;
234 }
235
236 // might still be -1 if not a single player was in view; fallback to team 0 in that case
237 if(m_Team == -1)
238 m_Team = 0;
239
240 // we are spectating only one player
241 m_Solo = Count == 1;
242 }
243
244 if(IsAnyViewed())
245 {
246 int SpectatorId = GameClient()->m_Snap.m_SpecInfo.m_SpectatorId;
247 int NewSpectatorId = SPEC_FREEVIEW;
248
249 vec2 CurPosition = GameClient()->m_Camera.m_Center;
250 if(SpectatorId != SPEC_FREEVIEW && GameClient()->m_Snap.m_aCharacters[SpectatorId].m_Active)
251 {
252 const CNetObj_Character &CurCharacter = GameClient()->m_Snap.m_aCharacters[SpectatorId].m_Cur;
253 CurPosition.x = CurCharacter.m_X;
254 CurPosition.y = CurCharacter.m_Y;
255 }
256
257 int ClosestDistance = std::numeric_limits<int>::max();
258 for(int ClientId = 0; ClientId < MAX_CLIENTS; ClientId++)
259 {
260 if(!GameClient()->m_Snap.m_apPlayerInfos[ClientId] || GameClient()->m_Snap.m_apPlayerInfos[ClientId]->m_Team == TEAM_SPECTATORS || GameClient()->m_Teams.Team(ClientId) != m_Team)
261 continue;
262
263 vec2 PlayerPos;
264 if(GameClient()->m_Snap.m_aCharacters[ClientId].m_Active)
265 PlayerPos = vec2(GameClient()->m_aClients[ClientId].m_RenderPos.x, GameClient()->m_aClients[ClientId].m_RenderPos.y);
266 else if(GameClient()->m_aClients[ClientId].m_Spec) // tee is in spec
267 PlayerPos = GameClient()->m_aClients[ClientId].m_SpecChar;
268 else
269 continue;
270
271 int Distance = distance(a: CurPosition, b: PlayerPos);
272 if(NewSpectatorId == SPEC_FREEVIEW || Distance < ClosestDistance)
273 {
274 NewSpectatorId = ClientId;
275 ClosestDistance = Distance;
276 }
277 }
278
279 if(NewSpectatorId != SPEC_FREEVIEW)
280 GameClient()->m_Spectator.Spectate(SpectatorId: NewSpectatorId);
281 return true;
282 }
283 return false;
284}
285
286void CMultiView::UpdateView()
287{
288 const bool AnyViewed = IsAnyViewed();
289 vec2 MinPos, MaxPos;
290 float SumVel = 0.0f;
291 int AmountPlayers = 0;
292
293 for(int ClientId = 0; ClientId < MAX_CLIENTS; ClientId++)
294 {
295 CViewedClient &ViewedClient = m_aViewedClients[ClientId];
296 // look at players who are vanished
297 if(ViewedClient.m_Vanish)
298 {
299 // not in freeze anymore and the delay is over
300 if(ViewedClient.m_LastFreeze + 6.0f <= Client()->LocalTime() && GameClient()->m_aClients[ClientId].m_FreezeEnd == 0)
301 {
302 ViewedClient.m_Vanish = false;
303 ViewedClient.m_LastFreeze = 0.0f;
304 }
305 }
306
307 // we look at team 0 and the player is not in the spec list
308 if(AnyViewed && !ViewedClient.m_IsViewed)
309 continue;
310
311 // player is vanished
312 if(ViewedClient.m_Vanish)
313 continue;
314
315 // the player is not in the team we are spectating
316 if(GameClient()->m_Teams.Team(ClientId) != m_Team)
317 continue;
318
319 vec2 PlayerPos;
320 if(GameClient()->m_Snap.m_aCharacters[ClientId].m_Active)
321 PlayerPos = GameClient()->m_aClients[ClientId].m_RenderPos;
322 else if(GameClient()->m_aClients[ClientId].m_Spec) // tee is in spec
323 PlayerPos = GameClient()->m_aClients[ClientId].m_SpecChar;
324 else
325 continue;
326
327 // player is far away and frozen
328 if(distance(a: m_OldPos, b: PlayerPos) > 1100 && GameClient()->m_aClients[ClientId].m_FreezeEnd != 0)
329 {
330 // check if the player is frozen for more than 3 seconds, if so vanish them
331 if(ViewedClient.m_LastFreeze == 0.0f)
332 {
333 ViewedClient.m_LastFreeze = Client()->LocalTime();
334 }
335 else if(ViewedClient.m_LastFreeze + 3.0f <= Client()->LocalTime())
336 {
337 ViewedClient.m_Vanish = true;
338 // player we want to be vanished is our "main" tee, so lets switch the tee
339 if(ClientId == GameClient()->m_Snap.m_SpecInfo.m_SpectatorId)
340 GameClient()->m_Spectator.Spectate(SpectatorId: FindFirstViewed());
341 }
342 }
343 else if(ViewedClient.m_LastFreeze != 0)
344 {
345 ViewedClient.m_LastFreeze = 0;
346 }
347
348 // set the minimum and maximum position
349 if(AmountPlayers == 0)
350 {
351 MinPos = PlayerPos;
352 MaxPos = PlayerPos;
353 }
354 else
355 {
356 MinPos.x = std::min(a: MinPos.x, b: PlayerPos.x);
357 MaxPos.x = std::max(a: MaxPos.x, b: PlayerPos.x);
358 MinPos.y = std::min(a: MinPos.y, b: PlayerPos.y);
359 MaxPos.y = std::max(a: MaxPos.y, b: PlayerPos.y);
360 }
361
362 // sum up the velocity of all players we are spectating
363 const CNetObj_Character &CurrentCharacter = GameClient()->m_Snap.m_aCharacters[ClientId].m_Cur;
364 SumVel += length(a: vec2(CurrentCharacter.m_VelX / 256.0f, CurrentCharacter.m_VelY / 256.0f)) * 50.0f / 32.0f;
365 AmountPlayers++;
366 }
367
368 // if we have found no players, we disable multi view
369 if(AmountPlayers == 0)
370 {
371 if(m_SecondChance == 0.0f)
372 {
373 m_SecondChance = Client()->LocalTime() + 0.3f;
374 }
375 else if(m_SecondChance < Client()->LocalTime())
376 {
377 Reset();
378 }
379 return;
380 }
381 else if(m_SecondChance != 0.0f)
382 {
383 m_SecondChance = 0.0f;
384 }
385
386 // if we only have one tee that's in the list, we activate solo-mode
387 m_Solo = false;
388 for(auto &ViewedClient : m_aViewedClients)
389 {
390 if(ViewedClient.m_IsViewed)
391 {
392 if(!m_Solo)
393 {
394 m_Solo = true;
395 }
396 else
397 {
398 // we found a second view, so we can stop searching
399 m_Solo = false;
400 break;
401 }
402 }
403 }
404
405 vec2 TargetPos = vec2((MinPos.x + MaxPos.x) / 2.0f, (MinPos.y + MaxPos.y) / 2.0f);
406 // dont hide the position hud if its only one player
407 m_ShowHud = AmountPlayers == 1;
408 // get the average velocity
409 float AvgVel = std::clamp(val: SumVel / AmountPlayers, lo: 0.0f, hi: 1000.0f);
410
411 if(m_OldPersonalZoom == m_PersonalZoom)
412 GameClient()->m_Camera.SetZoom(Target: CalculateZoom(MinPos, MaxPos, Vel: AvgVel), Smoothness: g_Config.m_ClMultiViewZoomSmoothness, IsUser: false);
413 else
414 GameClient()->m_Camera.SetZoom(Target: CalculateZoom(MinPos, MaxPos, Vel: AvgVel), Smoothness: 50, IsUser: false);
415
416 GameClient()->m_Snap.m_SpecInfo.m_Position = m_OldPos + ((TargetPos - m_OldPos) * CalculateMultiplier(TargetPos));
417 m_OldPos = GameClient()->m_Snap.m_SpecInfo.m_Position;
418 GameClient()->m_Snap.m_SpecInfo.m_UsePosition = true;
419}
420
421float CMultiView::CalculateMultiplier(vec2 TargetPos)
422{
423 float MaxCameraDist = 200.0f;
424 float MinCameraDist = 20.0f;
425 float MaxVel = g_Config.m_ClMultiViewSensitivity / 150.0f;
426 float MinVel = 0.007f;
427 float CurrentCameraDistance = distance(a: m_OldPos, b: TargetPos);
428 float UpperLimit = 1.0f;
429
430 if(m_Teleported && CurrentCameraDistance <= 100.0f)
431 m_Teleported = false;
432
433 // somebody got teleported very likely
434 if((m_Teleported || CurrentCameraDistance - m_OldCameraDistance > 100.0f) && m_OldCameraDistance != 0.0f)
435 {
436 UpperLimit = 0.1f; // dont try to compensate it by flickering
437 m_Teleported = true;
438 }
439 m_OldCameraDistance = CurrentCameraDistance;
440
441 return std::clamp(val: MapValue(MaxValue: MaxCameraDist, MinValue: MinCameraDist, MaxRange: MaxVel, MinRange: MinVel, Value: CurrentCameraDistance), lo: MinVel, hi: UpperLimit);
442}
443
444float CMultiView::CalculateZoom(vec2 MinPos, vec2 MaxPos, float Vel)
445{
446 float Ratio = Graphics()->ScreenAspect();
447 float ZoomX = 0.0f, ZoomY;
448
449 // only calc two axis if the aspect ratio is not 1:1
450 if(Ratio != 1.0f)
451 ZoomX = (0.001309f - 0.000328f * Ratio) * (MaxPos.x - MinPos.x) + (0.741413f - 0.032959f * Ratio);
452
453 // calculate the according zoom with linear function
454 ZoomY = 0.001309f * (MaxPos.y - MinPos.y) + 0.741413f;
455 // choose the highest zoom
456 float Zoom = std::max(a: ZoomX, b: ZoomY);
457 // zoom out to maximum 10 percent of the current zoom for 70 velocity
458 float Diff = std::clamp(val: MapValue(MaxValue: 70.0f, MinValue: 15.0f, MaxRange: Zoom * 0.10f, MinRange: 0.0f, Value: Vel), lo: 0.0f, hi: Zoom * 0.10f);
459 // zoom should stay between 1.1 and 20.0
460 Zoom = std::clamp(val: Zoom + Diff, lo: 1.1f, hi: 20.0f);
461 // dont go below default zoom
462 Zoom = std::max(a: CCamera::ZoomStepsToValue(Steps: g_Config.m_ClDefaultZoom - 10), b: Zoom);
463 // add the user preference
464 Zoom -= Zoom * 0.1f * m_PersonalZoom;
465 m_OldPersonalZoom = m_PersonalZoom;
466
467 return Zoom;
468}
469
470void CMultiView::CViewedClient::Reset()
471{
472 m_IsViewed = false;
473 m_LastFreeze = 0.0f;
474 m_Vanish = false;
475}
476