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