GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/chardb.cpp Lines: 6 68 8.8 %
Date: 2021-03-17 Branches: 0 50 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
 *  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
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
    }
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
125
204
void CharDB::unload()
126
{
127
204
    logger->log1("Unloading char database...");
128
129
204
    mLoaded = false;
130
204
}
131
132
unsigned CharDB::getMinHairColor()
133
{
134
    return mMinHairColor;
135
}
136
137
unsigned CharDB::getMaxHairColor()
138
{
139
    return mMaxHairColor;
140
}
141
142
unsigned CharDB::getMinHairStyle()
143
{
144
    return mMinHairStyle;
145
}
146
147
unsigned CharDB::getMaxHairStyle()
148
{
149
    return mMaxHairStyle;
150
}
151
152
unsigned CharDB::getMinStat()
153
{
154
    return mMinStat;
155
}
156
157
unsigned CharDB::getMaxStat()
158
{
159
    return mMaxStat;
160
}
161
162
unsigned CharDB::getSumStat()
163
{
164
    return mSumStat;
165
}
166
167
unsigned CharDB::getMinLook()
168
{
169
    return mMinLook;
170
}
171
172
unsigned CharDB::getMaxLook()
173
{
174
    return mMaxLook;
175
}
176
177
unsigned CharDB::getMinRace()
178
{
179
    return mMinRace;
180
}
181
182
unsigned CharDB::getMaxRace()
183
{
184
    return mMaxRace;
185
}
186
187
const STD_VECTOR<BeingSlot> &CharDB::getDefaultItems()
188
{
189
    return mDefaultItems;
190
2
}