Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 632 was 632, checked in by dumenim, 16 years ago

networkstuff bluber fubber

File size: 5.4 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  std::cout << "loadSnapshot: loading gs: " << state.id << std::endl;
68  // get the start of the Synchronisable list
69  orxonox::Iterator<Synchronisable> it=orxonox::ObjectList<Synchronisable>::start();
70  syncData sync;
71  // loop as long as we have some data ;)
72  while(data < state.data+state.size){
73    // prepare the syncData struct
74    sync.length = (int)*data;
75    data+=sizeof(int);
76    sync.objectID = (int)*data;
77    data+=sizeof(int);
78    sync.classID = (int)*data;
79    data+=sizeof(int);
80    sync.data = data;
81    data+=sync.length;
82
83    if(it->objectID!=sync.objectID){
84      // bad luck ;)
85      // delete the synchronisable (obviously seems to be deleted on the server)
86      while(it != 0 && it->objectID!=sync.objectID){
87        removeObject(it);
88      }
89      if(it==0){
90        orxonox::BaseObject *no = ID(sync.classID)->fabricate();
91        ((Synchronisable *)no)->objectID=sync.objectID;
92        ((Synchronisable *)no)->classID=sync.classID;
93        it=orxonox::ObjectList<Synchronisable>::end();
94        // update data and create object/entity...
95        if( !(((Synchronisable *)no)->updateData(sync)) && !(((Synchronisable *)no)->create()) )
96          COUT(0) << "We couldn't create/update the object: " << sync.objectID << std::endl;
97        ++it;
98      }
99    } else {
100      // we have our object
101      if(! it->updateData(sync))
102        std::cout << "We couldn't update objectID: " \
103            << sync.objectID << "; classID: " << sync.classID << std::endl;
104    }
105    ++it;
106  }
107
108  return true;
109}
110
111GameState GameStateClient::diff(GameState a, GameState b){
112  unsigned char *ap = a.data, *bp = b.data;
113  int of=0; // pointers offset
114  int dest_length=0;
115  if(a.size>=b.size)
116    dest_length=a.size;
117  else
118    dest_length=b.size;
119  unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char));
120  while(of<a.size && of<b.size){
121    *(dp+of)=*(ap+of)^*(bp+of); // do the xor
122    ++of;
123  }
124  if(a.size!=b.size){ // do we have to fill up ?
125    unsigned char n=0;
126    if(a.size<b.size){
127      while(of<dest_length){
128        *(dp+of)=n^*(bp+of);
129        of++;
130      }
131    } else{
132      while(of<dest_length){
133        *(dp+of)=*(ap+of)^n;
134        of++;
135      }
136    }
137  }
138  // should be finished now
139  GameState r = {b.id, dest_length, dp};
140  return r;
141}
142
143
144GameState GameStateClient::decompress(GameStateCompressed a){
145  int normsize = a.normsize;
146  int compsize = a.compsize;
147  int bufsize;
148  if(normsize < compsize)
149    bufsize = compsize;
150  else
151    bufsize = normsize;
152  unsigned char* dest = (unsigned char*)malloc( bufsize );
153  int retval;
154  uLongf length=normsize;
155  //std::cout << "gamestateclient" << std::endl;
156  //std::cout << "normsize " << a.normsize << " compsize " << a.compsize << " " << bufsize << std::endl;
157  retval = uncompress( dest, &length, a.data, (uLong)compsize );
158  //std::cout << "length " << length << std::endl;
159  switch ( retval ) {
160    case Z_OK: std::cout << "successfully decompressed" << std::endl; break;
161    case Z_MEM_ERROR: std::cout << "not enough memory available" << std::endl; break;
162    case Z_BUF_ERROR: std::cout << "not enough memory available in the buffer" << std::endl; break;
163    case Z_DATA_ERROR: std::cout << "data corrupted" << std::endl; break;
164  }
165
166  GameState gamestate;
167  gamestate.id = a.id;
168  gamestate.size = normsize;
169  gamestate.data = dest;
170  gamestate.diffed = a.diffed;
171 
172  return gamestate;
173}
174
175
176GameState GameStateClient::decode(GameState a, GameStateCompressed x){
177  GameState t = decompress(x);
178  return diff(a, t);
179}
180
181GameState GameStateClient::decode(GameStateCompressed x){
182  GameState t = decompress(x);
183  return t;
184}
185
186
187
188}
Note: See TracBrowser for help on using the repository browser.