Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/network/packet/Gamestate.cc @ 2483

Last change on this file since 2483 was 2476, checked in by scheusso, 16 years ago

fix of possible bug

  • Property svn:eol-style set to native
File size: 21.2 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 *      Oliver Scheuss, (C) 2008
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "Gamestate.h"
30#include "../GamestateHandler.h"
31#include "../synchronisable/Synchronisable.h"
32#include "../TrafficControl.h"
33#include "core/CoreIncludes.h"
34#include "core/Iterator.h"
35
36#include <zlib.h>
37#include <assert.h>
38
39
40
41namespace orxonox {
42
43namespace packet {
44
45#define GAMESTATE_START(data) (data + sizeof(GamestateHeader))
46#define GAMESTATE_HEADER(data) ((GamestateHeader *)data)
47#define HEADER GAMESTATE_HEADER(data_)
48
49
50#define PACKET_FLAG_GAMESTATE  ENET_PACKET_FLAG_RELIABLE
51
52Gamestate::Gamestate()
53{
54  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
55}
56
57Gamestate::Gamestate(uint8_t *data, unsigned int clientID):
58    Packet(data, clientID)
59{
60  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
61}
62
63Gamestate::Gamestate(uint8_t *data)
64{
65  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
66  data_=data;
67}
68
69
70Gamestate::~Gamestate()
71{
72}
73
74bool Gamestate::collectData(int id, uint8_t mode)
75{
76  uint32_t tempsize=0, currentsize=0;
77  assert(data_==0);
78  uint32_t size = calcGamestateSize(id, mode);
79
80  COUT(4) << "G.ST.Man: producing gamestate with id: " << id << std::endl;
81  if(size==0)
82    return false;
83  data_ = new unsigned char[size + sizeof(GamestateHeader)];
84  if(!data_){
85    COUT(2) << "GameStateManager: could not allocate memory" << std::endl;
86    return false;
87  }
88
89  //start collect data synchronisable by synchronisable
90  uint8_t *mem=data_;
91  mem+=sizeof(GamestateHeader);
92  ObjectList<Synchronisable>::iterator it;
93  for(it = ObjectList<Synchronisable>::begin(); it; ++it){
94    tempsize=it->getSize(id, mode);
95
96    if(currentsize+tempsize > size){
97      assert(0); // if we don't use multithreading this part shouldn't be neccessary
98      // start allocate additional memory
99      COUT(3) << "G.St.Man: need additional memory" << std::endl;
100      ObjectList<Synchronisable>::iterator temp = it;
101      int addsize=tempsize;
102      while(++temp)
103        addsize+=temp->getSize(id, mode);
104      data_ = (uint8_t *)realloc(data_, sizeof(GamestateHeader) + currentsize + addsize);
105      if(!data_)
106        return false;
107      size = currentsize+addsize;
108    }// stop allocate additional memory
109
110
111    //if(it->doSelection(id))
112    if(tempsize!=0)
113      dataMap_.push_back( obj(it->getObjectID(), it->getCreatorID(), tempsize, mem-data_) );
114//     dataMap_[mem-data_]=(*it);  // save the mem location of the synchronisable data
115    if(!it->getData(mem, id, mode))
116      return false; // mem pointer gets automatically increased because of call by reference
117    // increase size counter by size of current synchronisable
118    currentsize+=tempsize;
119  }
120
121
122  //start write gamestate header
123  HEADER->packetType = ENUM::Gamestate;
124  HEADER->datasize = currentsize;
125  HEADER->id = id;
126  HEADER->diffed = false;
127  HEADER->complete = true;
128  HEADER->compressed = false;
129  //stop write gamestate header
130
131  COUT(5) << "G.ST.Man: Gamestate size: " << currentsize << std::endl;
132  COUT(5) << "G.ST.Man: 'estimated' (and corrected) Gamestate size: " << size << std::endl;
133  return true;
134}
135
136bool Gamestate::spreadData(uint8_t mode)
137{
138  assert(data_);
139  assert(!HEADER->compressed);
140  assert(!HEADER->diffed);
141  uint8_t *mem=data_+sizeof(GamestateHeader);
142    // get the start of the Synchronisable list
143  //ObjectList<Synchronisable>::iterator it=ObjectList<Synchronisable>::begin();
144  Synchronisable *s;
145
146  // update the data of the objects we received
147  while(mem < data_+sizeof(GamestateHeader)+HEADER->datasize){
148    synchronisableHeader *objectheader = (synchronisableHeader*)mem;
149
150    s = Synchronisable::getSynchronisable( objectheader->objectID );
151    if(!s)
152    {
153      Synchronisable::fabricate(mem, mode);
154//         COUT(0) << "could not fabricate synchronisable: " << objectheader->objectID << " classid: " << objectheader->classID << " creator: " << objectheader->creatorID << endl;
155//       else
156//         COUT(0) << "fabricated: " << objectheader->objectID << " classid: " << objectheader->classID << " creator: "  << objectheader->creatorID << endl;
157    }
158    else
159    {
160      bool b = s->updateData(mem, mode);
161      assert(b);
162    }
163  }
164
165   // In debug mode, check first, whether there are no duplicate objectIDs
166#ifndef NDEBUG
167  ObjectList<Synchronisable>::iterator it;
168  for (it = ObjectList<Synchronisable>::begin(); it != ObjectList<Synchronisable>::end(); ++it) {
169    if (it->getObjectID() == OBJECTID_UNKNOWN) {
170      if (it->objectMode_ != 0x0) {
171        COUT(0) << "Found object with OBJECTID_UNKNOWN on the client with objectMode != 0x0!" << std::endl;
172        assert(false);
173      }
174    }
175    else {
176      ObjectList<Synchronisable>::iterator it2;
177      for (it2 = ObjectList<Synchronisable>::begin(); it2 != ObjectList<Synchronisable>::end(); ++it2) {
178        if (it->getObjectID() == it2->getObjectID() && *it != *it2) {
179           COUT(0) << "Found duplicate objectIDs on the client!" << std::endl
180                   << "Are you sure you don't create a Sychnronisable objcect with 'new' \
181                       that doesn't have objectMode = 0x0?" << std::endl;
182           assert(false);
183        }
184      }
185    }
186  }
187#endif
188
189  return true;
190}
191
192
193
194int Gamestate::getID(){
195  return HEADER->id;
196}
197
198uint32_t Gamestate::getSize() const
199{
200  assert(data_);
201  if(HEADER->compressed)
202    return HEADER->compsize+sizeof(GamestateHeader);
203  else
204  {
205    return HEADER->datasize+sizeof(GamestateHeader);
206  }
207}
208
209bool Gamestate::operator==(packet::Gamestate gs){
210  uint8_t *d1 = data_+sizeof(GamestateHeader);
211  uint8_t *d2 = gs.data_+sizeof(GamestateHeader);
212  assert(!isCompressed());
213  assert(!gs.isCompressed());
214  while(d1<data_+HEADER->datasize)
215  {
216    if(*d1!=*d2)
217      return false;
218    d1++;
219    d2++;
220  }
221  return true;
222}
223
224bool Gamestate::process()
225{
226  return GamestateHandler::addGamestate(this, getClientID());
227}
228
229
230
231bool Gamestate::compressData()
232{
233  assert(HEADER);
234  assert(!HEADER->compressed);
235  uLongf buffer = (uLongf)(((HEADER->datasize + 12)*1.01)+1);
236  if(buffer==0)
237    return false;
238
239  uint8_t *ndata = new uint8_t[buffer+sizeof(GamestateHeader)];
240  uint8_t *dest = GAMESTATE_START(ndata);
241  //unsigned char *dest = new unsigned char[buffer];
242  uint8_t *source = GAMESTATE_START(data_);
243  int retval;
244  retval = compress( dest, &buffer, source, (uLong)(HEADER->datasize) );
245  switch ( retval ) {
246    case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break;
247    case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl; return false;
248    case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl; return false;
249    case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl; return false;
250  }
251#ifndef NDEBUG
252  //decompress and compare the start and the decompressed data
253  uint8_t *rdata = new uint8_t[HEADER->datasize+sizeof(GamestateHeader)];
254  uint8_t *d2 = GAMESTATE_START(rdata);
255  uLongf length2 = HEADER->datasize;
256  uncompress(d2, &length2, dest, buffer);
257  for(unsigned int i=0; i<HEADER->datasize; i++){
258    assert(*(source+i)==*(d2+i));
259  }
260  delete[] rdata;
261#endif
262
263  //copy and modify header
264#ifndef NDEBUG
265  HEADER->crc32 = calcCRC(data_+sizeof(GamestateHeader), HEADER->datasize);
266#endif
267  *GAMESTATE_HEADER(ndata) = *HEADER;
268  //delete old data
269  delete[] data_;
270  //save new data
271  data_ = ndata;
272  HEADER->compsize = buffer;
273  HEADER->compressed = true;
274  assert(HEADER->compressed);
275  COUT(4) << "gamestate compress datasize: " << HEADER->datasize << " compsize: " << HEADER->compsize << std::endl;
276  return true;
277}
278bool Gamestate::decompressData()
279{
280  assert(HEADER);
281  assert(HEADER->compressed);
282  COUT(4) << "GameStateClient: uncompressing gamestate. id: " << HEADER->id << ", baseid: " << HEADER->base_id << ", datasize: " << HEADER->datasize << ", compsize: " << HEADER->compsize << std::endl;
283  uint32_t datasize = HEADER->datasize;
284  uint32_t compsize = HEADER->compsize;
285  uint32_t bufsize;
286//  assert(compsize<=datasize);
287  bufsize = datasize;
288  assert(bufsize!=0);
289  uint8_t *ndata = new uint8_t[bufsize + sizeof(GamestateHeader)];
290  uint8_t *dest = ndata + sizeof(GamestateHeader);
291  uint8_t *source = data_ + sizeof(GamestateHeader);
292  int retval;
293  uLongf length=bufsize;
294  retval = uncompress( dest, &length, source, (uLong)compsize );
295  switch ( retval ) {
296    case Z_OK: COUT(5) << "successfully decompressed" << std::endl; break;
297    case Z_MEM_ERROR: COUT(1) << "not enough memory available" << std::endl; return false;
298    case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer" << std::endl; return false;
299    case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return false;
300  }
301#ifndef NDEBUG
302  assert(HEADER->crc32==calcCRC(ndata+sizeof(GamestateHeader), HEADER->datasize));
303#endif
304
305  //copy over the header
306  *GAMESTATE_HEADER(ndata) = *HEADER;
307
308  if (this->bDataENetAllocated_){
309    // Memory was allocated by ENet. --> We let it be since enet_packet_destroy will
310    // deallocated it anyway. So data and packet stay together.
311    this->bDataENetAllocated_ = false;
312  }
313  else{
314    // We allocated the memory in the first place (unlikely). So we destroy the old data
315    // and overwrite it with the new decompressed data.
316    delete[] this->data_;
317  }
318
319  //set new pointers
320  data_ = ndata;
321  HEADER->compressed = false;
322  assert(HEADER->datasize==datasize);
323  assert(HEADER->compsize==compsize);
324  return true;
325}
326
327Gamestate *Gamestate::diff(Gamestate *base)
328{
329  assert(HEADER);
330  assert(!HEADER->compressed);
331  assert(!HEADER->diffed);
332  //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;
333  uint8_t *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_);
334  uint32_t of=0; // pointers offset
335  uint32_t dest_length=0;
336  dest_length=HEADER->datasize;
337  if(dest_length==0)
338    return NULL;
339  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+sizeof(GamestateHeader)];
340  uint8_t *dest = ndata + sizeof(GamestateHeader);
341  while(of < GAMESTATE_HEADER(base->data_)->datasize && of < HEADER->datasize){
342    *(dest+of)=*(basep+of)^*(gs+of); // do the xor
343    ++of;
344  }
345  if(GAMESTATE_HEADER(base->data_)->datasize!=HEADER->datasize){
346    uint8_t n=0;
347    if(GAMESTATE_HEADER(base->data_)->datasize < HEADER->datasize){
348      while(of<dest_length){
349        *(dest+of)=n^*(gs+of);
350        of++;
351      }
352    }
353  }
354
355  *GAMESTATE_HEADER(ndata) = *HEADER;
356  GAMESTATE_HEADER(ndata)->diffed = true;
357  GAMESTATE_HEADER(ndata)->base_id = base->getID();
358  Gamestate *g = new Gamestate(ndata, getClientID());
359  g->flags_=flags_;
360  g->packetDirection_ = packetDirection_;
361  return g;
362}
363
364Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){
365  assert(data_);
366  std::list<obj>::iterator it;
367
368  // allocate memory for new data
369  uint8_t *gdata = new uint8_t[HEADER->datasize+sizeof(GamestateHeader)];
370  // create a gamestate out of it
371  Gamestate *gs = new Gamestate(gdata);
372  uint8_t *newdata = gdata + sizeof(GamestateHeader);
373  uint8_t *origdata = GAMESTATE_START(data_);
374
375  //copy the GamestateHeader
376  *(GamestateHeader*)gdata = *HEADER;
377
378  synchronisableHeader *oldobjectheader, *newobjectheader;
379  uint32_t objectOffset;
380  unsigned int objectsize, destsize=0;
381  // TODO: Why is this variable not used?
382  //Synchronisable *object;
383
384  //call TrafficControl
385  TrafficControl::getInstance()->processObjectList( clientID, HEADER->id, &dataMap_ );
386 
387  //copy in the zeros
388  for(it=dataMap_.begin(); it!=dataMap_.end();){
389//    if((*it).objSize==0)
390//      continue;
391    oldobjectheader = (synchronisableHeader*)origdata;
392    newobjectheader = (synchronisableHeader*)newdata;
393    if ( (*it).objSize == 0 )
394      continue;
395//     object = Synchronisable::getSynchronisable( (*it).objID );
396//     assert(object->objectID == oldobjectheader->objectID);
397    objectsize = oldobjectheader->size;
398    objectOffset=sizeof(synchronisableHeader); //skip the size and the availableData variables in the objectheader
399    if ( (*it).objID == oldobjectheader->objectID ){
400      memcpy(newdata, origdata, objectsize);
401      assert(newobjectheader->dataAvailable==true);
402      ++it;
403    }else{
404      *newobjectheader = *oldobjectheader;
405      newobjectheader->dataAvailable=false;
406      memset(newdata+objectOffset, 0, objectsize-objectOffset);
407    }
408    newdata += objectsize;
409    origdata += objectsize;
410    destsize += objectsize;
411//     origdata += objectsize;
412  }
413  ((GamestateHeader*)gdata)->datasize = destsize;
414  assert(destsize==HEADER->datasize);
415  assert(destsize!=0);
416  return gs;
417}
418
419
420Gamestate* Gamestate::intelligentDiff(Gamestate *base, unsigned int clientID){
421  // asserts
422  assert(data_);
423  assert(base->data_);
424  assert(!GAMESTATE_HEADER(base->data_)->diffed);
425  assert(!GAMESTATE_HEADER(base->data_)->compressed);
426  assert(!HEADER->compressed);
427  assert(!HEADER->diffed);
428
429  //preparations
430  std::list<obj>::iterator it;
431  uint8_t *origdata, *basedata, *destdata, *ndata;
432  uint32_t objectOffset, streamOffset=0;    //data offset
433  uint32_t minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize;
434  synchronisableHeader *origheader;
435  synchronisableHeader *destheader;
436  Synchronisable *object;
437
438  origdata = GAMESTATE_START(this->data_);
439  basedata = GAMESTATE_START(base->data_);
440  ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)];
441  destdata = ndata + sizeof(GamestateHeader);
442
443  // do the diff
444  for(it=dataMap_.begin(); it!=dataMap_.end(); it++){
445    assert(streamOffset<HEADER->datasize);
446    origheader = (synchronisableHeader *)(origdata+streamOffset);
447    destheader = (synchronisableHeader *)(destdata+streamOffset);
448    object = Synchronisable::getSynchronisable(origheader->objectID);
449    bool sendData = object->doSelection(HEADER->id);
450
451    //copy and partially diff the object header
452    assert(sizeof(synchronisableHeader)==3*sizeof(uint32_t)+sizeof(bool));
453    *(uint32_t*)destdata = *(uint32_t*)origdata; //size (do not diff)
454    *(bool*)(destdata+sizeof(uint32_t)) = sendData;
455    if(sendData){
456      *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = *(uint32_t*)(basedata+sizeof(uint32_t)+sizeof(bool)) ^ *(uint32_t*)(origdata+sizeof(uint32_t)+sizeof(bool)); //objectid (diff it)
457      *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = *(uint32_t*)(basedata+2*sizeof(uint32_t)+sizeof(bool)) ^ *(uint32_t*)(origdata+2*sizeof(uint32_t)+sizeof(bool)); //classid (diff it)
458    }else{
459      *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = 0;
460      *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = 0;
461    }
462    objectOffset=sizeof(synchronisableHeader);
463    streamOffset+=sizeof(synchronisableHeader);
464
465    //now handle the object data or fill with zeros
466    while(objectOffset<origheader->size ){
467
468      if(sendData && streamOffset<minsize)
469        *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor
470      else if(sendData)
471        *(destdata+objectOffset)=((uint8_t)0)^*(origdata+objectOffset); // xor with 0 (basestream is too short)
472      else
473        *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered
474
475      objectOffset++;
476      streamOffset++;
477    }
478    destdata+=objectOffset;
479    origdata+=objectOffset;
480    basedata+=objectOffset;
481  }
482
483  //copy over the gamestate header and set the diffed flag
484  *(GamestateHeader *)ndata = *HEADER; //copy over the header
485  Gamestate *gs = new Gamestate(ndata);
486  GAMESTATE_HEADER(ndata)->diffed=true;
487  return gs;
488}
489
490Gamestate* Gamestate::intelligentUnDiff(Gamestate *base){
491  // asserts
492  assert(data_);
493  assert(base->data_);
494  assert(!GAMESTATE_HEADER(base->data_)->diffed);
495  assert(!GAMESTATE_HEADER(base->data_)->compressed);
496  assert(!HEADER->compressed);
497  assert(HEADER->diffed);
498
499  //preparations
500  std::list<obj>::iterator it;
501  uint8_t *origdata, *basedata, *destdata, *ndata;
502  uint32_t objectOffset, streamOffset=0;    //data offset
503  uint32_t minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize;
504  synchronisableHeader *origheader;
505  synchronisableHeader *destheader;
506  Synchronisable *object;
507
508  origdata = GAMESTATE_START(this->data_);
509  basedata = GAMESTATE_START(base->data_);
510  ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)];
511  destdata = ndata + sizeof(GamestateHeader);
512
513  // do the undiff
514  for(it=dataMap_.begin(); it!=dataMap_.end(); it++){
515    assert(streamOffset<HEADER->datasize);
516    origheader = (synchronisableHeader *)(origdata+streamOffset);
517    destheader = (synchronisableHeader *)(destdata+streamOffset);
518    object = Synchronisable::getSynchronisable( origheader->objectID );
519    bool sendData;
520
521    //copy and partially diff the object header
522    assert(sizeof(synchronisableHeader)==3*sizeof(uint32_t)+sizeof(bool));
523    *(uint32_t*)destdata = *(uint32_t*)origdata; //size (do not diff)
524    *(bool*)(destdata+sizeof(uint32_t)) = *(bool*)(origdata+sizeof(uint32_t));
525    sendData = *(bool*)(origdata+sizeof(uint32_t));
526    if(sendData){
527      *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = *(uint32_t*)(basedata+sizeof(uint32_t)+sizeof(bool)) ^ *(uint32_t*)(origdata+sizeof(uint32_t)+sizeof(bool)); //objectid (diff it)
528      *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = *(uint32_t*)(basedata+2*sizeof(uint32_t)+sizeof(bool)) ^ *(uint32_t*)(origdata+2*sizeof(uint32_t)+sizeof(bool)); //classid (diff it)
529    }else{
530      *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = 0;
531      *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = 0;
532    }
533    objectOffset=sizeof(synchronisableHeader);
534    streamOffset+=sizeof(synchronisableHeader);
535
536    //now handle the object data or fill with zeros
537    while(objectOffset<origheader->size ){
538
539      if(sendData && streamOffset<minsize)
540        *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor
541      else if(sendData)
542        *(destdata+objectOffset)=((unsigned char)0)^*(origdata+objectOffset); // xor with 0 (basestream is too short)
543      else
544        *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered
545
546      objectOffset++;
547      streamOffset++;
548    }
549    destdata+=objectOffset;
550    origdata+=objectOffset;
551    basedata+=objectOffset;
552  }
553
554  //copy over the gamestate header and set the diffed flag
555  *(GamestateHeader *)ndata = *HEADER; //copy over the header
556  Gamestate *gs = new Gamestate(ndata);
557  GAMESTATE_HEADER(ndata)->diffed=false;
558  return gs;
559}
560
561Gamestate *Gamestate::undiff(Gamestate *base)
562{
563  assert(this && base);assert(HEADER);
564  assert(HEADER->diffed);
565  assert(!HEADER->compressed && !GAMESTATE_HEADER(base->data_)->compressed);
566  //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;
567  uint8_t *basep = GAMESTATE_START(base->data_);
568  uint8_t *gs = GAMESTATE_START(this->data_);
569  uint32_t of=0; // pointers offset
570  uint32_t dest_length=0;
571  dest_length=HEADER->datasize;
572  if(dest_length==0)
573    return NULL;
574  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+sizeof(GamestateHeader)];
575  uint8_t *dest = ndata + sizeof(GamestateHeader);
576  while(of < GAMESTATE_HEADER(base->data_)->datasize && of < HEADER->datasize){
577    *(dest+of)=*(basep+of)^*(gs+of); // do the xor
578    ++of;
579  }
580  if(GAMESTATE_HEADER(base->data_)->datasize!=HEADER->datasize){
581    uint8_t n=0;
582    if(GAMESTATE_HEADER(base->data_)->datasize < HEADER->datasize){
583      while(of < dest_length){
584        *(dest+of)=n^*(gs+of);
585        of++;
586      }
587    }
588  }
589  *GAMESTATE_HEADER(ndata) = *HEADER;
590  GAMESTATE_HEADER(ndata)->diffed = false;
591  Gamestate *g = new Gamestate(ndata, getClientID());
592  g->flags_=flags_;
593  g->packetDirection_ = packetDirection_;
594  assert(!g->isDiffed());
595  assert(!g->isCompressed());
596  return g;
597}
598
599
600uint32_t Gamestate::calcGamestateSize(int32_t id, uint8_t mode)
601{
602  uint32_t size=0;
603    // get the start of the Synchronisable list
604  ObjectList<Synchronisable>::iterator it;
605    // get total size of gamestate
606  for(it = ObjectList<Synchronisable>::begin(); it; ++it)
607    size+=it->getSize(id, mode); // size of the actual data of the synchronisable
608//  size+=sizeof(GamestateHeader);
609  return size;
610}
611
612/**
613 * This function removes a Synchronisable out of the universe
614 * @param it iterator of the list pointing to the object
615 * @return iterator pointing to the next object in the list
616 */
617  void Gamestate::removeObject(ObjectList<Synchronisable>::iterator &it) {
618    ObjectList<Synchronisable>::iterator temp=it;
619    ++it;
620    delete  *temp;
621  }
622
623  bool Gamestate::isDiffed(){
624    return HEADER->diffed;
625  }
626
627  bool Gamestate::isCompressed(){
628    return HEADER->compressed;
629  }
630
631  int Gamestate::getBaseID(){
632    return HEADER->base_id;
633  }
634}
635
636}
Note: See TracBrowser for help on using the repository browser.