Index: code/branches/presentation/src/network/GamestateManager.cc
===================================================================
--- code/branches/presentation/src/network/GamestateManager.cc	(revision 2560)
+++ code/branches/presentation/src/network/GamestateManager.cc	(revision 2561)
@@ -109,36 +109,9 @@
   }
 
-  /**
-   * this function is used to keep the memory usage low
-   * it tries to delete all the unused gamestates
-   *
-   *
-   */
-/*  void GamestateManager::cleanup(){
-    std::map<int,int>::iterator it = gamestateUsed.begin();
-    while(it!=gamestateUsed.end()){
-      if((id_-(*it).first)<KEEP_GAMESTATES)
-        break;
-      if( (*it).second <= 0 ){
-        COUT(5) << "GameStateManager: deleting gamestate with id: " << (*it).first << ", uses: " << (*it).second << std::endl;
-        std::map<int, packet::Gamestate *>::iterator tempit = gamestateMap.find((*it).first);
-        if( tempit != gamestateMap.end() ){
-          packet::Gamestate *temp = tempit->second;
-          if(temp){
-            delete gamestateMap[(*it).first];
-            gamestateMap.erase((*it).first);
-          }
-        }
-        gamestateUsed.erase(it++);
-        continue;
-      }
-      it++;
-    }
-  }*/
 
   packet::Gamestate *GamestateManager::popGameState(unsigned int clientID) {
     //why are we searching the same client's gamestate id as we searched in
     //Server::sendGameState?
-    packet::Gamestate *gs, *tempgs;
+    packet::Gamestate *gs;
     unsigned int gID = ClientInformation::findClient(clientID)->getGamestateID();
     if(!reference)
@@ -147,24 +120,22 @@
 //     gs = new packet::Gamestate(*reference);
     // save the (undiffed) gamestate in the clients gamestate map
-    gamestateMap_[clientID].insert(std::pair<int, packet::Gamestate*>(gs->getID(), gs));
+    gamestateMap_[clientID][gs->getID()]=gs;
     //chose wheather the next gamestate is the first or not
-    packet::Gamestate *client=NULL;
+    packet::Gamestate *client=0;
     if(gID != GAMESTATEID_INITIAL){
-      std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(clientID);
-      if(clientMap!=gamestateMap_.end()){
-        std::map<unsigned int, packet::Gamestate*>::iterator it = clientMap->second.find(gID);
-        if(it!=clientMap->second.end())
-          client = it->second;
+      assert(gamestateMap_.find(clientID)!=gamestateMap_.end());
+      std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateMap_[clientID].find(gID);
+      if(it!=gamestateMap_[clientID].end())
+      {
+        client = it->second;
       }
     }
     if(client){
 //       COUT(3) << "diffing" << std::endl;
-      tempgs = gs->diff(client);
-      delete gs;
-      gs = tempgs;
+      gs = gs->diff(client);
     }
     else{
 //       COUT(3) << "not diffing" << std::endl;
-//       gs = new packet::Gamestate(*gs); //not necessary
+      gs = new packet::Gamestate(*gs);
     }
     bool b = gs->compressData();
@@ -181,4 +152,10 @@
     if(gamestateID == ACKID_NACK){
       temp->setGamestateID(GAMESTATEID_INITIAL);
+      // now delete all saved gamestates for this client
+      std::map<unsigned int, packet::Gamestate*>::iterator it;
+      for(it = gamestateMap_[clientID].begin(); it!=gamestateMap_[clientID].end(); ){
+        delete it->second;
+        gamestateMap_[clientID].erase(it++);
+      }
       return true;
     }
@@ -186,9 +163,8 @@
     assert(curid==(unsigned int)GAMESTATEID_INITIAL || curid<gamestateID);
     COUT(4) << "acking gamestate " << gamestateID << " for clientid: " << clientID << " curid: " << curid << std::endl;
-    std::map<unsigned int, packet::Gamestate*>::iterator it, tempit;
-    for(it = gamestateMap_[clientID].begin(); it!=gamestateMap_[clientID].end() && it->first<gamestateID; it++){
+    std::map<unsigned int, packet::Gamestate*>::iterator it;
+    for(it = gamestateMap_[clientID].begin(); it!=gamestateMap_[clientID].end() && it->first<gamestateID; ){
       delete it->second;
-      tempit=it++;
-      gamestateMap_[clientID].erase(tempit);
+      gamestateMap_[clientID].erase(it++);
     }
     temp->setGamestateID(gamestateID);
