Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 5, 2007, 5:59:25 PM (16 years ago)
Author:
scheusso
Message:

still errors

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/FICN/src/network/GameStateManager.cc

    r407 r413  
    1616GameStateManager::GameStateManager()
    1717{
     18  id=0;
    1819}
    1920
     
    2223}
    2324
     25void GameStateManager::update(){
     26  reference = getSnapshot(id++);
     27  return;
     28}
     29
     30GameStateCompressed GameStateManager::popGameState(int clientID){
     31  GameState *client = clientGameState[clientID]; 
     32  GameState *server = &reference;
     33  return encode(client, server);
     34}
     35
     36
     37
    2438/**
    2539 * This function goes through the whole list of synchronisables and
     
    2741 * @return struct of type gamestate containing the size of the whole gamestate and a pointer linking to the flat list
    2842 */
    29 GameStateCompressed GameStateManager::getSnapshot(int id)
     43GameState GameStateManager::getSnapshot(int id)
    3044{
    3145  //the size of the gamestate
     
    6377  }
    6478  retval.size=totalsize;
    65   return compress(retval);
     79  return retval;
    6680}
    6781
    68 /**
    69  * This function loads a Snapshort of the gamestate into the universe
    70  * @param state a GameState struct containing the size of the gamestate and a pointer linking to a flat list (returned by getSnapshot)
    71  */
    72 bool GameStateManager::loadSnapshot(GameStateCompressed compstate)
    73 {
    74   GameState state = decompress(compstate);
    75   unsigned char *data=state.data;
    76   // get the start of the Synchronisable list
    77   orxonox::Iterator<Synchronisable> it=orxonox::ObjectList<Synchronisable>::start();
    78   syncData sync;
    79   // loop as long as we have some data ;)
    80   while(data < state.data+state.size){
    81     // prepare the syncData struct
    82     sync.length = *(int *)data;
    83     data+=sizeof(int);
    84     sync.objectID = *(int *)data;
    85     data+=sizeof(int);
    86     sync.classID = *(int *)data;
    87     data+=sizeof(int);
    88     sync.data = data;
    89     data+=sync.length;
    90    
    91     if(it->objectID!=sync.objectID){
    92       // bad luck ;)
    93       // delete the synchronisable (obviously seems to be deleted on the server)
    94       while(it != 0 && it->objectID!=sync.objectID){
    95         removeObject(it);
    96       }
    97       if(it==0){  // add the new object
    98         // =================== factory command to add object
    99         // can we be sure the object really was added?
    100         it=orxonox::ObjectList<Synchronisable>::end();
    101         it->objectID=sync.objectID;
    102         it->classID=sync.classID;
    103       }
    104     } else {
    105       // we have our object
    106       if(! it->updateData(sync))
    107         std::cout << "We couldn't update objectID: " \
    108           << sync.objectID << "; classID: " << sync.classID << std::endl;
    109     }
    110    
    111   }
    112  
    113   return true;
     82
     83
     84GameStateCompressed GameStateManager::encode(GameState *a, GameState *b){
     85  GameState r = diff(a,b);
     86  return compress_(r);
    11487}
    11588
    116 /**
    117  * This function removes a Synchronisable out of the universe
    118  * @param it iterator of the list pointing to the object
    119  * @return iterator pointing to the next object in the list
    120  */
    121 void GameStateManager::removeObject(orxonox::Iterator<Synchronisable> &it){
    122   orxonox::Iterator<Synchronisable> temp=it;
    123   ++it;
    124   delete  *temp;
    125 //   return it;
    126 }
    12789
    128 GameStateCompressed GameStateManager::encode(GameState a, GameState b){
    129   GameState r = diff(a,b);
    130   return compress(r);
    131 }
    132 
    133 GameState GameStateManager::decode(GameState a, GameStateCompressed x){
    134   GameState t = decompress(x);
    135   return diff(a, t);
    136 }
    137 
    138 GameState GameStateManager::diff(GameState a, GameState b){
    139   unsigned char *ap = a.data, *bp = b.data;
     90GameState GameStateManager::diff(GameState *a, GameState *b){
     91  unsigned char *ap = a->data, *bp = b->data;
    14092  int of=0; // pointers offset
    14193  int dest_length=0;
    142   if(a.size>=b.size)
    143     dest_length=a.size;
     94  if(a->size>=b->size)
     95    dest_length=a->size;
    14496  else
    145     dest_length=b.size;
     97    dest_length=b->size;
    14698  unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char));
    147   while(of<a.size && of<b.size){
     99  while(of<a->size && of<b->size){
    148100    *(dp+of)=*(ap+of)^*(bp+of); // do the xor
    149101    ++of;
    150102  }
    151   if(a.size!=b.size){ // do we have to fill up ?
     103  if(a->size!=b->size){ // do we have to fill up ?
    152104    unsigned char n=0;
    153     if(a.size<b.size){
     105    if(a->size<b->size){
    154106      while(of<dest_length){
    155107        *(dp+of)=n^*(bp+of);
     
    164116  }
    165117  // should be finished now
    166   GameState r = {b.id, dest_length, dp};
     118  GameState r = {b->id, dest_length, dp};
    167119  return r;
    168120}
    169121
    170 GameStateCompressed GameStateManager::compress(GameState a) {
     122GameStateCompressed GameStateManager::compress_(GameState a) {
    171123  int size = a.size;
    172124  uLongf buffer = (uLongf)((a.size + 12)*1.01)+1;
     
    176128 
    177129  switch ( retval ) {
    178   case Z_OK: cout << "successfully compressed" << endl; break;
    179   case Z_MEM_ERROR: cout << "not enough memory available" << endl; break;
    180   case Z_BUF_ERROR: cout << "not enough memory available in the buffer" << endl; break;
    181   case Z_DATA_ERROR: cout << "data corrupted" << endl; break;
     130  case Z_OK: std::cout << "successfully compressed" << std::endl; break;
     131  case Z_MEM_ERROR: std::cout << "not enough memory available" << std::endl; break;
     132  case Z_BUF_ERROR: std::cout << "not enough memory available in the buffer" << std::endl; break;
     133  case Z_DATA_ERROR: std::cout << "data corrupted" << std::endl; break;
    182134  }
    183135 
    184   GameStateCompressed compressedGamestate = new GameStateCompressed;
     136  GameStateCompressed compressedGamestate;
    185137  compressedGamestate.compsize = buffer;
    186138  compressedGamestate.normsize = size;
     
    191143}
    192144
    193 GameState GameStateManager::decompress(GameStateCompressed a){
    194   int normsize = a.normsize;
    195   int compsize = a.compsize;
    196   unsigned char* dest = (unsigned char*)malloc( normsize );
    197   int retval;
    198   retval = uncompress( dest, &normsize, a.data, compsize );
    199  
    200   switch ( retval ) {
    201   case Z_OK: cout << "successfully compressed" << endl; break;
    202   case Z_MEM_ERROR: cout << "not enough memory available" << endl; break;
    203   case Z_BUF_ERROR: cout << "not enough memory available in the buffer" << endl; break;
    204   case Z_DATA_ERROR: cout << "data corrupted" << endl; break;
    205   }
    206  
    207   GameState gamestate = new GameState;
    208   gamestate.id = a.id;
    209   gamestate.size = normsize;
    210   gamestate.data = dest;
    211  
    212   return gamestate;
    213 }
     145
    214146
    215147}
Note: See TracChangeset for help on using the changeset viewer.