Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/network/GamestateManager.cc @ 2266

Last change on this file since 2266 was 2176, checked in by scheusso, 16 years ago

(re)implemented NACKing for gamestates

  • Property svn:eol-style set to native
File size: 6.3 KB
RevLine 
[1705]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Oliver Scheuss, (C) 2007
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29//
30// C++ Implementation: GameStateManager
31//
32// Description:
33//
34//
35// Author:  Oliver Scheuss, (C) 2007
36//
37// Copyright: See COPYING file that comes with this distribution
38//
39//
40
41#include "GamestateManager.h"
42
43#include <utility>
44#include <iostream>
45#include <zlib.h>
[1755]46#include <cassert>
[1705]47
48#include "core/CoreIncludes.h"
49#include "core/BaseObject.h"
50#include "ClientInformation.h"
51#include "Synchronisable.h"
[2176]52#include "packet/Acknowledgement.h"
[1705]53
[2171]54namespace orxonox
[1705]55{
56  GamestateManager::GamestateManager() {
57    id_=0;
58  }
59
60  GamestateManager::~GamestateManager() {
61  }
62
63  bool GamestateManager::update(){
[1907]64//     cleanup();
[1730]65    return getSnapshot();
[1705]66  }
[2087]67
68  bool GamestateManager::add(packet::Gamestate *gs, unsigned int clientID){
[1705]69    assert(gs);
[2087]70    std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateQueue.find(clientID);
[1705]71    if(it!=gamestateQueue.end()){
72      // delete obsolete gamestate
73      delete it->second;
74    }
75    gamestateQueue[clientID] = gs;
76    return true;
77  }
[2087]78
[1705]79  bool GamestateManager::processGamestates(){
[2087]80    std::map<unsigned int, packet::Gamestate*>::iterator it;
[1705]81    // now push only the most recent gamestates we received (ignore obsolete ones)
82    for(it = gamestateQueue.begin(); it!=gamestateQueue.end(); it++){
[2087]83      bool b = processGamestate(it->second);
84      assert(b);
[1705]85      delete it->second;
86    }
87    // now clear the queue
88    gamestateQueue.clear();
89    return true;
90  }
[2087]91
92
[1705]93  bool GamestateManager::getSnapshot(){
94    reference = new packet::Gamestate();
[2087]95    if(!reference->collectData(++id_)){ //we have no data to send
96      delete reference;
97      reference=0;
98    }
[1705]99    return true;
100  }
[2087]101
[1705]102  /**
103   * this function is used to keep the memory usage low
104   * it tries to delete all the unused gamestates
[2087]105   *
106   *
[1705]107   */
[1907]108/*  void GamestateManager::cleanup(){
[1705]109    std::map<int,int>::iterator it = gamestateUsed.begin();
110    while(it!=gamestateUsed.end()){
111      if((id_-(*it).first)<KEEP_GAMESTATES)
112        break;
113      if( (*it).second <= 0 ){
114        COUT(5) << "GameStateManager: deleting gamestate with id: " << (*it).first << ", uses: " << (*it).second << std::endl;
115        std::map<int, packet::Gamestate *>::iterator tempit = gamestateMap.find((*it).first);
116        if( tempit != gamestateMap.end() ){
117          packet::Gamestate *temp = tempit->second;
118          if(temp){
119            delete gamestateMap[(*it).first];
120            gamestateMap.erase((*it).first);
121          }
122        }
123        gamestateUsed.erase(it++);
124        continue;
125      }
126      it++;
127    }
[1907]128  }*/
[1705]129
[2087]130  packet::Gamestate *GamestateManager::popGameState(unsigned int clientID) {
[1705]131    //why are we searching the same client's gamestate id as we searched in
132    //Server::sendGameState?
[1751]133    packet::Gamestate *gs;
[2087]134    unsigned int gID = ClientInformation::findClient(clientID)->getGamestateID();
135    if(!reference)
136      return 0;
[1907]137    gs = reference->doSelection(clientID);
138//     gs = new packet::Gamestate(*reference);
139//     gs = new packet::Gamestate(*reference);
140    // save the (undiffed) gamestate in the clients gamestate map
141    gamestateMap_[clientID].insert(std::pair<int, packet::Gamestate*>(gs->getID(), gs));
[1705]142    //chose wheather the next gamestate is the first or not
[1907]143    packet::Gamestate *client=NULL;
[1705]144    if(gID != GAMESTATEID_INITIAL){
[2087]145      std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(clientID);
[1907]146      if(clientMap!=gamestateMap_.end()){
[2087]147        std::map<unsigned int, packet::Gamestate*>::iterator it = clientMap->second.find(gID);
[1907]148        if(it!=clientMap->second.end())
149          client = it->second;
150      }
[1705]151    }
[1907]152    if(client){
153//       COUT(3) << "diffing" << std::endl;
154      gs = gs->diff(client);
155    }
156    else{
157//       COUT(3) << "not diffing" << std::endl;
158      gs = new packet::Gamestate(*gs);
159    }
160    bool b = gs->compressData();
161    assert(b);
[1751]162    return gs;
[1705]163  }
[2087]164
165
166  bool GamestateManager::ack(unsigned int gamestateID, unsigned int clientID) {
[1705]167    ClientInformation *temp = ClientInformation::findClient(clientID);
[1907]168    assert(temp);
[2087]169    unsigned int curid = temp->getGamestateID();
170
[2176]171    if(gamestateID == ACKID_NACK){
[1705]172      temp->setGamestateID(GAMESTATEID_INITIAL);
[1769]173      return true;
[1705]174    }
[2087]175
176    assert(curid==(unsigned int)GAMESTATEID_INITIAL || curid<gamestateID);
[1705]177    COUT(4) << "acking gamestate " << gamestateID << " for clientid: " << clientID << " curid: " << curid << std::endl;
[2087]178    std::map<unsigned int, packet::Gamestate*>::iterator it, tempit;
[1907]179    for(it = gamestateMap_[clientID].begin(); it!=gamestateMap_[clientID].end() && it->first<gamestateID; it++){
180      delete it->second;
181      tempit=it++;
182      gamestateMap_[clientID].erase(tempit);
[1705]183    }
[1907]184    temp->setGamestateID(gamestateID);
[1705]185    return true;
186  }
187
188  void GamestateManager::removeClient(ClientInformation* client){
[1907]189    assert(client);
[2087]190    std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(client->getID());
[1907]191    // first delete all remained gamestates
[2087]192    std::map<unsigned int, packet::Gamestate*>::iterator it;
[1907]193    for(it=clientMap->second.begin(); it!=clientMap->second.end(); it++)
194      delete it->second;
195    // now delete the clients gamestatemap
196    gamestateMap_.erase(clientMap);
[1705]197  }
[2087]198
[1712]199  bool GamestateManager::processGamestate(packet::Gamestate *gs){
[1751]200    if(gs->isCompressed())
[1907]201    {
202       bool b = gs->decompressData();
203       assert(b);
204    }
[1712]205    assert(!gs->isDiffed());
206    return gs->spreadData();
207  }
[1705]208
209}
Note: See TracBrowser for help on using the repository browser.