OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4Teams.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2005-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2011-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 // player team management for teamwork melees
17 
18 #ifndef INC_C4Teams
19 #define INC_C4Teams
20 
21 #include "lib/C4InputValidation.h"
22 
23 // constant used by lobby to indicate invisible, random team
24 const int32_t TEAMID_Unknown = -1;
25 
26 // constant used by InitScenarioPlayer() to indicate creation of a new team
27 const int32_t TEAMID_New = -1;
28 
29 // one player team
30 class C4Team
31 {
32 private:
33  // std::vector...
34  // containing player info IDs
35  int32_t *piPlayers;
36  int32_t iPlayerCount;
37  int32_t iPlayerCapacity;
38 
39 public:
40  // copying
41  C4Team(const C4Team &rCopy);
42  C4Team &operator = (const C4Team &rCopy);
43 
44 protected:
45  // team identification; usually > 0 for a valid team
46  int32_t iID;
47  char Name[C4MaxName+1];
48  int32_t iPlrStartIndex; // 0 for unassigned; 1 to 4 if all players of that team shall be assigned a specific [Player*]-section in the Scenario.txt
49  uint32_t dwClr; // team color
50  StdCopyStrBuf sIconSpec; // icon drawing specification for offline or runtime team selection dialog
51  int32_t iMaxPlayer; // maximum number of players allowed in this team - 0 for infinite
52 
53  friend class C4TeamList;
54 
55 public:
56  C4Team() : piPlayers(nullptr), iPlayerCount(0), iPlayerCapacity(0), iID(0), iPlrStartIndex(0), dwClr(0), iMaxPlayer(0) { *Name=0; }
57  ~C4Team() { Clear(); }
58 
59  void Clear();
60  void AddPlayer(class C4PlayerInfo &rInfo, bool fAdjustPlayer); // add player by info; adjusts ID in info and at any joined player
61  void RemoveIndexedPlayer(int32_t iIndex); // remove info at index; this changes the local list only
62  void RemovePlayerByID(int32_t iID);
63 
64  int32_t GetPlayerCount() const { return iPlayerCount; }
65  const char *GetName() const { return Name; }
66  int32_t GetID() const { return iID; }
67  bool IsPlayerIDInTeam(int32_t iID); // search list for a player with the given ID
68  int32_t GetFirstUnjoinedPlayerID() const; // search for a player that does not have the join-flag set
69  int32_t GetPlrStartIndex() const { return iPlrStartIndex; }
70  uint32_t GetColor() const { return dwClr; }
71  const char *GetIconSpec() const { return sIconSpec.getData(); }
72  bool IsFull() const { return iMaxPlayer && (iPlayerCount >= iMaxPlayer); } // whether no more players may join this team
73  StdStrBuf GetNameWithParticipants() const; // compose team name like "Team 1 (boni, GhostBear, Clonko)"
74  bool HasWon() const; // return true if any member player of the team has won
75 
76  int32_t GetIndexedPlayer(int32_t iIndex) const { return Inside<int32_t>(iIndex, 0, iPlayerCount-1) ? piPlayers[iIndex] : 0; }
77 
78  void CompileFunc(StdCompiler *pComp);
79 
80  // this rechecks teams for all (not removed) players; sets players here by team selection in player infos
81  void RecheckPlayers();
82 
83  // this assigns a team color if it's still zero
84  void RecheckColor(C4TeamList &rForList);
85 };
86 
87 // global team list
89 {
90 public:
91  // team config constants
93  {
94  TEAM_None = 0,
98  TEAM_Dist = 4,
102  };
103 
104  // team distribution configuration
105  enum TeamDist
106  {
108  TEAMDIST_Free = 0, // anyone can choose teams
109  TEAMDIST_Host = 1, // host decides teams
110  TEAMDIST_None = 2, // no teams
111  TEAMDIST_Random = 3, // fixed random teams
112  TEAMDIST_RandomInv = 4, // fixed random teams invisible in lobby
114  };
115 
116 private:
117  // std::vector...
118  C4Team **ppList;
119  int32_t iTeamCount;
120  int32_t iTeamCapacity;
121 
122  int32_t iLastTeamID;
123  bool fAllowHostilityChange; // hostility not fixed
124  bool fAllowTeamSwitch; // teams not fixed
125  bool fActive;
126  bool fCustom; // set if read from team file or changed in scenario
127  bool fTeamColors; // if set, player colors are determined by team colors
128  bool fAutoGenerateTeams; // teams are generated automatically so there's enough teams for everyone
129  TeamDist eTeamDist;
130  int32_t iMaxScriptPlayers; // maximum number of script players to be added in the lobby
131  StdStrBuf sScriptPlayerNames; // default script player names
132 
133 public:
134  C4TeamList() : ppList(nullptr), iTeamCount(0), iTeamCapacity(0), iLastTeamID(0), fAllowHostilityChange(true), fAllowTeamSwitch(false),
135  fActive(true), fCustom(false), fTeamColors(false), fAutoGenerateTeams(false), eTeamDist(TEAMDIST_Free), iMaxScriptPlayers(0) {}
137  void Clear();
138 
139  C4TeamList &operator =(const C4TeamList &rCopy);
140 
141 private:
142  // no copying
143  C4TeamList(const C4TeamList &rCopy);
144 
145 private:
146  void AddTeam(C4Team *pNewTeam); // add a team; grow list if necessary
147  void ClearTeams(); // delete all teams
148  int32_t GetFreeTeamID();
149  bool GenerateDefaultTeams(int32_t iUpToID); // generate Team 1, Team 2, etc.
150 
151 public:
152  C4Team *GetTeamByID(int32_t iID) const; // get team by ID
153  C4Team *GetGenerateTeamByID(int32_t iID); // get team by ID; generate if not existant. Always generate for TEAMID_New.
154  C4Team *GetTeamByIndex(int32_t iIndex) const; // get team by list index, to enumerate all teams
155  C4Team *GetTeamByName(const char *szName) const; // match team name; case sensitive and not trimmed
156  C4Team *GetTeamByPlayerID(int32_t iID) const; // get team by player ID (not number!)
157  int32_t GetLargestTeamID() const;
158  C4Team *GetRandomSmallestTeam() const; // get team with least amount of players in it
159  int32_t GetTeamCount() const { return iTeamCount; }
160 
161  C4Team *CreateTeam(const char *szName); // create a custom team
162 
163  bool IsMultiTeams() const { return fActive; } // teams can be selected
164  bool IsCustom() const { return fCustom; } // whether teams are not generated as default
165  bool IsHostilityChangeAllowed() const { return fAllowHostilityChange; } // allow (temporary) hostility changes at runtime
166  bool IsTeamSwitchAllowed() const { return fAllowTeamSwitch; } // allow permanent team changes at runtime
167  bool CanLocalChooseTeam() const; // whether the local host can determine teams (e.g., not if teams are random, or if all except the player's current team are full)
168  bool CanLocalChooseTeam(int32_t idPlayer) const; // whether the local host can determine teams (e.g., not if teams are random, or if all except the player's current team are full)
169  bool CanLocalSeeTeam() const;
170  bool IsTeamColors() const { return fTeamColors; } // whether team colors are enabled
171  bool IsRandomTeam() const { return eTeamDist==TEAMDIST_Random ||eTeamDist==TEAMDIST_RandomInv; } // whether a random team mode is selected
172  bool IsJoin2TeamAllowed(int32_t idTeam, C4PlayerType plrType); // checks whether a team ID is valid and still available for new joins
173  bool IsAutoGenerateTeams() const { return fAutoGenerateTeams; }
174  bool IsRuntimeJoinTeamChoice() const { return IsCustom() && IsMultiTeams(); } // whether players joining at runtime must select a team first
175  int32_t GetMaxScriptPlayers() const { return iMaxScriptPlayers; } // return max number of script players to be added inthe lobby
176  int32_t GetForcedTeamSelection(int32_t idForPlayer) const; // if there's only one team for the player to join, return that team ID
177  StdStrBuf GetScriptPlayerName() const; // get a name to assign to a new script player. Try to avoid name conflicts
178  bool IsTeamVisible() const; // teams invisible during lobby time if TEAMDIST_RandomInv
179 
180  void EnforceLeagueRules(); // enforce some league settings, such as disallowing runtime team change
181 
182  // assign a team ID to player info; recheck if any user-set team infos are valid within the current team options
183  // creates a new team for melee; assigns the least-used team for teamwork melee
184  // host/single-call only; using UnsyncedRandom!
185  bool RecheckPlayerInfoTeams(C4PlayerInfo &rNewJoin, bool fByHost);
186 
187  // compiler
188  void CompileFunc(StdCompiler *pComp);
189  bool Load(C4Group &hGroup, class C4Scenario *pInitDefault, class C4LangStringTable *pLang); // clear self and load from group file (C4CFN_Teams); init default by scen if no team fiel is present
190  bool Save(C4Group &hGroup); // save to group file (C4CFN_Teams)
191 
192  // this rechecks teams for all (not removed) players; sets players here by team selection in player infos
193  void RecheckPlayers();
194 
195  // this reorders any unjoined players to teams if they are unevenly filled and team distribution is random
196  // any changed players will be flagged "updated"
197  void RecheckTeams();
198 
199  // marks all unjoined players as not-in-team and reassigns a team for them
200  // also automatically flags all affected player infos as updated
201  void ReassignAllTeams();
202 
203 private:
204  // team distribution configuration
205  StdStrBuf GetTeamDistName(TeamDist eTeamDist) const;
206 public:
207  void FillTeamDistOptions(C4GUI::ComboBox_FillCB *pFiller) const;
208  void SendSetTeamDist(TeamDist eNewDist);
209  TeamDist GetTeamDist() const { return eTeamDist; }
211  bool HasTeamDistOptions() const;
212  void SetTeamDistribution(TeamDist eToVal);
213  void SendSetTeamColors(bool fEnabled);
214  void SetTeamColors(bool fEnabled);
215 
216  // return number of non-empty teams. if players have not been assigned, project a guess on future team count.
217  int32_t GetStartupTeamCount(int32_t startup_player_count);
218 };
219 
220 #endif // INC_C4Teams
void RecheckTeams()
Definition: C4Teams.cpp:670
const char * getData() const
Definition: StdBuf.h:450
int32_t GetPlayerCount() const
Definition: C4Teams.h:64
void RecheckPlayers()
Definition: C4Teams.cpp:664
void SendSetTeamDist(TeamDist eNewDist)
Definition: C4Teams.cpp:778
const char * GetIconSpec() const
Definition: C4Teams.h:71
int32_t GetID() const
Definition: C4Teams.h:66
char Name[C4MaxName+1]
Definition: C4Teams.h:47
bool IsRandomTeam() const
Definition: C4Teams.h:171
bool IsTeamColors() const
Definition: C4Teams.h:170
void RecheckColor(C4TeamList &rForList)
Definition: C4Teams.cpp:170
Definition: C4Teams.h:30
uint32_t GetColor() const
Definition: C4Teams.h:70
bool IsAutoGenerateTeams() const
Definition: C4Teams.h:173
StdStrBuf GetNameWithParticipants() const
Definition: C4Teams.cpp:210
const int32_t TEAMID_Unknown
Definition: C4Teams.h:24
C4Team * GetGenerateTeamByID(int32_t iID)
Definition: C4Teams.cpp:391
int32_t GetMaxScriptPlayers() const
Definition: C4Teams.h:175
~C4Team()
Definition: C4Teams.h:57
bool IsTeamVisible() const
Definition: C4Teams.cpp:455
void SendSetTeamColors(bool fEnabled)
Definition: C4Teams.cpp:821
bool IsMultiTeams() const
Definition: C4Teams.h:163
C4Team * GetTeamByID(int32_t iID) const
Definition: C4Teams.cpp:384
C4Team * GetRandomSmallestTeam() const
Definition: C4Teams.cpp:436
void ReassignAllTeams()
Definition: C4Teams.cpp:714
C4Team * CreateTeam(const char *szName)
Definition: C4Teams.cpp:361
bool CanLocalChooseTeam() const
Definition: C4Teams.cpp:293
C4TeamList()
Definition: C4Teams.h:134
bool Load(C4Group &hGroup, class C4Scenario *pInitDefault, class C4LangStringTable *pLang)
Definition: C4Teams.cpp:605
void RemoveIndexedPlayer(int32_t iIndex)
Definition: C4Teams.cpp:84
bool Save(C4Group &hGroup)
Definition: C4Teams.cpp:647
bool RecheckPlayerInfoTeams(C4PlayerInfo &rNewJoin, bool fByHost)
Definition: C4Teams.cpp:464
int32_t iPlrStartIndex
Definition: C4Teams.h:48
bool IsTeamSwitchAllowed() const
Definition: C4Teams.h:166
bool IsFull() const
Definition: C4Teams.h:72
int32_t GetForcedTeamSelection(int32_t idForPlayer) const
Definition: C4Teams.cpp:851
void SetTeamColors(bool fEnabled)
Definition: C4Teams.cpp:827
void AddPlayer(class C4PlayerInfo &rInfo, bool fAdjustPlayer)
Definition: C4Teams.cpp:53
C4Team * GetTeamByIndex(int32_t iIndex) const
Definition: C4Teams.cpp:405
int32_t GetPlrStartIndex() const
Definition: C4Teams.h:69
bool CanLocalSeeTeam() const
Definition: C4Teams.cpp:328
bool IsCustom() const
Definition: C4Teams.h:164
const unsigned int C4MaxName
const char * GetName() const
Definition: C4Teams.h:65
C4Team()
Definition: C4Teams.h:56
C4Team * GetTeamByPlayerID(int32_t iID) const
Definition: C4Teams.cpp:421
C4PlayerType
Definition: C4Constants.h:152
const int32_t TEAMID_New
Definition: C4Teams.h:27
bool HasTeamDistOptions() const
Definition: C4Teams.cpp:791
uint32_t dwClr
Definition: C4Teams.h:49
int32_t GetLargestTeamID() const
Definition: C4Teams.cpp:428
void RemovePlayerByID(int32_t iID)
Definition: C4Teams.cpp:95
void Clear()
Definition: C4Teams.cpp:256
int32_t GetFirstUnjoinedPlayerID() const
Definition: C4Teams.cpp:113
bool IsPlayerIDInTeam(int32_t iID)
Definition: C4Teams.cpp:106
TeamDist GetTeamDist() const
Definition: C4Teams.h:209
void RecheckPlayers()
Definition: C4Teams.cpp:141
C4Team & operator=(const C4Team &rCopy)
void CompileFunc(StdCompiler *pComp)
Definition: C4Teams.cpp:126
StdCopyStrBuf sIconSpec
Definition: C4Teams.h:50
void FillTeamDistOptions(C4GUI::ComboBox_FillCB *pFiller) const
Definition: C4Teams.cpp:766
~C4TeamList()
Definition: C4Teams.h:136
C4Team * GetTeamByName(const char *szName) const
Definition: C4Teams.cpp:413
void SetTeamDistribution(TeamDist eToVal)
Definition: C4Teams.cpp:797
C4TeamList & operator=(const C4TeamList &rCopy)
Definition: C4Teams.cpp:274
int32_t GetTeamCount() const
Definition: C4Teams.h:159
void CompileFunc(StdCompiler *pComp)
Definition: C4Teams.cpp:546
int32_t iID
Definition: C4Teams.h:46
bool IsRuntimeJoinTeamChoice() const
Definition: C4Teams.h:174
bool IsJoin2TeamAllowed(int32_t idTeam, C4PlayerType plrType)
Definition: C4Teams.cpp:535
bool HasWon() const
Definition: C4Teams.cpp:235
StdStrBuf GetTeamDistString() const
Definition: C4Teams.cpp:785
int32_t iMaxPlayer
Definition: C4Teams.h:51
void EnforceLeagueRules()
Definition: C4Teams.cpp:844
int32_t GetStartupTeamCount(int32_t startup_player_count)
Definition: C4Teams.cpp:901
bool IsHostilityChangeAllowed() const
Definition: C4Teams.h:165
StdStrBuf GetScriptPlayerName() const
Definition: C4Teams.cpp:887
void Clear()
Definition: C4Teams.cpp:45
int32_t GetIndexedPlayer(int32_t iIndex) const
Definition: C4Teams.h:76