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