Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/network/GameStateClient.cc @ 417

Last change on this file since 417 was 416, checked in by scheusso, 17 years ago

blabla

File size: 3.7 KB
Line 
1#include "GameStateClient.h"
2
3namespace network {
4
5GameStateClient::GameStateClient()
6{
7}
8
9
10GameStateClient::~GameStateClient()
11{
12}
13
14bool GameStateClient::pushGameState(GameStateCompressed compstate){
15  return loadSnapshot(decode(reference, compstate));
16}
17
18
19/**
20 * This function removes a Synchronisable out of the universe
21 * @param it iterator of the list pointing to the object
22 * @return iterator pointing to the next object in the list
23 */
24void GameStateClient::removeObject(orxonox::Iterator<Synchronisable> &it){
25  orxonox::Iterator<Synchronisable> temp=it;
26  ++it;
27  delete  *temp;
28}
29
30/**
31 * This function loads a Snapshort of the gamestate into the universe
32 * @param state a GameState struct containing the size of the gamestate and a pointer linking to a flat list (returned by getSnapshot)
33 */
34bool GameStateClient::loadSnapshot(GameState state)
35{
36  unsigned char *data=state.data;
37  // get the start of the Synchronisable list
38  orxonox::Iterator<Synchronisable> it=orxonox::ObjectList<Synchronisable>::start();
39  syncData sync;
40  // loop as long as we have some data ;)
41  while(data < state.data+state.size){
42    // prepare the syncData struct
43    sync.length = *(int *)data;
44    data+=sizeof(int);
45    sync.objectID = *(int *)data;
46    data+=sizeof(int);
47    sync.classID = *(int *)data;
48    data+=sizeof(int);
49    sync.data = data;
50    data+=sync.length;
51   
52    if(it->objectID!=sync.objectID){
53      // bad luck ;)
54      // delete the synchronisable (obviously seems to be deleted on the server)
55      while(it != 0 && it->objectID!=sync.objectID){
56        removeObject(it);
57      }
58      if(it==0){  // add the new object
59        // =================== factory command to add object
60        // can we be sure the object really was added?
61        it=orxonox::ObjectList<Synchronisable>::end();
62        it->objectID=sync.objectID;
63        it->classID=sync.classID;
64      }
65    } else {
66      // we have our object
67      if(! it->updateData(sync))
68        std::cout << "We couldn't update objectID: " \
69            << sync.objectID << "; classID: " << sync.classID << std::endl;
70    }
71   
72  }
73 
74  return true;
75}
76
77GameState GameStateClient::diff(GameState a, GameState b){
78  unsigned char *ap = a.data, *bp = b.data;
79  int of=0; // pointers offset
80  int dest_length=0;
81  if(a.size>=b.size)
82    dest_length=a.size;
83  else
84    dest_length=b.size;
85  unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char));
86  while(of<a.size && of<b.size){
87    *(dp+of)=*(ap+of)^*(bp+of); // do the xor
88    ++of;
89  }
90  if(a.size!=b.size){ // do we have to fill up ?
91    unsigned char n=0;
92    if(a.size<b.size){
93      while(of<dest_length){
94        *(dp+of)=n^*(bp+of);
95        of++;
96      }
97    } else{
98      while(of<dest_length){
99        *(dp+of)=*(ap+of)^n;
100        of++;
101      }
102    }
103  }
104  // should be finished now
105  GameState r = {b.id, dest_length, dp};
106  return r;
107}
108
109
110GameState GameStateClient::decompress(GameStateCompressed a){
111  int normsize = a.normsize;
112  int compsize = a.compsize;
113  unsigned char* dest = (unsigned char*)malloc( normsize );
114  int retval;
115  uLongf length=normsize;
116  retval = uncompress( dest, &length, a.data, (uLong)compsize );
117 
118  switch ( retval ) {
119    case Z_OK: std::cout << "successfully compressed" << std::endl; break;
120    case Z_MEM_ERROR: std::cout << "not enough memory available" << std::endl; break;
121    case Z_BUF_ERROR: std::cout << "not enough memory available in the buffer" << std::endl; break;
122    case Z_DATA_ERROR: std::cout << "data corrupted" << std::endl; break;
123  }
124 
125  GameState gamestate;
126  gamestate.id = a.id;
127  gamestate.size = normsize;
128  gamestate.data = dest;
129 
130  return gamestate;
131}
132
133
134GameState GameStateClient::decode(GameState a, GameStateCompressed x){
135  GameState t = decompress(x);
136  return diff(a, t);
137}
138
139
140
141}
Note: See TracBrowser for help on using the repository browser.