Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/network/GamestateManager.cc @ 2337

Last change on this file since 2337 was 2171, checked in by landauf, 16 years ago

merged revisions 2111-2170 from objecthierarchy branch back to trunk.

  • 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"
52
[2171]53namespace orxonox
[1705]54{
55  GamestateManager::GamestateManager() {
56    id_=0;
57  }
58
59  GamestateManager::~GamestateManager() {
60  }
61
62  bool GamestateManager::update(){
[1907]63//     cleanup();
[1730]64    return getSnapshot();
[1705]65  }
[2087]66
67  bool GamestateManager::add(packet::Gamestate *gs, unsigned int clientID){
[1705]68    assert(gs);
[2087]69    std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateQueue.find(clientID);
[1705]70    if(it!=gamestateQueue.end()){
71      // delete obsolete gamestate
72      delete it->second;
73    }
74    gamestateQueue[clientID] = gs;
75    return true;
76  }
[2087]77
[1705]78  bool GamestateManager::processGamestates(){
[2087]79    std::map<unsigned int, packet::Gamestate*>::iterator it;
[1705]80    // now push only the most recent gamestates we received (ignore obsolete ones)
81    for(it = gamestateQueue.begin(); it!=gamestateQueue.end(); it++){
[2087]82      bool b = processGamestate(it->second);
83      assert(b);
[1705]84      delete it->second;
85    }
86    // now clear the queue
87    gamestateQueue.clear();
88    return true;
89  }
[2087]90
91
[1705]92  bool GamestateManager::getSnapshot(){
93    reference = new packet::Gamestate();
[2087]94    if(!reference->collectData(++id_)){ //we have no data to send
95      delete reference;
96      reference=0;
97    }
[1705]98    return true;
99  }
[2087]100
[1705]101  /**
102   * this function is used to keep the memory usage low
103   * it tries to delete all the unused gamestates
[2087]104   *
105   *
[1705]106   */
[1907]107/*  void GamestateManager::cleanup(){
[1705]108    std::map<int,int>::iterator it = gamestateUsed.begin();
109    while(it!=gamestateUsed.end()){
110      if((id_-(*it).first)<KEEP_GAMESTATES)
111        break;
112      if( (*it).second <= 0 ){
113        COUT(5) << "GameStateManager: deleting gamestate with id: " << (*it).first << ", uses: " << (*it).second << std::endl;
114        std::map<int, packet::Gamestate *>::iterator tempit = gamestateMap.find((*it).first);
115        if( tempit != gamestateMap.end() ){
116          packet::Gamestate *temp = tempit->second;
117          if(temp){
118            delete gamestateMap[(*it).first];
119            gamestateMap.erase((*it).first);
120          }
121        }
122        gamestateUsed.erase(it++);
123        continue;
124      }
125      it++;
126    }
[1907]127  }*/
[1705]128
[2087]129  packet::Gamestate *GamestateManager::popGameState(unsigned int clientID) {
[1705]130    //why are we searching the same client's gamestate id as we searched in
131    //Server::sendGameState?
[1751]132    packet::Gamestate *gs;
[2087]133    unsigned int gID = ClientInformation::findClient(clientID)->getGamestateID();
134    if(!reference)
135      return 0;
[1907]136    gs = reference->doSelection(clientID);
137//     gs = new packet::Gamestate(*reference);
138//     gs = new packet::Gamestate(*reference);
139    // save the (undiffed) gamestate in the clients gamestate map
140    gamestateMap_[clientID].insert(std::pair<int, packet::Gamestate*>(gs->getID(), gs));
[1705]141    //chose wheather the next gamestate is the first or not
[1907]142    packet::Gamestate *client=NULL;
[1705]143    if(gID != GAMESTATEID_INITIAL){
[2087]144      std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(clientID);
[1907]145      if(clientMap!=gamestateMap_.end()){
[2087]146        std::map<unsigned int, packet::Gamestate*>::iterator it = clientMap->second.find(gID);
[1907]147        if(it!=clientMap->second.end())
148          client = it->second;
149      }
[1705]150    }
[1907]151    if(client){
152//       COUT(3) << "diffing" << std::endl;
153      gs = gs->diff(client);
154    }
155    else{
156//       COUT(3) << "not diffing" << std::endl;
157      gs = new packet::Gamestate(*gs);
158    }
159    bool b = gs->compressData();
160    assert(b);
[1751]161    return gs;
[1705]162  }
[2087]163
164
165  bool GamestateManager::ack(unsigned int gamestateID, unsigned int clientID) {
[1705]166    ClientInformation *temp = ClientInformation::findClient(clientID);
[1907]167    assert(temp);
[2087]168    unsigned int curid = temp->getGamestateID();
169
[1769]170    if(gamestateID == 0){
[1705]171      temp->setGamestateID(GAMESTATEID_INITIAL);
[1769]172      return true;
[1705]173    }
[2087]174
175    assert(curid==(unsigned int)GAMESTATEID_INITIAL || curid<gamestateID);
[1705]176    COUT(4) << "acking gamestate " << gamestateID << " for clientid: " << clientID << " curid: " << curid << std::endl;
[2087]177    std::map<unsigned int, packet::Gamestate*>::iterator it, tempit;
[1907]178    for(it = gamestateMap_[clientID].begin(); it!=gamestateMap_[clientID].end() && it->first<gamestateID; it++){
179      delete it->second;
180      tempit=it++;
181      gamestateMap_[clientID].erase(tempit);
[1705]182    }
[1907]183    temp->setGamestateID(gamestateID);
[1705]184    return true;
185  }
186
187  void GamestateManager::removeClient(ClientInformation* client){
[1907]188    assert(client);
[2087]189    std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(client->getID());
[1907]190    // first delete all remained gamestates
[2087]191    std::map<unsigned int, packet::Gamestate*>::iterator it;
[1907]192    for(it=clientMap->second.begin(); it!=clientMap->second.end(); it++)
193      delete it->second;
194    // now delete the clients gamestatemap
195    gamestateMap_.erase(clientMap);
[1705]196  }
[2087]197
[1712]198  bool GamestateManager::processGamestate(packet::Gamestate *gs){
[1751]199    if(gs->isCompressed())
[1907]200    {
201       bool b = gs->decompressData();
202       assert(b);
203    }
[1712]204    assert(!gs->isDiffed());
205    return gs->spreadData();
206  }
[1705]207
208}
Note: See TracBrowser for help on using the repository browser.