Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/network/GameStateManager.cc @ 555

Last change on this file since 555 was 514, checked in by nicolasc, 17 years ago

added copyright notice

File size: 6.0 KB
RevLine 
[514]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
[247]28//
29// C++ Implementation: GameStateManager
30//
[514]31// Description:
[247]32//
33//
34// Author:  Oliver Scheuss, (C) 2007
35//
36// Copyright: See COPYING file that comes with this distribution
37//
38//
39#include "GameStateManager.h"
40
41namespace network {
42
[436]43GameStateManager::GameStateManager(ClientInformation *head)
[247]44{
[413]45  id=0;
[436]46  head_=head;
[247]47}
48
49GameStateManager::~GameStateManager()
50{
51}
52
[413]53void GameStateManager::update(){
[422]54  reference = getSnapshot(id);
[436]55  gameStateMap.insert(std::pair<int, GameState*>(id, reference));
56  gameStateUsed[id]=0;
[422]57  ++id;
[413]58  return;
59}
60
61GameStateCompressed GameStateManager::popGameState(int clientID){
[436]62  int gID = head_->findClient(clientID)->getGamestateID();
63  if(gID!=GAMESTATEID_INITIAL){
64    GameState *client = gameStateMap[gID];
65    GameState *server = reference;
66    return encode(client, server);
67  }
[422]68  GameState *server = reference;
[436]69  return encode(server);
70  // return an undiffed gamestate and set appropriate flags
[413]71}
72
73
74
[332]75/**
[514]76 * This function goes through the whole list of synchronisables and
[332]77 * saves all the synchronisables to a flat "list".
78 * @return struct of type gamestate containing the size of the whole gamestate and a pointer linking to the flat list
79 */
[422]80GameState *GameStateManager::getSnapshot(int id)
[332]81{
82  //the size of the gamestate
83  int totalsize=0;
84  //the size of one specific synchronisable
85  int tempsize=0;
86  // get the start of the Synchronisable list
87  orxonox::Iterator<Synchronisable> it;
88  // struct for return value of Synchronisable::getData()
89  syncData sync;
[514]90
[422]91  GameState *retval=new GameState; //return value
92  retval->id=id;
[332]93  // reserve a little memory and increase it later on
[422]94  retval->data = (unsigned char*)malloc(1);
[514]95
[332]96  // offset of memory functions
97  int offset=0;
98  // go through all Synchronisables
99  for(it = orxonox::ObjectList<Synchronisable>::start(); it != 0; ++it){
100    //get size of the synchronisable
101    tempsize=it->getSize();
102    // add place for data and 3 ints (length,classid,objectid)
103    totalsize+=tempsize+3*sizeof(int);
104    // allocate additional space
[422]105    retval->data = (unsigned char *)realloc((void *)retval->data, totalsize);
[514]106
[332]107    // run Synchronisable::getData with offset and additional place for 3 ints in between (for ids and length)
[422]108    sync=it->getData(retval->data+offset+3*sizeof(int));
109    *(retval->data+offset)=sync.length;
110    *(retval->data+offset+sizeof(int))=sync.objectID;
111    *(retval->data+offset+2*sizeof(int))=sync.classID;
[332]112    // increase data pointer
113    offset+=tempsize+3*sizeof(int);
114  }
[422]115  retval->size=totalsize;
[413]116  return retval;
[247]117}
118
119
[332]120
[413]121GameStateCompressed GameStateManager::encode(GameState *a, GameState *b){
[436]122    GameState r = diff(a,b);
123  r.diffed = true;
124  return compress_(&r);
[385]125}
[332]126
[436]127GameStateCompressed GameStateManager::encode(GameState *a){
128  a->diffed=false;
129  return compress_(a);
130}
[332]131
[413]132GameState GameStateManager::diff(GameState *a, GameState *b){
133  unsigned char *ap = a->data, *bp = b->data;
[385]134  int of=0; // pointers offset
135  int dest_length=0;
[413]136  if(a->size>=b->size)
137    dest_length=a->size;
[385]138  else
[413]139    dest_length=b->size;
[385]140  unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char));
[413]141  while(of<a->size && of<b->size){
[385]142    *(dp+of)=*(ap+of)^*(bp+of); // do the xor
143    ++of;
144  }
[413]145  if(a->size!=b->size){ // do we have to fill up ?
[385]146    unsigned char n=0;
[413]147    if(a->size<b->size){
[385]148      while(of<dest_length){
149        *(dp+of)=n^*(bp+of);
150        of++;
151      }
152    } else{
153      while(of<dest_length){
154        *(dp+of)=*(ap+of)^n;
155        of++;
156      }
157    }
158  }
159  // should be finished now
[413]160  GameState r = {b->id, dest_length, dp};
[385]161  return r;
162}
[332]163
[436]164GameStateCompressed GameStateManager::compress_(GameState *a) {
165  int size = a->size;
166  uLongf buffer = (uLongf)((a->size + 12)*1.01)+1;
[407]167  unsigned char* dest = (unsigned char*)malloc( buffer );
168  int retval;
[514]169  retval = compress( dest, &buffer, a->data, (uLong)size );
170
[407]171  switch ( retval ) {
[413]172  case Z_OK: std::cout << "successfully compressed" << std::endl; break;
173  case Z_MEM_ERROR: std::cout << "not enough memory available" << std::endl; break;
174  case Z_BUF_ERROR: std::cout << "not enough memory available in the buffer" << std::endl; break;
175  case Z_DATA_ERROR: std::cout << "data corrupted" << std::endl; break;
[407]176  }
[514]177
[413]178  GameStateCompressed compressedGamestate;
[407]179  compressedGamestate.compsize = buffer;
180  compressedGamestate.normsize = size;
181  compressedGamestate.id = GAMESTATE;
182  compressedGamestate.data = dest;
[436]183  compressedGamestate.diffed = a->diffed;
[514]184
[407]185  return compressedGamestate;
[385]186}
187
[422]188void GameStateManager::ackGameState(int clientID, int gamestateID){
[436]189  ClientInformation *temp = head_->findClient(clientID);
190  int curid = temp->getID();
191  // decrease usage of gamestate and save it
192  deleteUnusedGameState(curid);
193  //increase gamestateused
194  ++gameStateUsed.find(gamestateID)->second;
195  temp->setGamestateID(gamestateID);
196  /*
[422]197  GameState *old = clientGameState[clientID];
198  deleteUnusedGameState(old);
[436]199  clientGameState[clientID]=idGameState[gamestateID];*/
[422]200}
[385]201
[436]202bool GameStateManager::deleteUnusedGameState(int gamestateID){
203  int used = --(gameStateUsed.find(gamestateID)->second);
204  if(id-gamestateID>KEEP_GAMESTATES && used==0){
205    // delete gamestate
206    delete gameStateMap.find(gamestateID)->second;
207    gameStateMap.erase(gamestateID);
208    return true;
[422]209  }
[436]210  return false;
[422]211}
[413]212
[385]213}
214
215
Note: See TracBrowser for help on using the repository browser.