Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core4/src/network/GamestateManager.cc @ 3279

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