Changeset 413 for code/branches/FICN/src/network/GameStateManager.cc
- Timestamp:
- Dec 5, 2007, 5:59:25 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/FICN/src/network/GameStateManager.cc
r407 r413 16 16 GameStateManager::GameStateManager() 17 17 { 18 id=0; 18 19 } 19 20 … … 22 23 } 23 24 25 void GameStateManager::update(){ 26 reference = getSnapshot(id++); 27 return; 28 } 29 30 GameStateCompressed GameStateManager::popGameState(int clientID){ 31 GameState *client = clientGameState[clientID]; 32 GameState *server = &reference; 33 return encode(client, server); 34 } 35 36 37 24 38 /** 25 39 * This function goes through the whole list of synchronisables and … … 27 41 * @return struct of type gamestate containing the size of the whole gamestate and a pointer linking to the flat list 28 42 */ 29 GameState CompressedGameStateManager::getSnapshot(int id)43 GameState GameStateManager::getSnapshot(int id) 30 44 { 31 45 //the size of the gamestate … … 63 77 } 64 78 retval.size=totalsize; 65 return compress(retval);79 return retval; 66 80 } 67 81 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 84 GameStateCompressed GameStateManager::encode(GameState *a, GameState *b){ 85 GameState r = diff(a,b); 86 return compress_(r); 114 87 } 115 88 116 /**117 * This function removes a Synchronisable out of the universe118 * @param it iterator of the list pointing to the object119 * @return iterator pointing to the next object in the list120 */121 void GameStateManager::removeObject(orxonox::Iterator<Synchronisable> &it){122 orxonox::Iterator<Synchronisable> temp=it;123 ++it;124 delete *temp;125 // return it;126 }127 89 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; 90 GameState GameStateManager::diff(GameState *a, GameState *b){ 91 unsigned char *ap = a->data, *bp = b->data; 140 92 int of=0; // pointers offset 141 93 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; 144 96 else 145 dest_length=b .size;97 dest_length=b->size; 146 98 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){ 148 100 *(dp+of)=*(ap+of)^*(bp+of); // do the xor 149 101 ++of; 150 102 } 151 if(a .size!=b.size){ // do we have to fill up ?103 if(a->size!=b->size){ // do we have to fill up ? 152 104 unsigned char n=0; 153 if(a .size<b.size){105 if(a->size<b->size){ 154 106 while(of<dest_length){ 155 107 *(dp+of)=n^*(bp+of); … … 164 116 } 165 117 // should be finished now 166 GameState r = {b .id, dest_length, dp};118 GameState r = {b->id, dest_length, dp}; 167 119 return r; 168 120 } 169 121 170 GameStateCompressed GameStateManager::compress (GameState a) {122 GameStateCompressed GameStateManager::compress_(GameState a) { 171 123 int size = a.size; 172 124 uLongf buffer = (uLongf)((a.size + 12)*1.01)+1; … … 176 128 177 129 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; 182 134 } 183 135 184 GameStateCompressed compressedGamestate = new GameStateCompressed;136 GameStateCompressed compressedGamestate; 185 137 compressedGamestate.compsize = buffer; 186 138 compressedGamestate.normsize = size; … … 191 143 } 192 144 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 214 146 215 147 }
Note: See TracChangeset
for help on using the changeset viewer.