Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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