ManaPlus
skillunitdb.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2008-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 
31 
32 #include "utils/checkutils.h"
33 #include "utils/dtor.h"
34 #include "utils/gettext.h"
35 
36 #include "debug.h"
37 
38 namespace
39 {
41  bool mLoaded = false;
42 } // namespace
43 
45 {
46  if (mLoaded)
47  unload();
48 
49  logger->log1("Initializing skill unit database...");
50  loadXmlFile(paths.getStringValue("skillUnitsFile"), SkipError_false);
51  loadXmlFile(paths.getStringValue("skillUnitsPatchFile"), SkipError_true);
52  loadXmlDir("skillUnitsPatchDir", loadXmlFile)
53  mLoaded = true;
54 }
55 
56 void SkillUnitDb::loadXmlFile(const std::string &fileName,
57  const SkipError skipError)
58 {
59  XML::Document doc(fileName, UseVirtFs_true, skipError);
60  XmlNodeConstPtrConst rootNode = doc.rootNode();
61 
62  if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "skillunits"))
63  {
64  logger->log("SkillUnitDb: Error while loading %s!",
65  fileName.c_str());
66  return;
67  }
68 
69  // iterate <skillunit>s
70  for_each_xml_child_node(skillUnitNode, rootNode)
71  {
72  if (xmlNameEqual(skillUnitNode, "include"))
73  {
74  const std::string name = XML::getProperty(skillUnitNode,
75  "name", "");
76  if (!name.empty())
77  loadXmlFile(name, skipError);
78  continue;
79  }
80  else if (!xmlNameEqual(skillUnitNode, "skillunit"))
81  {
82  continue;
83  }
84 
86  skillUnitNode, "id", -1), BeingTypeId);
87  if (id == BeingTypeId_negOne)
88  {
89  reportAlways("SkillUnitDb: skill unit with missing ID in %s!",
90  paths.getStringValue("skillUnitsFile").c_str())
91  continue;
92  }
93 
94  BeingInfo *currentInfo = nullptr;
95  if (mSkillUnitInfos.find(id) != mSkillUnitInfos.end())
96  currentInfo = mSkillUnitInfos[id];
97  if (currentInfo == nullptr)
98  currentInfo = new BeingInfo;
99 
100  currentInfo->setName(XML::langProperty(skillUnitNode,
101  // TRANSLATORS: unknown info name
102  "name", _("skill")));
103 
104  currentInfo->setTargetSelection(XML::getBoolProperty(skillUnitNode,
105  "targetSelection", true));
106 
107  BeingCommon::readBasicAttributes(currentInfo, skillUnitNode, "attack");
108  BeingCommon::readWalkingAttributes(currentInfo, skillUnitNode, 0);
109 
110  currentInfo->setDeadSortOffsetY(XML::getProperty(skillUnitNode,
111  "deadSortOffsetY", 31));
112 
113  SpriteDisplay display;
114  for_each_xml_child_node(spriteNode, skillUnitNode)
115  {
116  if (!XmlHaveChildContent(spriteNode))
117  continue;
118 
119  if (xmlNameEqual(spriteNode, "sprite"))
120  {
121  SpriteReference *const currentSprite = new SpriteReference;
122  currentSprite->sprite = XmlChildContent(spriteNode);
123  currentSprite->variant =
124  XML::getProperty(spriteNode, "variant", 0);
125  display.sprites.push_back(currentSprite);
126  }
127  else if (xmlNameEqual(spriteNode, "particlefx"))
128  {
129  std::string particlefx = XmlChildContent(spriteNode);
130  display.particles.push_back(particlefx);
131  }
132  }
133 
134  currentInfo->setDisplay(display);
135 
136  mSkillUnitInfos[id] = currentInfo;
137  }
138 }
139 
141 {
142  logger->log1("Unloading skill unit database...");
144  mSkillUnitInfos.clear();
145 
146  mLoaded = false;
147 }
148 
150 {
151  const BeingInfoIterator i = mSkillUnitInfos.find(id);
152 
153  if (i == mSkillUnitInfos.end())
154  {
155  reportAlways("SkillUnitDb: Warning, unknown skill unit id "
156  "%d requested",
157  toInt(id, int))
158  return BeingInfo::unknown;
159  }
160  return i->second;
161 }
SkipError
bool SkipError
Definition: skiperror.h:29
BeingInfos
std::map< BeingTypeId, BeingInfo * > BeingInfos
Definition: beinginfo.h:408
BeingInfo::setDisplay
void setDisplay(const SpriteDisplay &display)
Definition: beinginfo.cpp:126
reportAlways
#define reportAlways(...)
Definition: checkutils.h:252
skillunitdb.h
SkillUnitDb::get
BeingInfo * get(const BeingTypeId id)
Definition: skillunitdb.cpp:149
SkipError_true
const bool SkipError_true
Definition: skiperror.h:29
BeingInfo
Definition: beinginfo.h:52
Net::unload
void unload()
Definition: net.cpp:179
SpriteDisplay::sprites
std::vector< SpriteReference * > sprites
Definition: spritedisplay.h:46
anonymous_namespace{skillunitdb.cpp}::mLoaded
bool mLoaded
Definition: skillunitdb.cpp:41
BeingTypeId
int BeingTypeId
Definition: beingtypeid.h:29
paths
Configuration paths
Definition: configuration.cpp:55
SkipError_false
const bool SkipError_false
Definition: skiperror.h:29
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
logger
Logger * logger
Definition: logger.cpp:88
SkillUnitDb::loadXmlFile
void loadXmlFile(const std::string &fileName, const SkipError skipError)
Definition: skillunitdb.cpp:56
fileName
std::string fileName
Definition: testmain.cpp:38
SpriteReference
Definition: spritereference.h:30
XML::getBoolProperty
bool getBoolProperty(const xmlNodePtr node, const char *const name, const bool def)
Definition: libxml.cpp:268
anonymous_namespace{skillunitdb.cpp}::mSkillUnitInfos
BeingInfos mSkillUnitInfos
Definition: skillunitdb.cpp:40
loadXmlDir
#define loadXmlDir(name, function)
Definition: beingcommon.h:38
checkutils.h
BeingInfo::setTargetSelection
void setTargetSelection(const bool n)
Definition: beinginfo.h:179
beingcommon.h
BeingTypeId_negOne
const BeingTypeId BeingTypeId_negOne
Definition: beingtypeid.h:30
gettext.h
SkillUnitDb::load
void load()
Definition: skillunitdb.cpp:44
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
Configuration::getStringValue
std::string getStringValue(const std::string &key) const
Definition: configuration.cpp:487
spritereference.h
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
SpriteDisplay::particles
StringVect particles
Definition: spritedisplay.h:47
delete_all
void delete_all(Container &c)
Definition: dtor.h:55
BeingInfo::setDeadSortOffsetY
void setDeadSortOffsetY(const int n)
Definition: beinginfo.h:194
beinginfo.h
configuration.h
SpriteReference::variant
int variant
Definition: spritereference.h:48
BeingInfoIterator
BeingInfos::iterator BeingInfoIterator
Definition: beinginfo.h:409
fromInt
#define fromInt(val, name)
Definition: intdefines.h:45
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
SpriteReference::sprite
std::string sprite
Definition: spritereference.h:47
_
#define _(s)
Definition: gettext.h:34
SkillUnitDb::unload
void unload()
Definition: skillunitdb.cpp:140
BeingInfo::setName
void setName(const std::string &name)
Definition: beinginfo.h:64
Logger::log
void log(const char *const log_text,...)
Definition: logger.cpp:268
debug.h