ManaPlus
statuseffectdb.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  * 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 #include "statuseffect.h"
28 
29 #include "utils/checkutils.h"
30 
31 #include "resources/beingcommon.h"
32 
33 #include "debug.h"
34 
35 namespace
36 {
37  typedef std::map<int, StatusEffect *> IdToEffectMap[2];
38  bool mLoaded = false;
39  int fakeId = 10000;
45 } // namespace
46 
48  const Enable enabling)
49 {
50  std::map<int, StatusEffect *> &effects
51  = statusEffects[enabling == Enable_true];
52  const std::map<int, StatusEffect *>::iterator it = effects.find(index);
53  if (it != effects.end())
54  return (*it).second;
55  reportAlways("Missing status effect: %d",
56  index)
57  return nullptr;
58 }
59 
61 {
62  if (mLoaded)
63  unload();
64 
65  logger->log1("Initializing status effect database...");
66 
67  loadXmlFile(paths.getStringValue("statusEffectsFile"), SkipError_false);
68  loadXmlFile(paths.getStringValue("statusEffectsPatchFile"),
70  loadXmlDir("statusEffectsPatchDir", loadXmlFile)
71 
72  mLoaded = true;
73 }
74 
75 void StatusEffectDB::loadXmlFile(const std::string &fileName,
76  const SkipError skipError)
77 {
78  XML::Document doc(fileName, UseVirtFs_true, skipError);
79  XmlNodeConstPtrConst rootNode = doc.rootNode();
80 
81  if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "status-effects"))
82  {
83  logger->log("Error loading status effects file: " + fileName);
84  return;
85  }
86 
87  for_each_xml_child_node(node, rootNode)
88  {
89  if (xmlNameEqual(node, "include"))
90  {
91  const std::string incName = XML::getProperty(node, "name", "");
92  if (!incName.empty())
93  loadXmlFile(incName, skipError);
94  continue;
95  }
96  else if (!xmlNameEqual(node, "status-effect"))
97  {
98  continue;
99  }
100 
101  int id = XML::getProperty(node, "id", -1);
102  if (id == -1)
103  {
104  id = fakeId;
105  fakeId ++;
106  }
107  const int option = XML::getProperty(node, "option", 0);
108  const int opt1 = XML::getProperty(node, "opt1", 0);
109  const int opt2 = XML::getProperty(node, "opt2", 0);
110  const int opt3 = XML::getProperty(node, "opt3", 0);
111  if (option != 0)
112  {
113  optionToIdMap[option] = id;
114  }
115  if (opt1 != 0)
116  {
117  opt1ToIdMap[opt1] = id;
118  }
119  if (opt2 != 0)
120  {
121  opt2ToIdMap[opt2] = id;
122  }
123  if (opt3 != 0)
124  {
125  opt3ToIdMap[opt3] = id;
126  }
127 
128  StatusEffect *startEffect = statusEffects[1][id];
129  StatusEffect *endEffect = statusEffects[0][id];
130  const std::string name = XML::getProperty(node, "name", "");
131  const std::string name2 = XML::langProperty(node, "name", "");
132  if (startEffect == nullptr)
133  startEffect = new StatusEffect;
134  if (endEffect == nullptr)
135  endEffect = new StatusEffect;
136 
137  startEffect->mName = name2;
138  startEffect->mIsPoison =
139  (name == paths.getStringValue("poisonEffectName"));
140  startEffect->mIsCart =
141  (name == paths.getStringValue("cartEffectName"));
142  startEffect->mIsRiding =
143  (name == paths.getStringValue("ridingEffectName"));
144  startEffect->mIsTrickDead =
145  (name == paths.getStringValue("trickDeadEffectName"));
146  startEffect->mIsPostDelay =
147  (name == paths.getStringValue("postDelayName"));
148  startEffect->mMessage = XML::getProperty(
149  node, "start-message", "");
150  startEffect->mSFXEffect = XML::getProperty(
151  node, "start-audio", "");
152  startEffect->mStartParticleEffect = XML::getProperty(
153  node, "start-particle", "");
154  startEffect->mParticleEffect = XML::getProperty(
155  node, "particle", "");
156 
157  startEffect->mIcon = XML::getProperty(node, "icon", "");
158  startEffect->mAction = XML::getProperty(node, "action", "");
159  startEffect->mIsPersistent = (XML::getProperty(
160  node, "persistent-particle-effect", "no")) != "no";
161 
162  endEffect->mName = startEffect->mName;
163  endEffect->mIsPoison = startEffect->mIsPoison;
164  endEffect->mIsCart = startEffect->mIsCart;
165  endEffect->mIsRiding = startEffect->mIsRiding;
166  endEffect->mIsTrickDead = startEffect->mIsTrickDead;
167  endEffect->mIsPostDelay = startEffect->mIsPostDelay;
168  endEffect->mMessage = XML::getProperty(node, "end-message", "");
169  endEffect->mSFXEffect = XML::getProperty(node, "end-audio", "");
171  node, "end-particle", "");
172 
173  statusEffects[1][id] = startEffect;
174  statusEffects[0][id] = endEffect;
175  }
176 }
177 
178 static void unloadMap(std::map<int, StatusEffect *> &map)
179 {
180  for (std::map<int, StatusEffect *>::iterator it = map.begin();
181  it != map.end(); ++it)
182  {
183  delete (*it).second;
184  }
185 
186  map.clear();
187 }
188 
190 {
191  if (!mLoaded)
192  return;
193 
194  logger->log1("Unloading status effect database...");
195 
196  fakeId = 10000;
199 
200  optionToIdMap.clear();
201  opt1ToIdMap.clear();
202  opt2ToIdMap.clear();
203  opt3ToIdMap.clear();
204 
205  mLoaded = false;
206 }
207 
209 {
210  return optionToIdMap;
211 }
212 
214 {
215  return opt1ToIdMap;
216 }
217 
219 {
220  return opt2ToIdMap;
221 }
222 
224 {
225  return opt3ToIdMap;
226 }
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
#define reportAlways(...)
Definition: checkutils.h:253
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
std::string mIcon
Definition: statuseffect.h:81
bool mIsPostDelay
Definition: statuseffect.h:89
std::string mSFXEffect
Definition: statuseffect.h:78
std::string mMessage
Definition: statuseffect.h:77
std::string mStartParticleEffect
Definition: statuseffect.h:79
bool mIsTrickDead
Definition: statuseffect.h:88
std::string mName
Definition: statuseffect.h:83
std::string mParticleEffect
Definition: statuseffect.h:80
bool mIsPersistent
Definition: statuseffect.h:84
std::string mAction
Definition: statuseffect.h:82
xmlNodePtr rootNode()
Definition: libxml.cpp:169
Configuration paths
bool Enable
Definition: enable.h:30
const bool Enable_true
Definition: enable.h:30
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:161
Logger * logger
Definition: logger.cpp:89
void unload()
Definition: net.cpp:180
const OptionsMap & getOpt3Map()
const OptionsMap & getOpt2Map()
StatusEffect * getStatusEffect(const int index, const Enable enabling)
const OptionsMap & getOpt1Map()
void loadXmlFile(const std::string &fileName, const SkipError skipError)
const OptionsMap & getOptionMap()
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::map< int, StatusEffect * > IdToEffectMap[2]
const bool SkipError_false
Definition: skiperror.h:30
const bool SkipError_true
Definition: skiperror.h:30
bool SkipError
Definition: skiperror.h:30
static void unloadMap(std::map< int, StatusEffect * > &map)
std::map< uint32_t, uint32_t > OptionsMap
std::string fileName
Definition: testmain.cpp:39
const bool UseVirtFs_true
Definition: usevirtfs.h:30