Changeset 1021 for code/trunk/src/network/GameStateClient.cc
- Timestamp:
- Apr 10, 2008, 5:03:34 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/network/GameStateClient.cc
r871 r1021 40 40 41 41 bool GameStateClient::pushGameState(GameStateCompressed *compstate) { 42 GameState *gs; 42 43 if(compstate->diffed) 43 return loadSnapshot(decode(reference, *compstate));44 gs = decode(reference, compstate); 44 45 else 45 return loadSnapshot(decode(*compstate)); 46 gs = decode(compstate); 47 if(gs) 48 return loadSnapshot(gs); 49 COUT(4) << "could not use gamestate sent by server" << std::endl; 50 return false; 46 51 } 47 52 … … 61 66 * @param state a GameState struct containing the size of the gamestate and a pointer linking to a flat list (returned by getSnapshot) 62 67 */ 63 bool GameStateClient::loadSnapshot(GameState state) {64 unsigned char *data=state .data;65 std::cout << "loadSnapshot: loading gs: " << state.id << std::endl;68 bool GameStateClient::loadSnapshot(GameState *state) { 69 unsigned char *data=state->data; 70 COUT(4) << "loadSnapshot: loading gs: " << state->id << std::endl; 66 71 // get the start of the Synchronisable list 67 72 orxonox::Iterator<Synchronisable> it=orxonox::ObjectList<Synchronisable>::start(); 68 73 syncData sync; 69 74 // loop as long as we have some data ;) 70 while(data < state .data+state.size){75 while(data < state->data+state->size){ 71 76 // prepare the syncData struct 72 77 sync.length = (int)*data; … … 82 87 // bad luck ;) 83 88 // delete the synchronisable (obviously seems to be deleted on the server) 84 while(it && it->objectID!=sync.objectID) {89 while(it && it->objectID!=sync.objectID) 85 90 removeObject(it); 86 } 87 if(it==0){ 88 std::cout << "classid: " << sync.classID << ", name: " << ID(sync.classID)->getName() << std::endl; 89 Synchronisable *no = (Synchronisable*)(ID(sync.classID)->fabricate()); 91 92 93 if(!it){ 94 COUT(5) << "classid: " << sync.classID << ", name: " << ID((unsigned int) sync.classID)->getName() << std::endl; 95 Synchronisable *no = (Synchronisable*)(ID((unsigned int) sync.classID)->fabricate()); 90 96 no->objectID=sync.objectID; 91 97 no->classID=sync.classID; … … 93 99 // update data and create object/entity... 94 100 if( !no->updateData(sync) && !no->create() ) 95 COUT( 0) << "We couldn't create/update the object: " << sync.objectID << std::endl;101 COUT(1) << "We couldn't create/update the object: " << sync.objectID << std::endl; 96 102 ++it; 97 103 } … … 99 105 // we have our object 100 106 if(! it->updateData(sync)) 101 std::cout<< "We couldn't update objectID: " \107 COUT(1) << "We couldn't update objectID: " \ 102 108 << sync.objectID << "; classID: " << sync.classID << std::endl; 103 109 } … … 108 114 } 109 115 110 GameState GameStateClient::diff(GameState a, GameStateb) {111 unsigned char *ap = a .data, *bp = b.data;116 GameState *GameStateClient::undiff(GameState *a, GameState *b) { 117 unsigned char *ap = a->data, *bp = b->data; 112 118 int of=0; // pointers offset 113 119 int dest_length=0; 114 if(a .size>=b.size)115 dest_length=a .size;120 if(a->size>=b->size) 121 dest_length=a->size; 116 122 else 117 dest_length=b .size;123 dest_length=b->size; 118 124 unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char)); 119 while(of<a .size && of<b.size){125 while(of<a->size && of<b->size){ 120 126 *(dp+of)=*(ap+of)^*(bp+of); // do the xor 121 127 ++of; 122 128 } 123 if(a .size!=b.size){ // do we have to fill up ?129 if(a->size!=b->size){ // do we have to fill up ? 124 130 unsigned char n=0; 125 if(a .size<b.size){131 if(a->size<b->size){ 126 132 while(of<dest_length){ 127 133 *(dp+of)=n^*(bp+of); … … 137 143 // should be finished now 138 144 // FIXME: is it true or false now? (struct has changed, producing warnings) 139 GameState r = {b.id, dest_length, true, dp}; 145 GameState *r = new GameState; 146 r->id = b->id; 147 r->size = dest_length; 148 r->diffed = false; 149 r->data = dp; 140 150 return r; 141 151 } 142 152 143 GameState GameStateClient::decompress(GameStateCompresseda) {144 int normsize = a .normsize;145 int compsize = a .compsize;153 GameState *GameStateClient::decompress(GameStateCompressed *a) { 154 int normsize = a->normsize; 155 int compsize = a->compsize; 146 156 int bufsize; 147 157 if(normsize < compsize) … … 154 164 //std::cout << "gamestateclient" << std::endl; 155 165 //std::cout << "normsize " << a.normsize << " compsize " << a.compsize << " " << bufsize << std::endl; 156 retval = uncompress( dest, &length, a .data, (uLong)compsize );166 retval = uncompress( dest, &length, a->data, (uLong)compsize ); 157 167 //std::cout << "length " << length << std::endl; 158 168 switch ( retval ) { 159 case Z_OK: std::cout<< "successfully decompressed" << std::endl; break;160 case Z_MEM_ERROR: std::cout << "not enough memory available" << std::endl; break;161 case Z_BUF_ERROR: std::cout << "not enough memory available in the buffer" << std::endl; break;162 case Z_DATA_ERROR: std::cout << "data corrupted" << std::endl; break;169 case Z_OK: COUT(4) << "successfully decompressed" << std::endl; break; 170 case Z_MEM_ERROR: COUT(1) << "not enough memory available" << std::endl; return NULL; 171 case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer" << std::endl; return NULL; 172 case Z_DATA_ERROR: COUT(2) << "data corrupted" << std::endl; return NULL; 163 173 } 164 174 165 GameState gamestate; 166 gamestate.id = a.id; 167 gamestate.size = normsize; 168 gamestate.data = dest; 169 gamestate.diffed = a.diffed; 175 GameState *gamestate = new GameState; 176 gamestate->id = a->id; 177 gamestate->size = normsize; 178 gamestate->data = dest; 179 gamestate->diffed = a->diffed; 180 181 delete a->data; //delete compressed data 182 delete a; //we do not need the old (struct) gamestate anymore 170 183 171 184 return gamestate; 172 185 } 173 186 174 GameState GameStateClient::decode(GameState a, GameStateCompressedx) {175 GameState t = decompress(x);176 return diff(a, t);187 GameState *GameStateClient::decode(GameState *a, GameStateCompressed *x) { 188 GameState *t = decompress(x); 189 return undiff(a, t); 177 190 } 178 191 179 GameState GameStateClient::decode(GameStateCompressedx) {180 GameState t = decompress(x);192 GameState *GameStateClient::decode(GameStateCompressed *x) { 193 GameState *t = decompress(x); 181 194 return t; 182 195 }
Note: See TracChangeset
for help on using the changeset viewer.