Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2732 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
Line 
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>
46#include <cassert>
47
48#include "core/CoreIncludes.h"
49#include "core/BaseObject.h"
50#include "ClientInformation.h"
51#include "Synchronisable.h"
52#include "packet/Acknowledgement.h"
53
54namespace orxonox
55{
56  GamestateManager::GamestateManager() {
57    id_=0;
58  }
59
60  GamestateManager::~GamestateManager() {
61  }
62
63  bool GamestateManager::update(){
64//     cleanup();
65    return getSnapshot();
66  }
67
68  bool GamestateManager::add(packet::Gamestate *gs, unsigned int clientID){
69    assert(gs);
70    std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateQueue.find(clientID);
71    if(it!=gamestateQueue.end()){
72      // delete obsolete gamestate
73      delete it->second;
74    }
75    gamestateQueue[clientID] = gs;
76    return true;
77  }
78
79  bool GamestateManager::processGamestates(){
80    std::map<unsigned int, packet::Gamestate*>::iterator it;
81    // now push only the most recent gamestates we received (ignore obsolete ones)
82    for(it = gamestateQueue.begin(); it!=gamestateQueue.end(); it++){
83      bool b = processGamestate(it->second);
84      assert(b);
85      delete it->second;
86    }
87    // now clear the queue
88    gamestateQueue.clear();
89    return true;
90  }
91
92
93  bool GamestateManager::getSnapshot(){
94    reference = new packet::Gamestate();
95    if(!reference->collectData(++id_)){ //we have no data to send
96      delete reference;
97      reference=0;
98    }
99    return true;
100  }
101
102  /**
103   * this function is used to keep the memory usage low
104   * it tries to delete all the unused gamestates
105   *
106   *
107   */
108/*  void GamestateManager::cleanup(){
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    }
128  }*/
129
130  packet::Gamestate *GamestateManager::popGameState(unsigned int clientID) {
131    //why are we searching the same client's gamestate id as we searched in
132    //Server::sendGameState?
133    packet::Gamestate *gs;
134    unsigned int gID = ClientInformation::findClient(clientID)->getGamestateID();
135    if(!reference)
136      return 0;
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));
142    //chose wheather the next gamestate is the first or not
143    packet::Gamestate *client=NULL;
144    if(gID != GAMESTATEID_INITIAL){
145      std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(clientID);
146      if(clientMap!=gamestateMap_.end()){
147        std::map<unsigned int, packet::Gamestate*>::iterator it = clientMap->second.find(gID);
148        if(it!=clientMap->second.end())
149          client = it->second;
150      }
151    }
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);
162    return gs;
163  }
164
165
166  bool GamestateManager::ack(unsigned int gamestateID, unsigned int clientID) {
167    ClientInformation *temp = ClientInformation::findClient(clientID);
168    assert(temp);
169    unsigned int curid = temp->getGamestateID();
170
171    if(gamestateID == ACKID_NACK){
172      temp->setGamestateID(GAMESTATEID_INITIAL);
173      return true;
174    }
175
176    assert(curid==(unsigned int)GAMESTATEID_INITIAL || curid<gamestateID);
177    COUT(4) << "acking gamestate " << gamestateID << " for clientid: " << clientID << " curid: " << curid << std::endl;
178    std::map<unsigned int, packet::Gamestate*>::iterator it, tempit;
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);
183    }
184    temp->setGamestateID(gamestateID);
185    return true;
186  }
187
188  void GamestateManager::removeClient(ClientInformation* client){
189    assert(client);
190    std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(client->getID());
191    // first delete all remained gamestates
192    std::map<unsigned int, packet::Gamestate*>::iterator it;
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);
197  }
198
199  bool GamestateManager::processGamestate(packet::Gamestate *gs){
200    if(gs->isCompressed())
201    {
202       bool b = gs->decompressData();
203       assert(b);
204    }
205    assert(!gs->isDiffed());
206    return gs->spreadData();
207  }
208
209}
Note: See TracBrowser for help on using the repository browser.