Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/network/GameStateClient.cc @ 1258

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