diff options
author | Guillaume Melquiond <guillaume.melquiond@gmail.com> | 2006-12-30 16:46:42 +0000 |
---|---|---|
committer | Guillaume Melquiond <guillaume.melquiond@gmail.com> | 2006-12-30 16:46:42 +0000 |
commit | 7a6bef223e5267eb377e39881de946251f7c7924 (patch) | |
tree | db55b9adcb0cbcb636219f325366debeaf582a6f /src | |
parent | 7d23129b8f16dfa73e3842ac8e91c61e5a3393aa (diff) | |
download | manaserv-7a6bef223e5267eb377e39881de946251f7c7924.tar.gz manaserv-7a6bef223e5267eb377e39881de946251f7c7924.tar.bz2 manaserv-7a6bef223e5267eb377e39881de946251f7c7924.tar.xz manaserv-7a6bef223e5267eb377e39881de946251f7c7924.zip |
Removed reference-counted pointers from the game server.
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/game-server/accountconnection.cpp | 6 | ||||
-rw-r--r-- | src/game-server/gameclient.cpp | 57 | ||||
-rw-r--r-- | src/game-server/gameclient.hpp | 70 | ||||
-rw-r--r-- | src/game-server/gamehandler.cpp | 64 | ||||
-rw-r--r-- | src/game-server/mapcomposite.cpp | 16 | ||||
-rw-r--r-- | src/game-server/mapcomposite.hpp | 4 | ||||
-rw-r--r-- | src/game-server/state.cpp | 10 | ||||
-rw-r--r-- | src/game-server/state.hpp | 4 | ||||
-rw-r--r-- | src/object.h | 14 | ||||
-rw-r--r-- | src/player.h | 8 |
11 files changed, 67 insertions, 188 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 49e49fb4..99434fa9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -108,8 +108,6 @@ tmwserv_game_SOURCES = \ skill.cpp \ game-server/accountconnection.hpp \ game-server/accountconnection.cpp \ - game-server/gameclient.hpp \ - game-server/gameclient.cpp \ game-server/gamehandler.hpp \ game-server/gamehandler.cpp \ game-server/itemmanager.hpp \ diff --git a/src/game-server/accountconnection.cpp b/src/game-server/accountconnection.cpp index 51fc13f9..a0f51f16 100644 --- a/src/game-server/accountconnection.cpp +++ b/src/game-server/accountconnection.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:$ + * $Id$ */ #include "configuration.h" @@ -30,7 +30,7 @@ #include "net/messageout.hpp" #include "utils/logger.h" -extern void registerGameClient(std::string const &, PlayerPtr); +extern void registerGameClient(std::string const &, Player *); bool AccountConnection::start() { @@ -75,7 +75,7 @@ void AccountConnection::processMessage(MessageIn &msg) ptr->setMapId(msg.readShort()); ptr->setSpeed(150); // TODO std::string token = msg.readString(32); - registerGameClient(token, PlayerPtr(ptr)); + registerGameClient(token, ptr); } break; default: diff --git a/src/game-server/gameclient.cpp b/src/game-server/gameclient.cpp deleted file mode 100644 index bd5d5492..00000000 --- a/src/game-server/gameclient.cpp +++ /dev/null @@ -1,57 +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$ - */ - -#include <cassert> - -#include "game-server/gameclient.hpp" -#include "game-server/gamehandler.hpp" -#include "game-server/state.hpp" - -GameClient::GameClient(ENetPeer *peer): - NetComputer(peer), - mCharacterPtr(NULL) -{ -} - -GameClient::~GameClient() -{ - unsetCharacter(); -} - -void GameClient::setCharacter(PlayerPtr ch) -{ - assert(mCharacterPtr.get() == NULL); - mCharacterPtr = ch; - assert(mCharacterPtr->mClient == NULL); - mCharacterPtr->mClient = this; - gameState->addObject(ObjectPtr(mCharacterPtr)); -} - -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/game-server/gameclient.hpp b/src/game-server/gameclient.hpp deleted file mode 100644 index 14f084db..00000000 --- a/src/game-server/gameclient.hpp +++ /dev/null @@ -1,70 +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_GAMECLIENT_H_ -#define _TMWSERV_GAMECLIENT_H_ - -#include <enet/enet.h> - -#include "player.h" -#include "net/netcomputer.hpp" - -class GameHandler; - -/** - * A connected computer with an associated character. - */ -class GameClient: public NetComputer -{ - public: - /** - * Constructor. - */ - GameClient(ENetPeer *peer); - - /** - * Destructor. - */ - ~GameClient(); - - /** - * Set the selected character associated with connection. - */ - void setCharacter(PlayerPtr ch); - - /** - * Deselect the character associated with connection. - */ - void unsetCharacter(); - - /** - * Get character associated with the connection. - */ - PlayerPtr getCharacter() { return mCharacterPtr; } - - private: - /** Character associated with the conneciton. */ - PlayerPtr mCharacterPtr; -}; - -#endif diff --git a/src/game-server/gamehandler.cpp b/src/game-server/gamehandler.cpp index b0b036e3..4347886f 100644 --- a/src/game-server/gamehandler.cpp +++ b/src/game-server/gamehandler.cpp @@ -25,7 +25,6 @@ #include <map> #include "map.h" -#include "game-server/gameclient.hpp" #include "game-server/gamehandler.hpp" #include "game-server/state.hpp" #include "net/messagein.hpp" @@ -33,9 +32,15 @@ #include "net/netcomputer.hpp" #include "utils/logger.h" +struct GameClient: NetComputer +{ + GameClient(ENetPeer *peer): NetComputer(peer), character(NULL) {} + Player *character; +}; + struct GamePendingLogin { - PlayerPtr character; + Player *character; int timeout; }; @@ -60,13 +65,15 @@ static GamePendingClients pendingClients; * Notification that a particular token has been given to allow a certain * player to enter the game. */ -void registerGameClient(std::string const &token, PlayerPtr ch) +void registerGameClient(std::string const &token, Player *ch) { GamePendingClients::iterator i = pendingClients.find(token); if (i != pendingClients.end()) { GameClient *computer = i->second; - computer->setCharacter(ch); + computer->character = ch; + ch->setClient(computer); + gameState->addObject(ch); pendingClients.erase(i); MessageOut result; result.writeShort(GPMSG_CONNECT_RESPONSE); @@ -92,17 +99,18 @@ GameHandler::startListen(enet_uint16 port) void GameHandler::removeOutdatedPending() { GamePendingLogins::iterator i = pendingLogins.begin(); - GamePendingLogins::iterator next; while (i != pendingLogins.end()) { - next = i; - ++next; if (--i->second.timeout <= 0) { - pendingLogins.erase(i); + delete i->second.character; + pendingLogins.erase(i++); + } + else + { + ++i; } - i = next; } } @@ -113,8 +121,8 @@ NetComputer *GameHandler::computerConnected(ENetPeer *peer) void GameHandler::computerDisconnected(NetComputer *computer) { - GamePendingClients::iterator i; - for (i = pendingClients.begin(); i != pendingClients.end(); ++i) + for (GamePendingClients::iterator i = pendingClients.begin(), + i_end = pendingClients.end(); i != i_end; ++i) { if (i->second == computer) { @@ -122,6 +130,11 @@ void GameHandler::computerDisconnected(NetComputer *computer) break; } } + if (Player *ch = static_cast< GameClient * >(computer)->character) + { + gameState->removeObject(ch); + delete ch; + } delete computer; } @@ -136,21 +149,24 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) GameClient &computer = *static_cast< GameClient * >(comp); MessageOut result; - if (computer.getCharacter().get() == NULL) { + if (computer.character == NULL) + { if (message.getId() != PGMSG_CONNECT) return; std::string magic_token = message.readString(32); GamePendingLogins::iterator i = pendingLogins.find(magic_token); if (i == pendingLogins.end()) { - GamePendingClients::iterator i; - for (i = pendingClients.begin(); i != pendingClients.end(); ++i) + for (GamePendingClients::iterator j = pendingClients.begin(), + j_end = pendingClients.end(); j != j_end; ++j) { - if (i->second == &computer) return; + if (j->second == &computer) return; } pendingClients.insert(std::make_pair(magic_token, &computer)); return; } - computer.setCharacter(i->second.character); + computer.character = i->second.character; + computer.character->setClient(&computer); + gameState->addObject(computer.character); pendingLogins.erase(i); result.writeShort(GPMSG_CONNECT_RESPONSE); result.writeByte(ERRMSG_OK); @@ -163,7 +179,7 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) case PGMSG_SAY: { std::string say = message.readString(); - gameState->sayAround(computer.getCharacter().get(), say); + gameState->sayAround(computer.character, say); } break; /* @@ -175,7 +191,7 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) // remove the item from world map // send feedback - computer.getCharacter()->addItem(itemId); + computer.character->addItem(itemId); result.writeShort(GPMSG_PICKUP_RESPONSE); result.writeByte(ERRMSG_OK); } break; @@ -186,7 +202,7 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) result.writeShort(GPMSG_USE_RESPONSE); - if (computer.getCharacter()->hasItem(itemId)) { + if (computer.character->hasItem(itemId)) { // use item // this should execute a script which will do the appropriate action // (the script will determine if the item is 1 use only) @@ -202,7 +218,7 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) unsigned x = message.readShort(); unsigned y = message.readShort(); Point dst = {x, y}; - computer.getCharacter()->setDestination(dst); + computer.character->setDestination(dst); // no response should be required } break; @@ -214,17 +230,17 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) char slot = message.readByte(); result.writeShort(GPMSG_EQUIP_RESPONSE); - result.writeByte(computer.getCharacter()->equip(slot) ? + result.writeByte(computer.character->equip(slot) ? ERRMSG_OK : ERRMSG_FAILURE); } break; */ case PGMSG_ATTACK: { - LOG_DEBUG("Player " << computer.getCharacter()->getPublicID() + LOG_DEBUG("Player " << computer.character->getPublicID() << " attacks", 0); - computer.getCharacter()->setDirection(message.readByte()); - computer.getCharacter()->setAttacking(true); + computer.character->setDirection(message.readByte()); + computer.character->setAttacking(true); } break; default: diff --git a/src/game-server/mapcomposite.cpp b/src/game-server/mapcomposite.cpp index 1591ad9f..b877a775 100644 --- a/src/game-server/mapcomposite.cpp +++ b/src/game-server/mapcomposite.cpp @@ -396,15 +396,15 @@ ZoneIterator MapComposite::getAroundPlayerIterator(Player *obj, int radius) cons return ZoneIterator(r2, this); }
-bool MapComposite::insert(ObjectPtr obj) +bool MapComposite::insert(Object *obj) { Point const &pos = obj->getPosition(); - zones[(pos.x / zoneDiam) + (pos.y / zoneDiam) * mapWidth].insert(obj.get()); + zones[(pos.x / zoneDiam) + (pos.y / zoneDiam) * mapWidth].insert(obj); int type = obj->getType(); if (type == OBJECT_MONSTER || type == OBJECT_PLAYER || type == OBJECT_NPC) { - if (!allocate(static_cast< MovingObject * >(obj.get()))) + if (!allocate(static_cast< MovingObject * >(obj))) { return false; } @@ -414,21 +414,21 @@ bool MapComposite::insert(ObjectPtr obj) return true; } -void MapComposite::remove(ObjectPtr obj) +void MapComposite::remove(Object *obj) { Point const &pos = obj->getPosition(); - zones[(pos.x / zoneDiam) + (pos.y / zoneDiam) * mapWidth].remove(obj.get()); + zones[(pos.x / zoneDiam) + (pos.y / zoneDiam) * mapWidth].remove(obj); int type = obj->getType(); if (type == OBJECT_MONSTER || type == OBJECT_PLAYER || type == OBJECT_NPC) { - deallocate(static_cast< MovingObject * >(obj.get())); + deallocate(static_cast< MovingObject * >(obj)); } for (Objects::iterator o = objects.begin(), o_end = objects.end(); o != o_end; ++o) { - if (o->get() == obj.get()) + if (*o == obj) { *o = *(o_end - 1); objects.pop_back(); @@ -454,7 +454,7 @@ void MapComposite::update() { continue; } - MovingObject *obj = static_cast< MovingObject * >(o->get()); + MovingObject *obj = static_cast< MovingObject * >(*o); Point const &pos1 = obj->getOldPosition(), &pos2 = obj->getPosition(); diff --git a/src/game-server/mapcomposite.hpp b/src/game-server/mapcomposite.hpp index 663271dd..84fbb2d0 100644 --- a/src/game-server/mapcomposite.hpp +++ b/src/game-server/mapcomposite.hpp @@ -154,12 +154,12 @@ class MapComposite { /** * Inserts an object on the map and sets its public ID. */ - bool insert(ObjectPtr); + bool insert(Object *); /** * Removes an object from the map. */ - void remove(ObjectPtr); + void remove(Object *); /** * Updates zones of every moving beings. diff --git a/src/game-server/state.cpp b/src/game-server/state.cpp index 95e093c2..f4eb75d5 100644 --- a/src/game-server/state.cpp +++ b/src/game-server/state.cpp @@ -44,7 +44,7 @@ State::State() being->setMapId(1); Point pos = { 720, 900 }; being->setPosition(pos); - addObject(ObjectPtr(being)); + addObject(being); } } @@ -246,7 +246,7 @@ void State::update() } void -State::addObject(ObjectPtr objectPtr) +State::addObject(Object *objectPtr) { unsigned mapId = objectPtr->getMapId(); MapComposite *map = loadMap(mapId); @@ -257,7 +257,7 @@ State::addObject(ObjectPtr objectPtr) } objectPtr->raiseUpdateFlags(NEW_ON_MAP); if (objectPtr->getType() != OBJECT_PLAYER) return; - Player *playerPtr = static_cast< Player * >(objectPtr.get()); + Player *playerPtr = static_cast< Player * >(objectPtr); /* Since the player doesn't know yet where on the world he is after * connecting to the map server, we send him an initial change map message. @@ -272,7 +272,7 @@ State::addObject(ObjectPtr objectPtr) } void -State::removeObject(ObjectPtr objectPtr) +State::removeObject(Object *objectPtr) { unsigned mapId = objectPtr->getMapId(); std::map< unsigned, MapComposite * >::iterator m = maps.find(mapId); @@ -282,7 +282,7 @@ State::removeObject(ObjectPtr objectPtr) int type = objectPtr->getType(); if (type == OBJECT_MONSTER || type == OBJECT_PLAYER || type == OBJECT_NPC) { - MovingObject *obj = static_cast< MovingObject * >(objectPtr.get()); + MovingObject *obj = static_cast< MovingObject * >(objectPtr); MessageOut msg(GPMSG_BEING_LEAVE); msg.writeShort(obj->getPublicID()); Point objectPos = obj->getPosition(); diff --git a/src/game-server/state.hpp b/src/game-server/state.hpp index 7fcfcf2f..95ba73b5 100644 --- a/src/game-server/state.hpp +++ b/src/game-server/state.hpp @@ -69,12 +69,12 @@ class State /** * Adds object to the map. */ - void addObject(ObjectPtr objectPtr); + void addObject(Object *); /** * Removes an object from the map. */ - void removeObject(ObjectPtr objectPtr); + void removeObject(Object *); /** * Says something around an object. diff --git a/src/object.h b/src/object.h index 3e296d54..b3511bf8 100644 --- a/src/object.h +++ b/src/object.h @@ -27,7 +27,6 @@ #include <vector> #include "point.h" -#include "utils/countedptr.h" class MapComposite; @@ -213,19 +212,8 @@ class MovingObject: public Object }; /** - * Type definition for a smart pointer to Object. - */ -typedef utils::CountedPtr<Object> ObjectPtr; - -/** - * Type definition for a smart pointer to MovingObject. - */ -typedef utils::CountedPtr<MovingObject> MovingObjectPtr; - - -/** * Type definition for a list of Objects. */ -typedef std::vector<ObjectPtr> Objects; +typedef std::vector< Object * > Objects; #endif // _TMWSERV_OBJECT_H_ diff --git a/src/player.h b/src/player.h index 091ace74..deffb752 100644 --- a/src/player.h +++ b/src/player.h @@ -229,6 +229,12 @@ class Player : public Being GameClient *getClient() const { return mClient; } + /** + * Sets client computer. + */ + void setClient(GameClient *c) + { mClient = c; } + private: Player(Player const &); Player &operator=(Player const &); @@ -246,8 +252,6 @@ class Player : public Being Inventory inventory; /**< Player inventory and Equipment */ bool mIsAttacking; /**< attacking state */ - - friend class GameClient; }; /** |