Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/network/GamestateManager.cc @ 1705

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

further changes

File size: 6.0 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 <assert.h>
47
48#include "core/CoreIncludes.h"
49#include "core/BaseObject.h"
50#include "ClientInformation.h"
51#include "Synchronisable.h"
52
53namespace network
54{
55  GamestateManager::GamestateManager() {
56    id_=0;
57  }
58
59  GamestateManager::~GamestateManager() {
60  }
61
62  bool GamestateManager::update(){
63    cleanup();
64    getSnapshot();
65    return true;
66  }
67 
68  bool GamestateManager::add(packet::Gamestate *gs, int clientID){
69    assert(gs);
70    std::map<int, packet::Gamestate*>::iterator it = gamestateQueue.find(clientID);
71    if(it!=gamestateQueue.end()){
72      // delete obsolete gamestate
73      delete it->second;
74    }
75    gamestateQueue[clientID] = gs;
76    return true;
77  }
78 
79  bool GamestateManager::processGamestates(){
80    std::map<int, packet::Gamestate*>::iterator it;
81    // now push only the most recent gamestates we received (ignore obsolete ones)
82    for(it = gamestateQueue.begin(); it!=gamestateQueue.end(); it++){
83      it->second->spreadData();
84      delete it->second;
85    }
86    // now clear the queue
87    gamestateQueue.clear();
88    return true;
89  }
90 
91 
92  bool GamestateManager::getSnapshot(){
93    reference = new packet::Gamestate();
94    reference->collectData(++id_);
95    COUT(4) << "inserting gamestate: " << reference << std::endl;
96    gamestateMap.insert(std::pair<int, packet::Gamestate*>(id_, reference));
97    gamestateUsed[id_]=0;
98    return true;
99  }
100 
101  /**
102   * this function is used to keep the memory usage low
103   * it tries to delete all the unused gamestates
104   *
105   *
106   */
107  void GamestateManager::cleanup(){
108    std::map<int,int>::iterator it = gamestateUsed.begin();
109    while(it!=gamestateUsed.end()){
110      if((id_-(*it).first)<KEEP_GAMESTATES)
111        break;
112      if( (*it).second <= 0 ){
113        COUT(5) << "GameStateManager: deleting gamestate with id: " << (*it).first << ", uses: " << (*it).second << std::endl;
114        std::map<int, packet::Gamestate *>::iterator tempit = gamestateMap.find((*it).first);
115        if( tempit != gamestateMap.end() ){
116          packet::Gamestate *temp = tempit->second;
117          if(temp){
118            delete gamestateMap[(*it).first];
119            gamestateMap.erase((*it).first);
120          }
121        }
122        gamestateUsed.erase(it++);
123        continue;
124      }
125      it++;
126    }
127  }
128
129  packet::Gamestate *GamestateManager::popGameState(int clientID) {
130    //why are we searching the same client's gamestate id as we searched in
131    //Server::sendGameState?
132    int gID = ClientInformation::findClient(clientID)->getGamestateID();
133    COUT(4) << "G.St.Man: popgamestate: sending gstate_id: " << id_ << " diffed from: " << gID << std::endl;
134//     COUT(3) << "gamestatemap: " << &gameStateMap << std::endl;
135    //chose wheather the next gamestate is the first or not
136    if(gID != GAMESTATEID_INITIAL){
137      // TODO something with the gamestatemap is wrong
138      packet::Gamestate *client=NULL;
139      std::map<int, packet::Gamestate*>::iterator it = gamestateMap.find(gID);
140      if(it!=gamestateMap.end())
141        client = it->second;
142      packet::Gamestate *gs;
143      if(client)
144        gs = reference->diff(client);
145      else
146        gs = new packet::Gamestate(*reference);
147      gs->compressData();
148      return gs;
149    } else {
150      COUT(4) << "we got a GAMESTATEID_INITIAL for clientID: " << clientID << std::endl;
151//       ackGameState(clientID, reference->id);
152      return new packet::Gamestate(*reference);
153      // return an undiffed gamestate and set appropriate flags
154    }
155  }
156 
157 
158  bool GamestateManager::ack(int gamestateID, int clientID) {
159    ClientInformation *temp = ClientInformation::findClient(clientID);
160    if(temp==0)
161      return false;
162    int curid = temp->getGamestateID();
163   
164    if(gamestateID == GAMESTATEID_INITIAL){
165      temp->setGamestateID(GAMESTATEID_INITIAL);
166      if(curid!=GAMESTATEID_INITIAL){
167        assert(gamestateUsed.find(curid)!=gamestateUsed.end());
168        --(gamestateUsed.find(curid)->second);
169      }
170      return false;
171    }
172    if(curid > gamestateID)
173      // the network packets got messed up
174      return true;
175    COUT(4) << "acking gamestate " << gamestateID << " for clientid: " << clientID << " curid: " << curid << std::endl;
176    // decrease usage of gamestate and save it
177//     deleteUnusedGameState(curid);
178    //increase gamestateused
179    std::map<int, int>::iterator it = gamestateUsed.find(curid);
180    if(curid!=GAMESTATEID_INITIAL){
181      if(it!=gamestateUsed.end())
182        --(it->second);
183    }
184    it = gamestateUsed.find(gamestateID);
185    if(it!=gamestateUsed.end()){
186      ++(it->second);
187      temp->setGamestateID(gamestateID);
188    }
189    return true;
190  }
191
192  void GamestateManager::removeClient(ClientInformation* client){
193    if(!client)
194      return;
195    if(client->getGamestateID()>=0)
196      gamestateUsed[client->getGamestateID()]--;
197    ClientInformation::removeClient(client->getID());
198  }
199
200}
Note: See TracBrowser for help on using the repository browser.