Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/network/GameStateManager.cc @ 1016

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

blub

File size: 11.1 KB
Line 
1/*
2*   ORXONOX - the hottest 3D action shooter ever to exist
3*
4*
5*   License notice:
6*
7*   This program is free software; you can redistribute it and/or
8*   modify it under the terms of the GNU General Public License
9*   as published by the Free Software Foundation; either version 2
10*   of the License, or (at your option) any later version.
11*
12*   This program is distributed in the hope that it will be useful,
13*   but WITHOUT ANY WARRANTY; without even the implied warranty of
14*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*   GNU General Public License for more details.
16*
17*   You should have received a copy of the GNU General Public License
18*   along with this program; if not, write to the Free Software
19*   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20*
21*   Author:
22*      Oliver Scheuss, (C) 2007
23*   Co-authors:
24*      ...
25*
26*/
27
28//
29// C++ Implementation: GameStateManager
30//
31// Description:
32//
33//
34// Author:  Oliver Scheuss, (C) 2007
35//
36// Copyright: See COPYING file that comes with this distribution
37//
38//
39
40#include <utility>
41#include <iostream>
42#include <zlib.h>
43
44#include "core/CoreIncludes.h"
45
46#include "ClientInformation.h"
47#include "Synchronisable.h"
48#include "GameStateManager.h"
49
50namespace network
51{
52  GameStateManager::GameStateManager(ClientInformation *head) {
53    id=0;
54    head_=head;
55  }
56
57  GameStateManager::~GameStateManager() {
58  }
59
60  void GameStateManager::update(){
61    cleanup();
62    reference = getSnapshot(id);
63    gameStateMap.insert(std::pair<int, GameState*>(id, reference));
64    gameStateUsed[id]=0;
65    ++id;
66    return;
67  }
68 
69 
70  /**
71   * this function is used to keep the memory usage low
72   * it tries to delete all the unused gamestates
73   *
74   *
75   */
76  void GameStateManager::cleanup(){
77    std::map<int,int>::iterator it = gameStateUsed.begin();
78    while(it!=gameStateUsed.end()){
79      if( (*it).second <= 0 ){
80        free(gameStateMap[(*it).first]->data);
81        delete gameStateMap[(*it).first];
82        gameStateMap.erase((*it).first);
83        gameStateUsed.erase(it++);
84      }else  //as soon as we got a used gamestate break here because we could use newer gamestates in future
85        break;
86    }
87  }
88
89  GameStateCompressed *GameStateManager::popGameState(int clientID) {
90    //why are we searching the same client's gamestate id as we searched in
91    //Server::sendGameState?
92    int gID = head_->findClient(clientID)->getGamestateID();
93    COUT(4) << "G.St.Man: popgamestate: sending gstate_id: " << id << " diffed from: " << gID << " (not diffed yet)" << std::endl;
94   
95    //chose wheather the next gamestate is the first or not
96    if(gID != GAMESTATEID_INITIAL){
97      GameState *client = gameStateMap[gID];
98      GameState *server = reference;
99      //head_->findClient(clientID)->setGamestateID(id);
100      return encode(client, server);
101    } else {
102      GameState *server = reference;
103      //head_->findClient(clientID)->setGamestateID(id);
104      return encode(server);
105      // return an undiffed gamestate and set appropriate flags
106    }
107  }
108
109  /**
110  * This function goes through the whole list of synchronisables and
111  * saves all the synchronisables to a flat "list".
112  * @return struct of type gamestate containing the size of the whole gamestate and a pointer linking to the flat list
113  */
114  GameState *GameStateManager::getSnapshot(int id)
115  {
116    //std::cout << "begin getSnapshot" << std::endl;
117    //the size of the gamestate
118    int totalsize=0;
119    int memsize=1000;
120    //the size of one specific synchronisable
121    int tempsize=0;
122    // get the start of the Synchronisable list
123    orxonox::Iterator<Synchronisable> it;
124    // struct for return value of Synchronisable::getData()
125    syncData sync;
126
127    GameState *retval=new GameState; //return value
128    retval->id=id++;
129    COUT(4) << "G.ST.Man: producing gamestate with id: " << retval->id << std::endl;
130    // reserve a little memory and increase it later on
131    COUT(5) << "G.ST.Man: mallocing: " << memsize << std::endl;
132    retval->data = (unsigned char*)malloc(memsize);
133    COUT(5) << "G.ST.Man: malloced: " << memsize << std::endl;
134
135    // offset of memory functions
136    int offset=0;
137    // go through all Synchronisables
138    for(it = orxonox::ObjectList<Synchronisable>::start(); it; ++it){
139      //std::cout << "begin inner loop" << std::endl;
140      //std::cout << "gamestatemanager: in for loop" << std::endl;
141      //get size of the synchronisable
142      tempsize=it->getSize();
143      //COUT(5) << "size of temp gamestate: " << tempsize << std::endl;
144      //COUT(2) << "size of synchronisable: " << tempsize << std::endl;
145      // add place for data and 3 ints (length,classid,objectid)
146      totalsize+=tempsize+3*sizeof(int);
147      //std::cout << "totalsize: " << totalsize << std::endl;
148      //COUT(5) << "G.St.Man: current totalsize=" << totalsize << std::endl;
149      //COUT(5) << "G.St.Man: current it->classID=" << it->classID << " it->objectID=" << it->objectID << std::endl;
150      // allocate additional space
151      if((totalsize+tempsize) > memsize){
152        COUT(5) << "G.St.Man: need additional memory" << std::endl;
153        if(tempsize < 1000){
154          retval->data = (unsigned char *)realloc((void *)retval->data, totalsize+1000);
155          memsize+=1000;
156        } else {
157          retval->data = (unsigned char *)realloc((void *)retval->data, totalsize+1000);
158          memsize+=tempsize+1000;
159        }
160        COUT(5) << "G.St.Man: additional space allocation finished" << std::endl;
161      }
162
163      // run Synchronisable::getData with offset and additional place for 3 ints in between (for ids and length)
164      sync=it->getData((retval->data)+offset+3*sizeof(int));
165      memcpy(retval->data+offset, (void *)&sync.length, sizeof(int));
166      //*(retval->data+offset)=sync.length;
167      memcpy(retval->data+offset+sizeof(int), (void *)&sync.objectID, sizeof(int));
168      //*(retval->data+offset+sizeof(int))=sync.objectID;
169      memcpy(retval->data+offset+2*sizeof(int), (void *)&sync.classID, sizeof(int));
170      //*(retval->data+offset+2*sizeof(int))=sync.classID;
171      // increase data pointer
172      offset+=tempsize+3*sizeof(int);
173      //std::cout << "end inner loop" << std::endl;
174    }
175    retval->size=totalsize;
176    //#### bugfix
177    retval->diffed = false;
178    //std::cout << "end snapShot" << std::endl;
179    COUT(5) << "G.ST.Man: Gamestate size: " << totalsize << std::endl;
180    return retval;
181  }
182
183  //##### ADDED FOR TESTING PURPOSE #####
184  GameStateCompressed* GameStateManager::testCompress( GameState* g ) {
185    return compress_( g );
186  }
187
188  GameState* GameStateManager::testDiff( GameState* a, GameState* b ) {
189    return diff( a, b );
190  }
191  //##### END TESTING PURPOSE #####
192
193  GameStateCompressed *GameStateManager::encode(GameState *a, GameState *b) {
194    COUT(5) << "G.St.Man: this will be a DIFFED gamestate" << std::endl;
195    //GameState r = diff(a,b);
196    //r.diffed = true;
197    GameState *r = b;
198    r->diffed = false;
199    //return compress_(r);
200    GameStateCompressed *g = new GameStateCompressed;
201    g->base_id = b->base_id;
202    g->id = b->id;
203    g->diffed = b->diffed;
204    g->data = b->data;
205    g->normsize = b->size;
206    g->compsize = b->size;
207    return g;
208  }
209
210  GameStateCompressed *GameStateManager::encode(GameState *a) {
211    COUT(5) << "G.St.Man: this will be a not diffed gamestate" << std::endl;
212    a->diffed=false;
213    GameStateCompressed *g = new GameStateCompressed;
214    g->base_id = a->base_id;
215    g->id = a->id;
216    g->diffed = a->diffed;
217    g->data = a->data;
218    g->normsize = a->size;
219    g->compsize = a->size;
220    return g;
221  }
222
223  GameState *GameStateManager::diff(GameState *a, GameState *b) {
224    unsigned char *ap = a->data, *bp = b->data;
225    int of=0; // pointers offset
226    int dest_length=0;
227    if(a->size>=b->size)
228      dest_length=a->size;
229    else
230      dest_length=b->size;
231    unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char));
232    while(of<a->size && of<b->size){
233      *(dp+of)=*(ap+of)^*(bp+of); // do the xor
234      ++of;
235    }
236    if(a->size!=b->size){ // do we have to fill up ?
237      unsigned char n=0;
238      if(a->size<b->size){
239        while(of<dest_length){
240          *(dp+of)=n^*(bp+of);
241          of++;
242        }
243      } else{
244        while(of<dest_length){
245          *(dp+of)=*(ap+of)^n;
246          of++;
247        }
248      }
249    }
250   
251    GameState *r = new GameState;
252    r->id = b->id;
253    r->size = dest_length;
254    r->diffed = true;
255    r->base_id = a->id;
256    r->data = dp;
257    return r;
258  }
259
260  GameStateCompressed *GameStateManager::compress_(GameState *a) {
261    COUT(4) << "G.St.Man: compressing gamestate" << std::endl;
262    COUT(4) << "G.St.Man: a: id: " << a->id << " base_id: " << a->base_id << " size: " << a->size << " diffed: " << a->diffed << std::endl;
263    int size = a->size;
264    uLongf buffer = (uLongf)((a->size + 12)*1.01)+1;
265    COUT(4) << "size: " << size << ", buffer: " << buffer << std::endl;
266    unsigned char* dest = (unsigned char*)malloc( buffer );
267    COUT(4) << "dest: " << dest << std::endl;
268    int retval;
269    //std::cout << "before ziped " << buffer << std::endl;
270    retval = compress( dest, &buffer, a->data, (uLong)size );
271    COUT(4) << "bloablabla aft3er compress" << std::endl;
272    //std::cout << "after ziped " << buffer << std::endl;
273
274    switch ( retval ) {
275      case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break;
276      case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl; 
277      return NULL;
278      case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl;
279      return NULL;
280      case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl;
281      return NULL;
282    }
283
284    GameStateCompressed *compressedGamestate = new GameStateCompressed;
285    compressedGamestate->compsize = buffer;
286//     std::cout << "compressedGamestate.compsize = buffer; " << buffer << std::endl;
287    compressedGamestate->normsize = size;
288//     std::cout << "compressedGamestate.normsize = size; " << size << std::endl;
289    compressedGamestate->id = a->id;
290    compressedGamestate->data = dest;
291    compressedGamestate->diffed = a->diffed;
292    compressedGamestate->base_id = a->base_id;
293    COUT(5) << "G.St.Man: saved compressed data in GameStateCompressed:" << std::endl;
294    return compressedGamestate;
295  }
296
297  void GameStateManager::ackGameState(int clientID, int gamestateID) {
298    ClientInformation *temp = head_->findClient(clientID);
299    int curid = temp->getID();
300    // decrease usage of gamestate and save it
301    deleteUnusedGameState(curid);
302    //increase gamestateused
303    ++gameStateUsed.find(gamestateID)->second;
304    temp->setGamestateID(gamestateID);
305    /*
306    GameState *old = clientGameState[clientID];
307    deleteUnusedGameState(old);
308    clientGameState[clientID]=idGameState[gamestateID];*/
309  }
310
311  bool GameStateManager::deleteUnusedGameState(int gamestateID) {
312    int used = --(gameStateUsed.find(gamestateID)->second);
313    if(id-gamestateID>KEEP_GAMESTATES && used==0){
314      // delete gamestate
315      delete gameStateMap.find(gamestateID)->second;
316      gameStateMap.erase(gamestateID);
317      return true;
318    }
319    return false;
320  }
321
322}
Note: See TracBrowser for help on using the repository browser.