Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 564 was 514, checked in by nicolasc, 17 years ago

added copyright notice

File size: 4.8 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      ...
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28#include "GameStateClient.h"
29
30namespace network {
31
32GameStateClient::GameStateClient()
33{
34}
35
36
37GameStateClient::~GameStateClient()
38{
39}
40
41bool GameStateClient::pushGameState(GameStateCompressed compstate){
42  if(compstate.diffed)
43    return loadSnapshot(decode(reference, compstate));
44  else
45    return loadSnapshot(decode(compstate));
46}
47
48
49/**
50 * This function removes a Synchronisable out of the universe
51 * @param it iterator of the list pointing to the object
52 * @return iterator pointing to the next object in the list
53 */
54void GameStateClient::removeObject(orxonox::Iterator<Synchronisable> &it){
55  orxonox::Iterator<Synchronisable> temp=it;
56  ++it;
57  delete  *temp;
58}
59
60/**
61 * This function loads a Snapshort of the gamestate into the universe
62 * @param state a GameState struct containing the size of the gamestate and a pointer linking to a flat list (returned by getSnapshot)
63 */
64bool GameStateClient::loadSnapshot(GameState state)
65{
66  unsigned char *data=state.data;
67  // get the start of the Synchronisable list
68  orxonox::Iterator<Synchronisable> it=orxonox::ObjectList<Synchronisable>::start();
69  syncData sync;
70  // loop as long as we have some data ;)
71  while(data < state.data+state.size){
72    // prepare the syncData struct
73    sync.length = *(int *)data;
74    data+=sizeof(int);
75    sync.objectID = *(int *)data;
76    data+=sizeof(int);
77    sync.classID = *(int *)data;
78    data+=sizeof(int);
79    sync.data = data;
80    data+=sync.length;
81
82    if(it->objectID!=sync.objectID){
83      // bad luck ;)
84      // delete the synchronisable (obviously seems to be deleted on the server)
85      while(it != 0 && it->objectID!=sync.objectID){
86        removeObject(it);
87      }
88      if(it==0){
89        orxonox::BaseObject *no = ID(sync.classID)->fabricate();
90        ((Synchronisable *)no)->objectID=sync.objectID;
91        ((Synchronisable *)no)->classID=sync.classID;
92        it=orxonox::ObjectList<Synchronisable>::end();
93      }
94    } else {
95      // we have our object
96      if(! it->updateData(sync))
97        std::cout << "We couldn't update objectID: " \
98            << sync.objectID << "; classID: " << sync.classID << std::endl;
99    }
100    ++it;
101  }
102
103  return true;
104}
105
106GameState GameStateClient::diff(GameState a, GameState b){
107  unsigned char *ap = a.data, *bp = b.data;
108  int of=0; // pointers offset
109  int dest_length=0;
110  if(a.size>=b.size)
111    dest_length=a.size;
112  else
113    dest_length=b.size;
114  unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char));
115  while(of<a.size && of<b.size){
116    *(dp+of)=*(ap+of)^*(bp+of); // do the xor
117    ++of;
118  }
119  if(a.size!=b.size){ // do we have to fill up ?
120    unsigned char n=0;
121    if(a.size<b.size){
122      while(of<dest_length){
123        *(dp+of)=n^*(bp+of);
124        of++;
125      }
126    } else{
127      while(of<dest_length){
128        *(dp+of)=*(ap+of)^n;
129        of++;
130      }
131    }
132  }
133  // should be finished now
134  GameState r = {b.id, dest_length, dp};
135  return r;
136}
137
138
139GameState GameStateClient::decompress(GameStateCompressed a){
140  int normsize = a.normsize;
141  int compsize = a.compsize;
142  unsigned char* dest = (unsigned char*)malloc( normsize );
143  int retval;
144  uLongf length=normsize;
145  retval = uncompress( dest, &length, a.data, (uLong)compsize );
146
147  switch ( retval ) {
148    case Z_OK: std::cout << "successfully compressed" << std::endl; break;
149    case Z_MEM_ERROR: std::cout << "not enough memory available" << std::endl; break;
150    case Z_BUF_ERROR: std::cout << "not enough memory available in the buffer" << std::endl; break;
151    case Z_DATA_ERROR: std::cout << "data corrupted" << std::endl; break;
152  }
153
154  GameState gamestate;
155  gamestate.id = a.id;
156  gamestate.size = normsize;
157  gamestate.data = dest;
158  gamestate.diffed = a.diffed;
159
160  return gamestate;
161}
162
163
164GameState GameStateClient::decode(GameState a, GameStateCompressed x){
165  GameState t = decompress(x);
166  return diff(a, t);
167}
168
169GameState GameStateClient::decode(GameStateCompressed x){
170  GameState t = decompress(x);
171  return t;
172}
173
174
175
176}
Note: See TracBrowser for help on using the repository browser.