Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/network/GamestateManager.cc @ 2341

Last change on this file since 2341 was 2341, checked in by scheusso, 15 years ago

made some changes, but now yet complete

  • Property svn:eol-style set to native
File size: 6.2 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 network
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    // save the (undiffed) gamestate in the clients gamestate map
138    gamestateMap_[clientID].insert(std::pair<int, packet::Gamestate*>(gs->getID(), gs));
139    //chose wheather the next gamestate is the first or not
140    packet::Gamestate *client=NULL;
141    if(gID != GAMESTATEID_INITIAL){
142      std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(clientID);
143      if(clientMap!=gamestateMap_.end()){
144        std::map<unsigned int, packet::Gamestate*>::iterator it = clientMap->second.find(gID);
145        if(it!=clientMap->second.end())
146          client = it->second;
147      }
148    }
149    if(client){
150//       COUT(3) << "diffing" << std::endl;
151      gs = gs->diff(client);
152    }
153    else{
154//       COUT(3) << "not diffing" << std::endl;
155      gs = new packet::Gamestate(*gs);
156    }
157    bool b = gs->compressData();
158    assert(b);
159    return gs;
160  }
161
162
163  bool GamestateManager::ack(unsigned int gamestateID, unsigned int clientID) {
164    ClientInformation *temp = ClientInformation::findClient(clientID);
165    assert(temp);
166    unsigned int curid = temp->getGamestateID();
167
168    if(gamestateID == 0){
169      temp->setGamestateID(GAMESTATEID_INITIAL);
170      return true;
171    }
172
173    assert(curid==(unsigned int)GAMESTATEID_INITIAL || curid<gamestateID);
174    COUT(4) << "acking gamestate " << gamestateID << " for clientid: " << clientID << " curid: " << curid << std::endl;
175    std::map<unsigned int, packet::Gamestate*>::iterator it, tempit;
176    for(it = gamestateMap_[clientID].begin(); it!=gamestateMap_[clientID].end() && it->first<gamestateID; it++){
177      delete it->second;
178      tempit=it++;
179      gamestateMap_[clientID].erase(tempit);
180    }
181    temp->setGamestateID(gamestateID);
182    return true;
183  }
184
185  void GamestateManager::removeClient(ClientInformation* client){
186    assert(client);
187    std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(client->getID());
188    // first delete all remained gamestates
189    std::map<unsigned int, packet::Gamestate*>::iterator it;
190    for(it=clientMap->second.begin(); it!=clientMap->second.end(); it++)
191      delete it->second;
192    // now delete the clients gamestatemap
193    gamestateMap_.erase(clientMap);
194  }
195
196  bool GamestateManager::processGamestate(packet::Gamestate *gs){
197    if(gs->isCompressed())
198    {
199       bool b = gs->decompressData();
200       assert(b);
201    }
202    assert(!gs->isDiffed());
203    return gs->spreadData();
204  }
205
206}
Note: See TracBrowser for help on using the repository browser.