Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 619 was 567, checked in by scheusso, 17 years ago

some minor changes/enhancements in GameStateClient

File size: 5.0 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        // update data and create object/entity...
94        if( !(((Synchronisable *)no)->updateData(sync)) && !(((Synchronisable *)no)->create()) )
95          COUT(0) << "We couldn't create/update the object: " << sync.objectID << std::endl;
96        ++it;
97      }
98    } else {
99      // we have our object
100      if(! it->updateData(sync))
101        std::cout << "We couldn't update objectID: " \
102            << sync.objectID << "; classID: " << sync.classID << std::endl;
103    }
104    ++it;
105  }
106
107  return true;
108}
109
110GameState GameStateClient::diff(GameState a, GameState b){
111  unsigned char *ap = a.data, *bp = b.data;
112  int of=0; // pointers offset
113  int dest_length=0;
114  if(a.size>=b.size)
115    dest_length=a.size;
116  else
117    dest_length=b.size;
118  unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char));
119  while(of<a.size && of<b.size){
120    *(dp+of)=*(ap+of)^*(bp+of); // do the xor
121    ++of;
122  }
123  if(a.size!=b.size){ // do we have to fill up ?
124    unsigned char n=0;
125    if(a.size<b.size){
126      while(of<dest_length){
127        *(dp+of)=n^*(bp+of);
128        of++;
129      }
130    } else{
131      while(of<dest_length){
132        *(dp+of)=*(ap+of)^n;
133        of++;
134      }
135    }
136  }
137  // should be finished now
138  GameState r = {b.id, dest_length, dp};
139  return r;
140}
141
142
143GameState GameStateClient::decompress(GameStateCompressed a){
144  int normsize = a.normsize;
145  int compsize = a.compsize;
146  unsigned char* dest = (unsigned char*)malloc( normsize );
147  int retval;
148  uLongf length=normsize;
149  retval = uncompress( dest, &length, a.data, (uLong)compsize );
150
151  switch ( retval ) {
152    case Z_OK: std::cout << "successfully compressed" << std::endl; break;
153    case Z_MEM_ERROR: std::cout << "not enough memory available" << std::endl; break;
154    case Z_BUF_ERROR: std::cout << "not enough memory available in the buffer" << std::endl; break;
155    case Z_DATA_ERROR: std::cout << "data corrupted" << std::endl; break;
156  }
157
158  GameState gamestate;
159  gamestate.id = a.id;
160  gamestate.size = normsize;
161  gamestate.data = dest;
162  gamestate.diffed = a.diffed;
163
164  return gamestate;
165}
166
167
168GameState GameStateClient::decode(GameState a, GameStateCompressed x){
169  GameState t = decompress(x);
170  return diff(a, t);
171}
172
173GameState GameStateClient::decode(GameStateCompressed x){
174  GameState t = decompress(x);
175  return t;
176}
177
178
179
180}
Note: See TracBrowser for help on using the repository browser.