From 78f7e290e46e6084bc51023ae3c8619b0695b7b6 Mon Sep 17 00:00:00 2001 From: Philipp Sehmisch Date: Sun, 26 Nov 2006 10:12:14 +0000 Subject: Equipment database namespace and support for gender specific equipment sprites --- src/being.cpp | 6 +- src/being.h | 4 +- src/engine.cpp | 1 + src/main.cpp | 6 +- src/player.cpp | 35 ++++++++-- src/player.h | 2 +- src/resources/equipmentdb.cpp | 157 ++++++++++++++++++++++++++++++++++++++++++ src/resources/equipmentdb.h | 52 ++++++++++++++ src/resources/equipmentinfo.h | 52 ++++++++++++++ src/resources/itemdb.cpp | 6 +- src/resources/itemdb.h | 53 -------------- 11 files changed, 307 insertions(+), 67 deletions(-) create mode 100644 src/resources/equipmentdb.cpp create mode 100644 src/resources/equipmentdb.h create mode 100644 src/resources/equipmentinfo.h (limited to 'src') diff --git a/src/being.cpp b/src/being.cpp index 14d2df00..37552df3 100644 --- a/src/being.cpp +++ b/src/being.cpp @@ -62,7 +62,8 @@ Being::Being(Uint32 id, Uint16 job, Map *map): mSpeechTime(0), mDamageTime(0), mShowSpeech(false), mShowDamage(false), - mSprites(VECTOREND_SPRITE, NULL) + mSprites(VECTOREND_SPRITE, NULL), + mEquipmentSpriteIDs(VECTOREND_SPRITE, 0) { setMap(map); } @@ -122,8 +123,9 @@ Being::setHairStyle(Uint16 style) } void -Being::setVisibleEquipment(Uint8 slot, Uint8 id) +Being::setVisibleEquipment(Uint8 slot, int id) { + mEquipmentSpriteIDs[slot] = id; } void diff --git a/src/being.h b/src/being.h index 362fa393..3c497b75 100644 --- a/src/being.h +++ b/src/being.h @@ -189,7 +189,7 @@ class Being : public Sprite * Sets visible equipments for this being. */ virtual void - setVisibleEquipment(Uint8 slot, Uint8 id); + setVisibleEquipment(Uint8 slot, int id); /** * Sets the sex for this being. @@ -333,7 +333,6 @@ class Being : public Sprite getYOffset() const { return getOffset(UP, DOWN); } std::auto_ptr mEquipment; - int mVisibleEquipment[6]; /**< Visible equipments */ protected: /** @@ -372,6 +371,7 @@ class Being : public Sprite Sint32 mPx, mPy; /**< Pixel coordinates */ std::vector mSprites; + std::vector mEquipmentSpriteIDs; }; #endif diff --git a/src/engine.cpp b/src/engine.cpp index 0be41e2a..d0d37c67 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -44,6 +44,7 @@ #include "net/messageout.h" #include "net/protocol.h" + #include "resources/mapreader.h" #include "resources/resourcemanager.h" #include "resources/spriteset.h" diff --git a/src/main.cpp b/src/main.cpp index 5379086c..70bc469b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -71,6 +71,7 @@ #include "net/messageout.h" #include "net/network.h" +#include "resources/equipmentdb.h" #include "resources/image.h" #include "resources/itemdb.h" #include "resources/resourcemanager.h" @@ -305,7 +306,8 @@ void init_engine(const Options &options) logger->log("Warning: %s", err); } - // Initialize item database + // Load XML databases + EquipmentDB::load(); ItemDB::load(); } @@ -332,6 +334,8 @@ void exit_engine() ResourceManager::deleteInstance(); delete logger; + // Unload XML databases + EquipmentDB::unload(); ItemDB::unload(); } diff --git a/src/player.cpp b/src/player.cpp index df9b74f6..d6bd42f1 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -27,6 +27,8 @@ #include "game.h" #include "graphics.h" +#include "resources/equipmentdb.h" + #include "utils/tostring.h" #include "gui/gui.h" @@ -87,6 +89,7 @@ Player::setSex(Uint8 sex) { if (sex != mSex) { + //reload base sprite delete mSprites[BASE_SPRITE]; if (sex == 0) { @@ -98,7 +101,20 @@ Player::setSex(Uint8 sex) mSprites[BASE_SPRITE] = new AnimatedSprite( "graphics/sprites/player_female_base.xml", 0); } + + //reload equipment + for (int i=1; igetSprite(sex), + 0); + } + } } + Being::setSex(sex); } @@ -170,7 +186,7 @@ Player::setHairStyle(Uint16 style) } void -Player::setVisibleEquipment(Uint8 slot, Uint8 id) +Player::setVisibleEquipment(Uint8 slot, int id) { // Translate eAthena specific slot Uint8 position = 0; @@ -194,11 +210,20 @@ Player::setVisibleEquipment(Uint8 slot, Uint8 id) } else { - char stringId[4]; - sprintf(stringId, "%03i", id); + AnimatedSprite *equipmentSprite; + + if (mSex == 0) + { + equipmentSprite = new AnimatedSprite( + "graphics/sprites/" + EquipmentDB::get(id)->getSprite(0), + 0); + } + else { + equipmentSprite = new AnimatedSprite( + "graphics/sprites/" + EquipmentDB::get(id)->getSprite(1), + 0); + } - AnimatedSprite *equipmentSprite = new AnimatedSprite( - "graphics/sprites/item" + toString(stringId) + ".xml", 0); equipmentSprite->setDirection(getSpriteDirection()); delete mSprites[position]; diff --git a/src/player.h b/src/player.h index dab081f8..d666a485 100644 --- a/src/player.h +++ b/src/player.h @@ -56,7 +56,7 @@ class Player : public Being setHairStyle(Uint16 style); virtual void - setVisibleEquipment(Uint8 slot, Uint8 id); + setVisibleEquipment(Uint8 slot, int id); virtual void setWeapon(Uint16 weapon); diff --git a/src/resources/equipmentdb.cpp b/src/resources/equipmentdb.cpp new file mode 100644 index 00000000..8bbfc177 --- /dev/null +++ b/src/resources/equipmentdb.cpp @@ -0,0 +1,157 @@ +/* + * The Mana World + * Copyright 2006 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 "equipmentdb.h" + +#include "resourcemanager.h" + +#include "../log.h" + +#include "../utils/dtor.h" +#include "../utils/xml.h" + +namespace +{ + EquipmentDB::EquipmentInfos mEquipmentInfos; + EquipmentInfo mUnknown; + bool mLoaded = false; +} + +void +EquipmentDB::load() +{ + mUnknown.setSprite("error.xml", 0); + mUnknown.setSprite("error.xml", 1); + + ResourceManager *resman = ResourceManager::getInstance(); + int size; + char *data = (char*)resman->loadFile("equipment.xml", size); + + if (!data) + { + logger->error("Equipment Database: Could not find equipment.xml!"); + } + + xmlDocPtr doc = xmlParseMemory(data, size); + free(data); + + if (!doc) + { + logger->error("Equipment Database: Error while parsing equipment database (equipment.xml)!"); + } + + xmlNodePtr rootNode = xmlDocGetRootElement(doc); + if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "equipments")) + { + logger->error("Equipment Database: equipment.xml is not a valid database file!"); + } + + //iterate s + for ( xmlNodePtr equipmentNode = rootNode->xmlChildrenNode; + equipmentNode != NULL; + equipmentNode = equipmentNode->next) + { + + if (!xmlStrEqual(equipmentNode->name, BAD_CAST "equipment")) + { + continue; + } + + EquipmentInfo *currentInfo = new EquipmentInfo(); + + currentInfo->setSlot (XML::getProperty(equipmentNode, "slot", 0)); + + //iterate s + for ( xmlNodePtr spriteNode = equipmentNode->xmlChildrenNode; + spriteNode != NULL; + spriteNode = spriteNode->next) + { + if (!xmlStrEqual(spriteNode->name, BAD_CAST "sprite")) + { + continue; + } + + std::string gender = XML::getProperty(spriteNode, "gender", "unisex"); + std::string filename = (const char*) spriteNode->xmlChildrenNode->content; + + if (gender == "male" || gender == "unisex") + { + currentInfo->setSprite(filename, 0); + } + + if (gender == "female" || gender == "unisex") + { + currentInfo->setSprite(filename, 1); + } + } + + setEquipment( XML::getProperty(equipmentNode, "id", 0), + currentInfo); + } + + mLoaded = true; +} + +void +EquipmentDB::unload() +{ + // kill EquipmentInfos + for_each ( mEquipmentInfos.begin(), mEquipmentInfos.end(), + make_dtor(mEquipmentInfos)); + mEquipmentInfos.clear(); + + mLoaded = false; +} + +EquipmentInfo* +EquipmentDB::get(int id) +{ + if (!mLoaded) { + logger->error("Error: Equipment database used before initialization!"); + } + + EquipmentInfoIterator i = mEquipmentInfos.find(id); + + if (i == mEquipmentInfos.end() ) + { + logger->log("EquipmentDB: Error, unknown equipment ID# %d", id); + return &mUnknown; + } + else + { + return i->second; + } +} + +void +EquipmentDB::setEquipment(int id, EquipmentInfo* equipmentInfo) +{ + if (mEquipmentInfos.find(id) != mEquipmentInfos.end()) { + logger->log("Warning: Equipment Piece with ID %d defined multiple times", + id); + delete equipmentInfo; + } + else { + mEquipmentInfos[id] = equipmentInfo; + }; +} diff --git a/src/resources/equipmentdb.h b/src/resources/equipmentdb.h new file mode 100644 index 00000000..b8618f5f --- /dev/null +++ b/src/resources/equipmentdb.h @@ -0,0 +1,52 @@ +/* + * The Mana World + * Copyright 2006 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_EQUIPMENT_DB_H +#define _TMW_EQUIPMENT_DB_H + +#include + +#include "equipmentinfo.h" + +namespace EquipmentDB +{ + /** + * Loads the equipment info from Items.xml + */ + void load(); + + /** + * Frees equipment data + */ + void unload(); + + void setEquipment(int id, EquipmentInfo* equipmentInfo); + + EquipmentInfo* get(int id); + + // Equipment database types + typedef std::map EquipmentInfos; + typedef EquipmentInfos::iterator EquipmentInfoIterator; +} + +#endif diff --git a/src/resources/equipmentinfo.h b/src/resources/equipmentinfo.h new file mode 100644 index 00000000..bc3bdd8d --- /dev/null +++ b/src/resources/equipmentinfo.h @@ -0,0 +1,52 @@ +/* + * The Mana World + * Copyright 2006 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_EQUIPMENTINFO_H_ +#define _TMW_EQUIPMENTINFO_H_ + +#include +#include + +class EquipmentInfo +{ + public: + EquipmentInfo(): + mSlot (0) + { + }; + + void + setSlot (int slot) { mSlot = slot; }; + + std::string + getSprite(int gender) {return animationFiles[gender]; }; + + void + setSprite(std::string animationFile, int gender) {animationFiles[gender] = animationFile; }; + + private: + int mSlot; //not used at the moment but maybe useful on our own server + std::map animationFiles; +}; + +#endif diff --git a/src/resources/itemdb.cpp b/src/resources/itemdb.cpp index 2f791f07..d57a3780 100644 --- a/src/resources/itemdb.cpp +++ b/src/resources/itemdb.cpp @@ -18,7 +18,7 @@ * 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: itemmanager.cpp 2650 2006-09-03 15:00:47Z b_lindeijer $ + * $Id: */ #include "itemdb.h" @@ -38,9 +38,9 @@ target = cast((const char*)prop); \ xmlFree(prop); \ } -namespace ItemDB +namespace { - ItemInfos mItemInfos; + ItemDB::ItemInfos mItemInfos; ItemInfo mUnknown; } diff --git a/src/resources/itemdb.h b/src/resources/itemdb.h index 2821a2cf..5922984a 100644 --- a/src/resources/itemdb.h +++ b/src/resources/itemdb.h @@ -51,56 +51,3 @@ namespace ItemDB } #endif -/* - * The Mana World - * Copyright 2004 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: itemdb.h 2650 2006-09-03 15:00:47Z b_lindeijer $ - */ - -#ifndef _TMW_ITEM_MANAGER_H -#define _TMW_ITEM_MANAGER_H - -#include "iteminfo.h" - -#include - -/** - * The namespace that holds the item information - */ -namespace ItemDB -{ - /** - * Loads the item data from Items.xml - */ - void load(); - - /** - * Frees item data - */ - void unload(); - - const ItemInfo& get(int id); - - // Items database - typedef std::map ItemInfos; - typedef ItemInfos::iterator ItemInfoIterator; -} - -#endif -- cgit v1.2.3-70-g09d2