Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged netp4 back to trunk

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