summaryrefslogtreecommitdiff
path: root/src/resources/npcdb.cpp
diff options
context:
space:
mode:
authorPhilipp Sehmisch <tmw@crushnet.org>2008-05-21 21:44:27 +0000
committerPhilipp Sehmisch <tmw@crushnet.org>2008-05-21 21:44:27 +0000
commit0ac3b58c4c5079f22e6bf4b0efba7f5fe99565cd (patch)
tree6b2a208fac407cf5cf4ddcab2c5e1ebc2b9723dc /src/resources/npcdb.cpp
parent0c869fc47a30616967cb8b69af9ec772566d7076 (diff)
downloadmana-0ac3b58c4c5079f22e6bf4b0efba7f5fe99565cd.tar.gz
mana-0ac3b58c4c5079f22e6bf4b0efba7f5fe99565cd.tar.bz2
mana-0ac3b58c4c5079f22e6bf4b0efba7f5fe99565cd.tar.xz
mana-0ac3b58c4c5079f22e6bf4b0efba7f5fe99565cd.zip
Merged revisions 4013,4027,4043,4174,4250,4254 via svnmerge from
https://themanaworld.svn.sourceforge.net/svnroot/themanaworld/tmw/trunk ........ r4013 | crush_tmw | 2008-04-01 01:18:19 +0200 (Di, 01 Apr 2008) | 1 line Implemented NPC XML database which maps NPC IDs to one or more animation files and thus enables animated NPCs. ........ r4027 | crush_tmw | 2008-04-02 01:34:14 +0200 (Mi, 02 Apr 2008) | 1 line misspelled filename ........ r4043 | b_lindeijer | 2008-04-07 10:37:23 +0200 (Mo, 07 Apr 2008) | 3 lines Added XML::Document class which simplifies parsing an XML document and automatically cleans it up again. ........ r4174 | b_lindeijer | 2008-04-23 14:57:45 +0200 (Mi, 23 Apr 2008) | 2 lines Fixed svn:keywords properties and added header to guild.h. ........ r4250 | crush_tmw | 2008-05-19 18:18:38 +0200 (Mo, 19 Mai 2008) | 1 line Added the possibility to add particle effects to NPCs in npcs.xml. ........ r4254 | crush_tmw | 2008-05-20 15:58:26 +0200 (Di, 20 Mai 2008) | 1 line fixed some filename confusion messup in the last commit. ........ NOTE: This was my first commit using svnmerge. Please check if I did everything correctly.
Diffstat (limited to 'src/resources/npcdb.cpp')
-rw-r--r--src/resources/npcdb.cpp136
1 files changed, 136 insertions, 0 deletions
diff --git a/src/resources/npcdb.cpp b/src/resources/npcdb.cpp
new file mode 100644
index 00000000..5869e295
--- /dev/null
+++ b/src/resources/npcdb.cpp
@@ -0,0 +1,136 @@
+/*
+ * The Mana World
+ * Copyright 2008 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * The Mana World is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * $Id$
+ */
+
+#include "npcdb.h"
+
+#include "resourcemanager.h"
+
+#include "../log.h"
+
+#include "../utils/dtor.h"
+#include "../utils/xml.h"
+
+namespace
+{
+ NPCInfos mNPCInfos;
+ NPCInfo mUnknown;
+ bool mLoaded = false;
+}
+
+void NPCDB::load()
+{
+ if (mLoaded)
+ return;
+
+ NPCsprite *unknownSprite = new NPCsprite;
+ unknownSprite->sprite = "error.xml";
+ unknownSprite->variant = 0;
+ mUnknown.sprites.push_back(unknownSprite);
+
+ logger->log("Initializing NPC database...");
+
+ XML::Document doc("npcs.xml");
+ xmlNodePtr rootNode = doc.rootNode();
+
+ if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "npcs"))
+ {
+ logger->error("NPC Database: Error while loading items.xml!");
+ }
+
+ //iterate <monster>s
+ for_each_xml_child_node(npcNode, rootNode)
+ {
+ if (!xmlStrEqual(npcNode->name, BAD_CAST "npc"))
+ continue;
+
+ int id = XML::getProperty(npcNode, "id", 0);
+ if (id == 0)
+ {
+ logger->log("NPC Database: NPC with missing ID in npcs.xml!");
+ continue;
+ }
+
+ NPCInfo *currentInfo = new NPCInfo;
+
+ for_each_xml_child_node(spriteNode, npcNode)
+ {
+ if (xmlStrEqual(spriteNode->name, BAD_CAST "sprite"))
+ {
+ NPCsprite *currentSprite = new NPCsprite;
+ currentSprite->sprite = (const char*)spriteNode->xmlChildrenNode->content;
+ currentSprite->variant = XML::getProperty(spriteNode, "variant", 0);
+ currentInfo->sprites.push_back(currentSprite);
+ }
+ else if (xmlStrEqual(spriteNode->name, BAD_CAST "particlefx"))
+ {
+ std::string particlefx = (const char*)spriteNode->xmlChildrenNode->content;
+ currentInfo->particles.push_back(particlefx);
+ }
+ }
+ mNPCInfos[id] = currentInfo;
+ }
+
+ mLoaded = true;
+}
+
+void
+NPCDB::unload()
+{
+ for ( NPCInfosIterator i = mNPCInfos.begin();
+ i != mNPCInfos.end();
+ i++)
+ {
+ while (!i->second->sprites.empty())
+ {
+ delete i->second->sprites.front();
+ i->second->sprites.pop_front();
+ }
+ delete i->second;
+ }
+
+ mNPCInfos.clear();
+
+ while (!mUnknown.sprites.empty())
+ {
+ delete mUnknown.sprites.front();
+ mUnknown.sprites.pop_front();
+ }
+
+ mLoaded = false;
+}
+
+const NPCInfo&
+NPCDB::get(int id)
+{
+ NPCInfosIterator i = mNPCInfos.find(id);
+
+ if (i == mNPCInfos.end())
+ {
+ logger->log("NPCDB: Warning, unknown NPC ID %d requested", id);
+ return mUnknown;
+ }
+ else
+ {
+ return *(i->second);
+ }
+}