From ce849af300a70425b574b724c62dc9f5fce6016b Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Fri, 4 Aug 2006 16:58:20 +0000 Subject: Generalized the properties functionality as was done in the client before. Removed some more remaining usages of tmwserv namespace. Added some documentation and de-complicized the code a bit. Removed checks for SDL and SDL_net. --- src/Makefile.am | 1 + src/accounthandler.cpp | 5 +++- src/chatchannel.h | 6 ++-- src/gamehandler.cpp | 75 ++++++++++++++++++++++++++++++++++++------------ src/items.cpp | 2 +- src/items.h | 5 ---- src/main.cpp | 33 +++++++++++----------- src/map.cpp | 25 ---------------- src/map.h | 25 ++-------------- src/properties.h | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/utils/logger.h | 34 ++++++---------------- src/utils/timer.cpp | 3 -- src/utils/timer.h | 3 -- 13 files changed, 171 insertions(+), 123 deletions(-) create mode 100644 src/properties.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 7b04c953..5aedb6b2 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -32,6 +32,7 @@ tmwserv_SOURCES = main.cpp \ gameclient.cpp \ gamehandler.h \ gamehandler.cpp \ + properties.h \ state.h \ state.cpp \ debug.h \ diff --git a/src/accounthandler.cpp b/src/accounthandler.cpp index 992f7888..271186c0 100644 --- a/src/accounthandler.cpp +++ b/src/accounthandler.cpp @@ -426,7 +426,10 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) break; // no character selected } std::string magic_token(32, ' '); - for(int i = 0; i < 32; ++i) magic_token[i] = 1 + (int) (127 * (rand() / (RAND_MAX + 1.0))); + for (int i = 0; i < 32; ++i) { + magic_token[i] = + 1 + (int) (127 * (rand() / (RAND_MAX + 1.0))); + } result.writeByte(ERRMSG_OK); result.writeString("localhost"); result.writeShort(9603); diff --git a/src/chatchannel.h b/src/chatchannel.h index f46b1dae..5571701e 100644 --- a/src/chatchannel.h +++ b/src/chatchannel.h @@ -97,17 +97,17 @@ class ChatChannel { private: /** - * The Channel's name + * The Channel's name. */ std::string mChannelName; /** - * The Channel's name + * The Channel's announcement. */ std::string mChannelAnnouncement; /** - * The Channel's name + * The Channel's password. */ std::string mChannelPassword; diff --git a/src/gamehandler.cpp b/src/gamehandler.cpp index ef0ed924..b3dfb324 100644 --- a/src/gamehandler.cpp +++ b/src/gamehandler.cpp @@ -41,11 +41,29 @@ struct GamePendingLogin }; typedef std::map< std::string, GamePendingLogin > GamePendingLogins; +typedef std::map< std::string, GameClient * > GamePendingClients; + +/** + * The pending logins represent clients who were given a magic token by the + * account server but who have not yet logged in to the game server. + */ static GamePendingLogins pendingLogins; -typedef std::map< std::string, GameClient * > GamePendingClients; +/** + * The pending clients represent clients who tried to login to the game server, + * but for which no magic token is available yet. This can happen when the + * communication between the account server and client went faster than the + * communication between the account server and the game server. + */ static GamePendingClients pendingClients; +/** + * Notification that a particular token has been given to allow a certain + * player to enter the game. + * + * This method is currently called directly from the account server. Later on + * it should be a message sent from the account server to the game server. + */ void registerGameClient(std::string const &token, PlayerPtr ch) { GamePendingClients::iterator i = pendingClients.find(token); @@ -70,11 +88,17 @@ void registerGameClient(std::string const &token, PlayerPtr ch) void GameHandler::removeOutdatedPending() { - GamePendingLogins::iterator i = pendingLogins.begin(), next; + GamePendingLogins::iterator i = pendingLogins.begin(); + GamePendingLogins::iterator next; + while (i != pendingLogins.end()) { - next = i; ++next; - if (--i->second.timeout <= 0) pendingLogins.erase(i); + next = i; + ++next; + if (--i->second.timeout <= 0) + { + pendingLogins.erase(i); + } i = next; } } @@ -86,8 +110,8 @@ NetComputer *GameHandler::computerConnected(ENetPeer *peer) void GameHandler::computerDisconnected(NetComputer *computer) { - for (GamePendingClients::iterator i = pendingClients.begin(), i_end = pendingClients.end(); - i != i_end; ++i) + GamePendingClients::iterator i; + for (i = pendingClients.begin(); i != pendingClients.end(); ++i) { if (i->second == computer) { @@ -115,8 +139,9 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) GamePendingLogins::iterator i = pendingLogins.find(magic_token); if (i == pendingLogins.end()) { - for (GamePendingClients::iterator i = pendingClients.begin(), i_end = pendingClients.end(); - i != i_end; ++i) { + GamePendingClients::iterator i; + for (i = pendingClients.begin(); i != pendingClients.end(); ++i) + { if (i->second == &computer) return; } pendingClients.insert(std::make_pair(magic_token, &computer)); @@ -202,30 +227,44 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) void GameHandler::sayAround(GameClient &computer, std::string const &text) { PlayerPtr beingPtr = computer.getCharacter(); - MessageOut msg; - msg.writeShort(GPMSG_SAY); + + MessageOut msg(GPMSG_SAY); msg.writeString(beingPtr->getName()); msg.writeString(text); + unsigned speakerMapId = beingPtr->getMapId(); std::pair speakerXY = beingPtr->getXY(); + for (NetComputers::iterator i = clients.begin(), i_end = clients.end(); i != i_end; ++i) { // See if the other being is near enough, then send the message - Player const *listener = static_cast< GameClient * >(*i)->getCharacter().get(); - if (!listener || listener->getMapId() != speakerMapId) continue; + Player const *listener = + static_cast(*i)->getCharacter().get(); + + if (!listener || listener->getMapId() != speakerMapId) { + continue; + } + std::pair listenerXY = listener->getXY(); - if (abs(listenerXY.first - speakerXY.first ) > (int)AROUND_AREA_IN_TILES) continue; - if (abs(listenerXY.second - speakerXY.second) > (int)AROUND_AREA_IN_TILES) continue; - (*i)->send(msg.getPacket()); + + if (abs(listenerXY.first - speakerXY.first) <= + (int)AROUND_AREA_IN_TILES && + abs(listenerXY.second - speakerXY.second) <= + (int)AROUND_AREA_IN_TILES) + { + (*i)->send(msg.getPacket()); + } } } void GameHandler::sendTo(PlayerPtr beingPtr, MessageOut &msg) { - for (NetComputers::iterator i = clients.begin(), i_end = clients.end(); - i != i_end; ++i) { - if (static_cast< GameClient * >(*i)->getCharacter().get() == beingPtr.get()) { + for (NetComputers::iterator i = clients.begin(); i != clients.end(); ++i) + { + PlayerPtr clientChar = static_cast(*i)->getCharacter(); + if (clientChar.get() == beingPtr.get()) + { (*i)->send(msg.getPacket()); break; } diff --git a/src/items.cpp b/src/items.cpp index e0606920..659e24db 100644 --- a/src/items.cpp +++ b/src/items.cpp @@ -23,7 +23,7 @@ #include "items.h" -void tmwserv::Item::use() +void Item::use() { // } diff --git a/src/items.h b/src/items.h index 0d8aefcc..1b973566 100644 --- a/src/items.h +++ b/src/items.h @@ -26,9 +26,6 @@ #include "object.h" -namespace tmwserv -{ - /** * Class for all types of in-game items. */ @@ -59,6 +56,4 @@ class Item : public Object unsigned int getType() { return type; } }; -} // namespace tmwserv - #endif diff --git a/src/main.cpp b/src/main.cpp index 28afc187..24bf79af 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -78,7 +78,7 @@ std::string scriptLanugage = "none"; #define DEFAULT_SERVER_PORT 9601 #endif -tmwserv::utils::Timer worldTimer(100, false); /**< Timer for world tics set to 100 ms */ +utils::Timer worldTimer(100, false); /**< Timer for world tics set to 100 ms */ int worldTime = 0; /**< Current world time in 100ms ticks */ bool running = true; /**< Determines if server keeps running */ @@ -157,7 +157,7 @@ void initialize() // Initialize the global handlers // FIXME: Make the global handlers global vars or part of a bigger - // singleton or a local vatiable in the event-loop + // singleton or a local variable in the event-loop chatChannelManager = new ChatChannelManager(); chatHandler = new ChatHandler(); @@ -167,28 +167,28 @@ void initialize() // Reset to default segmentation fault handling for debugging purposes signal(SIGSEGV, SIG_DFL); - // set enet to quit on exit. + // Set enet to quit on exit. atexit(enet_deinitialize); - // initialize enet. + // Initialize enet. if (enet_initialize() != 0) { LOG_FATAL("An error occurred while initializing ENet", 0); exit(2); } - // initialize scripting subsystem. + // Initialize scripting subsystem. #ifdef RUBY_SUPPORT LOG_INFO("Script Language: " << scriptLanguage, 0); - // initialize ruby + // Initialize ruby ruby_init(); ruby_init_loadpath(); ruby_script("tmw"); - // initialize bindings + // Initialize bindings Init_Tmw(); - // run test script + // Run test script rb_load_file("scripts/init.rb"); rubyStatus = ruby_exec(); #else @@ -205,7 +205,7 @@ void initialize() LOG_WARN("No Database Backend Support.", 0); #endif - // initialize configuration defaults + // Initialize configuration defaults config.setValue("dbuser", ""); config.setValue("dbpass", ""); config.setValue("dbhost", ""); @@ -236,7 +236,7 @@ void deinitialize() ruby_cleanup(rubyStatus); #endif - // destroy message handlers + // Destroy message handlers delete accountHandler; delete chatHandler; delete gameHandler; @@ -331,20 +331,17 @@ int main(int argc, char *argv[]) return 3; } - using namespace tmwserv; - - // create storage wrapper + // Create storage wrapper Storage& store = Storage::instance("tmw"); store.setUser(config.getValue("dbuser", "")); store.setPassword(config.getValue("dbpass", "")); store.close(); store.open(); - // - // create state machine + // Create state machine gameState = new State; - // initialize world timer + // Initialize world timer worldTimer.start(); while (running) { @@ -354,7 +351,9 @@ int main(int argc, char *argv[]) if (elapsedWorldTicks > 1) { - LOG_WARN(elapsedWorldTicks -1 <<" World Tick(s) skipped because of insufficient time. please buy a faster machine ;-)", 0); + LOG_WARN(elapsedWorldTicks -1 << " World Tick(s) skipped " + "because of insufficient time. please buy a faster " + "machine ;-)", 0); }; // Print world time at 10 second intervals to show we're alive diff --git a/src/map.cpp b/src/map.cpp index 436a9c42..89c34ee6 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -147,31 +147,6 @@ Map::getTileHeight() return tileHeight; } -std::string -Map::getProperty(const std::string &name) -{ - std::map::iterator i = properties.find(name); - - if (i != properties.end()) - { - return (*i).second; - } - - return ""; -} - -bool -Map::hasProperty(const std::string &name) -{ - return (properties.find(name) != properties.end()); -} - -void -Map::setProperty(const std::string &name, const std::string &value) -{ - properties[name] = value; -} - std::list Map::findPath(int startX, int startY, int destX, int destY) { diff --git a/src/map.h b/src/map.h index 0f31d7b4..9cc07044 100644 --- a/src/map.h +++ b/src/map.h @@ -28,6 +28,8 @@ #include #include +#include "properties.h" + struct PATH_NODE; /** @@ -76,7 +78,7 @@ class Location /** * A tile map. */ -class Map +class Map : public Properties { public: /** @@ -155,27 +157,6 @@ class Map findPath(int startX, int startY, int destX, int destY); - /** - * Get a map property. - * - * @return the value of the given property or an empty string when it - * doesn't exist. - */ - std::string - getProperty(const std::string &name); - - /** - * Returns whether a certain property is available. - */ - bool - hasProperty(const std::string &name); - - /** - * Set a map property. - */ - void - setProperty(const std::string &name, const std::string &value); - private: int width, height; int tileWidth, tileHeight; diff --git a/src/properties.h b/src/properties.h new file mode 100644 index 00000000..5f8d751d --- /dev/null +++ b/src/properties.h @@ -0,0 +1,77 @@ +/* + * The Mana World + * 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 _TMW_PROPERTIES_H_ +#define _TMW_PROPERTIES_H_ + +#include +#include + +/** + * A class holding a set of properties. + */ +class Properties +{ + public: + virtual + ~Properties() {} + + /** + * Get a map property. + * + * @return the value of the given property or an empty string when it + * doesn't exist. + */ + const std::string& + getProperty(const std::string &name) + { + const static std::string undefined = ""; + PropertyMap::const_iterator i = mProperties.find(name); + + return (i != mProperties.end()) ? i->second : undefined; + } + + /** + * Returns whether a certain property is available. + */ + bool + hasProperty(const std::string &name) + { + return mProperties.find(name) != mProperties.end(); + } + + /** + * Set a map property. + */ + void + setProperty(const std::string &name, const std::string &value) + { + mProperties[name] = value; + } + + private: + typedef std::map PropertyMap; + PropertyMap mProperties; +}; + +#endif diff --git a/src/utils/logger.h b/src/utils/logger.h index e37f33af..6ce97a71 100644 --- a/src/utils/logger.h +++ b/src/utils/logger.h @@ -33,7 +33,6 @@ namespace utils { - /** * A very simple logger that writes messages to a log file. * If the log file is not set, the messages are routed to the standard output @@ -54,7 +53,7 @@ namespace utils * * int main(void) * { - * using namespace tmwserv::utils; + * using namespace utils; * * Logger& logger = Logger::instance(); * logger.setLogFile("/path/to/logfile"); @@ -88,7 +87,6 @@ class Logger: public Singleton // friend so that Singleton can call the constructor. friend class Singleton; - public: /** * Set the log file. @@ -103,7 +101,6 @@ class Logger: public Singleton void setLogFile(const std::string& logFile); - /** * Add/remove the timestamp. * @@ -114,7 +111,6 @@ class Logger: public Singleton setTimestamp(bool flag = true) throw(); - /** * Set tee mode. * @@ -134,7 +130,7 @@ class Logger: public Singleton * 2 = + Packets names and messages sent. */ void - setVerbosity(unsigned short verbosity = 0) { mVerbosity = verbosity; }; + setVerbosity(unsigned short verbosity = 0) { mVerbosity = verbosity; } /** * Set tee mode. @@ -143,7 +139,7 @@ class Logger: public Singleton * output and the log file (if set) (default = true). */ unsigned short - getVerbosity() { return mVerbosity; }; + getVerbosity() { return mVerbosity; } /** * Log a generic message. @@ -156,8 +152,7 @@ class Logger: public Singleton * @exception std::ios::failure. */ void - log(const std::string& msg, unsigned short atVerbosity = 0); - + log(const std::string &msg, unsigned short atVerbosity = 0); /** * Log a debug message. @@ -170,8 +165,7 @@ class Logger: public Singleton * @exception std::ios::failure. */ void - debug(const std::string& msg, unsigned short atVerbosity = 0); - + debug(const std::string &msg, unsigned short atVerbosity = 0); /** * Log an info message. @@ -184,8 +178,7 @@ class Logger: public Singleton * @exception std::ios::failure. */ void - info(const std::string& msg, unsigned short atVerbosity = 0); - + info(const std::string &msg, unsigned short atVerbosity = 0); /** * Log a warn message. @@ -198,8 +191,7 @@ class Logger: public Singleton * @exception std::ios::failure. */ void - warn(const std::string& msg, unsigned short atVerbosity = 0); - + warn(const std::string &msg, unsigned short atVerbosity = 0); /** * Log an error message. @@ -212,8 +204,7 @@ class Logger: public Singleton * @exception std::ios::failure. */ void - error(const std::string& msg, unsigned short atVerbosity = 0); - + error(const std::string &msg, unsigned short atVerbosity = 0); /** * Log a fatal error message. @@ -226,8 +217,7 @@ class Logger: public Singleton * @exception std::ios::failure. */ void - fatal(const std::string& msg, unsigned short atVerbosity = 0); - + fatal(const std::string &msg, unsigned short atVerbosity = 0); private: /** @@ -236,27 +226,23 @@ class Logger: public Singleton Logger(void) throw(); - /** * Destructor. */ ~Logger(void) throw(); - /** * Copy constructor. */ Logger(const Logger& rhs); - /** * Assignment operator. */ Logger& operator=(const Logger& rhs); - /** * Log a generic message. * @@ -271,7 +257,6 @@ class Logger: public Singleton const std::string& msg, const std::string& prefix = ""); - /** * Get the current time. * @@ -281,7 +266,6 @@ class Logger: public Singleton getCurrentTime(void); - private: std::ofstream mLogFile; /**< the log file */ bool mHasTimestamp; /**< the timestamp flag */ bool mTeeMode; /**< the tee mode flag */ diff --git a/src/utils/timer.cpp b/src/utils/timer.cpp index a476ebec..f055e664 100644 --- a/src/utils/timer.cpp +++ b/src/utils/timer.cpp @@ -22,8 +22,6 @@ #include #include "timer.h" -namespace tmwserv -{ namespace utils { @@ -87,4 +85,3 @@ uint64_t Timer::getTimeInMillisec() }; } // ::utils -} // ::tmwserv diff --git a/src/utils/timer.h b/src/utils/timer.h index 603d24cd..69ca634e 100644 --- a/src/utils/timer.h +++ b/src/utils/timer.h @@ -35,8 +35,6 @@ #include "wingettimeofday.h" #endif -namespace tmwserv -{ namespace utils { @@ -99,6 +97,5 @@ class Timer }; } // ::utils -} // ::tmwserv #endif -- cgit v1.2.3-70-g09d2