ManaPlus
mercenarydb.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 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 {
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 
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 
132 {
133  logger->log1("Unloading mercenary database...");
135  mMercenaryInfos.clear();
136 
137  mLoaded = false;
138 }
139 
140 
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 }
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
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
BeingTypeId
int BeingTypeId
Definition: beingtypeid.h:29
paths
Configuration paths
Definition: configuration.cpp:55
SkipError_false
const bool SkipError_false
Definition: skiperror.h:29
MercenaryDB::loadXmlFile
void loadXmlFile(const std::string &fileName, const SkipError skipError)
Definition: mercenarydb.cpp:55
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
anonymous_namespace{mercenarydb.cpp}::mLoaded
bool mLoaded
Definition: mercenarydb.cpp:39
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
MercenaryDB::load
void load()
Definition: mercenarydb.cpp:42
checkutils.h
MercenaryDB::get
BeingInfo * get(const BeingTypeId id)
Definition: mercenarydb.cpp:141
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
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
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
MercenaryDB::unload
void unload()
Definition: mercenarydb.cpp:131
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
anonymous_namespace{mercenarydb.cpp}::mMercenaryInfos
BeingInfos mMercenaryInfos
Definition: mercenarydb.cpp:38
mercenarydb.h
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
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