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