OpenClonk
C4ObjectInfoList Class Reference

#include <C4ObjectInfoList.h>

Collaboration diagram for C4ObjectInfoList:
[legend]

Public Member Functions

 C4ObjectInfoList ()
 
 ~C4ObjectInfoList ()
 
void Default ()
 
void Clear ()
 
void Evaluate ()
 
void DetachFromObjects ()
 
int32_t Load (C4Group &hGroup)
 
bool Add (C4ObjectInfo *pInfo)
 
bool Save (C4Group &hGroup, bool fSavegame, bool fStoreTiny, C4DefList *pDefs)
 
C4ObjectInfoNew (C4ID n_id, C4DefList *pDefs)
 
C4ObjectInfoGetIdle (C4ID c_id, C4DefList &rDefs)
 
C4ObjectInfoGetIdle (const char *szByName)
 
C4ObjectInfoGetFirst ()
 
bool IsElement (C4ObjectInfo *pInfo)
 
void Strip (C4DefList &rDefs)
 
void MakeValidName (char *sName)
 
bool NameExists (const char *szName)
 

Public Attributes

int32_t iNumCreated
 

Protected Member Functions

C4ObjectInfoGetLast ()
 
C4ObjectInfoGetPrevious (C4ObjectInfo *pInfo)
 

Protected Attributes

C4ObjectInfoFirst
 

Detailed Description

Definition at line 25 of file C4ObjectInfoList.h.

Constructor & Destructor Documentation

◆ C4ObjectInfoList()

C4ObjectInfoList::C4ObjectInfoList ( )

Definition at line 30 of file C4ObjectInfoList.cpp.

31 {
32  Default();
33 }

References Default().

Here is the call graph for this function:

◆ ~C4ObjectInfoList()

C4ObjectInfoList::~C4ObjectInfoList ( )

Definition at line 35 of file C4ObjectInfoList.cpp.

36 {
37  Clear();
38 }

References Clear().

Here is the call graph for this function:

Member Function Documentation

◆ Add()

bool C4ObjectInfoList::Add ( C4ObjectInfo pInfo)

Definition at line 95 of file C4ObjectInfoList.cpp.

96 {
97  if (!pInfo)
98  {
99  return false;
100  }
101  pInfo->Next = First;
102  First = pInfo;
103  return true;
104 }
C4ObjectInfo * Next
Definition: C4ObjectInfo.h:41
C4ObjectInfo * First

References First, and C4ObjectInfo::Next.

Referenced by Load(), and New().

Here is the caller graph for this function:

◆ Clear()

void C4ObjectInfoList::Clear ( )

Definition at line 46 of file C4ObjectInfoList.cpp.

47 {
48  C4ObjectInfo *next;
49  while (First)
50  {
51  next = First->Next;
52  delete First;
53  First = next;
54  }
55 }

References First, and C4ObjectInfo::Next.

Referenced by ~C4ObjectInfoList().

Here is the caller graph for this function:

◆ Default()

void C4ObjectInfoList::Default ( )

Definition at line 40 of file C4ObjectInfoList.cpp.

41 {
42  First = nullptr;
43  iNumCreated = 0;
44 }

References First, and iNumCreated.

Referenced by C4ObjectInfoList(), and C4Player::C4Player().

Here is the caller graph for this function:

◆ DetachFromObjects()

void C4ObjectInfoList::DetachFromObjects ( )

Definition at line 262 of file C4ObjectInfoList.cpp.

263 {
264  C4ObjectInfo *cinf;
265  for (cinf = First; cinf; cinf = cinf->Next)
266  {
267  ::Objects.ClearInfo(cinf);
268  }
269 }
C4GameObjects Objects
Definition: C4Globals.cpp:48
void ClearInfo(C4ObjectInfo *info)

References C4ObjectList::ClearInfo(), First, C4ObjectInfo::Next, and Objects.

Referenced by C4PlayerList::Remove().

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

◆ Evaluate()

void C4ObjectInfoList::Evaluate ( )

Definition at line 211 of file C4ObjectInfoList.cpp.

212 {
213  C4ObjectInfo *cinf;
214  for (cinf = First; cinf; cinf = cinf->Next)
215  {
216  cinf->Evaluate();
217  }
218 }

References C4ObjectInfo::Evaluate(), First, and C4ObjectInfo::Next.

Referenced by C4Player::Evaluate().

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

◆ GetFirst()

C4ObjectInfo* C4ObjectInfoList::GetFirst ( )
inline

Definition at line 45 of file C4ObjectInfoList.h.

45 { return First; }

References First.

Referenced by C4Player::LocalSync().

Here is the caller graph for this function:

◆ GetIdle() [1/2]

C4ObjectInfo * C4ObjectInfoList::GetIdle ( C4ID  c_id,
C4DefList rDefs 
)

Definition at line 134 of file C4ObjectInfoList.cpp.

135 {
136  C4Def *pDef;
137  C4ObjectInfo *pInfo;
138  C4ObjectInfo *pHiExp = nullptr;
139 
140  // Search list
141  for (pInfo = First; pInfo; pInfo = pInfo->Next)
142  {
143  // Valid only
144  if ((pDef = rDefs.ID2Def(pInfo->id)))
145  {
146  // Use standard crew or matching id
147  if ( (!c_id && !pDef->NativeCrew) || (pInfo->id == c_id) )
148  {
149  // Participating and not in action
150  if (pInfo->Participation && !pInfo->InAction && !pInfo->HasDied)
151  {
152  // Highest experience
153  if (!pHiExp || (pInfo->Experience > pHiExp->Experience))
154  {
155  // Set this
156  pHiExp = pInfo;
157  }
158  }
159  }
160  }
161  }
162 
163  // Found
164  if (pHiExp)
165  {
166  pHiExp->Recruit();
167  return pHiExp;
168  }
169 
170  return nullptr;
171 }
Definition: C4Def.h:99
int32_t NativeCrew
Definition: C4Def.h:113
C4Def * ID2Def(C4ID id)
int32_t Participation
Definition: C4InfoCore.h:38
int32_t Experience
Definition: C4InfoCore.h:43

References C4ObjectInfoCore::Experience, First, C4ObjectInfo::HasDied, C4ObjectInfoCore::id, C4DefList::ID2Def(), C4ObjectInfo::InAction, C4Def::NativeCrew, C4ObjectInfo::Next, C4ObjectInfoCore::Participation, and C4ObjectInfo::Recruit().

Referenced by C4Player::MakeCrewMember(), and C4Player::PlaceReadyCrew().

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

◆ GetIdle() [2/2]

C4ObjectInfo * C4ObjectInfoList::GetIdle ( const char *  szByName)

Definition at line 244 of file C4ObjectInfoList.cpp.

245 {
246  C4ObjectInfo *pInfo;
247  // Find matching name, participating, alive and not in action
248  for (pInfo = First; pInfo; pInfo = pInfo->Next)
249  {
250  if (SEqualNoCase(pInfo->Name, szByName)
251  && pInfo->Participation
252  && !pInfo->InAction
253  && !pInfo->HasDied)
254  {
255  pInfo->Recruit();
256  return pInfo;
257  }
258  }
259  return nullptr;
260 }
bool SEqualNoCase(const char *szStr1, const char *szStr2, int iLen)
Definition: Standard.cpp:213
char Name[C4MaxName+1]
Definition: C4InfoCore.h:37

References First, C4ObjectInfo::HasDied, C4ObjectInfo::InAction, C4ObjectInfoCore::Name, C4ObjectInfo::Next, C4ObjectInfoCore::Participation, C4ObjectInfo::Recruit(), and SEqualNoCase().

Here is the call graph for this function:

◆ GetLast()

C4ObjectInfo * C4ObjectInfoList::GetLast ( )
protected

Definition at line 271 of file C4ObjectInfoList.cpp.

272 {
273  C4ObjectInfo *cInfo = First;
274  while (cInfo && cInfo->Next)
275  {
276  cInfo = cInfo->Next;
277  }
278  return cInfo;
279 }

References First, and C4ObjectInfo::Next.

Referenced by Save().

Here is the caller graph for this function:

◆ GetPrevious()

C4ObjectInfo * C4ObjectInfoList::GetPrevious ( C4ObjectInfo pInfo)
protected

Definition at line 281 of file C4ObjectInfoList.cpp.

282 {
283  for (C4ObjectInfo *cInfo = First; cInfo; cInfo=cInfo->Next)
284  {
285  if (cInfo->Next == pInfo)
286  {
287  return cInfo;
288  }
289  }
290  return nullptr;
291 }

References First, and C4ObjectInfo::Next.

Referenced by Save().

Here is the caller graph for this function:

◆ IsElement()

bool C4ObjectInfoList::IsElement ( C4ObjectInfo pInfo)

Definition at line 293 of file C4ObjectInfoList.cpp.

294 {
295  for (C4ObjectInfo *cInfo = First; cInfo; cInfo = cInfo->Next)
296  {
297  if (cInfo == pInfo)
298  {
299  return true;
300  }
301  }
302  return false;
303 }

References First, and C4ObjectInfo::Next.

Referenced by C4Player::SetObjectCrewStatus().

Here is the caller graph for this function:

◆ Load()

int32_t C4ObjectInfoList::Load ( C4Group hGroup)

Definition at line 57 of file C4ObjectInfoList.cpp.

58 {
59  C4ObjectInfo *ninf;
60  int32_t infn = 0;
61  char entryname[256 + 1];
62 
63  // Search all oci files
64  hGroup.ResetSearch();
65  while (hGroup.FindNextEntry(C4CFN_ObjectInfoFiles, entryname))
66  {
67  if ((ninf = new C4ObjectInfo))
68  {
69  if (ninf->Load(hGroup,entryname))
70  {
71  Add(ninf);
72  infn++;
73  }
74  else
75  {
76  delete ninf;
77  }
78  }
79  }
80 
81  // Search subfolders
82  hGroup.ResetSearch();
83  while (hGroup.FindNextEntry("*", entryname))
84  {
85  C4Group ItemGroup;
86  if (ItemGroup.OpenAsChild(&hGroup, entryname))
87  {
88  Load(ItemGroup);
89  }
90  }
91 
92  return infn;
93 }
#define C4CFN_ObjectInfoFiles
Definition: C4Components.h:170
bool FindNextEntry(const char *wildcard, StdStrBuf *filename=nullptr, size_t *size=nullptr, bool start_at_filename=false)
Definition: C4Group.cpp:2217
bool OpenAsChild(C4Group *mother, const char *entry_name, bool is_exclusive=false, bool do_create=false)
Definition: C4Group.cpp:1952
void ResetSearch(bool reload_contents=false)
Definition: C4Group.cpp:1316
bool Load(C4Group &hGroup)
bool Add(C4ObjectInfo *pInfo)
int32_t Load(C4Group &hGroup)

References Add(), C4CFN_ObjectInfoFiles, C4Group::FindNextEntry(), C4ObjectInfo::Load(), C4Group::OpenAsChild(), and C4Group::ResetSearch().

Referenced by C4Player::Load(), and C4Player::Strip().

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

◆ MakeValidName()

void C4ObjectInfoList::MakeValidName ( char *  sName)

Definition at line 106 of file C4ObjectInfoList.cpp.

107 {
108  // Append a number to the name if it already exists, starting at 2
109 
110  // 1 space, up to 10 characters for integer max size,
111  // 1 terminating zero, 1 extra space for overflow (negative value) = 13 characters
112  char number_suffix[13];
113  size_t namelen = SLen(sName);
114  for (int32_t identifier = 2; NameExists(sName); identifier++)
115  {
116  sprintf(number_suffix, " %d", identifier);
117  SCopy(number_suffix, sName + std::min(namelen, C4MaxName - SLen(number_suffix)));
118  }
119 }
const unsigned int C4MaxName
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:152
#define sprintf
Definition: Standard.h:162
size_t SLen(const char *sptr)
Definition: Standard.h:74
bool NameExists(const char *szName)

References C4MaxName, NameExists(), SCopy(), SLen(), and sprintf.

Referenced by New().

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

◆ NameExists()

bool C4ObjectInfoList::NameExists ( const char *  szName)

Definition at line 121 of file C4ObjectInfoList.cpp.

122 {
123  C4ObjectInfo *cinf;
124  for (cinf = First; cinf; cinf = cinf->Next)
125  {
126  if (SEqualNoCase(szName, cinf->Name))
127  {
128  return true;
129  }
130  }
131  return false;
132 }

References First, C4ObjectInfoCore::Name, C4ObjectInfo::Next, and SEqualNoCase().

Referenced by MakeValidName().

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

◆ New()

C4ObjectInfo * C4ObjectInfoList::New ( C4ID  n_id,
C4DefList pDefs 
)

Definition at line 173 of file C4ObjectInfoList.cpp.

174 {
175  C4ObjectInfo *pInfo;
176  // Create new info object
177  if (!(pInfo = new C4ObjectInfo))
178  {
179  return nullptr;
180  }
181  // Default type clonk if none specified
182  if (n_id == C4ID::None)
183  {
184  n_id = C4ID::Clonk;
185  }
186  // Check type valid and def available
187  C4Def *pDef = nullptr;
188  if (pDefs && !(pDef = pDefs->ID2Def(n_id)))
189  {
190  delete pInfo;
191  return nullptr;
192  }
193  // Set name source
194  const char *cpNames = Game.Names.GetData();
195  if (pDef->pClonkNames)
196  {
197  cpNames = pDef->pClonkNames->GetData();
198  }
199  // Default by type
200  ((C4ObjectInfoCore*) pInfo)->Default(n_id, pDefs, cpNames);
201  // Set birthday
202  pInfo->Birthday = time(nullptr); // TODO: Use the function from C4ObjectInfo here, instead of assigning it manually
203  // Make valid names
204  MakeValidName(pInfo->Name);
205  // Add
206  Add(pInfo);
207  ++iNumCreated;
208  return pInfo;
209 }
C4Game Game
Definition: C4Globals.cpp:52
const char * GetData() const
C4ComponentHost * pClonkNames
Definition: C4Def.h:185
C4ComponentHost Names
Definition: C4Game.h:78
static const C4ID Clonk
Definition: C4Id.h:40
static const C4ID None
Definition: C4Id.h:39
int32_t Birthday
Definition: C4InfoCore.h:46
void MakeValidName(char *sName)

References Add(), C4ObjectInfoCore::Birthday, C4ID::Clonk, Game, C4ComponentHost::GetData(), C4DefList::ID2Def(), iNumCreated, MakeValidName(), C4ObjectInfoCore::Name, C4Game::Names, C4ID::None, and C4Def::pClonkNames.

Referenced by C4Player::MakeCrewMember(), and C4Player::PlaceReadyCrew().

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

◆ Save()

bool C4ObjectInfoList::Save ( C4Group hGroup,
bool  fSavegame,
bool  fStoreTiny,
C4DefList pDefs 
)

Definition at line 220 of file C4ObjectInfoList.cpp.

221 {
222  // Save in opposite order (for identical crew order on load)
223  C4ObjectInfo *pInfo;
224  for (pInfo = GetLast(); pInfo; pInfo = GetPrevious(pInfo))
225  {
226  // Don't save TemporaryCrew in regular player files
227  if (!fSavegame)
228  {
229  C4Def *pDef = C4Id2Def(pInfo->id);
230  if (pDef && pDef->TemporaryCrew)
231  {
232  continue;
233  }
234  }
235  // save
236  if (!pInfo->Save(hGroup, fStoreTiny, pDefs))
237  {
238  return false;
239  }
240  }
241  return true;
242 }
C4Def * C4Id2Def(C4ID id)
Definition: C4DefList.h:84
int32_t TemporaryCrew
Definition: C4Def.h:140
bool Save(C4Group &hGroup, bool fStoreTiny, C4DefList *pDefs)
C4ObjectInfo * GetLast()
C4ObjectInfo * GetPrevious(C4ObjectInfo *pInfo)

References C4Id2Def(), GetLast(), GetPrevious(), C4ObjectInfoCore::id, C4ObjectInfo::Save(), and C4Def::TemporaryCrew.

Referenced by C4Player::Save(), and C4Player::Strip().

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

◆ Strip()

void C4ObjectInfoList::Strip ( C4DefList rDefs)

Definition at line 305 of file C4ObjectInfoList.cpp.

306 {
307  C4ObjectInfo *pInfo;
308  C4ObjectInfo *pPrev;
309  // Search list
310  for (pInfo = First, pPrev = nullptr; pInfo; )
311  {
312  // Invalid?
313  if (!rDefs.ID2Def(pInfo->id))
314  {
315  C4ObjectInfo *pDelete = pInfo;
316  if (pPrev)
317  {
318  pPrev->Next = pInfo->Next;
319  }
320  else
321  {
322  First = pInfo->Next;
323  }
324  pInfo = pInfo->Next;
325  delete pDelete;
326  }
327  else
328  {
329  pPrev = pInfo;
330  pInfo = pInfo->Next;
331  }
332  }
333 }

References First, C4ObjectInfoCore::id, C4DefList::ID2Def(), and C4ObjectInfo::Next.

Referenced by C4Player::Save(), and C4Player::Strip().

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

Member Data Documentation

◆ First

C4ObjectInfo* C4ObjectInfoList::First
protected

◆ iNumCreated

int32_t C4ObjectInfoList::iNumCreated

Definition at line 33 of file C4ObjectInfoList.h.

Referenced by C4Player::CompileFunc(), Default(), and New().


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