diff options
Diffstat (limited to 'src/game-server')
-rw-r--r-- | src/game-server/accountconnection.cpp | 4 | ||||
-rw-r--r-- | src/game-server/character.cpp | 2 | ||||
-rw-r--r-- | src/game-server/gamehandler.cpp | 14 | ||||
-rw-r--r-- | src/game-server/inventory.cpp | 7 | ||||
-rw-r--r-- | src/game-server/itemmanager.cpp | 26 | ||||
-rw-r--r-- | src/game-server/itemmanager.hpp | 41 | ||||
-rw-r--r-- | src/game-server/main-game.cpp | 23 | ||||
-rw-r--r-- | src/game-server/mapmanager.cpp | 15 | ||||
-rw-r--r-- | src/game-server/mapmanager.hpp | 65 | ||||
-rw-r--r-- | src/game-server/spawnarea.cpp | 2 | ||||
-rw-r--r-- | src/game-server/state.cpp | 31 | ||||
-rw-r--r-- | src/game-server/state.hpp | 81 | ||||
-rw-r--r-- | src/game-server/testing.cpp | 15 | ||||
-rw-r--r-- | src/game-server/trigger.cpp | 2 |
14 files changed, 158 insertions, 170 deletions
diff --git a/src/game-server/accountconnection.cpp b/src/game-server/accountconnection.cpp index fa09792..3bdd971 100644 --- a/src/game-server/accountconnection.cpp +++ b/src/game-server/accountconnection.cpp @@ -48,7 +48,7 @@ bool AccountConnection::start() MessageOut msg(GAMSG_REGISTER); msg.writeString(config.getValue("gameServerAddress", "localhost")); msg.writeShort(int(config.getValue("gameServerPort", DEFAULT_SERVER_PORT + 3))); - MapManager::Maps const &m = mapManager->getMaps(); + MapManager::Maps const &m = MapManager::getMaps(); for (MapManager::Maps::const_iterator i = m.begin(), i_end = m.end(); i != i_end; ++i) { msg.writeShort(i->first); @@ -81,7 +81,7 @@ void AccountConnection::processMessage(MessageIn &msg) case AGMSG_ACTIVE_MAP: { int id = msg.readShort(); - mapManager->raiseActive(id); + MapManager::raiseActive(id); } break; case AGMSG_REDIRECT_RESPONSE: diff --git a/src/game-server/character.cpp b/src/game-server/character.cpp index 3e5d919..4ab26c9 100644 --- a/src/game-server/character.cpp +++ b/src/game-server/character.cpp @@ -120,5 +120,5 @@ int Character::getMapId() const void Character::setMapId(int id) { - setMap(mapManager->getMap(id)); + setMap(MapManager::getMap(id)); } diff --git a/src/game-server/gamehandler.cpp b/src/game-server/gamehandler.cpp index 8c33bf1..71b0cfe 100644 --- a/src/game-server/gamehandler.cpp +++ b/src/game-server/gamehandler.cpp @@ -65,7 +65,7 @@ void GameHandler::computerDisconnected(NetComputer *comp) } else if (Character *ch = computer.character) { - gameState->remove(ch); + GameState::remove(ch); delete ch; } delete &computer; @@ -138,7 +138,7 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) case PGMSG_SAY: { std::string say = message.readString(); - gameState->sayAround(computer.character, say); + GameState::sayAround(computer.character, say); } break; case PGMSG_PICKUP: @@ -162,7 +162,7 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) ItemClass *ic = item->getItemClass(); Inventory(computer.character) .insert(ic->getDatabaseID(), item->getAmount()); - gameState->remove(item); + GameState::remove(item); break; } } @@ -174,13 +174,13 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) int slot = message.readByte(); int amount = message.readByte(); Inventory inv(computer.character); - if (ItemClass *ic = itemManager->getItem(inv.getItem(slot))) + if (ItemClass *ic = ItemManager::getItem(inv.getItem(slot))) { int nb = inv.removeFromSlot(slot, amount); Item *item = new Item(ic, amount - nb); item->setMap(computer.character->getMap()); item->setPosition(computer.character->getPosition()); - gameState->insert(item); + GameState::insert(item); } } break; @@ -248,7 +248,7 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) magic_token); } // TODO: implement a delayed remove - gameState->remove(computer.character); + GameState::remove(computer.character); accountHandler->sendCharacterData(computer.character); @@ -328,7 +328,7 @@ GameHandler::tokenMatched(GameClient* computer, Character* character) result.writeByte(ERRMSG_OK); computer->send(result); - gameState->insert(character); + GameState::insert(character); Inventory(character).sendFull(); } diff --git a/src/game-server/inventory.cpp b/src/game-server/inventory.cpp index c8cea6d..4204304 100644 --- a/src/game-server/inventory.cpp +++ b/src/game-server/inventory.cpp @@ -27,6 +27,7 @@ #include "defines.h" #include "game-server/gamehandler.hpp" #include "game-server/inventory.hpp" +#include "game-server/item.hpp" #include "game-server/itemmanager.hpp" #include "net/messageout.hpp" @@ -181,7 +182,7 @@ int Inventory::fillFreeSlot(int itemId, int amount, int maxPerSlot) int Inventory::insert(int itemId, int amount) { int slot = 0; - int maxPerSlot = itemManager->getItem(itemId)->getMaxPerSlot(); + int maxPerSlot = ItemManager::getItem(itemId)->getMaxPerSlot(); for (std::vector< InventoryItem >::iterator i = poss.inventory.begin(), i_end = poss.inventory.end(); i != i_end; ++i) @@ -323,7 +324,7 @@ bool Inventory::equip(int slot) int availableSlots = 0, firstSlot = 0, secondSlot = 0; - switch (itemManager->getItem(itemId)->getType()) + switch (ItemManager::getItem(itemId)->getType()) { case ITEM_EQUIPMENT_TWO_HANDS_WEAPON: { @@ -405,7 +406,7 @@ bool Inventory::equip(int slot) { case 2: if (id && !poss.equipment[secondSlot] && - itemManager->getItem(id)->getType() != + ItemManager::getItem(id)->getType() != ITEM_EQUIPMENT_TWO_HANDS_WEAPON) { // The first slot is full and the second slot is empty. diff --git a/src/game-server/itemmanager.cpp b/src/game-server/itemmanager.cpp index 8802ce7..ea63be2 100644 --- a/src/game-server/itemmanager.cpp +++ b/src/game-server/itemmanager.cpp @@ -21,14 +21,19 @@ * $Id$ */ -#include "game-server/itemmanager.hpp" +#include <map> #include "defines.h" #include "resourcemanager.h" +#include "game-server/item.hpp" +#include "game-server/itemmanager.hpp" #include "utils/logger.h" #include "utils/xml.hpp" -ItemManager::ItemManager(std::string const &itemReferenceFile) +typedef std::map< int, ItemClass * > ItemClasses; +static ItemClasses itemClasses; /**< Item reference */ + +void ItemManager::initialize(std::string const &itemReferenceFile) { ResourceManager *resman = ResourceManager::getInstance(); int size; @@ -106,7 +111,7 @@ ItemManager::ItemManager(std::string const &itemReferenceFile) item->setMaxPerSlot(maxPerSlot); item->setScriptName(scriptName); item->setModifiers(modifiers); - mItemReference[id] = item; + itemClasses[id] = item; ++nbItems; if (maxPerSlot == 0) @@ -131,8 +136,17 @@ ItemManager::ItemManager(std::string const &itemReferenceFile) xmlFreeDoc(doc); } -ItemClass *ItemManager::getItem(int itemId) const +void ItemManager::deinitialize() +{ + for (ItemClasses::iterator i = itemClasses.begin(), i_end = itemClasses.end(); i != i_end; ++i) + { + delete i->second; + } + itemClasses.clear(); +} + +ItemClass *ItemManager::getItem(int itemId) { - std::map< int, ItemClass * >::const_iterator i = mItemReference.find(itemId); - return i != mItemReference.end() ? i->second : NULL; + ItemClasses::const_iterator i = itemClasses.find(itemId); + return i != itemClasses.end() ? i->second : NULL; } diff --git a/src/game-server/itemmanager.hpp b/src/game-server/itemmanager.hpp index a144fa8..25e3e94 100644 --- a/src/game-server/itemmanager.hpp +++ b/src/game-server/itemmanager.hpp @@ -24,31 +24,26 @@ #ifndef _TMW_ITEMMANAGER_H #define _TMW_ITEMMANAGER_H -#include <map> +#include <string> -#include "game-server/item.hpp" +class ItemClass; -/** - * The Item Manager loads the item reference database - * and also offers an API to items information, and more. - */ -class ItemManager +namespace ItemManager { - public: - /** - * Constructor (loads item reference file) - */ - ItemManager(std::string const &itemReferenceFile); - - /** - * Gives an Item having the demanded information. - */ - ItemClass *getItem(int itemId) const; - - private: - std::map< int, ItemClass * > mItemReference; /**< Item reference */ -}; - -extern ItemManager *itemManager; + /** + * Loads item reference file. + */ + void initialize(std::string const &); + + /** + * Destroy item classes. + */ + void deinitialize(); + + /** + * Gets the ItemClass having the given ID. + */ + ItemClass *getItem(int itemId); +} #endif diff --git a/src/game-server/main-game.cpp b/src/game-server/main-game.cpp index f579ece..72aaef5 100644 --- a/src/game-server/main-game.cpp +++ b/src/game-server/main-game.cpp @@ -84,21 +84,12 @@ Configuration config; /**< XML config reader */ utils::StringFilter *stringFilter; /**< Slang's Filter */ -/** Item manager */ -ItemManager *itemManager; - -/** Map manager */ -MapManager *mapManager; - /** Core game message handler */ GameHandler *gameHandler; /** Account server message handler */ AccountConnection *accountHandler; -/** Global game state */ -State *gameState; - /** Callback used when SIGQUIT signal is received. */ void closeGracefully(int dummy) { @@ -171,9 +162,9 @@ void initialize() // Initialize the slang's and double quotes filter. stringFilter = new StringFilter(&config); // Initialize the map manager - mapManager = new MapManager(DEFAULT_MAPSDB_FILE); + MapManager::initialize(DEFAULT_MAPSDB_FILE); // Initialize the item manager - itemManager = new ItemManager(DEFAULT_ITEMSDB_FILE); + ItemManager::initialize(DEFAULT_ITEMSDB_FILE); // --- Initialize the global handlers // FIXME: Make the global handlers global vars or part of a bigger @@ -243,8 +234,8 @@ void deinitialize() // Destroy Managers delete stringFilter; - delete itemManager; - delete mapManager; + ItemManager::deinitialize(); + MapManager::deinitialize(); PHYSFS_deinit(); } @@ -338,9 +329,6 @@ int main(int argc, char *argv[]) return 3; } - // Create state machine - gameState = new State; - // Initialize world timer worldTimer.start(); @@ -365,7 +353,7 @@ int main(int argc, char *argv[]) accountHandler->process(); gameHandler->process(); // Update all active objects/beings - gameState->update(); + GameState::update(); // Send potentially urgent outgoing messages gameHandler->flush(); } @@ -375,6 +363,5 @@ int main(int argc, char *argv[]) LOG_INFO("Received: Quit signal, closing down..."); gameHandler->stopListen(); accountHandler->stop(); - delete gameState; deinitialize(); } diff --git a/src/game-server/mapmanager.cpp b/src/game-server/mapmanager.cpp index 7de489c..58aace1 100644 --- a/src/game-server/mapmanager.cpp +++ b/src/game-server/mapmanager.cpp @@ -31,7 +31,17 @@ #include "utils/logger.h" #include "utils/xml.hpp" -MapManager::MapManager(std::string const &mapReferenceFile) +/** + * List of all the game maps, be they present or not on this server. + */ +static MapManager::Maps maps; + +MapManager::Maps const &MapManager::getMaps() +{ + return maps; +} + +void MapManager::initialize(std::string const &mapReferenceFile) { ResourceManager *resman = ResourceManager::getInstance(); int size; @@ -80,12 +90,13 @@ MapManager::MapManager(std::string const &mapReferenceFile) xmlFreeDoc(doc); } -MapManager::~MapManager() +void MapManager::deinitialize() { for (Maps::iterator i = maps.begin(), i_end = maps.end(); i != i_end; ++i) { delete i->second; } + maps.clear(); } MapComposite *MapManager::getMap(int mapId) diff --git a/src/game-server/mapmanager.hpp b/src/game-server/mapmanager.hpp index 63ef32c..432b0b1 100644 --- a/src/game-server/mapmanager.hpp +++ b/src/game-server/mapmanager.hpp @@ -29,43 +29,34 @@ class MapComposite; -/** - * MapManager loads/unloads maps - */ -class MapManager +namespace MapManager { - public: - typedef std::map< int, MapComposite * > Maps; - - /** - * Constructor (loads map reference file). - */ - MapManager(std::string const &); - - /** - * Returns the requested map. - */ - MapComposite *getMap(int); - - /** - * Returns all the maps. - */ - Maps const &getMaps() const { return maps; } - - /** - * Sets the activity status of the map. - */ - void raiseActive(int); - - /** - * Destructor. - */ - ~MapManager(); - - private: - Maps maps; -}; - -extern MapManager *mapManager; + typedef std::map< int, MapComposite * > Maps; + + /** + * Loads map reference file and prepares maps. + */ + void initialize(std::string const &); + + /** + * Destroy loaded maps. + */ + void deinitialize(); + + /** + * Returns the requested map. + */ + MapComposite *getMap(int); + + /** + * Returns all the maps. + */ + Maps const &getMaps(); + + /** + * Sets the activity status of the map. + */ + void raiseActive(int); +} #endif diff --git a/src/game-server/spawnarea.cpp b/src/game-server/spawnarea.cpp index 5e2c041..c9d53e0 100644 --- a/src/game-server/spawnarea.cpp +++ b/src/game-server/spawnarea.cpp @@ -79,7 +79,7 @@ SpawnArea::update() being->setMap(map); being->setPosition(position); DelayedEvent e = { EVENT_INSERT }; - gameState->enqueueEvent(being, e); + GameState::enqueueEvent(being, e); mNumBeings++; } diff --git a/src/game-server/state.cpp b/src/game-server/state.cpp index 3903d93..b1095f4 100644 --- a/src/game-server/state.cpp +++ b/src/game-server/state.cpp @@ -35,7 +35,17 @@ #include "net/messageout.hpp" #include "utils/logger.h" -void State::updateMap(MapComposite *map) +typedef std::map< Object *, DelayedEvent > DelayedEvents; + +/** + * List of delayed events. + */ +static DelayedEvents delayedEvents; + +/** + * Updates object states on the map. + */ +static void updateMap(MapComposite *map) { // 1. update object status. std::vector< Thing * > const &things = map->getEverything(); @@ -67,7 +77,7 @@ void State::updateMap(MapComposite *map) if ((*i)->getUpdateFlags() & UPDATEFLAG_REMOVE) { DelayedEvent e = { EVENT_REMOVE}; - enqueueEvent((*i), e); + GameState::enqueueEvent((*i), e); } } @@ -75,7 +85,10 @@ void State::updateMap(MapComposite *map) map->update(); } -void State::informPlayer(MapComposite *map, Character *p) +/** + * Informs a player of what happened around the character. + */ +static void informPlayer(MapComposite *map, Character *p) { MessageOut moveMsg(GPMSG_BEINGS_MOVE); MessageOut damageMsg(GPMSG_BEINGS_DAMAGE); @@ -273,14 +286,14 @@ void State::informPlayer(MapComposite *map, Character *p) static bool dbgLockObjects; #endif -void State::update() +void GameState::update() { # ifndef NDEBUG dbgLockObjects = true; # endif // Update game state (update AI, etc.) - MapManager::Maps const &maps = mapManager->getMaps(); + MapManager::Maps const &maps = MapManager::getMaps(); for (MapManager::Maps::const_iterator m = maps.begin(), m_end = maps.end(); m != m_end; ++m) { MapComposite *map = m->second; @@ -365,7 +378,7 @@ void State::update() delayedEvents.clear(); } -void State::insert(Thing *ptr) +void GameState::insert(Thing *ptr) { assert(!dbgLockObjects); MapComposite *map = ptr->getMap(); @@ -392,7 +405,7 @@ void State::insert(Thing *ptr) } } -void State::remove(Thing *ptr) +void GameState::remove(Thing *ptr) { assert(!dbgLockObjects); MapComposite *map = ptr->getMap(); @@ -433,7 +446,7 @@ void State::remove(Thing *ptr) map->remove(ptr); } -void State::enqueueEvent(Object *ptr, DelayedEvent const &e) +void GameState::enqueueEvent(Object *ptr, DelayedEvent const &e) { std::pair< DelayedEvents::iterator, bool > p = delayedEvents.insert(std::make_pair(ptr, e)); @@ -444,7 +457,7 @@ void State::enqueueEvent(Object *ptr, DelayedEvent const &e) } } -void State::sayAround(Object *obj, std::string text) +void GameState::sayAround(Object *obj, std::string const &text) { MessageOut msg(GPMSG_SAY); msg.writeShort(!obj->canMove() ? 65535 : diff --git a/src/game-server/state.hpp b/src/game-server/state.hpp index 0d03743..356d5ba 100644 --- a/src/game-server/state.hpp +++ b/src/game-server/state.hpp @@ -24,13 +24,11 @@ #ifndef _TMW_SERVER_STATE_ #define _TMW_SERVER_STATE_ -#include <map> #include <string> class MapComposite; class Thing; class Object; -class Character; enum { @@ -45,57 +43,34 @@ struct DelayedEvent MapComposite *map; }; -/** - * State class contains all information/procedures associated with the game - * world's state. - */ -class State +namespace GameState { - typedef std::map< Object *, DelayedEvent > DelayedEvents; - - /** - * List of delayed events. - */ - DelayedEvents delayedEvents; - - /** - * Updates object states on the map. - */ - void updateMap(MapComposite *); - - /** - * Informs a player of what happened around the character. - */ - void informPlayer(MapComposite *, Character *); - - public: - - /** - * Updates game state (contains core server logic). - */ - void update(); - - /** - * Inserts an object on the map. - */ - void insert(Thing *); - - /** - * Removes an object from the map. - */ - void remove(Thing *); - - /** - * Enqueues an event. It will be executed at end of update. - */ - void enqueueEvent(Object *, DelayedEvent const &); - - /** - * Says something around an object. - */ - void sayAround(Object *, std::string text); -}; - -extern State *gameState; + /** + * Updates game state (contains core server logic). + */ + void update(); + + /** + * Inserts an object in the game world. + * No update may be in progress. + */ + void insert(Thing *); + + /** + * Removes an object from the game world. + * No update may be in progress. + */ + void remove(Thing *); + + /** + * Enqueues an event. It will be executed at end of update. + */ + void enqueueEvent(Object *, DelayedEvent const &); + + /** + * Says something around an object. + */ + void sayAround(Object *, std::string const &text); +} #endif diff --git a/src/game-server/testing.cpp b/src/game-server/testing.cpp index 495b361..8bde8e6 100644 --- a/src/game-server/testing.cpp +++ b/src/game-server/testing.cpp @@ -5,6 +5,7 @@ #include <cassert> #include "defines.h" +#include "game-server/item.hpp" #include "game-server/itemmanager.hpp" #include "game-server/mapcomposite.hpp" #include "game-server/mapmanager.hpp" @@ -14,21 +15,21 @@ static void dropItem(MapComposite *map, int x, int y, int type) { - ItemClass *ic = itemManager->getItem(type); + ItemClass *ic = ItemManager::getItem(type); assert(ic); Item *i = new Item(ic, 1); i->setMap(map); Point pos(x, y); i->setPosition(pos); - gameState->insert(i); + GameState::insert(i); } void testingMap(MapComposite *map) { static Rectangle rectA = { 56 * 32, 12 * 32, 5 * 32, 32 }; - static WarpAction warpA(mapManager->getMap(3), 44 * 32 + 16, 80 * 32 + 16); + static WarpAction warpA(MapManager::getMap(3), 44 * 32 + 16, 80 * 32 + 16); static Rectangle rectB = { 42 * 32, 88 * 32, 5 * 32, 32 }; - static WarpAction warpB(mapManager->getMap(1), 58 * 32 + 16, 17 * 32 + 16); + static WarpAction warpB(MapManager::getMap(1), 58 * 32 + 16, 17 * 32 + 16); switch (map->getID()) { @@ -36,10 +37,10 @@ void testingMap(MapComposite *map) { // Create maggot spawn area Rectangle maggotSpawnRect = { 720, 900, 320, 320 }; - gameState->insert(new SpawnArea(map, maggotSpawnRect)); + GameState::insert(new SpawnArea(map, maggotSpawnRect)); // Portal to map 3 - gameState->insert(new TriggerArea(map, rectA, &warpA)); + GameState::insert(new TriggerArea(map, rectA, &warpA)); // Drop some items dropItem(map, 58 * 32 + 16, 20 * 32 + 16, 508); @@ -49,7 +50,7 @@ void testingMap(MapComposite *map) case 3: { // Portal to map 1 - gameState->insert(new TriggerArea(map, rectB, &warpB)); + GameState::insert(new TriggerArea(map, rectB, &warpB)); } break; } } diff --git a/src/game-server/trigger.cpp b/src/game-server/trigger.cpp index b1cdc7c..3320506 100644 --- a/src/game-server/trigger.cpp +++ b/src/game-server/trigger.cpp @@ -33,7 +33,7 @@ void WarpAction::process(Object *obj) if (obj->getType() == OBJECT_CHARACTER) { DelayedEvent e = { EVENT_WARP, mX, mY, mMap }; - gameState->enqueueEvent(obj, e); + GameState::enqueueEvent(obj, e); } } |