Index: code/branches/network3/src/network/GameStateManager.cc
===================================================================
--- code/branches/network3/src/network/GameStateManager.cc	(revision 1173)
+++ code/branches/network3/src/network/GameStateManager.cc	(revision 1199)
@@ -52,5 +52,5 @@
 {
   GameStateManager::GameStateManager(ClientInformation *head) {
-    id=0;
+    id_=0;
     head_=head;
   }
@@ -61,8 +61,9 @@
   void GameStateManager::update(){
     cleanup();
-    reference = getSnapshot(id);
-    gameStateMap.insert(std::pair<int, GameState*>(id, reference));
-    gameStateUsed[id]=0;
-    ++id;
+    reference = getSnapshot();
+    COUT(4) << "inserting gamestate: " << reference << std::endl;
+    gameStateMap.insert(std::pair<int, GameState*>(id_, reference));
+    gameStateUsed[id_]=0;
+    printGameStates();
     return;
   }
@@ -79,10 +80,15 @@
     while(it!=gameStateUsed.end()){
       if( (*it).second <= 0 ){
-        free(gameStateMap[(*it).first]->data);
+        COUT(4) << "GameStateManager: deleting gamestate with id: " << (*it).first << ", uses: " << (*it).second << std::endl;
+        delete[] gameStateMap[(*it).first]->data;
         delete gameStateMap[(*it).first];
         gameStateMap.erase((*it).first);
         gameStateUsed.erase(it++);
-      }else  //as soon as we got a used gamestate break here because we could use newer gamestates in future
+        continue;
+      }else if(id_-it->first<=KEEP_GAMESTATES){  //as soon as we got a used gamestate break here because we could use newer gamestates in future but only if we do not exceed KEEP_GAMESTATES # of gamestates in cache
+        COUT(4) << "breaking " << std::endl;
         break;
+      }
+      it++;
     }
   }
@@ -92,14 +98,21 @@
     //Server::sendGameState?
     int gID = head_->findClient(clientID)->getGamestateID();
-    COUT(4) << "G.St.Man: popgamestate: sending gstate_id: " << id << " diffed from: " << gID << " (not diffed yet)" << std::endl;
-    
+    COUT(4) << "G.St.Man: popgamestate: sending gstate_id: " << id_ << " diffed from: " << gID << " (not diffed yet)" << std::endl;
+//     COUT(3) << "gamestatemap: " << &gameStateMap << std::endl;
     //chose wheather the next gamestate is the first or not
     if(gID != GAMESTATEID_INITIAL){
-      GameState *client = gameStateMap[gID];
+      // TODO something with the gamestatemap is wrong
+      GameState *client = gameStateMap.find(gID)->second;
       GameState *server = reference;
       //head_->findClient(clientID)->setGamestateID(id);
-      return encode(client, server);
+      COUT(3) << "client: " << client << " server: " << server << " gamestatemap: " << &gameStateMap << std::endl;
+      if(client)
+        return encode(client, server);
+      else
+        return encode(server);
     } else {
+      COUT(4) << "we got a GAMESTATEID_INITIAL for clientID: " << clientID << std::endl;
       GameState *server = reference;
+      ackGameState(clientID, reference->id);
       //head_->findClient(clientID)->setGamestateID(id);
       return encode(server);
@@ -113,10 +126,10 @@
   * @return struct of type gamestate containing the size of the whole gamestate and a pointer linking to the flat list
   */
-  GameState *GameStateManager::getSnapshot(int id)
+  GameState *GameStateManager::getSnapshot()
   {
     //std::cout << "begin getSnapshot" << std::endl;
     //the size of the gamestate
     int totalsize=0;
-    int memsize=1000;
+    int memsize=0;
     //the size of one specific synchronisable
     int tempsize=0;
@@ -127,5 +140,5 @@
 
     GameState *retval=new GameState; //return value
-    retval->id=id++;
+    retval->id=++id_;
     COUT(4) << "G.ST.Man: producing gamestate with id: " << retval->id << std::endl;
     // offset of memory functions
@@ -133,8 +146,9 @@
     // get total size of gamestate
     for(it = orxonox::ObjectList<Synchronisable>::start(); it; ++it){
-      size+=it->getSize();
-      size+=3*sizeof(int);
-    }
-    retval->data = (unsigned char*)malloc(size);
+      size+=it->getSize(); // size of the actual data of the synchronisable
+      size+=3*sizeof(int); // size of datasize, classID and objectID
+    }
+    //retval->data = (unsigned char*)malloc(size);
+    retval->data = new unsigned char[size];
     if(!retval->data){
       COUT(2) << "GameStateManager: could not allocate memory" << std::endl;
@@ -148,7 +162,7 @@
       // add place for data and 3 ints (length,classid,objectid)
       totalsize+=tempsize+3*sizeof(int);
-      // allocate additional space
-      if((totalsize+tempsize) > memsize){
-        COUT(5) << "G.St.Man: need additional memory" << std::endl;
+      // allocate+tempsize additional space
+      if(totalsize > size){
+        COUT(3) << "G.St.Man: need additional memory" << std::endl;
 //         if(tempsize < 1000){
 //           retval->data = (unsigned char *)realloc((void *)retval->data, totalsize+1000);
@@ -190,16 +204,16 @@
   GameStateCompressed *GameStateManager::encode(GameState *a, GameState *b) {
     COUT(5) << "G.St.Man: this will be a DIFFED gamestate" << std::endl;
-    //GameState r = diff(a,b);
+    GameState *r = diff(a,b);
     //r.diffed = true;
-    GameState *r = b;
-    r->diffed = false;
+//     GameState *r = b;
+//     r->diffed = false;
     //return compress_(r);
     GameStateCompressed *g = new GameStateCompressed;
-    g->base_id = b->base_id;
-    g->id = b->id;
-    g->diffed = b->diffed;
-    g->data = b->data;
-    g->normsize = b->size;
-    g->compsize = b->size;
+    g->base_id = r->base_id;
+    g->id = r->id;
+    g->diffed = r->diffed;
+    g->data = r->data;
+    g->normsize = r->size;
+    g->compsize = r->size;
     return g;
   }
@@ -226,5 +240,6 @@
     else
       dest_length=b->size;
-    unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char));
+    //unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char));
+    unsigned char *dp = new unsigned char[dest_length*sizeof(unsigned char)];
     while(of<a->size && of<b->size){
       *(dp+of)=*(ap+of)^*(bp+of); // do the xor
@@ -263,5 +278,6 @@
     uLongf buffer = (uLongf)((a->size + 12)*1.01)+1;
     //COUT(4) << "size: " << size << ", buffer: " << buffer << std::endl;
-    unsigned char* dest = (unsigned char*)malloc( buffer );
+    //unsigned char* dest = (unsigned char*)malloc( buffer );
+    unsigned char *dest = new unsigned char[buffer];
     //COUT(4) << "dest: " << dest << std::endl;
     int retval;
@@ -296,9 +312,11 @@
   void GameStateManager::ackGameState(int clientID, int gamestateID) {
     ClientInformation *temp = head_->findClient(clientID);
-    int curid = temp->getID();
+    int curid = temp->getGamestateID();
+    COUT(4) << "acking gamestate " << gamestateID << " for clientid: " << clientID << " curid: " << curid << std::endl;
     // decrease usage of gamestate and save it
-    deleteUnusedGameState(curid);
+//     deleteUnusedGameState(curid);
     //increase gamestateused
-    ++gameStateUsed.find(gamestateID)->second;
+    --(gameStateUsed.find(curid)->second);
+    ++(gameStateUsed.find(gamestateID)->second);
     temp->setGamestateID(gamestateID);
     /*
@@ -308,13 +326,11 @@
   }
 
-  bool GameStateManager::deleteUnusedGameState(int gamestateID) {
-    int used = --(gameStateUsed.find(gamestateID)->second);
-    if(id-gamestateID>KEEP_GAMESTATES && used==0){
-      // delete gamestate
-      delete gameStateMap.find(gamestateID)->second;
-      gameStateMap.erase(gamestateID);
-      return true;
-    }
-    return false;
+  bool GameStateManager::printGameStates() {
+    std::map<int, GameState*>::iterator it;
+    COUT(4) << "gamestates: ";
+    for(it = gameStateMap.begin(); it!=gameStateMap.end(); it++){
+      COUT(4) << (*it).first << ":" << (*it).second << " | ";
+    }
+    COUT(4) << std::endl;
   }
 
