| 1 | /* | 
|---|
| 2 |    orxonox - the future of 3D-vertical-scrollers | 
|---|
| 3 |  | 
|---|
| 4 |    Copyright (C) 2004 orx | 
|---|
| 5 |  | 
|---|
| 6 |    This program is free software; you can redistribute it and/or modify | 
|---|
| 7 |    it under the terms of the GNU General Public License as published by | 
|---|
| 8 |    the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 9 |    any later version. | 
|---|
| 10 |  | 
|---|
| 11 | ### File Specific: | 
|---|
| 12 |    main-programmer: Christoph Renner rennerc@ee.ethz.ch | 
|---|
| 13 |    co-programmer:   Patrick Boenzli  boenzlip@orxonox.ethz.ch | 
|---|
| 14 |  | 
|---|
| 15 |      June 2006: finishing work on the network stream for pps presentation (rennerc@ee.ethz.ch) | 
|---|
| 16 |      July 2006: some code rearangement and integration of the proxy server mechanism (boenzlip@ee.ethz.ch) | 
|---|
| 17 | */ | 
|---|
| 18 |  | 
|---|
| 19 |  | 
|---|
| 20 |  | 
|---|
| 21 | #define DEBUG_MODULE_NETWORK | 
|---|
| 22 |  | 
|---|
| 23 | #include "util/loading/factory.h" | 
|---|
| 24 | #include "state.h" | 
|---|
| 25 | #include "debug.h" | 
|---|
| 26 |  | 
|---|
| 27 | #include "network_stream.h" | 
|---|
| 28 | #include "shared_network_data.h" | 
|---|
| 29 | #include "converter.h" | 
|---|
| 30 | #include "message_manager.h" | 
|---|
| 31 |  | 
|---|
| 32 | #include "playable.h" | 
|---|
| 33 | #include "player.h" | 
|---|
| 34 |  | 
|---|
| 35 | #include "game_world.h" | 
|---|
| 36 |  | 
|---|
| 37 | #include "game_rules.h" | 
|---|
| 38 | #include "network_game_rules.h" | 
|---|
| 39 |  | 
|---|
| 40 | #include "network_game_manager.h" | 
|---|
| 41 |  | 
|---|
| 42 | #include "multiplayer_team_deathmatch.h" | 
|---|
| 43 |  | 
|---|
| 44 | #include "parser/preferences/preferences.h" | 
|---|
| 45 |  | 
|---|
| 46 |  | 
|---|
| 47 |  | 
|---|
| 48 | ObjectListDefinition(NetworkGameManager); | 
|---|
| 49 | NetworkGameManager* NetworkGameManager::singletonRef = NULL; | 
|---|
| 50 |  | 
|---|
| 51 | /*! | 
|---|
| 52 |  * Standard constructor | 
|---|
| 53 |  */ | 
|---|
| 54 | NetworkGameManager::NetworkGameManager() | 
|---|
| 55 |   : Synchronizeable() | 
|---|
| 56 | { | 
|---|
| 57 |   PRINTF(0)("START\n"); | 
|---|
| 58 |  | 
|---|
| 59 |   /* set the class id for the base object */ | 
|---|
| 60 |   this->registerObject(this, NetworkGameManager::_objectList); | 
|---|
| 61 |  | 
|---|
| 62 |   this->setSynchronized(true); | 
|---|
| 63 |  | 
|---|
| 64 |   MessageManager::getInstance()->registerMessageHandler( MSGID_DELETESYNCHRONIZEABLE, delSynchronizeableHandler, NULL ); | 
|---|
| 65 |   MessageManager::getInstance()->registerMessageHandler( MSGID_PREFEREDTEAM, preferedTeamHandler, NULL ); | 
|---|
| 66 |   MessageManager::getInstance()->registerMessageHandler( MSGID_CHATMESSAGE, chatMessageHandler, NULL ); | 
|---|
| 67 |  | 
|---|
| 68 |   this->gameState = 0; | 
|---|
| 69 |   registerVar( new SynchronizeableInt( &gameState, &gameState, "gameState", PERMISSION_MASTER_SERVER ) ); | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | /*! | 
|---|
| 73 |  * Standard destructor | 
|---|
| 74 |  */ | 
|---|
| 75 | NetworkGameManager::~NetworkGameManager() | 
|---|
| 76 | { | 
|---|
| 77 |   delete MessageManager::getInstance(); | 
|---|
| 78 |  | 
|---|
| 79 |   PlayerStats::deleteAllPlayerStats(); | 
|---|
| 80 |  | 
|---|
| 81 |   NetworkGameManager::singletonRef = NULL; | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 |  | 
|---|
| 85 | /** | 
|---|
| 86 |  * insert new player into game | 
|---|
| 87 |  * @param userId | 
|---|
| 88 |  * @return | 
|---|
| 89 |  */ | 
|---|
| 90 | bool NetworkGameManager::signalNewPlayer( int userId ) | 
|---|
| 91 | { | 
|---|
| 92 |   assert( SharedNetworkData::getInstance()->isMasterServer()); | 
|---|
| 93 |   assert( State::getGameRules() ); | 
|---|
| 94 |   assert( State::getGameRules()->isA( NetworkGameRules::staticClassID()) ); | 
|---|
| 95 |  | 
|---|
| 96 |   NetworkGameRules & rules = *(dynamic_cast<NetworkGameRules*>(State::getGameRules())); | 
|---|
| 97 |  | 
|---|
| 98 |   int          team = rules.getTeamForNewUser(); | 
|---|
| 99 |   ClassID   playableClassId = rules.getPlayableClassId( userId, team ); | 
|---|
| 100 |   std::string  playableModel = rules.getPlayableModelFileName( userId, team, playableClassId ); | 
|---|
| 101 |   std::string  playableTexture = rules.getPlayableModelFileName( userId, team, playableClassId ); | 
|---|
| 102 |   float        playableScale = rules.getPlayableScale( userId, team, playableClassId ); | 
|---|
| 103 |  | 
|---|
| 104 |   BaseObject * bo = Factory::fabricate( playableClassId ); | 
|---|
| 105 |  | 
|---|
| 106 |   assert( bo != NULL ); | 
|---|
| 107 |   assert( bo->isA( Playable::staticClassID()) ); | 
|---|
| 108 |  | 
|---|
| 109 |   Playable & playable = *(dynamic_cast<Playable*>(bo)); | 
|---|
| 110 |  | 
|---|
| 111 |   playable.loadMD2Texture( playableTexture ); | 
|---|
| 112 |   playable.setTeam(team); | 
|---|
| 113 |   playable.loadModel( playableModel, playableScale ); | 
|---|
| 114 |   playable.setOwner( userId ); | 
|---|
| 115 |   playable.setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); | 
|---|
| 116 |   playable.setSynchronized( true ); | 
|---|
| 117 |  | 
|---|
| 118 |   PlayerStats * stats = rules.getNewPlayerStats( userId ); | 
|---|
| 119 |  | 
|---|
| 120 |   stats->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); | 
|---|
| 121 |   stats->setSynchronized( true ); | 
|---|
| 122 |   stats->setOwner( SharedNetworkData::getInstance()->getHostID() ); | 
|---|
| 123 |  | 
|---|
| 124 |   stats->setTeamId( team ); | 
|---|
| 125 |   stats->setPlayableClassId( playableClassId ); | 
|---|
| 126 |   stats->setPlayableUniqueId( playable.getUniqueID() ); | 
|---|
| 127 |   stats->setModelFileName( playableModel ); | 
|---|
| 128 |  | 
|---|
| 129 |   if ( userId == 0 ) | 
|---|
| 130 |     stats->setNickName( Preferences::getInstance()->getString( "multiplayer", "nickname", "Server" ) ); | 
|---|
| 131 |  | 
|---|
| 132 |   if ( rules.isA( MultiplayerTeamDeathmatch::staticClassID()) ) | 
|---|
| 133 |     dynamic_cast<MultiplayerTeamDeathmatch*>(&rules)->respawnPlayable( &playable, team, 0.0f ); | 
|---|
| 134 |  | 
|---|
| 135 |   return true; | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 |  | 
|---|
| 139 | /** | 
|---|
| 140 |  * remove player from game | 
|---|
| 141 |  * @param userID | 
|---|
| 142 |  * @return | 
|---|
| 143 |  */ | 
|---|
| 144 | bool NetworkGameManager::signalLeftPlayer(int userID) | 
|---|
| 145 | { | 
|---|
| 146 |   if ( PlayerStats::getStats( userID ) ) | 
|---|
| 147 |   { | 
|---|
| 148 |     if ( PlayerStats::getStats( userID )->getPlayable() ) | 
|---|
| 149 |       delete PlayerStats::getStats( userID )->getPlayable(); | 
|---|
| 150 |     delete PlayerStats::getStats( userID ); | 
|---|
| 151 |   } | 
|---|
| 152 |  | 
|---|
| 153 |   return true; | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 |  | 
|---|
| 157 |  | 
|---|
| 158 | /** | 
|---|
| 159 |  * handler for remove synchronizeable messages | 
|---|
| 160 |  * @param messageType | 
|---|
| 161 |  * @param data | 
|---|
| 162 |  * @param dataLength | 
|---|
| 163 |  * @param someData | 
|---|
| 164 |  * @param userId | 
|---|
| 165 |  * @return true on successfull handling else handler will be called again | 
|---|
| 166 |  */ | 
|---|
| 167 | bool NetworkGameManager::delSynchronizeableHandler( MessageType messageType, byte * data, int dataLength, void * someData, int senderId, int destinationId  ) | 
|---|
| 168 | { | 
|---|
| 169 |  | 
|---|
| 170 |   PRINTF(0)(" del synchronizeable\n"); | 
|---|
| 171 |  | 
|---|
| 172 |   if ( SharedNetworkData::getInstance()->isMasterServer() || | 
|---|
| 173 |        SharedNetworkData::getInstance()->isProxyServerActive() && SharedNetworkData::getInstance()->isUserClient(senderId)) | 
|---|
| 174 |   { | 
|---|
| 175 |     PRINTF(0)("Recieved DeleteSynchronizeable message from client %d!\n", senderId); | 
|---|
| 176 |     return true; | 
|---|
| 177 |   } | 
|---|
| 178 |  | 
|---|
| 179 |   int uniqueId = 0; | 
|---|
| 180 |   int len = Converter::byteArrayToInt( data, &uniqueId ); | 
|---|
| 181 |  | 
|---|
| 182 |   if ( len != dataLength ) | 
|---|
| 183 |   { | 
|---|
| 184 |     PRINTF(2)("Recieved DeleteSynchronizeable message with incorrect size (%d) from client %d!\n", dataLength, senderId); | 
|---|
| 185 |     return true; | 
|---|
| 186 |   } | 
|---|
| 187 |  | 
|---|
| 188 |   for (ObjectList<Synchronizeable>::const_iterator it = Synchronizeable::objectList().begin(); | 
|---|
| 189 |        it != Synchronizeable::objectList().end(); | 
|---|
| 190 |       ++it) | 
|---|
| 191 |   { | 
|---|
| 192 |     if ( (*it)->getUniqueID() == uniqueId ) | 
|---|
| 193 |     { | 
|---|
| 194 |       if ( (*it)->isA(Playable::staticClassID()) ) | 
|---|
| 195 |       { | 
|---|
| 196 |         getInstance()->playablesToDelete.push_back( dynamic_cast<Playable*>(*it) ); | 
|---|
| 197 |         return true; | 
|---|
| 198 |       } | 
|---|
| 199 |  | 
|---|
| 200 |       delete (*it); | 
|---|
| 201 |       return true; | 
|---|
| 202 |     } | 
|---|
| 203 |   } | 
|---|
| 204 |  | 
|---|
| 205 |   return true; | 
|---|
| 206 | } | 
|---|
| 207 |  | 
|---|
| 208 | /** | 
|---|
| 209 |  * removes synchronizeable (also on clients) | 
|---|
| 210 |  * @param uniqueId uniqueid to delete | 
|---|
| 211 |  */ | 
|---|
| 212 | void NetworkGameManager::removeSynchronizeable( int uniqueId ) | 
|---|
| 213 | { | 
|---|
| 214 |   byte buf[INTSIZE]; | 
|---|
| 215 |  | 
|---|
| 216 |   assert( Converter::intToByteArray( uniqueId, buf, INTSIZE ) == INTSIZE ); | 
|---|
| 217 |  | 
|---|
| 218 |   MessageManager::getInstance()->sendMessage( MSGID_DELETESYNCHRONIZEABLE, buf, INTSIZE, RT_ALL_BUT_ME, NET_UNASSIGNED, MP_HIGHBANDWIDTH ); | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 221 |  | 
|---|
| 222 |  | 
|---|
| 223 | /** | 
|---|
| 224 |  * handler for MSGID_PREFEREDTEAM message | 
|---|
| 225 |  * @param messageType | 
|---|
| 226 |  * @param data | 
|---|
| 227 |  * @param dataLength | 
|---|
| 228 |  * @param someData | 
|---|
| 229 |  * @param userId | 
|---|
| 230 |  * @return | 
|---|
| 231 |  */ | 
|---|
| 232 | bool NetworkGameManager::preferedTeamHandler( MessageType messageType, byte * data, int dataLength, void * someData, int senderId, int destinationId  ) | 
|---|
| 233 | { | 
|---|
| 234 |   assert( SharedNetworkData::getInstance()->isMasterServer() ); | 
|---|
| 235 |  | 
|---|
| 236 |   int teamId = 0; | 
|---|
| 237 |   int len = Converter::byteArrayToInt( data, &teamId ); | 
|---|
| 238 |  | 
|---|
| 239 |   if ( len != dataLength ) | 
|---|
| 240 |   { | 
|---|
| 241 |     PRINTF(2)("Recieved DeleteSynchronizeable message with incorrect size (%d) from client %d!\n", dataLength, senderId); | 
|---|
| 242 |     return true; | 
|---|
| 243 |   } | 
|---|
| 244 |  | 
|---|
| 245 |   PRINTF(0)("Client %i wishes to play in team %i\n", senderId, teamId); | 
|---|
| 246 |   NetworkGameManager::getInstance()->setPreferedTeam( senderId, teamId ); | 
|---|
| 247 |  | 
|---|
| 248 |   return true; | 
|---|
| 249 | } | 
|---|
| 250 |  | 
|---|
| 251 |  | 
|---|
| 252 | /** | 
|---|
| 253 |  * this actualy sets the new prefered team id | 
|---|
| 254 |  * @param userId: the user that changes team | 
|---|
| 255 |  * @param teamId: the new team id for the user | 
|---|
| 256 |  */ | 
|---|
| 257 | void NetworkGameManager::setPreferedTeam( int userId, int teamId ) | 
|---|
| 258 | { | 
|---|
| 259 |   if ( !PlayerStats::getStats( userId ) ) | 
|---|
| 260 |     return; | 
|---|
| 261 |  | 
|---|
| 262 |   PlayerStats & stats = *(PlayerStats::getStats( userId )); | 
|---|
| 263 |  | 
|---|
| 264 |   stats.setPreferedTeamId( teamId ); | 
|---|
| 265 | } | 
|---|
| 266 |  | 
|---|
| 267 |  | 
|---|
| 268 | /** | 
|---|
| 269 |  * set prefered team for this host | 
|---|
| 270 |  * @param teamId | 
|---|
| 271 |  */ | 
|---|
| 272 | void NetworkGameManager::prefereTeam( int teamId ) | 
|---|
| 273 | { | 
|---|
| 274 |   if ( SharedNetworkData::getInstance()->isMasterServer() ) | 
|---|
| 275 |     this->setPreferedTeam( SharedNetworkData::getInstance()->getHostID(), teamId ); | 
|---|
| 276 |   else | 
|---|
| 277 |   { | 
|---|
| 278 |     byte buf[INTSIZE]; | 
|---|
| 279 |  | 
|---|
| 280 |     assert( Converter::intToByteArray( teamId, buf, INTSIZE) == INTSIZE ); | 
|---|
| 281 |  | 
|---|
| 282 |     // send this message to the master server | 
|---|
| 283 |     MessageManager::getInstance()->sendMessage( MSGID_PREFEREDTEAM, buf, INTSIZE, RT_USER, NET_ID_MASTER_SERVER, MP_HIGHBANDWIDTH ); | 
|---|
| 284 |   } | 
|---|
| 285 | } | 
|---|
| 286 |  | 
|---|
| 287 | /** | 
|---|
| 288 |  * this function will be called periodically by networkManager | 
|---|
| 289 |  * @param ds time elapsed since last call of tick | 
|---|
| 290 |  */ | 
|---|
| 291 | void NetworkGameManager::tick( float ds ) | 
|---|
| 292 | { | 
|---|
| 293 |   //delete playables if they are not assigned to local player anymore | 
|---|
| 294 |   for ( std::list<Playable*>::iterator it = playablesToDelete.begin(); it != playablesToDelete.end();  ) | 
|---|
| 295 |   { | 
|---|
| 296 |     if ( State::getPlayer()->getPlayable() != *it ) | 
|---|
| 297 |     { | 
|---|
| 298 |       if ( std::find( Playable::objectList().begin(), Playable::objectList().end(), *it ) != Playable::objectList().end() ) | 
|---|
| 299 |       { | 
|---|
| 300 |         PRINTF(0)("Delete unused playable: %s owner: %d\n", (*it)->getClassCName(), (*it)->getOwner() ); | 
|---|
| 301 |         std::list<Playable*>::iterator delit = it; | 
|---|
| 302 |         it++; | 
|---|
| 303 |         delete *delit; | 
|---|
| 304 |         playablesToDelete.erase( delit ); | 
|---|
| 305 |         continue; | 
|---|
| 306 |       } | 
|---|
| 307 |     } | 
|---|
| 308 |     it++; | 
|---|
| 309 |   } | 
|---|
| 310 | } | 
|---|
| 311 |  | 
|---|
| 312 |  | 
|---|
| 313 |  | 
|---|
| 314 | bool NetworkGameManager::chatMessageHandler( MessageType messageType, byte * data, int dataLength, void * someData, int senderId, int destinationId  ) | 
|---|
| 315 | { | 
|---|
| 316 |   PRINTF(0)("NetworkGameManager::chatMessageHandler %d %d\n", senderId, SharedNetworkData::getInstance()->getHostID() ); | 
|---|
| 317 |   if ( (SharedNetworkData::getInstance()->isMasterServer() /*|| SharedNetworkData::getInstance()->isProxyServerActive()*/) && senderId !=  SharedNetworkData::getInstance()->getHostID() ) | 
|---|
| 318 |   { | 
|---|
| 319 |     MessageManager::getInstance()->sendMessage( messageType, data, dataLength, RT_ALL_BUT_ME, NET_UNASSIGNED, MP_HIGHBANDWIDTH ); | 
|---|
| 320 |   } | 
|---|
| 321 |  | 
|---|
| 322 |   assert( State::getGameRules() ); | 
|---|
| 323 |   assert( State::getGameRules()->isA( NetworkGameRules::staticClassID()) ); | 
|---|
| 324 |  | 
|---|
| 325 |   NetworkGameRules & rules = *(dynamic_cast<NetworkGameRules*>(State::getGameRules())); | 
|---|
| 326 |  | 
|---|
| 327 |   if ( dataLength < 3*INTSIZE ) | 
|---|
| 328 |   { | 
|---|
| 329 |     PRINTF(2)("got too small chatmessage from client %d\n", senderId); | 
|---|
| 330 |  | 
|---|
| 331 |     return true; | 
|---|
| 332 |   } | 
|---|
| 333 |  | 
|---|
| 334 |   int chatType = 0; | 
|---|
| 335 |   Converter::byteArrayToInt( data, &chatType); | 
|---|
| 336 |   int senderUserId = 0; | 
|---|
| 337 |   Converter::byteArrayToInt( data+INTSIZE, &senderUserId ); | 
|---|
| 338 |   std::string message; | 
|---|
| 339 |   Converter::byteArrayToString( data+2*INTSIZE, message, dataLength-2*INTSIZE ); | 
|---|
| 340 |  | 
|---|
| 341 |   rules.handleChatMessage( senderUserId, message, chatType); | 
|---|
| 342 |  | 
|---|
| 343 |   return true; | 
|---|
| 344 | } | 
|---|
| 345 |  | 
|---|
| 346 | /** | 
|---|
| 347 |  * send chat message | 
|---|
| 348 |  * @param message message text | 
|---|
| 349 |  * @param messageType some int | 
|---|
| 350 |  */ | 
|---|
| 351 | void NetworkGameManager::sendChatMessage( const std::string & message, int messageType ) | 
|---|
| 352 | { | 
|---|
| 353 |   byte * buf = new byte[message.length()+3*INTSIZE]; | 
|---|
| 354 |  | 
|---|
| 355 |   assert( Converter::intToByteArray( messageType, buf, INTSIZE ) == INTSIZE ); | 
|---|
| 356 |   assert( Converter::intToByteArray( SharedNetworkData::getInstance()->getHostID(), buf+INTSIZE, INTSIZE ) == INTSIZE ); | 
|---|
| 357 |   assert( Converter::stringToByteArray(message, buf+2*INTSIZE, message.length()+INTSIZE) == message.length()+INTSIZE ); | 
|---|
| 358 |  | 
|---|
| 359 |   if ( SharedNetworkData::getInstance()->isMasterServer() /*|| SharedNetworkData::getInstance()->isProxyServerActive()*/) | 
|---|
| 360 |     MessageManager::getInstance()->sendMessage( MSGID_CHATMESSAGE, buf, message.length()+3*INTSIZE, RT_ALL_ME, NET_UNASSIGNED, MP_HIGHBANDWIDTH ); | 
|---|
| 361 |   else | 
|---|
| 362 |     MessageManager::getInstance()->sendMessage( MSGID_CHATMESSAGE, buf, message.length()+3*INTSIZE, RT_ALL_BUT_ME, NET_UNASSIGNED, MP_HIGHBANDWIDTH ); | 
|---|
| 363 |  | 
|---|
| 364 |  | 
|---|
| 365 |   delete [] buf; | 
|---|
| 366 | } | 
|---|
| 367 |  | 
|---|
| 368 |  | 
|---|
| 369 |  | 
|---|