ManaPlus
chardb.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2008 Aethyra Development Team
4  * Copyright (C) 2011-2019 The ManaPlus Developers
5  * Copyright (C) 2019-2021 Andrei Karas
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include "resources/db/chardb.h"
24 
25 #include "configuration.h"
26 #include "logger.h"
27 
28 #include "debug.h"
29 
30 namespace
31 {
32  bool mLoaded = false;
33  unsigned mMinHairColor = 0;
34  unsigned mMaxHairColor = 0;
35  unsigned mMinHairStyle = 0;
36  unsigned mMaxHairStyle = 0;
37  unsigned mMinStat = 0;
38  unsigned mMaxStat = 0;
39  unsigned mSumStat = 0;
40  unsigned mMinLook = 0;
41  unsigned mMaxLook = 0;
42  unsigned mMinRace = 0;
43  unsigned mMaxRace = 30;
44  STD_VECTOR<BeingSlot> mDefaultItems;
45 } // namespace
46 
48 {
49  if (mLoaded)
50  unload();
51 
52  logger->log1("Initializing char database...");
53 
54  XML::Document *doc = new XML::Document(
55  paths.getStringValue("charCreationFile"),
58  XmlNodeConstPtrConst root = doc->rootNode();
59 
60  if ((root == nullptr) || !xmlNameEqual(root, "chars"))
61  {
62  logger->log("CharDB: Failed to parse %s.",
63  paths.getStringValue("charCreationFile").c_str());
64  delete doc;
65  return;
66  }
67 
68  for_each_xml_child_node(node, root)
69  {
70  if (xmlNameEqual(node, "haircolor"))
71  {
73  }
74  else if (xmlNameEqual(node, "hairstyle"))
75  {
77  }
78  else if (xmlNameEqual(node, "look"))
79  {
80  loadMinMax(node, &mMinLook, &mMaxLook);
81  }
82  else if (xmlNameEqual(node, "stat"))
83  {
84  loadMinMax(node, &mMinStat, &mMaxStat);
85  mSumStat = XML::getProperty(node, "sum", 0);
86  }
87  else if (xmlNameEqual(node, "item"))
88  {
89  const int id = XML::getProperty(node, "id", 0);
90  if (id > 0)
91  {
92  BeingSlot slot;
93  slot.spriteId = id;
94  for (int f = 0; f < maxCards; f ++)
95  {
96  const std::string cardName = strprintf("card%d", f + 1);
97  slot.cardsId.cards[f] = XML::getProperty(node,
98  cardName.c_str(),
99  0);
100  }
101  mDefaultItems.push_back(slot);
102  }
103  }
104  else if (xmlNameEqual(node, "race"))
105  {
106  loadMinMax(node, &mMinRace, &mMaxRace);
107  }
108  }
109 
110  delete doc;
111 
112  mLoaded = true;
113 }
114 
115 void CharDB::loadMinMax(XmlNodeConstPtr node,
116  unsigned *restrict const min,
117  unsigned *restrict const max)
118 {
119  if (min != nullptr)
120  *min = XML::getProperty(node, "min", 1);
121  if (max != nullptr)
122  *max = XML::getProperty(node, "max", 10);
123 }
124 
126 {
127  logger->log1("Unloading char database...");
128 
129  mLoaded = false;
130 }
131 
133 {
134  return mMinHairColor;
135 }
136 
138 {
139  return mMaxHairColor;
140 }
141 
143 {
144  return mMinHairStyle;
145 }
146 
148 {
149  return mMaxHairStyle;
150 }
151 
153 {
154  return mMinStat;
155 }
156 
158 {
159  return mMaxStat;
160 }
161 
163 {
164  return mSumStat;
165 }
166 
168 {
169  return mMinLook;
170 }
171 
173 {
174  return mMaxLook;
175 }
176 
178 {
179  return mMinRace;
180 }
181 
183 {
184  return mMaxRace;
185 }
186 
187 const STD_VECTOR<BeingSlot> &CharDB::getDefaultItems()
188 {
189  return mDefaultItems;
190 }
#define maxCards
Definition: cards.h:25
std::string getStringValue(const std::string &key) const
void log(const char *const log_text,...)
Definition: logger.cpp:269
void log1(const char *const log_text)
Definition: logger.cpp:238
xmlNodePtr rootNode()
Definition: libxml.cpp:169
Configuration paths
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:161
#define restrict
Definition: localconsts.h:165
Logger * logger
Definition: logger.cpp:89
void unload()
Definition: chardb.cpp:125
const std::vector< BeingSlot > & getDefaultItems()
Definition: chardb.cpp:187
unsigned getSumStat()
Definition: chardb.cpp:162
unsigned getMinLook()
Definition: chardb.cpp:167
unsigned getMinRace()
Definition: chardb.cpp:177
unsigned getMinStat()
Definition: chardb.cpp:152
unsigned getMaxLook()
Definition: chardb.cpp:172
void load()
Definition: chardb.cpp:47
unsigned getMinHairStyle()
Definition: chardb.cpp:142
void loadMinMax(const xmlNodePtr node, unsigned *const min, unsigned *const max)
Definition: chardb.cpp:115
unsigned getMaxHairStyle()
Definition: chardb.cpp:147
unsigned getMaxStat()
Definition: chardb.cpp:157
unsigned getMaxHairColor()
Definition: chardb.cpp:137
unsigned getMaxRace()
Definition: chardb.cpp:182
unsigned getMinHairColor()
Definition: chardb.cpp:132
void unload()
Definition: net.cpp:180
int getProperty(const xmlNodePtr node, const char *const name, int def)
Definition: libxml.cpp:174
std::vector< BeingSlot > mDefaultItems
Definition: chardb.cpp:44
const bool SkipError_false
Definition: skiperror.h:30
std::string strprintf(const char *const format,...)
int spriteId
Definition: beingslot.h:45
CardsList cardsId
Definition: beingslot.h:46
int cards[4]
Definition: cardslist.h:68
const bool UseVirtFs_true
Definition: usevirtfs.h:30