1#ifndef GAME_SERVER_PLAYERMAPPING_H
2#define GAME_SERVER_PLAYERMAPPING_H
3
4#include <engine/shared/protocol.h>
5
6#include <generated/protocol7.h>
7
8#include <game/teamscore.h>
9
10class CGameContext;
11class CPlayer;
12
13// The teeworlds 0.7 client and older ddnet clients
14// only support up to 64 clients. And also only up to 64 client ids (0-63).
15// The CPlayerMapping class manages a mapping of real server internal
16// client ids in the range of 0-127 and the fake client ids send to clients
17// in the range of 0-63 (or vanilla 0.6: ids 0-15)
18//
19// If a client does not support 128 slots the server will only show the 63 (64-1 for empty client for chat)
20// closest players (that closest selection currently tries to minimize changes by only changing when really required)
21// to the client. Because these 64 closest players are not
22// guaranteed to have client ids in the range of 0-63 we need to maintain a
23// fake client id list for every old client.
24//
25// Additionally there is the "See Others" feature which lets you cycle through a list of
26// all players manually in pause or spectator mode by holding right shift (+spectate) or call vote menu.
27
28class CPlayerMapping
29{
30public:
31 class CSixupCfg
32 {
33 public:
34 CSixupCfg() :
35 m_SkipTimeoutedId(false),
36 m_ClearSlots(false)
37 {
38 }
39 bool m_SkipTimeoutedId;
40 bool m_ClearSlots;
41 };
42
43private:
44 class CGameContext *m_pGameServer;
45 class CConfig *m_pConfig;
46 class IServer *m_pServer;
47
48 // Number of players per page for see others feature in +spectate
49 // (128 - (64 - 2)) / 2 + 1 reserved slots for max 2 pages. We want to show some players still on screen, as we have enough slots with 64 slots
50 static constexpr int ms_MaxNumSeeOthers = 34;
51 // 16 - 2 - 1 is max for 16p clients, keep local char. MaxNumSeeOthers() will take care if reserved players take up more space.
52 static constexpr int ms_MaxNumSeeOthersVanilla = 13;
53 // Teams are messy. Dont highlight teams bigger than 10 tees in playermapping so that big teams wont break anything
54 static constexpr int ms_MaxTeamSizePlayerMap = 10;
55 // Dont reserve more than half of the 62 slots for players in teams, the rest is needed for the closest tees
56 static constexpr int ms_MaxTotalTeamSizePlayerMap = 30;
57
58 int m_aTeamSizes[NUM_DDRACE_TEAMS];
59 bool m_ReserveAnyTeamSlots;
60 char m_aSeeOthersName[MAX_NAME_LENGTH];
61
62 class CPlayerMap
63 {
64 public:
65 void Init(int ClientId, CPlayerMapping *pPlayerMapping);
66 void InitPlayer(CSixupCfg SixupCfg);
67 CPlayerMapping *m_pPlayerMapping;
68 CPlayer *Player() const;
69 int m_ClientId;
70 int m_NumReserved;
71 bool m_UpdateTeamsState;
72 bool m_aReserved[MAX_CLIENTS];
73 bool m_ResortReserved;
74 int *m_pMap;
75 int *m_pReverseMap;
76 void Update();
77 void Add(int MapId, int ClientId);
78 int Remove(int MapId);
79 void InsertNextEmptyOrReplace(int ClientId);
80 int MapSize() const;
81 // See others
82 int m_SeeOthersPage;
83 int m_TotalOverhang;
84 int m_NumPages;
85 int m_NumSeeOthers;
86 bool m_aWasSeeOthers[MAX_CLIENTS];
87 int m_LastSeeOthersVoteTick;
88 bool m_DoSeeOthersByVote;
89 void DoSeeOthers();
90 void CycleSeeOthers();
91 void UpdateSeeOthers() const;
92 void ResetSeeOthers();
93 int MaxNumSeeOthers();
94 } m_aMap[MAX_CLIENTS];
95 void UpdatePlayerMap(int ClientId);
96
97public:
98 class CGameContext *GameServer() { return m_pGameServer; }
99 class CConfig *Config() { return m_pConfig; }
100 class IServer *Server() { return m_pServer; }
101
102 void Init(CGameContext *pGameServer);
103 void Tick();
104
105 void InitPlayerMap(int ClientId, CSixupCfg SixupCfg = CSixupCfg()) { m_aMap[ClientId].InitPlayer(SixupCfg); }
106 void UpdateTeamsState(int ClientId) { m_aMap[ClientId].m_UpdateTeamsState = true; }
107 void ForceInsertPlayer(int Insert, int ClientId) { m_aMap[ClientId].InsertNextEmptyOrReplace(ClientId: Insert); }
108
109 enum class ESeeOthersInd
110 {
111 NONE = -1,
112 PLAYER = 0,
113 BUTTON = 1,
114 };
115 int SeeOthersId(int ClientId) const;
116 bool DoSeeOthers(int ClientId, int SelectedId, bool DoByVote = false);
117 void ResetSeeOthers(int ClientId);
118 int TotalOverhang(int ClientId) const;
119 ESeeOthersInd SeeOthersInd(int ClientId, int MapId) const;
120 const char *SeeOthersName(int ClientId);
121 bool ReserveTeamSlots(int DDTeam, int ClientId) const;
122};
123
124#endif
125