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 "camera.h"
5
6#include "controls.h"
7
8#include <base/log.h>
9#include <base/vmath.h>
10
11#include <engine/shared/config.h>
12
13#include <game/client/gameclient.h>
14#include <game/collision.h>
15#include <game/localization.h>
16#include <game/mapitems.h>
17
18CCamera::CCamera()
19{
20 m_CamType = CAMTYPE_UNDEFINED;
21 m_ZoomSet = false;
22 m_Zoom = 1.0f;
23 m_Zooming = false;
24 m_ForceFreeview = false;
25 m_GotoSwitchOffset = 0;
26 m_GotoTeleOffset = 0;
27 m_GotoSwitchLastPos = ivec2(-1, -1);
28 m_GotoTeleLastPos = ivec2(-1, -1);
29
30 std::fill(first: std::begin(arr&: m_aLastPos), last: std::end(arr&: m_aLastPos), value: vec2(0.0f, 0.0f));
31 m_PrevCenter = vec2(0, 0);
32 m_Center = vec2(0, 0);
33
34 m_PrevSpecId = -1;
35 m_WasSpectating = false;
36
37 m_CameraSmoothing = false;
38
39 m_LastTargetPos = vec2(0, 0);
40 m_DyncamTargetCameraOffset = vec2(0, 0);
41 std::fill(first: std::begin(arr&: m_aDyncamCurrentCameraOffset), last: std::end(arr&: m_aDyncamCurrentCameraOffset), value: vec2(0.0f, 0.0f));
42 m_DyncamSmoothingSpeedBias = 0.5f;
43
44 m_AutoSpecCamera = true;
45 m_AutoSpecCameraZooming = false;
46 m_CanUseCameraInfo = false;
47 m_UsingAutoSpecCamera = false;
48
49 m_aAutoSpecCameraTooltip[0] = '\0';
50}
51
52float CCamera::CameraSmoothingProgress(float CurrentTime) const
53{
54 float Progress = (CurrentTime - m_CameraSmoothingStart) / (m_CameraSmoothingEnd - m_CameraSmoothingStart);
55 return 1.0 - std::pow(x: 2.0, y: -10.0 * Progress);
56}
57
58float CCamera::ZoomProgress(float CurrentTime) const
59{
60 return (CurrentTime - m_ZoomSmoothingStart) / (m_ZoomSmoothingEnd - m_ZoomSmoothingStart);
61}
62
63void CCamera::ScaleZoom(float Factor)
64{
65 float CurrentTarget = m_Zooming ? m_ZoomSmoothingTarget : m_Zoom;
66 ChangeZoom(Target: CurrentTarget * Factor, Smoothness: GameClient()->m_Snap.m_SpecInfo.m_Active && GameClient()->m_MultiView.IsActivated() ? g_Config.m_ClMultiViewZoomSmoothness : g_Config.m_ClSmoothZoomTime, IsUser: true);
67
68 m_AutoSpecCamera = false;
69}
70
71float CCamera::MaxZoomLevel()
72{
73 return (g_Config.m_ClLimitMaxZoomLevel) ? ((Graphics()->IsTileBufferingEnabled() ? 240 : 30)) : 2000;
74}
75
76float CCamera::MinZoomLevel()
77{
78 return 0.01f;
79}
80
81void CCamera::ChangeZoom(float Target, int Smoothness, bool IsUser)
82{
83 if(Target > MaxZoomLevel() || Target < MinZoomLevel())
84 {
85 return;
86 }
87
88 float Now = Client()->LocalTime();
89 float Current = m_Zoom;
90 float Derivative = 0.0f;
91 if(m_Zooming)
92 {
93 float Progress = ZoomProgress(CurrentTime: Now);
94 Current = m_ZoomSmoothing.Evaluate(t: Progress);
95 Derivative = m_ZoomSmoothing.Derivative(t: Progress);
96 }
97
98 m_ZoomSmoothingTarget = Target;
99 m_ZoomSmoothing = CCubicBezier::With(Start: Current, StartDerivative: Derivative, EndDerivative: 0, End: m_ZoomSmoothingTarget);
100 m_ZoomSmoothingStart = Now;
101 m_ZoomSmoothingEnd = Now + (float)Smoothness / 1000;
102
103 if(IsUser)
104 m_UserZoomTarget = Target;
105
106 m_Zooming = true;
107}
108
109void CCamera::ResetAutoSpecCamera()
110{
111 m_AutoSpecCamera = true;
112}
113
114void CCamera::UpdateCamera()
115{
116 // use hardcoded smooth camera for spectating unless player explicitly turn it off
117 bool CanUseCameraInfo = !GameClient()->m_MultiView.IsActivated();
118 if(Client()->State() == IClient::STATE_DEMOPLAYBACK)
119 {
120 // only follow mode have the correct camera info
121 CanUseCameraInfo = CanUseCameraInfo && GameClient()->m_DemoSpecId == SPEC_FOLLOW;
122 }
123 else
124 {
125 CanUseCameraInfo = CanUseCameraInfo && GameClient()->m_Snap.m_SpecInfo.m_Active &&
126 GameClient()->m_Snap.m_SpecInfo.m_SpectatorId >= 0 &&
127 GameClient()->m_Snap.m_SpecInfo.m_SpectatorId != GameClient()->m_aLocalIds[0] &&
128 (!GameClient()->Client()->DummyConnected() || GameClient()->m_Snap.m_SpecInfo.m_SpectatorId != GameClient()->m_aLocalIds[1]);
129 }
130
131 bool UsingAutoSpecCamera = m_AutoSpecCamera && CanUseAutoSpecCamera();
132 float CurrentZoom = m_Zooming ? m_ZoomSmoothingTarget : m_Zoom;
133 bool ZoomChanged = false;
134 if(CanUseCameraInfo && UsingAutoSpecCamera && CurrentZoom != GameClient()->m_Snap.m_SpecInfo.m_Zoom)
135 {
136 // start spectating player / turn on auto spec camera
137 bool ChangeTarget = m_PrevSpecId != GameClient()->m_Snap.m_SpecInfo.m_SpectatorId;
138 float SmoothTime = ChangeTarget ? g_Config.m_ClSmoothSpectatingTime : 250;
139 ChangeZoom(Target: GameClient()->m_Snap.m_SpecInfo.m_Zoom, Smoothness: SmoothTime, IsUser: false);
140
141 // it is auto spec camera zooming if only the zoom is changed during activation, not at the start of the activation
142 m_AutoSpecCameraZooming = !ChangeTarget && CanUseCameraInfo && m_UsingAutoSpecCamera;
143
144 ZoomChanged = true;
145 }
146 else if((CanUseCameraInfo && !UsingAutoSpecCamera) && CurrentZoom != m_UserZoomTarget)
147 {
148 // turning off auto spec camera
149 ChangeZoom(Target: m_UserZoomTarget, Smoothness: g_Config.m_ClSmoothZoomTime, IsUser: false);
150 m_AutoSpecCameraZooming = false;
151
152 ZoomChanged = true;
153 }
154 else if(!CanUseCameraInfo && CurrentZoom != m_UserZoomTarget)
155 {
156 // stop spectating player
157 if(!GameClient()->m_MultiView.IsActivated())
158 ChangeZoom(Target: m_UserZoomTarget, Smoothness: g_Config.m_ClSmoothZoomTime, IsUser: false);
159 m_AutoSpecCameraZooming = false;
160
161 ZoomChanged = true;
162 }
163
164 // snap zoom when going in and out of spectating
165 if(ZoomChanged && m_WasSpectating != GameClient()->m_Snap.m_SpecInfo.m_Active)
166 {
167 m_Zoom = m_ZoomSmoothingTarget;
168 m_Zooming = false;
169 }
170
171 if(m_Zooming)
172 {
173 float Time = Client()->LocalTime();
174 if(Time >= m_ZoomSmoothingEnd)
175 {
176 m_Zoom = m_ZoomSmoothingTarget;
177 m_Zooming = false;
178 m_AutoSpecCameraZooming = false;
179 }
180 else
181 {
182 const float OldLevel = m_Zoom;
183 m_Zoom = m_ZoomSmoothing.Evaluate(t: ZoomProgress(CurrentTime: Time));
184 if((OldLevel < m_ZoomSmoothingTarget && m_Zoom > m_ZoomSmoothingTarget) || (OldLevel > m_ZoomSmoothingTarget && m_Zoom < m_ZoomSmoothingTarget))
185 {
186 m_Zoom = m_ZoomSmoothingTarget;
187 m_Zooming = false;
188 m_AutoSpecCameraZooming = false;
189 }
190 }
191 m_Zoom = std::clamp(val: m_Zoom, lo: MinZoomLevel(), hi: MaxZoomLevel());
192 }
193
194 if(!ZoomAllowed())
195 {
196 m_ZoomSet = false;
197 m_Zoom = 1.0f;
198 m_Zooming = false;
199 m_AutoSpecCameraZooming = false;
200 }
201 else if(!m_ZoomSet && g_Config.m_ClDefaultZoom != 10)
202 {
203 m_ZoomSet = true;
204 OnReset();
205 }
206
207 if(GameClient()->m_Snap.m_SpecInfo.m_Active && !GameClient()->m_Snap.m_SpecInfo.m_UsePosition)
208 {
209 m_aDyncamCurrentCameraOffset[g_Config.m_ClDummy] = vec2(0, 0);
210 m_CanUseCameraInfo = CanUseCameraInfo;
211 m_UsingAutoSpecCamera = UsingAutoSpecCamera;
212 return;
213 }
214
215 vec2 TargetPos = CanUseCameraInfo ? GameClient()->m_CursorInfo.Target() : GameClient()->m_Controls.m_aMousePos[g_Config.m_ClDummy];
216 int Smoothness = CanUseCameraInfo ? 50 : g_Config.m_ClDyncamSmoothness;
217 int Stabilizing = CanUseCameraInfo ? 50 : g_Config.m_ClDyncamStabilizing;
218 bool IsDyncam = CanUseCameraInfo ? true : g_Config.m_ClDyncam;
219
220 float DeltaTime = Client()->RenderFrameTime();
221
222 if(Smoothness > 0)
223 {
224 float CameraSpeed = (1.0f - (Smoothness / 100.0f)) * 9.5f + 0.5f;
225 float CameraStabilizingFactor = 1 + Stabilizing / 100.0f;
226
227 m_DyncamSmoothingSpeedBias += CameraSpeed * DeltaTime;
228 if(IsDyncam)
229 {
230 m_DyncamSmoothingSpeedBias -= length(a: TargetPos - m_LastTargetPos) * std::log10(x: CameraStabilizingFactor) * 0.02f;
231 m_DyncamSmoothingSpeedBias = std::clamp(val: m_DyncamSmoothingSpeedBias, lo: 0.5f, hi: CameraSpeed);
232 }
233 else
234 {
235 m_DyncamSmoothingSpeedBias = std::max(a: 5.0f, b: CameraSpeed); // make sure toggle back is fast
236 }
237 }
238
239 m_DyncamTargetCameraOffset = vec2(0, 0);
240 float l = length(a: TargetPos);
241 if(l > 0.0001f) // make sure that this isn't 0
242 {
243 float CurrentDeadzone = Deadzone();
244 float CurrentFollowFactor = FollowFactor();
245
246 // use provided camera setting from server
247 if(CanUseCameraInfo)
248 {
249 CurrentDeadzone = GameClient()->m_Snap.m_SpecInfo.m_Deadzone;
250 CurrentFollowFactor = GameClient()->m_Snap.m_SpecInfo.m_FollowFactor;
251
252 if(!UsingAutoSpecCamera)
253 {
254 // turn off dyncam if user zooms when spectating
255 CurrentDeadzone = 0;
256 CurrentFollowFactor = 0;
257 }
258 }
259
260 float OffsetAmount = std::max(a: l - CurrentDeadzone, b: 0.0f) * (CurrentFollowFactor / 100.0f);
261
262 if(CanUseCameraInfo)
263 {
264 OffsetAmount = std::min(a: OffsetAmount, b: 350.0f * m_Zoom);
265 }
266
267 m_DyncamTargetCameraOffset = normalize_pre_length(v: TargetPos, len: l) * OffsetAmount;
268 }
269
270 m_LastTargetPos = TargetPos;
271 vec2 CurrentCameraOffset = m_aDyncamCurrentCameraOffset[g_Config.m_ClDummy];
272 float SpeedBias = m_CameraSmoothing ? 50.0f : m_DyncamSmoothingSpeedBias;
273 if(Smoothness > 0)
274 CurrentCameraOffset += (m_DyncamTargetCameraOffset - CurrentCameraOffset) * std::min(a: DeltaTime * SpeedBias, b: 1.0f);
275 else
276 CurrentCameraOffset = m_DyncamTargetCameraOffset;
277
278 // directly put the camera in place when switching in and out of freeview or spectate mode
279 if(m_CanUseCameraInfo != CanUseCameraInfo)
280 {
281 CurrentCameraOffset = m_DyncamTargetCameraOffset;
282 }
283
284 m_aDyncamCurrentCameraOffset[g_Config.m_ClDummy] = CurrentCameraOffset;
285 m_CanUseCameraInfo = CanUseCameraInfo;
286 m_UsingAutoSpecCamera = UsingAutoSpecCamera;
287}
288
289void CCamera::OnRender()
290{
291 if(m_CameraSmoothing)
292 {
293 if(!GameClient()->m_Snap.m_SpecInfo.m_Active)
294 {
295 m_Center = m_CameraSmoothingTarget;
296 m_CameraSmoothing = false;
297 }
298 else
299 {
300 float Time = Client()->LocalTime();
301 if(Time >= m_CameraSmoothingEnd)
302 {
303 m_Center = m_CameraSmoothingTarget;
304 m_CameraSmoothing = false;
305 }
306 else
307 {
308 m_CameraSmoothingCenter = vec2(m_CameraSmoothingBezierX.Evaluate(t: CameraSmoothingProgress(CurrentTime: Time)), m_CameraSmoothingBezierY.Evaluate(t: CameraSmoothingProgress(CurrentTime: Time)));
309 if(distance(a: m_CameraSmoothingCenter, b: m_CameraSmoothingTarget) <= 0.1f)
310 {
311 m_Center = m_CameraSmoothingTarget;
312 m_CameraSmoothing = false;
313 }
314 }
315 }
316 }
317
318 // update camera center
319 if(GameClient()->m_Snap.m_SpecInfo.m_Active && !GameClient()->m_Snap.m_SpecInfo.m_UsePosition)
320 {
321 if(m_CamType != CAMTYPE_SPEC)
322 {
323 m_aLastPos[g_Config.m_ClDummy] = GameClient()->m_Controls.m_aMousePos[g_Config.m_ClDummy];
324 GameClient()->m_Controls.m_aMousePos[g_Config.m_ClDummy] = m_PrevCenter;
325 GameClient()->m_Controls.m_aMouseInputType[g_Config.m_ClDummy] = CControls::EMouseInputType::AUTOMATED;
326 GameClient()->m_Controls.ClampMousePos();
327 m_CamType = CAMTYPE_SPEC;
328 }
329 m_Center = GameClient()->m_Controls.m_aMousePos[g_Config.m_ClDummy];
330 }
331 else
332 {
333 if(m_CamType != CAMTYPE_PLAYER)
334 {
335 GameClient()->m_Controls.m_aMousePos[g_Config.m_ClDummy] = m_aLastPos[g_Config.m_ClDummy];
336 GameClient()->m_Controls.m_aMouseInputType[g_Config.m_ClDummy] = CControls::EMouseInputType::AUTOMATED;
337 GameClient()->m_Controls.ClampMousePos();
338 m_CamType = CAMTYPE_PLAYER;
339 }
340
341 if(GameClient()->m_Snap.m_SpecInfo.m_Active)
342 m_Center = GameClient()->m_Snap.m_SpecInfo.m_Position + m_aDyncamCurrentCameraOffset[g_Config.m_ClDummy];
343 else
344 m_Center = GameClient()->m_LocalCharacterPos + m_aDyncamCurrentCameraOffset[g_Config.m_ClDummy];
345 }
346
347 if(m_ForceFreeview && m_CamType == CAMTYPE_SPEC)
348 {
349 GameClient()->m_Controls.m_aMouseInputType[g_Config.m_ClDummy] = CControls::EMouseInputType::AUTOMATED;
350 m_Center = GameClient()->m_Controls.m_aMousePos[g_Config.m_ClDummy] = m_ForceFreeviewPos;
351 m_ForceFreeview = false;
352 }
353 else
354 {
355 m_ForceFreeviewPos = m_Center;
356 }
357
358 const int SpecId = GameClient()->m_Snap.m_SpecInfo.m_SpectatorId;
359
360 // start smoothing from the current position when the target changes
361 if(m_CameraSmoothing && SpecId != m_PrevSpecId)
362 m_CameraSmoothing = false;
363
364 if(GameClient()->m_Snap.m_SpecInfo.m_Active &&
365 (SpecId != m_PrevSpecId ||
366 (m_CameraSmoothing && m_CameraSmoothingTarget != m_Center)) && // the target is moving during camera smoothing
367 !(!m_WasSpectating && m_Center != m_PrevCenter) && // dont smooth when starting to spectate
368 m_CamType != CAMTYPE_SPEC &&
369 !GameClient()->m_MultiView.IsActivated())
370 {
371 float Now = Client()->LocalTime();
372 if(!m_CameraSmoothing)
373 m_CenterBeforeSmoothing = m_PrevCenter;
374
375 vec2 Derivative = {0.f, 0.f};
376 if(m_CameraSmoothing)
377 {
378 float Progress = CameraSmoothingProgress(CurrentTime: Now);
379 Derivative.x = m_CameraSmoothingBezierX.Derivative(t: Progress);
380 Derivative.y = m_CameraSmoothingBezierY.Derivative(t: Progress);
381 }
382
383 m_CameraSmoothingTarget = m_Center;
384 m_CameraSmoothingBezierX = CCubicBezier::With(Start: m_CenterBeforeSmoothing.x, StartDerivative: Derivative.x, EndDerivative: 0, End: m_CameraSmoothingTarget.x);
385 m_CameraSmoothingBezierY = CCubicBezier::With(Start: m_CenterBeforeSmoothing.y, StartDerivative: Derivative.y, EndDerivative: 0, End: m_CameraSmoothingTarget.y);
386
387 if(!m_CameraSmoothing)
388 {
389 m_CameraSmoothingStart = Now;
390 m_CameraSmoothingEnd = Now + (float)g_Config.m_ClSmoothSpectatingTime / 1000.0f;
391 }
392
393 if(!m_CameraSmoothing)
394 m_CameraSmoothingCenter = m_PrevCenter;
395
396 m_CameraSmoothing = true;
397 }
398
399 if(m_CameraSmoothing)
400 m_Center = m_CameraSmoothingCenter;
401
402 m_PrevCenter = m_Center;
403 m_PrevSpecId = SpecId;
404
405 // demo always count as spectating
406 m_WasSpectating = GameClient()->m_Snap.m_SpecInfo.m_Active;
407}
408
409void CCamera::OnConsoleInit()
410{
411 Console()->Register(pName: "zoom+", pParams: "?f[amount]", Flags: CFGFLAG_CLIENT, pfnFunc: ConZoomPlus, pUser: this, pHelp: "Zoom increase");
412 Console()->Register(pName: "zoom-", pParams: "?f[amount]", Flags: CFGFLAG_CLIENT, pfnFunc: ConZoomMinus, pUser: this, pHelp: "Zoom decrease");
413 Console()->Register(pName: "zoom", pParams: "?f", Flags: CFGFLAG_CLIENT, pfnFunc: ConZoom, pUser: this, pHelp: "Change zoom");
414 Console()->Register(pName: "set_view", pParams: "i[x]i[y]", Flags: CFGFLAG_CLIENT, pfnFunc: ConSetView, pUser: this, pHelp: "Set camera position to x and y in the map");
415 Console()->Register(pName: "set_view_relative", pParams: "i[x]i[y]", Flags: CFGFLAG_CLIENT, pfnFunc: ConSetViewRelative, pUser: this, pHelp: "Set camera position relative to current view in the map");
416 Console()->Register(pName: "goto_switch", pParams: "i[number]?i[offset]", Flags: CFGFLAG_CLIENT, pfnFunc: ConGotoSwitch, pUser: this, pHelp: "View switch found (at offset) with given number");
417 Console()->Register(pName: "goto_tele", pParams: "i[number]?i[offset]", Flags: CFGFLAG_CLIENT, pfnFunc: ConGotoTele, pUser: this, pHelp: "View tele found (at offset) with given number");
418}
419
420void CCamera::OnReset()
421{
422 m_CameraSmoothing = false;
423
424 m_Zoom = CCamera::ZoomStepsToValue(Steps: g_Config.m_ClDefaultZoom - 10);
425 m_Zooming = false;
426 m_AutoSpecCameraZooming = false;
427 m_UserZoomTarget = CCamera::ZoomStepsToValue(Steps: g_Config.m_ClDefaultZoom - 10);
428}
429
430void CCamera::ConZoomPlus(IConsole::IResult *pResult, void *pUserData)
431{
432 CCamera *pSelf = (CCamera *)pUserData;
433 if(!pSelf->ZoomAllowed())
434 return;
435
436 float ZoomAmount = pResult->NumArguments() ? pResult->GetFloat(Index: 0) : 1.0f;
437
438 pSelf->ScaleZoom(Factor: CCamera::ZoomStepsToValue(Steps: ZoomAmount));
439
440 if(pSelf->GameClient()->m_MultiView.IsActivated())
441 pSelf->GameClient()->m_MultiView.AddPersonalZoom(Amount: ZoomAmount);
442}
443void CCamera::ConZoomMinus(IConsole::IResult *pResult, void *pUserData)
444{
445 CCamera *pSelf = (CCamera *)pUserData;
446 if(!pSelf->ZoomAllowed())
447 return;
448
449 float ZoomAmount = pResult->NumArguments() ? pResult->GetFloat(Index: 0) : 1.0f;
450 ZoomAmount *= -1.0f;
451
452 pSelf->ScaleZoom(Factor: CCamera::ZoomStepsToValue(Steps: ZoomAmount));
453
454 if(pSelf->GameClient()->m_MultiView.IsActivated())
455 pSelf->GameClient()->m_MultiView.AddPersonalZoom(Amount: ZoomAmount);
456}
457void CCamera::ConZoom(IConsole::IResult *pResult, void *pUserData)
458{
459 CCamera *pSelf = (CCamera *)pUserData;
460 if(!pSelf->ZoomAllowed())
461 return;
462
463 bool IsReset = !pResult->NumArguments();
464
465 float TargetLevel = !IsReset ? pResult->GetFloat(Index: 0) : g_Config.m_ClDefaultZoom;
466
467 if(!pSelf->CanUseAutoSpecCamera() || !pSelf->m_CanUseCameraInfo)
468 pSelf->ChangeZoom(Target: CCamera::ZoomStepsToValue(Steps: TargetLevel - 10.0f), Smoothness: pSelf->GameClient()->m_Snap.m_SpecInfo.m_Active && pSelf->GameClient()->m_MultiView.IsActivated() ? g_Config.m_ClMultiViewZoomSmoothness : g_Config.m_ClSmoothZoomTime, IsUser: true);
469 else
470 pSelf->m_UserZoomTarget = CCamera::ZoomStepsToValue(Steps: TargetLevel - 10.0f);
471
472 pSelf->m_AutoSpecCamera = IsReset;
473
474 if(pSelf->GameClient()->m_MultiView.IsActivated() && pSelf->GameClient()->m_Snap.m_SpecInfo.m_Active)
475 pSelf->GameClient()->m_MultiView.SetPersonalZoom(TargetLevel - 10.0f);
476}
477void CCamera::ConSetView(IConsole::IResult *pResult, void *pUserData)
478{
479 CCamera *pSelf = (CCamera *)pUserData;
480 // wait until free view camera type to update the position
481 pSelf->SetView(Pos: ivec2(pResult->GetInteger(Index: 0), pResult->GetInteger(Index: 1)));
482}
483void CCamera::ConSetViewRelative(IConsole::IResult *pResult, void *pUserData)
484{
485 CCamera *pSelf = (CCamera *)pUserData;
486 // wait until free view camera type to update the position
487 pSelf->SetView(Pos: ivec2(pResult->GetInteger(Index: 0), pResult->GetInteger(Index: 1)), Relative: true);
488}
489void CCamera::ConGotoSwitch(IConsole::IResult *pResult, void *pUserData)
490{
491 CCamera *pSelf = (CCamera *)pUserData;
492 pSelf->GotoSwitch(Number: pResult->GetInteger(Index: 0), Offset: pResult->NumArguments() > 1 ? pResult->GetInteger(Index: 1) : -1);
493}
494void CCamera::ConGotoTele(IConsole::IResult *pResult, void *pUserData)
495{
496 CCamera *pSelf = (CCamera *)pUserData;
497 pSelf->GotoTele(Number: pResult->GetInteger(Index: 0), Offset: pResult->NumArguments() > 1 ? pResult->GetInteger(Index: 1) : -1);
498}
499
500void CCamera::SetView(ivec2 Pos, bool Relative)
501{
502 vec2 RealPos = vec2(Pos.x * 32.0, Pos.y * 32.0);
503 vec2 UntestedViewPos = Relative ? m_ForceFreeviewPos + RealPos : RealPos;
504
505 m_ForceFreeview = true;
506
507 m_ForceFreeviewPos = vec2(
508 std::clamp(val: UntestedViewPos.x, lo: 200.0f, hi: Collision()->GetWidth() * 32 - 200.0f),
509 std::clamp(val: UntestedViewPos.y, lo: 200.0f, hi: Collision()->GetHeight() * 32 - 200.0f));
510}
511
512void CCamera::GotoSwitch(int Number, int Offset)
513{
514 if(Collision()->SwitchLayer() == nullptr)
515 return;
516
517 int Match = -1;
518 ivec2 MatchPos = ivec2(-1, -1);
519
520 auto FindTile = [this, &Match, &MatchPos, &Number, &Offset]() {
521 for(int x = 0; x < Collision()->GetWidth(); x++)
522 {
523 for(int y = 0; y < Collision()->GetHeight(); y++)
524 {
525 int i = y * Collision()->GetWidth() + x;
526 if(Number == Collision()->GetSwitchNumber(Index: i))
527 {
528 Match++;
529 if(Offset != -1)
530 {
531 if(Match == Offset)
532 {
533 MatchPos = ivec2(x, y);
534 m_GotoSwitchOffset = Match;
535 return;
536 }
537 continue;
538 }
539 MatchPos = ivec2(x, y);
540 if(Match == m_GotoSwitchOffset)
541 return;
542 }
543 }
544 }
545 };
546 FindTile();
547
548 if(MatchPos == ivec2(-1, -1))
549 return;
550 if(Match < m_GotoSwitchOffset)
551 m_GotoSwitchOffset = -1;
552 SetView(Pos: MatchPos);
553 m_GotoSwitchOffset++;
554}
555
556void CCamera::GotoTele(int Number, int Offset)
557{
558 if(Collision()->TeleLayer() == nullptr)
559 return;
560 Number--;
561
562 if(m_GotoTeleLastNumber != Number)
563 m_GotoTeleLastPos = ivec2(-1, -1);
564
565 ivec2 MatchPos = ivec2(-1, -1);
566 const size_t NumTeles = Collision()->TeleAllSize(Number);
567 if(!NumTeles)
568 {
569 log_error("camera", "No teleporter with number %d found.", Number + 1);
570 return;
571 }
572
573 if(Offset != -1 || m_GotoTeleLastPos == ivec2(-1, -1))
574 {
575 if((size_t)Offset >= NumTeles || Offset < 0)
576 Offset = 0;
577 vec2 Tele = Collision()->TeleAllGet(Number, Offset);
578 MatchPos = ivec2(Tele.x / 32, Tele.y / 32);
579 m_GotoTeleOffset = Offset;
580 }
581 else
582 {
583 bool FullRound = false;
584 do
585 {
586 vec2 Tele = Collision()->TeleAllGet(Number, Offset: m_GotoTeleOffset);
587 MatchPos = ivec2(Tele.x / 32, Tele.y / 32);
588 m_GotoTeleOffset++;
589 if((size_t)m_GotoTeleOffset >= NumTeles)
590 {
591 m_GotoTeleOffset = 0;
592 if(FullRound)
593 {
594 MatchPos = m_GotoTeleLastPos;
595 break;
596 }
597 FullRound = true;
598 }
599 } while(distance(a: m_GotoTeleLastPos, b: MatchPos) < 10.0f);
600 }
601
602 if(MatchPos == ivec2(-1, -1))
603 return;
604 m_GotoTeleLastPos = MatchPos;
605 m_GotoTeleLastNumber = Number;
606 SetView(Pos: MatchPos);
607}
608
609void CCamera::SetZoom(float Target, int Smoothness, bool IsUser)
610{
611 ChangeZoom(Target, Smoothness, IsUser);
612}
613
614bool CCamera::ZoomAllowed() const
615{
616 return GameClient()->m_Snap.m_SpecInfo.m_Active ||
617 GameClient()->m_GameInfo.m_AllowZoom ||
618 Client()->State() == IClient::STATE_DEMOPLAYBACK;
619}
620
621int CCamera::Deadzone() const
622{
623 return g_Config.m_ClDyncam ? g_Config.m_ClDyncamDeadzone : g_Config.m_ClMouseDeadzone;
624}
625
626int CCamera::FollowFactor() const
627{
628 return g_Config.m_ClDyncam ? g_Config.m_ClDyncamFollowFactor : g_Config.m_ClMouseFollowfactor;
629}
630
631bool CCamera::CanUseAutoSpecCamera() const
632{
633 if(Client()->State() == IClient::STATE_DEMOPLAYBACK)
634 {
635 // only follow mode has the correct camera info
636 return GameClient()->m_Snap.m_SpecInfo.m_HasCameraInfo && GameClient()->m_DemoSpecId == SPEC_FOLLOW;
637 }
638
639 return g_Config.m_ClSpecAutoSync && GameClient()->m_Snap.m_SpecInfo.m_HasCameraInfo &&
640 GameClient()->m_Snap.m_SpecInfo.m_SpectatorId != GameClient()->m_aLocalIds[0] &&
641 (!GameClient()->Client()->DummyConnected() || GameClient()->m_Snap.m_SpecInfo.m_SpectatorId != GameClient()->m_aLocalIds[1]);
642}
643
644void CCamera::ToggleAutoSpecCamera()
645{
646 if(!g_Config.m_ClSpecAutoSync)
647 {
648 g_Config.m_ClSpecAutoSync = 1;
649 GameClient()->m_Camera.m_AutoSpecCamera = true;
650 }
651 else if(GameClient()->m_Camera.m_AutoSpecCamera && GameClient()->m_Camera.SpectatingPlayer() && GameClient()->m_Camera.CanUseAutoSpecCamera())
652 {
653 GameClient()->m_Camera.m_AutoSpecCamera = false;
654 }
655 else
656 {
657 g_Config.m_ClSpecAutoSync = 0;
658 }
659}
660
661void CCamera::UpdateAutoSpecCameraTooltip()
662{
663 const char *pFeatureText = Localize(pStr: "Auto-sync player camera");
664
665 if(!g_Config.m_ClSpecAutoSync)
666 str_format(buffer: m_aAutoSpecCameraTooltip, buffer_size: sizeof(m_aAutoSpecCameraTooltip), format: "%s: %s", pFeatureText, Localize(pStr: "Disabled", pContext: "Auto camera"));
667 else if(!SpectatingPlayer())
668 str_format(buffer: m_aAutoSpecCameraTooltip, buffer_size: sizeof(m_aAutoSpecCameraTooltip), format: "%s: %s", pFeatureText, Localize(pStr: "Enabled", pContext: "Auto camera"));
669 else if(!CanUseAutoSpecCamera())
670 str_format(buffer: m_aAutoSpecCameraTooltip, buffer_size: sizeof(m_aAutoSpecCameraTooltip), format: "%s: %s", pFeatureText, Localize(pStr: "Unavailable for this player", pContext: "Auto camera"));
671 else if(!m_AutoSpecCamera)
672 str_format(buffer: m_aAutoSpecCameraTooltip, buffer_size: sizeof(m_aAutoSpecCameraTooltip), format: "%s: %s", pFeatureText, Localize(pStr: "Inactive", pContext: "Auto camera"));
673 else
674 str_format(buffer: m_aAutoSpecCameraTooltip, buffer_size: sizeof(m_aAutoSpecCameraTooltip), format: "%s: %s", pFeatureText, Localize(pStr: "Active", pContext: "Auto camera"));
675}
676