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 CGameContext *m_pGameContext;
47
48 /**
49 * Kill the whole team.
50 * @param Team The team id to kill
51 * @param NewStrongId The player with that id will get strong hook on everyone else, -1 will set the normal spawning order
52 * @param ExceptId The player that should not get killed
53 */
54 void KillTeam(int Team, int NewStrongId, int ExceptId = -1);
55 bool TeamFinished(int Team);
56 void OnTeamFinish(int Team, CPlayer **Players, unsigned int Size, int TimeTicks, const char *pTimestamp);
57 void OnFinish(CPlayer *Player, int TimeTicks, const char *pTimestamp);
58
59public:
60 CTeamsCore m_Core;
61
62 CGameTeams(CGameContext *pGameContext);
63
64 // helper methods
65 CCharacter *Character(int ClientId);
66 const CCharacter *Character(int ClientId) const;
67 CPlayer *GetPlayer(int ClientId);
68 CGameContext *GameServer();
69 const CGameContext *GameServer() const;
70 class IServer *Server();
71
72 void OnCharacterStart(int ClientId);
73 void OnCharacterFinish(int ClientId);
74 void OnCharacterSpawn(int ClientId);
75 void OnCharacterDeath(int ClientId, int Weapon);
76 void Tick();
77
78 // sets pError to an empty string on success (true)
79 // and sets pError if it returns false
80 bool CanJoinTeam(int ClientId, int Team, char *pError, int ErrorSize) const;
81
82 // returns true if successful. Writes error into pError on failure
83 bool SetCharacterTeam(int ClientId, int Team, char *pError, int ErrorSize);
84 void CheckTeamFinished(int Team);
85
86 void ChangeTeamState(int Team, ETeamState State);
87
88 CClientMask TeamMask(int Team, int ExceptId = -1, int Asker = -1, int VersionFlags = CGameContext::FLAG_SIX | CGameContext::FLAG_SIXUP);
89
90 int Count(int Team) const;
91
92 // need to be very careful using this method. SERIOUSLY...
93 void SetForceCharacterTeam(int ClientId, int Team);
94
95 void Reset();
96 void ResetRoundState(int Team);
97 void ResetSwitchers(int Team);
98
99 void SendTeamsState(int ClientId);
100 void SetTeamLock(int Team, bool Lock);
101 void SetTeamFlock(int Team, bool Mode);
102 void ResetInvited(int Team);
103 void SetClientInvited(int Team, int ClientId, bool Invited);
104
105 ERaceState GetDDRaceState(const CPlayer *Player) const;
106 int GetStartTime(CPlayer *Player);
107 float *GetCurrentTimeCp(CPlayer *Player);
108 void SetDDRaceState(CPlayer *Player, ERaceState DDRaceState);
109 void SetStartTime(CPlayer *Player, int StartTime);
110 void SetLastTimeCp(CPlayer *Player, int LastTimeCp);
111 void KillCharacterOrTeam(int ClientId, int Team);
112 void ResetSavedTeam(int ClientId, int Team);
113 void RequestTeamSwap(CPlayer *pPlayer, CPlayer *pTargetPlayer, int Team);
114 void SwapTeamCharacters(CPlayer *pPrimaryPlayer, CPlayer *pTargetPlayer, int Team);
115 void CancelTeamSwap(CPlayer *pPlayer, int Team);
116 void ProcessSaveTeam();
117 std::optional<int> GetFirstEmptyTeam() const;
118 bool TeeStarted(int ClientId) const;
119 bool TeeFinished(int ClientId) const;
120 ETeamState GetTeamState(int Team) const;
121 bool TeamLocked(int Team) const;
122 bool TeamFlock(int Team) const;
123 bool IsInvited(int Team, int ClientId) const;
124 bool IsStarted(int Team) const;
125 void SetStarted(int ClientId, bool Started);
126 void SetFinished(int ClientId, bool Finished);
127 void SetSaving(int TeamId, std::shared_ptr<CScoreSaveResult> &SaveResult);
128 bool GetSaving(int TeamId) const;
129 void SetPractice(int Team, bool Enabled);
130 bool IsPractice(int Team);
131 bool IsValidTeamNumber(int Team) const;
132};
133
134#endif
135