Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

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