Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1109 was 1062, checked in by rgrieder, 17 years ago
  • changed header file inclusion order
File size: 6.3 KB
RevLine 
[1056]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 */
[1062]28
29#include "GameStateClient.h"
[1056]30
[777]31#include <zlib.h>
[514]32
[777]33#include "core/CoreIncludes.h"
34#include "Synchronisable.h"
[416]35
[777]36namespace network
[416]37{
[777]38  GameStateClient::GameStateClient() {
39  }
[416]40
[777]41  GameStateClient::~GameStateClient() {
42  }
[416]43
[777]44  bool GameStateClient::pushGameState(GameStateCompressed *compstate) {
[1021]45    GameState *gs;
[777]46    if(compstate->diffed)
[1021]47      gs = decode(reference, compstate);
[777]48    else
[1021]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;
[777]54  }
[416]55
[777]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  }
[416]66
[777]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  */
[1021]71  bool GameStateClient::loadSnapshot(GameState *state) {
72    unsigned char *data=state->data;
73    COUT(4) << "loadSnapshot: loading gs: " << state->id << std::endl;
[777]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 ;)
[1021]78    while(data < state->data+state->size){
[777]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;
[416]88
[777]89      if(it->objectID!=sync.objectID){
90        // bad luck ;)
91        // delete the synchronisable (obviously seems to be deleted on the server)
[1021]92        while(it && it->objectID!=sync.objectID)
[777]93          removeObject(it);
[1056]94
95
[1021]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());
[777]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() )
[1021]104            COUT(1) << "We couldn't create/update the object: " << sync.objectID << std::endl;
[777]105          ++it;
106        }
107      } else {
108        // we have our object
109        if(! it->updateData(sync))
[1021]110          COUT(1) << "We couldn't update objectID: " \
[777]111          << sync.objectID << "; classID: " << sync.classID << std::endl;
[416]112      }
[777]113      ++it;
[416]114    }
[777]115
116    return true;
[416]117  }
[514]118
[1021]119  GameState *GameStateClient::undiff(GameState *a, GameState *b) {
120    unsigned char *ap = a->data, *bp = b->data;
[777]121    int of=0; // pointers offset
122    int dest_length=0;
[1021]123    if(a->size>=b->size)
124      dest_length=a->size;
[777]125    else
[1021]126      dest_length=b->size;
[777]127    unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char));
[1021]128    while(of<a->size && of<b->size){
[777]129      *(dp+of)=*(ap+of)^*(bp+of); // do the xor
130      ++of;
131    }
[1021]132    if(a->size!=b->size){ // do we have to fill up ?
[777]133      unsigned char n=0;
[1021]134      if(a->size<b->size){
[777]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        }
[416]144      }
145    }
[777]146    // should be finished now
[782]147    // FIXME: is it true or false now? (struct has changed, producing warnings)
[1021]148    GameState *r = new GameState;
149    r->id = b->id;
150    r->size = dest_length;
151    r->diffed = false;
152    r->data = dp;
[777]153    return r;
[416]154  }
155
[1021]156  GameState *GameStateClient::decompress(GameStateCompressed *a) {
157    int normsize = a->normsize;
158    int compsize = a->compsize;
[777]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;
[1021]169    retval = uncompress( dest, &length, a->data, (uLong)compsize );
[777]170    //std::cout << "length " << length << std::endl;
171    switch ( retval ) {
[1021]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;
[777]176    }
[416]177
[1021]178    GameState *gamestate = new GameState;
179    gamestate->id = a->id;
180    gamestate->size = normsize;
181    gamestate->data = dest;
182    gamestate->diffed = a->diffed;
[1056]183
[1021]184    delete a->data; //delete compressed data
185    delete a; //we do not need the old (struct) gamestate anymore
[777]186
187    return gamestate;
[416]188  }
[514]189
[1021]190  GameState *GameStateClient::decode(GameState *a, GameStateCompressed *x) {
191    GameState *t = decompress(x);
192    return undiff(a, t);
[777]193  }
[416]194
[1021]195  GameState *GameStateClient::decode(GameStateCompressed *x) {
196    GameState *t = decompress(x);
[777]197    return t;
198  }
[416]199
200}
Note: See TracBrowser for help on using the repository browser.