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 <game/alloc.h>
13#include <game/server/save.h>
14
15#include <memory>
16#include <optional>
17
18class CCharacter;
19class CGameContext;
20class IServer;
21struct CNetObj_PlayerInput;
22struct CScorePlayerResult;
23
24// player object
25class CPlayer
26{
27 MACRO_ALLOC_POOL_ID()
28
29public:
30 CPlayer(CGameContext *pGameServer, uint32_t UniqueClientId, int ClientId, int Team);
31 ~CPlayer();
32
33 void Reset();
34
35 void TryRespawn();
36 void Respawn(bool WeakHook = false); // with WeakHook == true the character will be spawned after all calls of Tick from other Players
37 CCharacter *ForceSpawn(vec2 Pos); // required for loading savegames
38 void SetTeam(int Team, bool DoChatMsg = true);
39 int GetTeam() const { return m_Team; }
40 int GetCid() const { return m_ClientId; }
41 uint32_t GetUniqueCid() const { return m_UniqueClientId; }
42 int GetClientVersion() const;
43 bool SetTimerType(int TimerType);
44
45 void Tick();
46 void PostTick();
47
48 // will be called after all Tick and PostTick calls from other players
49 void PostPostTick();
50 void Snap(int SnappingClient);
51 void FakeSnap();
52
53 void OnDirectInput(const CNetObj_PlayerInput *pNewInput);
54 void OnPredictedInput(const CNetObj_PlayerInput *pNewInput);
55 void OnPredictedEarlyInput(const CNetObj_PlayerInput *pNewInput);
56 void OnDisconnect();
57
58 void KillCharacter(int Weapon = WEAPON_GAME, bool SendKillMsg = true);
59 CCharacter *GetCharacter();
60 const CCharacter *GetCharacter() const;
61
62 void SpectatePlayerName(const char *pName);
63
64 //---------------------------------------------------------
65 // this is used for snapping so we know how we can clip the view for the player
66 vec2 m_ViewPos;
67 int m_TuneZone;
68 int m_TuneZoneOld;
69
70 // states if the client is chatting, accessing a menu etc.
71 int m_PlayerFlags;
72
73 // used for snapping to just update latency if the scoreboard is active
74 int m_aCurLatency[MAX_CLIENTS];
75
76 int m_SentSnaps = 0;
77
78 int SpectatorId() const { return m_SpectatorId; }
79 void SetSpectatorId(int Id);
80
81 bool m_IsReady;
82
83 //
84 int m_Vote;
85 int m_VotePos;
86 //
87 int m_LastVoteCall;
88 int m_LastVoteTry;
89 int m_LastChat;
90 int m_LastSetTeam;
91 int m_LastSetSpectatorMode;
92 int m_LastChangeInfo;
93 int m_LastEmote;
94 int m_LastEmoteGlobal;
95 int m_LastKill;
96 int m_aLastCommands[4];
97 int m_LastCommandPos;
98 int m_LastWhisperTo;
99 int m_LastInvited;
100
101 int m_SendVoteIndex;
102
103 CTeeInfo m_TeeInfos;
104
105 int m_DieTick;
106 int m_PreviousDieTick;
107 int m_JoinTick;
108 int m_LastActionTick;
109 int m_TeamChangeTick;
110
111 // network latency calculations
112 struct
113 {
114 int m_Accum;
115 int m_AccumMin;
116 int m_AccumMax;
117 int m_Avg;
118 int m_Min;
119 int m_Max;
120 } m_Latency;
121
122private:
123 const uint32_t m_UniqueClientId;
124 CCharacter *m_pCharacter;
125 int m_NumInputs;
126 CGameContext *m_pGameServer;
127
128 CGameContext *GameServer() const { return m_pGameServer; }
129 IServer *Server() const;
130
131 //
132 bool m_Spawning;
133 bool m_WeakHookSpawn;
134 int m_ClientId;
135 int m_Team;
136
137 // used for spectator mode
138 int m_SpectatorId;
139
140 int m_Paused;
141 int64_t m_ForcePauseTime;
142 int64_t m_LastPause;
143 bool m_Afk;
144
145 int m_DefEmote;
146 int m_OverrideEmote;
147 int m_OverrideEmoteReset;
148 bool m_Halloween;
149
150public:
151 enum
152 {
153 PAUSE_NONE = 0,
154 PAUSE_PAUSED,
155 PAUSE_SPEC
156 };
157
158 enum
159 {
160 TIMERTYPE_DEFAULT = -1,
161 TIMERTYPE_GAMETIMER,
162 TIMERTYPE_BROADCAST,
163 TIMERTYPE_GAMETIMER_AND_BROADCAST,
164 TIMERTYPE_SIXUP,
165 TIMERTYPE_NONE,
166 };
167
168 bool m_DND;
169 bool m_Whispers;
170 int64_t m_FirstVoteTick;
171 char m_aTimeoutCode[64];
172
173 void ProcessPause();
174 int Pause(int State, bool Force);
175 int ForcePause(int Time);
176 int IsPaused() const;
177 bool CanSpec() const;
178
179 bool IsPlaying() const;
180 int64_t m_LastKickVote;
181 int64_t m_LastDDRaceTeamChange;
182 int m_ShowOthers;
183 bool m_ShowAll;
184 bool m_EnableSpectatorCount;
185 vec2 m_ShowDistance;
186 bool m_SpecTeam;
187 bool m_NinjaJetpack;
188
189 // camera info is used sparingly for converting aim target to absolute world coordinates
190 class CCameraInfo
191 {
192 friend class CPlayer;
193 bool m_HasCameraInfo;
194 float m_Zoom;
195 int m_Deadzone;
196 int m_FollowFactor;
197
198 public:
199 vec2 ConvertTargetToWorld(vec2 Position, vec2 Target) const;
200 void Write(const CNetMsg_Cl_CameraInfo *pMsg);
201 void Reset();
202 } m_CameraInfo;
203
204 int m_ChatScore;
205
206 bool m_Moderating;
207
208 void UpdatePlaytime();
209 void AfkTimer();
210 void SetAfk(bool Afk);
211 void SetInitialAfk(bool Afk);
212 bool IsAfk() const { return m_Afk; }
213
214 int64_t m_LastPlaytime;
215 int64_t m_LastEyeEmote;
216 int64_t m_LastBroadcast;
217 bool m_LastBroadcastImportance;
218
219 CNetObj_PlayerInput *m_pLastTarget;
220 bool m_LastTargetInit;
221
222 bool m_EyeEmoteEnabled;
223 int m_TimerType;
224
225 int GetDefaultEmote() const;
226 void OverrideDefaultEmote(int Emote, int Tick);
227 bool CanOverrideDefaultEmote() const;
228
229 bool m_FirstPacket;
230 int64_t m_LastSqlQuery;
231 void ProcessScoreResult(CScorePlayerResult &Result);
232 std::shared_ptr<CScorePlayerResult> m_ScoreQueryResult;
233 std::shared_ptr<CScorePlayerResult> m_ScoreFinishResult;
234 bool m_NotEligibleForFinish;
235 int64_t m_EligibleForFinishCheck;
236 bool m_VotedForPractice;
237 int m_SwapTargetsClientId; //Client ID of the swap target for the given player
238 bool m_BirthdayAnnounced;
239
240 int m_RescueMode;
241
242 CSaveTee m_LastTeleTee;
243 std::optional<CSaveTee> m_LastDeath;
244};
245
246#endif
247