[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: |
---|
| 12 | main-programmer: Benjamin Wuest |
---|
| 13 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | /* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module |
---|
| 18 | For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput |
---|
| 19 | */ |
---|
| 20 | #define DEBUG_MODULE_NETWORK |
---|
| 21 | |
---|
[7193] | 22 | #include "util/loading/factory.h" |
---|
[7349] | 23 | #include "state.h" |
---|
| 24 | #include "class_list.h" |
---|
| 25 | |
---|
[6341] | 26 | #include "network_stream.h" |
---|
[7349] | 27 | #include "shared_network_data.h" |
---|
[6341] | 28 | #include "converter.h" |
---|
[7954] | 29 | #include "message_manager.h" |
---|
[6341] | 30 | |
---|
[6498] | 31 | #include "playable.h" |
---|
| 32 | #include "player.h" |
---|
[6424] | 33 | |
---|
[7349] | 34 | #include "game_world.h" |
---|
[6424] | 35 | |
---|
[8068] | 36 | #include "game_rules.h" |
---|
| 37 | #include "network_game_rules.h" |
---|
| 38 | |
---|
[6116] | 39 | #include "network_game_manager.h" |
---|
[6067] | 40 | |
---|
| 41 | |
---|
| 42 | /* using namespace std is default, this needs to be here */ |
---|
| 43 | using namespace std; |
---|
| 44 | |
---|
[6341] | 45 | NetworkGameManager* NetworkGameManager::singletonRef = NULL; |
---|
| 46 | |
---|
[6067] | 47 | /*! |
---|
| 48 | * Standard constructor |
---|
| 49 | */ |
---|
[6116] | 50 | NetworkGameManager::NetworkGameManager() |
---|
[6695] | 51 | : Synchronizeable() |
---|
[6067] | 52 | { |
---|
[6341] | 53 | PRINTF(0)("START\n"); |
---|
| 54 | |
---|
[6067] | 55 | /* set the class id for the base object */ |
---|
[6341] | 56 | this->setClassID(CL_NETWORK_GAME_MANAGER, "NetworkGameManager"); |
---|
| 57 | |
---|
[6695] | 58 | this->setSynchronized(true); |
---|
[7954] | 59 | |
---|
[8068] | 60 | MessageManager::getInstance()->registerMessageHandler( MSGID_DELETESYNCHRONIZEABLE, delSynchronizeableHandler, NULL ); |
---|
[8147] | 61 | MessageManager::getInstance()->registerMessageHandler( MSGID_PREFEREDTEAM, preferedTeamHandler, NULL ); |
---|
[8068] | 62 | |
---|
| 63 | this->gameState = 0; |
---|
| 64 | registerVar( new SynchronizeableInt( &gameState, &gameState, "gameState" ) ); |
---|
[6067] | 65 | } |
---|
| 66 | |
---|
| 67 | /*! |
---|
| 68 | * Standard destructor |
---|
| 69 | */ |
---|
[6116] | 70 | NetworkGameManager::~NetworkGameManager() |
---|
[6067] | 71 | { |
---|
| 72 | } |
---|
| 73 | |
---|
[6341] | 74 | |
---|
[8068] | 75 | /** |
---|
| 76 | * insert new player into game |
---|
| 77 | * @param userId |
---|
| 78 | * @return |
---|
[6067] | 79 | */ |
---|
[8068] | 80 | bool NetworkGameManager::signalNewPlayer( int userId ) |
---|
[6067] | 81 | { |
---|
[8068] | 82 | assert( SharedNetworkData::getInstance()->isGameServer() ); |
---|
| 83 | assert( State::getGameRules() ); |
---|
| 84 | assert( State::getGameRules()->isA( CL_NETWORK_GAME_RULES ) ); |
---|
[7954] | 85 | |
---|
[8068] | 86 | NetworkGameRules & rules = *(dynamic_cast<NetworkGameRules*>(State::getGameRules())); |
---|
[7954] | 87 | |
---|
[8068] | 88 | int team = rules.getTeamForNewUser(); |
---|
[8147] | 89 | ClassID playableClassId = rules.getPlayableClassId( userId, team ); |
---|
| 90 | std::string playableModel = rules.getPlayableModelFileName( userId, team, playableClassId ); |
---|
[8068] | 91 | |
---|
| 92 | BaseObject * bo = Factory::fabricate( playableClassId ); |
---|
| 93 | |
---|
| 94 | assert( bo != NULL ); |
---|
| 95 | assert( bo->isA( CL_PLAYABLE ) ); |
---|
| 96 | |
---|
| 97 | Playable & playable = *(dynamic_cast<Playable*>(bo)); |
---|
| 98 | |
---|
| 99 | playable.loadModel( playableModel ); |
---|
| 100 | playable.setOwner( userId ); |
---|
| 101 | playable.setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); |
---|
| 102 | playable.setSynchronized( true ); |
---|
| 103 | |
---|
| 104 | PlayerStats * stats = rules.getNewPlayerStats( userId ); |
---|
| 105 | |
---|
| 106 | stats->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); |
---|
| 107 | stats->setSynchronized( true ); |
---|
| 108 | stats->setOwner( getHostID() ); |
---|
| 109 | |
---|
| 110 | stats->setTeamId( team ); |
---|
| 111 | stats->setPlayableClassId( playableClassId ); |
---|
| 112 | stats->setPlayableUniqueId( playable.getUniqueID() ); |
---|
| 113 | stats->setModelFileName( playableModel ); |
---|
[8228] | 114 | |
---|
| 115 | return true; |
---|
[6737] | 116 | } |
---|
| 117 | |
---|
[6341] | 118 | |
---|
| 119 | /** |
---|
[8068] | 120 | * remove player from game |
---|
| 121 | * @param userID |
---|
| 122 | * @return |
---|
[6341] | 123 | */ |
---|
[8068] | 124 | bool NetworkGameManager::signalLeftPlayer(int userID) |
---|
[6341] | 125 | { |
---|
[8228] | 126 | if ( PlayerStats::getStats( userID ) ) |
---|
| 127 | { |
---|
| 128 | if ( PlayerStats::getStats( userID )->getPlayable() ) |
---|
| 129 | delete PlayerStats::getStats( userID )->getPlayable(); |
---|
| 130 | delete PlayerStats::getStats( userID ); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | return true; |
---|
[6341] | 134 | } |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | /** |
---|
[8068] | 139 | * handler for remove synchronizeable messages |
---|
| 140 | * @param messageId |
---|
| 141 | * @param data |
---|
| 142 | * @param dataLength |
---|
| 143 | * @param someData |
---|
| 144 | * @param userId |
---|
| 145 | * @return true on successfull handling else handler will be called again |
---|
[6341] | 146 | */ |
---|
[8068] | 147 | bool NetworkGameManager::delSynchronizeableHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId ) |
---|
[6341] | 148 | { |
---|
[8068] | 149 | if ( getInstance()->isServer() ) |
---|
[6341] | 150 | { |
---|
[8068] | 151 | PRINTF(2)("Recieved DeleteSynchronizeable message from client %d!\n", userId); |
---|
| 152 | return true; |
---|
[6341] | 153 | } |
---|
[8068] | 154 | |
---|
| 155 | int uniqueId = 0; |
---|
| 156 | int len = Converter::byteArrayToInt( data, &uniqueId ); |
---|
| 157 | |
---|
| 158 | if ( len != dataLength ) |
---|
[6341] | 159 | { |
---|
[8068] | 160 | PRINTF(2)("Recieved DeleteSynchronizeable message with incorrect size (%d) from client %d!\n", dataLength, userId); |
---|
| 161 | return true; |
---|
[6341] | 162 | } |
---|
[7954] | 163 | |
---|
[8068] | 164 | const std::list<BaseObject*> * list = ClassList::getList( CL_SYNCHRONIZEABLE ); |
---|
[7954] | 165 | |
---|
[8068] | 166 | for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) |
---|
[6341] | 167 | { |
---|
[8068] | 168 | if ( dynamic_cast<Synchronizeable*>(*it)->getUniqueID() == uniqueId ) |
---|
[6341] | 169 | { |
---|
[8147] | 170 | if ( (*it)->isA(CL_PLAYABLE) ) |
---|
| 171 | { |
---|
| 172 | getInstance()->playablesToDelete.push_back( dynamic_cast<Playable*>(*it) ); |
---|
| 173 | return true; |
---|
| 174 | } |
---|
[8228] | 175 | |
---|
[8068] | 176 | delete dynamic_cast<Synchronizeable*>(*it); |
---|
| 177 | return true; |
---|
[6341] | 178 | } |
---|
| 179 | } |
---|
[8228] | 180 | |
---|
| 181 | return true; |
---|
[6341] | 182 | } |
---|
| 183 | |
---|
[6695] | 184 | /** |
---|
[8068] | 185 | * removes synchronizeable (also on clients) |
---|
| 186 | * @param uniqueId uniqueid to delete |
---|
[6695] | 187 | */ |
---|
[8068] | 188 | void NetworkGameManager::removeSynchronizeable( int uniqueId ) |
---|
[6695] | 189 | { |
---|
[8068] | 190 | byte buf[INTSIZE]; |
---|
[7954] | 191 | |
---|
[8068] | 192 | assert( Converter::intToByteArray( uniqueId, buf, INTSIZE ) == INTSIZE ); |
---|
[7954] | 193 | |
---|
[8068] | 194 | MessageManager::getInstance()->sendMessage( MSGID_DELETESYNCHRONIZEABLE, buf, INTSIZE, RT_ALL_NOT_ME, 0, MP_HIGHBANDWIDTH ); |
---|
[7954] | 195 | } |
---|
| 196 | |
---|
[6341] | 197 | |
---|
| 198 | |
---|
[8147] | 199 | /** |
---|
| 200 | * handler for MSGID_PREFEREDTEAM message |
---|
| 201 | * @param messageId |
---|
| 202 | * @param data |
---|
| 203 | * @param dataLength |
---|
| 204 | * @param someData |
---|
| 205 | * @param userId |
---|
| 206 | * @return |
---|
| 207 | */ |
---|
| 208 | bool NetworkGameManager::preferedTeamHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId ) |
---|
| 209 | { |
---|
| 210 | assert( NetworkGameManager::getInstance()->isServer() ); |
---|
| 211 | |
---|
| 212 | int teamId = 0; |
---|
| 213 | int len = Converter::byteArrayToInt( data, &teamId ); |
---|
| 214 | |
---|
| 215 | if ( len != dataLength ) |
---|
| 216 | { |
---|
| 217 | PRINTF(2)("Recieved DeleteSynchronizeable message with incorrect size (%d) from client %d!\n", dataLength, userId); |
---|
| 218 | return true; |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | NetworkGameManager::getInstance()->setPreferedTeam( userId, teamId ); |
---|
[8228] | 222 | |
---|
| 223 | return true; |
---|
[8147] | 224 | } |
---|
[6341] | 225 | |
---|
[8147] | 226 | void NetworkGameManager::setPreferedTeam( int userId, int teamId ) |
---|
| 227 | { |
---|
| 228 | if ( !PlayerStats::getStats( userId ) ) |
---|
| 229 | return; |
---|
| 230 | |
---|
| 231 | PlayerStats & stats = *(PlayerStats::getStats( userId )); |
---|
| 232 | |
---|
| 233 | stats.setPreferedTeamId( teamId ); |
---|
| 234 | } |
---|
[7954] | 235 | |
---|
[8147] | 236 | /** |
---|
| 237 | * set prefered team for this host |
---|
| 238 | * @param teamId |
---|
| 239 | */ |
---|
| 240 | void NetworkGameManager::prefereTeam( int teamId ) |
---|
| 241 | { |
---|
| 242 | if ( isServer() ) |
---|
| 243 | setPreferedTeam( getHostID(), teamId ); |
---|
| 244 | else |
---|
| 245 | { |
---|
| 246 | byte buf[INTSIZE]; |
---|
| 247 | |
---|
| 248 | assert( Converter::intToByteArray( teamId, buf, INTSIZE) == INTSIZE ); |
---|
| 249 | |
---|
| 250 | MessageManager::getInstance()->sendMessage( MSGID_PREFEREDTEAM, buf, INTSIZE, RT_USER, 0, MP_HIGHBANDWIDTH ); |
---|
| 251 | } |
---|
| 252 | } |
---|
[6341] | 253 | |
---|
[8147] | 254 | /** |
---|
| 255 | * this function will be called periodically by networkManager |
---|
| 256 | * @param ds time elapsed since last call of tick |
---|
| 257 | */ |
---|
| 258 | void NetworkGameManager::tick( float ds ) |
---|
| 259 | { |
---|
| 260 | //delete playables if they are not assigned to local player anymore |
---|
| 261 | for ( std::list<Playable*>::iterator it = playablesToDelete.begin(); it != playablesToDelete.end(); ) |
---|
| 262 | { |
---|
| 263 | if ( State::getPlayer()->getPlayable() != *it ) |
---|
| 264 | { |
---|
| 265 | PRINTF(0)("Delete unused playable: %s owner: %d\n", (*it)->getClassName(), (*it)->getOwner() ); |
---|
| 266 | std::list<Playable*>::iterator delit = it; |
---|
| 267 | it++; |
---|
| 268 | delete *delit; |
---|
| 269 | playablesToDelete.erase( delit ); |
---|
| 270 | continue; |
---|
| 271 | } |
---|
| 272 | it++; |
---|
| 273 | } |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | |
---|
| 277 | |
---|
| 278 | |
---|