GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/chardb.cpp Lines: 6 74 8.1 %
Date: 2020-06-04 Branches: 0 52 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2008  Aethyra Development Team
4
 *  Copyright (C) 2011-2019  The ManaPlus 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/db/chardb.h"
23
24
#include "configuration.h"
25
#include "logger.h"
26
27
#include "debug.h"
28
29
namespace
30
{
31
    bool mLoaded = false;
32
    bool showHiddenGender = 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
1
    STD_VECTOR<BeingSlot> mDefaultItems;
45
}  // namespace
46
47
void CharDB::load()
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"),
56
        UseVirtFs_true,
57
        SkipError_false);
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
        {
72
            loadMinMax(node, &mMinHairColor, &mMaxHairColor);
73
        }
74
        else if (xmlNameEqual(node, "hairstyle"))
75
        {
76
            loadMinMax(node, &mMinHairStyle, &mMaxHairStyle);
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
        else if (xmlNameEqual(node, "flags"))
109
        {
110
            loadFlags(node);
111
        }
112
    }
113
114
    delete doc;
115
116
    mLoaded = true;
117
}
118
119
void CharDB::loadMinMax(XmlNodeConstPtr node,
120
                        unsigned *restrict const min,
121
                        unsigned *restrict const max)
122
{
123
    if (min != nullptr)
124
        *min = XML::getProperty(node, "min", 1);
125
    if (max != nullptr)
126
        *max = XML::getProperty(node, "max", 10);
127
}
128
129
void CharDB::loadFlags(XmlNodeConstPtr node)
130
{
131
    showHiddenGender = XML::getBoolProperty(node, "showHiddeGender", false);
132
}
133
134
204
void CharDB::unload()
135
{
136
204
    logger->log1("Unloading char database...");
137
138
204
    mLoaded = false;
139
204
}
140
141
unsigned CharDB::getMinHairColor()
142
{
143
    return mMinHairColor;
144
}
145
146
unsigned CharDB::getMaxHairColor()
147
{
148
    return mMaxHairColor;
149
}
150
151
unsigned CharDB::getMinHairStyle()
152
{
153
    return mMinHairStyle;
154
}
155
156
unsigned CharDB::getMaxHairStyle()
157
{
158
    return mMaxHairStyle;
159
}
160
161
unsigned CharDB::getMinStat()
162
{
163
    return mMinStat;
164
}
165
166
unsigned CharDB::getMaxStat()
167
{
168
    return mMaxStat;
169
}
170
171
unsigned CharDB::getSumStat()
172
{
173
    return mSumStat;
174
}
175
176
unsigned CharDB::getMinLook()
177
{
178
    return mMinLook;
179
}
180
181
unsigned CharDB::getMaxLook()
182
{
183
    return mMaxLook;
184
}
185
186
unsigned CharDB::getMinRace()
187
{
188
    return mMinRace;
189
}
190
191
unsigned CharDB::getMaxRace()
192
{
193
    return mMaxRace;
194
}
195
196
const STD_VECTOR<BeingSlot> &CharDB::getDefaultItems()
197
{
198
    return mDefaultItems;
199
}
200
201
bool CharDB::getShowHiddenGender()
202
{
203
    return showHiddenGender;
204
2
}