GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/elementaldb.cpp Lines: 2 61 3.3 %
Date: 2021-03-17 Branches: 0 106 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-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/elementaldb.h"
25
26
#include "configuration.h"
27
28
#include "resources/beingcommon.h"
29
#include "resources/beinginfo.h"
30
31
#include "utils/checkutils.h"
32
#include "utils/dtor.h"
33
34
#include "debug.h"
35
36
namespace
37
{
38
1
    BeingInfos mElementalInfos;
39
    bool mLoaded = false;
40
}  // namespace
41
42
void ElementalDb::load()
43
{
44
    if (mLoaded)
45
        unload();
46
47
    logger->log1("Initializing elemental database...");
48
    loadXmlFile(paths.getStringValue("elementalsFile"), SkipError_false);
49
    loadXmlFile(paths.getStringValue("elementalsPatchFile"), SkipError_true);
50
    loadXmlDir("elementalsPatchDir", loadXmlFile)
51
52
    mLoaded = true;
53
}
54
55
void ElementalDb::loadXmlFile(const std::string &fileName,
56
                              const SkipError skipError)
57
{
58
    XML::Document doc(fileName, UseVirtFs_true, skipError);
59
    XmlNodeConstPtr rootNode = doc.rootNode();
60
61
    if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "elementals"))
62
    {
63
        logger->log("Elemental Database: Error while loading %s!",
64
            paths.getStringValue("elementalsFile").c_str());
65
        mLoaded = true;
66
        return;
67
    }
68
69
    const int offset = XML::getProperty(rootNode, "offset", 0);
70
71
    // iterate <elemental>s
72
    for_each_xml_child_node(elementalNode, rootNode)
73
    {
74
        if (xmlNameEqual(elementalNode, "include"))
75
        {
76
            const std::string name = XML::getProperty(
77
                elementalNode, "name", "");
78
            if (!name.empty())
79
                loadXmlFile(name, skipError);
80
            continue;
81
        }
82
        if (!xmlNameEqual(elementalNode, "elemental"))
83
            continue;
84
85
        const int id = XML::getProperty(elementalNode, "id", 0);
86
        BeingInfo *currentInfo = nullptr;
87
        if (mElementalInfos.find(fromInt(id + offset, BeingTypeId))
88
            != mElementalInfos.end())
89
        {
90
            logger->log("ElementalDb: Redefinition of elemental ID %d", id);
91
            currentInfo = mElementalInfos[fromInt(id + offset, BeingTypeId)];
92
        }
93
        if (currentInfo == nullptr)
94
            currentInfo = new BeingInfo;
95
96
        currentInfo->setBlockType(BlockType::NONE);
97
        BeingCommon::readBasicAttributes(currentInfo,
98
            elementalNode, "attack");
99
        BeingCommon::readWalkingAttributes(currentInfo,
100
            elementalNode,
101
            0);
102
103
        currentInfo->setMaxHP(XML::getProperty(elementalNode, "maxHP", 0));
104
105
        currentInfo->setDeadSortOffsetY(XML::getProperty(
106
            elementalNode, "deadSortOffsetY", 31));
107
108
        currentInfo->setColorsList(XML::getProperty(elementalNode,
109
            "colors", ""));
110
111
        if (currentInfo->getMaxHP() != 0)
112
            currentInfo->setStaticMaxHP(true);
113
114
        SpriteDisplay display;
115
116
        // iterate <sprite>s and <sound>s
117
        for_each_xml_child_node(spriteNode, elementalNode)
118
        {
119
            BeingCommon::readObjectNodes(spriteNode, display,
120
                currentInfo, "ElementalDb");
121
        }
122
        currentInfo->setDisplay(display);
123
124
        mElementalInfos[fromInt(id + offset, BeingTypeId)] = currentInfo;
125
    }
126
}
127
128
void ElementalDb::unload()
129
{
130
    logger->log1("Unloading elemental database...");
131
    delete_all(mElementalInfos);
132
    mElementalInfos.clear();
133
134
    mLoaded = false;
135
}
136
137
138
BeingInfo *ElementalDb::get(const BeingTypeId id)
139
{
140
    BeingInfoIterator i = mElementalInfos.find(id);
141
142
    if (i == mElementalInfos.end())
143
    {
144
        i = mElementalInfos.find(id);
145
        if (i == mElementalInfos.end())
146
        {
147
            reportAlways("ElementalDb: Warning, unknown elemental ID "
148
                "%d requested",
149
                toInt(id, int))
150
            return BeingInfo::unknown;
151
        }
152
    }
153
    return i->second;
154
2
}