ManaPlus
Functions
MonsterDB Namespace Reference

Functions

void load ()
 
void unload ()
 
void loadXmlFile (const std::string &fileName, const SkipError skipError)
 
BeingInfoget (const BeingTypeId id)
 

Detailed Description

Monster information database.

Function Documentation

◆ get()

BeingInfo * MonsterDB::get ( const BeingTypeId  id)

Definition at line 145 of file monsterdb.cpp.

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 }
BeingInfos::iterator BeingInfoIterator
Definition: beinginfo.h:410
int BeingTypeId
Definition: beingtypeid.h:30
#define reportAlways(...)
Definition: checkutils.h:253
static BeingInfo * unknown
Definition: beinginfo.h:56
#define toInt(val, name)
Definition: intdefines.h:47
#define fromInt(val, name)
Definition: intdefines.h:46
static const unsigned int OLD_TMWATHENA_OFFSET
Definition: monsterdb.cpp:39

References fromInt, anonymous_namespace{monsterdb.cpp}::mMonsterInfos, OLD_TMWATHENA_OFFSET, reportAlways, toInt, and BeingInfo::unknown.

Referenced by BrowserBoxTools::replaceLinkCommands(), and Being::setSubtype().

◆ load()

void MonsterDB::load ( )

Definition at line 47 of file monsterdb.cpp.

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 }
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
std::string getStringValue(const std::string &key) const
void log1(const char *const log_text)
Definition: logger.cpp:238
Configuration paths
Logger * logger
Definition: logger.cpp:89
void unload()
Definition: net.cpp:180
const bool SkipError_false
Definition: skiperror.h:30
const bool SkipError_true
Definition: skiperror.h:30

References Configuration::getStringValue(), loadXmlDir, loadXmlFile(), Logger::log1(), logger, anonymous_namespace{monsterdb.cpp}::mLoaded, paths, SkipError_false, SkipError_true, and Net::unload().

Referenced by DbManager::loadDb().

◆ loadXmlFile()

void MonsterDB::loadXmlFile ( const std::string &  fileName,
const SkipError  skipError 
)

Definition at line 60 of file monsterdb.cpp.

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 }
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
void setMaxHP(const int n)
Definition: beinginfo.h:168
void log(const char *const log_text,...)
Definition: logger.cpp:269
#define _(s)
Definition: gettext.h:35
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:161
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
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
std::string fileName
Definition: testmain.cpp:39
const bool UseVirtFs_true
Definition: usevirtfs.h:30

References _, fileName, for_each_xml_child_node, fromInt, BeingInfo::getMaxHP(), XML::getProperty(), Configuration::getStringValue(), XML::langProperty(), loadXmlFile(), Logger::log(), logger, anonymous_namespace{monsterdb.cpp}::mLoaded, anonymous_namespace{monsterdb.cpp}::mMonsterInfos, BlockMask::MONSTERWALL, BlockType::NONE, OLD_TMWATHENA_OFFSET, paths, BeingCommon::readBasicAttributes(), BeingCommon::readObjectNodes(), BeingCommon::readWalkingAttributes(), XML::Document::rootNode(), BeingInfo::setBlockType(), BeingInfo::setColorsList(), BeingInfo::setDeadSortOffsetY(), BeingInfo::setDisplay(), BeingInfo::setMaxHP(), BeingInfo::setName(), BeingInfo::setStaticMaxHP(), and UseVirtFs_true.

◆ unload()

void MonsterDB::unload ( )

Definition at line 135 of file monsterdb.cpp.

136 {
137  logger->log1("Unloading monster database...");
139  mMonsterInfos.clear();
140 
141  mLoaded = false;
142 }
void delete_all(Container &c)
Definition: dtor.h:56

References delete_all(), Logger::log1(), logger, anonymous_namespace{monsterdb.cpp}::mLoaded, and anonymous_namespace{monsterdb.cpp}::mMonsterInfos.

Referenced by DbManager::unloadDb().