diff options
author | Guillaume Melquiond <guillaume.melquiond@gmail.com> | 2007-07-01 17:00:26 +0000 |
---|---|---|
committer | Guillaume Melquiond <guillaume.melquiond@gmail.com> | 2007-07-01 17:00:26 +0000 |
commit | d44476beb6736faafe6480cfbe527aef7e88427a (patch) | |
tree | 1a92c517d9bdd064aa6999bb5841ab8e4952913b | |
parent | d7e84be1d3dc935f47cefc0f600ced74f37e46fb (diff) | |
download | manaserv-d44476beb6736faafe6480cfbe527aef7e88427a.tar.gz manaserv-d44476beb6736faafe6480cfbe527aef7e88427a.tar.bz2 manaserv-d44476beb6736faafe6480cfbe527aef7e88427a.tar.xz manaserv-d44476beb6736faafe6480cfbe527aef7e88427a.zip |
Reverted inventory handling code to the last known working state. Simplified serialization interface along the way.
-rw-r--r-- | ChangeLog | 19 | ||||
-rw-r--r-- | src/Makefile.am | 8 | ||||
-rw-r--r-- | src/abstractcharacterdata.cpp | 101 | ||||
-rw-r--r-- | src/abstractcharacterdata.hpp | 185 | ||||
-rw-r--r-- | src/account-server/characterdata.cpp | 19 | ||||
-rw-r--r-- | src/account-server/characterdata.hpp | 40 | ||||
-rw-r--r-- | src/account-server/dalstorage.cpp | 2 | ||||
-rw-r--r-- | src/account-server/serverhandler.cpp | 3 | ||||
-rw-r--r-- | src/common/inventorydata.hpp | 57 | ||||
-rw-r--r-- | src/game-server/accountconnection.cpp | 5 | ||||
-rw-r--r-- | src/game-server/character.cpp | 45 | ||||
-rw-r--r-- | src/game-server/character.hpp | 25 | ||||
-rw-r--r-- | src/game-server/inventory.cpp | 82 | ||||
-rw-r--r-- | src/serialize/characterdata.hpp | 104 |
14 files changed, 263 insertions, 432 deletions
@@ -1,4 +1,21 @@ -2007-06-28 Philipp Sehmisch <tmw@crushnet.org> +2007-07-01 Guillaume Melquiond <guillaume.melquiong@gmail.com> + + * src/abstractcharacterdata.cpp, src/abstractcharacterdata.hpp, + src/serialize/characterdata.hpp, src/account-server/serverhandler.cpp, + src/game-server/accountconnection.cpp: Removed multiple inheritance + mechanism for serialization. Replaced it by a less cumbersome template + interface. + * src/common/inventorydata.hpp: Set inventory structures in a directory + common to both servers. + * src/account-server/dalstorage.cpp: Commented out unused code. + * src/Makefile.am: Taken new files into account. + * src/account-server/characterdata.cpp, src/game-server/character.cpp, + src/account-server/characterdata.hpp, src/game-server/character.hpp: + Removed new inventory code, as it was not doing anything. + * src/inventory.cpp: Reverted to the old code, as it was working just + fine. + +2007-06-28 Philipp Sehmisch <tmw@crushnet.org> * src/defines.h, src/game-server/state.cpp: Attack direction is now included in being attack messages. diff --git a/src/Makefile.am b/src/Makefile.am index 69ff347e..213ba2a3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -6,8 +6,6 @@ bin_PROGRAMS = tmwserv-account tmwserv-game tmwserv_account_SOURCES = \ account-server/main-account.cpp \ - abstractcharacterdata.hpp \ - abstractcharacterdata.cpp \ configuration.h \ configuration.cpp \ defines.h \ @@ -41,6 +39,7 @@ tmwserv_account_SOURCES = \ chat-server/chatchannel.cpp \ chat-server/chatchannelmanager.hpp \ chat-server/chatchannelmanager.cpp \ + common/inventorydata.hpp \ dal/dalexcept.h \ dal/dataprovider.h \ dal/dataprovider.cpp \ @@ -56,6 +55,7 @@ tmwserv_account_SOURCES = \ net/messageout.cpp \ net/netcomputer.hpp \ net/netcomputer.cpp \ + serialize/characterdata.hpp \ utils/countedptr.h \ utils/functors.h \ utils/logger.h \ @@ -70,8 +70,6 @@ tmwserv_account_SOURCES = \ tmwserv_game_SOURCES = \ game-server/main-game.cpp \ - abstractcharacterdata.hpp \ - abstractcharacterdata.cpp \ configuration.h \ configuration.cpp \ defines.h \ @@ -80,6 +78,7 @@ tmwserv_game_SOURCES = \ resourcemanager.cpp \ skill.h \ skill.cpp \ + common/inventorydata.hpp \ game-server/accountconnection.hpp \ game-server/accountconnection.cpp \ game-server/being.hpp \ @@ -128,6 +127,7 @@ tmwserv_game_SOURCES = \ net/messageout.cpp \ net/netcomputer.hpp \ net/netcomputer.cpp \ + serialize/characterdata.hpp \ utils/base64.h \ utils/base64.cpp \ utils/mathutils.h \ diff --git a/src/abstractcharacterdata.cpp b/src/abstractcharacterdata.cpp deleted file mode 100644 index 57685273..00000000 --- a/src/abstractcharacterdata.cpp +++ /dev/null @@ -1,101 +0,0 @@ -/* - * The Mana World Server - * Copyright 2007 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 "abstractcharacterdata.hpp" - -#include "defines.h" -#include "net/messagein.hpp" -#include "net/messageout.hpp" -#include "point.h" - -void AbstractCharacterData::serialize(MessageOut &msg) const -{ - msg.writeLong(getDatabaseID()); - msg.writeString(getName()); - msg.writeByte(getGender()); - msg.writeByte(getHairStyle()); - msg.writeByte(getHairColor()); - msg.writeByte(getLevel()); - msg.writeShort(getMoney()); - - for (int i = 0; i < NB_BASE_ATTRIBUTES; ++i) - { - msg.writeByte(getBaseAttribute(i)); - } - - msg.writeShort(getMapId()); - msg.writeShort(getPosition().x); - msg.writeShort(getPosition().y); - - /** - * TODO: uncomment/redesign after Inventory is redesigned/improved - * TODO: Test and debug. - * - for (int j = 0; j < getNumberOfInventoryItems(); ++j) - { - msg.writeShort(getInventoryItem(j).itemClassId); - msg.writeShort(getInventoryItem(j).numberOfItemsInSlot); - msg.writeByte((char)getInventoryItem(j).isEquiped); - } - */ -} - -void AbstractCharacterData::deserialize(MessageIn &msg) -{ - setDatabaseID(msg.readLong()); - setName(msg.readString()); - setGender(msg.readByte()); - setHairStyle(msg.readByte()); - setHairColor(msg.readByte()); - setLevel(msg.readByte()); - setMoney(msg.readShort()); - - for (int i = 0; i < NB_BASE_ATTRIBUTES; ++i) - { - setBaseAttribute(i, msg.readByte()); - } - - setMapId(msg.readShort()); - - Point temporaryPoint; - temporaryPoint.x = msg.readShort(); - temporaryPoint.y = msg.readShort(); - setPosition(temporaryPoint); - - /** - * TODO: uncomment/redesign after Inventory is redesigned/improved - * TODO: Test and debug. - * - - clearInventory(); - - // byte = 1, short = 2 - while (msg.getUnreadLength() >= 5) - { - InventoryItem tempItem; - tempItem.itemClassId = msg.readShort(); - tempItem.numberOfItemsInSlot = msg.readShort(); - tempItem.isEquiped = (bool) msg.readByte(); - addItemToInventory(tempItem); - } - */ -} diff --git a/src/abstractcharacterdata.hpp b/src/abstractcharacterdata.hpp deleted file mode 100644 index b3a4a908..00000000 --- a/src/abstractcharacterdata.hpp +++ /dev/null @@ -1,185 +0,0 @@ -/* - * The Mana World Server - * 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$ - */ - -#ifndef _TMWSERV_ABSTRACTCHARACTERDATA -#define _TMWSERV_ABSTRACTCHARACTERDATA - -#include <string> - -class MessageIn; -class MessageOut; -class Point; - -/** - * Gender of a Character. - */ -enum -{ - GENDER_MALE = 0, - GENDER_FEMALE -}; - -/** - * Numbers of inventory slots - */ -enum -{ - EQUIPMENT_SLOTS = 11, - INVENTORY_SLOTS = 50 -}; - -/** - * Structure representing an item stored in the inventory. - * Primarily for the communication between account- and gameserver. - */ -struct InventoryItem -{ - unsigned short itemClassId; - unsigned short numberOfItemsInSlot; - bool isEquiped; -}; - -class AbstractCharacterData -{ - public: - /** - * Empty virtual destructor. - */ - virtual ~AbstractCharacterData() {} - - /** - * Stores data into a outgoing message. - */ - void serialize(MessageOut &) const; - - /** - * Restores data from a incomming message. - */ - void deserialize(MessageIn &); - - protected: - /* - * Get and set methods - */ - - /** Gets the database id of the character. */ - virtual int - getDatabaseID() const = 0; - - /** Sets the database id of the character. */ - virtual void - setDatabaseID(int id) = 0; - - /** Gets the name of the character. */ - virtual std::string const & - getName() const = 0; - - /** Sets the name of the character. */ - virtual void - setName(const std::string& name) = 0; - - /** Gets the gender of the character (male or female). */ - virtual int - getGender() const = 0; - - /** Sets the gender of the character (male or female). */ - virtual void - setGender(int gender) = 0; - - /** Gets the hairstyle of the character. */ - virtual int - getHairStyle() const = 0; - - /** Sets the hairstyle of the character. */ - virtual void - setHairStyle(int style) = 0; - - /** Gets the haircolor of the character. */ - virtual int - getHairColor() const = 0; - - /** Sets the haircolor of the character. */ - virtual void - setHairColor(int color) = 0; - - /** Gets the level of the character. */ - virtual int - getLevel() const = 0; - - /** Sets the level of the character. */ - virtual void - setLevel(int level) = 0; - - /** Gets the amount of money the character has. */ - virtual int - getMoney() const = 0; - - /** Sets the amount of money the character has. */ - virtual void - setMoney(int amount) = 0; - - /** Gets the value of a base attribute of the character. */ - virtual unsigned short - getBaseAttribute(int attributeNumber) const = 0; - - /** Sets the value of a base attribute of the character. */ - virtual void - setBaseAttribute(int attributeNumber, int value) = 0; - - /** Gets the Id of the map that the character is on. */ - virtual int - getMapId() const = 0; - - /** Sets the Id of the map that the character is on. */ - virtual void - setMapId(int mapId) = 0; - - /** Gets the position of the character on the map. */ - virtual Point const & - getPosition() const = 0; - - /** Sets the position of the character on the map. */ - virtual void - setPosition(const Point &p) = 0; - - /* - * The access functions for inventory - */ - - /** Returns the number of inventory items. */ - virtual int - getNumberOfInventoryItems() const = 0; - - /** Returns a reference to the item in inventory at slot. */ - virtual InventoryItem const & - getInventoryItem(unsigned short slot) const = 0; - - /** Clears the inventory, in preperation for an update. */ - virtual void - clearInventory() = 0; - - /** Adds an inventory item to the inventory. */ - virtual void - addItemToInventory(const InventoryItem& item) = 0; -}; - -#endif diff --git a/src/account-server/characterdata.cpp b/src/account-server/characterdata.cpp index 95b5e75a..5dab4c08 100644 --- a/src/account-server/characterdata.cpp +++ b/src/account-server/characterdata.cpp @@ -21,8 +21,8 @@ */ #include "account-server/characterdata.hpp" - #include "net/messagein.hpp" +#include "serialize/characterdata.hpp" CharacterData::CharacterData(std::string const &name, int id): mDatabaseID(id), mAccountID(-1), mName(name), mGender(0), mHairStyle(0), @@ -42,21 +42,6 @@ CharacterData::CharacterData(MessageIn & msg): { mBaseAttributes[i] = 0; } - deserialize(msg); -} - -InventoryItem const & -CharacterData::getInventoryItem(unsigned short slot) const -{ - return mInventory[slot]; + deserializeCharacterData(*this, msg); } -void -CharacterData::addItemToInventory(const InventoryItem& item) -{ - // Discard items if the inventory is full - if ((int)mInventory.size() < INVENTORY_SLOTS) - { - mInventory.push_back(item); - } -} diff --git a/src/account-server/characterdata.hpp b/src/account-server/characterdata.hpp index 8d7e2599..76b43da0 100644 --- a/src/account-server/characterdata.hpp +++ b/src/account-server/characterdata.hpp @@ -23,18 +23,17 @@ #ifndef _TMWSERV_CHARACTERDATA #define _TMWSERV_CHARACTERDATA -#include "abstractcharacterdata.hpp" -#include "defines.h" #include <string> #include <vector> -#include "utils/countedptr.h" - +#include "defines.h" #include "point.h" +#include "common/inventorydata.hpp" +#include "utils/countedptr.h" class MessageIn; -class CharacterData: public AbstractCharacterData +class CharacterData { public: @@ -139,22 +138,6 @@ class CharacterData: public AbstractCharacterData void setPosition(const Point &p) { mPos = p; } - /** Returns the number of inventory items. */ - int - getNumberOfInventoryItems() const { return mInventory.size(); } - - /** Returns a reference to the item in inventory at slot. */ - InventoryItem const & - getInventoryItem(unsigned short slot) const; - - /** Clears the inventory, in preperation for an update. */ - void - clearInventory() { mInventory.clear(); } - - /** Adds an inventory item to the inventory. */ - void - addItemToInventory(const InventoryItem& item); - /** Add a guild to the character */ void addGuild(const std::string &name) { mGuilds.push_back(name); } @@ -162,6 +145,18 @@ class CharacterData: public AbstractCharacterData std::vector<std::string> getGuilds() const { return mGuilds; } + /** + * Gets a reference on the possessions. + */ + Possessions const &getPossessions() const + { return mPossessions; } + + /** + * Gets a reference on the possessions. + */ + Possessions &getPossessions() + { return mPossessions; } + private: CharacterData(CharacterData const &); CharacterData &operator=(CharacterData const &); @@ -180,8 +175,7 @@ class CharacterData: public AbstractCharacterData //!< character. unsigned short mMapId; //!< Map the being is on. Point mPos; //!< Position the being is at. - std::vector< InventoryItem > mInventory; //!< All the possesions of - //!< the character. + Possessions mPossessions; //!< All the possesions of the character. std::vector<std::string> mGuilds; //!< All the guilds the player //!< belongs to. }; diff --git a/src/account-server/dalstorage.cpp b/src/account-server/dalstorage.cpp index fd6c8419..ff5684dc 100644 --- a/src/account-server/dalstorage.cpp +++ b/src/account-server/dalstorage.cpp @@ -681,6 +681,7 @@ DALStorage::updateCharacter(CharacterPtr character) * Character's inventory */ + /* // Delete the old inventory first try { @@ -735,6 +736,7 @@ DALStorage::updateCharacter(CharacterPtr character) return false; } } + */ return true; } diff --git a/src/account-server/serverhandler.cpp b/src/account-server/serverhandler.cpp index 47c23966..3adb7173 100644 --- a/src/account-server/serverhandler.cpp +++ b/src/account-server/serverhandler.cpp @@ -34,6 +34,7 @@ #include "net/messagein.hpp" #include "net/messageout.hpp" #include "net/netcomputer.hpp" +#include "serialize/characterdata.hpp" #include "utils/logger.h" #include "utils/tokendispenser.hpp" #include "utils/tokencollector.hpp" @@ -83,7 +84,7 @@ void ServerHandler::registerGameClient(std::string const &token, CharacterPtr pt MessageOut msg(AGMSG_PLAYER_ENTER); msg.writeString(token, MAGIC_TOKEN_LENGTH); - ptr->serialize(msg); //Characterdata + serializeCharacterData(*ptr, msg); Servers::const_iterator i = servers.find(mapId); assert(i != servers.end()); diff --git a/src/common/inventorydata.hpp b/src/common/inventorydata.hpp new file mode 100644 index 00000000..51cb034e --- /dev/null +++ b/src/common/inventorydata.hpp @@ -0,0 +1,57 @@ +/* + * The Mana World Server + * Copyright 2007 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 _TMWSERV_COMMON_INVENTORYDATA_HPP_ +#define _TMWSERV_COMMON_INVENTORYDATA_HPP_ + +/** + * Numbers of inventory slots + */ + +enum +{ + EQUIPMENT_SLOTS = 11, + INVENTORY_SLOTS = 50 +}; + +/** + * Structure storing an item in the inventory. + * When the itemId is zero, this item represents "amount" consecutive empty slots. + */ + +struct InventoryItem +{ + unsigned short itemId; + unsigned char amount; +}; + +/** + * Structure storing the equipment and inventory of a Player. + */ +struct Possessions +{ + unsigned short equipment[EQUIPMENT_SLOTS]; + std::vector< InventoryItem > inventory; + Possessions() { for (int i = 0; i < EQUIPMENT_SLOTS; ++i) equipment[i] = 0; } +}; + +#endif diff --git a/src/game-server/accountconnection.cpp b/src/game-server/accountconnection.cpp index 3ae5816e..de1ec618 100644 --- a/src/game-server/accountconnection.cpp +++ b/src/game-server/accountconnection.cpp @@ -24,14 +24,15 @@ #include "configuration.h" #include "defines.h" #include "game-server/accountconnection.hpp" +#include "game-server/character.hpp" #include "game-server/gamehandler.hpp" #include "game-server/map.hpp" #include "game-server/mapcomposite.hpp" #include "game-server/mapmanager.hpp" -#include "game-server/character.hpp" #include "game-server/state.hpp" #include "net/messagein.hpp" #include "net/messageout.hpp" +#include "serialize/characterdata.hpp" #include "utils/logger.h" #include "utils/tokendispenser.hpp" #include "utils/tokencollector.hpp" @@ -59,7 +60,7 @@ bool AccountConnection::start() void AccountConnection::sendCharacterData(Character *p) { MessageOut msg(GAMSG_PLAYER_DATA); - p->serialize(msg); + serializeCharacterData(*p, msg); send(msg); } diff --git a/src/game-server/character.cpp b/src/game-server/character.cpp index c33300b0..b9613c1f 100644 --- a/src/game-server/character.cpp +++ b/src/game-server/character.cpp @@ -20,15 +20,13 @@ * $Id$ */ -#include "game-server/character.hpp" - #include <cassert> #include "defines.h" +#include "game-server/character.hpp" #include "net/messagein.hpp" #include "net/messageout.hpp" - -InventoryItem tempItem; +#include "serialize/characterdata.hpp" Character::Character(MessageIn & msg): Being(OBJECT_CHARACTER, 65535), @@ -37,16 +35,11 @@ Character::Character(MessageIn & msg): mDatabaseID(-1), mName(""), mGender(0), mHairStyle(0), mHairColor(0), mLevel(0), mMoney(0) { - // clear equipment - for (int i = 0; i < EQUIPMENT_SLOTS; ++i) - { - mPossessions.equipment[i] = 0; - } // prepare attributes vector mAttributes.resize(NB_ATTRIBUTES_CHAR, 1); mOldAttributes.resize(NB_ATTRIBUTES_CHAR, 0); // get base attributes - deserialize(msg); + deserializeCharacterData(*this, msg); // give the player 10 weapon skill for testing purpose setAttribute(CHAR_SKILL_WEAPON_UNARMED, 10); @@ -72,38 +65,6 @@ Character::update() } } -int -Character::getNumberOfInventoryItems() const -{ - // TODO: implement after redesign/improvement of Inventory - return 0; -} - -InventoryItem const & -Character::getInventoryItem(unsigned short slot) const -{ - // TODO: implement after redesign/improvement of Inventory - //InventoryItem tempItem; - - tempItem.itemClassId = 0; - tempItem.numberOfItemsInSlot = 0; - tempItem.isEquiped = false; - - return tempItem; -} - -void -Character::clearInventory() -{ - // TODO: implement after redesign/improvement of Inventory -} - -void -Character::addItemToInventory(const InventoryItem& item) -{ - // TODO: implement after redesign/improvement of Inventory -} - void Character::calculateDerivedAttributes() { Being::calculateDerivedAttributes(); diff --git a/src/game-server/character.hpp b/src/game-server/character.hpp index 4931624c..275b5e67 100644 --- a/src/game-server/character.hpp +++ b/src/game-server/character.hpp @@ -23,27 +23,21 @@ #ifndef _TMWSERV_CHARACTER_HPP_ #define _TMWSERV_CHARACTER_HPP_ -#include "abstractcharacterdata.hpp" -#include "game-server/being.hpp" - #include <string> #include <vector> +#include "game-server/being.hpp" +#include "common/inventorydata.hpp" + class GameClient; class MessageIn; +class MessageOut; class Point; - -struct Possessions -{ - unsigned short equipment[EQUIPMENT_SLOTS]; - std::vector< InventoryItem > inventory; -}; - /** * The representation of a player's character in the game world. */ -class Character : public Being, public AbstractCharacterData +class Character : public Being { public: @@ -71,8 +65,13 @@ class Character : public Being, public AbstractCharacterData { mClient = c; } /** - * Gets a reference on the possession. - * Used in the current Inventory class + * Gets a reference on the possessions. + */ + Possessions const &getPossessions() const + { return mPossessions; } + + /** + * Gets a reference on the possessions. */ Possessions &getPossessions() { return mPossessions; } diff --git a/src/game-server/inventory.cpp b/src/game-server/inventory.cpp index 0d2a82f5..c8cea6de 100644 --- a/src/game-server/inventory.cpp +++ b/src/game-server/inventory.cpp @@ -30,9 +30,6 @@ #include "game-server/itemmanager.hpp" #include "net/messageout.hpp" -// For the InventoryItem structure -#include "abstractcharacterdata.hpp" - Inventory::Inventory(Character *p) : poss(p->getPossessions()), msg(GPMSG_INVENTORY), client(p) {} @@ -62,16 +59,16 @@ void Inventory::sendFull() const for (std::vector< InventoryItem >::iterator i = poss.inventory.begin(), i_end = poss.inventory.end(); i != i_end; ++i) { - if (i->itemClassId) + if (i->itemId) { m.writeByte(slot); - m.writeShort(i->itemClassId); - m.writeByte(i->numberOfItemsInSlot); + m.writeShort(i->itemId); + m.writeByte(i->amount); ++slot; } else { - slot += i->numberOfItemsInSlot; + slot += i->amount; } } @@ -85,10 +82,10 @@ int Inventory::getItem(int slot) const { if (slot == 0) { - return i->itemClassId; + return i->itemId; } - slot -= i->itemClassId ? 1 : i->numberOfItemsInSlot; + slot -= i->itemId ? 1 : i->amount; if (slot < 0) { @@ -109,7 +106,7 @@ int Inventory::getIndex(int slot) const return index; } - slot -= i->itemClassId ? 1 : i->numberOfItemsInSlot; + slot -= i->itemId ? 1 : i->amount; if (slot < 0) { @@ -125,7 +122,7 @@ int Inventory::getSlot(int index) const for (std::vector< InventoryItem >::iterator i = poss.inventory.begin(), i_end = poss.inventory.begin() + index; i != i_end; ++i) { - slot += i->itemClassId ? 1 : i->numberOfItemsInSlot; + slot += i->itemId ? 1 : i->amount; } return slot; } @@ -136,18 +133,18 @@ int Inventory::fillFreeSlot(int itemId, int amount, int maxPerSlot) for (int i = 0, i_end = poss.inventory.size(); i < i_end; ++i) { InventoryItem &it = poss.inventory[i]; - if (it.itemClassId == 0) + if (it.itemId == 0) { int nb = std::min(amount, maxPerSlot); - if (it.numberOfItemsInSlot <= 1) + if (it.amount <= 1) { - it.itemClassId = itemId; - it.numberOfItemsInSlot = nb; + it.itemId = itemId; + it.amount = nb; } else { - --it.numberOfItemsInSlot; - InventoryItem iu = { itemId, nb, false }; + --it.amount; + InventoryItem iu = { itemId, nb }; poss.inventory.insert(poss.inventory.begin() + i, iu); ++i_end; } @@ -169,7 +166,7 @@ int Inventory::fillFreeSlot(int itemId, int amount, int maxPerSlot) { int nb = std::min(amount, maxPerSlot); amount -= nb; - InventoryItem it = { itemId, nb, false }; + InventoryItem it = { itemId, nb }; poss.inventory.push_back(it); msg.writeByte(slot + EQUIP_CLIENT_INVENTORY); @@ -189,15 +186,15 @@ int Inventory::insert(int itemId, int amount) for (std::vector< InventoryItem >::iterator i = poss.inventory.begin(), i_end = poss.inventory.end(); i != i_end; ++i) { - if (i->itemClassId == itemId) + if (i->itemId == itemId) { - int nb = std::min(maxPerSlot - i->numberOfItemsInSlot, amount); - i->numberOfItemsInSlot += nb; + int nb = std::min(maxPerSlot - i->amount, amount); + i->amount += nb; amount -= nb; msg.writeByte(slot + EQUIP_CLIENT_INVENTORY); msg.writeShort(itemId); - msg.writeByte(i->numberOfItemsInSlot); + msg.writeByte(i->amount); if (amount == 0) { @@ -207,7 +204,7 @@ int Inventory::insert(int itemId, int amount) } else { - slot += i->itemClassId ? 1 : i->numberOfItemsInSlot; + slot += i->itemId ? 1 : i->amount; } } @@ -221,9 +218,9 @@ int Inventory::count(int itemId) const for (std::vector< InventoryItem >::iterator i = poss.inventory.begin(), i_end = poss.inventory.end(); i != i_end; ++i) { - if (i->itemClassId == itemId) + if (i->itemId == itemId) { - nb += i->numberOfItemsInSlot; + nb += i->amount; } } @@ -238,23 +235,22 @@ void Inventory::freeIndex(int i) { poss.inventory.pop_back(); } - else if (poss.inventory[i + 1].itemClassId == 0) + else if (poss.inventory[i + 1].itemId == 0) { - it.itemClassId = 0; - it.numberOfItemsInSlot = poss.inventory[i + 1].numberOfItemsInSlot + 1; + it.itemId = 0; + it.amount = poss.inventory[i + 1].amount + 1; poss.inventory.erase(poss.inventory.begin() + i + 1); } else { - it.itemClassId = 0; - it.numberOfItemsInSlot = 1; + it.itemId = 0; + it.amount = 1; } - if (i > 0 && poss.inventory[i - 1].itemClassId == 0) + if (i > 0 && poss.inventory[i - 1].itemId == 0) { // Note: "it" is no longer a valid iterator. - poss.inventory[i - 1].numberOfItemsInSlot - += poss.inventory[i].numberOfItemsInSlot; + poss.inventory[i - 1].amount += poss.inventory[i].amount; poss.inventory.erase(poss.inventory.begin() + i); } } @@ -264,18 +260,18 @@ int Inventory::remove(int itemId, int amount) for (int i = poss.inventory.size() - 1; i >= 0; --i) { InventoryItem &it = poss.inventory[i]; - if (it.itemClassId == itemId) + if (it.itemId == itemId) { - int nb = std::min((int)it.numberOfItemsInSlot, amount); - it.numberOfItemsInSlot -= nb; + int nb = std::min((int)it.amount, amount); + it.amount -= nb; amount -= nb; msg.writeByte(getSlot(i) + EQUIP_CLIENT_INVENTORY); msg.writeShort(itemId); - msg.writeByte(it.numberOfItemsInSlot); + msg.writeByte(it.amount); // If the slot is empty, compress the inventory. - if (it.numberOfItemsInSlot == 0) + if (it.amount == 0) { freeIndex(i); } @@ -300,16 +296,16 @@ int Inventory::removeFromSlot(int slot, int amount) } InventoryItem &it = poss.inventory[i]; - int nb = std::min((int)it.numberOfItemsInSlot, amount); - it.numberOfItemsInSlot -= nb; + int nb = std::min((int)it.amount, amount); + it.amount -= nb; amount -= nb; msg.writeByte(slot + EQUIP_CLIENT_INVENTORY); - msg.writeShort(it.itemClassId); - msg.writeByte(it.numberOfItemsInSlot); + msg.writeShort(it.itemId); + msg.writeByte(it.amount); // If the slot is empty, compress the inventory. - if (it.numberOfItemsInSlot == 0) + if (it.amount == 0) { freeIndex(i); } diff --git a/src/serialize/characterdata.hpp b/src/serialize/characterdata.hpp new file mode 100644 index 00000000..2f24b579 --- /dev/null +++ b/src/serialize/characterdata.hpp @@ -0,0 +1,104 @@ +/* + * The Mana World Server + * Copyright 2007 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 _TMWSERV_SERIALIZE_CHARACTERDATA_HPP_ +#define _TMWSERV_SERIALIZE_CHARACTERDATA_HPP_ + +#include "defines.h" +#include "common/inventorydata.hpp" +#include "net/messagein.hpp" +#include "net/messageout.hpp" +#include "point.h" + +template< class T > +void serializeCharacterData(T const &data, MessageOut &msg) +{ + msg.writeLong(data.getDatabaseID()); + msg.writeString(data.getName()); + msg.writeByte(data.getGender()); + msg.writeByte(data.getHairStyle()); + msg.writeByte(data.getHairColor()); + msg.writeByte(data.getLevel()); + msg.writeShort(data.getMoney()); + + for (int i = 0; i < NB_BASE_ATTRIBUTES; ++i) + { + msg.writeByte(data.getBaseAttribute(i)); + } + + msg.writeShort(data.getMapId()); + Point const &pos = data.getPosition(); + msg.writeShort(pos.x); + msg.writeShort(pos.y); + + Possessions const &poss = data.getPossessions(); + for (int j = 0; j < EQUIPMENT_SLOTS; ++j) + { + msg.writeShort(poss.equipment[j]); + } + for (std::vector< InventoryItem >::const_iterator j = poss.inventory.begin(), + j_end = poss.inventory.end(); j != j_end; ++j) + { + msg.writeShort(j->itemId); + msg.writeByte(j->amount); + } +} + +template< class T > +void deserializeCharacterData(T &data, MessageIn &msg) +{ + data.setDatabaseID(msg.readLong()); + data.setName(msg.readString()); + data.setGender(msg.readByte()); + data.setHairStyle(msg.readByte()); + data.setHairColor(msg.readByte()); + data.setLevel(msg.readByte()); + data.setMoney(msg.readShort()); + + for (int i = 0; i < NB_BASE_ATTRIBUTES; ++i) + { + data.setBaseAttribute(i, msg.readByte()); + } + + data.setMapId(msg.readShort()); + + Point temporaryPoint; + temporaryPoint.x = msg.readShort(); + temporaryPoint.y = msg.readShort(); + data.setPosition(temporaryPoint); + + Possessions &poss = data.getPossessions(); + for (int j = 0; j < EQUIPMENT_SLOTS; ++j) + { + poss.equipment[j] = msg.readShort(); + } + poss.inventory.clear(); + while (msg.getUnreadLength()) + { + InventoryItem i; + i.itemId = msg.readShort(); + i.amount = msg.readByte(); + poss.inventory.push_back(i); + } +} + +#endif |