Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 12, 2008, 7:40:47 PM (16 years ago)
Author:
scheusso
Message:

merged network branch back to trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/network/GamestateManager.cc

    r1769 r1907  
    6161
    6262  bool GamestateManager::update(){
    63     cleanup();
     63//     cleanup();
    6464    return getSnapshot();
    6565  }
     
    9292    reference = new packet::Gamestate();
    9393    reference->collectData(++id_);
    94     COUT(4) << "inserting gamestate: " << reference << std::endl;
    95     gamestateMap.insert(std::pair<int, packet::Gamestate*>(id_, reference));
    96     gamestateUsed[id_]=0;
     94    //COUT(4) << "inserting gamestate: " << reference << std::endl;
     95    //gamestateMap_.insert(std::pair<int, packet::Gamestate*>(id_, reference));
     96//     gamestateUsed[id_]=0;
    9797    return true;
    9898  }
     
    104104   *
    105105   */
    106   void GamestateManager::cleanup(){
     106/*  void GamestateManager::cleanup(){
    107107    std::map<int,int>::iterator it = gamestateUsed.begin();
    108108    while(it!=gamestateUsed.end()){
     
    124124      it++;
    125125    }
    126   }
     126  }*/
    127127
    128128  packet::Gamestate *GamestateManager::popGameState(int clientID) {
     
    131131    packet::Gamestate *gs;
    132132    int gID = ClientInformation::findClient(clientID)->getGamestateID();
    133     //COUT(4) << "G.St.Man: popgamestate: sending gstate_id: " << id_ << " diffed from: " << gID << std::endl;
     133    gs = reference->doSelection(clientID);
     134//     gs = new packet::Gamestate(*reference);
     135//     gs = new packet::Gamestate(*reference);
     136    // save the (undiffed) gamestate in the clients gamestate map
     137    gamestateMap_[clientID].insert(std::pair<int, packet::Gamestate*>(gs->getID(), gs));
    134138    //chose wheather the next gamestate is the first or not
     139    packet::Gamestate *client=NULL;
    135140    if(gID != GAMESTATEID_INITIAL){
    136       packet::Gamestate *client=NULL;
    137       std::map<int, packet::Gamestate*>::iterator it = gamestateMap.find(gID);
    138       if(it!=gamestateMap.end())
    139         client = it->second;
    140       if(client)
    141         gs = reference->diff(client);
    142       else
    143         gs = new packet::Gamestate(*reference);
    144     } else {
    145       COUT(4) << "we got a GAMESTATEID_INITIAL for clientID: " << clientID << std::endl;
    146       gs = new packet::Gamestate(*reference);
    147     }
    148     assert(gs->compressData());
     141      std::map<unsigned int, std::map<int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(clientID);
     142      if(clientMap!=gamestateMap_.end()){
     143        std::map<int, packet::Gamestate*>::iterator it = clientMap->second.find(gID);
     144        if(it!=clientMap->second.end())
     145          client = it->second;
     146      }
     147    }
     148    if(client){
     149//       COUT(3) << "diffing" << std::endl;
     150      gs = gs->diff(client);
     151    }
     152    else{
     153//       COUT(3) << "not diffing" << std::endl;
     154      gs = new packet::Gamestate(*gs);
     155    }
     156    bool b = gs->compressData();
     157    assert(b);
    149158    return gs;
    150159  }
     
    153162  bool GamestateManager::ack(int gamestateID, int clientID) {
    154163    ClientInformation *temp = ClientInformation::findClient(clientID);
    155     if(temp==0)
    156       return false;
     164    assert(temp);
    157165    int curid = temp->getGamestateID();
    158166   
    159167    if(gamestateID == 0){
    160168      temp->setGamestateID(GAMESTATEID_INITIAL);
    161       if(curid!=GAMESTATEID_INITIAL){
    162         assert(gamestateUsed.find(curid)!=gamestateUsed.end());
    163         --(gamestateUsed.find(curid)->second);
    164       }
    165169      return true;
    166170    }
    167     //if(curid > gamestateID)
     171   
    168172    assert(curid<gamestateID);
    169       // the network packets got messed up
    170       //return true;
    171173    COUT(4) << "acking gamestate " << gamestateID << " for clientid: " << clientID << " curid: " << curid << std::endl;
    172     // decrease usage of gamestate and save it
    173 //     deleteUnusedGameState(curid);
    174     //increase gamestateused
    175     std::map<int, int>::iterator it = gamestateUsed.find(curid);
    176     if(curid!=GAMESTATEID_INITIAL){
    177       if(it!=gamestateUsed.end())
    178         --(it->second);
    179     }
    180     it = gamestateUsed.find(gamestateID);
    181     if(it!=gamestateUsed.end()){
    182       ++(it->second);
    183       temp->setGamestateID(gamestateID);
    184     }
     174    std::map<int, packet::Gamestate*>::iterator it, tempit;
     175    for(it = gamestateMap_[clientID].begin(); it!=gamestateMap_[clientID].end() && it->first<gamestateID; it++){
     176      delete it->second;
     177      tempit=it++;
     178      gamestateMap_[clientID].erase(tempit);
     179    }
     180    temp->setGamestateID(gamestateID);
    185181    return true;
    186182  }
    187183
    188184  void GamestateManager::removeClient(ClientInformation* client){
    189     if(!client)
    190       return;
    191     if(client->getGamestateID()>=0)
    192       gamestateUsed[client->getGamestateID()]--;
     185    assert(client);
     186    std::map<unsigned int, std::map<int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(client->getID());
     187    // first delete all remained gamestates
     188    std::map<int, packet::Gamestate*>::iterator it;
     189    for(it=clientMap->second.begin(); it!=clientMap->second.end(); it++)
     190      delete it->second;
     191    // now delete the clients gamestatemap
     192    gamestateMap_.erase(clientMap);
    193193  }
    194194 
    195195  bool GamestateManager::processGamestate(packet::Gamestate *gs){
    196196    if(gs->isCompressed())
    197        assert(gs->decompressData());
     197    {
     198       bool b = gs->decompressData();
     199       assert(b);
     200    }
    198201    assert(!gs->isDiffed());
    199202    return gs->spreadData();
Note: See TracChangeset for help on using the changeset viewer.