OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4RankSystem Class Reference

#include <C4RankSystem.h>

Public Types

enum  { EXP_NoPromotion = -1 }
 

Public Member Functions

 C4RankSystem ()
 
 ~C4RankSystem ()
 
void Default ()
 
void Clear ()
 
int Init (const char *szRegister, const char *szDefRanks, int iRankBase)
 
bool Load (C4Group &hGroup, const char *szFilenames, int DefRankBase, const char *szLanguage)
 
int Experience (int iRank)
 
int RankByExperience (int iExp)
 
StdStrBuf GetRankName (int iRank, bool fReturnLastIfOver)
 
bool Check (int iRank, const char *szDefRankName)
 
int32_t GetExtendedRankNum () const
 
int32_t GetBaseRankNum () const
 

Static Public Member Functions

static bool DrawRankSymbol (C4FacetSurface *fctSymbol, int32_t iRank, C4Facet *pfctRankSymbols, int32_t iRankSymbolCount, bool fOwnSurface, int32_t iXOff=0, C4Facet *cgoDrawDirect=nullptr)
 

Protected Attributes

char Register [256+1]
 
char RankName [C4MaxName+1]
 
int RankBase
 
char ** pszRankNames {nullptr}
 
char * szRankNames {nullptr}
 
int iRankNum
 
char ** pszRankExtensions {nullptr}
 
int iRankExtNum
 

Detailed Description

Definition at line 25 of file C4RankSystem.h.

Member Enumeration Documentation

anonymous enum
Enumerator
EXP_NoPromotion 

Definition at line 31 of file C4RankSystem.h.

31 { EXP_NoPromotion = -1 }; // experience value for NextRankExp: No more promotion possible

Constructor & Destructor Documentation

C4RankSystem::C4RankSystem ( )

Definition at line 30 of file C4RankSystem.cpp.

References Default().

31 {
32  Default();
33 }

Here is the call graph for this function:

C4RankSystem::~C4RankSystem ( )
inline

Definition at line 29 of file C4RankSystem.h.

References Clear().

29 { Clear(); }

Here is the call graph for this function:

Member Function Documentation

bool C4RankSystem::Check ( int  iRank,
const char *  szDefRankName 
)

Definition at line 245 of file C4RankSystem.cpp.

References C4MaxName, Register, SLen(), and sprintf.

246 {
247 #ifdef _WIN32
248  char keyname[30];
249  sprintf(keyname,"Rank%03d",iRank);
250  if (!GetRegistryString(Register,keyname).isNull())
251  return false;
252  if (!szDefRankName || (SLen(szDefRankName)>C4MaxName))
253  return false;
254  return SetRegistryString(Register,keyname,szDefRankName);
255 #else
256  return true;
257 #endif
258 }
#define sprintf
Definition: Standard.h:164
size_t SLen(const char *sptr)
Definition: Standard.h:74
char Register[256+1]
Definition: C4RankSystem.h:34
const unsigned int C4MaxName

Here is the call graph for this function:

void C4RankSystem::Clear ( )

Definition at line 260 of file C4RankSystem.cpp.

References iRankExtNum, iRankNum, pszRankExtensions, pszRankNames, and szRankNames.

Referenced by Default(), Init(), Load(), and ~C4RankSystem().

261 {
262  // clear any loaded rank names
263  if (pszRankNames) { delete [] pszRankNames; pszRankNames=nullptr; }
264  if (pszRankExtensions) { delete [] pszRankExtensions; pszRankExtensions = nullptr; }
265  if (szRankNames) { delete [] szRankNames; szRankNames=nullptr; }
266  // reset number of ranks
267  iRankNum=0;
268  iRankExtNum=0;
269 }
char ** pszRankExtensions
Definition: C4RankSystem.h:40
char * szRankNames
Definition: C4RankSystem.h:38
char ** pszRankNames
Definition: C4RankSystem.h:37

Here is the caller graph for this function:

void C4RankSystem::Default ( )

Definition at line 271 of file C4RankSystem.cpp.

References Clear(), iRankExtNum, RankBase, RankName, and Register.

Referenced by C4RankSystem(), and C4Game::Default().

272 {
273  Clear();
274  Register[0]=0;
275  RankName[0]=0;
276  RankBase=1000;
277  iRankExtNum=0;
278 }
char RankName[C4MaxName+1]
Definition: C4RankSystem.h:35
char Register[256+1]
Definition: C4RankSystem.h:34

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4RankSystem::DrawRankSymbol ( C4FacetSurface fctSymbol,
int32_t  iRank,
C4Facet pfctRankSymbols,
int32_t  iRankSymbolCount,
bool  fOwnSurface,
int32_t  iXOff = 0,
C4Facet cgoDrawDirect = nullptr 
)
static

Definition at line 280 of file C4RankSystem.cpp.

References C4FacetSurface::Create(), C4Facet::Draw(), C4Facet::DrawX(), C4GraphicsResource::fctCaptain, C4Facet::GetPhase(), C4Facet::GetPhaseNum(), GraphicsResource, iSize, C4FacetSurface::Set(), C4Facet::Surface, C4Facet::Wdt, C4Facet::X, and C4Facet::Y.

Referenced by C4GraphicsOverlay::DrawRankSymbol(), and C4ObjectInfo::Save().

281 {
282  // safety
283  if (iRank<0) iRank = 0;
284  // symbol by rank
285  int32_t iMaxRankSym,Q;
286  if (pfctRankSymbols->GetPhaseNum(iMaxRankSym, Q))
287  {
288  if (!iMaxRankSym) iMaxRankSym=1;
289  int32_t iBaseRank=iRank%iRankSymbolCount;
290  if (iRank/iRankSymbolCount)
291  {
292  // extended rank: draw
293  // extension star defaults to captain star; but use extended symbols if they are in the gfx
294  C4Facet fctExtended = ::GraphicsResource.fctCaptain;
295  if (iMaxRankSym > iRankSymbolCount)
296  {
297  int32_t iExtended = iRank / iRankSymbolCount - 1 + iRankSymbolCount;
298  if (iExtended >= iMaxRankSym)
299  {
300  // max rank exceeded
301  iExtended = iMaxRankSym - 1;
302  iBaseRank = iRankSymbolCount - 1;
303  }
304  fctExtended = pfctRankSymbols->GetPhase(iExtended);
305  }
306  int32_t iSize = pfctRankSymbols->Wdt;
307  if (!cgoDrawDirect)
308  {
309  fctSymbol->Create(iSize,iSize);
310  pfctRankSymbols->DrawX(fctSymbol->Surface, 0,0,iSize,iSize,iBaseRank);
311  fctExtended.DrawX(fctSymbol->Surface, 0,0,iSize*2/3,iSize*2/3);
312  }
313  else
314  {
315  pfctRankSymbols->Draw(cgoDrawDirect->Surface,cgoDrawDirect->X+iXOff,cgoDrawDirect->Y,iBaseRank);
316  fctExtended.Draw(cgoDrawDirect->Surface, cgoDrawDirect->X+iXOff-4,cgoDrawDirect->Y-3);
317  }
318  }
319  else
320  {
321  // regular rank: copy facet
322  if (cgoDrawDirect)
323  {
324  pfctRankSymbols->Draw(cgoDrawDirect->Surface, cgoDrawDirect->X+iXOff,cgoDrawDirect->Y,iBaseRank);
325  }
326  else if (fOwnSurface)
327  {
328  int32_t iSize = pfctRankSymbols->Wdt;
329  fctSymbol->Create(iSize,iSize);
330  pfctRankSymbols->DrawX(fctSymbol->Surface, 0,0,iSize,iSize,iBaseRank);
331  }
332  else
333  {
334  fctSymbol->Set(pfctRankSymbols->GetPhase(iBaseRank));
335  }
336  }
337  return true;
338  }
339  return false;
340 }
float Y
Definition: C4Facet.h:118
bool GetPhaseNum(int32_t &rX, int32_t &rY)
Definition: C4Facet.cpp:472
C4GraphicsResource GraphicsResource
void Set(const C4Facet &cpy)
Definition: C4FacetEx.h:46
void DrawX(C4Surface *sfcTarget, float iX, float iY, float iWdt, float iHgt, int32_t iPhaseX=0, int32_t iPhaseY=0) const
Definition: C4Facet.cpp:358
void Draw(C4Facet &cgo, bool fAspect=true, int32_t iPhaseX=0, int32_t iPhaseY=0, bool fTransparent=true)
Definition: C4Facet.cpp:154
bool Create(int iWdt, int iHgt, int iWdt2=C4FCT_Full, int iHgt2=C4FCT_Full)
Definition: C4FacetEx.cpp:54
C4Surface * Surface
Definition: C4Facet.h:117
float Wdt
Definition: C4Facet.h:118
C4Facet GetPhase(int iPhaseX=0, int iPhaseY=0)
Definition: C4Facet.cpp:59
float X
Definition: C4Facet.h:118
int iSize
Definition: TstC4NetIO.cpp:32

Here is the call graph for this function:

Here is the caller graph for this function:

int C4RankSystem::Experience ( int  iRank)

Definition at line 232 of file C4RankSystem.cpp.

References RankBase.

Referenced by C4PlayerInfoCore::CheckPromotion(), C4Object::DoExperience(), C4ObjectInfoCore::GetNextRankInfo(), RankByExperience(), and C4ObjectInfoCore::UpdateCustomRanks().

233 {
234  if (iRank<0) return 0;
235  return (int) ( pow (double(iRank), 1.5) * RankBase );
236 }

Here is the caller graph for this function:

int32_t C4RankSystem::GetBaseRankNum ( ) const
inline

Definition at line 52 of file C4RankSystem.h.

References iRankNum.

Referenced by C4GraphicsOverlay::DrawRankSymbol().

52 { return iRankNum; }

Here is the caller graph for this function:

int32_t C4RankSystem::GetExtendedRankNum ( ) const
inline

Definition at line 51 of file C4RankSystem.h.

References iRankExtNum.

51 { return iRankExtNum; }
StdStrBuf C4RankSystem::GetRankName ( int  iRank,
bool  fReturnLastIfOver 
)

Definition at line 186 of file C4RankSystem.cpp.

References StdStrBuf::Format(), iRankExtNum, iRankNum, StdStrBuf::isNull(), pszRankExtensions, pszRankNames, StdStrBuf::Ref(), Register, and sprintf.

Referenced by C4PlayerInfoCore::Default(), C4ObjectInfoCore::GetNextRankInfo(), C4ObjectInfoCore::Promote(), C4PlayerInfoCore::Promote(), C4Object::Promote(), C4ObjectInfo::Recruit(), and C4ObjectInfoCore::UpdateCustomRanks().

187 {
188  if (iRank<0) return StdStrBuf();
189  // if a new-style ranklist is loaded, seek there
190  if (pszRankNames)
191  {
192  if (iRankNum<=0) return StdStrBuf();
193  // overflow check
194  if (iRank>=iRankNum*(iRankExtNum+1))
195  {
196  // rank undefined: Fallback to last rank
197  if (!fReturnLastIfOver) return StdStrBuf();
198  iRank = iRankNum*(iRankExtNum+1)-1;
199  }
200  StdStrBuf sResult;
201  if (iRank >= iRankNum)
202  {
203  // extended rank composed of two parts
204  int iExtension = iRank / iRankNum - 1;
205  iRank = iRank % iRankNum;
206  sResult.Format(pszRankExtensions[iExtension], pszRankNames[iRank]);
207  }
208  else
209  {
210  // simple rank
211  sResult.Ref(pszRankNames[iRank]);
212  }
213  return sResult;
214  }
215 #ifdef _WIN32
216  // old-style registry fallback
217  while (iRank>=0)
218  {
219  char keyname[30];
220  StdCopyStrBuf rankname;
221  sprintf(keyname,"Rank%03d",iRank+1);
222  rankname = GetRegistryString(Register,keyname);
223  if (!rankname.isNull())
224  return rankname;
225  if (!fReturnLastIfOver) return StdStrBuf();
226  --iRank;
227  }
228 #endif
229  return StdStrBuf();
230 }
#define sprintf
Definition: Standard.h:164
void Format(const char *szFmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: StdBuf.cpp:174
char ** pszRankExtensions
Definition: C4RankSystem.h:40
char Register[256+1]
Definition: C4RankSystem.h:34
void Ref(const char *pnData)
Definition: StdBuf.h:455
bool isNull() const
Definition: StdBuf.h:441
char ** pszRankNames
Definition: C4RankSystem.h:37

Here is the call graph for this function:

Here is the caller graph for this function:

int C4RankSystem::Init ( const char *  szRegister,
const char *  szDefRanks,
int  iRankBase 
)

Definition at line 35 of file C4RankSystem.cpp.

References StdStrBuf::AppendChars(), C4MaxName, Clear(), StdStrBuf::getData(), StdStrBuf::getMData(), iRankNum, StdStrBuf::isNull(), pszRankNames, RankBase, Register, SCopy(), SCopySegment(), sprintf, and szRankNames.

Referenced by C4Game::PreInit().

38 {
39 
40  // Init
41  SCopy(szRegister,Register,256);
42  RankBase=iRankBase;
43 
44  // Check registry for present rank names and set defaults
45 #ifdef _WIN32
46  int crank=0;
47  char keyname[30];
48  StdCopyStrBuf rankname;
49  bool Checking=true;
50  while (Checking)
51  {
52  sprintf(keyname,"Rank%03d",crank+1);
53  rankname = GetRegistryString(Register,keyname);
54  if (!rankname.isNull())
55  {
56  // Rank present
57  crank++;
58  }
59  else
60  {
61  // Rank not defined, check for default
62  rankname.AppendChars('\0', C4MaxName);
63  if (SCopySegment(szDefRanks,crank,rankname.getMData(),'|',C4MaxName)
64  && SetRegistryString(Register,keyname,rankname.getData()))
65  crank++;
66  else
67  Checking=false;
68  }
69  }
70  return crank;
71 #else
72  // clear any loaded rank names
73  Clear();
74  if (!szDefRanks) return 0;
75  // make a copy
76  szRankNames = new char[strlen(szDefRanks) + 1];
77  strcpy (szRankNames, szDefRanks);
78  // split into substrings by replacing the | with zeros
79  for (char * p = szRankNames; *p; ++p) if (*p == '|')
80  {
81  *p = 0;
82  ++iRankNum;
83  }
84  ++ iRankNum; // The last rank is already terminated by zero
85  // build a list of substrings
86  pszRankNames = new char *[iRankNum];
87  char * p = szRankNames;
88  for (int i = 0; i < iRankNum; ++i)
89  {
90  pszRankNames[i] = p;
91  p += strlen(p) + 1;
92  }
93  return iRankNum;
94 #endif
95 }
const char * getData() const
Definition: StdBuf.h:442
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:152
#define sprintf
Definition: Standard.h:164
bool SCopySegment(const char *szString, int iSegment, char *sTarget, char cSeparator, int iMaxL, bool fSkipWhitespace)
Definition: Standard.cpp:273
void AppendChars(char cChar, size_t iCnt)
Definition: StdBuf.h:582
char Register[256+1]
Definition: C4RankSystem.h:34
char * getMData()
Definition: StdBuf.h:443
char * szRankNames
Definition: C4RankSystem.h:38
const unsigned int C4MaxName
bool isNull() const
Definition: StdBuf.h:441
char ** pszRankNames
Definition: C4RankSystem.h:37

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4RankSystem::Load ( C4Group hGroup,
const char *  szFilenames,
int  DefRankBase,
const char *  szLanguage 
)

Definition at line 97 of file C4RankSystem.cpp.

References Clear(), C4ComponentHost::GetData(), C4ComponentHost::GetDataSize(), iRankExtNum, iRankNum, iSize, C4Language::LoadComponentHost(), pszRankExtensions, pszRankNames, RankBase, SCharPos(), SEqual(), and szRankNames.

98 {
99  // clear any loaded rank names
100  Clear();
101  assert(szFilenames); assert(szLanguage);
102  // load new
103  C4ComponentHost Ranks;
104  if (!C4Language::LoadComponentHost(&Ranks, hGroup, szFilenames, szLanguage)) return false;
105  size_t iSize = Ranks.GetDataSize();
106  if (!iSize) return false;
107  szRankNames=new char[iSize+1];
108  memcpy(szRankNames, Ranks.GetData(), iSize * sizeof(char));
109  szRankNames[iSize]=0;
110  // replace line breaks by zero-chars
111  unsigned int i=0;
112  for (; i<iSize; ++i)
113  if (szRankNames[i] == 0x0A || szRankNames[i] == 0x0D) szRankNames[i]=0;
114  // count names
115  char *pRank0=szRankNames, *pPos=szRankNames;
116  for (i=0; i<iSize; ++i,++pPos)
117  if (!*pPos)
118  {
119  // zero-character found: content?
120  if (pPos-pRank0>0)
121  {
122  // rank extension?
123  if (*pRank0 == '*')
124  ++iRankExtNum;
125  // no comment?
126  else if (*pRank0 != '#')
127  // no setting?
128  if (SCharPos('=', pRank0) < 0)
129  // count as name!
130  ++iRankNum;
131  }
132  // advance pos
133  pRank0=pPos+1;
134  }
135  // safety
136  if (!iRankNum) { Clear(); return false; }
137  // set default rank base
138  RankBase=DefRankBase;
139  // alloc lists
140  pszRankNames = new char *[iRankNum];
141  if (iRankExtNum) pszRankExtensions = new char *[iRankExtNum];
142  // fill list with names
143  // count names
144  pRank0=szRankNames; pPos=szRankNames;
145  char **pszCurrRank=pszRankNames;
146  char **pszCurrRankExt=pszRankExtensions;
147  for (i=0; i<iSize; ++i,++pPos)
148  if (!*pPos)
149  {
150  // zero-character found: content?
151  if (pPos-pRank0>0)
152  {
153  // extension?
154  if (*pRank0 == '*')
155  {
156  *pszCurrRankExt++ = pRank0+1;
157  }
158  // no comment?
159  else if (*pRank0 != '#')
160  {
161  // check if it's a setting
162  int iEqPos=SCharPos('=', pRank0);
163  if (iEqPos >= 0)
164  {
165  // get name and value of setting
166  pRank0[iEqPos]=0; char *szValue=pRank0+iEqPos+1;
167  if (SEqual(pRank0, "Base"))
168  // get rankbase
169  // note that invalid numbers may cause desyncs here...not very likely though :)
170  sscanf(szValue, "%d", &RankBase);
171  }
172  else
173  // yeeehaa! it's a name! store it, store it!
174  *pszCurrRank++=pRank0;
175  }
176  }
177  // advance pos
178  pRank0=pPos+1;
179  }
180  // check rankbase
181  if (!RankBase) RankBase=1000;
182  // ranks read, success
183  return true;
184 }
const char * GetData() const
char ** pszRankExtensions
Definition: C4RankSystem.h:40
bool SEqual(const char *szStr1, const char *szStr2)
Definition: Standard.h:93
size_t GetDataSize() const
char * szRankNames
Definition: C4RankSystem.h:38
static bool LoadComponentHost(C4ComponentHost *host, C4Group &hGroup, const char *szFilename, const char *szLanguage)
Definition: C4Language.cpp:232
int SCharPos(char cTarget, const char *szInStr, int iIndex)
Definition: Standard.cpp:233
char ** pszRankNames
Definition: C4RankSystem.h:37
int iSize
Definition: TstC4NetIO.cpp:32

Here is the call graph for this function:

int C4RankSystem::RankByExperience ( int  iExp)

Definition at line 238 of file C4RankSystem.cpp.

References Experience().

239 {
240  int iRank=0;
241  while (Experience(iRank+1)<=iExp) ++iRank;
242  return iRank;
243 }
int Experience(int iRank)

Here is the call graph for this function:

Member Data Documentation

int C4RankSystem::iRankExtNum
protected

Definition at line 41 of file C4RankSystem.h.

Referenced by Clear(), Default(), GetExtendedRankNum(), GetRankName(), and Load().

int C4RankSystem::iRankNum
protected

Definition at line 39 of file C4RankSystem.h.

Referenced by Clear(), GetBaseRankNum(), GetRankName(), Init(), and Load().

char** C4RankSystem::pszRankExtensions {nullptr}
protected

Definition at line 40 of file C4RankSystem.h.

Referenced by Clear(), GetRankName(), and Load().

char** C4RankSystem::pszRankNames {nullptr}
protected

Definition at line 37 of file C4RankSystem.h.

Referenced by Clear(), GetRankName(), Init(), and Load().

int C4RankSystem::RankBase
protected

Definition at line 36 of file C4RankSystem.h.

Referenced by Default(), Experience(), Init(), and Load().

char C4RankSystem::RankName[C4MaxName+1]
protected

Definition at line 35 of file C4RankSystem.h.

Referenced by Default().

char C4RankSystem::Register[256+1]
protected

Definition at line 34 of file C4RankSystem.h.

Referenced by Check(), Default(), GetRankName(), and Init().

char* C4RankSystem::szRankNames {nullptr}
protected

Definition at line 38 of file C4RankSystem.h.

Referenced by Clear(), Init(), and Load().


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