diff options
author | Guillaume Melquiond <guillaume.melquiond@gmail.com> | 2007-01-05 11:32:34 +0000 |
---|---|---|
committer | Guillaume Melquiond <guillaume.melquiond@gmail.com> | 2007-01-05 11:32:34 +0000 |
commit | 9a55df8602e624051e4f3a527c053d655f78c501 (patch) | |
tree | ad34a1c9b78f3fd6d89ec7eb1289a780b11bfd8f | |
parent | 9c0a75240812ae7d5f4836ee4b558086e383670a (diff) | |
download | manaserv-9a55df8602e624051e4f3a527c053d655f78c501.tar.gz manaserv-9a55df8602e624051e4f3a527c053d655f78c501.tar.bz2 manaserv-9a55df8602e624051e4f3a527c053d655f78c501.tar.xz manaserv-9a55df8602e624051e4f3a527c053d655f78c501.zip |
Added full update of Inventory on Player connection.
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | src/defines.h | 1 | ||||
-rw-r--r-- | src/game-server/gamehandler.cpp | 16 | ||||
-rw-r--r-- | src/game-server/inventory.cpp | 55 | ||||
-rw-r--r-- | src/game-server/inventory.hpp | 30 | ||||
-rw-r--r-- | src/game-server/player.hpp | 1 |
6 files changed, 85 insertions, 24 deletions
@@ -18,7 +18,11 @@ * src/game-server/testing.cpp: Added a piece of equipment. * src/game-server/defines.h, src/game-server/gamehandler.cpp: Implemented item dropping. - * src/inventory.cpp: Fixed reported amounts of inventory items. + * src/game-server/player.hpp: Removed useless dependency on Inventory. + * src/game-server/inventory.cpp: Fixed reported amounts of inventory + items. Embeded handling of client message directly into the class. + * src/defines.h, src/game-server/gamehandler.cpp: Added full update of + Inventory on Player connection. 2007-01-04 Guillaume Melquiond <guillaume.melquiond@gmail.com> diff --git a/src/defines.h b/src/defines.h index 68e6b4e9..25e6bb56 100644 --- a/src/defines.h +++ b/src/defines.h @@ -140,6 +140,7 @@ enum { PGMSG_DROP = 0x0111, // B slot, B amount PGMSG_EQUIP = 0x0112, // B slot GPMSG_INVENTORY = 0x0120, // { B slot, W item id [, B amount] }* + GPMSG_INVENTORY_FULL = 0x0121, // { B slot, W item id [, B amount] }* GPMSG_BEING_ENTER = 0x0200, // B type, W being id // player: S name, B hair style, B hair color, B gender // monster: W type id diff --git a/src/game-server/gamehandler.cpp b/src/game-server/gamehandler.cpp index 4d963129..b171a9b9 100644 --- a/src/game-server/gamehandler.cpp +++ b/src/game-server/gamehandler.cpp @@ -25,6 +25,7 @@ #include <map> #include "game-server/gamehandler.hpp" +#include "game-server/inventory.hpp" #include "game-server/item.hpp" #include "game-server/itemmanager.hpp" #include "game-server/map.hpp" @@ -81,11 +82,11 @@ static void linkCharacter(GameClient *computer, Player *ch) computer->character = ch; computer->status = CLIENT_CONNECTED; ch->setClient(computer); - gameState->insert(ch); - MessageOut result; - result.writeShort(GPMSG_CONNECT_RESPONSE); + MessageOut result(GPMSG_CONNECT_RESPONSE); result.writeByte(ERRMSG_OK); computer->send(result); + gameState->insert(ch); + Inventory(ch).sendFull(); } /** @@ -252,10 +253,9 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) Point opos = o->getPosition(); if (o->getType() == OBJECT_ITEM && opos.x == x && opos.y == y) { - result.writeShort(GPMSG_INVENTORY); Item *item = static_cast< Item * >(o); ItemClass *ic = item->getItemClass(); - Inventory(computer.character, result) + Inventory(computer.character) .insert(ic->getDatabaseID(), item->getAmount()); gameState->remove(item); break; @@ -268,10 +268,9 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) { int slot = message.readByte(); int amount = message.readByte(); - Inventory inv(computer.character, result); + Inventory inv(computer.character); if (ItemClass *ic = itemManager->getItem(inv.getItem(slot))) { - result.writeShort(GPMSG_INVENTORY); int nb = inv.removeFromSlot(slot, amount); Item *item = new Item(ic, amount - nb); item->setMapId(computer.character->getMapId()); @@ -293,8 +292,7 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) case PGMSG_EQUIP: { int slot = message.readByte(); - result.writeShort(GPMSG_INVENTORY); - Inventory(computer.character, result).equip(slot); + Inventory(computer.character).equip(slot); } break; case PGMSG_ATTACK: diff --git a/src/game-server/inventory.cpp b/src/game-server/inventory.cpp index f1c2eabd..4b64728e 100644 --- a/src/game-server/inventory.cpp +++ b/src/game-server/inventory.cpp @@ -24,11 +24,58 @@ #include <algorithm> #include <cassert> +#include "defines.h" +#include "game-server/gamehandler.hpp" #include "game-server/inventory.hpp" #include "game-server/itemmanager.hpp" #include "net/messageout.hpp" -int Inventory::getItem(int slot) +Inventory::Inventory(Player *p) + : poss(p->getPossessions()), msg(GPMSG_INVENTORY), client(p) +{} + +Inventory::~Inventory() +{ + if (msg.getLength() > 2) + { + gameHandler->sendTo(client, msg); + } +} + +void Inventory::sendFull() const +{ + MessageOut m(GPMSG_INVENTORY_FULL); + + for (int i = 0; i < EQUIPMENT_SLOTS; ++i) + { + if (int id = poss.equipment[i]) + { + m.writeByte(i); + m.writeShort(id); + } + } + + int slot = EQUIP_CLIENT_INVENTORY; + for (std::vector< InventoryItem >::iterator i = poss.inventory.begin(), + i_end = poss.inventory.end(); i != i_end; ++i) + { + if (i->itemId) + { + m.writeByte(slot); + m.writeShort(i->itemId); + m.writeByte(i->amount); + ++slot; + } + else + { + slot += i->amount; + } + } + + gameHandler->sendTo(client, m); +} + +int Inventory::getItem(int slot) const { for (std::vector< InventoryItem >::iterator i = poss.inventory.begin(), i_end = poss.inventory.end(); i != i_end; ++i) @@ -48,7 +95,7 @@ int Inventory::getItem(int slot) return 0; } -int Inventory::getIndex(int slot) +int Inventory::getIndex(int slot) const { int index = 0; for (std::vector< InventoryItem >::iterator i = poss.inventory.begin(), @@ -69,7 +116,7 @@ int Inventory::getIndex(int slot) return -1; } -int Inventory::getSlot(int index) +int Inventory::getSlot(int index) const { int slot = 0; for (std::vector< InventoryItem >::iterator i = poss.inventory.begin(), @@ -164,7 +211,7 @@ int Inventory::insert(int itemId, int amount) return amount > 0 ? fillFreeSlot(itemId, amount, maxPerSlot) : 0; } -int Inventory::count(int itemId) +int Inventory::count(int itemId) const { int nb = 0; diff --git a/src/game-server/inventory.hpp b/src/game-server/inventory.hpp index 950dc013..06a4ad03 100644 --- a/src/game-server/inventory.hpp +++ b/src/game-server/inventory.hpp @@ -25,6 +25,8 @@ #define INVENTORY_H #include "playerdata.hpp" +#include "game-server/player.hpp" +#include "net/messageout.hpp" enum { @@ -57,7 +59,7 @@ enum EQUIP_CLIENT_INVENTORY = 32 }; -class MessageOut; +class GameClient; /** * Class used to handle Player possessions and prepare outgoing messages. @@ -65,11 +67,21 @@ class MessageOut; class Inventory { Possessions &poss; - MessageOut &msg; + MessageOut msg; + Player *client; + public: - Inventory(PlayerData *p, MessageOut &m) - : poss(p->getPossessions()), msg(m) - {} + Inventory(Player *); + + /** + * Sends the update message to the client. + */ + ~Inventory(); + + /** + * Sends a complete inventory update to the client. + */ + void sendFull() const; /** * Equips item from given inventory slot. @@ -103,12 +115,12 @@ class Inventory /** * Counts number of items with given ID. */ - int count(int itemId); + int count(int itemId) const; /** * Gets the ID of the items in a given slot. */ - int getItem(int slot); + int getItem(int slot) const; private: /** @@ -125,12 +137,12 @@ class Inventory /** * Gets the real index associated to a slot. */ - int getIndex(int slot); + int getIndex(int slot) const; /** * Gets the slot number of an inventory index. */ - int getSlot(int index); + int getSlot(int index) const; }; diff --git a/src/game-server/player.hpp b/src/game-server/player.hpp index 63bf06c7..5a0336a8 100644 --- a/src/game-server/player.hpp +++ b/src/game-server/player.hpp @@ -28,7 +28,6 @@ #include "playerdata.hpp" #include "game-server/being.hpp" -#include "game-server/inventory.hpp" class GameClient; |