ManaPlus
elementaldb.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 "configuration.h"
27 
28 #include "resources/beingcommon.h"
29 #include "resources/beinginfo.h"
30 
31 #include "utils/checkutils.h"
32 #include "utils/dtor.h"
33 
34 #include "debug.h"
35 
36 namespace
37 {
39  bool mLoaded = false;
40 } // namespace
41 
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 }
54 
55 void ElementalDb::loadXmlFile(const std::string &fileName,
56  const SkipError skipError)
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 }
127 
129 {
130  logger->log1("Unloading elemental database...");
132  mElementalInfos.clear();
133 
134  mLoaded = false;
135 }
136 
137 
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 }
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 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 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 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: elementaldb.cpp:55
void unload()
Definition: net.cpp:180
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