1/* (c) Shereef Marzouk. See "licence DDRace.txt" and the readme.txt in the root of the distribution for more information. */
2#ifndef GAME_SERVER_TEAMS_H
3#define GAME_SERVER_TEAMS_H
4
5#include <engine/shared/protocol.h>
6
7#include <game/race_state.h>
8#include <game/server/gamecontext.h>
9#include <game/team_state.h>
10#include <game/teamscore.h>
11
12#include <memory>
13#include <optional>
14
15class CCharacter;
16class CPlayer;
17class IServer;
18struct CScoreSaveResult;
19
20class CGameTeams
21{
22 // `m_TeeStarted` is used to keep track whether a given tee has hit the
23 // start of the map yet. If a tee that leaves hasn't hit the start line
24 // yet, the team will be marked as "not allowed to finish"
25 // (`ETeamState::STARTED_UNFINISHABLE`). If this were not the case, tees
26 // could go around the startline on a map, leave one tee behind at
27 // start, go to the finish line, let the tee start and kill, allowing
28 // the team to finish instantly.
29 bool m_aTeeStarted[MAX_CLIENTS];
30 bool m_aTeeFinished[MAX_CLIENTS];
31 int m_aLastChat[MAX_CLIENTS];
32
33 ETeamState m_aTeamState[NUM_DDRACE_TEAMS];
34 bool m_aTeamLocked[NUM_DDRACE_TEAMS];
35 bool m_aTeamFlock[NUM_DDRACE_TEAMS];
36 CClientMask m_aInvited[NUM_DDRACE_TEAMS];
37 bool m_aPractice[NUM_DDRACE_TEAMS];
38 std::shared_ptr<CScoreSaveResult> m_apSaveTeamResult[NUM_DDRACE_TEAMS];
39 uint64_t m_aLastSwap[MAX_CLIENTS]; // index is id of player who initiated swap
40 bool m_aTeamSentStartWarning[NUM_DDRACE_TEAMS];
41 // `m_aTeamUnfinishableKillTick` is -1 by default and gets set when a
42 // team becomes unfinishable. If the team hasn't entered practice mode
43 // by that time, it'll get killed to prevent people not understanding
44 // the message from playing for a long time in an unfinishable team.
45 int m_aTeamUnfinishableKillTick[NUM_DDRACE_TEAMS];
46
47 // Team numbers as they are sent to clients before VERSION_DDNET_128_TEAMS, see UpdateLegacyTeamMap
48 int m_aLegacyTeamMap[NUM_DDRACE_TEAMS];
49 void UpdateLegacyTeamMap();
50
51 CGameContext *m_pGameContext;
52
53 /**
54 * Kill the whole team.
55 * @param Team The team id to kill
56 * @param NewStrongId The player with that id will get strong hook on everyone else, -1 will set the normal spawning order
57 * @param ExceptId The player that should not get killed
58 */
59 void KillTeam(int Team, int NewStrongId, int ExceptId = -1);
60 bool TeamFinished(int Team);
61 void OnTeamFinish(int Team, CPlayer **Players, unsigned int Size, int TimeTicks, const char *pTimestamp);
62 void OnFinish(CPlayer *Player, int TimeTicks, const char *pTimestamp);
63
64public:
65 CTeamsCore m_Core;
66
67 CGameTeams(CGameContext *pGameContext);
68
69 // helper methods
70 CCharacter *Character(int ClientId);
71 const CCharacter *Character(int ClientId) const;
72 CPlayer *GetPlayer(int ClientId);
73 CGameContext *GameServer();
74 const CGameContext *GameServer() const;
75 IServer *Server();
76 const IServer *Server() const;
77
78 /**
79 * Translate a team number for a client that does not support all team numbers yet.
80 * @param Team The team id to translate
81 * @param ClientId The client the team number is sent to
82 */
83 int TeamForClient(int Team, int ClientId) const;
84 bool ClientSupportsServerNumTeams(int ClientId) const;
85
86 void OnCharacterStart(int ClientId);
87 void OnCharacterFinish(int ClientId);
88 void OnCharacterSpawn(int ClientId);
89 void OnCharacterDeath(int ClientId, int Weapon);
90 void Tick();
91
92 // sets pError to an empty string on success (true)
93 // and sets pError if it returns false
94 bool CanJoinTeam(int ClientId, int Team, char *pError, int ErrorSize) const;
95
96 // returns true if successful. Writes error into pError on failure
97 bool SetCharacterTeam(int ClientId, int Team, char *pError, int ErrorSize);
98 void CheckTeamFinished(int Team);
99
100 void ChangeTeamState(int Team, ETeamState State);
101
102 CClientMask TeamMask(int Team, int ExceptId = -1, int Asker = -1, int VersionFlags = CGameContext::FLAG_SIX | CGameContext::FLAG_SIXUP);
103
104 int TeamSize(int Team) const;
105
106 // need to be very careful using this method. SERIOUSLY...
107 void SetForceCharacterTeam(int ClientId, int Team);
108
109 void Reset();
110 void ResetRoundState(int Team);
111 void ResetSwitchers(int Team);
112
113 void SendTeamsState(int ClientId);
114 void SetTeamLock(int Team, bool Lock);
115 void SetTeamFlock(int Team, bool Mode);
116 void ResetInvited(int Team);
117 void SetClientInvited(int Team, int ClientId, bool Invited);
118
119 ERaceState GetDDRaceState(const CPlayer *Player) const;
120 int GetStartTime(CPlayer *Player);
121 float *GetCurrentTimeCp(CPlayer *Player);
122 void SetDDRaceState(CPlayer *Player, ERaceState DDRaceState);
123 void SetStartTime(CPlayer *Player, int StartTime);
124 void SetLastTimeCp(CPlayer *Player, int LastTimeCp);
125 void KillCharacterOrTeam(int ClientId, int Team);
126 void ResetSavedTeam(int ClientId, int Team);
127 void RequestTeamSwap(CPlayer *pPlayer, CPlayer *pTargetPlayer, int Team);
128 void SwapTeamCharacters(CPlayer *pPrimaryPlayer, CPlayer *pTargetPlayer, int Team);
129 void CancelTeamSwap(CPlayer *pPlayer, int Team);
130 void ProcessSaveTeam();
131 std::optional<int> GetFirstEmptyTeam() const;
132 bool TeeStarted(int ClientId) const;
133 bool TeeFinished(int ClientId) const;
134 ETeamState GetTeamState(int Team) const;
135 bool TeamLocked(int Team) const;
136 bool TeamFlock(int Team) const;
137 bool IsInvited(int Team, int ClientId) const;
138 bool IsStarted(int Team) const;
139 void SetStarted(int ClientId, bool Started);
140 void SetFinished(int ClientId, bool Finished);
141 void SetSaving(int TeamId, std::shared_ptr<CScoreSaveResult> &SaveResult);
142 bool GetSaving(int TeamId) const;
143 void SetPractice(int Team, bool Enabled);
144 bool IsPractice(int Team);
145 bool IsValidTeamNumber(int Team) const;
146};
147
148#endif
149