Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

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