Index: trunk/src/lib/network/network_game_manager.cc
===================================================================
--- trunk/src/lib/network/network_game_manager.cc	(revision 6634)
+++ trunk/src/lib/network/network_game_manager.cc	(revision 6695)
@@ -46,4 +46,5 @@
  */
 NetworkGameManager::NetworkGameManager()
+  : Synchronizeable()
 {
   PRINTF(0)("START\n");
@@ -52,7 +53,6 @@
   this->setClassID(CL_NETWORK_GAME_MANAGER, "NetworkGameManager");
 
-  newUniqueID = MAX_CONNECTIONS + 2;
-
   hasRequestedWorld = false;
+  this->setSynchronized(true);
 }
 
@@ -80,7 +80,8 @@
     b = data[i++];
 
+    /**************** Commands only processed by servers ****************/
     if ( isServer() )
     {
-      if ( b == REQUEST_CREATE )
+      if ( b == NET_REQUEST_CREATE )
       {
         if ( !handleRequestCreate( i, data, length, sender ) )
@@ -88,5 +89,5 @@
         continue;
       }
-      if ( b == REQUEST_REMOVE )
+      else if ( b == NET_REQUEST_REMOVE )
       {
         if ( !handleRequestRemove( i, data, length, sender ) )
@@ -94,8 +95,15 @@
         continue;
       }
+      else if ( b == NET_REQUEST_PNODE_PATH )
+      {
+        if ( !handleRequestPNodePath( i, data, length, sender ) )
+          return i;
+        continue;
+      }
     }
     else
     {
-      if ( b == CREATE_ENTITY )
+      /**************** Commands only processed by clients ****************/
+      if ( b == NET_CREATE_ENTITY )
       {
         PRINTF(0)("CREATE_ENTITY\n");
@@ -104,5 +112,5 @@
         continue;
       }
-      if ( b == REMOVE_ENTITY )
+      else if ( b == NET_REMOVE_ENTITY )
       {
         if ( !handleRemoveEntity( i, data, length, sender ) )
@@ -110,5 +118,5 @@
         continue;
       }
-      if ( b == CREATE_ENTITY_LIST )
+      else if ( b == NET_CREATE_ENTITY_LIST )
       {
         if ( !handleCreateEntityList( i, data, length, sender ) )
@@ -116,5 +124,5 @@
         continue;
       }
-      if ( b == REMOVE_ENTITY_LIST )
+      else if ( b == NET_REMOVE_ENTITY_LIST )
       {
         if ( !handleRemoveEntityList( i, data, length, sender ) )
@@ -122,5 +130,5 @@
         continue;
       }
-      if ( b == YOU_ARE_ENTITY )
+      else if ( b == NET_YOU_ARE_ENTITY )
       {
         if ( !handleYouAreEntity( i, data, length, sender ) )
@@ -130,11 +138,11 @@
     }
 
-    if ( b == REQUEST_ENTITY_LIST )
+    /**************** Commands processed by servers and clients ****************/
+    if ( b == NET_REQUEST_ENTITY_LIST )
     {
       sendEntityList( sender );
       continue;
     }
-
-    if ( b == REQUEST_SYNC )
+    else if ( b == NET_REQUEST_SYNC )
     {
       if ( !handleRequestSync( i, data, length, sender ) )
@@ -143,5 +151,8 @@
     }
 
-    //if we get her something with data is wrong
+
+    PRINTF(1)("Network is asynchronous: couldn't decode the command sent by %i\n", sender);
+    PRINTF(1)("Probably this is because the network protocol has different \n");
+    PRINTF(1)("versions or there occured an error in the sending algorithm\n");
     PRINTF(1)("Data is not in the right format! i=%d\n", i);
     return i;
@@ -156,5 +167,5 @@
   {
     SYNCHELP_WRITE_BEGIN();
-    byte b = REQUEST_ENTITY_LIST;
+    byte b = NET_REQUEST_ENTITY_LIST;
     SYNCHELP_WRITE_BYTE( b );
     hasRequestedWorld = true;
@@ -200,13 +211,12 @@
 int NetworkGameManager::createEntity( ClassID classID, int owner )
 {
-  if ( this->isServer() )
-  {
-    if ( newUniqueID < 0 )
+  if ( this->isServer())
+  {
+    if ( SharedNetworkData::getInstance()->getNewUniqueID() < 0 )
     {
       PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n");
       return -1;
     }
-
-    return this->executeCreateEntity( classID, newUniqueID++, owner );
+    return this->executeCreateEntity( classID, SharedNetworkData::getInstance()->getNewUniqueID(), owner );
   }
   else
@@ -227,5 +237,5 @@
   if ( this->isServer() )
   {
-    if ( newUniqueID < 0 )
+    if ( SharedNetworkData::getInstance()->getNewUniqueID() < 0 )
     {
       PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n");
@@ -245,7 +255,8 @@
     {
       Synchronizeable * s = dynamic_cast<Synchronizeable*>(b);
-      s->setUniqueID( newUniqueID++ );
+      s->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() );
       s->setOwner( 0 );
-      this->networkStream->connectSynchronizeable( *s );
+      // all entities created via this function are added automaticaly to the synchronizeable list
+      s->setSynchronized(true);
       return b;
     }
@@ -258,4 +269,5 @@
   }
   else
+
   {
     PRINTF(1)("This node is not a server and cannot create id %x\n", element->Value());
@@ -295,5 +307,5 @@
       continue;
 
-    if ( !writeToClientBuffer( outBuffer[i], (byte)REQUEST_CREATE ) )
+    if ( !writeToClientBuffer( outBuffer[i], (byte)NET_REQUEST_CREATE ) )
       return;
     if ( !writeToClientBuffer( outBuffer[i], (int)classID ) )
@@ -313,5 +325,5 @@
       continue;
 
-    if ( !writeToClientBuffer( outBuffer[i], (byte)REQUEST_REMOVE ) )
+    if ( !writeToClientBuffer( outBuffer[i], (byte)NET_REQUEST_REMOVE ) )
       return;
     if ( !writeToClientBuffer( outBuffer[i], uniqueID ) )
@@ -332,5 +344,5 @@
       continue;
 
-    if ( !writeToClientBuffer( outBuffer[i], (byte)CREATE_ENTITY ) )
+    if ( !writeToClientBuffer( outBuffer[i], (byte)NET_CREATE_ENTITY ) )
       return -1;
     if ( !writeToClientBuffer( outBuffer[i], (int)classID ) )
@@ -342,4 +354,5 @@
   }
 
+  PRINTF(0)("ExecuteCreateEntity: server side: classID: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner);
   doCreateEntity( classID, uniqueID, owner );
 
@@ -359,5 +372,5 @@
       continue;
 
-    if ( !writeToClientBuffer( outBuffer[i], (byte)REMOVE_ENTITY ) )
+    if ( !writeToClientBuffer( outBuffer[i], (byte)NET_REMOVE_ENTITY ) )
       return;
     if ( !writeToClientBuffer( outBuffer[i], uniqueID ) )
@@ -394,8 +407,9 @@
   e = this->networkStream->getSyncEnd();
 
-  if ( !writeToClientBuffer( outBuffer[userID], (byte)CREATE_ENTITY_LIST ) )
+  // send the packet header
+  if ( !writeToClientBuffer( outBuffer[userID], (byte)NET_CREATE_ENTITY_LIST ) )
     return;
 
-  // -2 because you must not send network_game_manager and handshake
+  // send the number of entities: -2 because you must not send network_game_manager and handshake
   if ( !writeToClientBuffer( outBuffer[userID], networkStream->getSyncCount() ) )
     return;
@@ -403,18 +417,27 @@
   //PRINTF(0)("SendEntityList: n = %d\n", networkStream->getSyncCount()-2 );
 
-  int n = 0;
-
+  // first send the NullParent
+  if ( !writeToClientBuffer( outBuffer[userID], (int)PNode::getNullParent()->getLeafClassID()) )
+    return;
+  if ( !writeToClientBuffer( outBuffer[userID], (int)PNode::getNullParent()->getUniqueID()) )
+    return;
+  if ( !writeToClientBuffer( outBuffer[userID], (int)PNode::getNullParent()->getOwner()) )
+    return;
+
+  // now send the rest of the entities
   while ( it != e )
   {
-    PRINTF(5)("SENDING ENTITY %s id %d\n", (*it)->getClassName(), (*it)->getUniqueID() );
-    if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getLeafClassID()) ) )
-      return;
-
-    if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getUniqueID()) ) )
-      return;
-
-    if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getOwner()) ) )
-      return;
-
+    if( (*it)->beSynchronized() && (*it) != PNode::getNullParent())
+    {
+      PRINTF(0)("SENDING ENTITY %s classid: %x, uniqueid %d\n", (*it)->getClassName(), (*it)->getLeafClassID(), (*it)->getUniqueID() );
+      if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getLeafClassID()) ) )
+        return;
+
+      if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getUniqueID()) ) )
+        return;
+
+      if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getOwner()) ) )
+        return;
+    }
     it++;
   }
@@ -467,5 +490,15 @@
 BaseObject* NetworkGameManager::doCreateEntity( ClassID classID, int uniqueID, int owner )
 {
-  BaseObject * b = Factory::fabricate( classID );
+  PRINTF(0)("Creating Entity via Factory: classid: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner);
+
+  BaseObject * b;
+  /* These are some small exeptions in creation: Not all objects can/should be created via Factory */
+  /* Exception 1: NullParent */
+  if( classID == CL_NULL_PARENT)
+  {
+    b = (BaseObject*)PNode::getNullParent();
+  }
+  else
+    b = Factory::fabricate( classID );
 
   if ( !b )
@@ -480,5 +513,6 @@
     s->setUniqueID( uniqueID );
     s->setOwner( owner );
-    this->networkStream->connectSynchronizeable( *s );
+    s->setSynchronized(true);
+    //this->networkStream->connectSynchronizeable( *s );
     if ( !isServer() )
       s->setIsOutOfSync( true );
@@ -646,5 +680,5 @@
   if ( userID != 0 )
   {
-    if ( !writeToClientBuffer( outBuffer[userID], (byte)YOU_ARE_ENTITY ) )
+    if ( !writeToClientBuffer( outBuffer[userID], (byte)NET_YOU_ARE_ENTITY ) )
       return;
 
@@ -714,4 +748,5 @@
   i += Converter::byteArrayToInt( &data[i], &owner );
 
+  PRINTF(0)("handleCreateEntity: client side: classID: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner);
   doCreateEntity( (ClassID)classID, uniqueID, owner );
 
@@ -779,14 +814,4 @@
     {
       BaseObject* b = doCreateEntity( (ClassID)classID, uniqueID, owner );
-
-      /*if ( b != NULL )
-      {
-        if ( b->isA(CL_WORLD_ENTITY) )
-        {
-          int n = dynamic_cast<WorldEntity*>(b)->writeState( data, length, sender );
-
-          i += n;
-        }
-    }*/
     }
 
@@ -854,4 +879,44 @@
   return true;
 }
+
+
+/**
+ *  handles the network signal NET_REQUEST_PNODE_PATH
+ * @param i byte offset in the buffer
+ * @param data data array
+ * @param length length of the data arary
+ * @param sender the sender id
+ * @return true if process terminated sucessfully
+ */
+bool NetworkGameManager::handleRequestPNodePath(int& i, const byte* data, int length, int sender)
+{
+  if( INTSIZE > length-i )
+  {
+    PRINTF(1)("Cannot read n from buffer! Not enough data left!\n");
+    return false;
+  }
+  PRINTF(0)("HandleRequestPNodePath:  data[i..i+3] = %d %d %d %d\n", data[i], data[i+1], data[i+2], data[i+3]);
+
+  int uniqueID1, uniqueID2;
+  if( INTSIZE > length-i )
+  {
+    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
+    return false;
+  }
+  i += Converter::byteArrayToInt( &data[i], &uniqueID1 );
+
+  if( INTSIZE > length-i )
+  {
+    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
+    return false;
+  }
+  i += Converter::byteArrayToInt( &data[i], &uniqueID2 );
+
+
+  PRINTF(0)("HandleRequestPNodePath: got a request for path from uid %i to uid %i\n", uniqueID1, uniqueID2);
+
+  return true;
+}
+
 
 bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, byte b )
@@ -898,5 +963,5 @@
     resizeBufferVector( user );
 
-  if ( !writeToClientBuffer( outBuffer[user], (byte)REQUEST_SYNC ) )
+  if ( !writeToClientBuffer( outBuffer[user], (byte)NET_REQUEST_SYNC ) )
     return;
   if ( !writeToClientBuffer( outBuffer[user], uniqueID ) )
