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