Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core/src/network/GameStateClient.cc @ 820

Last change on this file since 820 was 790, checked in by nicolasc, 17 years ago

merged FICN back into trunk
awaiting release.

File size: 5.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#include <zlib.h>
28
29#include "core/CoreIncludes.h"
30#include "Synchronisable.h"
31#include "GameStateClient.h"
32
33namespace network
34{
35  GameStateClient::GameStateClient() {
36  }
37
38  GameStateClient::~GameStateClient() {
39  }
40
41  bool 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  * This function removes a Synchronisable out of the universe
50  * @param it iterator of the list pointing to the object
51  * @return iterator pointing to the next object in the list
52  */
53  void GameStateClient::removeObject(orxonox::Iterator<Synchronisable> &it) {
54    orxonox::Iterator<Synchronisable> temp=it;
55    ++it;
56    delete  *temp;
57  }
58
59  /**
60  * This function loads a Snapshort of the gamestate into the universe
61  * @param state a GameState struct containing the size of the gamestate and a pointer linking to a flat list (returned by getSnapshot)
62  */
63  bool GameStateClient::loadSnapshot(GameState state) {
64    unsigned char *data=state.data;
65    std::cout << "loadSnapshot: loading gs: " << state.id << std::endl;
66    // get the start of the Synchronisable list
67    orxonox::Iterator<Synchronisable> it=orxonox::ObjectList<Synchronisable>::start();
68    syncData sync;
69    // loop as long as we have some data ;)
70    while(data < state.data+state.size){
71      // prepare the syncData struct
72      sync.length = (int)*data;
73      data+=sizeof(int);
74      sync.objectID = (int)*data;
75      data+=sizeof(int);
76      sync.classID = (int)*data;
77      data+=sizeof(int);
78      sync.data = data;
79      data+=sync.length;
80
81      if(it->objectID!=sync.objectID){
82        // bad luck ;)
83        // delete the synchronisable (obviously seems to be deleted on the server)
84        while(it != 0 && it->objectID!=sync.objectID){
85          removeObject(it);
86        }
87        if(it==0){
88          std::cout << "classid: " << sync.classID << ", name: " << ID(sync.classID)->getName() << std::endl;
89          Synchronisable *no = (Synchronisable*)(ID(sync.classID)->fabricate());
90          no->objectID=sync.objectID;
91          no->classID=sync.classID;
92          it=orxonox::ObjectList<Synchronisable>::end();
93          // update data and create object/entity...
94          if( !no->updateData(sync) && !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
110  GameState 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    // FIXME: is it true or false now? (struct has changed, producing warnings)
139    GameState r = {b.id, dest_length, true, dp};
140    return r;
141  }
142
143  GameState GameStateClient::decompress(GameStateCompressed a) {
144    int normsize = a.normsize;
145    int compsize = a.compsize;
146    int bufsize;
147    if(normsize < compsize)
148      bufsize = compsize;
149    else
150      bufsize = normsize;
151    unsigned char* dest = (unsigned char*)malloc( bufsize );
152    int retval;
153    uLongf length=normsize;
154    //std::cout << "gamestateclient" << std::endl;
155    //std::cout << "normsize " << a.normsize << " compsize " << a.compsize << " " << bufsize << std::endl;
156    retval = uncompress( dest, &length, a.data, (uLong)compsize );
157    //std::cout << "length " << length << std::endl;
158    switch ( retval ) {
159      case Z_OK: std::cout << "successfully decompressed" << std::endl; break;
160      case Z_MEM_ERROR: std::cout << "not enough memory available" << std::endl; break;
161      case Z_BUF_ERROR: std::cout << "not enough memory available in the buffer" << std::endl; break;
162      case Z_DATA_ERROR: std::cout << "data corrupted" << std::endl; break;
163    }
164
165    GameState gamestate;
166    gamestate.id = a.id;
167    gamestate.size = normsize;
168    gamestate.data = dest;
169    gamestate.diffed = a.diffed;
170
171    return gamestate;
172  }
173
174  GameState GameStateClient::decode(GameState a, GameStateCompressed x) {
175    GameState t = decompress(x);
176    return diff(a, t);
177  }
178
179  GameState GameStateClient::decode(GameStateCompressed x) {
180    GameState t = decompress(x);
181    return t;
182  }
183
184}
Note: See TracBrowser for help on using the repository browser.