Index: branches/network/src/lib/network/synchronizeable.cc
===================================================================
--- branches/network/src/lib/network/synchronizeable.cc	(revision 7507)
+++ branches/network/src/lib/network/synchronizeable.cc	(revision 7508)
@@ -96,7 +96,103 @@
  * @return n bytes copied into data
  */
-int Synchronizeable::getStateDiff( int userId, byte* data, int maxLength, int stateId, int priorityTH )
-{
-#warning implement this
+int Synchronizeable::getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH )
+{
+  //make sure this user has his history
+  if ( sentStates.size() <= userId )
+    sentStates.resize( userId+1 );
+  
+  //calculate needed memory
+  int neededSize = 0;
+  
+  for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ )
+    neededSize += (*it)->getSize();
+  
+  assert( neededSize <= maxLength );
+  
+  //remove older states from history than fromStateId
+  StateHistory::iterator it = sentStates[userId].begin();
+  
+  while ( it != sentStates[userId].end() && (*it)->stateId < fromStateId )
+    it++;
+  
+  if ( it != sentStates[userId].begin() )
+  {
+    for ( StateHistory::iterator it2 = sentStates[userId].begin(); it2 != it; it2++ )
+    {
+      if ( (*it2)->data != NULL )
+      {
+        delete (*it2)->data;
+        (*it2)->data = NULL;
+      }
+    }
+    sentStates[userId].erase( sentStates[userId].begin(), it );
+  }
+  
+  //find state to create diff from
+  StateHistoryEntry * stateFrom = NULL;
+  
+  it = sentStates[userId].begin();
+  while ( it != sentStates[userId].end() && (*it)->stateId != fromStateId )
+    it++;
+  
+  if ( it == sentStates[userId].end() )
+  {
+    StateHistoryEntry * initialEntry = new StateHistoryEntry();
+    
+    initialEntry->stateId = fromStateId;
+    initialEntry->dataLength = 0;
+    initialEntry->data = NULL;
+    
+    stateFrom = initialEntry;
+  }
+  else
+    stateFrom = (*it);
+  
+  StateHistoryEntry * stateTo = new StateHistoryEntry();
+    
+  stateTo->stateId = stateId;
+  stateTo->dataLength = neededSize;
+  stateTo->data = (byte*)malloc( neededSize );
+  
+  std::list<int>::iterator sizeIter = stateFrom->sizeList.begin();
+  
+  int i = 0;
+  int n;
+  
+  for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ )
+  {
+    if ( (*it)->getPriority() >= priorityTH || sizeIter == stateFrom->sizeList.end() )
+    {
+      n = (*it)->writeToBuf( stateTo->data+i, stateTo->dataLength - i );
+      stateTo->sizeList.push_back( n );
+      i += n;
+    }
+    else
+    {
+      for ( int j = 0; j<(*sizeIter); j++ )
+      {
+        assert( i < stateFrom->dataLength );
+        stateTo->data[i] = stateFrom->data[i];
+        i++;
+      }
+      stateTo->sizeList.push_back( (*sizeIter) );
+    }
+    
+    if ( sizeIter != stateFrom->sizeList.end() )
+      sizeIter++;
+  }
+  
+  sentStates[userId].push_back( stateTo );
+  
+  //write diff to data
+  for ( i = 0; i<neededSize; i++ )
+  {
+    if ( i < stateFrom->dataLength )
+      data[i] = stateTo->data[i] - stateFrom->data[i];
+    else
+      data[i] = stateTo->data[i];
+  }
+  
+  return neededSize;
 }
 
@@ -109,7 +205,77 @@
  * @return true on success
  */
-bool Synchronizeable::setStateDiff( int userId, byte* data, int length, int stateId )
-{
-#warning implement this
+bool Synchronizeable::setStateDiff( int userId, byte* data, int length, int stateId, int fromStateId )
+{
+  //make sure this user has his history
+  if ( recvStates.size() <= userId )
+    recvStates.resize( userId+1 );
+  
+  //create new state
+  StateHistoryEntry * stateTo = new StateHistoryEntry();
+  stateTo->stateId = stateId;
+  stateTo->dataLength = length;
+  stateTo->data = (byte*)malloc( length );
+  
+  //remove old states 
+  StateHistory::iterator it = recvStates[userId].begin();
+  
+  while ( it != recvStates[userId].end() && (*it)->stateId < fromStateId )
+    it++;
+  
+  if ( it != recvStates[userId].begin() )
+  {
+    for ( StateHistory::iterator it2 = recvStates[userId].begin(); it2 != it; it2++ )
+    {
+      if ( (*it2)->data != NULL )
+      {
+        delete (*it2)->data;
+        (*it2)->data = NULL;
+      }
+    }
+    recvStates[userId].erase( recvStates[userId].begin(), it );
+  }
+  
+  //find state to apply diff to
+  StateHistoryEntry * stateFrom = NULL;
+  
+  it = recvStates[userId].begin();
+  while ( it != recvStates[userId].end() && (*it)->stateId != fromStateId )
+    it++;
+  
+  if ( it == recvStates[userId].end() )
+  {
+    StateHistoryEntry * initialEntry = new StateHistoryEntry();
+    
+    initialEntry->stateId = fromStateId;
+    initialEntry->dataLength = 0;
+    initialEntry->data = NULL;
+    
+    stateFrom = initialEntry;
+  }
+  else
+    stateFrom = (*it);
+  
+  //apply diff
+  for ( int i = 0; i<length; i++ )
+  {
+    if ( i < stateFrom->dataLength )
+      stateTo->data[i] = stateFrom->data[i] + data[i];
+    else
+      stateTo->data[i] = data[i];
+  }
+  
+  //add state to state history
+  recvStates[userId].push_back( stateTo );
+  
+  int i = 0; 
+  
+  for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ )
+  {
+    i += (*it)->readFromBuf( stateTo->data + i, stateTo->dataLength - i );
+  }
+  
+  assert( i == length -1 );
+  
+  return length;
 }
 
@@ -129,5 +295,5 @@
 void Synchronizeable::registerVar( SynchronizeableVar * var )
 {
-#warning implement this
+  syncVarList.push_back( var );
 }
 
@@ -140,6 +306,7 @@
 int Synchronizeable::registerVarId( SynchronizeableVar * var )
 {
-#warning implement this
-}
-
-
+  syncVarList.push_back( var );
+  return syncVarList.size()-1;
+}
+
+
Index: branches/network/src/lib/network/synchronizeable.h
===================================================================
--- branches/network/src/lib/network/synchronizeable.h	(revision 7507)
+++ branches/network/src/lib/network/synchronizeable.h	(revision 7508)
@@ -28,4 +28,16 @@
 #define STATE_SERVER 1
 
+struct StateHistoryEntry
+{
+  int             stateId;
+  byte *          data;
+  int             dataLength;
+  std::list<int>  sizeList;
+};
+
+typedef std::list<StateHistoryEntry*> StateHistory;
+
+typedef std::vector<StateHistory> UserStateHistory;
+
 enum {
   PERMISSION_OWNER = 1,
@@ -33,5 +45,5 @@
 };
 
-typedef std::vector<SynchronizeableVar> SyncVarList;
+typedef std::vector<SynchronizeableVar*> SyncVarList;
 
 class NetworkStream;
@@ -49,6 +61,6 @@
     virtual void varChangeHandler( std::list<int> & id );
     
-    int getStateDiff( int userId, byte* data, int maxLength, int stateId, int priorityTH );
-    bool setStateDiff( int userId, byte* data, int length, int stateId );
+    int getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH );
+    bool setStateDiff( int userId, byte* data, int length, int stateId, int fromStateId );
     
     void registerVar( SynchronizeableVar * var );
@@ -80,4 +92,9 @@
     int               hostID;         //!< my own host id
     bool              bSynchronize;   //!< do we need beeing synchronized?
+    
+    SyncVarList       syncVarList;    //!< list containing variables to synchronize
+    
+    UserStateHistory  sentStates;     //!< store already sent states to create diffs from
+    UserStateHistory  recvStates;     //!< store recieved states to apply diffs
 
 };
Index: branches/network/src/lib/network/synchronizeable_var/synchronizeable_bool.cc
===================================================================
--- branches/network/src/lib/network/synchronizeable_var/synchronizeable_bool.cc	(revision 7507)
+++ branches/network/src/lib/network/synchronizeable_var/synchronizeable_bool.cc	(revision 7508)
@@ -21,5 +21,5 @@
  * standard constructor
  */
-SynchronizeableBool::SynchronizeableBool( bool * ptrIn, bool * ptrOut, std::string name, int permission, int priority) : SynchronizeableVar( ptrIn, ptrOut, name, 0, permission, priority )
+SynchronizeableBool::SynchronizeableBool( bool * ptrIn, bool * ptrOut, std::string name, int permission, int priority) : SynchronizeableVar( ptrIn, ptrOut, name, 1, permission, priority )
 {
   this->vPtrIn = ptrIn;
Index: branches/network/src/lib/network/synchronizeable_var/synchronizeable_int.cc
===================================================================
--- branches/network/src/lib/network/synchronizeable_var/synchronizeable_int.cc	(revision 7507)
+++ branches/network/src/lib/network/synchronizeable_var/synchronizeable_int.cc	(revision 7508)
@@ -23,5 +23,5 @@
  * @todo this constructor is not jet implemented - do it
 */
-SynchronizeableInt::SynchronizeableInt( int * ptrIn, int * ptrOut, std::string name, int permission, int priority) : SynchronizeableVar( ptrIn, ptrOut, name, 0, permission, priority )
+SynchronizeableInt::SynchronizeableInt( int * ptrIn, int * ptrOut, std::string name, int permission, int priority) : SynchronizeableVar( ptrIn, ptrOut, name, INTSIZE, permission, priority )
 {
   this->vPtrIn = ptrIn;
Index: branches/network/src/lib/network/synchronizeable_var/synchronizeable_string.h
===================================================================
--- branches/network/src/lib/network/synchronizeable_var/synchronizeable_string.h	(revision 7507)
+++ branches/network/src/lib/network/synchronizeable_var/synchronizeable_string.h	(revision 7508)
@@ -5,4 +5,5 @@
 
 #include "synchronizeable_var/synchronizeable_var.h"
+#include "converter.h"
 
 #ifndef _SYNCHRONIZEABLE_STRING_H
@@ -27,4 +28,6 @@
     virtual bool hasStaticSize(){ return false; };
     
+    virtual int getSize(){ return vPtrIn->length()+INTSIZE; }
+    
   private:
     std::string * vPtrIn;       //!< pointer to data (read)
Index: branches/network/src/lib/network/synchronizeable_var/synchronizeable_var.h
===================================================================
--- branches/network/src/lib/network/synchronizeable_var/synchronizeable_var.h	(revision 7507)
+++ branches/network/src/lib/network/synchronizeable_var/synchronizeable_var.h	(revision 7508)
@@ -45,4 +45,10 @@
     
     /**
+     * get size writeToBuf needs
+     * @return size in bytes
+     */
+    virtual int getSize(){ return length; }
+    
+    /**
      * check for permission to write
      * @return true if you can write
@@ -61,4 +67,21 @@
      */
     inline void setName( std::string name ) { this->name = name; }
+    
+    /**
+     * get priority
+     * @return priority
+     */
+    inline int getPriority() { return this->priority; }
+    
+    /**
+     * set priority
+     * @param p priority
+     */
+    inline void setPriority( int p ) { this->priority = p; }
+    
+    /**
+     * reset priority to variable specific default value
+     */
+    inline void resetPriority() { this->priority = this->real_priority; }
 
 
