GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/homunculusdb.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/homunculusdb.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 mHomunculusInfos;
39
    bool mLoaded = false;
40
}  // namespace
41
42
void HomunculusDB::load()
43
{
44
    if (mLoaded)
45
        unload();
46
47
    logger->log1("Initializing homunculus database...");
48
    loadXmlFile(paths.getStringValue("homunculusesFile"), SkipError_false);
49
    loadXmlFile(paths.getStringValue("homunculusesPatchFile"), SkipError_true);
50
    loadXmlDir("homunculusesPatchDir", loadXmlFile);
51
52
    mLoaded = true;
53
}
54
55
void HomunculusDB::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, "homunculuses"))
62
    {
63
        logger->log("Homunculus Database: Error while loading %s!",
64
            paths.getStringValue("homunculusesFile").c_str());
65
        mLoaded = true;
66
        return;
67
    }
68
69
    const int offset = XML::getProperty(rootNode, "offset", 0);
70
71
    // iterate <homunculus>s
72
    for_each_xml_child_node(homunculusNode, rootNode)
73
    {
74
        if (xmlNameEqual(homunculusNode, "include"))
75
        {
76
            const std::string name = XML::getProperty(
77
                homunculusNode, "name", "");
78
            if (!name.empty())
79
                loadXmlFile(name, skipError);
80
            continue;
81
        }
82
        if (!xmlNameEqual(homunculusNode, "homunculus"))
83
            continue;
84
85
        const int id = XML::getProperty(homunculusNode, "id", 0);
86
        BeingInfo *currentInfo = nullptr;
87
        if (mHomunculusInfos.find(fromInt(id + offset, BeingTypeId))
88
            != mHomunculusInfos.end())
89
        {
90
            logger->log("HomunculusDB: Redefinition of homunculus ID %d", id);
91
            currentInfo = mHomunculusInfos[fromInt(id + offset, BeingTypeId)];
92
        }
93
        if (currentInfo == nullptr)
94
            currentInfo = new BeingInfo;
95
96
        currentInfo->setBlockType(BlockType::NONE);
97
        currentInfo->setName(XML::langProperty(
98
            // TRANSLATORS: unknown info name
99
            homunculusNode, "name", _("unnamed")));
100
101
        BeingCommon::readBasicAttributes(currentInfo,
102
            homunculusNode, "attack");
103
        BeingCommon::readWalkingAttributes(currentInfo,
104
            homunculusNode,
105
            0);
106
        BeingCommon::readAiAttributes(currentInfo,
107
            homunculusNode);
108
109
        currentInfo->setMaxHP(XML::getProperty(homunculusNode, "maxHP", 0));
110
111
        currentInfo->setDeadSortOffsetY(XML::getProperty(
112
            homunculusNode, "deadSortOffsetY", 31));
113
114
        currentInfo->setColorsList(XML::getProperty(homunculusNode,
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, homunculusNode)
124
        {
125
            BeingCommon::readObjectNodes(spriteNode, display,
126
                currentInfo, "HomunculusDB");
127
        }
128
        currentInfo->setDisplay(display);
129
130
        mHomunculusInfos[fromInt(id + offset, BeingTypeId)] = currentInfo;
131
    }
132
}
133
134
void HomunculusDB::unload()
135
{
136
    logger->log1("Unloading homunculus database...");
137
    delete_all(mHomunculusInfos);
138
    mHomunculusInfos.clear();
139
140
    mLoaded = false;
141
}
142
143
BeingInfo *HomunculusDB::get(const BeingTypeId id)
144
{
145
    BeingInfoIterator i = mHomunculusInfos.find(id);
146
147
    if (i == mHomunculusInfos.end())
148
    {
149
        i = mHomunculusInfos.find(id);
150
        if (i == mHomunculusInfos.end())
151
        {
152
            reportAlways("HomunculusDB: Warning, unknown homunculus ID "
153
                "%d requested",
154
                toInt(id, int));
155
            return BeingInfo::unknown;
156
        }
157
    }
158
    return i->second;
159
4
}