ManaPlus
Functions
HomunculusDB Namespace Reference

Functions

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

Detailed Description

Homunculus information database.

Function Documentation

◆ get()

BeingInfo * HomunculusDB::get ( const BeingTypeId  id)

Definition at line 144 of file homunculusdb.cpp.

145 {
146  BeingInfoIterator i = mHomunculusInfos.find(id);
147 
148  if (i == mHomunculusInfos.end())
149  {
150  i = mHomunculusInfos.find(id);
151  if (i == mHomunculusInfos.end())
152  {
153  reportAlways("HomunculusDB: Warning, unknown homunculus ID "
154  "%d requested",
155  toInt(id, int))
156  return BeingInfo::unknown;
157  }
158  }
159  return i->second;
160 }
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{homunculusdb.cpp}::mHomunculusInfos, reportAlways, toInt, and BeingInfo::unknown.

Referenced by BrowserBoxTools::replaceLinkCommands(), and Being::setSubtype().

◆ load()

void HomunculusDB::load ( )

Definition at line 43 of file homunculusdb.cpp.

44 {
45  if (mLoaded)
46  unload();
47 
48  logger->log1("Initializing homunculus database...");
49  loadXmlFile(paths.getStringValue("homunculusesFile"), SkipError_false);
50  loadXmlFile(paths.getStringValue("homunculusesPatchFile"), SkipError_true);
51  loadXmlDir("homunculusesPatchDir", loadXmlFile)
52 
53  mLoaded = true;
54 }
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{homunculusdb.cpp}::mLoaded, paths, SkipError_false, SkipError_true, and Net::unload().

Referenced by DbManager::loadDb().

◆ loadXmlFile()

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

Definition at line 56 of file homunculusdb.cpp.

58 {
59  XML::Document doc(fileName, UseVirtFs_true, skipError);
60  XmlNodeConstPtr rootNode = doc.rootNode();
61 
62  if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "homunculuses"))
63  {
64  logger->log("Homunculus Database: Error while loading %s!",
65  paths.getStringValue("homunculusesFile").c_str());
66  mLoaded = true;
67  return;
68  }
69 
70  const int offset = XML::getProperty(rootNode, "offset", 0);
71 
72  // iterate <homunculus>s
73  for_each_xml_child_node(homunculusNode, rootNode)
74  {
75  if (xmlNameEqual(homunculusNode, "include"))
76  {
77  const std::string name = XML::getProperty(
78  homunculusNode, "name", "");
79  if (!name.empty())
80  loadXmlFile(name, skipError);
81  continue;
82  }
83  if (!xmlNameEqual(homunculusNode, "homunculus"))
84  continue;
85 
86  const int id = XML::getProperty(homunculusNode, "id", 0);
87  BeingInfo *currentInfo = nullptr;
88  if (mHomunculusInfos.find(fromInt(id + offset, BeingTypeId))
89  != mHomunculusInfos.end())
90  {
91  logger->log("HomunculusDB: Redefinition of homunculus ID %d", id);
92  currentInfo = mHomunculusInfos[fromInt(id + offset, BeingTypeId)];
93  }
94  if (currentInfo == nullptr)
95  currentInfo = new BeingInfo;
96 
97  currentInfo->setBlockType(BlockType::NONE);
98  currentInfo->setName(XML::langProperty(
99  // TRANSLATORS: unknown info name
100  homunculusNode, "name", _("unnamed")));
101 
103  homunculusNode, "attack");
105  homunculusNode,
106  0);
107  BeingCommon::readAiAttributes(currentInfo,
108  homunculusNode);
109 
110  currentInfo->setMaxHP(XML::getProperty(homunculusNode, "maxHP", 0));
111 
113  homunculusNode, "deadSortOffsetY", 31));
114 
115  currentInfo->setColorsList(XML::getProperty(homunculusNode,
116  "colors", ""));
117 
118  if (currentInfo->getMaxHP() != 0)
119  currentInfo->setStaticMaxHP(true);
120 
121  SpriteDisplay display;
122 
123  // iterate <sprite>s and <sound>s
124  for_each_xml_child_node(spriteNode, homunculusNode)
125  {
126  BeingCommon::readObjectNodes(spriteNode, display,
127  currentInfo, "HomunculusDB");
128  }
129  currentInfo->setDisplay(display);
130 
131  mHomunculusInfos[fromInt(id + offset, BeingTypeId)] = currentInfo;
132  }
133 }
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 setName(const std::string &name)
Definition: beinginfo.h:65
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 _(s)
Definition: gettext.h:35
#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 readAiAttributes(BeingInfo *const info, xmlNode *const node)
Definition: beingcommon.cpp:96
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
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::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(), XML::langProperty(), loadXmlFile(), Logger::log(), logger, anonymous_namespace{homunculusdb.cpp}::mHomunculusInfos, anonymous_namespace{homunculusdb.cpp}::mLoaded, BlockType::NONE, paths, BeingCommon::readAiAttributes(), BeingCommon::readBasicAttributes(), BeingCommon::readObjectNodes(), BeingCommon::readWalkingAttributes(), XML::Document::rootNode(), BeingInfo::setBlockType(), BeingInfo::setColorsList(), BeingInfo::setDeadSortOffsetY(), BeingInfo::setDisplay(), BeingInfo::setMaxHP(), BeingInfo::setName(), BeingInfo::setStaticMaxHP(), and UseVirtFs_true.

◆ unload()

void HomunculusDB::unload ( )

Definition at line 135 of file homunculusdb.cpp.

136 {
137  logger->log1("Unloading homunculus database...");
139  mHomunculusInfos.clear();
140 
141  mLoaded = false;
142 }
void delete_all(Container &c)
Definition: dtor.h:56

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

Referenced by DbManager::unloadDb().