Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/network/GamestateManager.cc @ 1990

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