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#ifndef GAME_SERVER_PLAYER_H
4#define GAME_SERVER_PLAYER_H
5
6#include "teeinfo.h"
7
8#include <base/vmath.h>
9
10#include <engine/shared/protocol.h>
11
12#include <generated/protocol.h>
13
14#include <game/alloc.h>
15#include <game/server/save.h>
16
17#include <memory>
18#include <optional>
19
20class CCharacter;
21class CGameContext;
22class IServer;
23struct CVoteOptionServer;
24struct CNetObj_PlayerInput;
25struct CScorePlayerResult;
26
27// player object
28class CPlayer
29{
30 MACRO_ALLOC_POOL_ID()
31
32public:
33 CPlayer(CGameContext *pGameServer, uint32_t UniqueClientId, int ClientId, int Team);
34 ~CPlayer();
35
36 void Reset();
37
38 void TryRespawn();
39
40 // mark respawning, with weak hook if WeakHook is true and strong otherwise
41 void Respawn(bool WeakHook = false);
42 CCharacter *ForceSpawn(vec2 Pos); // required for loading savegames
43 void SetTeam(int Team, bool DoChatMsg = true);
44 int GetTeam() const { return m_Team; }
45 int GetCid() const { return m_ClientId; }
46 uint32_t GetUniqueCid() const { return m_UniqueClientId; }
47 int GetClientVersion() const;
48 bool SetTimerType(int TimerType);
49
50 void Tick();
51 void PostTick();
52
53 // will be called after all Tick and PostTick calls from other players
54 void PostPostTick();
55 void Snap(int SnappingClient);
56 void FakeSnap();
57 void SendConnect(int FakeId, int ClientId);
58 void SendDisconnect(int FakeId);
59 int m_aStrongWeakId[LEGACY_MAX_CLIENTS];
60
61 void OnDirectInput(const CNetObj_PlayerInput *pNewInput);
62 void OnPredictedInput(const CNetObj_PlayerInput *pNewInput);
63 void OnPredictedEarlyInput(const CNetObj_PlayerInput *pNewInput);
64 void OnDisconnect();
65
66 void KillCharacter(int Weapon = WEAPON_GAME, bool SendKillMsg = true);
67 CCharacter *GetCharacter();
68 const CCharacter *GetCharacter() const;
69
70 void SpectatePlayerName(const char *pName);
71
72 //---------------------------------------------------------
73 // this is used for snapping so we know how we can clip the view for the player
74 vec2 m_ViewPos;
75 int m_TuneZone;
76 int m_TuneZoneOld;
77
78 // states if the client is chatting, accessing a menu etc.
79 int m_PlayerFlags;
80
81 // used for snapping to just update latency if the scoreboard is active
82 int m_aCurLatency[MAX_CLIENTS];
83
84 int m_SentSnaps = 0;
85
86 int SpectatorId() const { return m_SpectatorId; }
87 void SetSpectatorId(int Id);
88
89 bool m_IsReady;
90
91 //
92 int m_Vote;
93 int m_VotePos;
94 //
95 int m_LastVoteCall;
96 int m_LastVoteTry;
97 int m_LastChat;
98 int m_LastSetTeam;
99 int m_LastSetSpectatorMode;
100 int m_LastChangeInfo;
101 int m_LastEmote;
102 int m_LastEmoteGlobal;
103 int m_LastKill;
104 int m_aLastCommands[4];
105 int m_LastCommandPos;
106 int m_LastWhisperTo;
107 int m_LastInvited;
108
109 int m_SendVoteIndex;
110 // last option sent, only read while m_SendVoteIndex is greater than zero. Options
111 // appended after the list was fully sent follow it.
112 const CVoteOptionServer *m_pLastSentVoteOption;
113
114 const CTeeInfo &TeeInfos() const { return m_TeeInfos; }
115 void SetTeeInfos(const CTeeInfo &TeeInfos);
116 // Sets the 0.6 tee infos, deriving the 0.7 skin parts from them unless the client is 0.7
117 void SetTeeInfos(const char *pSkinName, bool UseCustomColor, int ColorBody, int ColorFeet);
118 // The snapped client info is the same for every snapping client, so it is cached
119 // and only rebuilt once the name, clan, country or tee infos have changed
120 void InvalidateClientInfo() { m_ClientInfoValid = false; }
121
122 int m_DieTick;
123 int m_PreviousDieTick;
124 int m_JoinTick;
125 int m_LastActionTick;
126 int m_TeamChangeTick;
127
128 // network latency calculations
129 struct
130 {
131 int m_Accum;
132 int m_AccumMin;
133 int m_AccumMax;
134 int m_Avg;
135 int m_Min;
136 int m_Max;
137 } m_Latency;
138
139private:
140 CTeeInfo m_TeeInfos;
141 CNetObj_ClientInfo m_ClientInfo = {};
142 bool m_ClientInfoValid = false;
143
144 const uint32_t m_UniqueClientId;
145 CCharacter *m_pCharacter;
146 int m_NumInputs;
147 CGameContext *m_pGameServer;
148
149 CGameContext *GameServer() const { return m_pGameServer; }
150 IServer *Server() const;
151
152 //
153 bool m_Spawning;
154 bool m_WeakHookSpawn;
155 int m_ClientId;
156 int m_Team;
157
158 // used for spectator mode
159 int m_SpectatorId;
160
161 int m_Paused;
162 int64_t m_ForcePauseTime;
163 int64_t m_LastPause;
164 bool m_Afk;
165
166 int m_DefEmote;
167 int m_OverrideEmote;
168 int m_OverrideEmoteReset;
169 bool m_Halloween;
170
171public:
172 enum
173 {
174 PAUSE_NONE = 0,
175 PAUSE_PAUSED,
176 PAUSE_SPEC
177 };
178
179 enum
180 {
181 TIMERTYPE_DEFAULT = -1,
182 TIMERTYPE_GAMETIMER,
183 TIMERTYPE_BROADCAST,
184 TIMERTYPE_GAMETIMER_AND_BROADCAST,
185 TIMERTYPE_SIXUP,
186 TIMERTYPE_NONE,
187 };
188
189 bool m_DND;
190 bool m_Whispers;
191 int64_t m_FirstVoteTick;
192 char m_aTimeoutCode[64];
193
194 void ProcessPause();
195 int Pause(int State, bool Force);
196 int ForcePause(int Time);
197 int IsPaused() const;
198 bool CanSpec() const;
199
200 bool IsPlaying() const;
201 int64_t m_LastKickVote;
202 std::optional<int64_t> m_LastDDRaceTeamChange;
203 int m_ShowOthers;
204 bool m_ShowAll;
205 bool m_EnableSpectatorCount;
206 vec2 m_ShowDistance;
207 bool m_SpecTeam;
208 bool m_NinjaJetpack;
209
210 // camera info is used sparingly for converting aim target to absolute world coordinates
211 class CCameraInfo
212 {
213 friend class CPlayer;
214 bool m_HasCameraInfo;
215 float m_Zoom;
216 int m_Deadzone;
217 int m_FollowFactor;
218
219 public:
220 vec2 ConvertTargetToWorld(vec2 Position, vec2 Target) const;
221 void Write(const CNetMsg_Cl_CameraInfo *pMsg);
222 void Reset();
223 } m_CameraInfo;
224
225 // effective radius for network clipping, updated every tick since it depends on the dynamic camera offset
226 vec2 m_NetworkClipRadius;
227 void UpdateNetworkClipRadius();
228
229 int m_ChatScore;
230
231 bool m_Moderating;
232
233 void UpdatePlaytime();
234 void AfkTimer();
235 void SetAfk(bool Afk);
236 void SetInitialAfk(bool Afk);
237 bool IsAfk() const { return m_Afk; }
238
239 int64_t m_LastPlaytime;
240 int64_t m_LastEyeEmote;
241 int64_t m_LastBroadcast;
242 bool m_LastBroadcastImportance;
243
244 CNetObj_PlayerInput *m_pLastTarget;
245 bool m_LastTargetInit;
246
247 bool m_EyeEmoteEnabled;
248 int m_TimerType;
249
250 // Tick at which to kick the client if it still hasn't identified as a DDNet-based client
251 int m_DDNetVersionKickTick;
252
253 int GetDefaultEmote() const;
254 void OverrideDefaultEmote(int Emote, int Tick);
255 bool CanOverrideDefaultEmote() const;
256
257 bool m_FirstPacket;
258 int64_t m_LastSqlQuery;
259 void ProcessScoreResult(CScorePlayerResult &Result);
260 std::shared_ptr<CScorePlayerResult> m_ScoreQueryResult;
261 std::shared_ptr<CScorePlayerResult> m_ScoreFinishResult;
262 bool m_NotEligibleForFinish;
263 int64_t m_EligibleForFinishCheck;
264 bool m_VotedForPractice;
265 int m_SwapTargetsClientId; //Client ID of the swap target for the given player
266 bool m_BirthdayAnnounced;
267
268 int m_RescueMode;
269
270 CSaveTee m_LastTeleTee;
271 std::optional<CSaveTee> m_LastDeath;
272};
273
274#endif
275