GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/statuseffectdb.cpp Lines: 8 116 6.9 %
Date: 2021-03-17 Branches: 7 188 3.7 %

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-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#include "resources/db/statuseffectdb.h"
25
26
#include "configuration.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

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

3
}