Index: code/trunk/src/network/packet/Acknowledgement.cc
===================================================================
--- code/trunk/src/network/packet/Acknowledgement.cc	(revision 2171)
+++ code/trunk/src/network/packet/Acknowledgement.cc	(revision 2662)
@@ -64,4 +64,5 @@
 
 bool Acknowledgement::process(){
+COUT(0) << "processing ACK with ID: " << getAckID() << endl;
   bool b = GamestateHandler::ackGamestate(getAckID(), clientID_);
   delete this;
Index: code/trunk/src/network/packet/Acknowledgement.h
===================================================================
--- code/trunk/src/network/packet/Acknowledgement.h	(revision 2171)
+++ code/trunk/src/network/packet/Acknowledgement.h	(revision 2662)
@@ -32,4 +32,5 @@
 #include "Packet.h"
 
+const unsigned int ACKID_NACK = 0;
 
 namespace orxonox {
Index: code/trunk/src/network/packet/DeleteObjects.cc
===================================================================
--- code/trunk/src/network/packet/DeleteObjects.cc	(revision 2171)
+++ code/trunk/src/network/packet/DeleteObjects.cc	(revision 2662)
@@ -30,5 +30,5 @@
 #include "DeleteObjects.h"
 #include <enet/enet.h>
-#include "network/Synchronisable.h"
+#include "network/synchronisable/Synchronisable.h"
 #include "core/CoreIncludes.h"
 #include <assert.h>
@@ -61,9 +61,9 @@
   if(number==0)
     return false;
-  COUT(3) << "sending DeleteObjects: ";
+  COUT(4) << "sending DeleteObjects: ";
   unsigned int size = sizeof(ENUM::Type) + sizeof(uint32_t)*(number+1);
   data_ = new uint8_t[size];
   uint8_t *tdata = data_;
-  *(ENUM::Type *)(tdata) = ENUM::DeleteObjects;
+  *reinterpret_cast<ENUM::Type*>(tdata) = ENUM::DeleteObjects;
   tdata += sizeof(ENUM::Type);
   *(uint32_t *)tdata = number;
@@ -72,9 +72,9 @@
     unsigned int temp = Synchronisable::popDeletedObject();
 //     assert(temp<10000); //ugly hack
-    *(uint32_t *)(tdata) = temp;
-    COUT(3) << temp << " ";
+    *reinterpret_cast<uint32_t*>(tdata) = temp;
+    COUT(4) << temp << " ";
     tdata += sizeof(uint32_t);
   }
-  COUT(3) << std::endl;
+  COUT(4) << std::endl;
   return true;
 }
@@ -87,5 +87,5 @@
 bool DeleteObjects::process(){
   for(unsigned int i=0; i<*(unsigned int *)(data_+_QUANTITY); i++){
-    COUT(3) << "deleting object with id: " << *(uint32_t*)(data_+_OBJECTIDS+i*sizeof(uint32_t)) << std::endl;
+    COUT(4) << "deleting object with id: " << *(uint32_t*)(data_+_OBJECTIDS+i*sizeof(uint32_t)) << std::endl;
     Synchronisable::deleteObject( *(uint32_t*)(data_+_OBJECTIDS+i*sizeof(uint32_t)) );
   }
Index: code/trunk/src/network/packet/Gamestate.cc
===================================================================
--- code/trunk/src/network/packet/Gamestate.cc	(revision 2171)
+++ code/trunk/src/network/packet/Gamestate.cc	(revision 2662)
@@ -28,11 +28,13 @@
 
 #include "Gamestate.h"
-#include "network/ClientInformation.h"
-#include "network/GamestateHandler.h"
+#include "../GamestateHandler.h"
+#include "../synchronisable/Synchronisable.h"
+#include "../TrafficControl.h"
+#include "core/Core.h"
 #include "core/CoreIncludes.h"
 #include "core/Iterator.h"
 
 #include <zlib.h>
-#include <assert.h>
+#include <cassert>
 
 
@@ -42,14 +44,17 @@
 namespace packet {
 
-#define GAMESTATE_START(data) (data + sizeof(GamestateHeader))
-#define GAMESTATE_HEADER(data) ((GamestateHeader *)data)
-#define HEADER GAMESTATE_HEADER(data_)
-
+#define GAMESTATE_START(data) (data + GamestateHeader::getSize())
 
 #define PACKET_FLAG_GAMESTATE  ENET_PACKET_FLAG_RELIABLE
 
+// Gamestate::Gamestate()
+// {
+//   flags_ = flags_ | PACKET_FLAG_GAMESTATE;
+// }
+
 Gamestate::Gamestate()
 {
   flags_ = flags_ | PACKET_FLAG_GAMESTATE;
+  header_ = 0;
 }
 
@@ -58,4 +63,5 @@
 {
   flags_ = flags_ | PACKET_FLAG_GAMESTATE;
+  header_ = new GamestateHeader(data_);
 }
 
@@ -64,4 +70,12 @@
   flags_ = flags_ | PACKET_FLAG_GAMESTATE;
   data_=data;
+  header_ = new GamestateHeader(data_);
+}
+
+Gamestate::Gamestate(const Gamestate& g) :
+    Packet( *(Packet*)&g )
+{
+  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
+  header_ = new GamestateHeader(data_);
 }
 
@@ -73,24 +87,29 @@
 bool Gamestate::collectData(int id, uint8_t mode)
 {
-  unsigned int tempsize=0, currentsize=0;
+  assert(this->header_==0); // make sure the header didn't exist before
+  uint32_t tempsize=0, currentsize=0;
   assert(data_==0);
-  unsigned int size = calcGamestateSize(id, mode);
+  uint32_t size = calcGamestateSize(id, mode);
 
   COUT(4) << "G.ST.Man: producing gamestate with id: " << id << std::endl;
   if(size==0)
     return false;
-  data_ = new unsigned char[size + sizeof(GamestateHeader)];
+  data_ = new uint8_t[size + GamestateHeader::getSize()];
   if(!data_){
     COUT(2) << "GameStateManager: could not allocate memory" << std::endl;
     return false;
   }
+  
+  // create the header object
+  header_ = new GamestateHeader(data_);
 
   //start collect data synchronisable by synchronisable
   uint8_t *mem=data_;
-  mem+=sizeof(GamestateHeader);
+  mem += GamestateHeader::getSize();
   ObjectList<Synchronisable>::iterator it;
   for(it = ObjectList<Synchronisable>::begin(); it; ++it){
+    
+#ifndef NDEBUG
     tempsize=it->getSize(id, mode);
-
     if(currentsize+tempsize > size){
       assert(0); // if we don't use multithreading this part shouldn't be neccessary
@@ -98,16 +117,18 @@
       COUT(3) << "G.St.Man: need additional memory" << std::endl;
       ObjectList<Synchronisable>::iterator temp = it;
-      int addsize=tempsize;
+      uint32_t addsize=tempsize;
       while(++temp)
         addsize+=temp->getSize(id, mode);
-      data_ = (uint8_t *)realloc(data_, sizeof(GamestateHeader) + currentsize + addsize);
+      data_ = (uint8_t *)realloc(data_, GamestateHeader::getSize() + currentsize + addsize);
       if(!data_)
         return false;
       size = currentsize+addsize;
     }// stop allocate additional memory
-
+#endif
 
     //if(it->doSelection(id))
-    dataMap_[mem-data_]=(*it);  // save the mem location of the synchronisable data
+    if ( it->doSync( id, mode ) )
+      dataMap_.push_back( obj(it->getObjectID(), it->getCreatorID(), tempsize, mem-data_) );
+//     dataMap_[mem-data_]=(*it);  // save the mem location of the synchronisable data
     if(!it->getData(mem, id, mode))
       return false; // mem pointer gets automatically increased because of call by reference
@@ -118,10 +139,9 @@
 
   //start write gamestate header
-  HEADER->packetType = ENUM::Gamestate;
-  HEADER->datasize = currentsize;
-  HEADER->id = id;
-  HEADER->diffed = false;
-  HEADER->complete = true;
-  HEADER->compressed = false;
+  header_->setDataSize( currentsize );
+  header_->setID( id );
+  header_->setDiffed( false );
+  header_->setComplete( true );
+  header_->setCompressed( false );
   //stop write gamestate header
 
@@ -133,8 +153,9 @@
 bool Gamestate::spreadData(uint8_t mode)
 {
-  assert(data_);
-  assert(!HEADER->compressed);
-  assert(!HEADER->diffed);
-  uint8_t *mem=data_+sizeof(GamestateHeader);
+  COUT(4) << "processing gamestate with id " << header_->getID() << endl;
+  assert(data_);
+  assert(!header_->isCompressed());
+  assert(!header_->isDiffed());
+  uint8_t *mem=data_+GamestateHeader::getSize();
     // get the start of the Synchronisable list
   //ObjectList<Synchronisable>::iterator it=ObjectList<Synchronisable>::begin();
@@ -142,11 +163,21 @@
 
   // update the data of the objects we received
-  while(mem < data_+sizeof(GamestateHeader)+HEADER->datasize){
-    synchronisableHeader *objectheader = (synchronisableHeader*)mem;
-
-    s = Synchronisable::getSynchronisable( objectheader->objectID );
+  while(mem < data_+GamestateHeader::getSize()+header_->getDataSize()){
+    SynchronisableHeader objectheader(mem);
+
+    s = Synchronisable::getSynchronisable( objectheader.getObjectID() );
     if(!s)
     {
-      Synchronisable::fabricate(mem, mode);
+      if (!Core::isMaster())
+      {
+        Synchronisable::fabricate(mem, mode);
+      }
+      else
+      {
+        mem += objectheader.getDataSize();
+      }
+//         COUT(0) << "could not fabricate synchronisable: " << objectheader->objectID << " classid: " << objectheader->classID << " creator: " << objectheader->creatorID << endl;
+//       else
+//         COUT(0) << "fabricated: " << objectheader->objectID << " classid: " << objectheader->classID << " creator: "  << objectheader->creatorID << endl;
     }
     else
@@ -157,30 +188,50 @@
   }
 
+   // In debug mode, check first, whether there are no duplicate objectIDs
+#ifndef NDEBUG
+  ObjectList<Synchronisable>::iterator it;
+  for (it = ObjectList<Synchronisable>::begin(); it != ObjectList<Synchronisable>::end(); ++it) {
+    if (it->getObjectID() == OBJECTID_UNKNOWN) {
+      if (it->objectMode_ != 0x0) {
+        COUT(0) << "Found object with OBJECTID_UNKNOWN on the client with objectMode != 0x0!" << std::endl;
+        COUT(0) << "Possible reason for this error: Client created a synchronized object without the Server's approval." << std::endl;
+        COUT(0) << "Objects class: " << it->getIdentifier()->getName() << std::endl;
+        assert(false);
+      }
+    }
+    else {
+      ObjectList<Synchronisable>::iterator it2;
+      for (it2 = ObjectList<Synchronisable>::begin(); it2 != ObjectList<Synchronisable>::end(); ++it2) {
+        if (it->getObjectID() == it2->getObjectID() && *it != *it2) {
+           COUT(0) << "Found duplicate objectIDs on the client!" << std::endl
+                   << "Are you sure you don't create a Sychnronisable objcect with 'new' \
+                       that doesn't have objectMode = 0x0?" << std::endl;
+           assert(false);
+        }
+      }
+    }
+  }
+#endif
+
   return true;
 }
 
-
-
-int Gamestate::getID(){
-  return HEADER->id;
-}
-
-unsigned int Gamestate::getSize() const
-{
-  assert(data_);
-  if(HEADER->compressed)
-    return HEADER->compsize+sizeof(GamestateHeader);
+uint32_t Gamestate::getSize() const
+{
+  assert(data_);
+  if(header_->isCompressed())
+    return header_->getCompSize()+GamestateHeader::getSize();
   else
   {
-    return HEADER->datasize+sizeof(GamestateHeader);
+    return header_->getDataSize()+GamestateHeader::getSize();
   }
 }
 
 bool Gamestate::operator==(packet::Gamestate gs){
-  uint8_t *d1 = data_+sizeof(GamestateHeader);
-  uint8_t *d2 = gs.data_+sizeof(GamestateHeader);
+  uint8_t *d1 = data_+GamestateHeader::getSize();
+  uint8_t *d2 = gs.data_+GamestateHeader::getSize();
   assert(!isCompressed());
   assert(!gs.isCompressed());
-  while(d1<data_+HEADER->datasize)
+  while(d1<data_+header_->getDataSize())
   {
     if(*d1!=*d2)
@@ -201,16 +252,16 @@
 bool Gamestate::compressData()
 {
-  assert(HEADER);
-  assert(!HEADER->compressed);
-  uLongf buffer = (uLongf)(((HEADER->datasize + 12)*1.01)+1);
+  assert(data_);
+  assert(!header_->isCompressed());
+  uLongf buffer = (uLongf)(((header_->getDataSize() + 12)*1.01)+1);
   if(buffer==0)
     return false;
 
-  uint8_t *ndata = new uint8_t[buffer+sizeof(GamestateHeader)];
-  uint8_t *dest = GAMESTATE_START(ndata);
+  uint8_t *ndata = new uint8_t[buffer+GamestateHeader::getSize()];
+  uint8_t *dest = ndata + GamestateHeader::getSize();
   //unsigned char *dest = new unsigned char[buffer];
-  uint8_t *source = GAMESTATE_START(data_);
+  uint8_t *source = data_ + GamestateHeader::getSize();
   int retval;
-  retval = compress( dest, &buffer, source, (uLong)(HEADER->datasize) );
+  retval = compress( dest, &buffer, source, (uLong)(header_->getDataSize()) );
   switch ( retval ) {
     case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break;
@@ -219,45 +270,32 @@
     case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl; return false;
   }
-#ifndef NDEBUG
-  //decompress and compare the start and the decompressed data
-  uint8_t *rdata = new uint8_t[HEADER->datasize+sizeof(GamestateHeader)];
-  uint8_t *d2 = GAMESTATE_START(rdata);
-  uLongf length2 = HEADER->datasize;
-  uncompress(d2, &length2, dest, buffer);
-  for(unsigned int i=0; i<HEADER->datasize; i++){
-    assert(*(source+i)==*(d2+i));
-  }
-  delete[] rdata;
-#endif
 
   //copy and modify header
-#ifndef NDEBUG
-  HEADER->crc32 = calcCRC(data_+sizeof(GamestateHeader), HEADER->datasize);
-#endif
-  *GAMESTATE_HEADER(ndata) = *HEADER;
+  GamestateHeader *temp = header_;
+  header_ = new GamestateHeader(ndata, temp);
+  delete temp;
   //delete old data
   delete[] data_;
   //save new data
   data_ = ndata;
-  HEADER->compsize = buffer;
-  HEADER->compressed = true;
-  assert(HEADER->compressed);
-  COUT(4) << "gamestate compress datasize: " << HEADER->datasize << " compsize: " << HEADER->compsize << std::endl;
+  header_->setCompSize( buffer );
+  header_->setCompressed( true );
+  COUT(5) << "gamestate compress datasize: " << header_->getDataSize() << " compsize: " << header_->getCompSize() << std::endl;
   return true;
 }
 bool Gamestate::decompressData()
 {
-  assert(HEADER);
-  assert(HEADER->compressed);
-  COUT(4) << "GameStateClient: uncompressing gamestate. id: " << HEADER->id << ", baseid: " << HEADER->base_id << ", datasize: " << HEADER->datasize << ", compsize: " << HEADER->compsize << std::endl;
-  unsigned int datasize = HEADER->datasize;
-  unsigned int compsize = HEADER->compsize;
-  unsigned int bufsize;
+  assert(data_);
+  assert(header_->isCompressed());
+  COUT(4) << "GameStateClient: uncompressing gamestate. id: " << header_->getID() << ", baseid: " << header_->getBaseID() << ", datasize: " << header_->getDataSize() << ", compsize: " << header_->getCompSize() << std::endl;
+  uint32_t datasize = header_->getDataSize();
+  uint32_t compsize = header_->getCompSize();
+  uint32_t bufsize;
 //  assert(compsize<=datasize);
   bufsize = datasize;
   assert(bufsize!=0);
-  uint8_t *ndata = new uint8_t[bufsize + sizeof(GamestateHeader)];
-  uint8_t *dest = ndata + sizeof(GamestateHeader);
-  uint8_t *source = data_ + sizeof(GamestateHeader);
+  uint8_t *ndata = new uint8_t[bufsize + GamestateHeader::getSize()];
+  uint8_t *dest = ndata + GamestateHeader::getSize();
+  uint8_t *source = data_ + GamestateHeader::getSize();
   int retval;
   uLongf length=bufsize;
@@ -269,10 +307,9 @@
     case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return false;
   }
-#ifndef NDEBUG
-  assert(HEADER->crc32==calcCRC(ndata+sizeof(GamestateHeader), HEADER->datasize));
-#endif
 
   //copy over the header
-  *GAMESTATE_HEADER(ndata) = *HEADER;
+  GamestateHeader *temp = header_;
+  header_ = new GamestateHeader( data_, header_ );
+  delete temp;
 
   if (this->bDataENetAllocated_){
@@ -289,7 +326,7 @@
   //set new pointers
   data_ = ndata;
-  HEADER->compressed = false;
-  assert(HEADER->datasize==datasize);
-  assert(HEADER->compsize==compsize);
+  header_->setCompressed( false );
+  assert(header_->getDataSize()==datasize);
+  assert(header_->getCompSize()==compsize);
   return true;
 }
@@ -297,23 +334,24 @@
 Gamestate *Gamestate::diff(Gamestate *base)
 {
-  assert(HEADER);
-  assert(!HEADER->compressed);
-  assert(!HEADER->diffed);
+  assert(data_);
+  assert(!header_->isCompressed());
+  assert(!header_->isDiffed());
+  GamestateHeader diffHeader(base->data_);
   //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;
   uint8_t *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_);
-  unsigned int of=0; // pointers offset
-  unsigned int dest_length=0;
-  dest_length=HEADER->datasize;
+  uint32_t of=0; // pointers offset
+  uint32_t dest_length=0;
+  dest_length=header_->getDataSize();
   if(dest_length==0)
     return NULL;
-  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+sizeof(GamestateHeader)];
-  uint8_t *dest = ndata + sizeof(GamestateHeader);
-  while(of < GAMESTATE_HEADER(base->data_)->datasize && of < HEADER->datasize){
+  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];
+  uint8_t *dest = ndata + GamestateHeader::getSize();
+  while(of < diffHeader.getDataSize() && of < header_->getDataSize()){
     *(dest+of)=*(basep+of)^*(gs+of); // do the xor
     ++of;
   }
-  if(GAMESTATE_HEADER(base->data_)->datasize!=HEADER->datasize){
+  if(diffHeader.getDataSize()!=header_->getDataSize()){
     uint8_t n=0;
-    if(GAMESTATE_HEADER(base->data_)->datasize < HEADER->datasize){
+    if(diffHeader.getDataSize() < header_->getDataSize()){
       while(of<dest_length){
         *(dest+of)=n^*(gs+of);
@@ -323,8 +361,8 @@
   }
 
-  *GAMESTATE_HEADER(ndata) = *HEADER;
-  GAMESTATE_HEADER(ndata)->diffed = true;
-  GAMESTATE_HEADER(ndata)->base_id = base->getID();
   Gamestate *g = new Gamestate(ndata, getClientID());
+  *(g->header_) = *header_;
+  g->header_->setDiffed( true );
+  g->header_->setBaseID( base->getID() );
   g->flags_=flags_;
   g->packetDirection_ = packetDirection_;
@@ -332,203 +370,96 @@
 }
 
-Gamestate* Gamestate::doSelection(unsigned int clientID){
-  assert(data_);
-  std::map<unsigned int, Synchronisable *>::iterator it;
+Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){
+  assert(data_);
+  std::list<obj>::iterator it;
 
   // allocate memory for new data
-  uint8_t *gdata = new uint8_t[HEADER->datasize+sizeof(GamestateHeader)];
+  uint8_t *gdata = new uint8_t[header_->getDataSize()+GamestateHeader::getSize()];
   // create a gamestate out of it
   Gamestate *gs = new Gamestate(gdata);
-  uint8_t *newdata = gdata + sizeof(GamestateHeader);
+  uint8_t *newdata = gdata + GamestateHeader::getSize();
   uint8_t *origdata = GAMESTATE_START(data_);
 
   //copy the GamestateHeader
-  *(GamestateHeader*)gdata = *HEADER;
-
-  synchronisableHeader *oldobjectheader, *newobjectheader;
-  unsigned int objectOffset;
+  assert(gs->header_);
+  *(gs->header_) = *header_;
+
+  uint32_t objectOffset;
+  unsigned int objectsize, destsize=0;
+  // TODO: Why is this variable not used?
+  //Synchronisable *object;
+
+  //call TrafficControl
+  TrafficControl::getInstance()->processObjectList( clientID, header_->getID(), &dataMap_ );
 
   //copy in the zeros
-  for(it=dataMap_.begin(); it!=dataMap_.end(); it++){
-    oldobjectheader = (synchronisableHeader*)origdata;
-    newobjectheader = (synchronisableHeader*)newdata;
-    unsigned int objectsize = oldobjectheader->size;
-    assert(it->second->objectID==oldobjectheader->objectID);
-    *newobjectheader = *oldobjectheader;
-    objectOffset=sizeof(synchronisableHeader); //skip the size and the availableData variables in the objectheader
-    if(it->second->doSelection(HEADER->id)){
-      assert(newobjectheader->dataAvailable==true);
-      memcpy(newdata+objectOffset, origdata+objectOffset, objectsize-objectOffset);
+  for(it=dataMap_.begin(); it!=dataMap_.end();){
+//    if((*it).objSize==0)
+//      continue;
+//    if(it->second->getSize(HEADER->id)==0) // merged from objecthierarchy2, doesn't work anymore; TODO: change this
+//      continue;                            // merged from objecthierarchy2, doesn't work anymore; TODO: change this
+    SynchronisableHeader oldobjectheader(origdata);
+    SynchronisableHeader newobjectheader(newdata);
+    if ( (*it).objSize == 0 )
+    {
+      ++it;
+      continue;
+    }
+//     object = Synchronisable::getSynchronisable( (*it).objID );
+//     assert(object->objectID == oldobjectheader->objectID);
+    objectsize = oldobjectheader.getDataSize();
+    objectOffset=SynchronisableHeader::getSize(); //skip the size and the availableData variables in the objectheader
+    if ( (*it).objID == oldobjectheader.getObjectID() ){
+      memcpy(newdata, origdata, objectsize);
+      assert(newobjectheader.isDataAvailable()==true);
+      ++it;
     }else{
-      newobjectheader->dataAvailable=false;
+      newobjectheader = oldobjectheader;
+      newobjectheader.setDataAvailable(false);
       memset(newdata+objectOffset, 0, objectsize-objectOffset);
-      assert(objectOffset==objectsize);
     }
     newdata += objectsize;
     origdata += objectsize;
-  }
+    destsize += objectsize;
+  }
+#ifndef NDEBUG
+  uint32_t origsize = destsize;
+  while ( origsize < header_->getDataSize() )
+  {
+    SynchronisableHeader oldobjectheader(origdata);
+    objectsize = oldobjectheader.getDataSize();
+    origdata += objectsize;
+    origsize += objectsize;
+  }
+  assert(origsize==header_->getDataSize());
+  assert(destsize!=0);
+#endif
+  gs->header_->setDataSize( destsize );
   return gs;
 }
 
 
-Gamestate* Gamestate::intelligentDiff(Gamestate *base, unsigned int clientID){
-  // asserts
-  assert(data_);
-  assert(base->data_);
-  assert(!GAMESTATE_HEADER(base->data_)->diffed);
-  assert(!GAMESTATE_HEADER(base->data_)->compressed);
-  assert(!HEADER->compressed);
-  assert(!HEADER->diffed);
-
-  //preparations
-  std::map<unsigned int, Synchronisable *>::iterator it;
-  uint8_t *origdata, *basedata, *destdata, *ndata;
-  unsigned int objectOffset, streamOffset=0;    //data offset
-  unsigned int minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize;
-  synchronisableHeader *origheader;
-  synchronisableHeader *destheader;
-
-  origdata = GAMESTATE_START(this->data_);
-  basedata = GAMESTATE_START(base->data_);
-  ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)];
-  destdata = ndata + sizeof(GamestateHeader);
-
-  // do the diff
-  for(it=dataMap_.begin(); it!=dataMap_.end(); it++){
-    assert(streamOffset<HEADER->datasize);
-    bool sendData = it->second->doSelection(HEADER->id);
-    origheader = (synchronisableHeader *)(origdata+streamOffset);
-    destheader = (synchronisableHeader *)(destdata+streamOffset);
-
-    //copy and partially diff the object header
-    assert(sizeof(synchronisableHeader)==3*sizeof(unsigned int)+sizeof(bool));
-    *(uint32_t*)destdata = *(uint32_t*)origdata; //size (do not diff)
-    *(bool*)(destdata+sizeof(uint32_t)) = sendData;
-    if(sendData){
-      *(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)
-      *(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)
-    }else{
-      *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = 0;
-      *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = 0;
-    }
-    objectOffset=sizeof(synchronisableHeader);
-    streamOffset+=sizeof(synchronisableHeader);
-
-    //now handle the object data or fill with zeros
-    while(objectOffset<origheader->size ){
-
-      if(sendData && streamOffset<minsize)
-        *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor
-      else if(sendData)
-        *(destdata+objectOffset)=((uint8_t)0)^*(origdata+objectOffset); // xor with 0 (basestream is too short)
-      else
-        *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered
-
-      objectOffset++;
-      streamOffset++;
-    }
-    destdata+=objectOffset;
-    origdata+=objectOffset;
-    basedata+=objectOffset;
-  }
-
-  //copy over the gamestate header and set the diffed flag
-  *(GamestateHeader *)ndata = *HEADER; //copy over the header
-  Gamestate *gs = new Gamestate(ndata);
-  GAMESTATE_HEADER(ndata)->diffed=true;
-  return gs;
-}
-
-Gamestate* Gamestate::intelligentUnDiff(Gamestate *base){
-  // asserts
-  assert(data_);
-  assert(base->data_);
-  assert(!GAMESTATE_HEADER(base->data_)->diffed);
-  assert(!GAMESTATE_HEADER(base->data_)->compressed);
-  assert(!HEADER->compressed);
-  assert(HEADER->diffed);
-
-  //preparations
-  std::map<unsigned int, Synchronisable *>::iterator it;
-  uint8_t *origdata, *basedata, *destdata, *ndata;
-  unsigned int objectOffset, streamOffset=0;    //data offset
-  unsigned int minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize;
-  synchronisableHeader *origheader;
-  synchronisableHeader *destheader;
-
-  origdata = GAMESTATE_START(this->data_);
-  basedata = GAMESTATE_START(base->data_);
-  ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)];
-  destdata = ndata + sizeof(GamestateHeader);
-
-  // do the undiff
-  for(it=dataMap_.begin(); it!=dataMap_.end(); it++){
-    assert(streamOffset<HEADER->datasize);
-    origheader = (synchronisableHeader *)(origdata+streamOffset);
-    destheader = (synchronisableHeader *)(destdata+streamOffset);
-    bool sendData;
-
-    //copy and partially diff the object header
-    assert(sizeof(synchronisableHeader)==3*sizeof(unsigned int)+sizeof(bool));
-    *(unsigned int*)destdata = *(unsigned int*)origdata; //size (do not diff)
-    *(bool*)(destdata+sizeof(unsigned int)) = *(bool*)(origdata+sizeof(unsigned int));
-    sendData = *(bool*)(origdata+sizeof(unsigned int));
-    if(sendData){
-      *(unsigned int*)(destdata+sizeof(unsigned int)+sizeof(bool)) = *(unsigned int*)(basedata+sizeof(unsigned int)+sizeof(bool)) ^ *(unsigned int*)(origdata+sizeof(unsigned int)+sizeof(bool)); //objectid (diff it)
-      *(unsigned int*)(destdata+2*sizeof(unsigned int)+sizeof(bool)) = *(unsigned int*)(basedata+2*sizeof(unsigned int)+sizeof(bool)) ^ *(unsigned int*)(origdata+2*sizeof(unsigned int)+sizeof(bool)); //classid (diff it)
-    }else{
-      *(unsigned int*)(destdata+sizeof(unsigned int)+sizeof(bool)) = 0;
-      *(unsigned int*)(destdata+2*sizeof(unsigned int)+sizeof(bool)) = 0;
-    }
-    objectOffset=sizeof(synchronisableHeader);
-    streamOffset+=sizeof(synchronisableHeader);
-
-    //now handle the object data or fill with zeros
-    while(objectOffset<origheader->size ){
-
-      if(sendData && streamOffset<minsize)
-        *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor
-      else if(sendData)
-        *(destdata+objectOffset)=((unsigned char)0)^*(origdata+objectOffset); // xor with 0 (basestream is too short)
-      else
-        *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered
-
-      objectOffset++;
-      streamOffset++;
-    }
-    destdata+=objectOffset;
-    origdata+=objectOffset;
-    basedata+=objectOffset;
-  }
-
-  //copy over the gamestate header and set the diffed flag
-  *(GamestateHeader *)ndata = *HEADER; //copy over the header
-  Gamestate *gs = new Gamestate(ndata);
-  GAMESTATE_HEADER(ndata)->diffed=false;
-  return gs;
-}
-
 Gamestate *Gamestate::undiff(Gamestate *base)
 {
-  assert(this && base);assert(HEADER);
-  assert(HEADER->diffed);
-  assert(!HEADER->compressed && !GAMESTATE_HEADER(base->data_)->compressed);
+  assert(this && base);assert(data_);
+  assert(header_->isDiffed());
+  assert(!header_->isCompressed() && !base->header_->isCompressed());
   //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;
   uint8_t *basep = GAMESTATE_START(base->data_);
   uint8_t *gs = GAMESTATE_START(this->data_);
-  unsigned int of=0; // pointers offset
-  unsigned int dest_length=0;
-  dest_length=HEADER->datasize;
+  uint32_t of=0; // pointers offset
+  uint32_t dest_length=0;
+  dest_length=header_->getDataSize();
   if(dest_length==0)
     return NULL;
-  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+sizeof(GamestateHeader)];
-  uint8_t *dest = ndata + sizeof(GamestateHeader);
-  while(of < GAMESTATE_HEADER(base->data_)->datasize && of < HEADER->datasize){
+  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];
+  uint8_t *dest = ndata + GamestateHeader::getSize();
+  while(of < base->header_->getDataSize() && of < header_->getDataSize()){
     *(dest+of)=*(basep+of)^*(gs+of); // do the xor
     ++of;
   }
-  if(GAMESTATE_HEADER(base->data_)->datasize!=HEADER->datasize){
+  if(base->header_->getDataSize()!=header_->getDataSize()){
     uint8_t n=0;
-    if(GAMESTATE_HEADER(base->data_)->datasize < HEADER->datasize){
+    if(base->header_->getDataSize() < header_->getDataSize()){
       while(of < dest_length){
         *(dest+of)=n^*(gs+of);
@@ -537,7 +468,8 @@
     }
   }
-  *GAMESTATE_HEADER(ndata) = *HEADER;
-  GAMESTATE_HEADER(ndata)->diffed = false;
   Gamestate *g = new Gamestate(ndata, getClientID());
+  assert(g->header_);
+  *(g->header_) = *header_;
+  g->header_->setDiffed( false );
   g->flags_=flags_;
   g->packetDirection_ = packetDirection_;
@@ -548,7 +480,7 @@
 
 
-unsigned int Gamestate::calcGamestateSize(unsigned int id, uint8_t mode)
-{
-  unsigned int size=0;
+uint32_t Gamestate::calcGamestateSize(int32_t id, uint8_t mode)
+{
+  uint32_t size=0;
     // get the start of the Synchronisable list
   ObjectList<Synchronisable>::iterator it;
@@ -556,31 +488,7 @@
   for(it = ObjectList<Synchronisable>::begin(); it; ++it)
     size+=it->getSize(id, mode); // size of the actual data of the synchronisable
-//  size+=sizeof(GamestateHeader);
   return size;
 }
 
-/**
- * This function removes a Synchronisable out of the universe
- * @param it iterator of the list pointing to the object
- * @return iterator pointing to the next object in the list
- */
-  void Gamestate::removeObject(ObjectList<Synchronisable>::iterator &it) {
-    ObjectList<Synchronisable>::iterator temp=it;
-    ++it;
-    delete  *temp;
-  }
-
-  bool Gamestate::isDiffed(){
-    return HEADER->diffed;
-  }
-
-  bool Gamestate::isCompressed(){
-    return HEADER->compressed;
-  }
-
-  int Gamestate::getBaseID(){
-    return HEADER->base_id;
-  }
-}
-
-}
+} //namespace packet
+} //namespace orxonox
Index: code/trunk/src/network/packet/Gamestate.h
===================================================================
--- code/trunk/src/network/packet/Gamestate.h	(revision 2171)
+++ code/trunk/src/network/packet/Gamestate.h	(revision 2662)
@@ -31,9 +31,11 @@
 #define NETWORK_PACKETGAMESTATE_H
 
-#include "../NetworkPrereqs.h"
+#include "network/NetworkPrereqs.h"
 
 #include "Packet.h"
-#include "network/Synchronisable.h"
+#include "network/TrafficControl.h"
+#include "core/CoreIncludes.h"
 #include <map>
+#include <list>
 #ifndef NDEBUG
 #include "util/CRC32.h"
@@ -44,16 +46,55 @@
 namespace packet {
 
-struct _NetworkExport GamestateHeader{
-  ENUM::Type packetType;
-  int32_t id; // id of the gamestate
-  uint32_t compsize;
-  uint32_t datasize;
-  int32_t base_id; // id of the base-gamestate diffed from
-  bool diffed:1; // wheter diffed or not
-  bool complete:1; // wheter it is a complete gamestate or only partial
-  bool compressed:1;
-#ifndef NDEBUG
-  uint32_t crc32;
-#endif
+class _NetworkExport GamestateHeader{
+  public:
+    GamestateHeader(uint8_t *data){ assert(data); data_ = data; *(uint32_t*)data_ = ENUM::Gamestate; }
+    GamestateHeader(uint8_t *data, GamestateHeader* h)
+    { assert(data); data_=data; memcpy(data_, h->data_, getSize()); }
+    static inline uint32_t getSize()
+    { return 21; }
+
+    inline int32_t getID() const
+    { assert(data_); return *(int32_t*)(data_+4); }
+    inline void setID(int32_t id)
+    { assert(data_); *(int32_t*)(data_+4) = id; }
+
+    inline int32_t getBaseID() const
+    { assert(data_); return *(int32_t*)(data_+8); }
+    inline void setBaseID(int32_t id)
+    { assert(data_); *(int32_t*)(data_+8) = id; }
+
+    inline uint32_t getDataSize() const
+    { assert(data_); return *(uint32_t*)(data_+12); }
+    inline void setDataSize(uint32_t size)
+    { assert(data_); *(uint32_t*)(data_+12) = size; }
+
+    inline uint32_t getCompSize() const
+    { assert(data_); return *(uint32_t*)(data_+16); }
+    inline void setCompSize(uint32_t size)
+    { assert(data_); *(uint32_t*)(data_+16) = size; }
+
+    inline bool isDiffed() const
+    { assert(data_); return *(int8_t*)(data_+20) & 0x1; }
+    inline void setDiffed(bool b)
+    { assert(data_); *(int8_t*)(data_+20) = (b<<0) | (*(int8_t*)(data_+20) & 0x6 ); }
+
+    inline bool isComplete() const
+    { assert(data_); return *(int8_t*)(data_+20) & 0x2; }
+    inline void setComplete(bool b)
+    { assert(data_); *(int8_t*)(data_+20) = (b<<1) | (*(int8_t*)(data_+20) & 0x5 ); }
+
+    inline bool isCompressed() const
+    { assert(data_); return *(int8_t*)(data_+20) & 0x4; }
+    inline void setCompressed(bool b)
+    { assert(data_); *(int8_t*)(data_+20) = (b<<2) | (*(int8_t*)(data_+20) & 0x3 ); }
+
+    inline void operator=(GamestateHeader& h)
+    { assert(data_); assert(h.data_); memcpy( data_, h.data_, getSize()); }
+  private:
+    uint8_t *data_;
+//#define GAMESTATE_START(data) (data + sizeof(GamestateHeader))
+//#define GAMESTATE_HEADER(data) ((GamestateHeader *)data)
+//#define HEADER GAMESTATE_HEADER(data_)
+
 };
 
@@ -66,4 +107,5 @@
     Gamestate(uint8_t *data, unsigned int clientID);
     Gamestate(uint8_t *data);
+    Gamestate(const Gamestate& g);
 
     ~Gamestate();
@@ -71,13 +113,11 @@
     bool collectData(int id, uint8_t mode=0x0);
     bool spreadData( uint8_t mode=0x0);
-    int getID();
-    bool isDiffed();
-    bool isCompressed();
-    int getBaseID();
+    inline int32_t getID() const { return header_->getID(); }
+    inline bool isDiffed() const { return header_->isDiffed(); }
+    inline bool isCompressed() const { return header_->isCompressed(); }
+    inline int32_t getBaseID() const { return header_->getBaseID(); }
     Gamestate *diff(Gamestate *base);
-    Gamestate* intelligentDiff(Gamestate *base, unsigned int clientID);
     Gamestate *undiff(Gamestate *base);
-    Gamestate* intelligentUnDiff(Gamestate *base);
-    Gamestate* doSelection(unsigned int clientID);
+    Gamestate* doSelection(unsigned int clientID, unsigned int targetSize);
     bool compressData();
     bool decompressData();
@@ -85,12 +125,12 @@
     // Packet functions
   private:
-    virtual unsigned int getSize() const;
-    virtual bool process();
+    virtual uint32_t getSize() const;
+    virtual inline bool process();
 
     bool operator ==(packet::Gamestate gs);
   private:
-    unsigned int calcGamestateSize(unsigned int id, uint8_t mode=0x0);
-    void removeObject(ObjectListIterator<Synchronisable> &it);
-    std::map<unsigned int, Synchronisable*> dataMap_;
+    uint32_t calcGamestateSize(int32_t id, uint8_t mode=0x0);
+    std::list<obj> dataMap_;
+    GamestateHeader* header_;
 };
 
Index: code/trunk/src/network/packet/Packet.cc
===================================================================
--- code/trunk/src/network/packet/Packet.cc	(revision 2171)
+++ code/trunk/src/network/packet/Packet.cc	(revision 2662)
@@ -129,7 +129,4 @@
       return false;
     }
-    // Assures we don't create a packet and destroy it right after in another thread
-    // without having a reference in the packetMap_
-    boost::recursive_mutex::scoped_lock lock(Packet::packetMap_mutex);
     // We deliver ENet the data address so that it doesn't memcpy everything again.
     // --> We have to delete data_ ourselves!
@@ -138,5 +135,10 @@
     // Add the packet to a global list so we can access it again once enet calls our
     // deletePacket method. We can of course only give a one argument function to the ENet C library.
-    packetMap_[(size_t)(void*)enetPacket_] = this;
+    {
+      // Assures we don't create a packet and destroy it right after in another thread
+      // without having a reference in the packetMap_
+      boost::recursive_mutex::scoped_lock lock(Packet::packetMap_mutex);
+      packetMap_[(size_t)(void*)enetPacket_] = this;
+    }
   }
 #ifndef NDEBUG
Index: code/trunk/src/network/packet/Welcome.cc
===================================================================
--- code/trunk/src/network/packet/Welcome.cc	(revision 2171)
+++ code/trunk/src/network/packet/Welcome.cc	(revision 2662)
@@ -32,5 +32,5 @@
 #include "Welcome.h"
 #include "network/Host.h"
-#include "network/Synchronisable.h"
+#include "network/synchronisable/Synchronisable.h"
 #include "core/CoreIncludes.h"
 #include <assert.h>
@@ -42,7 +42,7 @@
 #define _PACKETID             0
 #define _CLIENTID             _PACKETID + sizeof(ENUM::Type)
-#define _SHIPID               _CLIENTID + sizeof(uint32_t)
-  
-  Welcome::Welcome( unsigned int clientID, unsigned int shipID )
+#define _ENDIANTEST           _CLIENTID + sizeof(uint32_t)
+
+  Welcome::Welcome( uint32_t clientID, uint32_t shipID )
  : Packet()
 {
@@ -52,9 +52,9 @@
   assert(data_);
   *(packet::ENUM::Type *)(data_ + _PACKETID ) = packet::ENUM::Welcome;
-  *(uint32_t *)&data_[ _CLIENTID ] = clientID;
-  *(uint32_t *)&data_[ _SHIPID ] = shipID;
+  *(uint32_t *)(data_ + _CLIENTID ) = static_cast<uint32_t>(clientID);
+  *(uint32_t *)(data_ + _ENDIANTEST ) = 0xFEDC4321;
 }
 
-Welcome::Welcome( uint8_t* data, unsigned int clientID )
+Welcome::Welcome( uint8_t* data, uint32_t clientID )
   : Packet(data, clientID)
 {
@@ -74,10 +74,9 @@
 
 bool Welcome::process(){
-  unsigned int shipID, clientID;
-  clientID = *(uint32_t *)&data_[ _CLIENTID ];
-  shipID = *(uint32_t *)&data_[ _SHIPID ];
+  uint32_t clientID;
+  clientID = *(uint32_t *)(data_ + _CLIENTID );
+  assert(*(uint32_t *)(data_ + _ENDIANTEST ) == 0xFEDC4321);
   Host::setClientID(clientID);
-  Host::setShipID(shipID);
-  COUT(3) << "Welcome set clientId: " << clientID << " shipID: " << shipID << std::endl;
+  COUT(3) << "Welcome set clientId: " << clientID << endl;
   Synchronisable::setClient(true);
   delete this;
Index: code/trunk/src/network/packet/Welcome.h
===================================================================
--- code/trunk/src/network/packet/Welcome.h	(revision 2171)
+++ code/trunk/src/network/packet/Welcome.h	(revision 2662)
@@ -42,6 +42,6 @@
 {
 public:
-  Welcome( unsigned int clientID, unsigned int shipID );
-  Welcome( uint8_t* data, unsigned int clientID );
+  Welcome( uint32_t clientID, uint32_t shipID );
+  Welcome( uint8_t* data, uint32_t clientID );
   virtual ~Welcome();
 
