diff options
author | Guillaume Melquiond <guillaume.melquiond@gmail.com> | 2006-06-17 10:29:02 +0000 |
---|---|---|
committer | Guillaume Melquiond <guillaume.melquiond@gmail.com> | 2006-06-17 10:29:02 +0000 |
commit | b344d0f5f2b31e3b01f47fe3e3c57d41315fabf7 (patch) | |
tree | ca1d9be395c3934fd24c38f01f0099c7fa01be9b | |
parent | 6922c9e54bd495bd79ad5766fafcbff4e5565460 (diff) | |
download | manaserv-b344d0f5f2b31e3b01f47fe3e3c57d41315fabf7.tar.gz manaserv-b344d0f5f2b31e3b01f47fe3e3c57d41315fabf7.tar.bz2 manaserv-b344d0f5f2b31e3b01f47fe3e3c57d41315fabf7.tar.xz manaserv-b344d0f5f2b31e3b01f47fe3e3c57d41315fabf7.zip |
Started handling beings on maps.
-rw-r--r-- | ChangeLog | 9 | ||||
-rw-r--r-- | src/client.cpp | 26 | ||||
-rw-r--r-- | src/defines.h | 4 | ||||
-rw-r--r-- | src/gamehandler.cpp | 7 | ||||
-rw-r--r-- | src/main.cpp | 8 | ||||
-rw-r--r-- | src/state.cpp | 193 | ||||
-rw-r--r-- | src/state.h | 45 |
7 files changed, 119 insertions, 173 deletions
@@ -1,4 +1,9 @@ -2006-05-20 Guillaume Melquiond <guillaume.melquiond@gmail.com> +2006-06-17 Guillaume Melquiond <guillaume.melquiond@gmail.com> + + * src/state.cpp, src/state.h, src/defines.h, src/client.cpp, + src/main.cpp, src/gamehandler.cpp: Started handling beings on maps. + +2006-06-11 Guillaume Melquiond <guillaume.melquiond@gmail.com> * src/chatchannelmanager.cpp: Fixed public/private ranges. * src/chathandler.h: Removed useless prototype. @@ -6,7 +11,7 @@ src/gamehandler.cpp, src/client.cpp: Sanitized message packets and errors. -2006-05-20 Guillaume Melquiond <guillaume.melquiond@gmail.com> +2006-05-23 Guillaume Melquiond <guillaume.melquiond@gmail.com> * src/chatchannelmanager.cpp, src/chatchannelmanager.h: Removed unwarranted constness. Used binary searches instead of loops. diff --git a/src/client.cpp b/src/client.cpp index d90edfbd..4ee33385 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -672,6 +672,32 @@ void parsePacket(char *data, int recvLength) { << msg.readString() << std::endl; } break; + case GPMSG_BEING_LEAVE: + { + switch (msg.readByte()) { + case OBJECT_PLAYER: + std::cout << "Player " << msg.readLong() << " left map." << std::endl; + break; + default: + std::cout << "Unknown being left map." << std::endl; + } + } break; + + case GPMSG_BEING_ENTER: + { + switch (msg.readByte()) { + case OBJECT_PLAYER: + std::cout << "Player " << msg.readLong() << " entered map" << std::endl; + std::cout << " name: " << msg.readString() << std::endl; + std::cout << " hair style: " << (int)msg.readByte() << std::endl; + std::cout << " hair color: " << (int)msg.readByte() << std::endl; + std::cout << " gender: " << (int)msg.readByte() << std::endl; + break; + default: + std::cout << "Unknown being entered map." << std::endl; + } + } break; + case APMSG_ENTER_WORLD_RESPONSE: { switch (msg.readByte()) { diff --git a/src/defines.h b/src/defines.h index a19df556..22de5862 100644 --- a/src/defines.h +++ b/src/defines.h @@ -147,7 +147,9 @@ enum { // Game PGMSG_PICKUP = 0x0110, GPMSG_PICKUP_RESPONSE = 0x0111, - SMSG_NEW_BEING = 0x0200, + GPMSG_BEING_ENTER = 0x0200, // B type, L being id + // player: S name, B hair style, B hair color, B gender + GPMSG_BEING_LEAVE = 0x0201, // B type, L being id PGMSG_WALK = 0x0260, // L*2 destination PGMSG_SAY = 0x02A0, // S text GPMSG_SAY = 0x02A1, // S being, S text diff --git a/src/gamehandler.cpp b/src/gamehandler.cpp index c61a95db..ebda00d6 100644 --- a/src/gamehandler.cpp +++ b/src/gamehandler.cpp @@ -84,17 +84,16 @@ GameClient::~GameClient() void GameClient::setCharacter(tmwserv::BeingPtr ch) { assert(mCharacterPtr.get() == NULL); - tmwserv::State &state = tmwserv::State::instance(); mCharacterPtr = ch; - state.addBeing(mCharacterPtr, mCharacterPtr->getMapId()); + gameState->addBeing(mCharacterPtr); + gameState->informBeing(mCharacterPtr); } void GameClient::unsetCharacter() { if (mCharacterPtr.get() == NULL) return; // remove being from world - tmwserv::State &state = tmwserv::State::instance(); - state.removeBeing(mCharacterPtr); + gameState->removeBeing(mCharacterPtr); mCharacterPtr = tmwserv::BeingPtr(NULL); } diff --git a/src/main.cpp b/src/main.cpp index 1b609f89..8a5dd5b6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -99,6 +99,9 @@ ChatChannelManager *chatChannelManager; /** Core game message handler */ GameHandler *gameHandler; +/** Global game state */ +tmwserv::State *gameState; + /** * Initializes the server. */ @@ -340,7 +343,7 @@ int main(int argc, char *argv[]) // // create state machine - State &state = State::instance(); + gameState = new State; // initialize world timer worldTimer.start(); @@ -365,7 +368,7 @@ int main(int argc, char *argv[]) chatHandler->process(); gameHandler->process(); // Update all active objects/beings - state.update(); + gameState->update(); // Send potentially urgent outgoing messages gameHandler->flush(); } @@ -374,6 +377,7 @@ int main(int argc, char *argv[]) LOG_INFO("Received: Quit signal, closing down...", 0); gameHandler->stopListen(); + delete gameState; chatHandler->stopListen(); accountHandler->stopListen(); deinitialize(); diff --git a/src/state.cpp b/src/state.cpp index e6c2089e..bcd63f5d 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -33,10 +33,10 @@ namespace tmwserv { -State::State() throw() { +State::State() { } -State::~State() throw() { +State::~State() { for (std::map<unsigned int, MapComposite>::iterator i = maps.begin(); i != maps.end(); i++) { @@ -56,88 +56,70 @@ void State::update() b->get()->update(); } } - - // notify clients about changes in the game world (only on their maps) - // NOTE: Should only send differences in the game state. - for (std::map<unsigned int, MapComposite>::iterator i = maps.begin(); - i != maps.end(); - i++) { - // - for (Beings::iterator b = i->second.beings.begin(); - b != i->second.beings.end(); - b++) { - // send info about other players - for (Beings::iterator b2 = i->second.beings.begin(); - b2 != i->second.beings.end(); - b2++) { - if (b != b2) { - MessageOut msg; - msg.writeShort(SMSG_NEW_BEING); // - msg.writeLong(OBJECT_PLAYER); // type - msg.writeString(b2->get()->getName()); // Name - msg.writeLong(b2->get()->getX());// x - msg.writeLong(b2->get()->getY());// y - - gameHandler->sendTo(*b, msg); - } - } - } - } } -void State::addBeing(BeingPtr beingPtr, const unsigned int mapId) { - if (!beingExists(beingPtr)) { - if (!mapExists(mapId)) - if (!loadMap(mapId)) - return; - maps[mapId].beings.push_back(beingPtr); +void State::addBeing(BeingPtr beingPtr) { + unsigned mapId = beingPtr->getMapId(); + if (!loadMap(mapId)) return; + Beings &beings = maps[mapId].beings; + beings.push_back(beingPtr); + MessageOut msg; + msg.writeShort(GPMSG_BEING_ENTER); + msg.writeByte(OBJECT_PLAYER); + msg.writeLong(0); // ID + msg.writeString(beingPtr->getName()); + msg.writeByte(beingPtr->getHairStyle()); + msg.writeByte(beingPtr->getHairColor()); + msg.writeByte(beingPtr->getGender()); + for (Beings::iterator b = beings.begin(), end = beings.end(); + b != end; ++b) { + gameHandler->sendTo(*b, msg); } } void State::removeBeing(BeingPtr beingPtr) { - for (std::map<unsigned int, MapComposite>::iterator i = maps.begin(); - i != maps.end(); - i++) { - for (Beings::iterator b = i->second.beings.begin(); - b != i->second.beings.end(); - b++) { - if (b->get() == beingPtr.get()) { - i->second.beings.erase(b); - return; - } - } + unsigned mapId = beingPtr->getMapId(); + std::map<unsigned, MapComposite>::iterator i = maps.find(mapId); + if (i == maps.end()) return; + Beings &beings = i->second.beings; + MessageOut msg; + msg.writeShort(GPMSG_BEING_LEAVE); + msg.writeByte(OBJECT_PLAYER); + msg.writeLong(0); // ID + Beings::iterator j = beings.end(); + for (Beings::iterator b = beings.begin(), end = beings.end(); + b != end; ++b) { + if (b->get() == beingPtr.get()) + j = b; + else + gameHandler->sendTo(*b, msg); } + if (j != beings.end()) beings.erase(j); } -bool State::mapExists(const unsigned int mapId) { - std::map<unsigned int, MapComposite>::iterator i = maps.find(mapId); - if (i == maps.end()) - return false; - return true; -} - -bool State::beingExists(BeingPtr beingPtr) { - for (std::map<unsigned int, MapComposite>::iterator i = maps.begin(); - i != maps.end(); - i++) { - for (Beings::iterator b = i->second.beings.begin(); - b != i->second.beings.end(); - b++) { - if (b->get() == beingPtr.get()) - return true; - } +void State::informBeing(BeingPtr beingPtr) { + unsigned mapId = beingPtr->getMapId(); + std::map<unsigned, MapComposite>::iterator i = maps.find(mapId); + if (i == maps.end()) return; + Beings &beings = i->second.beings; + for (Beings::iterator b = beings.begin(), end = beings.end(); + b != end; ++b) { + MessageOut msg; + msg.writeShort(GPMSG_BEING_ENTER); + msg.writeByte(OBJECT_PLAYER); + msg.writeLong(0); // ID + msg.writeString((*b)->getName()); + msg.writeByte((*b)->getHairStyle()); + msg.writeByte((*b)->getHairColor()); + msg.writeByte((*b)->getGender()); + gameHandler->sendTo(beingPtr, msg); } - return false; } bool State::loadMap(const unsigned int mapId) { + if (maps.find(mapId) != maps.end()) return true; Map *tmp = MapManager::instance().loadMap(mapId); - if (!tmp) - { - return false; - } - - maps[mapId] = MapComposite(); + if (!tmp) return false; maps[mapId].map = tmp; // will need to load extra map related resources here also @@ -145,70 +127,23 @@ bool State::loadMap(const unsigned int mapId) { return true; // We let true for testing on beings } -void State::addObject(ObjectPtr objectPtr, const unsigned int mapId) { - if (!objectExists(objectPtr)) { - if (!mapExists(mapId)) - if (!loadMap(mapId)) - return; - maps[mapId].objects.push_back(objectPtr); - } +void State::addObject(ObjectPtr objectPtr) { + unsigned mapId = objectPtr->getMapId(); + if (!loadMap(mapId)) return; + maps[mapId].objects.push_back(objectPtr); } void State::removeObject(ObjectPtr objectPtr) { - for (std::map<unsigned int, MapComposite>::iterator i = maps.begin(); - i != maps.end(); - i++) { - for (Objects::iterator b = i->second.objects.begin(); - b != i->second.objects.end(); - b++) { - if (b->get() == objectPtr.get()) { - i->second.objects.erase(b); - return; - } - } - } -} - -bool State::objectExists(const ObjectPtr objectPtr) { - for (std::map<unsigned int, MapComposite>::iterator i = maps.begin(); - i != maps.end(); - i++) { - for (Objects::iterator b = i->second.objects.begin(); - b != i->second.objects.end(); - b++) { - if (b->get() == objectPtr.get()) - return true; - } - } - return false; -} - -const unsigned int State::findPlayer(BeingPtr beingPtr) { - for (std::map<unsigned int, MapComposite>::iterator i = maps.begin(); - i != maps.end(); - i++) { - for (Beings::iterator b = i->second.beings.begin(); - b != i->second.beings.end(); - b++) { - if (b->get() == beingPtr.get()) - return i->first; - } - } - return 0; -} - -const unsigned int State::findObject(ObjectPtr objectPtr) { - for (std::map<unsigned int, MapComposite>::iterator i = maps.begin(); - i != maps.end(); - i++) { - for (Objects::iterator b = i->second.objects.begin(); - b != i->second.objects.end(); - b++) { - if (b->get() == objectPtr.get()) - return i->first; + unsigned mapId = objectPtr->getMapId(); + std::map<unsigned, MapComposite>::iterator i = maps.find(mapId); + if (i == maps.end()) return; + for (Objects::iterator b = i->second.objects.begin(), end = i->second.objects.end(); + b != end; ++b) { + if (b->get() == objectPtr.get()) { + i->second.objects.erase(b); + return; } } - return 0; } } // namespace tmwserv diff --git a/src/state.h b/src/state.h index 0c99afc5..2ebf8b1e 100644 --- a/src/state.h +++ b/src/state.h @@ -28,10 +28,6 @@ #include "being.h" -#include "utils/singleton.h" - -class ConnectionHandler; - namespace tmwserv { class Map; @@ -65,19 +61,16 @@ struct MapComposite { * State class contains all information/procedures associated with the game * world's state. */ -class State : public utils::Singleton<State> +class State { - friend class utils::Singleton<State>; - - State() throw(); - ~State() throw(); - /** * List of maps */ std::map<unsigned int, MapComposite> maps; public: + State(); + ~State(); /** * Update game state (contains core server logic) @@ -87,7 +80,7 @@ class State : public utils::Singleton<State> /** * Add being to game world at specified map */ - void addBeing(BeingPtr beingPtr, const unsigned int mapId); + void addBeing(BeingPtr beingPtr); /** * Remove being from game world @@ -95,46 +88,28 @@ class State : public utils::Singleton<State> void removeBeing(BeingPtr beingPtr); /** - * Check to see if a map exists in game world + * Send game state to given being */ - bool mapExists(const unsigned int mapId); - - /** - * Check if being exists in game world already - */ - bool beingExists(BeingPtr beingPtr); + void informBeing(BeingPtr beingPtr); /** * Load map into game world */ - bool loadMap(const unsigned int mapId); + bool loadMap(unsigned mapId); /** * Add object to the map */ - void addObject(ObjectPtr objectPtr, const unsigned int mapId); + void addObject(ObjectPtr objectPtr); /** * Remove an object from the map */ void removeObject(ObjectPtr objectPtr); - - /** - * Find out whether an object exists in the game world or not - */ - bool objectExists(const ObjectPtr objectPtr); - - /** - * Find map player in world is on - */ - const unsigned int findPlayer(BeingPtr being); - - /** - * Find map object in world is on - */ - const unsigned int findObject(ObjectPtr objectPtr); }; } // namespace tmwserv +extern tmwserv::State * gameState; + #endif |