GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/statuseffectdb.cpp Lines: 9 131 6.9 %
Date: 2017-11-29 Branches: 7 204 3.4 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2008-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2017  The ManaPlus Developers
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/statuseffectdb.h"
24
25
#include "configuration.h"
26
#include "settings.h"
27
#include "statuseffect.h"
28
29
#include "utils/checkutils.h"
30
31
#include "resources/beingcommon.h"
32
33
#include "debug.h"
34
35
namespace
36
{
37
    typedef std::map<int, StatusEffect *> IdToEffectMap[2];
38
    bool mLoaded = false;
39
    int fakeId = 10000;
40

14
    IdToEffectMap statusEffects;
41
2
    OptionsMap optionToIdMap;
42
2
    OptionsMap opt1ToIdMap;
43
2
    OptionsMap opt2ToIdMap;
44
2
    OptionsMap opt3ToIdMap;
45
46
2
    OptionsMap blockIdToIdMap;
47
}  // namespace
48
49
int StatusEffectDB::blockIdToId(const int blockIndex)
50
{
51
    if (blockIdToIdMap.find(blockIndex) == blockIdToIdMap.end())
52
        return -1;
53
    return blockIdToIdMap[blockIndex];
54
}
55
56
StatusEffect *StatusEffectDB::getStatusEffect(const int index,
57
                                              const Enable enabling)
58
{
59
    std::map<int, StatusEffect *> &effects
60
        = statusEffects[enabling == Enable_true];
61
    const std::map<int, StatusEffect *>::iterator it = effects.find(index);
62
    if (it != effects.end())
63
        return (*it).second;
64
    reportAlways("Missing status effect: %d",
65
        index);
66
    return nullptr;
67
}
68
69
void StatusEffectDB::load()
70
{
71
    if (mLoaded)
72
        unload();
73
74
    logger->log1("Initializing status effect database...");
75
76
    loadXmlFile(paths.getStringValue("statusEffectsFile"), SkipError_false);
77
    loadXmlFile(paths.getStringValue("statusEffectsPatchFile"),
78
        SkipError_true);
79
    loadXmlDir("statusEffectsPatchDir", loadXmlFile);
80
81
    if (!blockIdToIdMap.empty())
82
    {
83
        reportAlways("Detected legacy attribute block-id "
84
            "in status-effects.xml");
85
    }
86
    mLoaded = true;
87
}
88
89
void StatusEffectDB::loadXmlFile(const std::string &fileName,
90
                                 const SkipError skipError)
91
{
92
    XML::Document doc(fileName, UseVirtFs_true, skipError);
93
    XmlNodeConstPtrConst rootNode = doc.rootNode();
94
95
    if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "status-effects"))
96
    {
97
        logger->log("Error loading status effects file: " + fileName);
98
        return;
99
    }
100
101
    for_each_xml_child_node(node, rootNode)
102
    {
103
        if (xmlNameEqual(node, "include"))
104
        {
105
            const std::string incName = XML::getProperty(node, "name", "");
106
            if (!incName.empty())
107
                loadXmlFile(incName, skipError);
108
            continue;
109
        }
110
        else if (!xmlNameEqual(node, "status-effect"))
111
        {
112
            continue;
113
        }
114
115
        int id = XML::getProperty(node, "id", -1);
116
117
        // legacy field. Only for clients 1.6.3.12 and older
118
        const int blockId = XML::getProperty(node, "block-id", -1);
119
        if (id >= 0 && blockId >= 0)
120
            blockIdToIdMap[blockId] = id;
121
122
        if (id == -1)
123
        {
124
            id = fakeId;
125
            fakeId ++;
126
        }
127
        const int option = XML::getProperty(node, "option", 0);
128
        const int opt1 = XML::getProperty(node, "opt1", 0);
129
        const int opt2 = XML::getProperty(node, "opt2", 0);
130
        const int opt3 = XML::getProperty(node, "opt3", 0);
131
        if (option != 0)
132
        {
133
            optionToIdMap[option] = id;
134
            settings.legacyEffects = false;
135
        }
136
        if (opt1 != 0)
137
        {
138
            opt1ToIdMap[opt1] = id;
139
            settings.legacyEffects = false;
140
        }
141
        if (opt2 != 0)
142
        {
143
            opt2ToIdMap[opt2] = id;
144
            settings.legacyEffects = false;
145
        }
146
        if (opt3 != 0)
147
        {
148
            opt3ToIdMap[opt3] = id;
149
            settings.legacyEffects = false;
150
        }
151
152
        StatusEffect *startEffect = statusEffects[1][id];
153
        StatusEffect *endEffect = statusEffects[0][id];
154
        const std::string name = XML::getProperty(node, "name", "");
155
        const std::string name2 = XML::langProperty(node, "name", "");
156
        if (startEffect == nullptr)
157
            startEffect = new StatusEffect;
158
        if (endEffect == nullptr)
159
            endEffect = new StatusEffect;
160
161
        startEffect->mName = name2;
162
        startEffect->mIsPoison =
163
            (name == paths.getStringValue("poisonEffectName"));
164
        startEffect->mIsCart =
165
            (name == paths.getStringValue("cartEffectName"));
166
        startEffect->mIsRiding =
167
            (name == paths.getStringValue("ridingEffectName"));
168
        startEffect->mIsTrickDead =
169
            (name == paths.getStringValue("trickDeadEffectName"));
170
        startEffect->mIsPostDelay =
171
            (name == paths.getStringValue("postDelayName"));
172
        startEffect->mMessage = XML::getProperty(
173
            node, "start-message", "");
174
        startEffect->mSFXEffect = XML::getProperty(
175
            node, "start-audio", "");
176
        startEffect->mStartParticleEffect = XML::getProperty(
177
            node, "start-particle", "");
178
        startEffect->mParticleEffect = XML::getProperty(
179
            node, "particle", "");
180
181
        startEffect->mIcon = XML::getProperty(node, "icon", "");
182
        startEffect->mAction = XML::getProperty(node, "action", "");
183
        startEffect->mIsPersistent = (XML::getProperty(
184
            node, "persistent-particle-effect", "no")) != "no";
185
186
        endEffect->mName = startEffect->mName;
187
        endEffect->mIsPoison = startEffect->mIsPoison;
188
        endEffect->mIsCart = startEffect->mIsCart;
189
        endEffect->mIsRiding = startEffect->mIsRiding;
190
        endEffect->mIsTrickDead = startEffect->mIsTrickDead;
191
        endEffect->mIsPostDelay = startEffect->mIsPostDelay;
192
        endEffect->mMessage = XML::getProperty(node, "end-message", "");
193
        endEffect->mSFXEffect = XML::getProperty(node, "end-audio", "");
194
        endEffect->mStartParticleEffect = XML::getProperty(
195
            node, "end-particle", "");
196
197
        statusEffects[1][id] = startEffect;
198
        statusEffects[0][id] = endEffect;
199
    }
200
}
201
202
static void unloadMap(std::map<int, StatusEffect *> &map)
203
{
204
    for (std::map<int, StatusEffect *>::iterator it = map.begin();
205
         it != map.end(); ++it)
206
    {
207
        delete (*it).second;
208
    }
209
210
    map.clear();
211
}
212
213
384
void StatusEffectDB::unload()
214
{
215
384
    if (!mLoaded)
216
        return;
217
218
    logger->log1("Unloading status effect database...");
219
220
    fakeId = 10000;
221
    unloadMap(statusEffects[0]);
222
    unloadMap(statusEffects[1]);
223
224
    optionToIdMap.clear();
225
    opt1ToIdMap.clear();
226
    opt2ToIdMap.clear();
227
    opt3ToIdMap.clear();
228
    blockIdToIdMap.clear();
229
230
    mLoaded = false;
231
}
232
233
const OptionsMap& StatusEffectDB::getOptionMap()
234
{
235
    return optionToIdMap;
236
}
237
238
const OptionsMap& StatusEffectDB::getOpt1Map()
239
{
240
    return opt1ToIdMap;
241
}
242
243
const OptionsMap& StatusEffectDB::getOpt2Map()
244
{
245
    return opt2ToIdMap;
246
}
247
248
const OptionsMap& StatusEffectDB::getOpt3Map()
249
{
250
    return opt3ToIdMap;
251

6
}