OpenClonk
C4Team Class Reference

#include <C4Teams.h>

Collaboration diagram for C4Team:
[legend]

Public Member Functions

 C4Team (const C4Team &rCopy)
 
C4Teamoperator= (const C4Team &rCopy)
 
 C4Team ()
 
 ~C4Team ()
 
void Clear ()
 
void AddPlayer (class C4PlayerInfo &rInfo, bool fAdjustPlayer)
 
void RemoveIndexedPlayer (int32_t iIndex)
 
void RemovePlayerByID (int32_t iID)
 
int32_t GetPlayerCount () const
 
const char * GetName () const
 
int32_t GetID () const
 
bool IsPlayerIDInTeam (int32_t iID)
 
int32_t GetFirstUnjoinedPlayerID () const
 
int32_t GetPlrStartIndex () const
 
uint32_t GetColor () const
 
const char * GetIconSpec () const
 
bool IsFull () const
 
StdStrBuf GetNameWithParticipants () const
 
bool HasWon () const
 
int32_t GetIndexedPlayer (int32_t iIndex) const
 
void CompileFunc (StdCompiler *pComp)
 
void RecheckPlayers ()
 
void RecheckColor (C4TeamList &rForList)
 

Protected Attributes

int32_t iID {0}
 
char Name [C4MaxName+1]
 
int32_t iPlrStartIndex {0}
 
uint32_t dwClr {0}
 
StdCopyStrBuf sIconSpec
 
int32_t iMaxPlayer {0}
 

Friends

class C4TeamList
 

Detailed Description

Definition at line 30 of file C4Teams.h.

Constructor & Destructor Documentation

◆ C4Team() [1/2]

C4Team::C4Team ( const C4Team rCopy)

Definition at line 30 of file C4Teams.cpp.

31  : piPlayers(new int32_t[rCopy.GetPlayerCount()]),
32  iPlayerCount(rCopy.GetPlayerCount()),
33  iPlayerCapacity(rCopy.GetPlayerCount()),
34  iID(rCopy.GetID()), iPlrStartIndex(rCopy.iPlrStartIndex), dwClr(rCopy.dwClr),
36 {
37  // copy name
38  SCopy(rCopy.GetName(), Name, C4MaxName);
39  // copy players
40  for (int32_t i = 0; i < iPlayerCount; i++)
41  piPlayers[i] = rCopy.GetIndexedPlayer(i);
42 }
const unsigned int C4MaxName
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:152
int32_t iID
Definition: C4Teams.h:46
uint32_t dwClr
Definition: C4Teams.h:49
char Name[C4MaxName+1]
Definition: C4Teams.h:47
int32_t GetID() const
Definition: C4Teams.h:66
int32_t GetIndexedPlayer(int32_t iIndex) const
Definition: C4Teams.h:76
const char * GetIconSpec() const
Definition: C4Teams.h:71
int32_t iMaxPlayer
Definition: C4Teams.h:51
StdCopyStrBuf sIconSpec
Definition: C4Teams.h:50
int32_t GetPlayerCount() const
Definition: C4Teams.h:64
const char * GetName() const
Definition: C4Teams.h:65
int32_t iPlrStartIndex
Definition: C4Teams.h:48

References C4MaxName, GetIndexedPlayer(), GetName(), Name, and SCopy().

Here is the call graph for this function:

◆ C4Team() [2/2]

C4Team::C4Team ( )
inline

Definition at line 56 of file C4Teams.h.

56 { *Name=0; }

References Name.

◆ ~C4Team()

C4Team::~C4Team ( )
inline

Definition at line 57 of file C4Teams.h.

57 { Clear(); }
void Clear()
Definition: C4Teams.cpp:44

References Clear().

Here is the call graph for this function:

Member Function Documentation

◆ AddPlayer()

void C4Team::AddPlayer ( class C4PlayerInfo rInfo,
bool  fAdjustPlayer 
)

Definition at line 52 of file C4Teams.cpp.

53 {
54  // must not happen!
55  assert(rInfo.GetID());
56  if (!rInfo.GetID()) return;
57  // add player; grow vector if necessary
58  if (iPlayerCount >= iPlayerCapacity)
59  {
60  int32_t *piNewPlayers = new int32_t[iPlayerCapacity = (iPlayerCount+4)&~3];
61  if (iPlayerCount) memcpy(piNewPlayers, piPlayers, iPlayerCount*sizeof(int32_t));
62  delete [] piPlayers; piPlayers = piNewPlayers;
63  }
64  // store new player
65  piPlayers[iPlayerCount++] = rInfo.GetID();
66  if (!fAdjustPlayer) return;
67  // set values in info
68  rInfo.SetTeam(GetID());
69  if (Game.Teams.IsTeamColors()) rInfo.SetColor(GetColor());
70  // and in actual player, if it is joined already
71  if (rInfo.IsJoined())
72  {
73  C4Player *pJoinedPlr = ::Players.GetByInfoID(rInfo.GetID());
74  assert(pJoinedPlr || (rInfo.GetType() == C4PT_Script));
75  if (pJoinedPlr)
76  {
77  pJoinedPlr->Team = GetID();
78  if (Game.Teams.IsTeamColors()) pJoinedPlr->SetPlayerColor(GetColor());
79  }
80  }
81 }
@ C4PT_Script
Definition: C4Constants.h:155
C4Game Game
Definition: C4Globals.cpp:52
C4PlayerList Players
C4TeamList & Teams
Definition: C4Game.h:70
void SetPlayerColor(uint32_t dwNewClr)
Definition: C4Player.cpp:1711
int32_t Team
Definition: C4Player.h:88
C4Player * GetByInfoID(int iInfoID) const
uint32_t GetColor() const
Definition: C4Teams.h:70
bool IsTeamColors() const
Definition: C4Teams.h:169

References C4PT_Script, Game, C4PlayerList::GetByInfoID(), GetColor(), C4PlayerInfo::GetID(), GetID(), C4PlayerInfo::GetType(), C4PlayerInfo::IsJoined(), C4TeamList::IsTeamColors(), Players, C4PlayerInfo::SetColor(), C4Player::SetPlayerColor(), C4PlayerInfo::SetTeam(), C4Player::Team, and C4Game::Teams.

Referenced by C4TeamList::RecheckPlayerInfoTeams(), RecheckPlayers(), C4TeamList::RecheckTeams(), and C4Player::ScenarioAndTeamInit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Clear()

void C4Team::Clear ( )

Definition at line 44 of file C4Teams.cpp.

45 {
46  delete [] piPlayers; piPlayers = nullptr;
47  iPlayerCount = iPlayerCapacity = iMaxPlayer = 0;
48  iID = 0; *Name=0;
49  sIconSpec.Clear();
50 }
void Clear()
Definition: StdBuf.h:466

References StdStrBuf::Clear(), iID, iMaxPlayer, Name, and sIconSpec.

Referenced by CompileFunc(), and ~C4Team().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CompileFunc()

void C4Team::CompileFunc ( StdCompiler pComp)

Definition at line 125 of file C4Teams.cpp.

126 {
127  if (pComp->isDeserializer()) Clear();
128  pComp->Value(mkNamingAdapt(iID, "id", 0));
129  pComp->Value(mkNamingAdapt(mkStringAdaptMA(Name), "Name", ""));
130  pComp->Value(mkNamingAdapt(iPlrStartIndex, "PlrStartIndex", 0));
131  pComp->Value(mkNamingAdapt(iPlayerCount, "PlayerCount", 0));
132  if (pComp->isDeserializer()) { delete [] piPlayers; piPlayers = new int32_t [iPlayerCapacity = iPlayerCount]; ZeroMem(piPlayers, sizeof(*piPlayers) * iPlayerCount); }
133  pComp->Value(mkNamingAdapt(mkArrayAdapt(piPlayers, iPlayerCount, -1), "Players"));
134  pComp->Value(mkNamingAdapt(dwClr, "Color", 0u));
136  "IconSpec", StdCopyStrBuf()));
137  pComp->Value(mkNamingAdapt(iMaxPlayer, "MaxPlayer", 0));
138 }
std::enable_if< std::is_pod< T >::value >::type ZeroMem(T *lpMem, size_t dwSize)
Definition: Standard.h:60
StdArrayAdapt< T > mkArrayAdapt(T *pArray, int iSize)
Definition: StdAdaptors.h:336
StdParameterAdapt< T, P > mkParAdapt(T &&rObj, P &&rPar)
Definition: StdAdaptors.h:490
StdStringAdapt mkStringAdaptMA(char(&szString)[size])
Definition: StdAdaptors.h:190
StdNamingAdapt< T > mkNamingAdapt(T &&rValue, const char *szName)
Definition: StdAdaptors.h:92
void Value(const T &rStruct)
Definition: StdCompiler.h:161
virtual bool isDeserializer()
Definition: StdCompiler.h:53

References Clear(), dwClr, iID, iMaxPlayer, iPlrStartIndex, StdCompiler::isDeserializer(), mkArrayAdapt(), mkNamingAdapt(), mkParAdapt(), mkStringAdaptMA(), Name, StdCompiler::RCT_All, sIconSpec, StdCompiler::Value(), and ZeroMem().

Here is the call graph for this function:

◆ GetColor()

uint32_t C4Team::GetColor ( ) const
inline

Definition at line 70 of file C4Teams.h.

70 { return dwClr; }

References dwClr.

Referenced by AddPlayer(), C4MainMenu::DoRefillInternal(), RecheckColor(), and C4PlayerInfoList::UpdatePlayerAttributes().

Here is the caller graph for this function:

◆ GetFirstUnjoinedPlayerID()

int32_t C4Team::GetFirstUnjoinedPlayerID ( ) const

Definition at line 112 of file C4Teams.cpp.

113 {
114  // search for a player that does not have the join-flag set
115  int32_t i=iPlayerCount, idPlr, *piPlr = piPlayers;
116  C4PlayerInfo *pInfo;
117  while (i--)
118  if ((pInfo = Game.PlayerInfos.GetPlayerInfoByID(idPlr = *piPlr++)))
119  if (!pInfo->HasJoinIssued())
120  return idPlr;
121  // none found
122  return 0;
123 }
C4PlayerInfoList & PlayerInfos
Definition: C4Game.h:71
bool HasJoinIssued() const
Definition: C4PlayerInfo.h:167
C4PlayerInfo * GetPlayerInfoByID(int32_t id) const

References Game, C4PlayerInfoList::GetPlayerInfoByID(), C4PlayerInfo::HasJoinIssued(), and C4Game::PlayerInfos.

Referenced by C4TeamList::RecheckTeams().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetIconSpec()

const char* C4Team::GetIconSpec ( ) const
inline

Definition at line 71 of file C4Teams.h.

71 { return sIconSpec.getData(); }
const char * getData() const
Definition: StdBuf.h:442

References StdStrBuf::getData(), and sIconSpec.

Referenced by C4MainMenu::DoRefillInternal().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetID()

int32_t C4Team::GetID ( ) const
inline

Definition at line 66 of file C4Teams.h.

66 { return iID; }

References iID.

Referenced by AddPlayer(), C4GameOverDlg::C4GameOverDlg(), C4MainMenu::DoRefillInternal(), C4TeamList::GetForcedTeamSelection(), C4TeamList::RecheckPlayerInfoTeams(), RecheckPlayers(), C4Player::ScenarioAndTeamInit(), and C4GameSave::WriteDescPlayers().

Here is the caller graph for this function:

◆ GetIndexedPlayer()

int32_t C4Team::GetIndexedPlayer ( int32_t  iIndex) const
inline

Definition at line 76 of file C4Teams.h.

76 { return Inside<int32_t>(iIndex, 0, iPlayerCount-1) ? piPlayers[iIndex] : 0; }

Referenced by C4Team(), C4Player::FindNewOwner(), and GetNameWithParticipants().

Here is the caller graph for this function:

◆ GetName()

const char* C4Team::GetName ( ) const
inline

Definition at line 65 of file C4Teams.h.

65 { return Name; }

References Name.

Referenced by C4Team(), GetNameWithParticipants(), and C4GameSave::WriteDescPlayers().

Here is the caller graph for this function:

◆ GetNameWithParticipants()

StdStrBuf C4Team::GetNameWithParticipants ( ) const

Definition at line 209 of file C4Teams.cpp.

210 {
211  // compose team name like "Team 1 (boni, GhostBear, Clonko)"
212  // or just "Team 1" for empty team
213  StdStrBuf sTeamName;
214  sTeamName.Copy(GetName());
215  if (GetPlayerCount())
216  {
217  sTeamName.Append(" (");
218  int32_t iTeamPlrCount=0;
219  for (int32_t j=0; j<GetPlayerCount(); ++j)
220  {
221  int32_t iPlr = GetIndexedPlayer(j);
222  C4PlayerInfo *pPlrInfo;
223  if (iPlr) if ((pPlrInfo = Game.PlayerInfos.GetPlayerInfoByID(iPlr)))
224  {
225  if (iTeamPlrCount++) sTeamName.Append(", ");
226  sTeamName.Append(pPlrInfo->GetName());
227  }
228  }
229  sTeamName.AppendChar(')');
230  }
231  return sTeamName;
232 }
const char * GetName() const
Definition: C4PlayerInfo.h:157
void AppendChar(char cChar)
Definition: StdBuf.h:588
void Copy()
Definition: StdBuf.h:467
void Append(const char *pnData, size_t iChars)
Definition: StdBuf.h:519

References StdStrBuf::Append(), StdStrBuf::AppendChar(), StdStrBuf::Copy(), Game, GetIndexedPlayer(), C4PlayerInfo::GetName(), GetName(), GetPlayerCount(), C4PlayerInfoList::GetPlayerInfoByID(), and C4Game::PlayerInfos.

Referenced by C4MainMenu::DoRefillInternal().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetPlayerCount()

int32_t C4Team::GetPlayerCount ( ) const
inline

Definition at line 64 of file C4Teams.h.

64 { return iPlayerCount; }

Referenced by C4MainMenu::DoRefillInternal(), C4Player::FindNewOwner(), GetNameWithParticipants(), C4TeamList::GetRandomSmallestTeam(), C4TeamList::GetStartupTeamCount(), C4TeamList::RecheckPlayerInfoTeams(), and C4TeamList::RecheckTeams().

Here is the caller graph for this function:

◆ GetPlrStartIndex()

int32_t C4Team::GetPlrStartIndex ( ) const
inline

Definition at line 69 of file C4Teams.h.

69 { return iPlrStartIndex; }

References iPlrStartIndex.

Referenced by C4Player::Execute(), and C4Player::ScenarioInit().

Here is the caller graph for this function:

◆ HasWon()

bool C4Team::HasWon ( ) const

Definition at line 234 of file C4Teams.cpp.

235 {
236  // return true if any member player of the team has won
237  bool fHasWon = false;
238  for (int32_t i=0; i<iPlayerCount; ++i)
239  {
240  int32_t id; C4PlayerInfo *pInfo;
241  if ((id = piPlayers[i]))
242  if ((pInfo = Game.PlayerInfos.GetPlayerInfoByID(id)))
243  if (pInfo->HasWon())
244  {
245  fHasWon = true;
246  break;
247  }
248  }
249  return fHasWon;
250 }
bool HasWon() const
Definition: C4PlayerInfo.h:181

References Game, C4PlayerInfoList::GetPlayerInfoByID(), C4PlayerInfo::HasWon(), and C4Game::PlayerInfos.

Referenced by C4PlayerInfo::HasTeamWon().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsFull()

bool C4Team::IsFull ( ) const
inline

Definition at line 72 of file C4Teams.h.

72 { return iMaxPlayer && (iPlayerCount >= iMaxPlayer); } // whether no more players may join this team

References iMaxPlayer.

Referenced by C4TeamList::IsJoin2TeamAllowed().

Here is the caller graph for this function:

◆ IsPlayerIDInTeam()

bool C4Team::IsPlayerIDInTeam ( int32_t  iID)

Definition at line 105 of file C4Teams.cpp.

106 {
107  int32_t i=iPlayerCount, *piPlr = piPlayers;
108  while (i--) if (*piPlr++ == iID) return true;
109  return false;
110 }

References iID.

Referenced by RecheckPlayers().

Here is the caller graph for this function:

◆ operator=()

C4Team& C4Team::operator= ( const C4Team rCopy)

◆ RecheckColor()

void C4Team::RecheckColor ( C4TeamList rForList)

Definition at line 169 of file C4Teams.cpp.

170 {
171  // number of times trying new player colors
172  const int32_t C4MaxTeamColorChangeTries = 100;
173  if (!dwClr)
174  {
175  const int defTeamColorCount = 10;
176  DWORD defTeamColorRGB[defTeamColorCount] = { 0xF40000, 0x00C800, 0xFCF41C, 0x2020FF, // red, green, yellow, blue,
177  0xC48444, 0xFFFFFF, 0x848484, 0xFF00EF, // brown, white, grey, pink,
178  0x00FFFF, 0x784830
179  }; // cyan, dk brown
180  // no color assigned yet: Generate by team ID
181  if (iID >=1 && iID <=defTeamColorCount)
182  {
183  // default colors
184  dwClr = defTeamColorRGB[iID-1] | 0xff000000;
185  }
186  else
187  {
188  // find a new, unused color
189  for (int32_t iTry=1; iTry<C4MaxTeamColorChangeTries; ++iTry)
190  {
192  int32_t iIdx=0; C4Team *pTeam; bool fOK=true;
193  while ((pTeam = rForList.GetTeamByIndex(iIdx++)))
194  if (pTeam != this)
195  if (IsColorConflict(pTeam->GetColor(), dwClr))
196  {
197  fOK=false;
198  break;
199  }
200  // color is fine?
201  if (fOK) return;
202  // it's not; try next color
203  }
204  // Giving up: Use last generated color
205  }
206  }
207 }
bool IsColorConflict(DWORD dwClr1, DWORD dwClr2)
DWORD GenerateRandomPlayerColor(int32_t iTry)
uint32_t DWORD
Definition: C4Teams.h:31
C4Team * GetTeamByIndex(int32_t iIndex) const
Definition: C4Teams.cpp:404

References dwClr, GenerateRandomPlayerColor(), GetColor(), C4TeamList::GetTeamByIndex(), iID, and IsColorConflict().

Referenced by C4TeamList::CreateTeam(), and C4TeamList::Load().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ RecheckPlayers()

void C4Team::RecheckPlayers ( )

Definition at line 140 of file C4Teams.cpp.

141 {
142  // check all players within the team
143  for (int32_t i=0; i<iPlayerCount; ++i)
144  {
145  bool fIsValid = false; int32_t id; C4PlayerInfo *pInfo;
146  if ((id = piPlayers[i]))
147  if ((pInfo = Game.PlayerInfos.GetPlayerInfoByID(id)))
148  if (pInfo->GetTeam() == GetID())
149  if (pInfo->IsUsingTeam())
150  fIsValid = true;
151  // removal will decrease iPlayerCount, which will abort the loop earlier
152  if (!fIsValid) RemoveIndexedPlayer(i--);
153  }
154  // now check for any new players in the team
155  int32_t id = 0; C4PlayerInfo *pInfo;
156  while ((pInfo = Game.PlayerInfos.GetNextPlayerInfoByID(id)))
157  {
158  id = pInfo->GetID();
159  if (pInfo->GetTeam() == GetID())
160  if (pInfo->IsUsingTeam())
161  if (!IsPlayerIDInTeam(id))
162  AddPlayer(*pInfo, false);
163  }
164 }
int32_t GetTeam() const
Definition: C4PlayerInfo.h:195
bool IsUsingTeam() const
Definition: C4PlayerInfo.h:173
int32_t GetID() const
Definition: C4PlayerInfo.h:194
C4PlayerInfo * GetNextPlayerInfoByID(int32_t id) const
void AddPlayer(class C4PlayerInfo &rInfo, bool fAdjustPlayer)
Definition: C4Teams.cpp:52
bool IsPlayerIDInTeam(int32_t iID)
Definition: C4Teams.cpp:105
void RemoveIndexedPlayer(int32_t iIndex)
Definition: C4Teams.cpp:83

References AddPlayer(), Game, C4PlayerInfo::GetID(), GetID(), C4PlayerInfoList::GetNextPlayerInfoByID(), C4PlayerInfoList::GetPlayerInfoByID(), C4PlayerInfo::GetTeam(), IsPlayerIDInTeam(), C4PlayerInfo::IsUsingTeam(), C4Game::PlayerInfos, and RemoveIndexedPlayer().

Here is the call graph for this function:

◆ RemoveIndexedPlayer()

void C4Team::RemoveIndexedPlayer ( int32_t  iIndex)

Definition at line 83 of file C4Teams.cpp.

84 {
85  // safety
86  assert(Inside<int32_t>(iIndex, 0, iPlayerCount-1));
87  if (!Inside<int32_t>(iIndex, 0, iPlayerCount-1)) return;
88  // move other players done
89  for (int32_t i = iIndex+1; i < iPlayerCount; ++i)
90  piPlayers[i-1] = piPlayers[i];
91  --iPlayerCount;
92 }

Referenced by RecheckPlayers(), and RemovePlayerByID().

Here is the caller graph for this function:

◆ RemovePlayerByID()

void C4Team::RemovePlayerByID ( int32_t  iID)

Definition at line 94 of file C4Teams.cpp.

95 {
96  // get index
97  int32_t i;
98  for (i=0; i < iPlayerCount; ++i)
99  if (piPlayers[i] == iID) break;
100  if (i == iPlayerCount) { assert(false); return; } // ID not found
101  // remove it
103 }

References iID, and RemoveIndexedPlayer().

Referenced by C4TeamList::RecheckTeams(), and C4Player::ScenarioAndTeamInit().

Here is the call graph for this function:
Here is the caller graph for this function:

Friends And Related Function Documentation

◆ C4TeamList

friend class C4TeamList
friend

Definition at line 53 of file C4Teams.h.

Member Data Documentation

◆ dwClr

uint32_t C4Team::dwClr {0}
protected

Definition at line 49 of file C4Teams.h.

Referenced by CompileFunc(), GetColor(), and RecheckColor().

◆ iID

int32_t C4Team::iID {0}
protected

◆ iMaxPlayer

int32_t C4Team::iMaxPlayer {0}
protected

Definition at line 51 of file C4Teams.h.

Referenced by Clear(), CompileFunc(), and IsFull().

◆ iPlrStartIndex

int32_t C4Team::iPlrStartIndex {0}
protected

Definition at line 48 of file C4Teams.h.

Referenced by CompileFunc(), and GetPlrStartIndex().

◆ Name

char C4Team::Name[C4MaxName+1]
protected

Definition at line 47 of file C4Teams.h.

Referenced by C4Team(), Clear(), CompileFunc(), C4TeamList::CreateTeam(), and GetName().

◆ sIconSpec

StdCopyStrBuf C4Team::sIconSpec
protected

Definition at line 50 of file C4Teams.h.

Referenced by Clear(), CompileFunc(), and GetIconSpec().


The documentation for this class was generated from the following files: