GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/chardb.cpp Lines: 6 69 8.7 %
Date: 2017-11-29 Branches: 0 48 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2008  Aethyra Development Team
4
 *  Copyright (C) 2011-2017  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
    unsigned mMinHairColor = 0;
33
    unsigned mMaxHairColor = 0;
34
    unsigned mMinHairStyle = 0;
35
    unsigned mMaxHairStyle = 0;
36
    unsigned mMinStat = 0;
37
    unsigned mMaxStat = 0;
38
    unsigned mSumStat = 0;
39
    unsigned mMinLook = 0;
40
    unsigned mMaxLook = 0;
41
    unsigned mMinRace = 0;
42
    unsigned mMaxRace = 30;
43
2
    STD_VECTOR<BeingSlot> mDefaultItems;
44
}  // namespace
45
46
void CharDB::load()
47
{
48
    if (mLoaded)
49
        unload();
50
51
    logger->log1("Initializing char database...");
52
53
    XML::Document *doc = new XML::Document(
54
        paths.getStringValue("charCreationFile"),
55
        UseVirtFs_true,
56
        SkipError_false);
57
    XmlNodeConstPtrConst root = doc->rootNode();
58
59
    if ((root == nullptr) || !xmlNameEqual(root, "chars"))
60
    {
61
        logger->log("CharDB: Failed to parse %s.",
62
            paths.getStringValue("charCreationFile").c_str());
63
        delete doc;
64
        return;
65
    }
66
67
    for_each_xml_child_node(node, root)
68
    {
69
        if (xmlNameEqual(node, "haircolor"))
70
        {
71
            loadMinMax(node, &mMinHairColor, &mMaxHairColor);
72
        }
73
        else if (xmlNameEqual(node, "hairstyle"))
74
        {
75
            loadMinMax(node, &mMinHairStyle, &mMaxHairStyle);
76
        }
77
        else if (xmlNameEqual(node, "look"))
78
        {
79
            loadMinMax(node, &mMinLook, &mMaxLook);
80
        }
81
        else if (xmlNameEqual(node, "stat"))
82
        {
83
            loadMinMax(node, &mMinStat, &mMaxStat);
84
            mSumStat = XML::getProperty(node, "sum", 0);
85
        }
86
        else if (xmlNameEqual(node, "item"))
87
        {
88
            const int id = XML::getProperty(node, "id", 0);
89
            if (id > 0)
90
            {
91
                BeingSlot slot;
92
                slot.spriteId = id;
93
                for (int f = 0; f < maxCards; f ++)
94
                {
95
                    const std::string cardName = strprintf("card%d", f + 1);
96
                    slot.cardsId.cards[f] = XML::getProperty(node,
97
                        cardName.c_str(),
98
                        0);
99
                }
100
                mDefaultItems.push_back(slot);
101
            }
102
        }
103
        else if (xmlNameEqual(node, "race"))
104
        {
105
            loadMinMax(node, &mMinRace, &mMaxRace);
106
        }
107
    }
108
109
    delete doc;
110
111
    mLoaded = true;
112
}
113
114
void CharDB::loadMinMax(XmlNodeConstPtr node,
115
                        unsigned *restrict const min,
116
                        unsigned *restrict const max)
117
{
118
    if (min != nullptr)
119
        *min = XML::getProperty(node, "min", 1);
120
    if (max != nullptr)
121
        *max = XML::getProperty(node, "max", 10);
122
}
123
124
384
void CharDB::unload()
125
{
126
384
    logger->log1("Unloading char database...");
127
128
384
    mLoaded = false;
129
384
}
130
131
unsigned CharDB::getMinHairColor()
132
{
133
    return mMinHairColor;
134
}
135
136
unsigned CharDB::getMaxHairColor()
137
{
138
    return mMaxHairColor;
139
}
140
141
unsigned CharDB::getMinHairStyle()
142
{
143
    return mMinHairStyle;
144
}
145
146
unsigned CharDB::getMaxHairStyle()
147
{
148
    return mMaxHairStyle;
149
}
150
151
unsigned CharDB::getMinStat()
152
{
153
    return mMinStat;
154
}
155
156
unsigned CharDB::getMaxStat()
157
{
158
    return mMaxStat;
159
}
160
161
unsigned CharDB::getSumStat()
162
{
163
    return mSumStat;
164
}
165
166
unsigned CharDB::getMinLook()
167
{
168
    return mMinLook;
169
}
170
171
unsigned CharDB::getMaxLook()
172
{
173
    return mMaxLook;
174
}
175
176
unsigned CharDB::getMinRace()
177
{
178
    return mMinRace;
179
}
180
181
unsigned CharDB::getMaxRace()
182
{
183
    return mMaxRace;
184
}
185
186
const STD_VECTOR<BeingSlot> &CharDB::getDefaultItems()
187
{
188
    return mDefaultItems;
189
4
}