Index: branches/network_merged/src/lib/network/network_game_manager.h
===================================================================
--- branches/network_merged/src/lib/network/network_game_manager.h	(revision 6338)
+++ branches/network_merged/src/lib/network/network_game_manager.h	(revision 6340)
@@ -14,31 +14,47 @@
 
 
+class TiXmlElement;
 
 /**
  * protocol definition
  *
- *  CREATE_ENTITY:      CLASS_ID, UNIQUE_ID, OWNER
- *  REMOVE_ENTITY:      UNIQUE_ID
+ *  CREATE_ENTITY:       CLASS_ID, UNIQUE_ID, OWNER
+ *  REMOVE_ENTITY:       UNIQUE_ID
  *
- *  CREATE_ENTITY_LIST: NUMBER, [CLASS_ID, UNIQUE_ID, OWNER][0..NUMBER]
- *  REMOVE_ENTITY_LIST: NUMBER, [UNIQUE_ID][0..NUMBER]
+ *  CREATE_ENTITY_LIST:  NUMBER, [CLASS_ID, UNIQUE_ID, OWNER][0..NUMBER]
+ *  REMOVE_ENTITY_LIST:  NUMBER, [UNIQUE_ID][0..NUMBER]
  *
- *  REQUEST_CREATE:     CLASS_ID
- *  REQUEST_REMOVE:     UNIQUE_ID
+ *  REQUEST_CREATE:      CLASS_ID
+ *  REQUEST_REMOVE:      UNIQUE_ID
  *
- *  REQUEST_SYNC:       UNIQUE_ID
- *  REQUEST_SYNC_LIST:  NUMBER, [UNIQUE_ID][0..NUMBER]
+ *  //REQUEST_CREATE_LIST: NUMBER, [CLASS_ID][0..NUMBER]
+ *  //REQUEST_CREATE_LIST: NUMBER, [UNIQUE_ID][0..NUMBER]
  *
+ *  REQUEST_ENTITY_LIST: //request the whole world :D
+ *  REQUEST_SYNC:        UNIQUE_ID
+ *  //REQUEST_SYNC_LIST:   NUMBER, [UNIQUE_ID][0..NUMBER]
+ *
+ *  YOU_ARE_ENTITY:      UNIQUE_ID
  *
  */
 
+typedef enum NetworkGameManagerProtocol{
+  CREATE_ENTITY = 0,
+  REMOVE_ENTITY,
+  CREATE_ENTITY_LIST,
+  REMOVE_ENTITY_LIST,
+  REQUEST_CREATE,
+  REQUEST_REMOVE,
+  REQUEST_SYNC,
+  YOU_ARE_ENTITY,
+  REQUEST_ENTITY_LIST
+};
 
-
-
-
-
-
-
-
+struct clientBuffer
+{
+  int length;
+  int maxLength;
+  byte * buffer;
+};
 
 /*!
@@ -48,41 +64,65 @@
 {
   public:
-    NetworkGameManager();
     ~NetworkGameManager();
 
-    virtual void writeBytes(const byte* data, int length);
+    static NetworkGameManager* NetworkGameManager::getInstance()
+    { if (!NetworkGameManager::singletonRef) NetworkGameManager::singletonRef = new NetworkGameManager(); return NetworkGameManager::singletonRef; }
+
+    virtual int writeBytes(const byte* data, int length, int sender);
     virtual int readBytes(byte* data, int maxLength, int * reciever);
     virtual void writeDebug() const;
     virtual void readDebug() const;
 
-    void createEntity(int classID);
-    void createEntityList(int* classIDList);
-    void removeEntity(int uniqueID);
-    void removeEntityList(int* uniqueIDList);
+    void createEntity( ClassID classID, int owner = 0 );
+    BaseObject* createEntity( TiXmlElement* element);
+    void removeEntity( int uniqueID );
+    void sendYouAre( int uniqueID, int userID );
 
-    void sync(int uniqueID);
-    void syncList(int* uniqueIDList);
+    void sync(int uniqueID, int owner);
 
+    void sendEntityList(int userID);
 
   private:
-    void requestCreateEntity(int classID);
-    void executeCreateEntity(int classID);
-    void requestCreateEntityList(int* classIDList);
-    void executeCreateEntityList(int* classIDList);
+    NetworkGameManager();
+
+    void requestCreateEntity(ClassID classID);
+    void executeCreateEntity(ClassID classID, int uniqueID = 0, int owner = 0);
 
     void requestRemoveEntity(int uniqueID);
     void executeRemoveEntity(int uniqueID);
-    void requestRemoveEntityList(int* uniqueIDList);
-    void executeRemoveEntityList(int* uniqueIDList);
 
+    void executeRequestSync( int uniqueID, int user );
 
+    void doCreateEntity(ClassID classID, int uniqueID, int owner);
+    void doRemoveEntity(int uniqueID);
+    void doRequestSync(int uniqueID, int userID);
+    void doYouAre( int uniqueID );
 
-    bool canCreateEntity(int classID);
+    bool canCreateEntity(ClassID classID);
 
+    void resizeBufferVector(int n);
 
+    inline bool writeToClientBuffer( clientBuffer &cb, byte*data, int length );
+    inline bool writeToClientBuffer( clientBuffer &cb, byte b );
+    inline bool writeToClientBuffer( clientBuffer &cb, int i );
+    inline bool readFromClientBuffer( clientBuffer &cb, byte*data, int length );
+
+    //helper functions for writeBytes
+    inline bool handleRequestCreate( int& i, const byte* data, int length, int sender );
+    inline bool handleRequestRemove( int& i, const byte* data, int length, int sender );
+    inline bool handleCreateEntity( int& i, const byte* data, int length, int sender );
+    inline bool handleRemoveEntity( int& i, const byte* data, int length, int sender );
+    inline bool handleCreateEntityList( int& i, const byte* data, int length, int sender );
+    inline bool handleRemoveEntityList( int& i, const byte* data, int length, int sender );
+    inline bool handleYouAreEntity( int& i, const byte* data, int length, int sender );
+    inline bool handleRequestSync( int& i, const byte* data, int length, int sender );
 
   private:
-    byte*          inBuffer;
-    byte*          outBuffer;
+    std::vector<clientBuffer>     outBuffer;
+    clientBuffer                  allOutBuffer;
+    static NetworkGameManager*    singletonRef;
+
+    int                           newUniqueID;
+    bool                          hasRequestedWorld;
 };
 
