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