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  *
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 
24 
25 #include "configuration.h"
26 
27 #include "resources/beingcommon.h"
28 #include "resources/beinginfo.h"
29 
30 #include "utils/checkutils.h"
31 #include "utils/dtor.h"
32 
33 #include "debug.h"
34 
35 namespace
36 {
38  bool mLoaded = false;
39 } // namespace
40 
42 {
43  if (mLoaded)
44  unload();
45 
46  logger->log1("Initializing elemental database...");
47  loadXmlFile(paths.getStringValue("elementalsFile"), SkipError_false);
48  loadXmlFile(paths.getStringValue("elementalsPatchFile"), SkipError_true);
49  loadXmlDir("elementalsPatchDir", loadXmlFile)
50 
51  mLoaded = true;
52 }
53 
54 void ElementalDb::loadXmlFile(const std::string &fileName,
55  const SkipError skipError)
56 {
57  XML::Document doc(fileName, UseVirtFs_true, skipError);
58  XmlNodeConstPtr rootNode = doc.rootNode();
59 
60  if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "elementals"))
61  {
62  logger->log("Elemental Database: Error while loading %s!",
63  paths.getStringValue("elementalsFile").c_str());
64  mLoaded = true;
65  return;
66  }
67 
68  const int offset = XML::getProperty(rootNode, "offset", 0);
69 
70  // iterate <elemental>s
71  for_each_xml_child_node(elementalNode, rootNode)
72  {
73  if (xmlNameEqual(elementalNode, "include"))
74  {
75  const std::string name = XML::getProperty(
76  elementalNode, "name", "");
77  if (!name.empty())
78  loadXmlFile(name, skipError);
79  continue;
80  }
81  if (!xmlNameEqual(elementalNode, "elemental"))
82  continue;
83 
84  const int id = XML::getProperty(elementalNode, "id", 0);
85  BeingInfo *currentInfo = nullptr;
86  if (mElementalInfos.find(fromInt(id + offset, BeingTypeId))
87  != mElementalInfos.end())
88  {
89  logger->log("ElementalDb: Redefinition of elemental ID %d", id);
90  currentInfo = mElementalInfos[fromInt(id + offset, BeingTypeId)];
91  }
92  if (currentInfo == nullptr)
93  currentInfo = new BeingInfo;
94 
95  currentInfo->setBlockType(BlockType::NONE);
97  elementalNode, "attack");
99  elementalNode,
100  0);
101 
102  currentInfo->setMaxHP(XML::getProperty(elementalNode, "maxHP", 0));
103 
105  elementalNode, "deadSortOffsetY", 31));
106 
107  currentInfo->setColorsList(XML::getProperty(elementalNode,
108  "colors", ""));
109 
110  if (currentInfo->getMaxHP() != 0)
111  currentInfo->setStaticMaxHP(true);
112 
113  SpriteDisplay display;
114 
115  // iterate <sprite>s and <sound>s
116  for_each_xml_child_node(spriteNode, elementalNode)
117  {
118  BeingCommon::readObjectNodes(spriteNode, display,
119  currentInfo, "ElementalDb");
120  }
121  currentInfo->setDisplay(display);
122 
123  mElementalInfos[fromInt(id + offset, BeingTypeId)] = currentInfo;
124  }
125 }
126 
128 {
129  logger->log1("Unloading elemental database...");
131  mElementalInfos.clear();
132 
133  mLoaded = false;
134 }
135 
136 
138 {
139  BeingInfoIterator i = mElementalInfos.find(id);
140 
141  if (i == mElementalInfos.end())
142  {
143  i = mElementalInfos.find(id);
144  if (i == mElementalInfos.end())
145  {
146  reportAlways("ElementalDb: Warning, unknown elemental ID "
147  "%d requested",
148  toInt(id, int))
149  return BeingInfo::unknown;
150  }
151  }
152  return i->second;
153 }
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
anonymous_namespace{elementaldb.cpp}::mElementalInfos
BeingInfos mElementalInfos
Definition: elementaldb.cpp:37
BeingTypeId
int BeingTypeId
Definition: beingtypeid.h:29
paths
Configuration paths
Definition: configuration.cpp:55
SkipError_false
const bool SkipError_false
Definition: skiperror.h:29
elementaldb.h
XML::Document
Definition: libxml.h:52
toInt
#define toInt(val, name)
Definition: intdefines.h:46
XML::getProperty
int getProperty(const xmlNodePtr node, const char *const name, int def)
Definition: libxml.cpp:173
ElementalDb::loadXmlFile
void loadXmlFile(const std::string &fileName, const SkipError skipError)
Definition: elementaldb.cpp:54
BeingInfo::setColorsList
void setColorsList(const std::string &name)
Definition: beinginfo.cpp:233
ElementalDb::load
void load()
Definition: elementaldb.cpp:41
logger
Logger * logger
Definition: logger.cpp:88
fileName
std::string fileName
Definition: testmain.cpp:38
loadXmlDir
#define loadXmlDir(name, function)
Definition: beingcommon.h:38
checkutils.h
beingcommon.h
ElementalDb::unload
void unload()
Definition: elementaldb.cpp:127
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
Configuration::getStringValue
std::string getStringValue(const std::string &key) const
Definition: configuration.cpp:487
SpriteDisplay
Definition: spritedisplay.h:32
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
delete_all
void delete_all(Container &c)
Definition: dtor.h:55
BeingInfo::setDeadSortOffsetY
void setDeadSortOffsetY(const int n)
Definition: beinginfo.h:194
beinginfo.h
anonymous_namespace{elementaldb.cpp}::mLoaded
bool mLoaded
Definition: elementaldb.cpp:38
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
BeingInfo::setStaticMaxHP
void setStaticMaxHP(const bool n)
Definition: beinginfo.h:176
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
ElementalDb::get
BeingInfo * get(const BeingTypeId id)
Definition: elementaldb.cpp:137