ManaPlus
homunculusdb.cpp
Go to the documentation of this file.
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  *
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 
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 {
39  bool mLoaded = false;
40 } // namespace
41 
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 
102  homunculusNode, "attack");
104  homunculusNode,
105  0);
106  BeingCommon::readAiAttributes(currentInfo,
107  homunculusNode);
108 
109  currentInfo->setMaxHP(XML::getProperty(homunculusNode, "maxHP", 0));
110 
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 
135 {
136  logger->log1("Unloading homunculus database...");
138  mHomunculusInfos.clear();
139 
140  mLoaded = false;
141 }
142 
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 }
SkipError
bool SkipError
Definition: skiperror.h:29
BeingInfos
std::map< BeingTypeId, BeingInfo * > BeingInfos
Definition: beinginfo.h:408
BlockType::NONE
@ NONE
Definition: blocktype.h:30
BeingInfo::setDisplay
void setDisplay(const SpriteDisplay &display)
Definition: beinginfo.cpp:126
reportAlways
#define reportAlways(...)
Definition: checkutils.h:252
HomunculusDB::loadXmlFile
void loadXmlFile(const std::string &fileName, const SkipError skipError)
Definition: homunculusdb.cpp:55
SkipError_true
const bool SkipError_true
Definition: skiperror.h:29
BeingInfo::setMaxHP
void setMaxHP(const int n)
Definition: beinginfo.h:167
BeingInfo
Definition: beinginfo.h:52
Net::unload
void unload()
Definition: net.cpp:179
BeingCommon::readAiAttributes
void readAiAttributes(BeingInfo *const info, xmlNode *const node)
Definition: beingcommon.cpp:95
HomunculusDB::load
void load()
Definition: homunculusdb.cpp:42
BeingTypeId
int BeingTypeId
Definition: beingtypeid.h:29
paths
Configuration paths
Definition: configuration.cpp:55
SkipError_false
const bool SkipError_false
Definition: skiperror.h:29
HomunculusDB::unload
void unload()
Definition: homunculusdb.cpp:134
XML::Document
Definition: libxml.h:52
toInt
#define toInt(val, name)
Definition: intdefines.h:46
XML::getProperty
int getProperty(const xmlNodePtr node, const char *const name, int def)
Definition: libxml.cpp:173
BeingInfo::setColorsList
void setColorsList(const std::string &name)
Definition: beinginfo.cpp:233
logger
Logger * logger
Definition: logger.cpp:88
fileName
std::string fileName
Definition: testmain.cpp:38
loadXmlDir
#define loadXmlDir(name, function)
Definition: beingcommon.h:38
anonymous_namespace{homunculusdb.cpp}::mHomunculusInfos
BeingInfos mHomunculusInfos
Definition: homunculusdb.cpp:38
checkutils.h
beingcommon.h
gettext.h
loadXmlFile
static void loadXmlFile(const std::string &file, const std::string &name, BadgesInfos &arr, const SkipError skipError)
Definition: badgesdb.cpp:42
UseVirtFs_true
const bool UseVirtFs_true
Definition: usevirtfs.h:29
BeingInfo::getMaxHP
int getMaxHP() const
Definition: beinginfo.h:170
Configuration::getStringValue
std::string getStringValue(const std::string &key) const
Definition: configuration.cpp:487
HomunculusDB::get
BeingInfo * get(const BeingTypeId id)
Definition: homunculusdb.cpp:143
SpriteDisplay
Definition: spritedisplay.h:32
XML::langProperty
std::string langProperty(const xmlNodePtr node, const char *const name, const std::string &def)
Definition: libxml.cpp:257
BeingCommon::readBasicAttributes
void readBasicAttributes(BeingInfo *const info, xmlNode *const node, const std::string &hoverCursor)
Definition: beingcommon.cpp:36
homunculusdb.h
Logger::log1
void log1(const char *const log_text)
Definition: logger.cpp:237
delete_all
void delete_all(Container &c)
Definition: dtor.h:55
BeingInfo::setDeadSortOffsetY
void setDeadSortOffsetY(const int n)
Definition: beinginfo.h:194
beinginfo.h
configuration.h
BeingInfoIterator
BeingInfos::iterator BeingInfoIterator
Definition: beinginfo.h:409
fromInt
#define fromInt(val, name)
Definition: intdefines.h:45
BeingCommon::readObjectNodes
bool readObjectNodes(xmlNode *const &spriteNode, SpriteDisplay &display, BeingInfo *const currentInfo, const std::string &dbName)
Definition: beingcommon.cpp:139
XML::Document::rootNode
xmlNodePtr rootNode()
Definition: libxml.cpp:168
BeingCommon::readWalkingAttributes
void readWalkingAttributes(BeingInfo *const info, xmlNode *const node, const int moreBlockFlags)
Definition: beingcommon.cpp:59
dtor.h
BeingInfo::unknown
static BeingInfo * unknown
Definition: beinginfo.h:55
for_each_xml_child_node
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:160
_
#define _(s)
Definition: gettext.h:34
BeingInfo::setStaticMaxHP
void setStaticMaxHP(const bool n)
Definition: beinginfo.h:176
BeingInfo::setName
void setName(const std::string &name)
Definition: beinginfo.h:64
anonymous_namespace{homunculusdb.cpp}::mLoaded
bool mLoaded
Definition: homunculusdb.cpp:39
BeingInfo::setBlockType
void setBlockType(const BlockTypeT &blockType)
Definition: beinginfo.h:125
Logger::log
void log(const char *const log_text,...)
Definition: logger.cpp:268
debug.h