ManaPlus
Functions
ElementalDb Namespace Reference

Functions

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

Detailed Description

Elemental information database.

Function Documentation

◆ get()

BeingInfo * ElementalDb::get ( const BeingTypeId  id)

Definition at line 138 of file elementaldb.cpp.

139 {
140  BeingInfoIterator i = mElementalInfos.find(id);
141 
142  if (i == mElementalInfos.end())
143  {
144  i = mElementalInfos.find(id);
145  if (i == mElementalInfos.end())
146  {
147  reportAlways("ElementalDb: Warning, unknown elemental ID "
148  "%d requested",
149  toInt(id, int))
150  return BeingInfo::unknown;
151  }
152  }
153  return i->second;
154 }
BeingInfos::iterator BeingInfoIterator
Definition: beinginfo.h:410
#define reportAlways(...)
Definition: checkutils.h:253
static BeingInfo * unknown
Definition: beinginfo.h:56
#define toInt(val, name)
Definition: intdefines.h:47

References anonymous_namespace{elementaldb.cpp}::mElementalInfos, reportAlways, toInt, and BeingInfo::unknown.

Referenced by Being::setSubtype().

◆ load()

void ElementalDb::load ( )

Definition at line 42 of file elementaldb.cpp.

43 {
44  if (mLoaded)
45  unload();
46 
47  logger->log1("Initializing elemental database...");
48  loadXmlFile(paths.getStringValue("elementalsFile"), SkipError_false);
49  loadXmlFile(paths.getStringValue("elementalsPatchFile"), SkipError_true);
50  loadXmlDir("elementalsPatchDir", loadXmlFile)
51 
52  mLoaded = true;
53 }
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{elementaldb.cpp}::mLoaded, paths, SkipError_false, SkipError_true, and Net::unload().

Referenced by DbManager::loadDb().

◆ loadXmlFile()

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

Definition at line 55 of file elementaldb.cpp.

57 {
58  XML::Document doc(fileName, UseVirtFs_true, skipError);
59  XmlNodeConstPtr rootNode = doc.rootNode();
60 
61  if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "elementals"))
62  {
63  logger->log("Elemental Database: Error while loading %s!",
64  paths.getStringValue("elementalsFile").c_str());
65  mLoaded = true;
66  return;
67  }
68 
69  const int offset = XML::getProperty(rootNode, "offset", 0);
70 
71  // iterate <elemental>s
72  for_each_xml_child_node(elementalNode, rootNode)
73  {
74  if (xmlNameEqual(elementalNode, "include"))
75  {
76  const std::string name = XML::getProperty(
77  elementalNode, "name", "");
78  if (!name.empty())
79  loadXmlFile(name, skipError);
80  continue;
81  }
82  if (!xmlNameEqual(elementalNode, "elemental"))
83  continue;
84 
85  const int id = XML::getProperty(elementalNode, "id", 0);
86  BeingInfo *currentInfo = nullptr;
87  if (mElementalInfos.find(fromInt(id + offset, BeingTypeId))
88  != mElementalInfos.end())
89  {
90  logger->log("ElementalDb: Redefinition of elemental ID %d", id);
91  currentInfo = mElementalInfos[fromInt(id + offset, BeingTypeId)];
92  }
93  if (currentInfo == nullptr)
94  currentInfo = new BeingInfo;
95 
96  currentInfo->setBlockType(BlockType::NONE);
98  elementalNode, "attack");
100  elementalNode,
101  0);
102 
103  currentInfo->setMaxHP(XML::getProperty(elementalNode, "maxHP", 0));
104 
106  elementalNode, "deadSortOffsetY", 31));
107 
108  currentInfo->setColorsList(XML::getProperty(elementalNode,
109  "colors", ""));
110 
111  if (currentInfo->getMaxHP() != 0)
112  currentInfo->setStaticMaxHP(true);
113 
114  SpriteDisplay display;
115 
116  // iterate <sprite>s and <sound>s
117  for_each_xml_child_node(spriteNode, elementalNode)
118  {
119  BeingCommon::readObjectNodes(spriteNode, display,
120  currentInfo, "ElementalDb");
121  }
122  currentInfo->setDisplay(display);
123 
124  mElementalInfos[fromInt(id + offset, BeingTypeId)] = currentInfo;
125  }
126 }
int BeingTypeId
Definition: beingtypeid.h:30
void setBlockType(const BlockTypeT &blockType)
Definition: beinginfo.h:126
void setColorsList(const std::string &name)
Definition: beinginfo.cpp:234
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 fromInt(val, name)
Definition: intdefines.h:46
#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
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(), loadXmlFile(), Logger::log(), logger, anonymous_namespace{elementaldb.cpp}::mElementalInfos, anonymous_namespace{elementaldb.cpp}::mLoaded, BlockType::NONE, paths, BeingCommon::readBasicAttributes(), BeingCommon::readObjectNodes(), BeingCommon::readWalkingAttributes(), XML::Document::rootNode(), BeingInfo::setBlockType(), BeingInfo::setColorsList(), BeingInfo::setDeadSortOffsetY(), BeingInfo::setDisplay(), BeingInfo::setMaxHP(), BeingInfo::setStaticMaxHP(), and UseVirtFs_true.

◆ unload()

void ElementalDb::unload ( )

Definition at line 128 of file elementaldb.cpp.

129 {
130  logger->log1("Unloading elemental database...");
132  mElementalInfos.clear();
133 
134  mLoaded = false;
135 }
void delete_all(Container &c)
Definition: dtor.h:56

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

Referenced by DbManager::unloadDb().