[6067] | 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: |
---|
[9406] | 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) |
---|
[6067] | 17 | */ |
---|
| 18 | |
---|
| 19 | |
---|
[9406] | 20 | |
---|
[6067] | 21 | #define DEBUG_MODULE_NETWORK |
---|
| 22 | |
---|
[7193] | 23 | #include "util/loading/factory.h" |
---|
[7349] | 24 | #include "state.h" |
---|
| 25 | #include "class_list.h" |
---|
[8623] | 26 | #include "debug.h" |
---|
[7349] | 27 | |
---|
[6341] | 28 | #include "network_stream.h" |
---|
[7349] | 29 | #include "shared_network_data.h" |
---|
[6341] | 30 | #include "converter.h" |
---|
[7954] | 31 | #include "message_manager.h" |
---|
[6341] | 32 | |
---|
[6498] | 33 | #include "playable.h" |
---|
| 34 | #include "player.h" |
---|
[6424] | 35 | |
---|
[7349] | 36 | #include "game_world.h" |
---|
[6424] | 37 | |
---|
[8068] | 38 | #include "game_rules.h" |
---|
| 39 | #include "network_game_rules.h" |
---|
| 40 | |
---|
[6116] | 41 | #include "network_game_manager.h" |
---|
[6067] | 42 | |
---|
[9008] | 43 | #include "multiplayer_team_deathmatch.h" |
---|
[6067] | 44 | |
---|
[9235] | 45 | #include "preferences.h" |
---|
[9008] | 46 | |
---|
[9235] | 47 | |
---|
[6067] | 48 | |
---|
[9406] | 49 | |
---|
[6341] | 50 | NetworkGameManager* NetworkGameManager::singletonRef = NULL; |
---|
| 51 | |
---|
[6067] | 52 | /*! |
---|
| 53 | * Standard constructor |
---|
| 54 | */ |
---|
[6116] | 55 | NetworkGameManager::NetworkGameManager() |
---|
[6695] | 56 | : Synchronizeable() |
---|
[6067] | 57 | { |
---|
[6341] | 58 | PRINTF(0)("START\n"); |
---|
| 59 | |
---|
[6067] | 60 | /* set the class id for the base object */ |
---|
[6341] | 61 | this->setClassID(CL_NETWORK_GAME_MANAGER, "NetworkGameManager"); |
---|
| 62 | |
---|
[6695] | 63 | this->setSynchronized(true); |
---|
[9406] | 64 | |
---|
[8068] | 65 | MessageManager::getInstance()->registerMessageHandler( MSGID_DELETESYNCHRONIZEABLE, delSynchronizeableHandler, NULL ); |
---|
[8147] | 66 | MessageManager::getInstance()->registerMessageHandler( MSGID_PREFEREDTEAM, preferedTeamHandler, NULL ); |
---|
[8623] | 67 | MessageManager::getInstance()->registerMessageHandler( MSGID_CHATMESSAGE, chatMessageHandler, NULL ); |
---|
[9406] | 68 | |
---|
[8068] | 69 | this->gameState = 0; |
---|
| 70 | registerVar( new SynchronizeableInt( &gameState, &gameState, "gameState" ) ); |
---|
[6067] | 71 | } |
---|
| 72 | |
---|
| 73 | /*! |
---|
| 74 | * Standard destructor |
---|
| 75 | */ |
---|
[6116] | 76 | NetworkGameManager::~NetworkGameManager() |
---|
[6067] | 77 | { |
---|
[8623] | 78 | delete MessageManager::getInstance(); |
---|
[9406] | 79 | |
---|
[8623] | 80 | PlayerStats::deleteAllPlayerStats(); |
---|
[9406] | 81 | |
---|
[9059] | 82 | NetworkGameManager::singletonRef = NULL; |
---|
[6067] | 83 | } |
---|
| 84 | |
---|
[6341] | 85 | |
---|
[8068] | 86 | /** |
---|
| 87 | * insert new player into game |
---|
[9406] | 88 | * @param userId |
---|
| 89 | * @return |
---|
[6067] | 90 | */ |
---|
[8068] | 91 | bool NetworkGameManager::signalNewPlayer( int userId ) |
---|
[6067] | 92 | { |
---|
[9406] | 93 | assert( SharedNetworkData::getInstance()->isMasterServer() || SharedNetworkData::getInstance()->isProxyServer()); |
---|
[8068] | 94 | assert( State::getGameRules() ); |
---|
| 95 | assert( State::getGameRules()->isA( CL_NETWORK_GAME_RULES ) ); |
---|
[9406] | 96 | |
---|
[8068] | 97 | NetworkGameRules & rules = *(dynamic_cast<NetworkGameRules*>(State::getGameRules())); |
---|
[9406] | 98 | |
---|
[8068] | 99 | int team = rules.getTeamForNewUser(); |
---|
[8147] | 100 | ClassID playableClassId = rules.getPlayableClassId( userId, team ); |
---|
| 101 | std::string playableModel = rules.getPlayableModelFileName( userId, team, playableClassId ); |
---|
[9110] | 102 | std::string playableTexture = rules.getPlayableModelFileName( userId, team, playableClassId ); |
---|
[9235] | 103 | float playableScale = rules.getPlayableScale( userId, team, playableClassId ); |
---|
[9406] | 104 | |
---|
[8068] | 105 | BaseObject * bo = Factory::fabricate( playableClassId ); |
---|
[9406] | 106 | |
---|
[8068] | 107 | assert( bo != NULL ); |
---|
| 108 | assert( bo->isA( CL_PLAYABLE ) ); |
---|
[9406] | 109 | |
---|
[8068] | 110 | Playable & playable = *(dynamic_cast<Playable*>(bo)); |
---|
[9406] | 111 | |
---|
[9235] | 112 | playable.loadMD2Texture( playableTexture ); |
---|
[9406] | 113 | |
---|
[9235] | 114 | playable.loadModel( playableModel, 100.0f ); |
---|
[8068] | 115 | playable.setOwner( userId ); |
---|
| 116 | playable.setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); |
---|
| 117 | playable.setSynchronized( true ); |
---|
[9406] | 118 | |
---|
[8068] | 119 | PlayerStats * stats = rules.getNewPlayerStats( userId ); |
---|
[9406] | 120 | |
---|
[8068] | 121 | stats->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); |
---|
| 122 | stats->setSynchronized( true ); |
---|
[8708] | 123 | stats->setOwner( SharedNetworkData::getInstance()->getHostID() ); |
---|
[9406] | 124 | |
---|
[8068] | 125 | stats->setTeamId( team ); |
---|
| 126 | stats->setPlayableClassId( playableClassId ); |
---|
| 127 | stats->setPlayableUniqueId( playable.getUniqueID() ); |
---|
| 128 | stats->setModelFileName( playableModel ); |
---|
[9406] | 129 | |
---|
[9235] | 130 | if ( userId == 0 ) |
---|
| 131 | stats->setNickName( Preferences::getInstance()->getString( "multiplayer", "nickname", "Server" ) ); |
---|
[9406] | 132 | |
---|
[9008] | 133 | if ( rules.isA( CL_MULTIPLAYER_TEAM_DEATHMATCH ) ) |
---|
| 134 | dynamic_cast<MultiplayerTeamDeathmatch*>(&rules)->respawnPlayable( &playable, team, 0.0f ); |
---|
[9406] | 135 | |
---|
[8228] | 136 | return true; |
---|
[6737] | 137 | } |
---|
| 138 | |
---|
[6341] | 139 | |
---|
| 140 | /** |
---|
[8068] | 141 | * remove player from game |
---|
[9406] | 142 | * @param userID |
---|
| 143 | * @return |
---|
[6341] | 144 | */ |
---|
[8068] | 145 | bool NetworkGameManager::signalLeftPlayer(int userID) |
---|
[6341] | 146 | { |
---|
[8228] | 147 | if ( PlayerStats::getStats( userID ) ) |
---|
| 148 | { |
---|
| 149 | if ( PlayerStats::getStats( userID )->getPlayable() ) |
---|
| 150 | delete PlayerStats::getStats( userID )->getPlayable(); |
---|
| 151 | delete PlayerStats::getStats( userID ); |
---|
| 152 | } |
---|
[9406] | 153 | |
---|
[8228] | 154 | return true; |
---|
[6341] | 155 | } |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | /** |
---|
[8068] | 160 | * handler for remove synchronizeable messages |
---|
[9406] | 161 | * @param messageId |
---|
| 162 | * @param data |
---|
| 163 | * @param dataLength |
---|
| 164 | * @param someData |
---|
| 165 | * @param userId |
---|
[8068] | 166 | * @return true on successfull handling else handler will be called again |
---|
[6341] | 167 | */ |
---|
[8068] | 168 | bool NetworkGameManager::delSynchronizeableHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId ) |
---|
[6341] | 169 | { |
---|
[9406] | 170 | if ( SharedNetworkData::getInstance()->isMasterServer() || SharedNetworkData::getInstance()->isProxyServer()) |
---|
[6341] | 171 | { |
---|
[8068] | 172 | PRINTF(2)("Recieved DeleteSynchronizeable message from client %d!\n", userId); |
---|
| 173 | return true; |
---|
[6341] | 174 | } |
---|
[9406] | 175 | |
---|
[8068] | 176 | int uniqueId = 0; |
---|
| 177 | int len = Converter::byteArrayToInt( data, &uniqueId ); |
---|
[9406] | 178 | |
---|
[8068] | 179 | if ( len != dataLength ) |
---|
[6341] | 180 | { |
---|
[8068] | 181 | PRINTF(2)("Recieved DeleteSynchronizeable message with incorrect size (%d) from client %d!\n", dataLength, userId); |
---|
| 182 | return true; |
---|
[6341] | 183 | } |
---|
[9406] | 184 | |
---|
[8068] | 185 | const std::list<BaseObject*> * list = ClassList::getList( CL_SYNCHRONIZEABLE ); |
---|
[9406] | 186 | |
---|
[8068] | 187 | for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) |
---|
[6341] | 188 | { |
---|
[8068] | 189 | if ( dynamic_cast<Synchronizeable*>(*it)->getUniqueID() == uniqueId ) |
---|
[6341] | 190 | { |
---|
[8147] | 191 | if ( (*it)->isA(CL_PLAYABLE) ) |
---|
| 192 | { |
---|
| 193 | getInstance()->playablesToDelete.push_back( dynamic_cast<Playable*>(*it) ); |
---|
| 194 | return true; |
---|
| 195 | } |
---|
[9406] | 196 | |
---|
[8068] | 197 | delete dynamic_cast<Synchronizeable*>(*it); |
---|
| 198 | return true; |
---|
[6341] | 199 | } |
---|
| 200 | } |
---|
[9406] | 201 | |
---|
[8228] | 202 | return true; |
---|
[6341] | 203 | } |
---|
| 204 | |
---|
[6695] | 205 | /** |
---|
[8068] | 206 | * removes synchronizeable (also on clients) |
---|
| 207 | * @param uniqueId uniqueid to delete |
---|
[6695] | 208 | */ |
---|
[8068] | 209 | void NetworkGameManager::removeSynchronizeable( int uniqueId ) |
---|
[6695] | 210 | { |
---|
[8068] | 211 | byte buf[INTSIZE]; |
---|
[9406] | 212 | |
---|
[8068] | 213 | assert( Converter::intToByteArray( uniqueId, buf, INTSIZE ) == INTSIZE ); |
---|
[8362] | 214 | |
---|
[8068] | 215 | MessageManager::getInstance()->sendMessage( MSGID_DELETESYNCHRONIZEABLE, buf, INTSIZE, RT_ALL_NOT_ME, 0, MP_HIGHBANDWIDTH ); |
---|
[7954] | 216 | } |
---|
| 217 | |
---|
[6341] | 218 | |
---|
| 219 | |
---|
[8147] | 220 | /** |
---|
| 221 | * handler for MSGID_PREFEREDTEAM message |
---|
[9406] | 222 | * @param messageId |
---|
| 223 | * @param data |
---|
| 224 | * @param dataLength |
---|
| 225 | * @param someData |
---|
| 226 | * @param userId |
---|
| 227 | * @return |
---|
[8147] | 228 | */ |
---|
| 229 | bool NetworkGameManager::preferedTeamHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId ) |
---|
| 230 | { |
---|
[9406] | 231 | assert( SharedNetworkData::getInstance()->isMasterServer() || SharedNetworkData::getInstance()->isProxyServer()); |
---|
| 232 | |
---|
[8147] | 233 | int teamId = 0; |
---|
| 234 | int len = Converter::byteArrayToInt( data, &teamId ); |
---|
[9406] | 235 | |
---|
[8147] | 236 | if ( len != dataLength ) |
---|
| 237 | { |
---|
| 238 | PRINTF(2)("Recieved DeleteSynchronizeable message with incorrect size (%d) from client %d!\n", dataLength, userId); |
---|
| 239 | return true; |
---|
| 240 | } |
---|
[9406] | 241 | |
---|
[8147] | 242 | NetworkGameManager::getInstance()->setPreferedTeam( userId, teamId ); |
---|
[9406] | 243 | |
---|
[8228] | 244 | return true; |
---|
[8147] | 245 | } |
---|
[6341] | 246 | |
---|
[8147] | 247 | void NetworkGameManager::setPreferedTeam( int userId, int teamId ) |
---|
| 248 | { |
---|
| 249 | if ( !PlayerStats::getStats( userId ) ) |
---|
| 250 | return; |
---|
[9406] | 251 | |
---|
[8147] | 252 | PlayerStats & stats = *(PlayerStats::getStats( userId )); |
---|
[9406] | 253 | |
---|
[8147] | 254 | stats.setPreferedTeamId( teamId ); |
---|
| 255 | } |
---|
[7954] | 256 | |
---|
[8147] | 257 | /** |
---|
| 258 | * set prefered team for this host |
---|
[9406] | 259 | * @param teamId |
---|
[8147] | 260 | */ |
---|
| 261 | void NetworkGameManager::prefereTeam( int teamId ) |
---|
| 262 | { |
---|
[9406] | 263 | if ( SharedNetworkData::getInstance()->isMasterServer() || SharedNetworkData::getInstance()->isProxyServer()) |
---|
[8708] | 264 | setPreferedTeam( SharedNetworkData::getInstance()->getHostID(), teamId ); |
---|
[8147] | 265 | else |
---|
| 266 | { |
---|
| 267 | byte buf[INTSIZE]; |
---|
[9406] | 268 | |
---|
[8147] | 269 | assert( Converter::intToByteArray( teamId, buf, INTSIZE) == INTSIZE ); |
---|
[9406] | 270 | |
---|
[8147] | 271 | MessageManager::getInstance()->sendMessage( MSGID_PREFEREDTEAM, buf, INTSIZE, RT_USER, 0, MP_HIGHBANDWIDTH ); |
---|
| 272 | } |
---|
| 273 | } |
---|
[6341] | 274 | |
---|
[8147] | 275 | /** |
---|
| 276 | * this function will be called periodically by networkManager |
---|
| 277 | * @param ds time elapsed since last call of tick |
---|
| 278 | */ |
---|
| 279 | void NetworkGameManager::tick( float ds ) |
---|
| 280 | { |
---|
| 281 | //delete playables if they are not assigned to local player anymore |
---|
| 282 | for ( std::list<Playable*>::iterator it = playablesToDelete.begin(); it != playablesToDelete.end(); ) |
---|
| 283 | { |
---|
| 284 | if ( State::getPlayer()->getPlayable() != *it ) |
---|
| 285 | { |
---|
[9059] | 286 | const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYABLE ); |
---|
[9406] | 287 | |
---|
[9059] | 288 | if ( list && std::find( list->begin(), list->end(), *it ) != list->end() ) |
---|
[9406] | 289 | { |
---|
| 290 | PRINTF(0)("Delete unused playable: %s owner: %d\n", (*it)->getClassCName(), (*it)->getOwner() ); |
---|
[9059] | 291 | std::list<Playable*>::iterator delit = it; |
---|
| 292 | it++; |
---|
| 293 | delete *delit; |
---|
| 294 | playablesToDelete.erase( delit ); |
---|
| 295 | continue; |
---|
| 296 | } |
---|
[8147] | 297 | } |
---|
| 298 | it++; |
---|
| 299 | } |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | |
---|
| 303 | |
---|
[8623] | 304 | bool NetworkGameManager::chatMessageHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId ) |
---|
| 305 | { |
---|
[8708] | 306 | PRINTF(0)("NetworkGameManager::chatMessageHandler %d %d\n", userId, SharedNetworkData::getInstance()->getHostID() ); |
---|
[9406] | 307 | if ( (SharedNetworkData::getInstance()->isMasterServer() || SharedNetworkData::getInstance()->isProxyServer()) && userId != SharedNetworkData::getInstance()->getHostID() ) |
---|
[8708] | 308 | { |
---|
| 309 | MessageManager::getInstance()->sendMessage( messageId, data, dataLength, RT_ALL_NOT_ME, 0, MP_HIGHBANDWIDTH ); |
---|
| 310 | } |
---|
[9406] | 311 | |
---|
[8623] | 312 | assert( State::getGameRules() ); |
---|
| 313 | assert( State::getGameRules()->isA( CL_NETWORK_GAME_RULES ) ); |
---|
[9406] | 314 | |
---|
[8623] | 315 | NetworkGameRules & rules = *(dynamic_cast<NetworkGameRules*>(State::getGameRules())); |
---|
[9406] | 316 | |
---|
[8708] | 317 | if ( dataLength < 3*INTSIZE ) |
---|
[8623] | 318 | { |
---|
| 319 | PRINTF(2)("got too small chatmessage from client %d\n", userId); |
---|
[9406] | 320 | |
---|
[8623] | 321 | return true; |
---|
| 322 | } |
---|
[9406] | 323 | |
---|
[8623] | 324 | int messageType = 0; |
---|
| 325 | Converter::byteArrayToInt( data, &messageType ); |
---|
[8708] | 326 | int senderUserId = 0; |
---|
| 327 | Converter::byteArrayToInt( data+INTSIZE, &senderUserId ); |
---|
[8623] | 328 | std::string message; |
---|
[8708] | 329 | Converter::byteArrayToString( data+2*INTSIZE, message, dataLength-2*INTSIZE ); |
---|
[9406] | 330 | |
---|
[8708] | 331 | rules.handleChatMessage( senderUserId, message, messageType ); |
---|
[8147] | 332 | |
---|
[8623] | 333 | return true; |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | /** |
---|
| 337 | * send chat message |
---|
| 338 | * @param message message text |
---|
| 339 | * @param messageType some int |
---|
| 340 | */ |
---|
[8708] | 341 | void NetworkGameManager::sendChatMessage( const std::string & message, int messageType ) |
---|
[8623] | 342 | { |
---|
[8708] | 343 | byte * buf = new byte[message.length()+3*INTSIZE]; |
---|
[8623] | 344 | |
---|
| 345 | assert( Converter::intToByteArray( messageType, buf, INTSIZE ) == INTSIZE ); |
---|
[8708] | 346 | assert( Converter::intToByteArray( SharedNetworkData::getInstance()->getHostID(), buf+INTSIZE, INTSIZE ) == INTSIZE ); |
---|
| 347 | assert( Converter::stringToByteArray(message, buf+2*INTSIZE, message.length()+INTSIZE) == message.length()+INTSIZE ); |
---|
[9406] | 348 | |
---|
| 349 | if ( SharedNetworkData::getInstance()->isMasterServer() || SharedNetworkData::getInstance()->isProxyServer()) |
---|
[8708] | 350 | MessageManager::getInstance()->sendMessage( MSGID_CHATMESSAGE, buf, message.length()+3*INTSIZE, RT_ALL_ME, 0, MP_HIGHBANDWIDTH ); |
---|
[8623] | 351 | else |
---|
[8708] | 352 | MessageManager::getInstance()->sendMessage( MSGID_CHATMESSAGE, buf, message.length()+3*INTSIZE, RT_ALL_NOT_ME, 0, MP_HIGHBANDWIDTH ); |
---|
| 353 | |
---|
[9406] | 354 | |
---|
[8623] | 355 | delete [] buf; |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | |
---|
| 359 | |
---|