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 "player.h"
4
5#include "entities/character.h"
6#include "gamecontext.h"
7#include "gamecontroller.h"
8#include "score.h"
9
10#include <base/dbg.h>
11#include <base/mem.h>
12#include <base/str.h>
13#include <base/time.h>
14
15#include <engine/antibot.h>
16#include <engine/server.h>
17#include <engine/shared/config.h>
18#include <engine/shared/protocol.h>
19
20#include <game/gamecore.h>
21#include <game/teamscore.h>
22
23#include <algorithm>
24
25MACRO_ALLOC_POOL_ID_IMPL(CPlayer, MAX_CLIENTS)
26
27IServer *CPlayer::Server() const { return m_pGameServer->Server(); }
28
29CPlayer::CPlayer(CGameContext *pGameServer, uint32_t UniqueClientId, int ClientId, int Team) :
30 m_UniqueClientId(UniqueClientId)
31{
32 m_pGameServer = pGameServer;
33 m_ClientId = ClientId;
34 dbg_assert(GameServer()->m_pController->IsValidTeam(Team), "Invalid Team: %d", Team);
35 m_Team = Team;
36 m_NumInputs = 0;
37 Reset();
38 GameServer()->Antibot()->OnPlayerInit(ClientId: m_ClientId);
39}
40
41CPlayer::~CPlayer()
42{
43 GameServer()->Antibot()->OnPlayerDestroy(ClientId: m_ClientId);
44 delete m_pLastTarget;
45 delete m_pCharacter;
46 m_pCharacter = nullptr;
47}
48
49void CPlayer::Reset()
50{
51 m_DieTick = Server()->Tick();
52 m_PreviousDieTick = m_DieTick;
53 m_JoinTick = Server()->Tick();
54 m_DDNetVersionKickTick = Server()->Tick() + 3 * Server()->TickSpeed();
55 delete m_pCharacter;
56 m_pCharacter = nullptr;
57 SetSpectatorId(SPEC_FREEVIEW);
58 m_LastActionTick = Server()->Tick();
59 m_TeamChangeTick = Server()->Tick();
60 m_LastSetTeam = 0;
61 m_LastInvited = 0;
62 m_WeakHookSpawn = false;
63
64 // DDRace
65
66 m_LastCommandPos = 0;
67 m_LastPlaytime = 0;
68 m_ChatScore = 0;
69 m_Moderating = false;
70 m_EyeEmoteEnabled = true;
71 if(Server()->IsSixup(ClientId: m_ClientId))
72 m_TimerType = TIMERTYPE_SIXUP;
73 else
74 m_TimerType = (g_Config.m_SvDefaultTimerType == TIMERTYPE_GAMETIMER || g_Config.m_SvDefaultTimerType == TIMERTYPE_GAMETIMER_AND_BROADCAST) ? TIMERTYPE_BROADCAST : g_Config.m_SvDefaultTimerType;
75
76 m_DefEmote = EMOTE_NORMAL;
77 m_Afk = true;
78 m_LastWhisperTo = -1;
79 m_LastSetSpectatorMode = 0;
80 m_aTimeoutCode[0] = '\0';
81 delete m_pLastTarget;
82 m_pLastTarget = new CNetObj_PlayerInput({.m_Direction: 0});
83 m_LastTargetInit = false;
84 m_TuneZone = 0;
85 m_TuneZoneOld = m_TuneZone;
86 m_Halloween = false;
87 m_FirstPacket = true;
88
89 m_SendVoteIndex = -1;
90 m_pLastSentVoteOption = nullptr;
91
92 if(g_Config.m_Events)
93 {
94 const ETimeSeason Season = time_season();
95 if(Season == ETimeSeason::NEWYEAR)
96 {
97 m_DefEmote = EMOTE_HAPPY;
98 }
99 else if(Season == ETimeSeason::HALLOWEEN)
100 {
101 m_DefEmote = EMOTE_ANGRY;
102 m_Halloween = true;
103 }
104 else
105 {
106 m_DefEmote = EMOTE_NORMAL;
107 }
108 }
109 m_OverrideEmoteReset = -1;
110
111 GameServer()->Score()->PlayerData(Id: m_ClientId)->Reset();
112
113 m_LastKickVote = 0;
114 m_LastDDRaceTeamChange.reset();
115 m_ShowOthers = g_Config.m_SvShowOthersDefault;
116 m_ShowAll = g_Config.m_SvShowAllDefault;
117 m_EnableSpectatorCount = true;
118 m_ShowDistance = vec2(1200, 800);
119 m_SpecTeam = false;
120 m_NinjaJetpack = false;
121
122 m_Paused = PAUSE_NONE;
123 m_DND = false;
124 m_Whispers = true;
125
126 m_LastPause = 0;
127
128 // Variable initialized:
129 m_LastSqlQuery = 0;
130 m_ScoreQueryResult = nullptr;
131 m_ScoreFinishResult = nullptr;
132
133 int64_t Now = Server()->Tick();
134 int64_t TickSpeed = Server()->TickSpeed();
135 // If the player joins within ten seconds of the server becoming
136 // non-empty, allow them to vote immediately. This allows players to
137 // vote after map changes or when they join an empty server.
138 //
139 // Otherwise, block voting in the beginning after joining.
140 if(Now > GameServer()->m_NonEmptySince + 10 * TickSpeed)
141 m_FirstVoteTick = Now + g_Config.m_SvJoinVoteDelay * TickSpeed;
142 else
143 m_FirstVoteTick = Now;
144
145 m_NotEligibleForFinish = false;
146 m_EligibleForFinishCheck = 0;
147 m_VotedForPractice = false;
148 m_SwapTargetsClientId = -1;
149 m_BirthdayAnnounced = false;
150 m_RescueMode = RESCUEMODE_AUTO;
151
152 m_CameraInfo.Reset();
153 UpdateNetworkClipRadius();
154 std::fill(first: std::begin(arr&: m_aStrongWeakId), last: std::end(arr&: m_aStrongWeakId), value: 0);
155 InvalidateClientInfo();
156}
157
158void CPlayer::SetTeeInfos(const CTeeInfo &TeeInfos)
159{
160 m_TeeInfos = TeeInfos;
161 InvalidateClientInfo();
162}
163
164void CPlayer::SetTeeInfos(const char *pSkinName, bool UseCustomColor, int ColorBody, int ColorFeet)
165{
166 str_copy(dst&: m_TeeInfos.m_aSkinName, src: pSkinName);
167 m_TeeInfos.m_UseCustomColor = UseCustomColor;
168 m_TeeInfos.m_ColorBody = ColorBody;
169 m_TeeInfos.m_ColorFeet = ColorFeet;
170 if(!Server()->IsSixup(ClientId: m_ClientId))
171 m_TeeInfos.ToSixup();
172 InvalidateClientInfo();
173}
174
175static int PlayerFlags_SixToSeven(int Flags)
176{
177 int Seven = 0;
178 if(Flags & PLAYERFLAG_CHATTING)
179 Seven |= protocol7::PLAYERFLAG_CHATTING;
180 if(Flags & PLAYERFLAG_SCOREBOARD)
181 Seven |= protocol7::PLAYERFLAG_SCOREBOARD;
182
183 return Seven;
184}
185
186void CPlayer::Tick()
187{
188 if(m_ScoreQueryResult != nullptr && m_ScoreQueryResult->m_Completed && m_SentSnaps >= 3)
189 {
190 ProcessScoreResult(Result&: *m_ScoreQueryResult);
191 m_ScoreQueryResult = nullptr;
192 }
193 if(m_ScoreFinishResult != nullptr && m_ScoreFinishResult->m_Completed)
194 {
195 ProcessScoreResult(Result&: *m_ScoreFinishResult);
196 m_ScoreFinishResult = nullptr;
197 }
198
199 if(!Server()->ClientIngame(ClientId: m_ClientId))
200 return;
201
202 if(m_ChatScore > 0)
203 m_ChatScore--;
204
205 if(m_Moderating && m_Afk)
206 {
207 m_Moderating = false;
208 GameServer()->SendChatTarget(To: m_ClientId, pText: "Active moderator mode disabled because you are afk.");
209
210 if(!GameServer()->PlayerModerating())
211 GameServer()->SendChat(ClientId: -1, Team: TEAM_ALL, pText: "Server kick/spec votes are no longer actively moderated.");
212 }
213
214 // do latency stuff
215 {
216 IServer::CClientInfo Info;
217 if(Server()->GetClientInfo(ClientId: m_ClientId, pInfo: &Info))
218 {
219 m_Latency.m_Accum += Info.m_Latency;
220 m_Latency.m_AccumMax = std::max(a: m_Latency.m_AccumMax, b: Info.m_Latency);
221 m_Latency.m_AccumMin = std::min(a: m_Latency.m_AccumMin, b: Info.m_Latency);
222 }
223 // each second
224 if(Server()->Tick() % Server()->TickSpeed() == 0)
225 {
226 m_Latency.m_Avg = m_Latency.m_Accum / Server()->TickSpeed();
227 m_Latency.m_Max = m_Latency.m_AccumMax;
228 m_Latency.m_Min = m_Latency.m_AccumMin;
229 m_Latency.m_Accum = 0;
230 m_Latency.m_AccumMin = 1000;
231 m_Latency.m_AccumMax = 0;
232 }
233 }
234
235 if(Server()->GetNetErrorString(ClientId: m_ClientId)[0])
236 {
237 SetInitialAfk(true);
238
239 char aBuf[512];
240 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "'%s' would have timed out, but can use timeout protection now", Server()->ClientName(ClientId: m_ClientId));
241 GameServer()->SendChat(ClientId: -1, Team: TEAM_ALL, pText: aBuf);
242 Server()->ResetNetErrorString(ClientId: m_ClientId);
243 }
244
245 if(!GameServer()->m_pController->IsGamePaused())
246 {
247 int EarliestRespawnTick = m_PreviousDieTick + Server()->TickSpeed() * 3;
248 int RespawnTick = std::max(a: m_DieTick, b: EarliestRespawnTick) + 2;
249 if(!m_pCharacter && RespawnTick <= Server()->Tick())
250 m_Spawning = true;
251
252 if(m_pCharacter)
253 {
254 if(m_pCharacter->IsAlive())
255 {
256 ProcessPause();
257 if(!m_Paused)
258 m_ViewPos = m_pCharacter->m_Pos;
259 }
260 else if(!m_pCharacter->IsPaused())
261 {
262 delete m_pCharacter;
263 m_pCharacter = nullptr;
264 }
265 }
266 else if(m_Spawning && !m_WeakHookSpawn)
267 TryRespawn();
268 }
269 else
270 {
271 ++m_DieTick;
272 ++m_PreviousDieTick;
273 ++m_JoinTick;
274 ++m_LastActionTick;
275 ++m_TeamChangeTick;
276 }
277
278 m_TuneZoneOld = m_TuneZone; // determine needed tunings with viewpos
279 int CurrentIndex = GameServer()->Collision()->GetMapIndex(Pos: m_ViewPos);
280 m_TuneZone = GameServer()->Collision()->IsTune(Index: CurrentIndex);
281
282 if(m_TuneZone != m_TuneZoneOld) // don't send tunings all the time
283 {
284 GameServer()->SendTuningParams(ClientId: m_ClientId, Zone: m_TuneZone);
285 }
286
287 if(m_OverrideEmoteReset >= 0 && m_OverrideEmoteReset <= Server()->Tick())
288 {
289 m_OverrideEmoteReset = -1;
290 }
291
292 if(m_Halloween && m_pCharacter && !m_pCharacter->IsPaused())
293 {
294 if(1200 - ((Server()->Tick() - m_pCharacter->GetLastAction()) % (1200)) < 5)
295 {
296 GameServer()->SendEmoticon(ClientId: GetCid(), Emoticon: EMOTICON_GHOST, TargetClientId: -1);
297 }
298 }
299}
300
301void CPlayer::PostTick()
302{
303 // update latency value
304 if(m_PlayerFlags & PLAYERFLAG_IN_MENU)
305 m_aCurLatency[m_ClientId] = GameServer()->m_apPlayers[m_ClientId]->m_Latency.m_Min;
306
307 if(m_PlayerFlags & PLAYERFLAG_SCOREBOARD)
308 {
309 for(int i = 0; i < MAX_CLIENTS; ++i)
310 {
311 if(GameServer()->m_apPlayers[i] && GameServer()->m_apPlayers[i]->GetTeam() != TEAM_SPECTATORS)
312 m_aCurLatency[i] = GameServer()->m_apPlayers[i]->m_Latency.m_Min;
313 }
314 }
315
316 // update view pos for spectators
317 if((m_Team == TEAM_SPECTATORS || m_Paused) && m_SpectatorId != SPEC_FREEVIEW && GameServer()->m_apPlayers[m_SpectatorId] && GameServer()->m_apPlayers[m_SpectatorId]->GetCharacter())
318 m_ViewPos = GameServer()->m_apPlayers[m_SpectatorId]->GetCharacter()->m_Pos;
319
320 UpdateNetworkClipRadius();
321}
322
323void CPlayer::PostPostTick()
324{
325 if(!Server()->ClientIngame(ClientId: m_ClientId))
326 return;
327
328 if(!GameServer()->m_pController->IsGamePaused() && !m_pCharacter && m_Spawning && m_WeakHookSpawn)
329 TryRespawn();
330}
331
332void CPlayer::Snap(int SnappingClient)
333{
334 if(!Server()->ClientIngame(ClientId: m_ClientId))
335 return;
336
337 int TranslatedId = m_ClientId;
338 if(!Server()->Translate(Target&: TranslatedId, ClientId: SnappingClient))
339 return;
340
341 if(!m_ClientInfoValid)
342 {
343 m_ClientInfoValid = true;
344 StrToInts(pInts: m_ClientInfo.m_aName, NumInts: std::size(m_ClientInfo.m_aName), pStr: Server()->ClientName(ClientId: m_ClientId));
345 StrToInts(pInts: m_ClientInfo.m_aClan, NumInts: std::size(m_ClientInfo.m_aClan), pStr: Server()->ClientClan(ClientId: m_ClientId));
346 m_ClientInfo.m_Country = Server()->ClientCountry(ClientId: m_ClientId);
347 StrToInts(pInts: m_ClientInfo.m_aSkin, NumInts: std::size(m_ClientInfo.m_aSkin), pStr: m_TeeInfos.m_aSkinName);
348 m_ClientInfo.m_UseCustomColor = m_TeeInfos.m_UseCustomColor;
349 m_ClientInfo.m_ColorBody = m_TeeInfos.m_ColorBody;
350 m_ClientInfo.m_ColorFeet = m_TeeInfos.m_ColorFeet;
351 }
352 Server()->SnapNewItem(Id: TranslatedId, Data: m_ClientInfo);
353
354 int SnappingClientVersion = GameServer()->GetClientVersion(ClientId: SnappingClient);
355 int Latency = SnappingClient == SERVER_DEMO_CLIENT ? m_Latency.m_Min : GameServer()->m_apPlayers[SnappingClient]->m_aCurLatency[m_ClientId];
356 int Score = GameServer()->m_pController->SnapPlayerScore(SnappingClient, pPlayer: this);
357
358 if(!Server()->IsSixup(ClientId: SnappingClient))
359 {
360 CNetObj_PlayerInfo PlayerInfo = {};
361 PlayerInfo.m_Latency = Latency;
362 PlayerInfo.m_Score = Score;
363 PlayerInfo.m_Local = (int)(m_ClientId == SnappingClient && (m_Paused != PAUSE_PAUSED || SnappingClientVersion >= VERSION_DDNET_OLD));
364 PlayerInfo.m_ClientId = TranslatedId;
365 PlayerInfo.m_Team = m_Team;
366 if(SnappingClientVersion < VERSION_DDNET_INDEPENDENT_SPECTATORS_TEAM)
367 {
368 // In older versions the SPECTATORS TEAM was also used if the own player is in PAUSE_PAUSED or if any player is in PAUSE_SPEC.
369 PlayerInfo.m_Team = (m_Paused != PAUSE_PAUSED || m_ClientId != SnappingClient) && m_Paused < PAUSE_SPEC ? m_Team : TEAM_SPECTATORS;
370 }
371 Server()->SnapNewItem(Id: TranslatedId, Data: PlayerInfo);
372 }
373 else
374 {
375 protocol7::CNetObj_PlayerInfo PlayerInfo = {};
376 PlayerInfo.m_PlayerFlags = PlayerFlags_SixToSeven(Flags: m_PlayerFlags);
377 if(SnappingClientVersion >= VERSION_DDRACE && (m_PlayerFlags & PLAYERFLAG_AIM))
378 PlayerInfo.m_PlayerFlags |= protocol7::PLAYERFLAG_AIM;
379 if(Server()->IsRconAuthed(ClientId: m_ClientId) && ((SnappingClient >= 0 && Server()->IsRconAuthed(ClientId: SnappingClient)) || !Server()->HasAuthHidden(ClientId: m_ClientId)))
380 PlayerInfo.m_PlayerFlags |= protocol7::PLAYERFLAG_ADMIN;
381 PlayerInfo.m_Score = Score;
382 PlayerInfo.m_Latency = Latency;
383 Server()->SnapNewItem(Id: TranslatedId, Data: PlayerInfo);
384 }
385
386 if(m_ClientId == SnappingClient && (m_Team == TEAM_SPECTATORS || m_Paused))
387 {
388 int SpectatorId = m_SpectatorId;
389 if(SpectatorId >= 0 && !Server()->Translate(Target&: SpectatorId, ClientId: m_ClientId))
390 {
391 SpectatorId = TranslatedId;
392 }
393
394 if(!Server()->IsSixup(ClientId: SnappingClient))
395 {
396 CNetObj_SpectatorInfo SpectatorInfo = {};
397 SpectatorInfo.m_SpectatorId = SpectatorId;
398 SpectatorInfo.m_X = m_ViewPos.x;
399 SpectatorInfo.m_Y = m_ViewPos.y;
400 Server()->SnapNewItem(Id: m_ClientId, Data: SpectatorInfo);
401 }
402 else
403 {
404 protocol7::CNetObj_SpectatorInfo SpectatorInfo = {};
405 SpectatorInfo.m_SpecMode = m_SpectatorId == SPEC_FREEVIEW ? protocol7::SPEC_FREEVIEW : protocol7::SPEC_PLAYER;
406 SpectatorInfo.m_SpectatorId = SpectatorId;
407 SpectatorInfo.m_X = m_ViewPos.x;
408 SpectatorInfo.m_Y = m_ViewPos.y;
409 Server()->SnapNewItem(Id: m_ClientId, Data: SpectatorInfo);
410 }
411 }
412
413 if(m_ClientId == SnappingClient)
414 {
415 // send extended spectator info even when playing, this allows demo to record camera settings for local player
416 const int SpectatingClient = ((m_Team != TEAM_SPECTATORS && !m_Paused) || m_SpectatorId < 0 || m_SpectatorId >= MAX_CLIENTS) ? m_ClientId : m_SpectatorId;
417 const CPlayer *pSpecPlayer = GameServer()->m_apPlayers[SpectatingClient];
418
419 if(pSpecPlayer)
420 {
421 CNetObj_DDNetSpectatorInfo DDNetSpectatorInfo = {};
422 DDNetSpectatorInfo.m_HasCameraInfo = pSpecPlayer->m_CameraInfo.m_HasCameraInfo;
423 DDNetSpectatorInfo.m_Zoom = pSpecPlayer->m_CameraInfo.m_Zoom * 1000.0f;
424 DDNetSpectatorInfo.m_Deadzone = pSpecPlayer->m_CameraInfo.m_Deadzone;
425 DDNetSpectatorInfo.m_FollowFactor = pSpecPlayer->m_CameraInfo.m_FollowFactor;
426
427 if(pSpecPlayer->m_EnableSpectatorCount && SpectatingClient == m_ClientId && SnappingClient != SERVER_DEMO_CLIENT && m_Team != TEAM_SPECTATORS && !m_Paused)
428 {
429 CNetObj_SpectatorCount SpectatorCountObj = {};
430 int SpectatorCount = 0;
431 for(auto &pPlayer : GameServer()->m_apPlayers)
432 {
433 if(!pPlayer || !pPlayer->m_EnableSpectatorCount || pPlayer->m_ClientId == m_ClientId || pPlayer->m_Afk ||
434 (Server()->IsRconAuthed(ClientId: pPlayer->m_ClientId) && Server()->HasAuthHidden(ClientId: pPlayer->m_ClientId)) ||
435 !(pPlayer->m_Paused || pPlayer->m_Team == TEAM_SPECTATORS))
436 {
437 continue;
438 }
439
440 if(pPlayer->m_SpectatorId == m_ClientId)
441 {
442 SpectatorCount++;
443 }
444 else if(GameServer()->m_apPlayers[m_ClientId]->GetCharacter())
445 {
446 vec2 CheckPos = GameServer()->m_apPlayers[m_ClientId]->GetCharacter()->GetPos();
447 float dx = pPlayer->m_ViewPos.x - CheckPos.x;
448 float dy = pPlayer->m_ViewPos.y - CheckPos.y;
449 if(absolute(a: dx) < (pPlayer->m_ShowDistance.x / 2.5f) && absolute(a: dy) < (pPlayer->m_ShowDistance.y / 2.3f))
450 SpectatorCount++;
451 }
452 }
453 DDNetSpectatorInfo.m_SpectatorCount = SpectatorCount;
454 SpectatorCountObj.m_NumSpectators = SpectatorCount;
455 Server()->SnapNewItem(Id: 0, Data: SpectatorCountObj);
456 }
457 Server()->SnapNewItem(Id: m_ClientId, Data: DDNetSpectatorInfo);
458 }
459 }
460
461 CNetObj_DDNetPlayer DDNetPlayer = {};
462 if((SnappingClient >= 0 && Server()->IsRconAuthed(ClientId: SnappingClient)) || !Server()->HasAuthHidden(ClientId: m_ClientId))
463 DDNetPlayer.m_AuthLevel = Server()->GetAuthedState(ClientId: m_ClientId);
464 else
465 DDNetPlayer.m_AuthLevel = AUTHED_NO;
466
467 DDNetPlayer.m_Flags = 0;
468 if(m_Afk)
469 DDNetPlayer.m_Flags |= EXPLAYERFLAG_AFK;
470 if(m_Paused == PAUSE_SPEC)
471 DDNetPlayer.m_Flags |= EXPLAYERFLAG_SPEC;
472 if(m_Paused == PAUSE_PAUSED)
473 DDNetPlayer.m_Flags |= EXPLAYERFLAG_PAUSED;
474
475 IGameController::CFinishTime PlayerTime = GameServer()->m_pController->SnapPlayerTime(SnappingClient, pPlayer: this);
476 DDNetPlayer.m_FinishTimeSeconds = PlayerTime.m_Seconds;
477 DDNetPlayer.m_FinishTimeMillis = PlayerTime.m_Milliseconds;
478 Server()->SnapNewItem(Id: TranslatedId, Data: DDNetPlayer);
479
480 if(Server()->IsSixup(ClientId: SnappingClient) && m_pCharacter && m_pCharacter->m_DDRaceState == ERaceState::STARTED &&
481 GameServer()->m_apPlayers[SnappingClient]->m_TimerType == TIMERTYPE_SIXUP)
482 {
483 protocol7::CNetObj_PlayerInfoRace RaceInfo = {};
484 RaceInfo.m_RaceStartTick = m_pCharacter->m_StartTime;
485 Server()->SnapNewItem(Id: TranslatedId, Data: RaceInfo);
486 }
487
488 bool ShowSpec = m_pCharacter && m_pCharacter->IsPaused() && m_pCharacter->CanSnapCharacter(SnappingClient);
489
490 if(SnappingClient != SERVER_DEMO_CLIENT)
491 {
492 CPlayer *pSnapPlayer = GameServer()->m_apPlayers[SnappingClient];
493 ShowSpec = ShowSpec && (GameServer()->GetDDRaceTeam(ClientId: m_ClientId) == GameServer()->GetDDRaceTeam(ClientId: SnappingClient) || pSnapPlayer->m_ShowOthers == SHOW_OTHERS_ON || (pSnapPlayer->GetTeam() == TEAM_SPECTATORS || pSnapPlayer->IsPaused()));
494 }
495
496 if(ShowSpec)
497 {
498 CNetObj_SpecChar SpecChar = {};
499 SpecChar.m_X = m_pCharacter->Core()->m_Pos.x;
500 SpecChar.m_Y = m_pCharacter->Core()->m_Pos.y;
501 Server()->SnapNewItem(Id: TranslatedId, Data: SpecChar);
502 }
503}
504
505void CPlayer::FakeSnap()
506{
507 m_SentSnaps++;
508 if(Server()->ClientSupportsServerMaxClients(ClientId: m_ClientId))
509 return;
510
511 // see others in spec and vote menu
512 int SeeOthersId = GameServer()->m_PlayerMapping.SeeOthersId(ClientId: m_ClientId);
513
514 if(Server()->IsSixup(ClientId: m_ClientId))
515 {
516 // 0.7 removed `PLAYERFLAG_IN_MENU` so they have to receive the fake player at all times to support voting
517 if(GameServer()->m_PlayerMapping.TotalOverhang(ClientId: m_ClientId))
518 {
519 protocol7::CNetObj_PlayerInfo PlayerInfo = {};
520 PlayerInfo.m_PlayerFlags = 0;
521 PlayerInfo.m_Score = FinishTime::NOT_FINISHED_TIMESCORE;
522 PlayerInfo.m_Latency = 0;
523 Server()->SnapNewItem(Id: SeeOthersId, Data: PlayerInfo);
524 }
525
526 // nothing more to process in fake snap for 0.7
527 return;
528 }
529
530 // See Others. For better 0.6 mod compatibility: Hide fake player even from `TEAM_BLUE` scoreboard if's not available currently and mod is TeamPlay
531 // Note: this causes Player and Vote menu to jump one line up when at the bottom, because the player gets removed after closing the menu.
532 const bool SeeOthersAvailable = (m_Paused != PAUSE_NONE || m_Team == TEAM_SPECTATORS || (m_PlayerFlags & PLAYERFLAG_IN_MENU));
533 const bool ShowSeeOthersPlayer = !GameServer()->m_pController->IsTeamPlay() || SeeOthersAvailable;
534 if(GameServer()->m_PlayerMapping.TotalOverhang(ClientId: m_ClientId) && ShowSeeOthersPlayer)
535 {
536 CNetObj_ClientInfo ClientInfo = {};
537 StrToInts(pInts: ClientInfo.m_aName, NumInts: std::size(ClientInfo.m_aName), pStr: GameServer()->m_PlayerMapping.SeeOthersName(ClientId: m_ClientId));
538 StrToInts(pInts: ClientInfo.m_aClan, NumInts: std::size(ClientInfo.m_aClan), pStr: "");
539 StrToInts(pInts: ClientInfo.m_aSkin, NumInts: std::size(ClientInfo.m_aSkin), pStr: "default");
540 ClientInfo.m_UseCustomColor = 1;
541 ClientInfo.m_ColorBody = ClientInfo.m_ColorFeet = 6618880;
542 Server()->SnapNewItem(Id: SeeOthersId, Data: ClientInfo);
543
544 CNetObj_PlayerInfo PlayerInfo = {};
545 PlayerInfo.m_Latency = 0;
546 PlayerInfo.m_Local = 0;
547 PlayerInfo.m_ClientId = SeeOthersId;
548 PlayerInfo.m_Score = FinishTime::NOT_FINISHED_TIMESCORE;
549 PlayerInfo.m_Team = TEAM_BLUE; // `TEAM_BLUE` to hide from ddrace scoreboards
550 Server()->SnapNewItem(Id: SeeOthersId, Data: PlayerInfo);
551 }
552
553 // Empty fake player for chat messages from untranslated players
554 int FakeId = Server()->GetMaxClients(ClientId: m_ClientId) - 1;
555 CNetObj_ClientInfo ClientInfo = {};
556 StrToInts(pInts: ClientInfo.m_aName, NumInts: std::size(ClientInfo.m_aName), pStr: " ");
557 StrToInts(pInts: ClientInfo.m_aClan, NumInts: std::size(ClientInfo.m_aClan), pStr: "");
558 StrToInts(pInts: ClientInfo.m_aSkin, NumInts: std::size(ClientInfo.m_aSkin), pStr: "default");
559 Server()->SnapNewItem(Id: FakeId, Data: ClientInfo);
560
561 // Support pause feature for vanilla 0.6. Requires local object on client side
562 if(GetClientVersion() >= VERSION_DDNET_OLD || m_Paused != PAUSE_PAUSED)
563 return;
564
565 CNetObj_PlayerInfo PlayerInfo = {};
566 PlayerInfo.m_Latency = m_Latency.m_Min;
567 PlayerInfo.m_Local = 1;
568 PlayerInfo.m_ClientId = FakeId;
569 PlayerInfo.m_Score = FinishTime::NOT_FINISHED_TIMESCORE;
570 PlayerInfo.m_Team = TEAM_SPECTATORS;
571 Server()->SnapNewItem(Id: FakeId, Data: PlayerInfo);
572
573 int SpectatorId = m_SpectatorId;
574 if(SpectatorId >= 0 && !Server()->Translate(Target&: SpectatorId, ClientId: m_ClientId))
575 {
576 SpectatorId = FakeId;
577 }
578
579 CNetObj_SpectatorInfo SpectatorInfo = {};
580 SpectatorInfo.m_SpectatorId = SpectatorId;
581 SpectatorInfo.m_X = m_ViewPos.x;
582 SpectatorInfo.m_Y = m_ViewPos.y;
583 Server()->SnapNewItem(Id: FakeId, Data: SpectatorInfo);
584}
585
586void CPlayer::SendConnect(int FakeId, int ClientId)
587{
588 if(!Server()->IsSixup(ClientId: m_ClientId))
589 return;
590
591 CPlayer *pPlayer = GameServer()->m_apPlayers[ClientId];
592 if(!pPlayer)
593 return;
594
595 protocol7::CNetMsg_Sv_ClientInfo NewClientInfoMsg;
596 NewClientInfoMsg.m_ClientId = FakeId;
597 NewClientInfoMsg.m_Local = ClientId == m_ClientId;
598 NewClientInfoMsg.m_Team = pPlayer->GetTeam();
599 NewClientInfoMsg.m_pName = Server()->ClientName(ClientId);
600 NewClientInfoMsg.m_pClan = Server()->ClientClan(ClientId);
601 NewClientInfoMsg.m_Country = Server()->ClientCountry(ClientId);
602 NewClientInfoMsg.m_Silent = 1;
603
604 for(int p = 0; p < protocol7::NUM_SKINPARTS; p++)
605 {
606 NewClientInfoMsg.m_apSkinPartNames[p] = pPlayer->TeeInfos().m_aaSkinPartNames[p];
607 NewClientInfoMsg.m_aUseCustomColors[p] = pPlayer->TeeInfos().m_aUseCustomColors[p];
608 NewClientInfoMsg.m_aSkinPartColors[p] = pPlayer->TeeInfos().m_aSkinPartColors[p];
609 }
610
611 Server()->SendPackMsg(pMsg: &NewClientInfoMsg, Flags: MSGFLAG_VITAL | MSGFLAG_NORECORD | MSGFLAG_NOTRANSLATE, ClientId: m_ClientId);
612}
613
614void CPlayer::SendDisconnect(int FakeId)
615{
616 if(!Server()->IsSixup(ClientId: m_ClientId))
617 return;
618
619 protocol7::CNetMsg_Sv_ClientDrop ClientDropMsg;
620 ClientDropMsg.m_ClientId = FakeId;
621 ClientDropMsg.m_pReason = "";
622 ClientDropMsg.m_Silent = 1;
623
624 Server()->SendPackMsg(pMsg: &ClientDropMsg, Flags: MSGFLAG_VITAL | MSGFLAG_NORECORD | MSGFLAG_NOTRANSLATE, ClientId: m_ClientId);
625}
626
627void CPlayer::OnDisconnect()
628{
629 KillCharacter();
630
631 m_Moderating = false;
632}
633
634void CPlayer::OnPredictedInput(const CNetObj_PlayerInput *pNewInput)
635{
636 // skip the input if chat is active
637 if((m_PlayerFlags & PLAYERFLAG_CHATTING) && (pNewInput->m_PlayerFlags & PLAYERFLAG_CHATTING))
638 return;
639
640 AfkTimer();
641
642 m_NumInputs++;
643
644 if(m_pCharacter && !m_Paused)
645 m_pCharacter->OnPredictedInput(pNewInput);
646
647 // Magic number when we can hope that client has successfully identified itself
648 if(m_NumInputs == 20 && g_Config.m_SvClientSuggestion[0] != '\0' && GetClientVersion() <= VERSION_DDNET_OLD)
649 GameServer()->SendBroadcast(pText: g_Config.m_SvClientSuggestion, ClientId: m_ClientId);
650}
651
652void CPlayer::OnDirectInput(const CNetObj_PlayerInput *pNewInput)
653{
654 Server()->SetClientFlags(ClientId: m_ClientId, Flags: pNewInput->m_PlayerFlags);
655
656 AfkTimer();
657
658 if(((pNewInput->m_PlayerFlags & PLAYERFLAG_SPEC_CAM) || GetClientVersion() < VERSION_DDNET_PLAYERFLAG_SPEC_CAM) && ((!m_pCharacter && m_Team == TEAM_SPECTATORS) || m_Paused) && m_SpectatorId == SPEC_FREEVIEW)
659 m_ViewPos = vec2(pNewInput->m_TargetX, pNewInput->m_TargetY);
660
661 // check for activity
662 // if a player is killed, their scoreboard opens automatically, so ignore that flag
663 CNetObj_PlayerInput NewWithoutScoreboard = *pNewInput;
664 CNetObj_PlayerInput LastWithoutScoreboard = *m_pLastTarget;
665 NewWithoutScoreboard.m_PlayerFlags &= ~PLAYERFLAG_SCOREBOARD;
666 LastWithoutScoreboard.m_PlayerFlags &= ~PLAYERFLAG_SCOREBOARD;
667 if(mem_comp(a: &NewWithoutScoreboard, b: &LastWithoutScoreboard, size: sizeof(CNetObj_PlayerInput)))
668 {
669 mem_copy(dest: m_pLastTarget, source: pNewInput, size: sizeof(CNetObj_PlayerInput));
670 // Ignore the first direct input and keep the player afk as it is sent automatically
671 if(m_LastTargetInit)
672 UpdatePlaytime();
673 m_LastActionTick = Server()->Tick();
674 m_LastTargetInit = true;
675 }
676}
677
678void CPlayer::OnPredictedEarlyInput(const CNetObj_PlayerInput *pNewInput)
679{
680 m_PlayerFlags = pNewInput->m_PlayerFlags;
681
682 if(!m_pCharacter && m_Team != TEAM_SPECTATORS && (pNewInput->m_Fire & 1))
683 m_Spawning = true;
684
685 // skip the input if chat is active
686 if(m_PlayerFlags & PLAYERFLAG_CHATTING)
687 return;
688
689 if(m_pCharacter && !m_Paused)
690 m_pCharacter->OnDirectInput(pNewInput);
691}
692
693int CPlayer::GetClientVersion() const
694{
695 return m_pGameServer->GetClientVersion(ClientId: m_ClientId);
696}
697
698CCharacter *CPlayer::GetCharacter()
699{
700 if(m_pCharacter && m_pCharacter->IsAlive())
701 return m_pCharacter;
702 return nullptr;
703}
704
705const CCharacter *CPlayer::GetCharacter() const
706{
707 if(m_pCharacter && m_pCharacter->IsAlive())
708 return m_pCharacter;
709 return nullptr;
710}
711
712void CPlayer::KillCharacter(int Weapon, bool SendKillMsg)
713{
714 if(m_pCharacter)
715 {
716 m_pCharacter->Die(Killer: m_ClientId, Weapon, SendKillMsg);
717
718 delete m_pCharacter;
719 m_pCharacter = nullptr;
720 }
721}
722
723void CPlayer::Respawn(bool WeakHook)
724{
725 if(m_Team != TEAM_SPECTATORS)
726 {
727 m_WeakHookSpawn = WeakHook;
728 m_Spawning = true;
729 }
730}
731
732CCharacter *CPlayer::ForceSpawn(vec2 Pos)
733{
734 m_Spawning = false;
735 m_pCharacter = new(m_ClientId) CCharacter(&GameServer()->m_World, GameServer()->GetLastPlayerInput(ClientId: m_ClientId));
736 m_pCharacter->Spawn(pPlayer: this, Pos);
737 m_Team = TEAM_GAME;
738 return m_pCharacter;
739}
740
741void CPlayer::SetTeam(int Team, bool DoChatMsg)
742{
743 KillCharacter();
744
745 m_Team = Team;
746 m_LastSetTeam = Server()->Tick();
747 m_LastActionTick = Server()->Tick();
748 SetSpectatorId(SPEC_FREEVIEW);
749
750 protocol7::CNetMsg_Sv_Team Msg;
751 Msg.m_ClientId = m_ClientId;
752 Msg.m_Team = m_Team;
753 Msg.m_Silent = !DoChatMsg;
754 Msg.m_CooldownTick = m_LastSetTeam + Server()->TickSpeed() * g_Config.m_SvTeamChangeDelay;
755 Server()->SendPackMsg(pMsg: &Msg, Flags: MSGFLAG_VITAL | MSGFLAG_NORECORD, ClientId: -1);
756
757 if(Team == TEAM_SPECTATORS)
758 {
759 // update spectator modes
760 for(auto &pPlayer : GameServer()->m_apPlayers)
761 {
762 if(pPlayer && pPlayer->m_SpectatorId == m_ClientId)
763 pPlayer->SetSpectatorId(SPEC_FREEVIEW);
764 }
765 }
766
767 Server()->ExpireServerInfo();
768}
769
770bool CPlayer::SetTimerType(int TimerType)
771{
772 if(TimerType == TIMERTYPE_DEFAULT)
773 {
774 if(Server()->IsSixup(ClientId: m_ClientId))
775 m_TimerType = TIMERTYPE_SIXUP;
776 else
777 SetTimerType(g_Config.m_SvDefaultTimerType);
778
779 return true;
780 }
781
782 if(Server()->IsSixup(ClientId: m_ClientId))
783 {
784 if(TimerType == TIMERTYPE_SIXUP || TimerType == TIMERTYPE_NONE)
785 {
786 m_TimerType = TimerType;
787 return true;
788 }
789 else
790 return false;
791 }
792
793 if(TimerType == TIMERTYPE_GAMETIMER)
794 {
795 if(GetClientVersion() >= VERSION_DDNET_GAMETICK)
796 m_TimerType = TimerType;
797 else
798 return false;
799 }
800 else if(TimerType == TIMERTYPE_GAMETIMER_AND_BROADCAST)
801 {
802 if(GetClientVersion() >= VERSION_DDNET_GAMETICK)
803 m_TimerType = TimerType;
804 else
805 {
806 m_TimerType = TIMERTYPE_BROADCAST;
807 return false;
808 }
809 }
810 else
811 m_TimerType = TimerType;
812
813 return true;
814}
815
816void CPlayer::TryRespawn()
817{
818 vec2 SpawnPos;
819
820 if(!GameServer()->m_pController->CanSpawn(Team: m_Team, pOutPos: &SpawnPos, ClientId: m_ClientId))
821 return;
822
823 m_WeakHookSpawn = false;
824 m_Spawning = false;
825 m_pCharacter = new(m_ClientId) CCharacter(&GameServer()->m_World, GameServer()->GetLastPlayerInput(ClientId: m_ClientId));
826 m_ViewPos = SpawnPos;
827 m_pCharacter->Spawn(pPlayer: this, Pos: SpawnPos);
828 GameServer()->CreatePlayerSpawn(Pos: SpawnPos, Mask: GameServer()->m_pController->GetMaskForPlayerWorldEvent(Asker: m_ClientId));
829
830 if(g_Config.m_SvTeam == SV_TEAM_FORCED_SOLO)
831 m_pCharacter->SetSolo(true);
832}
833
834void CPlayer::UpdatePlaytime()
835{
836 m_LastPlaytime = time_get();
837}
838
839void CPlayer::AfkTimer()
840{
841 SetAfk(g_Config.m_SvMaxAfkTime != 0 && m_LastPlaytime < time_get() - time_freq() * g_Config.m_SvMaxAfkTime);
842}
843
844void CPlayer::SetAfk(bool Afk)
845{
846 if(m_Afk != Afk)
847 {
848 Server()->ExpireServerInfo();
849 m_Afk = Afk;
850 }
851}
852
853void CPlayer::SetInitialAfk(bool Afk)
854{
855 if(g_Config.m_SvMaxAfkTime == 0)
856 {
857 SetAfk(false);
858 return;
859 }
860
861 SetAfk(Afk);
862
863 // Ensure that the AFK state is not reset again automatically
864 if(Afk)
865 m_LastPlaytime = time_get() - time_freq() * g_Config.m_SvMaxAfkTime - 1;
866 else
867 m_LastPlaytime = time_get();
868}
869
870int CPlayer::GetDefaultEmote() const
871{
872 if(m_OverrideEmoteReset >= 0)
873 return m_OverrideEmote;
874
875 return m_DefEmote;
876}
877
878void CPlayer::OverrideDefaultEmote(int Emote, int Tick)
879{
880 m_OverrideEmote = Emote;
881 m_OverrideEmoteReset = Tick;
882 m_LastEyeEmote = Server()->Tick();
883}
884
885bool CPlayer::CanOverrideDefaultEmote() const
886{
887 return m_LastEyeEmote == 0 || m_LastEyeEmote + (int64_t)g_Config.m_SvEyeEmoteChangeDelay * Server()->TickSpeed() < Server()->Tick();
888}
889
890bool CPlayer::CanSpec() const
891{
892 return m_pCharacter->IsGrounded() && m_pCharacter->m_Pos == m_pCharacter->m_PrevPos;
893}
894
895void CPlayer::ProcessPause()
896{
897 if(m_ForcePauseTime && m_ForcePauseTime < Server()->Tick())
898 {
899 m_ForcePauseTime = 0;
900 GameServer()->SendChatTarget(To: m_ClientId, pText: "The force pause timer is now over, you can exit with /spec");
901 }
902
903 if(m_Paused == PAUSE_SPEC && !m_pCharacter->IsPaused() && CanSpec())
904 {
905 m_pCharacter->Pause(Pause: true);
906 GameServer()->CreateDeath(Pos: m_pCharacter->m_Pos, ClientId: m_ClientId, Mask: GameServer()->m_pController->GetMaskForPlayerWorldEvent(Asker: m_ClientId));
907 GameServer()->CreateSound(Pos: m_pCharacter->m_Pos, Sound: SOUND_PLAYER_DIE, Mask: GameServer()->m_pController->GetMaskForPlayerWorldEvent(Asker: m_ClientId));
908 }
909}
910
911int CPlayer::Pause(int State, bool Force)
912{
913 if(State < PAUSE_NONE || State > PAUSE_SPEC) // Invalid pause state passed
914 return 0;
915
916 if(!m_pCharacter)
917 return 0;
918
919 char aBuf[128];
920 if(State != m_Paused)
921 {
922 // Get to wanted state
923 switch(State)
924 {
925 case PAUSE_PAUSED:
926 case PAUSE_NONE:
927 if(m_pCharacter->IsPaused()) // First condition might be unnecessary
928 {
929 if(!Force && m_LastPause && m_LastPause + (int64_t)g_Config.m_SvSpecFrequency * Server()->TickSpeed() > Server()->Tick())
930 {
931 GameServer()->SendChatTarget(To: m_ClientId, pText: "Can't /spec that quickly.");
932 return m_Paused; // Do not update state. Do not collect $200
933 }
934 m_pCharacter->Pause(Pause: false);
935 m_ViewPos = m_pCharacter->m_Pos;
936 GameServer()->CreatePlayerSpawn(Pos: m_pCharacter->m_Pos, Mask: GameServer()->m_pController->GetMaskForPlayerWorldEvent(Asker: m_ClientId));
937 }
938 [[fallthrough]];
939 case PAUSE_SPEC:
940 if(g_Config.m_SvPauseMessages)
941 {
942 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: (State > PAUSE_NONE) ? "'%s' speced" : "'%s' resumed", Server()->ClientName(ClientId: m_ClientId));
943 GameServer()->SendChat(ClientId: -1, Team: TEAM_ALL, pText: aBuf);
944 }
945 break;
946 }
947
948 // Update state
949 m_Paused = State;
950 m_LastPause = Server()->Tick();
951
952 // Sixup needs a teamchange
953 protocol7::CNetMsg_Sv_Team Msg;
954 Msg.m_ClientId = m_ClientId;
955 Msg.m_CooldownTick = Server()->Tick();
956 Msg.m_Silent = true;
957 Msg.m_Team = m_Paused ? protocol7::TEAM_SPECTATORS : m_Team;
958
959 GameServer()->Server()->SendPackMsg(pMsg: &Msg, Flags: MSGFLAG_VITAL | MSGFLAG_NORECORD, ClientId: m_ClientId);
960
961 if(m_Paused == PAUSE_NONE)
962 {
963 GameServer()->m_PlayerMapping.ResetSeeOthers(ClientId: m_ClientId);
964 }
965 }
966
967 return m_Paused;
968}
969
970int CPlayer::ForcePause(int Time)
971{
972 m_ForcePauseTime = Server()->Tick() + Server()->TickSpeed() * Time;
973
974 if(g_Config.m_SvPauseMessages)
975 {
976 char aBuf[128];
977 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "'%s' was force-paused for %ds", Server()->ClientName(ClientId: m_ClientId), Time);
978 GameServer()->SendChat(ClientId: -1, Team: TEAM_ALL, pText: aBuf);
979 }
980
981 return Pause(State: PAUSE_SPEC, Force: true);
982}
983
984int CPlayer::IsPaused() const
985{
986 return m_ForcePauseTime ? m_ForcePauseTime : -1 * m_Paused;
987}
988
989bool CPlayer::IsPlaying() const
990{
991 return m_pCharacter && m_pCharacter->IsAlive();
992}
993
994void CPlayer::SpectatePlayerName(const char *pName)
995{
996 auto ClientId = GameServer()->FindClientIdByName(pName);
997 if(!ClientId.has_value())
998 return;
999
1000 SetSpectatorId(ClientId.value());
1001}
1002
1003void CPlayer::SetSpectatorId(int Id)
1004{
1005 m_SpectatorId = Id;
1006 GameServer()->m_PlayerMapping.ResetSeeOthers(ClientId: m_ClientId);
1007}
1008
1009void CPlayer::ProcessScoreResult(CScorePlayerResult &Result)
1010{
1011 if(Result.m_Success) // SQL request was successful
1012 {
1013 switch(Result.m_MessageKind)
1014 {
1015 case CScorePlayerResult::DIRECT:
1016 for(auto &aMessage : Result.m_Data.m_aaMessages)
1017 {
1018 if(aMessage[0] == 0)
1019 break;
1020 GameServer()->SendChatTarget(To: m_ClientId, pText: aMessage);
1021 }
1022 break;
1023 case CScorePlayerResult::ALL:
1024 {
1025 bool PrimaryMessage = true;
1026 for(auto &aMessage : Result.m_Data.m_aaMessages)
1027 {
1028 if(aMessage[0] == 0)
1029 break;
1030
1031 // Only the primary message counts towards the chat score, the
1032 // follow-up messages of one command must not mute the player.
1033 if(PrimaryMessage && GameServer()->ProcessSpamProtection(ClientId: m_ClientId))
1034 break;
1035
1036 GameServer()->SendChat(ClientId: -1, Team: TEAM_ALL, pText: aMessage, SpamProtectionClientId: -1);
1037 PrimaryMessage = false;
1038 }
1039 break;
1040 }
1041 case CScorePlayerResult::BROADCAST:
1042 if(Result.m_Data.m_aBroadcast[0] != 0)
1043 GameServer()->SendBroadcast(pText: Result.m_Data.m_aBroadcast, ClientId: -1);
1044 break;
1045 case CScorePlayerResult::MAP_VOTE:
1046 if(GameServer()->m_VoteCloseTime)
1047 {
1048 GameServer()->SendChatTarget(To: m_ClientId, pText: "Wait for current vote to end before calling a new one.");
1049 return;
1050 }
1051
1052 GameServer()->m_VoteType = CGameContext::VOTE_TYPE_OPTION;
1053 GameServer()->m_LastMapVote = time_get();
1054
1055 char aCmd[256];
1056 str_format(buffer: aCmd, buffer_size: sizeof(aCmd),
1057 format: "sv_reset_file types/%s/flexreset.cfg; change_map \"%s\"",
1058 Result.m_Data.m_MapVote.m_aServer, Result.m_Data.m_MapVote.m_aMap);
1059
1060 char aChatmsg[512];
1061 str_format(buffer: aChatmsg, buffer_size: sizeof(aChatmsg), format: "'%s' called vote to change server option '%s' (%s)",
1062 Server()->ClientName(ClientId: m_ClientId), Result.m_Data.m_MapVote.m_aMap, "/map");
1063
1064 GameServer()->CallVote(ClientId: m_ClientId, pDesc: Result.m_Data.m_MapVote.m_aMap, pCmd: aCmd, pReason: "/map", pChatmsg: aChatmsg);
1065 break;
1066 case CScorePlayerResult::PLAYER_INFO:
1067 {
1068 if(Result.m_Data.m_Info.m_Time.has_value())
1069 {
1070 GameServer()->Score()->PlayerData(Id: m_ClientId)->Set(Time: Result.m_Data.m_Info.m_Time.value(), aTimeCp: Result.m_Data.m_Info.m_aTimeCp);
1071 Server()->SetClientScore(ClientId: m_ClientId, Score: Result.m_Data.m_Info.m_Time.value());
1072 // update map best time if player's time is better
1073 if(!GameServer()->m_pController->m_CurrentRecord.has_value() ||
1074 Result.m_Data.m_Info.m_Time.value() < GameServer()->m_pController->m_CurrentRecord.value())
1075 {
1076 GameServer()->Score()->LoadBestTime();
1077 }
1078 }
1079 Server()->ExpireServerInfo();
1080 int Birthday = Result.m_Data.m_Info.m_Birthday;
1081 if(Birthday != 0 && !m_BirthdayAnnounced && GetCharacter())
1082 {
1083 char aBuf[512];
1084 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
1085 format: "Happy DDNet birthday to %s for finishing their first map %d year%s ago!",
1086 Server()->ClientName(ClientId: m_ClientId), Birthday, Birthday > 1 ? "s" : "");
1087 GameServer()->SendChat(ClientId: -1, Team: TEAM_ALL, pText: aBuf, SpamProtectionClientId: m_ClientId);
1088 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
1089 format: "Happy DDNet birthday, %s!\nYou have finished your first map exactly %d year%s ago!",
1090 Server()->ClientName(ClientId: m_ClientId), Birthday, Birthday > 1 ? "s" : "");
1091 GameServer()->SendBroadcast(pText: aBuf, ClientId: m_ClientId);
1092 m_BirthdayAnnounced = true;
1093
1094 GameServer()->CreateBirthdayEffect(Pos: GetCharacter()->m_Pos, Mask: GetCharacter()->TeamMask());
1095 }
1096 GameServer()->SendRecord(ClientId: m_ClientId);
1097 break;
1098 }
1099 case CScorePlayerResult::PLAYER_TIMECP:
1100 GameServer()->Score()->PlayerData(Id: m_ClientId)->SetBestTimeCp(Result.m_Data.m_Info.m_aTimeCp);
1101 char aBuf[128], aTime[32];
1102 str_time_float(secs: Result.m_Data.m_Info.m_Time.value(), format: ETimeFormat::HOURS_CENTISECS, buffer: aTime, buffer_size: sizeof(aTime));
1103 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "Showing the checkpoint times for '%s' with a race time of %s", Result.m_Data.m_Info.m_aRequestedPlayer, aTime);
1104 GameServer()->SendChatTarget(To: m_ClientId, pText: aBuf);
1105 break;
1106 }
1107 }
1108}
1109
1110vec2 CPlayer::CCameraInfo::ConvertTargetToWorld(vec2 Position, vec2 Target) const
1111{
1112 vec2 TargetCameraOffset(0, 0);
1113 float l = length(a: Target);
1114
1115 if(l > 0.0001f) // make sure that this isn't 0
1116 {
1117 float OffsetAmount = std::max(a: l - m_Deadzone, b: 0.0f) * (m_FollowFactor / 100.0f);
1118 TargetCameraOffset = normalize_pre_length(v: Target, len: l) * OffsetAmount;
1119 }
1120
1121 return Position + (Target - TargetCameraOffset) * m_Zoom + TargetCameraOffset;
1122}
1123
1124void CPlayer::UpdateNetworkClipRadius()
1125{
1126 // Keep the full show distance as radius while not viewing the own character, it covers the camera
1127 // offset the client applies when spectating and must not depend on the spectated client's values
1128 const CCharacter *pChr = GetCharacter();
1129 if(!m_CameraInfo.m_HasCameraInfo || !pChr || m_Paused)
1130 {
1131 m_NetworkClipRadius = m_ShowDistance;
1132 return;
1133 }
1134
1135 // With the dynamic camera the client camera is offset from the view position towards the cursor,
1136 // this mirrors the offset computation of the client, see CCamera::UpdateCamera.
1137 // The aim target is not validated, so it's limited to the maximum distance a client can aim at.
1138 const float TargetDistance = std::min(a: length(a: vec2(pChr->Core()->m_Input.m_TargetX, pChr->Core()->m_Input.m_TargetY)), b: 2000.0f); // cl_dyncam_max_distance
1139 const float DyncamOffset = std::max(a: TargetDistance - m_CameraInfo.m_Deadzone, b: 0.0f) * (m_CameraInfo.m_FollowFactor / 100.0f);
1140
1141 // Border of 10 blocks outside of view range so that no entities randomly appear even with high latency.
1142 // ShowDistance / 2 because it's the radius. This heavily decreases bandwidth on some maps and was likely a bug
1143 // or misunderstanding introduced by whoever added showdistance. Now it's like this since forever and was noticed because
1144 // the playermapping algorithm depends on the NetworkClipped function to determine when to add a new tee.
1145 const float Border = 32.f * 10.f;
1146 m_NetworkClipRadius = m_ShowDistance / 2.f + vec2(Border + DyncamOffset, Border + DyncamOffset);
1147}
1148
1149void CPlayer::CCameraInfo::Write(const CNetMsg_Cl_CameraInfo *Msg)
1150{
1151 // Clamp to the ranges a client can configure, these values also affect other clients spectating this player
1152 m_HasCameraInfo = true;
1153 m_Zoom = std::clamp(val: Msg->m_Zoom, lo: 10, hi: 2000000) / 1000.0f; // CCamera::MinZoomLevel, MaxZoomLevel
1154 m_Deadzone = std::clamp(val: Msg->m_Deadzone, lo: 0, hi: 3000); // cl_mouse_deadzone, cl_dyncam_deadzone
1155 m_FollowFactor = std::clamp(val: Msg->m_FollowFactor, lo: 0, hi: 200); // cl_mouse_followfactor, cl_dyncam_follow_factor
1156}
1157
1158void CPlayer::CCameraInfo::Reset()
1159{
1160 m_HasCameraInfo = false;
1161 m_Zoom = 1.0f;
1162 m_Deadzone = 0.0f;
1163 m_FollowFactor = 0.0f;
1164}
1165