Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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