OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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 "object/C4Def.h"
24 #include "object/C4DefList.h"
25 #include "lib/C4Random.h"
26 #include "c4group/C4Components.h"
27 #include "game/C4Game.h"
28 #include "config/C4Config.h"
29 #include "game/C4Application.h"
30 #include "player/C4RankSystem.h"
31 #include "lib/C4Log.h"
32 #include "player/C4Player.h"
34 #include "player/C4PlayerList.h"
35 
36 #ifdef _MSC_VER
37 #define snprintf _snprintf
38 #endif
39 
41 {
42  Default();
43 }
44 
46 {
47  Clear();
48 }
49 
51 {
52  WasInAction=false;
53  InAction=false;
54  InActionTime=0;
55  HasDied=false;
56  ControlCount=0;
57  Filename[0]=0;
58  Next=nullptr;
59  pDef = nullptr;
60 }
61 
62 bool C4ObjectInfo::Load(C4Group &hMother, const char *szEntryname)
63 {
64 
65  // New version
66  if (SEqualNoCase(GetExtension(szEntryname),"oci"))
67  {
68  C4Group hChild;
69  if (hChild.OpenAsChild(&hMother,szEntryname))
70  {
71  if (!C4ObjectInfo::Load(hChild))
72  { hChild.Close(); return false; }
73  // resolve definition, if possible
74  // only works in game, but is not needed in frontend or startup editing anyway
75  pDef = C4Id2Def(id);
76  hChild.Close();
77  return true;
78  }
79  }
80 
81  return false;
82 }
83 
85 {
86  // Store group file name
88  // Load core
89  if (!C4ObjectInfoCore::Load(hGroup)) return false;
90  return true;
91 }
92 
93 bool C4ObjectInfo::Save(C4Group &hGroup, bool fStoreTiny, C4DefList *pDefs)
94 {
95  // Set group file name; rename if necessary
96  char szTempGroup[_MAX_PATH+1];
97  SCopy(Name, szTempGroup, _MAX_PATH);
98  MakeFilenameFromTitle(szTempGroup);
99  SAppend(".oci",szTempGroup, _MAX_PATH);
100  if (!SEqualNoCase(Filename, szTempGroup))
101  {
102  if (!Filename[0])
103  {
104  // first time creation of file - make sure it's not a duplicate
105  SCopy(szTempGroup, Filename, _MAX_PATH);
106  while (hGroup.FindEntry(Filename))
107  {
108  // if a crew info of that name exists already, rename!
110  int32_t iFinNum = GetTrailingNumber(Filename), iLen = SLen(Filename);
111  while (iLen && Inside(Filename[iLen-1], '0', '9')) --iLen;
112  if (iLen>_MAX_PATH-22) { LogF("Error generating unique filename for %s(%s): Path overflow", Name, hGroup.GetFullName().getData()); break; }
113  snprintf(Filename+iLen, 22, "%d", iFinNum+1);
114  EnforceExtension(Filename, "oci");
115  }
116  }
117  else
118  {
119  // Crew was renamed; file rename necessary, if the name is not blocked by another crew info
120  if (!hGroup.FindEntry(szTempGroup))
121  {
122  if (hGroup.Rename(Filename, szTempGroup))
123  SCopy(szTempGroup, Filename, _MAX_PATH);
124  else
125  {
126  // could not rename. Not fatal; just use old file
127  LogF("Error adjusting crew info for %s into %s: Rename error from %s to %s!", Name, hGroup.GetFullName().getData(), Filename, szTempGroup);
128  }
129  }
130  }
131  }
132  // Open group
133  C4Group hTemp;
134  if (!hTemp.OpenAsChild(&hGroup, Filename, false, true))
135  return false;
136  // custom rank image present?
137  if (pDefs && !fStoreTiny)
138  {
139  C4Def *pDef = pDefs->ID2Def(id);
140  if (pDef)
141  {
142  if (pDef->pRankSymbols)
143  {
144  C4FacetSurface fctRankSymbol;
145  if (C4RankSystem::DrawRankSymbol(&fctRankSymbol, Rank, pDef->pRankSymbols, pDef->iNumRankSymbols, true))
146  {
147  fctRankSymbol.GetFace().SavePNG(hTemp, C4CFN_ClonkRank);
148  }
149  }
150  else
151  {
152  // definition does not have custom rank symbols: Remove any rank image from Clonk
153  hTemp.Delete(C4CFN_ClonkRank);
154  }
155  }
156  }
157 
158  // Save info to temp group
159  if (!C4ObjectInfoCore::Save(hTemp, pDefs))
160  { hTemp.Close(); return false; }
161  // Close temp group
162  hTemp.Close();
163  // Success
164  return true;
165 }
166 
168 {
169  Retire();
170  if (WasInAction) Rounds++;
171 }
172 
174 {
175  pDef=nullptr;
176 }
177 
179 {
180  // already recruited?
181  if (InAction) return;
182  WasInAction=true;
183  InAction=true;
185  // rank name overload by def?
187  if (pDef) if (pDef->pRankNames)
188  {
189  StdStrBuf sRank(pDef->pRankNames->GetRankName(Rank, true));
190  if (sRank) sRankName.Copy(sRank);
191  }
192 }
193 
195 {
196  // not recruited?
197  if (!InAction) return;
198  // retire
199  InAction=false;
201 }
202 
204 {
205  Birthday=time(nullptr);
206 }
char * GetFilename(char *szPath)
Definition: StdFile.cpp:55
const char * getData() const
Definition: StdBuf.h:450
C4Def * ID2Def(C4ID id)
bool FindEntry(const char *szWildCard, StdStrBuf *sFileName=nullptr, size_t *iSize=nullptr)
Definition: C4Group.cpp:1774
int32_t Rounds
Definition: C4InfoCore.h:44
int32_t iNumRankSymbols
Definition: C4Def.h:193
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:122
#define C4CFN_ClonkRank
Definition: C4Components.h:121
void SAppend(const char *szSource, char *szTarget, int iMaxL)
Definition: Standard.cpp:227
C4Game Game
Definition: C4Globals.cpp:52
bool Save(C4Group &hGroup, class C4DefList *pDefs)
Definition: C4InfoCore.cpp:300
bool SavePNG(C4Group &hGroup, const char *szFilename, bool fSaveAlpha=true, bool fSaveOverlayOnly=false)
int32_t ControlCount
Definition: C4ObjectInfo.h:38
char Filename[_MAX_PATH+1]
Definition: C4ObjectInfo.h:40
bool SEqualNoCase(const char *szStr1, const char *szStr2, int iLen)
Definition: Standard.cpp:177
StdStrBuf sRankName
Definition: C4InfoCore.h:41
C4ObjectInfo * Next
Definition: C4ObjectInfo.h:41
bool Rename(const char *szFile, const char *szNewName)
Definition: C4Group.cpp:1424
bool Delete(const char *szFiles, bool fRecursive=false)
Definition: C4Group.cpp:1334
#define _MAX_PATH
size_t SLen(const char *sptr)
Definition: Standard.h:78
StdStrBuf GetRankName(int iRank, bool fReturnLastIfOver)
int32_t TotalPlayingTime
Definition: C4InfoCore.h:47
void MakeFilenameFromTitle(char *szTitle)
Definition: StdFile.cpp:412
int GetTrailingNumber(const char *strString)
Definition: StdFile.cpp:97
const char * GetName() const
Definition: C4Group.cpp:1845
StdStrBuf GetFullName() const
Definition: C4Group.cpp:2078
static bool DrawRankSymbol(C4FacetSurface *fctSymbol, int32_t iRank, C4Facet *pfctRankSymbols, int32_t iRankSymbolCount, bool fOwnSurface, int32_t iXOff=0, C4Facet *cgoDrawDirect=nullptr)
void SetBirthday()
int32_t Time
Definition: C4Game.h:133
Definition: C4Def.h:100
int32_t Birthday
Definition: C4InfoCore.h:47
bool Close()
Definition: C4Group.cpp:755
char Name[C4MaxName+1]
Definition: C4InfoCore.h:38
class C4Def * pDef
Definition: C4ObjectInfo.h:39
#define _MAX_FNAME
char * GetExtension(char *szFilename)
Definition: StdFile.cpp:131
void RemoveExtension(char *szFilename)
Definition: StdFile.cpp:316
C4FacetSurface * pRankSymbols
Definition: C4Def.h:192
C4Def * C4Id2Def(C4ID id)
Definition: C4DefList.h:80
void EnforceExtension(char *szFilename, const char *szExtension)
Definition: StdFile.cpp:299
bool Load(C4Group &hGroup)
Definition: C4InfoCore.cpp:293
bool WasInAction
Definition: C4ObjectInfo.h:34
C4DefList Definitions
Definition: C4Globals.cpp:49
C4RankSystem * pRankNames
Definition: C4Def.h:191
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:253
void Copy()
Definition: StdBuf.h:475
bool Load(C4Group &hGroup)
bool Inside(T ival, U lbound, V rbound)
Definition: Standard.h:45
C4Surface & GetFace()
Definition: C4FacetEx.h:52
bool OpenAsChild(C4Group *pMother, const char *szEntryName, bool fExclusive=false, bool fCreate=false)
Definition: C4Group.cpp:1585
bool Save(C4Group &hGroup, bool fStoreTiny, C4DefList *pDefs)
int32_t InActionTime
Definition: C4ObjectInfo.h:36