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  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
25 
26 #include "resources/beingcommon.h"
27 #include "resources/beinginfo.h"
28 
29 #include "utils/checkutils.h"
30 #include "utils/dtor.h"
31 #include "utils/gettext.h"
32 
33 #include "configuration.h"
34 
35 #include "debug.h"
36 
37 namespace
38 {
40  bool mLoaded = false;
41 } // namespace
42 
44 {
45  if (mLoaded)
46  unload();
47 
48  logger->log1("Initializing mercenary database...");
49  loadXmlFile(paths.getStringValue("mercenariesFile"), SkipError_false);
50  loadXmlFile(paths.getStringValue("mercenariesPatchFile"), SkipError_true);
51  loadXmlDir("mercenariesPatchDir", loadXmlFile)
52 
53  mLoaded = true;
54 }
55 
56 void MercenaryDB::loadXmlFile(const std::string &fileName,
57  const SkipError skipError)
58 {
61  skipError);
62  XmlNodeConstPtr rootNode = doc.rootNode();
63 
64  if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "mercenaries"))
65  {
66  logger->log("MercenaryDB: Error while loading %s!",
67  paths.getStringValue("mercenariesFile").c_str());
68  mLoaded = true;
69  return;
70  }
71 
72  const int offset = XML::getProperty(rootNode, "offset", 0);
73 
74  // iterate <mercenary>s
75  for_each_xml_child_node(mercenaryNode, rootNode)
76  {
77  if (xmlNameEqual(mercenaryNode, "include"))
78  {
79  const std::string name = XML::getProperty(
80  mercenaryNode, "name", "");
81  if (!name.empty())
82  loadXmlFile(name, skipError);
83  continue;
84  }
85  if (!xmlNameEqual(mercenaryNode, "mercenary"))
86  continue;
87 
88  const int id = XML::getProperty(mercenaryNode, "id", 0);
89  BeingInfo *currentInfo = nullptr;
90  if (mMercenaryInfos.find(fromInt(id + offset, BeingTypeId))
91  != mMercenaryInfos.end())
92  {
93  logger->log("MercenaryDB: Redefinition of mercenary ID %d", id);
94  currentInfo = mMercenaryInfos[fromInt(id + offset, BeingTypeId)];
95  }
96  if (currentInfo == nullptr)
97  currentInfo = new BeingInfo;
98 
99  currentInfo->setBlockType(BlockType::NONE);
100  currentInfo->setName(XML::langProperty(
101  // TRANSLATORS: unknown info name
102  mercenaryNode, "name", _("unnamed")));
103  BeingCommon::readBasicAttributes(currentInfo, mercenaryNode, "attack");
104  BeingCommon::readWalkingAttributes(currentInfo, mercenaryNode, 0);
105  BeingCommon::readAiAttributes(currentInfo, mercenaryNode);
106 
107  currentInfo->setMaxHP(XML::getProperty(mercenaryNode, "maxHP", 0));
108 
110  mercenaryNode, "deadSortOffsetY", 31));
111 
112  currentInfo->setColorsList(XML::getProperty(mercenaryNode,
113  "colors", ""));
114 
115  if (currentInfo->getMaxHP() != 0)
116  currentInfo->setStaticMaxHP(true);
117 
118  SpriteDisplay display;
119 
120  // iterate <sprite>s and <sound>s
121  for_each_xml_child_node(spriteNode, mercenaryNode)
122  {
123  BeingCommon::readObjectNodes(spriteNode, display,
124  currentInfo, "MonsterDB");
125  }
126  currentInfo->setDisplay(display);
127 
128  mMercenaryInfos[fromInt(id + offset, BeingTypeId)] = currentInfo;
129  }
130 }
131 
133 {
134  logger->log1("Unloading mercenary database...");
136  mMercenaryInfos.clear();
137 
138  mLoaded = false;
139 }
140 
141 
143 {
144  BeingInfoIterator i = mMercenaryInfos.find(id);
145 
146  if (i == mMercenaryInfos.end())
147  {
148  i = mMercenaryInfos.find(id);
149  if (i == mMercenaryInfos.end())
150  {
151  reportAlways("MercenaryDB: Warning, unknown mercenary ID "
152  "%d requested",
153  toInt(id, int))
154  return BeingInfo::unknown;
155  }
156  }
157  return i->second;
158 }
static void loadXmlFile(const std::string &file, const std::string &name, BadgesInfos &arr, const SkipError skipError)
Definition: badgesdb.cpp:43
#define loadXmlDir(name, function)
Definition: beingcommon.h:39
BeingInfos::iterator BeingInfoIterator
Definition: beinginfo.h:410
std::map< BeingTypeId, BeingInfo * > BeingInfos
Definition: beinginfo.h:409
int BeingTypeId
Definition: beingtypeid.h:30
#define reportAlways(...)
Definition: checkutils.h:253
void setBlockType(const BlockTypeT &blockType)
Definition: beinginfo.h:126
void setColorsList(const std::string &name)
Definition: beinginfo.cpp:234
void setName(const std::string &name)
Definition: beinginfo.h:65
void setDisplay(const SpriteDisplay &display)
Definition: beinginfo.cpp:127
void setDeadSortOffsetY(const int n)
Definition: beinginfo.h:195
void setStaticMaxHP(const bool n)
Definition: beinginfo.h:177
int getMaxHP() const
Definition: beinginfo.h:171
static BeingInfo * unknown
Definition: beinginfo.h:56
void setMaxHP(const int n)
Definition: beinginfo.h:168
std::string getStringValue(const std::string &key) const
void log(const char *const log_text,...)
Definition: logger.cpp:269
void log1(const char *const log_text)
Definition: logger.cpp:238
xmlNodePtr rootNode()
Definition: libxml.cpp:169
Configuration paths
void delete_all(Container &c)
Definition: dtor.h:56
#define _(s)
Definition: gettext.h:35
#define toInt(val, name)
Definition: intdefines.h:47
#define fromInt(val, name)
Definition: intdefines.h:46
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:161
Logger * logger
Definition: logger.cpp:89
bool readObjectNodes(xmlNode *const &spriteNode, SpriteDisplay &display, BeingInfo *const currentInfo, const std::string &dbName)
void readAiAttributes(BeingInfo *const info, xmlNode *const node)
Definition: beingcommon.cpp:96
void readBasicAttributes(BeingInfo *const info, xmlNode *const node, const std::string &hoverCursor)
Definition: beingcommon.cpp:37
void readWalkingAttributes(BeingInfo *const info, xmlNode *const node, const int moreBlockFlags)
Definition: beingcommon.cpp:60
BeingInfo * get(const BeingTypeId id)
void loadXmlFile(const std::string &fileName, const SkipError skipError)
Definition: mercenarydb.cpp:56
void unload()
Definition: net.cpp:180
std::string langProperty(const xmlNodePtr node, const char *const name, const std::string &def)
Definition: libxml.cpp:258
int getProperty(const xmlNodePtr node, const char *const name, int def)
Definition: libxml.cpp:174
const bool SkipError_false
Definition: skiperror.h:30
const bool SkipError_true
Definition: skiperror.h:30
bool SkipError
Definition: skiperror.h:30
std::string fileName
Definition: testmain.cpp:39
const bool UseVirtFs_true
Definition: usevirtfs.h:30