Changeset 1502 for code/trunk/src/network/GameStateClient.cc
- Timestamp:
- Jun 1, 2008, 3:54:20 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/network/GameStateClient.cc
r1360 r1502 21 21 * 22 22 * Author: 23 * ...23 * Oliver Scheuss 24 24 * Co-authors: 25 * ...25 * Dumeni Manatschal 26 26 * 27 27 */ … … 35 35 #include "Synchronisable.h" 36 36 37 #define GAMESTATEID_INITIAL -138 37 39 38 namespace network … … 43 42 int id; 44 43 }; 45 44 46 45 GameStateClient::GameStateClient() { 47 46 COUT(5) << "this: " << this << std::endl; 48 47 last_diff_=0; 49 48 last_gamestate_=GAMESTATEID_INITIAL-1; 49 tempGameState_=NULL; 50 myShip_=NULL; 50 51 } 51 52 … … 95 96 return false; 96 97 } 97 98 98 99 GameStateCompressed *GameStateClient::popPartialGameState(){ 99 100 GameState *gs = getPartialSnapshot(); 101 if(!gs) 102 return NULL; 100 103 GameStateCompressed *cgs = compress_(gs); 101 104 delete[] gs->data; … … 103 106 return cgs; 104 107 } 105 108 109 void GameStateClient::addGameState(GameStateCompressed *gs){ 110 if(tempGameState_!=NULL){ 111 //delete the obsolete gamestate 112 if(tempGameState_->id>gs->id) 113 return; 114 delete[] tempGameState_->data; 115 delete tempGameState_; 116 } 117 tempGameState_=gs; 118 } 119 int GameStateClient::processGameState(){ 120 if(tempGameState_==NULL) 121 return 0; 122 int id=tempGameState_->id; 123 bool b = saveShipCache(); 124 if(pushGameState(tempGameState_)){ 125 if(b) 126 loadShipCache(); 127 return id; 128 } 129 else 130 return GAMESTATEID_INITIAL; 131 } 132 106 133 107 134 /** … … 151 178 orxonox::Identifier* id = ID((unsigned int)sync.classID); 152 179 if(!id){ 153 COUT( 4) << "We could not identify a new object; classid: " << sync.classID<< std::endl;154 return false; 180 COUT(3) << "We could not identify a new object; classid: " << sync.classID << " uint: " << (unsigned int)sync.classID << " objectID: " << sync.objectID << " size: " << sync.length << std::endl; 181 return false; // most probably the gamestate is corrupted 155 182 } 156 183 Synchronisable *no = dynamic_cast<Synchronisable *>(id->fabricate()); … … 168 195 } 169 196 if( !no->create() ) 197 { 170 198 COUT(1) << "We couldn't manifest (create() ) the object: " << sync.objectID << std::endl; 199 } 171 200 it=orxonox::ObjectList<Synchronisable>::end(); 172 201 } … … 174 203 // we have our object 175 204 if(! it->updateData(sync)) 205 { 176 206 COUT(1) << "We couldn't update objectID: " \ 177 207 << sync.objectID << "; classID: " << sync.classID << std::endl; 208 } 178 209 } 179 210 ++it; … … 213 244 } 214 245 //retval->data = (unsigned char*)malloc(size); 246 if(size==0) 247 return NULL; 215 248 retval->data = new unsigned char[size]; 216 249 if(!retval->data){ … … 245 278 return retval; 246 279 } 247 248 280 281 249 282 GameState *GameStateClient::undiff(GameState *old, GameState *diff) { 250 283 if(!old || !diff) … … 253 286 int of=0; // pointers offset 254 287 int dest_length=0; 255 if(old->size>=diff->size)288 /*if(old->size>=diff->size) 256 289 dest_length=old->size; 257 else 290 else*/ 258 291 dest_length=diff->size; 259 292 // unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char)); 293 if(dest_length==0) 294 return NULL; 260 295 unsigned char *dp = new unsigned char[dest_length*sizeof(unsigned char)]; 261 296 while(of<old->size && of<diff->size){ … … 270 305 of++; 271 306 } 272 } else{307 } /*else{ 273 308 while(of<dest_length){ 274 309 *(dp+of)=*(ap+of)^n; 275 310 of++; 276 311 } 277 } 312 }*/ 278 313 } 279 314 // should be finished now … … 297 332 298 333 uLongf buffer = (uLongf)((a->size + 12)*1.01)+1; 334 if(buffer==0) 335 return NULL; 299 336 unsigned char *dest = new unsigned char[buffer]; 300 337 int retval; … … 303 340 switch ( retval ) { 304 341 case Z_OK: COUT(5) << "G.St.Cl: compress: successfully compressed" << std::endl; break; 305 case Z_MEM_ERROR: COUT(1) << "G.St.Cl: compress: not enough memory available in gamestate.compress" << std::endl; 342 case Z_MEM_ERROR: COUT(1) << "G.St.Cl: compress: not enough memory available in gamestate.compress" << std::endl; 306 343 return NULL; 307 344 case Z_BUF_ERROR: COUT(2) << "G.St.Cl: compress: not enough memory available in the buffer in gamestate.compress" << std::endl; … … 321 358 return compressedGamestate; 322 359 } 323 324 360 361 325 362 GameState *GameStateClient::decompress(GameStateCompressed *a) { 326 363 //COUT(4) << "GameStateClient: uncompressing gamestate. id: " << a->id << ", baseid: " << a->base_id << ", normsize: " << a->normsize << ", compsize: " << a->compsize << std::endl; … … 333 370 bufsize = normsize; 334 371 // unsigned char* dest = (unsigned char*)malloc( bufsize ); 372 if(bufsize==0) 373 return NULL; 335 374 unsigned char *dest = new unsigned char[bufsize]; 336 375 int retval; … … 376 415 return t; 377 416 } 378 417 379 418 void GameStateClient::cleanup(){ 380 419 std::map<int, GameState*>::iterator temp, it = gameStateMap.begin(); … … 388 427 gameStateMap.erase(temp); 389 428 } 429 tempGameState_=NULL; 390 430 } 391 431 … … 397 437 } 398 438 COUT(4) << std::endl; 399 400 } 401 402 439 440 } 441 442 bool GameStateClient::saveShipCache(){ 443 if(myShip_==NULL) 444 myShip_ = orxonox::SpaceShip::getLocalShip(); 445 if(myShip_){ 446 // unsigned char *data = new unsigned char[myShip_->getSize()]; 447 int size=myShip_->getSize(0x3); 448 if(size==0) 449 return false; 450 unsigned char *data = new unsigned char [size]; 451 shipCache_ = myShip_->getData(data, 0x1); 452 return true; 453 }else 454 return false; 455 } 456 457 bool GameStateClient::loadShipCache(){ 458 if(myShip_){ 459 myShip_->updateData(shipCache_, 0x2); 460 if(shipCache_.data){ 461 delete[] shipCache_.data; 462 } 463 return true; 464 }else 465 return false; 466 } 467 403 468 //##### ADDED FOR TESTING PURPOSE ##### 404 469 GameState* GameStateClient::testDecompress( GameStateCompressed* gc ) { 405 470 return decompress( gc ); 406 471 } 407 472 408 473 GameState* GameStateClient::testUndiff( GameState* g_old, GameState* g_diffed ) { 409 474 return undiff( g_old, g_diffed ); 410 475 } 411 476 //##### ADDED FOR TESTING PURPOSE ##### 412 413 477 478 414 479 } 415 480
Note: See TracChangeset
for help on using the changeset viewer.