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