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