[1705] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Oliver Scheuss, (C) 2007 |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | // |
---|
| 30 | // C++ Implementation: GameStateManager |
---|
| 31 | // |
---|
| 32 | // Description: |
---|
| 33 | // |
---|
| 34 | // |
---|
| 35 | // Author: Oliver Scheuss, (C) 2007 |
---|
| 36 | // |
---|
| 37 | // Copyright: See COPYING file that comes with this distribution |
---|
| 38 | // |
---|
| 39 | // |
---|
| 40 | |
---|
| 41 | #include "GamestateManager.h" |
---|
| 42 | |
---|
| 43 | #include <utility> |
---|
| 44 | #include <iostream> |
---|
| 45 | #include <zlib.h> |
---|
[1755] | 46 | #include <cassert> |
---|
[1705] | 47 | |
---|
| 48 | #include "core/CoreIncludes.h" |
---|
| 49 | #include "core/BaseObject.h" |
---|
| 50 | #include "ClientInformation.h" |
---|
[2371] | 51 | #include "synchronisable/Synchronisable.h" |
---|
| 52 | #include "synchronisable/NetworkCallbackManager.h" |
---|
[1705] | 53 | |
---|
[2171] | 54 | namespace orxonox |
---|
[1705] | 55 | { |
---|
| 56 | GamestateManager::GamestateManager() { |
---|
| 57 | id_=0; |
---|
[2382] | 58 | trafficControl_ = new TrafficControl(); |
---|
[1705] | 59 | } |
---|
| 60 | |
---|
| 61 | GamestateManager::~GamestateManager() { |
---|
[2382] | 62 | delete trafficControl_; |
---|
[1705] | 63 | } |
---|
| 64 | |
---|
| 65 | bool GamestateManager::update(){ |
---|
[1907] | 66 | // cleanup(); |
---|
[1730] | 67 | return getSnapshot(); |
---|
[1705] | 68 | } |
---|
[2087] | 69 | |
---|
| 70 | bool GamestateManager::add(packet::Gamestate *gs, unsigned int clientID){ |
---|
[1705] | 71 | assert(gs); |
---|
[2087] | 72 | std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateQueue.find(clientID); |
---|
[1705] | 73 | if(it!=gamestateQueue.end()){ |
---|
| 74 | // delete obsolete gamestate |
---|
| 75 | delete it->second; |
---|
| 76 | } |
---|
| 77 | gamestateQueue[clientID] = gs; |
---|
| 78 | return true; |
---|
| 79 | } |
---|
[2087] | 80 | |
---|
[1705] | 81 | bool GamestateManager::processGamestates(){ |
---|
[2087] | 82 | std::map<unsigned int, packet::Gamestate*>::iterator it; |
---|
[1705] | 83 | // now push only the most recent gamestates we received (ignore obsolete ones) |
---|
| 84 | for(it = gamestateQueue.begin(); it!=gamestateQueue.end(); it++){ |
---|
[2087] | 85 | bool b = processGamestate(it->second); |
---|
| 86 | assert(b); |
---|
[1705] | 87 | delete it->second; |
---|
| 88 | } |
---|
| 89 | // now clear the queue |
---|
| 90 | gamestateQueue.clear(); |
---|
[2371] | 91 | //and call all queued callbacks |
---|
| 92 | NetworkCallbackManager::callCallbacks(); |
---|
[1705] | 93 | return true; |
---|
| 94 | } |
---|
[2087] | 95 | |
---|
| 96 | |
---|
[1705] | 97 | bool GamestateManager::getSnapshot(){ |
---|
| 98 | reference = new packet::Gamestate(); |
---|
[2087] | 99 | if(!reference->collectData(++id_)){ //we have no data to send |
---|
| 100 | delete reference; |
---|
| 101 | reference=0; |
---|
| 102 | } |
---|
[1705] | 103 | return true; |
---|
| 104 | } |
---|
[2087] | 105 | |
---|
[1705] | 106 | /** |
---|
| 107 | * this function is used to keep the memory usage low |
---|
| 108 | * it tries to delete all the unused gamestates |
---|
[2087] | 109 | * |
---|
| 110 | * |
---|
[1705] | 111 | */ |
---|
[1907] | 112 | /* void GamestateManager::cleanup(){ |
---|
[1705] | 113 | std::map<int,int>::iterator it = gamestateUsed.begin(); |
---|
| 114 | while(it!=gamestateUsed.end()){ |
---|
| 115 | if((id_-(*it).first)<KEEP_GAMESTATES) |
---|
| 116 | break; |
---|
| 117 | if( (*it).second <= 0 ){ |
---|
| 118 | COUT(5) << "GameStateManager: deleting gamestate with id: " << (*it).first << ", uses: " << (*it).second << std::endl; |
---|
| 119 | std::map<int, packet::Gamestate *>::iterator tempit = gamestateMap.find((*it).first); |
---|
| 120 | if( tempit != gamestateMap.end() ){ |
---|
| 121 | packet::Gamestate *temp = tempit->second; |
---|
| 122 | if(temp){ |
---|
| 123 | delete gamestateMap[(*it).first]; |
---|
| 124 | gamestateMap.erase((*it).first); |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | gamestateUsed.erase(it++); |
---|
| 128 | continue; |
---|
| 129 | } |
---|
| 130 | it++; |
---|
| 131 | } |
---|
[1907] | 132 | }*/ |
---|
[1705] | 133 | |
---|
[2087] | 134 | packet::Gamestate *GamestateManager::popGameState(unsigned int clientID) { |
---|
[1705] | 135 | //why are we searching the same client's gamestate id as we searched in |
---|
| 136 | //Server::sendGameState? |
---|
[1751] | 137 | packet::Gamestate *gs; |
---|
[2087] | 138 | unsigned int gID = ClientInformation::findClient(clientID)->getGamestateID(); |
---|
| 139 | if(!reference) |
---|
| 140 | return 0; |
---|
[2371] | 141 | gs = reference->doSelection(clientID, 10000); |
---|
[1907] | 142 | // gs = new packet::Gamestate(*reference); |
---|
| 143 | // save the (undiffed) gamestate in the clients gamestate map |
---|
| 144 | gamestateMap_[clientID].insert(std::pair<int, packet::Gamestate*>(gs->getID(), gs)); |
---|
[1705] | 145 | //chose wheather the next gamestate is the first or not |
---|
[1907] | 146 | packet::Gamestate *client=NULL; |
---|
[1705] | 147 | if(gID != GAMESTATEID_INITIAL){ |
---|
[2087] | 148 | std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(clientID); |
---|
[1907] | 149 | if(clientMap!=gamestateMap_.end()){ |
---|
[2087] | 150 | std::map<unsigned int, packet::Gamestate*>::iterator it = clientMap->second.find(gID); |
---|
[1907] | 151 | if(it!=clientMap->second.end()) |
---|
| 152 | client = it->second; |
---|
| 153 | } |
---|
[1705] | 154 | } |
---|
[1907] | 155 | if(client){ |
---|
| 156 | // COUT(3) << "diffing" << std::endl; |
---|
| 157 | gs = gs->diff(client); |
---|
| 158 | } |
---|
| 159 | else{ |
---|
| 160 | // COUT(3) << "not diffing" << std::endl; |
---|
| 161 | gs = new packet::Gamestate(*gs); |
---|
| 162 | } |
---|
| 163 | bool b = gs->compressData(); |
---|
| 164 | assert(b); |
---|
[1751] | 165 | return gs; |
---|
[1705] | 166 | } |
---|
[2087] | 167 | |
---|
| 168 | |
---|
| 169 | bool GamestateManager::ack(unsigned int gamestateID, unsigned int clientID) { |
---|
[1705] | 170 | ClientInformation *temp = ClientInformation::findClient(clientID); |
---|
[1907] | 171 | assert(temp); |
---|
[2087] | 172 | unsigned int curid = temp->getGamestateID(); |
---|
| 173 | |
---|
[1769] | 174 | if(gamestateID == 0){ |
---|
[1705] | 175 | temp->setGamestateID(GAMESTATEID_INITIAL); |
---|
[1769] | 176 | return true; |
---|
[1705] | 177 | } |
---|
[2087] | 178 | |
---|
| 179 | assert(curid==(unsigned int)GAMESTATEID_INITIAL || curid<gamestateID); |
---|
[1705] | 180 | COUT(4) << "acking gamestate " << gamestateID << " for clientid: " << clientID << " curid: " << curid << std::endl; |
---|
[2087] | 181 | std::map<unsigned int, packet::Gamestate*>::iterator it, tempit; |
---|
[1907] | 182 | for(it = gamestateMap_[clientID].begin(); it!=gamestateMap_[clientID].end() && it->first<gamestateID; it++){ |
---|
| 183 | delete it->second; |
---|
| 184 | tempit=it++; |
---|
| 185 | gamestateMap_[clientID].erase(tempit); |
---|
[1705] | 186 | } |
---|
[1907] | 187 | temp->setGamestateID(gamestateID); |
---|
[2381] | 188 | TrafficControl::processAck(clientID, gamestateID); |
---|
[1705] | 189 | return true; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | void GamestateManager::removeClient(ClientInformation* client){ |
---|
[1907] | 193 | assert(client); |
---|
[2087] | 194 | std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(client->getID()); |
---|
[1907] | 195 | // first delete all remained gamestates |
---|
[2087] | 196 | std::map<unsigned int, packet::Gamestate*>::iterator it; |
---|
[1907] | 197 | for(it=clientMap->second.begin(); it!=clientMap->second.end(); it++) |
---|
| 198 | delete it->second; |
---|
| 199 | // now delete the clients gamestatemap |
---|
| 200 | gamestateMap_.erase(clientMap); |
---|
[1705] | 201 | } |
---|
[2087] | 202 | |
---|
[1712] | 203 | bool GamestateManager::processGamestate(packet::Gamestate *gs){ |
---|
[1751] | 204 | if(gs->isCompressed()) |
---|
[1907] | 205 | { |
---|
| 206 | bool b = gs->decompressData(); |
---|
| 207 | assert(b); |
---|
| 208 | } |
---|
[1712] | 209 | assert(!gs->isDiffed()); |
---|
| 210 | return gs->spreadData(); |
---|
| 211 | } |
---|
[1705] | 212 | |
---|
| 213 | } |
---|