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