Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ai/src/libraries/network/packet/Gamestate.cc @ 7656

Last change on this file since 7656 was 7163, checked in by dafrick, 14 years ago

Merged presentation3 branch into trunk.

  • Property svn:eol-style set to native
File size: 24.5 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
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "Gamestate.h"
30
31#include <zlib.h>
32
33#include "util/Debug.h"
34#include "core/GameMode.h"
35#include "core/ObjectList.h"
36#include "network/synchronisable/Synchronisable.h"
37#include "network/GamestateHandler.h"
38
39namespace orxonox {
40
41namespace packet {
42
43#define GAMESTATE_START(data) (data + GamestateHeader::getSize())
44
45#define PACKET_FLAG_GAMESTATE  PacketFlag::Reliable
46
47inline bool memzero( uint8_t* data, uint32_t datalength)
48{
49  uint64_t* d = (uint64_t*)data;
50
51  for( unsigned int i=0; i<datalength/8; i++ )
52  {
53    if( *(d+i) != 0 )
54      return false;
55  }
56  // now process the rest (when datalength isn't a multiple of 4)
57  for( unsigned int j = 8*(datalength/8); j<datalength; j++ )
58  {
59    if( *(data+j) != 0 )
60      return false;
61  }
62  return true;
63}
64
65
66Gamestate::Gamestate():
67  header_(0)
68{
69  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
70}
71
72
73Gamestate::Gamestate(uint8_t *data, unsigned int clientID):
74  Packet(data, clientID)
75{
76  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
77  header_ = new GamestateHeader(data_);
78}
79
80
81Gamestate::Gamestate(uint8_t *data)
82{
83  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
84  data_ = data;
85  header_ = new GamestateHeader(data_);
86}
87
88
89Gamestate::Gamestate(const Gamestate& g) :
90    Packet( *(Packet*)&g ), nrOfVariables_(0)
91{
92  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
93  header_ = new GamestateHeader(data_);
94  sizes_ = g.sizes_;
95}
96
97
98Gamestate::~Gamestate()
99{
100  if( header_ )
101    delete header_;
102}
103
104
105bool Gamestate::collectData(int id, uint8_t mode)
106{
107  assert(this->header_==0); // make sure the header didn't exist before
108  uint32_t tempsize=0, currentsize=0;
109  assert(data_==0);
110  uint32_t size = calcGamestateSize(id, mode);
111
112  COUT(4) << "G.ST.Man: producing gamestate with id: " << id << std::endl;
113  if(size==0)
114    return false;
115  data_ = new uint8_t[size + GamestateHeader::getSize()];
116  if(!data_)
117  {
118    COUT(2) << "GameStateManager: could not allocate memory" << std::endl;
119    return false;
120  }
121
122  // create the header object
123  assert( header_ == 0 );
124  header_ = new GamestateHeader(data_);
125
126  //start collect data synchronisable by synchronisable
127  uint8_t *mem = data_; // in this stream store all data of the variables and the headers of the synchronisable
128  mem += GamestateHeader::getSize();
129  ObjectList<Synchronisable>::iterator it;
130  for(it = ObjectList<Synchronisable>::begin(); it; ++it)
131  {
132
133//     tempsize=it->getSize(id, mode);
134
135    tempsize = it->getData(mem, this->sizes_, id, mode);
136    if ( tempsize != 0 )
137      dataVector_.push_back( obj(it->getObjectID(), it->getCreatorID(), tempsize, mem-data_) );
138
139#ifndef NDEBUG
140    if(currentsize+tempsize > size)
141    {
142      assert(0); // if we don't use multithreading this part shouldn't be neccessary
143      // start allocate additional memory
144      COUT(3) << "G.St.Man: need additional memory" << std::endl;
145      ObjectList<Synchronisable>::iterator temp = it;
146      uint32_t addsize=tempsize;
147      while(++temp)
148        addsize+=temp->getSize(id, mode);
149      data_ = (uint8_t *)realloc(data_, GamestateHeader::getSize() + currentsize + addsize);
150      if(!data_)
151        return false;
152      size = currentsize+addsize;
153    }// stop allocate additional memory
154#endif
155//     if(!it->getData(mem, id, mode))
156//       return false; // mem pointer gets automatically increased because of call by reference
157    // increase size counter by size of current synchronisable
158    currentsize+=tempsize;
159  }
160
161
162  //start write gamestate header
163  header_->setDataSize( currentsize );
164  header_->setID( id );
165  header_->setBaseID( GAMESTATEID_INITIAL );
166  header_->setDiffed( false );
167  header_->setComplete( true );
168  header_->setCompressed( false );
169  //stop write gamestate header
170
171  COUT(5) << "G.ST.Man: Gamestate size: " << currentsize << std::endl;
172  COUT(5) << "G.ST.Man: 'estimated' (and corrected) Gamestate size: " << size << std::endl;
173  return true;
174}
175
176
177bool Gamestate::spreadData(uint8_t mode)
178{
179  COUT(4) << "processing gamestate with id " << header_->getID() << endl;
180  assert(data_);
181  assert(!header_->isCompressed());
182  uint8_t *mem=data_+GamestateHeader::getSize();
183  Synchronisable *s;
184
185  // update the data of the objects we received
186  while(mem < data_+GamestateHeader::getSize()+header_->getDataSize())
187  {
188    SynchronisableHeader objectheader(mem);
189
190    s = Synchronisable::getSynchronisable( objectheader.getObjectID() );
191    if(!s)
192    {
193      if (!GameMode::isMaster())
194      {
195        Synchronisable::fabricate(mem, mode);
196      }
197      else
198      {
199        mem += objectheader.getDataSize() + ( objectheader.isDiffed() ? SynchronisableHeaderLight::getSize() : SynchronisableHeader::getSize() );
200      }
201    }
202    else
203    {
204      bool b = s->updateData(mem, mode);
205      assert(b);
206    }
207  }
208   // In debug mode, check first, whether there are no duplicate objectIDs
209#ifndef NDEBUG
210  if(this->getID()%1000==1)
211  {
212    std::list<uint32_t> v1;
213    ObjectList<Synchronisable>::iterator it;
214    for (it = ObjectList<Synchronisable>::begin(); it != ObjectList<Synchronisable>::end(); ++it)
215    {
216      if (it->getObjectID() == OBJECTID_UNKNOWN)
217      {
218        if (it->objectMode_ != 0x0)
219        {
220          COUT(0) << "Found object with OBJECTID_UNKNOWN on the client with objectMode != 0x0!" << std::endl;
221          COUT(0) << "Possible reason for this error: Client created a synchronized object without the Server's approval." << std::endl;
222          COUT(0) << "Objects class: " << it->getIdentifier()->getName() << std::endl;
223          assert(false);
224        }
225      }
226      else
227      {
228        std::list<uint32_t>::iterator it2;
229        for (it2 = v1.begin(); it2 != v1.end(); ++it2)
230        {
231          if (it->getObjectID() == *it2)
232          {
233            COUT(0) << "Found duplicate objectIDs on the client!" << std::endl
234                    << "Are you sure you don't create a Sychnronisable objcect with 'new' \
235                        that doesn't have objectMode = 0x0?" << std::endl;
236            assert(false);
237          }
238        }
239        v1.push_back(it->getObjectID());
240      }
241    }
242  }
243#endif
244  return true;
245}
246
247
248uint32_t Gamestate::getSize() const
249{
250  assert(data_);
251  if(header_->isCompressed())
252    return header_->getCompSize()+GamestateHeader::getSize();
253  else
254  {
255    return header_->getDataSize()+GamestateHeader::getSize();
256  }
257}
258
259
260bool Gamestate::operator==(packet::Gamestate gs)
261{
262  uint8_t *d1 = data_+GamestateHeader::getSize();
263  uint8_t *d2 = gs.data_+GamestateHeader::getSize();
264  GamestateHeader* h1 = new GamestateHeader(data_);
265  GamestateHeader* h2 = new GamestateHeader(gs.data_);
266  assert(h1->getDataSize() == h2->getDataSize());
267  assert(!isCompressed());
268  assert(!gs.isCompressed());
269  return memcmp(d1, d2, h1->getDataSize())==0;
270}
271
272
273bool Gamestate::process()
274{
275  return GamestateHandler::addGamestate(this, getClientID());
276}
277
278
279bool Gamestate::compressData()
280{
281  assert(data_);
282  assert(!header_->isCompressed());
283  uLongf buffer = (uLongf)(((header_->getDataSize() + 12)*1.01)+1);
284  if(buffer==0)
285    return false;
286
287  uint8_t *ndata = new uint8_t[buffer+GamestateHeader::getSize()];
288  uint8_t *dest = ndata + GamestateHeader::getSize();
289  uint8_t *source = data_ + GamestateHeader::getSize();
290  int retval;
291  retval = compress( dest, &buffer, source, (uLong)(header_->getDataSize()) );
292  switch ( retval )
293  {
294    case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break;
295    case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl; return false;
296    case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl; return false;
297    case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl; return false;
298  }
299
300  //copy and modify header
301  GamestateHeader *temp = header_;
302  header_ = new GamestateHeader(ndata, temp);
303  delete temp;
304  //delete old data
305  delete[] data_;
306  //save new data
307  data_ = ndata;
308  header_->setCompSize( buffer );
309  header_->setCompressed( true );
310  COUT(0) << "gamestate compress datasize: " << header_->getDataSize() << " compsize: " << header_->getCompSize() << std::endl;
311  return true;
312}
313
314
315bool Gamestate::decompressData()
316{
317  assert(data_);
318  assert(header_->isCompressed());
319  COUT(4) << "GameStateClient: uncompressing gamestate. id: " << header_->getID() << ", baseid: " << header_->getBaseID() << ", datasize: " << header_->getDataSize() << ", compsize: " << header_->getCompSize() << std::endl;
320  uint32_t datasize = header_->getDataSize();
321  uint32_t compsize = header_->getCompSize();
322  uint32_t bufsize;
323  bufsize = datasize;
324  assert(bufsize!=0);
325  uint8_t *ndata = new uint8_t[bufsize + GamestateHeader::getSize()];
326  uint8_t *dest = ndata + GamestateHeader::getSize();
327  uint8_t *source = data_ + GamestateHeader::getSize();
328  int retval;
329  uLongf length=bufsize;
330  retval = uncompress( dest, &length, source, (uLong)compsize );
331  switch ( retval )
332  {
333    case Z_OK: COUT(5) << "successfully decompressed" << std::endl; break;
334    case Z_MEM_ERROR: COUT(1) << "not enough memory available" << std::endl; return false;
335    case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer" << std::endl; return false;
336    case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return false;
337  }
338
339  //copy over the header
340  GamestateHeader *temp = header_;
341  header_ = new GamestateHeader( data_, header_ );
342  delete temp;
343
344  if (this->bDataENetAllocated_)
345  {
346    // Memory was allocated by ENet. --> We let it be since enet_packet_destroy will
347    // deallocated it anyway. So data and packet stay together.
348    this->bDataENetAllocated_ = false;
349  }
350  else
351  {
352    // We allocated the memory in the first place (unlikely). So we destroy the old data
353    // and overwrite it with the new decompressed data.
354    delete[] this->data_;
355  }
356
357  //set new pointers
358  data_ = ndata;
359  header_->setCompressed( false );
360  assert(header_->getDataSize()==datasize);
361  assert(header_->getCompSize()==compsize);
362  return true;
363}
364
365
366Gamestate* Gamestate::diffVariables(Gamestate *base)
367{
368  assert(this && base); assert(data_ && base->data_);
369  assert(!header_->isCompressed() && !base->header_->isCompressed());
370  assert(!header_->isDiffed());
371
372
373  // *** first do a raw diff of the two gamestates
374
375  uint8_t *baseData = GAMESTATE_START(base->data_);
376  uint8_t *origData = GAMESTATE_START(this->data_);
377  uint32_t origLength = header_->getDataSize();
378  uint32_t baseLength = base->header_->getDataSize();
379
380  assert( origLength && baseLength );
381
382  uint8_t *nData = new uint8_t[origLength + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_]; // this is the maximum size needed in the worst case
383  uint8_t *dest = GAMESTATE_START(nData);
384
385  uint32_t baseOffset = 0; //offset in the diffed stream
386  uint32_t origOffset = 0; //offset in the new stream with removed 0's
387  std::vector<uint32_t>::iterator sizes = this->sizes_.begin();
388
389  while( origOffset < origLength )
390  {
391    //iterate through all objects
392
393    SynchronisableHeader h(origData+origOffset);
394
395    // Find (if possible) the current object in the datastream of the old gamestate
396    // Start at the current offset position
397    if(baseOffset >= baseLength)
398      baseOffset = 0;
399    uint8_t* temp = baseData + baseOffset;
400    uint32_t objectID = h.getObjectID();
401    assert(temp < baseData+baseLength);
402    assert(dest < nData + origLength + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_);
403    assert(sizes != this->sizes_.end());
404    while ( temp < baseData+baseLength )
405    {
406      SynchronisableHeader htemp(temp);
407      assert( htemp.getDataSize()!=0 );
408      if ( htemp.getObjectID() == objectID )
409      {
410        assert( h.getClassID() == htemp.getClassID() );
411        goto DODIFF;
412      }
413//       {
414//         SynchronisableHeader htemp2(temp+htemp.getDataSize()+SynchronisableHeader::getSize());
415//         if( temp+htemp.getDataSize()+SynchronisableHeader::getSize() < baseData+baseLength )
416//         {
417//           assert(htemp2.getClassID()<500);
418//           assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000);
419//           assert(htemp2.isDiffed()==false);
420//         }
421//       }
422      temp += htemp.getDataSize()+SynchronisableHeader::getSize();
423       
424    }
425    // If not found start looking at the beginning
426    assert( temp==baseData+baseLength );
427    temp = baseData;
428//     {
429//       SynchronisableHeader htemp2(temp);
430//       if( temp < baseData+baseLength )
431//       {
432//         assert(htemp2.getClassID()<500);
433//         assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000);
434//         assert(htemp2.isDiffed()==false);
435//       }
436//     }
437    while ( temp < baseData+baseOffset )
438    {
439      SynchronisableHeader htemp(temp);
440      if ( htemp.getObjectID() == objectID )
441      {
442        assert( h.getClassID() == htemp.getClassID() );
443        goto DODIFF;
444      }
445//       {
446//         SynchronisableHeader htemp2(temp+htemp.getDataSize()+SynchronisableHeader::getSize());
447//         if( temp+htemp.getDataSize()+SynchronisableHeader::getSize() < baseData+baseLength )
448//         {
449//           assert(htemp2.getClassID()<500);
450//           assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000);
451//           assert(htemp2.isDiffed()==false);
452//         }
453//       }
454      temp += htemp.getDataSize()+SynchronisableHeader::getSize();
455    }
456    // Object is new, thus never transmitted -> just copy over
457    goto DOCOPY;
458
459
460DODIFF:
461    {
462//       COUT(4) << "dodiff" << endl;
463//       if(baseOffset==0)
464//       {
465//         assert(origOffset==0);
466//       }
467      uint32_t objectOffset = SynchronisableHeader::getSize(); // offset inside the object in the origData and baseData
468      // Check whether the whole object stayed the same
469      if( memcmp( origData+origOffset+objectOffset, temp+objectOffset, h.getDataSize()) == 0 )
470      {
471//         COUT(4) << "skip object" << Synchronisable::getSynchronisable(h.getObjectID())->getIdentifier()->getName() << endl;
472        origOffset += objectOffset+ h.getDataSize(); // skip the whole object
473        baseOffset = temp + h.getDataSize()+SynchronisableHeader::getSize() - baseData;
474        sizes += Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables();
475      }
476      else
477      {
478//         if( Synchronisable::getSynchronisable(h.getObjectID())->getIdentifier()->getName() == "Bot" )
479//           COUT(0) << "blub" << endl;
480//         COUT(4) << "object diff: " << Synchronisable::getSynchronisable(h.getObjectID())->getIdentifier()->getName() << endl;
481//         COUT(4) << "diff " << h.getObjectID() << ":";
482        // Now start to diff the Object
483        SynchronisableHeaderLight h2(dest);
484        h2 = h; // copy over the objectheader
485        VariableID variableID = 0;
486        uint32_t newObjectOffset = SynchronisableHeaderLight::getSize();
487        // iterate through all variables
488        while( objectOffset < h.getDataSize()+SynchronisableHeader::getSize() )
489        {
490          // check whether variable changed and write id and copy over variable to the new stream
491          // otherwise skip variable
492          assert(sizes != this->sizes_.end());
493          uint32_t varSize = *sizes;
494          assert( varSize == Synchronisable::getSynchronisable(h.getObjectID())->getVarSize(variableID) );
495          if ( varSize != 0 )
496          {
497            if ( memcmp(origData+origOffset+objectOffset, temp+objectOffset, varSize) != 0 )
498            {
499//               COUT(4) << "copy variable" << endl;
500              *(VariableID*)(dest+newObjectOffset) = variableID; // copy over the variableID
501              newObjectOffset += sizeof(VariableID);
502              memcpy( dest+newObjectOffset, origData+origOffset+objectOffset, varSize );
503              newObjectOffset += varSize;
504              objectOffset += varSize;
505            }
506            else
507            {
508//               COUT(4) << "skip variable" << endl;
509              objectOffset += varSize;
510            }
511          }
512//           else
513//             COUT(4) << "varsize 0" << endl;
514
515          ++variableID;
516          ++sizes;
517        }
518       
519        if( Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables() != variableID )
520          sizes += Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables() - variableID;
521//         COUT(4) << endl;
522        h2.setDiffed(true);
523        h2.setDataSize(newObjectOffset-SynchronisableHeaderLight::getSize());
524        assert(objectOffset == h.getDataSize()+SynchronisableHeader::getSize());
525        origOffset += objectOffset;
526//         baseOffset += temp + h.getDataSize()+SynchronisableHeader::getSize() - baseData;
527        //baseOffset += objectOffset;
528//         SynchronisableHeader htemp(temp);
529//         baseOffset += SynchronisableHeader::getSize() + htemp.getDataSize();
530//         {
531//           SynchronisableHeader htemp2( baseData+(temp-baseData+objectOffset) );
532//           if( baseData+(temp-baseData+objectOffset) < baseData+baseLength )
533//           {
534//             assert(htemp2.getClassID()<500);
535//             assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000);
536//             assert(htemp2.isDiffed()==false);
537//           }
538//         }
539        baseOffset = temp-baseData + objectOffset;
540        dest += newObjectOffset;
541      }
542
543      continue;
544    }
545
546DOCOPY:
547    {
548//       COUT(4) << "docopy" << endl;
549      // Just copy over the whole Object
550      memcpy( dest, origData+origOffset, h.getDataSize()+SynchronisableHeader::getSize() );
551      dest += h.getDataSize()+SynchronisableHeader::getSize();
552      origOffset += h.getDataSize()+SynchronisableHeader::getSize();
553      assert( Synchronisable::getSynchronisable(h.getObjectID()) );
554//       COUT(4) << "copy " << h.getObjectID() << endl;
555//       COUT(4) << "copy " << h.getObjectID() << ":";
556      //sizes += Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables();
557      for( unsigned int i = 0; i < Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables(); ++i )
558      {
559//         COUT(4) << " " << *sizes;
560        ++sizes;
561      }
562//       COUT(4) << endl;
563      assert(sizes != this->sizes_.end() || origOffset>=origLength);
564      continue;
565    }
566  }
567
568
569  Gamestate *g = new Gamestate(nData, getClientID());
570  assert(g->header_);
571  *(g->header_) = *header_;
572  g->header_->setBaseID( base->getID() );
573  g->header_->setDataSize(dest - nData - GamestateHeader::getSize());
574  g->flags_=flags_;
575  g->packetDirection_ = packetDirection_;
576  assert(!g->isCompressed());
577  return g;
578}
579
580
581Gamestate* Gamestate::diffData(Gamestate *base)
582{
583  assert(this && base); assert(data_ && base->data_);
584  assert(!header_->isCompressed() && !base->header_->isCompressed());
585  assert(!header_->isDiffed());
586
587  uint8_t *basep = GAMESTATE_START(base->data_);
588  uint8_t *gs = GAMESTATE_START(this->data_);
589  uint32_t dest_length = header_->getDataSize();
590
591  if(dest_length==0)
592    return NULL;
593
594  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];
595  uint8_t *dest = GAMESTATE_START(ndata);
596
597  rawDiff( dest, gs, basep, header_->getDataSize(), base->header_->getDataSize() );
598#ifndef NDEBUG
599  uint8_t *dest2 = new uint8_t[dest_length];
600  rawDiff( dest2, dest, basep, header_->getDataSize(), base->header_->getDataSize() );
601  assert( memcmp( dest2, gs, dest_length) == 0 );
602  delete dest2;
603#endif
604
605  Gamestate *g = new Gamestate(ndata, getClientID());
606  assert(g->header_);
607  *(g->header_) = *header_;
608  g->header_->setDiffed( true );
609  g->header_->setBaseID( base->getID() );
610  g->flags_=flags_;
611  g->packetDirection_ = packetDirection_;
612  assert(g->isDiffed());
613  assert(!g->isCompressed());
614  return g;
615}
616
617
618Gamestate* Gamestate::undiff(Gamestate *base)
619{
620  assert(this && base); assert(data_ && base->data_);
621  assert(!header_->isCompressed() && !base->header_->isCompressed());
622  assert(header_->isDiffed());
623
624  uint8_t *basep = GAMESTATE_START(base->data_);
625  uint8_t *gs = GAMESTATE_START(this->data_);
626  uint32_t dest_length = header_->getDataSize();
627
628  if(dest_length==0)
629    return NULL;
630
631  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];
632  uint8_t *dest = ndata + GamestateHeader::getSize();
633
634  rawDiff( dest, gs, basep, header_->getDataSize(), base->header_->getDataSize() );
635
636  Gamestate *g = new Gamestate(ndata, getClientID());
637  assert(g->header_);
638  *(g->header_) = *header_;
639  g->header_->setDiffed( false );
640  g->flags_=flags_;
641  g->packetDirection_ = packetDirection_;
642  assert(!g->isDiffed());
643  assert(!g->isCompressed());
644  return g;
645}
646
647
648void Gamestate::rawDiff( uint8_t* newdata, uint8_t* data, uint8_t* basedata, uint32_t datalength, uint32_t baselength)
649{
650  uint64_t* gd = (uint64_t*)data;
651  uint64_t* bd = (uint64_t*)basedata;
652  uint64_t* nd = (uint64_t*)newdata;
653
654  unsigned int i;
655  for( i=0; i<datalength/8; i++ )
656  {
657    if( i<baselength/8 )
658      *(nd+i) = *(gd+i) ^ *(bd+i);  // xor the data
659    else
660      *(nd+i) = *(gd+i); // just copy over the data
661  }
662  unsigned int j;
663  // now process the rest (when datalength isn't a multiple of 4)
664  for( j = 8*(datalength/8); j<datalength; j++ )
665  {
666    if( j<baselength )
667      *(newdata+j) = *(data+j) ^ *(basedata+j); // xor
668    else
669      *(newdata+j) = *(data+j); // just copy
670  }
671  assert(j==datalength);
672}
673
674
675Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){
676  assert(data_);
677  std::list<obj>::iterator it;
678
679  // allocate memory for new data
680  uint8_t *gdata = new uint8_t[header_->getDataSize()+GamestateHeader::getSize()];
681  // create a gamestate out of it
682  Gamestate *gs = new Gamestate(gdata);
683  uint8_t *newdata = gdata + GamestateHeader::getSize();
684  uint8_t *origdata = GAMESTATE_START(data_);
685
686  //copy the GamestateHeader
687  assert(gs->header_);
688  *(gs->header_) = *header_;
689
690  uint32_t objectOffset;
691  unsigned int objectsize, destsize=0;
692  // TODO: Why is this variable not used?
693  //Synchronisable *object;
694
695  //call TrafficControl
696  TrafficControl::getInstance()->processObjectList( clientID, header_->getID(), dataVector_ );
697
698  //copy in the zeros
699//   std::list<obj>::iterator itt;
700//   COUT(0) << "myvector contains:";
701//   for ( itt=dataVector_.begin() ; itt!=dataVector_.end(); itt++ )
702//     COUT(0) << " " << (*itt).objID;
703//   COUT(0) << endl;
704  for(it=dataVector_.begin(); it!=dataVector_.end();){
705    SynchronisableHeader oldobjectheader(origdata);
706    SynchronisableHeader newobjectheader(newdata);
707    if ( (*it).objSize == 0 )
708    {
709      ++it;
710      continue;
711    }
712    objectsize = oldobjectheader.getDataSize()+SynchronisableHeader::getSize();
713    objectOffset=SynchronisableHeader::getSize(); //skip the size and the availableData variables in the objectheader
714    if ( (*it).objID == oldobjectheader.getObjectID() ){
715      memcpy(newdata, origdata, objectsize);
716      ++it;
717    }else{
718      newobjectheader = oldobjectheader;
719      memset(newdata+objectOffset, 0, objectsize-objectOffset);
720    }
721    newdata += objectsize;
722    origdata += objectsize;
723    destsize += objectsize;
724  }
725#ifndef NDEBUG
726  uint32_t origsize = destsize;
727  while ( origsize < header_->getDataSize() )
728  {
729    SynchronisableHeader oldobjectheader(origdata);
730    objectsize = oldobjectheader.getDataSize()+SynchronisableHeader::getSize();
731    origdata += objectsize;
732    origsize += objectsize;
733  }
734  assert(origsize==header_->getDataSize());
735  assert(destsize!=0);
736#endif
737  gs->header_->setDataSize( destsize );
738  return gs;
739}
740
741
742uint32_t Gamestate::calcGamestateSize(int32_t id, uint8_t mode)
743{
744  uint32_t size = 0;
745  uint32_t nrOfVariables = 0;
746    // get the start of the Synchronisable list
747  ObjectList<Synchronisable>::iterator it;
748    // get total size of gamestate
749  for(it = ObjectList<Synchronisable>::begin(); it; ++it){
750    size+=it->getSize(id, mode); // size of the actual data of the synchronisable
751    nrOfVariables += it->getNrOfVariables();
752  }
753//   COUT(0) << "allocating " << nrOfVariables << " ints" << endl;
754  this->sizes_.reserve(nrOfVariables);
755  return size;
756}
757
758
759} //namespace packet
760} //namespace orxonox
Note: See TracBrowser for help on using the repository browser.