| 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 | |
|---|
| 22 | #include "util/loading/factory.h" |
|---|
| 23 | #include "state.h" |
|---|
| 24 | #include "class_list.h" |
|---|
| 25 | |
|---|
| 26 | #include "network_stream.h" |
|---|
| 27 | #include "shared_network_data.h" |
|---|
| 28 | #include "converter.h" |
|---|
| 29 | #include "message_manager.h" |
|---|
| 30 | |
|---|
| 31 | #include "playable.h" |
|---|
| 32 | #include "player.h" |
|---|
| 33 | |
|---|
| 34 | #include "game_world.h" |
|---|
| 35 | |
|---|
| 36 | #include "game_rules.h" |
|---|
| 37 | #include "network_game_rules.h" |
|---|
| 38 | |
|---|
| 39 | #include "network_game_manager.h" |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | /* using namespace std is default, this needs to be here */ |
|---|
| 43 | using namespace std; |
|---|
| 44 | |
|---|
| 45 | NetworkGameManager* NetworkGameManager::singletonRef = NULL; |
|---|
| 46 | |
|---|
| 47 | /*! |
|---|
| 48 | * Standard constructor |
|---|
| 49 | */ |
|---|
| 50 | NetworkGameManager::NetworkGameManager() |
|---|
| 51 | : Synchronizeable() |
|---|
| 52 | { |
|---|
| 53 | PRINTF(0)("START\n"); |
|---|
| 54 | |
|---|
| 55 | /* set the class id for the base object */ |
|---|
| 56 | this->setClassID(CL_NETWORK_GAME_MANAGER, "NetworkGameManager"); |
|---|
| 57 | |
|---|
| 58 | this->setSynchronized(true); |
|---|
| 59 | |
|---|
| 60 | MessageManager::getInstance()->registerMessageHandler( MSGID_DELETESYNCHRONIZEABLE, delSynchronizeableHandler, NULL ); |
|---|
| 61 | |
|---|
| 62 | this->gameState = 0; |
|---|
| 63 | registerVar( new SynchronizeableInt( &gameState, &gameState, "gameState" ) ); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | /*! |
|---|
| 67 | * Standard destructor |
|---|
| 68 | */ |
|---|
| 69 | NetworkGameManager::~NetworkGameManager() |
|---|
| 70 | { |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | /** |
|---|
| 75 | * insert new player into game |
|---|
| 76 | * @param userId |
|---|
| 77 | * @return |
|---|
| 78 | */ |
|---|
| 79 | bool NetworkGameManager::signalNewPlayer( int userId ) |
|---|
| 80 | { |
|---|
| 81 | assert( SharedNetworkData::getInstance()->isGameServer() ); |
|---|
| 82 | assert( State::getGameRules() ); |
|---|
| 83 | assert( State::getGameRules()->isA( CL_NETWORK_GAME_RULES ) ); |
|---|
| 84 | |
|---|
| 85 | NetworkGameRules & rules = *(dynamic_cast<NetworkGameRules*>(State::getGameRules())); |
|---|
| 86 | |
|---|
| 87 | int team = rules.getTeamForNewUser(); |
|---|
| 88 | ClassID playableClassId = rules.getPlayableClassId( team ); |
|---|
| 89 | std::string playableModel = rules.getPlayableModelFileName( team, playableClassId ); |
|---|
| 90 | |
|---|
| 91 | BaseObject * bo = Factory::fabricate( playableClassId ); |
|---|
| 92 | |
|---|
| 93 | assert( bo != NULL ); |
|---|
| 94 | assert( bo->isA( CL_PLAYABLE ) ); |
|---|
| 95 | |
|---|
| 96 | Playable & playable = *(dynamic_cast<Playable*>(bo)); |
|---|
| 97 | |
|---|
| 98 | playable.loadModel( playableModel ); |
|---|
| 99 | playable.setOwner( userId ); |
|---|
| 100 | playable.setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); |
|---|
| 101 | playable.setSynchronized( true ); |
|---|
| 102 | |
|---|
| 103 | PlayerStats * stats = rules.getNewPlayerStats( userId ); |
|---|
| 104 | |
|---|
| 105 | stats->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); |
|---|
| 106 | stats->setSynchronized( true ); |
|---|
| 107 | stats->setOwner( getHostID() ); |
|---|
| 108 | |
|---|
| 109 | stats->setTeamId( team ); |
|---|
| 110 | stats->setPlayableClassId( playableClassId ); |
|---|
| 111 | stats->setPlayableUniqueId( playable.getUniqueID() ); |
|---|
| 112 | stats->setModelFileName( playableModel ); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | /** |
|---|
| 117 | * remove player from game |
|---|
| 118 | * @param userID |
|---|
| 119 | * @return |
|---|
| 120 | */ |
|---|
| 121 | bool NetworkGameManager::signalLeftPlayer(int userID) |
|---|
| 122 | { |
|---|
| 123 | delete PlayerStats::getStats( userID )->getPlayable(); |
|---|
| 124 | delete PlayerStats::getStats( userID ); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | /** |
|---|
| 130 | * handler for remove synchronizeable messages |
|---|
| 131 | * @param messageId |
|---|
| 132 | * @param data |
|---|
| 133 | * @param dataLength |
|---|
| 134 | * @param someData |
|---|
| 135 | * @param userId |
|---|
| 136 | * @return true on successfull handling else handler will be called again |
|---|
| 137 | */ |
|---|
| 138 | bool NetworkGameManager::delSynchronizeableHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId ) |
|---|
| 139 | { |
|---|
| 140 | if ( getInstance()->isServer() ) |
|---|
| 141 | { |
|---|
| 142 | PRINTF(2)("Recieved DeleteSynchronizeable message from client %d!\n", userId); |
|---|
| 143 | return true; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | int uniqueId = 0; |
|---|
| 147 | int len = Converter::byteArrayToInt( data, &uniqueId ); |
|---|
| 148 | |
|---|
| 149 | if ( len != dataLength ) |
|---|
| 150 | { |
|---|
| 151 | PRINTF(2)("Recieved DeleteSynchronizeable message with incorrect size (%d) from client %d!\n", dataLength, userId); |
|---|
| 152 | return true; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | const std::list<BaseObject*> * list = ClassList::getList( CL_SYNCHRONIZEABLE ); |
|---|
| 156 | |
|---|
| 157 | for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) |
|---|
| 158 | { |
|---|
| 159 | if ( dynamic_cast<Synchronizeable*>(*it)->getUniqueID() == uniqueId ) |
|---|
| 160 | { |
|---|
| 161 | delete dynamic_cast<Synchronizeable*>(*it); |
|---|
| 162 | return true; |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | /** |
|---|
| 168 | * removes synchronizeable (also on clients) |
|---|
| 169 | * @param uniqueId uniqueid to delete |
|---|
| 170 | */ |
|---|
| 171 | void NetworkGameManager::removeSynchronizeable( int uniqueId ) |
|---|
| 172 | { |
|---|
| 173 | byte buf[INTSIZE]; |
|---|
| 174 | |
|---|
| 175 | assert( Converter::intToByteArray( uniqueId, buf, INTSIZE ) == INTSIZE ); |
|---|
| 176 | |
|---|
| 177 | MessageManager::getInstance()->sendMessage( MSGID_DELETESYNCHRONIZEABLE, buf, INTSIZE, RT_ALL_NOT_ME, 0, MP_HIGHBANDWIDTH ); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|