[514] | 1 | /* |
---|
[777] | 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * |
---|
| 4 | * |
---|
| 5 | * License notice: |
---|
| 6 | * |
---|
| 7 | * This program is free software; you can redistribute it and/or |
---|
| 8 | * modify it under the terms of the GNU General Public License |
---|
| 9 | * as published by the Free Software Foundation; either version 2 |
---|
| 10 | * of the License, or (at your option) any later version. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 20 | * |
---|
| 21 | * Author: |
---|
| 22 | * Oliver Scheuss, (C) 2007 |
---|
| 23 | * Co-authors: |
---|
| 24 | * ... |
---|
| 25 | * |
---|
| 26 | */ |
---|
[514] | 27 | |
---|
[247] | 28 | // |
---|
| 29 | // C++ Implementation: GameStateManager |
---|
| 30 | // |
---|
[514] | 31 | // Description: |
---|
[247] | 32 | // |
---|
| 33 | // |
---|
| 34 | // Author: Oliver Scheuss, (C) 2007 |
---|
| 35 | // |
---|
| 36 | // Copyright: See COPYING file that comes with this distribution |
---|
| 37 | // |
---|
| 38 | // |
---|
| 39 | |
---|
[777] | 40 | #include <utility> |
---|
| 41 | #include <iostream> |
---|
| 42 | #include <zlib.h> |
---|
[247] | 43 | |
---|
[777] | 44 | #include "core/CoreIncludes.h" |
---|
[905] | 45 | |
---|
[777] | 46 | #include "ClientInformation.h" |
---|
| 47 | #include "Synchronisable.h" |
---|
| 48 | #include "GameStateManager.h" |
---|
[247] | 49 | |
---|
[777] | 50 | namespace network |
---|
[247] | 51 | { |
---|
[777] | 52 | GameStateManager::GameStateManager(ClientInformation *head) { |
---|
| 53 | id=0; |
---|
| 54 | head_=head; |
---|
| 55 | } |
---|
[247] | 56 | |
---|
[777] | 57 | GameStateManager::~GameStateManager() { |
---|
| 58 | } |
---|
[413] | 59 | |
---|
[777] | 60 | void GameStateManager::update(){ |
---|
[1007] | 61 | cleanup(); |
---|
[777] | 62 | reference = getSnapshot(id); |
---|
| 63 | gameStateMap.insert(std::pair<int, GameState*>(id, reference)); |
---|
| 64 | gameStateUsed[id]=0; |
---|
| 65 | ++id; |
---|
| 66 | return; |
---|
[436] | 67 | } |
---|
[1005] | 68 | |
---|
| 69 | void GameStateManager::cleanup(){ |
---|
| 70 | /*unsigned int min_id=-1; |
---|
| 71 | int temp_id=0; |
---|
| 72 | ClientInformation *temp = head_; |
---|
| 73 | while(temp){ |
---|
| 74 | if(temp->head) |
---|
| 75 | continue; |
---|
| 76 | temp_id=temp->getID(); |
---|
| 77 | if(temp_id<min_id) |
---|
| 78 | min_id=temp_id; |
---|
| 79 | temp=temp->next(); |
---|
| 80 | }*/ // probably not very efficient |
---|
| 81 | |
---|
| 82 | std::map<int,int>::iterator it = gameStateUsed.begin(); |
---|
| 83 | while(it!=gameStateUsed.end()){ |
---|
| 84 | if( (*it).second <= 0 ){ |
---|
| 85 | free(gameStateMap[(*it).first]->data); |
---|
| 86 | delete gameStateMap[(*it).first]; |
---|
| 87 | gameStateMap.erase((*it).first); |
---|
| 88 | gameStateUsed.erase(it++); |
---|
[1007] | 89 | }else //as soon as we got a used gamestate break here because we could use newer gamestates in future |
---|
| 90 | break; |
---|
[1005] | 91 | } |
---|
| 92 | } |
---|
[413] | 93 | |
---|
[894] | 94 | GameStateCompressed *GameStateManager::popGameState(int clientID) { |
---|
[777] | 95 | int gID = head_->findClient(clientID)->getGamestateID(); |
---|
[889] | 96 | COUT(4) << "popgamestate: sending gstate id: " << id << "diffed from: " << gID << std::endl; |
---|
[777] | 97 | if(gID!=GAMESTATEID_INITIAL){ |
---|
| 98 | GameState *client = gameStateMap[gID]; |
---|
| 99 | GameState *server = reference; |
---|
| 100 | //head_->findClient(clientID)->setGamestateID(id); |
---|
[894] | 101 | return encode(client, server); |
---|
[777] | 102 | } else { |
---|
| 103 | GameState *server = reference; |
---|
| 104 | //head_->findClient(clientID)->setGamestateID(id); |
---|
[894] | 105 | return encode(server); |
---|
[777] | 106 | // return an undiffed gamestate and set appropriate flags |
---|
| 107 | } |
---|
| 108 | } |
---|
[413] | 109 | |
---|
[777] | 110 | /** |
---|
| 111 | * This function goes through the whole list of synchronisables and |
---|
| 112 | * saves all the synchronisables to a flat "list". |
---|
| 113 | * @return struct of type gamestate containing the size of the whole gamestate and a pointer linking to the flat list |
---|
| 114 | */ |
---|
| 115 | GameState *GameStateManager::getSnapshot(int id) |
---|
| 116 | { |
---|
| 117 | //the size of the gamestate |
---|
| 118 | int totalsize=0; |
---|
| 119 | int memsize=1000; |
---|
| 120 | //the size of one specific synchronisable |
---|
| 121 | int tempsize=0; |
---|
| 122 | // get the start of the Synchronisable list |
---|
| 123 | orxonox::Iterator<Synchronisable> it; |
---|
| 124 | // struct for return value of Synchronisable::getData() |
---|
| 125 | syncData sync; |
---|
[413] | 126 | |
---|
[777] | 127 | GameState *retval=new GameState; //return value |
---|
| 128 | retval->id=id++; |
---|
[889] | 129 | COUT(4) << "producing gamestate with id: " << retval->id << std::endl; |
---|
[777] | 130 | // reserve a little memory and increase it later on |
---|
[889] | 131 | COUT(5) << "mallocing" << std::endl; |
---|
[777] | 132 | retval->data = (unsigned char*)malloc(memsize); |
---|
[889] | 133 | COUT(5) << "malloced" << std::endl; |
---|
[514] | 134 | |
---|
[777] | 135 | // offset of memory functions |
---|
| 136 | int offset=0; |
---|
| 137 | // go through all Synchronisables |
---|
[871] | 138 | for(it = orxonox::ObjectList<Synchronisable>::start(); it; ++it){ |
---|
[777] | 139 | //std::cout << "gamestatemanager: in for loop" << std::endl; |
---|
| 140 | //get size of the synchronisable |
---|
| 141 | tempsize=it->getSize(); |
---|
[889] | 142 | // COUT(5) << "size of temp gamestate: " << tempsize << std::endl; |
---|
[777] | 143 | //COUT(2) << "size of synchronisable: " << tempsize << std::endl; |
---|
| 144 | // add place for data and 3 ints (length,classid,objectid) |
---|
| 145 | totalsize+=tempsize+3*sizeof(int); |
---|
| 146 | //std::cout << "totalsize: " << totalsize << std::endl; |
---|
| 147 | // allocate additional space |
---|
| 148 | if(totalsize+tempsize>memsize){ |
---|
| 149 | if(tempsize<1000){ |
---|
| 150 | retval->data = (unsigned char *)realloc((void *)retval->data, totalsize+1000); |
---|
| 151 | memsize+=1000; |
---|
| 152 | } else { |
---|
| 153 | retval->data = (unsigned char *)realloc((void *)retval->data, totalsize+1000); |
---|
| 154 | memsize+=tempsize+1000; |
---|
| 155 | } |
---|
| 156 | } |
---|
[514] | 157 | |
---|
[777] | 158 | // run Synchronisable::getData with offset and additional place for 3 ints in between (for ids and length) |
---|
| 159 | sync=it->getData((retval->data)+offset+3*sizeof(int)); |
---|
[986] | 160 | memcpy(retval->data+offset, (void *)&sync.length, sizeof(int)); |
---|
| 161 | //*(retval->data+offset)=sync.length; |
---|
| 162 | memcpy(retval->data+offset+sizeof(int), (void *)&sync.objectID, sizeof(int)); |
---|
| 163 | //*(retval->data+offset+sizeof(int))=sync.objectID; |
---|
| 164 | memcpy(retval->data+offset+2*sizeof(int), (void *)&sync.classID, sizeof(int)); |
---|
| 165 | //*(retval->data+offset+2*sizeof(int))=sync.classID; |
---|
[777] | 166 | // increase data pointer |
---|
| 167 | offset+=tempsize+3*sizeof(int); |
---|
[590] | 168 | } |
---|
[889] | 169 | COUT(5) << "Gamestate size: " << totalsize << std::endl; |
---|
[777] | 170 | retval->size=totalsize; |
---|
[984] | 171 | //#### bugfix |
---|
| 172 | retval->diffed = false; |
---|
[777] | 173 | return retval; |
---|
| 174 | } |
---|
[514] | 175 | |
---|
[984] | 176 | //##### ADDED FOR TESTING PURPOSE ##### |
---|
| 177 | GameStateCompressed* GameStateManager::testCompress( GameState* g ) { |
---|
| 178 | return compress_( g ); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | GameState* GameStateManager::testDiff( GameState* a, GameState* b ) { |
---|
| 182 | return diff( a, b ); |
---|
| 183 | } |
---|
| 184 | //##### END TESTING PURPOSE ##### |
---|
| 185 | |
---|
[891] | 186 | GameStateCompressed *GameStateManager::encode(GameState *a, GameState *b) { |
---|
[777] | 187 | //GameState r = diff(a,b); |
---|
| 188 | //r.diffed = true; |
---|
[891] | 189 | GameState *r = b; |
---|
| 190 | r->diffed = false; |
---|
| 191 | return compress_(r); |
---|
[332] | 192 | } |
---|
[247] | 193 | |
---|
[891] | 194 | GameStateCompressed *GameStateManager::encode(GameState *a) { |
---|
[777] | 195 | a->diffed=false; |
---|
| 196 | return compress_(a); |
---|
| 197 | } |
---|
[247] | 198 | |
---|
[891] | 199 | GameState *GameStateManager::diff(GameState *a, GameState *b) { |
---|
[777] | 200 | unsigned char *ap = a->data, *bp = b->data; |
---|
| 201 | int of=0; // pointers offset |
---|
| 202 | int dest_length=0; |
---|
| 203 | if(a->size>=b->size) |
---|
| 204 | dest_length=a->size; |
---|
| 205 | else |
---|
| 206 | dest_length=b->size; |
---|
| 207 | unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char)); |
---|
| 208 | while(of<a->size && of<b->size){ |
---|
| 209 | *(dp+of)=*(ap+of)^*(bp+of); // do the xor |
---|
| 210 | ++of; |
---|
| 211 | } |
---|
| 212 | if(a->size!=b->size){ // do we have to fill up ? |
---|
| 213 | unsigned char n=0; |
---|
| 214 | if(a->size<b->size){ |
---|
| 215 | while(of<dest_length){ |
---|
| 216 | *(dp+of)=n^*(bp+of); |
---|
| 217 | of++; |
---|
| 218 | } |
---|
| 219 | } else{ |
---|
| 220 | while(of<dest_length){ |
---|
| 221 | *(dp+of)=*(ap+of)^n; |
---|
| 222 | of++; |
---|
| 223 | } |
---|
[385] | 224 | } |
---|
| 225 | } |
---|
[891] | 226 | |
---|
| 227 | GameState *r = new GameState; |
---|
| 228 | r->id = b->id; |
---|
| 229 | r->size = dest_length; |
---|
| 230 | r->diffed = true; |
---|
[986] | 231 | r->base_id = a->id; |
---|
[891] | 232 | r->data = dp; |
---|
[777] | 233 | return r; |
---|
[385] | 234 | } |
---|
[332] | 235 | |
---|
[891] | 236 | GameStateCompressed *GameStateManager::compress_(GameState *a) { |
---|
[889] | 237 | COUT(5) << "compressing gamestate" << std::endl; |
---|
[777] | 238 | int size = a->size; |
---|
| 239 | uLongf buffer = (uLongf)((a->size + 12)*1.01)+1; |
---|
| 240 | unsigned char* dest = (unsigned char*)malloc( buffer ); |
---|
| 241 | int retval; |
---|
| 242 | //std::cout << "before ziped " << buffer << std::endl; |
---|
| 243 | retval = compress( dest, &buffer, a->data, (uLong)size ); |
---|
| 244 | //std::cout << "after ziped " << buffer << std::endl; |
---|
[514] | 245 | |
---|
[777] | 246 | switch ( retval ) { |
---|
[891] | 247 | case Z_OK: COUT(5) << "successfully compressed" << std::endl; break; |
---|
| 248 | case Z_MEM_ERROR: COUT(1) << "not enough memory available in gamestate.compress" << std::endl; |
---|
| 249 | return NULL; |
---|
| 250 | case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer in gamestate.compress" << std::endl; |
---|
| 251 | return NULL; |
---|
| 252 | case Z_DATA_ERROR: COUT(2) << "decompress: data corrupted in gamestate.compress" << std::endl; |
---|
| 253 | return NULL; |
---|
[777] | 254 | } |
---|
[514] | 255 | |
---|
[891] | 256 | GameStateCompressed *compressedGamestate = new GameStateCompressed; |
---|
| 257 | compressedGamestate->compsize = buffer; |
---|
[889] | 258 | // std::cout << "compressedGamestate.compsize = buffer; " << buffer << std::endl; |
---|
[891] | 259 | compressedGamestate->normsize = size; |
---|
[889] | 260 | // std::cout << "compressedGamestate.normsize = size; " << size << std::endl; |
---|
[891] | 261 | compressedGamestate->id = a->id; |
---|
| 262 | compressedGamestate->data = dest; |
---|
| 263 | compressedGamestate->diffed = a->diffed; |
---|
[986] | 264 | compressedGamestate->base_id = a->base_id; |
---|
[514] | 265 | |
---|
[777] | 266 | return compressedGamestate; |
---|
| 267 | } |
---|
[385] | 268 | |
---|
[777] | 269 | void GameStateManager::ackGameState(int clientID, int gamestateID) { |
---|
| 270 | ClientInformation *temp = head_->findClient(clientID); |
---|
| 271 | int curid = temp->getID(); |
---|
| 272 | // decrease usage of gamestate and save it |
---|
| 273 | deleteUnusedGameState(curid); |
---|
| 274 | //increase gamestateused |
---|
| 275 | ++gameStateUsed.find(gamestateID)->second; |
---|
| 276 | temp->setGamestateID(gamestateID); |
---|
| 277 | /* |
---|
| 278 | GameState *old = clientGameState[clientID]; |
---|
| 279 | deleteUnusedGameState(old); |
---|
| 280 | clientGameState[clientID]=idGameState[gamestateID];*/ |
---|
| 281 | } |
---|
[385] | 282 | |
---|
[777] | 283 | bool GameStateManager::deleteUnusedGameState(int gamestateID) { |
---|
| 284 | int used = --(gameStateUsed.find(gamestateID)->second); |
---|
| 285 | if(id-gamestateID>KEEP_GAMESTATES && used==0){ |
---|
| 286 | // delete gamestate |
---|
| 287 | delete gameStateMap.find(gamestateID)->second; |
---|
| 288 | gameStateMap.erase(gamestateID); |
---|
| 289 | return true; |
---|
| 290 | } |
---|
| 291 | return false; |
---|
[422] | 292 | } |
---|
[413] | 293 | |
---|
[385] | 294 | } |
---|