OpenClonk
C4ObjectInfo.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 1998-2000, Matthes Bender
5  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
6  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 /* Holds crew member information */
19 
20 #include "C4Include.h"
21 #include "object/C4ObjectInfo.h"
22 
23 #include "c4group/C4Components.h"
24 #include "game/C4Application.h"
26 #include "lib/C4Random.h"
27 #include "object/C4Def.h"
28 #include "object/C4DefList.h"
29 #include "player/C4Player.h"
30 #include "player/C4PlayerList.h"
31 #include "player/C4RankSystem.h"
32 
33 #ifdef _MSC_VER
34 #define snprintf _snprintf
35 #endif
36 
38 {
39  Default();
40 }
41 
43 {
44  Clear();
45 }
46 
48 {
49  WasInAction = false;
50  InAction = false;
51  InActionTime = 0;
52  HasDied = false;
53  ControlCount = 0;
54  Filename[0] = 0;
55  Next = nullptr;
56  pDef = nullptr;
57 }
58 
59 bool C4ObjectInfo::Load(C4Group &hMother, const char *szEntryname)
60 {
61  // New version
62  if (SEqualNoCase(GetExtension(szEntryname), "oci")) // TODO: Define file extensions in a static constant
63  {
64  C4Group hChild;
65  if (hChild.OpenAsChild(&hMother,szEntryname))
66  {
67  if (!C4ObjectInfo::Load(hChild))
68  {
69  hChild.Close();
70  return false;
71  }
72  // resolve definition, if possible
73  // only works in game, but is not needed in frontend or startup editing anyway
74  pDef = C4Id2Def(id);
75  hChild.Close();
76  return true;
77  }
78  }
79 
80  return false;
81 }
82 
84 {
85  // Store group file name
87  // Load core
88  return C4ObjectInfoCore::Load(hGroup);
89 }
90 
91 bool C4ObjectInfo::Save(C4Group &hGroup, bool fStoreTiny, C4DefList *pDefs)
92 {
93  // Set group file name; rename if necessary
94  char szTempGroup[_MAX_PATH_LEN];
95  SCopy(Name, szTempGroup, _MAX_PATH);
96  MakeFilenameFromTitle(szTempGroup);
97  SAppend(".oci", szTempGroup, _MAX_PATH); // TODO: File extension again, this time with a dot
98  if (!SEqualNoCase(Filename, szTempGroup))
99  {
100  if (!Filename[0])
101  {
102  // First time creation of file - make sure it's not a duplicate
103  SCopy(szTempGroup, Filename, _MAX_PATH);
104  while (hGroup.FindEntry(Filename))
105  {
106  // If a crew info of that name exists already, rename!
108  int32_t iFinNum = GetTrailingNumber(Filename);
109  int32_t iLen = SLen(Filename);
110  while (iLen && Inside(Filename[iLen-1], '0', '9'))
111  {
112  --iLen;
113  }
114  if (iLen > _MAX_PATH-22)
115  {
116  LogF("Error generating unique filename for %s(%s): Path overflow", Name, hGroup.GetFullName().getData());
117  break;
118  }
119  snprintf(Filename+iLen, 22, "%d", iFinNum+1);
120  EnforceExtension(Filename, "oci"); // TODO: File extension again
121  }
122  }
123  else
124  {
125  // Crew was renamed; file rename necessary, if the name is not blocked by another crew info
126  if (!hGroup.FindEntry(szTempGroup))
127  {
128  if (hGroup.Rename(Filename, szTempGroup))
129  {
130  SCopy(szTempGroup, Filename, _MAX_PATH);
131  }
132  else
133  {
134  // Could not rename. Not fatal; just use old file
135  LogF("Error adjusting crew info for %s into %s: Rename error from %s to %s!", Name, hGroup.GetFullName().getData(), Filename, szTempGroup);
136  }
137  }
138  }
139  }
140  // Open group
141  C4Group hTemp;
142  if (!hTemp.OpenAsChild(&hGroup, Filename, false, true))
143  {
144  return false;
145  }
146  // Custom rank image present?
147  if (pDefs && !fStoreTiny)
148  {
149  C4Def *pDef = pDefs->ID2Def(id);
150  if (pDef)
151  {
152  if (pDef->pRankSymbols)
153  {
154  C4FacetSurface fctRankSymbol;
156  {
157  fctRankSymbol.GetFace().SavePNG(hTemp, C4CFN_ClonkRank);
158  }
159  }
160  else
161  {
162  // definition does not have custom rank symbols: Remove any rank image from Clonk
163  hTemp.Delete(C4CFN_ClonkRank);
164  }
165  }
166  }
167 
168  // Save info to temp group
169  if (!C4ObjectInfoCore::Save(hTemp, pDefs))
170  {
171  hTemp.Close();
172  return false;
173  }
174  // Close temp group
175  hTemp.Close();
176  // Success
177  return true;
178 }
179 
181 {
182  Retire();
183  if (WasInAction)
184  {
185  Rounds++;
186  }
187 }
188 
190 {
191  pDef = nullptr;
192 }
193 
195 {
196  // Already recruited?
197  if (InAction)
198  {
199  return;
200  }
201  WasInAction = true;
202  InAction = true;
204  // Rank name overload by def?
206  if (pDef && pDef->pRankNames)
207  {
208  StdStrBuf sRank(pDef->pRankNames->GetRankName(Rank, true));
209  if (sRank)
210  {
211  sRankName.Copy(sRank);
212  }
213  }
214 }
215 
217 {
218  // Not recruited?
219  if (!InAction)
220  {
221  return;
222  }
223  // retire
224  InAction = false;
226 }
227 
229 {
230  Birthday = time(nullptr);
231 }
#define C4CFN_ClonkRank
Definition: C4Components.h:121
C4Def * C4Id2Def(C4ID id)
Definition: C4DefList.h:84
C4Game Game
Definition: C4Globals.cpp:52
C4DefList Definitions
Definition: C4Globals.cpp:49
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:262
#define _MAX_FNAME
#define _MAX_PATH
#define _MAX_PATH_LEN
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:152
bool SEqualNoCase(const char *szStr1, const char *szStr2, int iLen)
Definition: Standard.cpp:213
void SAppend(const char *szSource, char *szTarget, int iMaxL)
Definition: Standard.cpp:263
bool Inside(T ival, U lbound, V rbound)
Definition: Standard.h:43
size_t SLen(const char *sptr)
Definition: Standard.h:74
char * GetExtension(char *szFilename)
Definition: StdFile.cpp:118
void MakeFilenameFromTitle(char *szTitle)
Definition: StdFile.cpp:426
char * GetFilename(char *szPath)
Definition: StdFile.cpp:42
void EnforceExtension(char *szFilename, const char *szExtension)
Definition: StdFile.cpp:286
void RemoveExtension(char *szFilename)
Definition: StdFile.cpp:303
int GetTrailingNumber(const char *strString)
Definition: StdFile.cpp:84
Definition: C4Def.h:99
int32_t iNumRankSymbols
Definition: C4Def.h:190
C4FacetSurface * pRankSymbols
Definition: C4Def.h:189
C4RankSystem * pRankNames
Definition: C4Def.h:188
C4Def * ID2Def(C4ID id)
C4Surface & GetFace()
Definition: C4FacetEx.h:52
int32_t Time
Definition: C4Game.h:132
bool Rename(const char *filename, const char *new_name)
Definition: C4Group.cpp:1735
const char * GetName() const
Definition: C4Group.cpp:2309
StdStrBuf GetFullName() const
Definition: C4Group.cpp:2638
bool OpenAsChild(C4Group *mother, const char *entry_name, bool is_exclusive=false, bool do_create=false)
Definition: C4Group.cpp:1952
bool Close()
Definition: C4Group.cpp:971
bool Delete(const char *files, bool recursive=false)
Definition: C4Group.cpp:1645
bool FindEntry(const char *wildcard, StdStrBuf *filename=nullptr, size_t *size=nullptr)
Definition: C4Group.cpp:2211
bool Load(C4Group &hGroup)
Definition: C4InfoCore.cpp:336
int32_t TotalPlayingTime
Definition: C4InfoCore.h:46
char Name[C4MaxName+1]
Definition: C4InfoCore.h:37
int32_t Birthday
Definition: C4InfoCore.h:46
int32_t Rounds
Definition: C4InfoCore.h:43
bool Save(C4Group &hGroup, class C4DefList *pDefs)
Definition: C4InfoCore.cpp:343
StdStrBuf sRankName
Definition: C4InfoCore.h:40
C4ObjectInfo * Next
Definition: C4ObjectInfo.h:41
void SetBirthday()
bool Load(C4Group &hGroup)
bool Save(C4Group &hGroup, bool fStoreTiny, C4DefList *pDefs)
char Filename[_MAX_PATH_LEN]
Definition: C4ObjectInfo.h:40
class C4Def * pDef
Definition: C4ObjectInfo.h:39
int32_t ControlCount
Definition: C4ObjectInfo.h:38
int32_t InActionTime
Definition: C4ObjectInfo.h:36
bool WasInAction
Definition: C4ObjectInfo.h:34
static bool DrawRankSymbol(C4FacetSurface *fctSymbol, int32_t iRank, C4Facet *pfctRankSymbols, int32_t iRankSymbolCount, bool fOwnSurface, int32_t iXOff=0, C4Facet *cgoDrawDirect=nullptr)
StdStrBuf GetRankName(int iRank, bool fReturnLastIfOver)
bool SavePNG(C4Group &hGroup, const char *szFilename, bool fSaveAlpha=true, bool fSaveOverlayOnly=false)
const char * getData() const
Definition: StdBuf.h:442
void Copy()
Definition: StdBuf.h:467