OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4InfoCore.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 /* Structures for object and player info components */
19 
20 #include "C4Include.h"
21 #include "object/C4InfoCore.h"
22 
23 #include "c4group/C4Components.h"
24 #include "c4group/C4Group.h"
25 #include "lib/C4Markup.h"
26 #include "lib/C4Random.h"
27 #include "object/C4Def.h"
28 #include "object/C4DefList.h"
29 #include "object/C4GameObjects.h"
30 #include "player/C4RankSystem.h"
31 
32 
33 //------------------------------- Player Info ----------------------------------------
34 
36 {
37  Default();
38 }
39 
41 {
42  *Comment='\0';
43  *RankName='\0';
44  TotalScore=0;
47  *LeagueName='\0';
51  Rank=0;
52  SCopy("Neuling",PrefName);
53  if (pRanks) SCopy(pRanks->GetRankName(Rank,false).getData(),RankName);
54  else SCopy("Rang",RankName);
55  PrefClonkSkin=0;
56  PrefColor=0;
57  PrefColorDw=0xff;
58  PrefColor2Dw=0;
60  PrefMouse=1;
64  ExtraData.Reset();
65 }
66 
68 {
69  DWORD valRGB[12] = { 0xFF0000E8, 0xFFF40000, 0xFF00C800, 0xFFFCF41C,
70  0xFFC48444, 0xFF784830, 0xFFA04400, 0xFFF08050,
71  0xFF848484, 0xFFFFFFFF, 0xFF0094F8, 0xFFBC00C0
72  };
73  if (Inside<int32_t>(iPrefColor, 0, 11))
74  return valRGB[iPrefColor];
75  return 0xFFAAAAAA;
76 }
77 
79 {
80  // New version
81  StdStrBuf Source;
82  if (hGroup.LoadEntryString(C4CFN_PlayerInfoCore,&Source))
83  {
84  // Compile
85  StdStrBuf GrpName = hGroup.GetFullName(); GrpName.Append(DirSep C4CFN_PlayerInfoCore);
86  if (!CompileFromBuf_LogWarn<StdCompilerINIRead>(*this, Source, GrpName.getData()))
87  return false;
88  // Pref for AutoContextMenus is still undecided: default by player's control style
89  if (OldPrefAutoContextMenu == -1)
91  // Determine true color from indexed pref color
92  if (!PrefColorDw)
94  // Validate colors
95  PrefColorDw &= 0xffffff;
96  PrefColor2Dw &= 0xffffff;
97  // Validate name
99  // Success
100  return true;
101  }
102 
103  // Old version no longer supported - sorry
104  return false;
105 }
106 
108 {
109  StdStrBuf Source, Name = hGroup.GetFullName(); Name.Append(DirSep C4CFN_PlayerInfoCore);
110  if (!DecompileToBuf_Log<StdCompilerINIWrite>(*this, &Source, Name.getData()))
111  return false;
112  if (!hGroup.Add(C4CFN_PlayerInfoCore,Source,false,true))
113  return false;
114  hGroup.Delete("C4Player.ocb");
115  return true;
116 }
117 
118 void C4PlayerInfoCore::Promote(int32_t iRank, C4RankSystem &rRanks)
119 {
120  Rank=iRank;
122 }
123 
125 {
126  if (TotalScore>=rRanks.Experience(Rank+1))
127  {
128  Promote(Rank+1,rRanks);
129  return true;
130  }
131  return false;
132 }
133 
135 {
136  C4ValueNumbers numbers;
137  pComp->Name("Player");
138  pComp->Value(mkNamingAdapt(toC4CStr(PrefName),"Name", "Neuling"));
139  pComp->Value(mkNamingAdapt(toC4CStr(Comment), "Comment", ""));
140  pComp->Value(mkNamingAdapt(Rank, "Rank", 0));
141  pComp->Value(mkNamingAdapt(toC4CStr(RankName),"RankName", LoadResStr("IDS_MSG_RANK"))); // TODO: check if this would be desirable
142  pComp->Value(mkNamingAdapt(TotalScore, "Score", 0));
143  pComp->Value(mkNamingAdapt(Rounds, "Rounds", 0));
144  pComp->Value(mkNamingAdapt(RoundsWon, "RoundsWon", 0));
145  pComp->Value(mkNamingAdapt(RoundsLost, "RoundsLost", 0));
146  pComp->Value(mkNamingAdapt(TotalPlayingTime, "TotalPlayingTime", 0));
147  pComp->Value(mkNamingAdapt(mkParAdapt(ExtraData, &numbers), "ExtraData", C4ValueMapData()));
148  pComp->Value(mkNamingAdapt(numbers, "ExtraDataValues"));
149  if (pComp->isDeserializer())
150  {
151  numbers.Denumerate();
152  ExtraData.Denumerate(&numbers);
153  }
154  pComp->Value(mkNamingAdapt(toC4CStr(LeagueName),"LeagueName", ""));
155  pComp->NameEnd();
156 
157  pComp->Name("Preferences");
158  pComp->Value(mkNamingAdapt(PrefColor, "Color", 0));
159  pComp->Value(mkNamingAdapt(PrefColorDw, "ColorDw", 0xffu));
160  pComp->Value(mkNamingAdapt(PrefColor2Dw, "AlternateColorDw", 0u));
161  pComp->Value(mkNamingAdapt(PrefMouse, "Mouse", 1));
162  pComp->Value(mkNamingAdapt(OldPrefControl, "Control", 1));
163  pComp->Value(mkNamingAdapt(OldPrefControlStyle, "AutoStopControl", 0));
164  pComp->Value(mkNamingAdapt(OldPrefAutoContextMenu, "AutoContextMenu", -1)); // compiling default is -1 (if this is detected, AutoContextMenus will be defaulted by control style)
165  pComp->Value(mkNamingAdapt(PrefControl, "ControlSet", StdStrBuf()));
166  pComp->Value(mkNamingAdapt(PrefClonkSkin, "ClonkSkin", 0));
167  pComp->NameEnd();
168 
169  pComp->Value(mkNamingAdapt(LastRound, "LastRound"));
170 
171  pComp->Value(mkNamingAdapt(Achievements, "Achievements"));
172 }
173 
174 //------------------------------- Object Info ----------------------------------------
175 
177 {
178  Default();
179 }
180 
182  C4DefList *pDefs,
183  const char *cpNames)
184 {
185 
186  // Def
187  C4Def *pDef=nullptr;
188  if (pDefs) pDef = pDefs->ID2Def(n_id);
189 
190  // Defaults
191  id=n_id;
192  Participation=1;
193  Rank=0;
194  Experience=0;
195  Rounds=0;
196  DeathCount=0;
197  Birthday=0;
199  SCopy("Clonk",Name,C4MaxName);
200  SCopy("Clonk",TypeName,C4MaxName);
201  sRankName.Copy("Clonk");
203  NextRankExp=0;
204  DeathMessage[0]='\0';
205  Age=0;
206  ExtraData.Reset();
207 
208  // Type
209  if (pDef) SCopy(pDef->GetName(),TypeName,C4MaxName);
210 
211  // Name
212  if (cpNames)
213  {
214  SCopySegment(cpNames,Random(SCharCount(0x0A,cpNames)),Name,0x0A,C4MaxName+1);
216  SReplaceChar(Name,0x0D,0x00);
217  if (!Name[0]) SCopy("Clonk",Name,C4MaxName);
218  }
219 
220  if (pDefs) UpdateCustomRanks(pDefs);
221 }
222 
223 void C4ObjectInfoCore::Promote(int32_t iRank, C4RankSystem &rRanks, bool fForceRankName)
224 {
225  Rank=iRank;
226  // copy new rank name if defined only, or forced to use highest defined rank for too high info ranks
227  StdStrBuf sNewRank(rRanks.GetRankName(Rank,fForceRankName));
228  if (sNewRank) sRankName.Copy(sNewRank);
229 }
230 
232 {
233  assert(pDefs);
234  C4Def *pDef = pDefs->ID2Def(id);
235  if (!pDef) return;
236  if (pDef->pRankNames)
237  {
238  StdStrBuf sRank(pDef->pRankNames->GetRankName(Rank,false));
239  if (sRank) sRankName.Copy(sRank);
240  // next rank data
241  StdStrBuf sNextRank(pDef->pRankNames->GetRankName(Rank+1,false));
242  if (sNextRank)
243  {
244  sNextRankName.Copy(sNextRank);
246  }
247  else
248  {
249  // no more promotion possible by custom rank system
252  }
253  }
254  else
255  {
256  // definition does not have custom rank names
258  NextRankExp = 0;
259  }
260 }
261 
262 bool C4ObjectInfoCore::GetNextRankInfo(C4RankSystem &rDefaultRanks, int32_t *piNextRankExp, StdStrBuf *psNextRankName)
263 {
264  int32_t iNextRankExp;
265  // custom rank assigned?
266  if (NextRankExp)
267  {
268  iNextRankExp = NextRankExp;
269  if (psNextRankName) psNextRankName->Copy(sNextRankName);
270  }
271  else
272  {
273  // no custom rank: Get from default set
274  StdStrBuf sRank(rDefaultRanks.GetRankName(Rank+1, false));
275  if (sRank)
276  {
277  iNextRankExp = rDefaultRanks.Experience(Rank+1);
278  if (psNextRankName) psNextRankName->Copy(sRank);
279  }
280  else
281  // no more promotion
282  iNextRankExp = C4RankSystem::EXP_NoPromotion;
283  }
284  // return result
285  if (piNextRankExp) *piNextRankExp = iNextRankExp;
286  // return value is whether additional promotion is possible
287  return iNextRankExp != C4RankSystem::EXP_NoPromotion;
288 }
289 
291 {
292  StdStrBuf Source;
293  return hGroup.LoadEntryString(C4CFN_ObjectInfoCore, &Source) &&
294  Compile(Source.getData());
295 }
296 
298 {
299  // rank overload by def: Update any NextRank-stuff
300  if (pDefs) UpdateCustomRanks(pDefs);
301  char *Buffer; size_t BufferSize;
302  if (!Decompile(&Buffer,&BufferSize))
303  return false;
304  if (!hGroup.Add(C4CFN_ObjectInfoCore,Buffer,BufferSize,false,true) )
305  { delete [] Buffer; return false; }
306  return true;
307 }
308 
310 {
311  C4ValueNumbers numbers;
312  pComp->Value(mkNamingAdapt(id, "id", C4ID::None));
313  pComp->Value(mkNamingAdapt(toC4CStr(Name), "Name", "Clonk"));
314  pComp->Value(mkNamingAdapt(toC4CStr(DeathMessage), "DeathMessage", ""));
315  pComp->Value(mkNamingAdapt(Rank, "Rank", 0));
316  pComp->Value(mkNamingAdapt(sRankName, "RankName", "Clonk"));
317  pComp->Value(mkNamingAdapt(sNextRankName, "NextRankName", ""));
318  pComp->Value(mkNamingAdapt(toC4CStr(TypeName), "TypeName", "Clonk"));
319  pComp->Value(mkNamingAdapt(Participation, "Participation", 1));
320  pComp->Value(mkNamingAdapt(Experience, "Experience", 0));
321  pComp->Value(mkNamingAdapt(NextRankExp, "NextRankExp", 0));
322  pComp->Value(mkNamingAdapt(Rounds, "Rounds", 0));
323  pComp->Value(mkNamingAdapt(DeathCount, "DeathCount", 0));
324  pComp->Value(mkNamingAdapt(Birthday, "Birthday", 0));
325  pComp->Value(mkNamingAdapt(TotalPlayingTime, "TotalPlayingTime", 0));
326  pComp->Value(mkNamingAdapt(Age, "Age", 0));
327  pComp->Value(mkNamingAdapt(mkParAdapt(ExtraData, &numbers), "ExtraData", C4ValueMapData()));
328  pComp->Value(mkNamingAdapt(numbers, "ExtraDataValues"));
329  if (pComp->isDeserializer())
330  {
331  numbers.Denumerate();
332  ExtraData.Denumerate(&numbers);
333  }
334 }
335 
336 bool C4ObjectInfoCore::Compile(const char *szSource)
337 {
338  bool ret = CompileFromBuf_LogWarn<StdCompilerINIRead>(
339  mkNamingAdapt(*this, "ObjectInfo"),
340  StdStrBuf(szSource),
341  "ObjectInfo");
342  // DeathMessages are not allowed to stay forever
343  if ('@' == DeathMessage[0]) DeathMessage[0] = ' ';
344  return ret;
345 }
346 
347 bool C4ObjectInfoCore::Decompile(char **ppOutput, size_t *ipSize)
348 {
349  StdStrBuf Buf;
350  if (!DecompileToBuf_Log<StdCompilerINIWrite>(
351  mkNamingAdapt(*this, "ObjectInfo"),
352  &Buf,
353  "ObjectInfo"))
354  {
355  if (ppOutput) *ppOutput = nullptr;
356  if (ipSize) *ipSize = 0;
357  return false;
358  }
359  if (ppOutput) *ppOutput = Buf.GrabPointer();
360  if (ipSize) *ipSize = Buf.getSize();
361  return true;
362 }
363 
364 
365 //------------------------------- Round Info ------------------------------------------
366 
368 {
369  InplaceReconstruct(this);
370 }
371 
373 {
374  pComp->Value(mkNamingAdapt(Title, "Title", ""));
375  pComp->Value(mkNamingAdapt(Date, "Date", 0u));
376  pComp->Value(mkNamingAdapt(Duration, "Duration", 0));
377  pComp->Value(mkNamingAdapt(Won, "Won", 0));
378  pComp->Value(mkNamingAdapt(Score, "Score", 0));
379  pComp->Value(mkNamingAdapt(FinalScore, "FinalScore", 0));
380  pComp->Value(mkNamingAdapt(TotalScore, "TotalScore", 0));
381  pComp->Value(mkNamingAdapt(Bonus, "Bonus", 0));
382  pComp->Value(mkNamingAdapt(Level, "Level", 0));
383 }
384 
385 
386 
387 
388 
389 
int32_t OldPrefControlStyle
Definition: C4InfoCore.h:104
const char * getData() const
Definition: StdBuf.h:442
C4Def * ID2Def(C4ID id)
bool GetNextRankInfo(C4RankSystem &rDefaultRanks, int32_t *piNextRankExp, StdStrBuf *psNextRankName)
Definition: C4InfoCore.cpp:262
int32_t OldPrefAutoContextMenu
Definition: C4InfoCore.h:105
int32_t TotalScore
Definition: C4InfoCore.h:71
void Denumerate()
Definition: C4Value.cpp:281
int32_t TotalPlayingTime
Definition: C4InfoCore.h:91
int32_t Rounds
Definition: C4InfoCore.h:43
char * GrabPointer()
Definition: StdBuf.h:459
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:130
void Default(C4ID n_id=C4ID::None, class C4DefList *pDefs=nullptr, const char *cpNames=nullptr)
Definition: C4InfoCore.cpp:181
uint32_t Random()
Definition: C4Random.cpp:43
int32_t Won
Definition: C4InfoCore.h:70
int32_t Score
Definition: C4InfoCore.h:71
int32_t RoundsWon
Definition: C4InfoCore.h:90
int32_t OldPrefControl
Definition: C4InfoCore.h:103
bool Decompile(char **ppOutput, size_t *ipSize)
Definition: C4InfoCore.cpp:347
void Clear()
Definition: StdBuf.h:466
bool Save(C4Group &hGroup, class C4DefList *pDefs)
Definition: C4InfoCore.cpp:297
bool SCopySegment(const char *szString, int iSegment, char *sTarget, char cSeparator, int iMaxL, bool fSkipWhitespace)
Definition: Standard.cpp:251
void Denumerate(C4ValueNumbers *)
Definition: C4ValueMap.cpp:254
char PrefName[C4MaxName+1]
Definition: C4InfoCore.h:85
int32_t PrefClonkSkin
Definition: C4InfoCore.h:100
char LeagueName[C4MaxName+1]
Definition: C4InfoCore.h:94
StdCopyStrBuf PrefControl
Definition: C4InfoCore.h:96
StdStrBuf sRankName
Definition: C4InfoCore.h:40
char DeathMessage[C4MaxDeathMsg+1]
Definition: C4InfoCore.h:48
virtual bool Name(const char *szName)
Definition: StdCompiler.h:77
bool Delete(const char *szFiles, bool fRecursive=false)
Definition: C4Group.cpp:1334
#define toC4CStr(szString)
Definition: StdAdaptors.h:24
unsigned int SCharCount(char cTarget, const char *szInStr, const char *cpUntil)
Definition: Standard.cpp:298
int32_t Participation
Definition: C4InfoCore.h:38
int32_t PrefMouse
Definition: C4InfoCore.h:97
C4ValueMapData ExtraData
Definition: C4InfoCore.h:93
StdStrBuf GetRankName(int iRank, bool fReturnLastIfOver)
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
int32_t RoundsLost
Definition: C4InfoCore.h:90
const char * GetName() const override
Definition: C4PropList.cpp:267
int32_t TotalPlayingTime
Definition: C4InfoCore.h:46
StdNamingAdapt< T > mkNamingAdapt(T &&rValue, const char *szName)
Definition: StdAdaptors.h:92
char Comment[C4MaxComment+1]
Definition: C4InfoCore.h:86
static DWORD GetPrefColorValue(int32_t iPrefColor)
Definition: C4InfoCore.cpp:67
int32_t NextRankExp
Definition: C4InfoCore.h:42
bool Load(C4Group &hGroup)
Definition: C4InfoCore.cpp:78
uint32_t Date
Definition: C4InfoCore.h:68
void Default(C4RankSystem *pRanks=nullptr)
Definition: C4InfoCore.cpp:40
StdStrBuf GetFullName() const
Definition: C4Group.cpp:2078
bool Save(C4Group &hGroup)
Definition: C4InfoCore.cpp:107
#define C4CFN_PlayerInfoCore
Definition: C4Components.h:45
static const C4ID None
Definition: C4Id.h:39
void Append(const char *pnData, size_t iChars)
Definition: StdBuf.h:519
C4ValueMapData ExtraData
Definition: C4InfoCore.h:49
char RankName[C4MaxName+1]
Definition: C4InfoCore.h:88
Definition: C4Def.h:98
int32_t Birthday
Definition: C4InfoCore.h:46
const unsigned int C4MaxName
char Name[C4MaxName+1]
Definition: C4InfoCore.h:37
void UpdateCustomRanks(C4DefList *pDefs)
Definition: C4InfoCore.cpp:231
void CompileFunc(StdCompiler *pComp)
Definition: C4InfoCore.cpp:372
int32_t PrefColor
Definition: C4InfoCore.h:98
bool CheckPromotion(C4RankSystem &rRanks)
Definition: C4InfoCore.cpp:124
void Value(const T &rStruct)
Definition: StdCompiler.h:161
uint32_t PrefColorDw
Definition: C4InfoCore.h:99
Definition: C4Id.h:25
StdStrBuf sNextRankName
Definition: C4InfoCore.h:41
uint32_t PrefColor2Dw
Definition: C4InfoCore.h:99
std::enable_if< std::is_nothrow_default_constructible< T >::value >::type InplaceReconstruct(T *obj)
Definition: Standard.h:35
virtual bool isDeserializer()
Definition: StdCompiler.h:53
void CompileFunc(StdCompiler *pComp)
Definition: C4InfoCore.cpp:134
int32_t FinalScore
Definition: C4InfoCore.h:71
void Promote(int32_t iRank, C4RankSystem &rRanks, bool fForceRankName)
Definition: C4InfoCore.cpp:223
int32_t TotalScore
Definition: C4InfoCore.h:89
bool LoadEntryString(const char *szEntryName, StdStrBuf *Buf)
Definition: C4Group.cpp:1932
int SClearFrontBack(char *szString, char cClear)
Definition: Standard.cpp:433
void CompileFunc(StdCompiler *pComp)
Definition: C4InfoCore.cpp:309
size_t getSize() const
Definition: StdBuf.h:444
bool Load(C4Group &hGroup)
Definition: C4InfoCore.cpp:290
bool Add(const char *szFile, const char *szAddAs)
Definition: C4Group.cpp:1316
StdParameterAdapt< T, P > mkParAdapt(T &&rObj, P &&rPar)
Definition: StdAdaptors.h:458
int32_t Experience
Definition: C4InfoCore.h:43
bool Compile(const char *szSource)
Definition: C4InfoCore.cpp:336
#define C4CFN_ObjectInfoCore
Definition: C4Components.h:47
C4RankSystem * pRankNames
Definition: C4Def.h:189
#define DirSep
int32_t Rounds
Definition: C4InfoCore.h:90
void Promote(int32_t iRank, C4RankSystem &rRanks)
Definition: C4InfoCore.cpp:118
uint32_t DWORD
int32_t DeathCount
Definition: C4InfoCore.h:44
void Copy()
Definition: StdBuf.h:467
int32_t Duration
Definition: C4InfoCore.h:69
int32_t Level
Definition: C4InfoCore.h:73
C4ScenarioParameters Achievements
Definition: C4InfoCore.h:108
int Experience(int iRank)
int32_t Bonus
Definition: C4InfoCore.h:72
virtual void NameEnd(bool fBreak=false)
Definition: StdCompiler.h:78
char TypeName[C4MaxName+1+1]
Definition: C4InfoCore.h:45
void SReplaceChar(char *str, char fc, char tc)
Definition: Standard.cpp:326
StdCopyStrBuf Title
Definition: C4InfoCore.h:67
C4RoundResult LastRound
Definition: C4InfoCore.h:92
static bool StripMarkup(char *szText)
Definition: C4Markup.cpp:113