Mana
Loading...
Searching...
No Matches
chardb.cpp
Go to the documentation of this file.
1/*
2 * Character creation settings
3 * Copyright (C) 2011-2013 The ManaPlus Developers
4 * Copyright (C) 2013 The Mana Developers
5 *
6 * This file is part of The ManaPlus Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "resources/chardb.h"
23
24#include "log.h"
25
26#include "utils/xml.h"
27
28namespace
29{
30 bool mLoaded = false;
31 unsigned mMinHairColor = 0;
32 unsigned mMaxHairColor = 0;
33 unsigned mMinHairStyle = 0;
34 unsigned mMaxHairStyle = 0;
35 unsigned mMinStat = 0;
36 unsigned mMaxStat = 0;
37 unsigned mSumStat = 0;
38 std::vector<int> mDefaultItems;
39}
40
41static void loadMinMax(XML::Node node, unsigned *min, unsigned *max)
42{
43 *min = node.getProperty("min", 1);
44 *max = node.getProperty("max", 10);
45}
46
48{
49 if (mLoaded)
50 unload();
51
52 XML::Document doc("charcreation.xml");
53 XML::Node root = doc.rootNode();
54
55 if (!root || root.name() != "chars")
56 {
57 Log::info("CharDB: Failed to parse charcreation.xml.");
58 return;
59 }
60
61 for (auto node : root.children())
62 {
63 if (node.name() == "haircolor")
64 {
65 loadMinMax(node, &mMinHairColor, &mMaxHairColor);
66 }
67 else if (node.name() == "hairstyle")
68 {
69 loadMinMax(node, &mMinHairStyle, &mMaxHairStyle);
70 }
71 else if (node.name() == "stat")
72 {
73 loadMinMax(node, &mMinStat, &mMaxStat);
74 mSumStat = node.getProperty("sum", 0);
75 }
76 else if (node.name() == "item")
77 {
78 const int id = node.getProperty("id", 0);
79 if (id > 0)
80 mDefaultItems.push_back(id);
81 }
82 }
83
84 mLoaded = true;
85}
86
88{
89 Log::info("Unloading chars database...");
90
91 mLoaded = false;
92}
93
95{
96 return mMinHairColor;
97}
98
100{
101 return mMaxHairColor;
102}
103
105{
106 return mMinHairStyle;
107}
108
110{
111 return mMaxHairStyle;
112}
113
115{
116 return mMinStat;
117}
118
120{
121 return mMaxStat;
122}
123
125{
126 return mSumStat;
127}
128
129const std::vector<int> &CharDB::getDefaultItems()
130{
131 return mDefaultItems;
132}
A helper class for parsing an XML document, which also cleans it up again (RAII).
Definition xml.h:190
Node rootNode() const
Returns the root node of the document (or NULL if there was a load error).
Definition xml.h:213
std::string_view name() const
Definition xml.h:46
int getProperty(const char *name, int def) const
Definition xml.h:144
Children children() const
Definition xml.h:97
void unload()
Definition chardb.cpp:87
unsigned getSumStat()
Definition chardb.cpp:124
unsigned getMinStat()
Definition chardb.cpp:114
void load()
Definition chardb.cpp:47
unsigned getMinHairStyle()
Definition chardb.cpp:104
unsigned getMaxHairStyle()
Definition chardb.cpp:109
unsigned getMaxStat()
Definition chardb.cpp:119
unsigned getMaxHairColor()
Definition chardb.cpp:99
unsigned getMinHairColor()
Definition chardb.cpp:94
const std::vector< int > & getDefaultItems()
Definition chardb.cpp:129
void info(const char *log_text,...) LOG_PRINTF_ATTR