Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/bugger/src/network/GamestateManager.cc @ 2531

Last change on this file since 2531 was 2531, checked in by rgrieder, 15 years ago

Merged revision 2371 to bugger branch.

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