diff options
author | Guillaume Melquiond <guillaume.melquiond@gmail.com> | 2006-09-02 16:18:27 +0000 |
---|---|---|
committer | Guillaume Melquiond <guillaume.melquiond@gmail.com> | 2006-09-02 16:18:27 +0000 |
commit | 722d10af437c01de68887f551985ce74f9007122 (patch) | |
tree | 01f3eb3b937e5e40873bc789dd0a47889776aa1f /src | |
parent | 58c4bb86240a04e8e76dd8a5d68a053735c2a71c (diff) | |
download | manaserv-722d10af437c01de68887f551985ce74f9007122.tar.gz manaserv-722d10af437c01de68887f551985ce74f9007122.tar.bz2 manaserv-722d10af437c01de68887f551985ce74f9007122.tar.xz manaserv-722d10af437c01de68887f551985ce74f9007122.zip |
Added GameClient pointer to Player class for O(1) message sending.
Diffstat (limited to 'src')
-rw-r--r-- | src/gameclient.cpp | 3 | ||||
-rw-r--r-- | src/gamehandler.cpp | 18 | ||||
-rw-r--r-- | src/player.h | 20 |
3 files changed, 23 insertions, 18 deletions
diff --git a/src/gameclient.cpp b/src/gameclient.cpp index 562d85f4..26d8aaac 100644 --- a/src/gameclient.cpp +++ b/src/gameclient.cpp @@ -43,6 +43,8 @@ void GameClient::setCharacter(PlayerPtr ch) { assert(mCharacterPtr.get() == NULL); mCharacterPtr = ch; + assert(mCharacterPtr->mClient == NULL); + mCharacterPtr->mClient = this; gameState->addObject(ObjectPtr(mCharacterPtr)); gameState->informPlayer(mCharacterPtr); } @@ -52,5 +54,6 @@ void GameClient::unsetCharacter() if (mCharacterPtr.get() == NULL) return; // remove being from world gameState->removeObject(ObjectPtr(mCharacterPtr)); + mCharacterPtr->mClient = NULL; mCharacterPtr = PlayerPtr(NULL); } diff --git a/src/gamehandler.cpp b/src/gamehandler.cpp index cb38eacc..5186f92d 100644 --- a/src/gamehandler.cpp +++ b/src/gamehandler.cpp @@ -23,7 +23,7 @@ #include "gamehandler.h" -#include <iostream> +#include <cassert> #include <map> #include "gameclient.h" @@ -254,17 +254,7 @@ void GameHandler::sayAround(GameClient &computer, std::string const &text) void GameHandler::sendTo(PlayerPtr beingPtr, MessageOut &msg) { - /* TODO: This implementation is very inefficient. An alternative would be - * store the NetComputer reference with the player class, so that it can - * be directly accessed. - */ - for (NetComputers::iterator i = clients.begin(); i != clients.end(); ++i) - { - PlayerPtr clientChar = static_cast<GameClient *>(*i)->getCharacter(); - if (clientChar.get() == beingPtr.get()) - { - (*i)->send(msg); - break; - } - } + GameClient *client = beingPtr->getClient(); + assert(client != NULL); + client->send(msg); } diff --git a/src/player.h b/src/player.h index e812a73d..03917703 100644 --- a/src/player.h +++ b/src/player.h @@ -33,6 +33,8 @@ /** Maximum number of equipped slots */ const unsigned int MAX_EQUIP_SLOTS = 5; +class GameClient; + class Player : public Being { public: @@ -40,7 +42,8 @@ class Player : public Being Player(std::string const &name, int id = -1) : Being(OBJECT_PLAYER, 65535), mDatabaseID(id), - mName(name) + mName(name), + mClient(NULL) {} /** @@ -201,25 +204,32 @@ class Player : public Being unequip(unsigned char slot); /** - * Get database ID. + * Gets database ID. * - * @return the database ID, a negative number if none yet, or if not relevant. + * @return the database ID, a negative number if none yet. */ int getDatabaseID() const { return mDatabaseID; } /** - * Set database ID. + * Sets database ID. * The object shall not have any ID yet. */ void setDatabaseID(int id); + /** + * Gets client computer. + */ + GameClient *getClient() const + { return mClient; } + private: Player(Player const &); Player &operator=(Player const &); int mDatabaseID; /**< Player database ID (unique with respect to its type) */ std::string mName; /**< name of the being */ + GameClient *mClient; /**< client computer, directly set by GameClient */ Gender mGender; /**< gender of the being */ unsigned char mHairStyle; /**< Hair Style of the being */ unsigned char mHairColor; /**< Hair Color of the being */ @@ -231,6 +241,8 @@ class Player : public Being /** Equipped item ID's (from inventory) */ unsigned int equipment[MAX_EQUIP_SLOTS]; + + friend class GameClient; }; /** |