From 0ac3b58c4c5079f22e6bf4b0efba7f5fe99565cd Mon Sep 17 00:00:00 2001 From: Philipp Sehmisch Date: Wed, 21 May 2008 21:44:27 +0000 Subject: 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. --- src/resources/itemdb.cpp | 22 +------ src/resources/mapreader.cpp | 14 ++--- src/resources/monsterdb.cpp | 29 ++-------- src/resources/npcdb.cpp | 136 ++++++++++++++++++++++++++++++++++++++++++++ src/resources/npcdb.h | 61 ++++++++++++++++++++ src/resources/spritedef.cpp | 34 ++++------- src/resources/spritedef.h | 6 +- 7 files changed, 223 insertions(+), 79 deletions(-) create mode 100644 src/resources/npcdb.cpp create mode 100644 src/resources/npcdb.h (limited to 'src/resources') diff --git a/src/resources/itemdb.cpp b/src/resources/itemdb.cpp index 9d64534c..e8ad393b 100644 --- a/src/resources/itemdb.cpp +++ b/src/resources/itemdb.cpp @@ -59,26 +59,12 @@ void ItemDB::load() mUnknown->setSprite("error.xml", 0); mUnknown->setSprite("error.xml", 1); - ResourceManager *resman = ResourceManager::getInstance(); - int size; - char *data = (char*) resman->loadFile("items.xml", size); + XML::Document doc("items.xml"); + xmlNodePtr rootNode = doc.rootNode(); - if (!data) { - logger->error("ItemDB: Could not find items.xml!"); - } - - xmlDocPtr doc = xmlParseMemory(data, size); - free(data); - - if (!doc) - { - logger->error("ItemDB: Error while parsing item database (items.xml)!"); - } - - xmlNodePtr rootNode = xmlDocGetRootElement(doc); if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "items")) { - logger->error("ItemDB: items.xml is not a valid database file!"); + logger->error("ItemDB: Error while loading items.xml!"); } for_each_xml_child_node(node, rootNode) @@ -150,8 +136,6 @@ void ItemDB::load() #undef CHECK_PARAM } - xmlFreeDoc(doc); - mLoaded = true; } diff --git a/src/resources/mapreader.cpp b/src/resources/mapreader.cpp index 940ded36..2a080fb0 100644 --- a/src/resources/mapreader.cpp +++ b/src/resources/mapreader.cpp @@ -183,21 +183,19 @@ MapReader::readMap(const std::string &filename) inflatedSize = fileSize; } - xmlDocPtr doc = xmlParseMemory((char*) inflated, inflatedSize); + XML::Document doc((char*) inflated, inflatedSize); free(inflated); - // Parse the inflated map data - if (doc) { - xmlNodePtr node = xmlDocGetRootElement(doc); + xmlNodePtr node = doc.rootNode(); - if (!node || !xmlStrEqual(node->name, BAD_CAST "map")) { + // Parse the inflated map data + if (node) { + if (!xmlStrEqual(node->name, BAD_CAST "map")) { logger->log("Error: Not a map file (%s)!", filename.c_str()); } - else - { + else { map = readMap(node, filename); } - xmlFreeDoc(doc); } else { logger->log("Error while parsing map file (%s)!", filename.c_str()); } diff --git a/src/resources/monsterdb.cpp b/src/resources/monsterdb.cpp index e007eda0..0a98762f 100644 --- a/src/resources/monsterdb.cpp +++ b/src/resources/monsterdb.cpp @@ -50,27 +50,12 @@ MonsterDB::load() logger->log("Initializing monster database..."); - ResourceManager *resman = ResourceManager::getInstance(); - int size; - char *data = (char*)resman->loadFile("monsters.xml", size); + XML::Document doc("monsters.xml"); + xmlNodePtr rootNode = doc.rootNode(); - if (!data) - { - logger->error("Monster Database: Could not find monsters.xml!"); - } - - xmlDocPtr doc = xmlParseMemory(data, size); - free(data); - - if (!doc) - { - logger->error("Monster Database: Error while parsing monster database (monsters.xml)!"); - } - - xmlNodePtr rootNode = xmlDocGetRootElement(doc); if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "monsters")) { - logger->error("Monster Database: monster.xml is not a valid database file!"); + logger->error("Monster Database: Error while loading monster.xml!"); } //iterate s @@ -152,16 +137,14 @@ MonsterDB::load() mMonsterInfos[XML::getProperty(monsterNode, "id", 0)] = currentInfo; } - xmlFreeDoc(doc); - mLoaded = true; } void MonsterDB::unload() { - for_each ( mMonsterInfos.begin(), mMonsterInfos.end(), - make_dtor(mMonsterInfos)); + for_each(mMonsterInfos.begin(), mMonsterInfos.end(), + make_dtor(mMonsterInfos)); mMonsterInfos.clear(); mLoaded = false; @@ -169,7 +152,7 @@ MonsterDB::unload() const MonsterInfo& -MonsterDB::get (int id) +MonsterDB::get(int id) { MonsterInfoIterator i = mMonsterInfos.find(id); 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 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); + } +} diff --git a/src/resources/npcdb.h b/src/resources/npcdb.h new file mode 100644 index 00000000..2abf959b --- /dev/null +++ b/src/resources/npcdb.h @@ -0,0 +1,61 @@ +/* + * 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$ + */ + +#ifndef _TMW_NPC_DB_H +#define _TMW_NPC_DB_H + +#include +#include +#include + +struct NPCsprite +{ + std::string sprite; + int variant; +}; + +struct NPCInfo +{ + std::list sprites; + std::list particles; +}; + +typedef std::map NPCInfos; + +/** + * NPC information database. + */ +namespace NPCDB +{ + void + load(); + + void + unload(); + + const NPCInfo& get(int id); + + typedef NPCInfos::iterator NPCInfosIterator; +} + +#endif diff --git a/src/resources/spritedef.cpp b/src/resources/spritedef.cpp index ee823c3f..3f95b3ec 100644 --- a/src/resources/spritedef.cpp +++ b/src/resources/spritedef.cpp @@ -51,7 +51,6 @@ SpriteDef::getAction(SpriteAction action) const SpriteDef *SpriteDef::load(std::string const &animationFile, int variant) { - int size; ResourceManager *resman = ResourceManager::getInstance(); std::string::size_type pos = animationFile.find('|'); @@ -59,29 +58,18 @@ SpriteDef *SpriteDef::load(std::string const &animationFile, int variant) if (pos != std::string::npos) palettes = animationFile.substr(pos + 1); - char *data = (char*) resman->loadFile - (animationFile.substr(0, pos).c_str(), size); + XML::Document doc(animationFile.substr(0, pos)); + xmlNodePtr rootNode = doc.rootNode(); - if (!data && animationFile != "graphics/sprites/error.xml") - { - return load("graphics/sprites/error.xml", 0); - } - - xmlDocPtr doc = xmlParseMemory(data, size); - free(data); - - if (!doc) - { - logger->log("Error, failed to parse %s.", animationFile.c_str()); - return NULL; - } - - xmlNodePtr rootNode = xmlDocGetRootElement(doc); if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "sprite")) { - logger->log("Error, failed to parse %s.", animationFile.c_str()); - xmlFreeDoc(doc); - return NULL; + logger->log("Error, failed to parse %s", animationFile.c_str()); + + if (animationFile != "graphics/sprites/error.xml") { + return load("graphics/sprites/error.xml", 0); + } else { + return NULL; + } } // Get the variant @@ -111,8 +99,6 @@ SpriteDef *SpriteDef::load(std::string const &animationFile, int variant) } } - xmlFreeDoc(doc); - def->substituteActions(); return def; } @@ -279,7 +265,7 @@ SpriteDef::loadAnimation(xmlNodePtr animationNode, void SpriteDef::includeSprite(xmlNodePtr includeNode) { - std::string filename = XML::getProperty(includeNode, "file", ""); + const std::string filename = XML::getProperty(includeNode, "file", ""); ResourceManager *resman = ResourceManager::getInstance(); SpriteDef *sprite = resman->getSprite("graphics/sprites/" + filename); diff --git a/src/resources/spritedef.h b/src/resources/spritedef.h index 4f316875..531dfb3d 100644 --- a/src/resources/spritedef.h +++ b/src/resources/spritedef.h @@ -81,11 +81,10 @@ class SpriteDef : public Resource Action *getAction(SpriteAction action) const; private: - /** * Constructor. */ - SpriteDef(): mAction(NULL), mDirection(DIRECTION_DOWN), mLastTime(0) {} + SpriteDef() {} /** * Destructor. @@ -149,9 +148,6 @@ class SpriteDef : public Resource ImageSets mImageSets; Actions mActions; - Action *mAction; - SpriteDirection mDirection; - int mLastTime; }; #endif -- cgit v1.2.3-70-g09d2