Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/network/GameStateManager.cc @ 1097

Last change on this file since 1097 was 1064, checked in by rgrieder, 17 years ago
  • replaced all String2Number with ConvertValue
  • replaced all tokenize with SubString
  • dealt with warnings under msvc
  • removed some warnings by placing casts
  • bugfix in audio: local variable pushed into member variable std::vector
  • updated StableHeaders.h
File size: 8.1 KB
RevLine 
[1056]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 */
[514]28
[247]29//
30// C++ Implementation: GameStateManager
31//
[514]32// Description:
[247]33//
34//
35// Author:  Oliver Scheuss, (C) 2007
36//
37// Copyright: See COPYING file that comes with this distribution
38//
39//
40
[1062]41#include "GameStateManager.h"
42
[777]43#include <utility>
44#include <iostream>
45#include <zlib.h>
[247]46
[777]47#include "core/CoreIncludes.h"
48#include "ClientInformation.h"
49#include "Synchronisable.h"
[247]50
[777]51namespace network
[247]52{
[777]53  GameStateManager::GameStateManager(ClientInformation *head) {
54    id=0;
55    head_=head;
56  }
[247]57
[777]58  GameStateManager::~GameStateManager() {
59  }
[413]60
[777]61  void GameStateManager::update(){
62    reference = getSnapshot(id);
63    gameStateMap.insert(std::pair<int, GameState*>(id, reference));
64    gameStateUsed[id]=0;
65    ++id;
66    return;
[436]67  }
[413]68
[1021]69  GameStateCompressed *GameStateManager::popGameState(int clientID) {
[777]70    int gID = head_->findClient(clientID)->getGamestateID();
[1021]71    COUT(4) << "popgamestate: sending gstate id: " << id << "diffed from: " << gID << std::endl;
[777]72    if(gID!=GAMESTATEID_INITIAL){
73      GameState *client = gameStateMap[gID];
74      GameState *server = reference;
75      //head_->findClient(clientID)->setGamestateID(id);
76      return encode(client, server);
77    } else {
78      GameState *server = reference;
79      //head_->findClient(clientID)->setGamestateID(id);
80      return encode(server);
81      // return an undiffed gamestate and set appropriate flags
82    }
83  }
[413]84
[777]85  /**
86  * This function goes through the whole list of synchronisables and
87  * saves all the synchronisables to a flat "list".
88  * @return struct of type gamestate containing the size of the whole gamestate and a pointer linking to the flat list
89  */
90  GameState *GameStateManager::getSnapshot(int id)
91  {
92    //the size of the gamestate
93    int totalsize=0;
94    int memsize=1000;
95    //the size of one specific synchronisable
96    int tempsize=0;
97    // get the start of the Synchronisable list
98    orxonox::Iterator<Synchronisable> it;
99    // struct for return value of Synchronisable::getData()
100    syncData sync;
[413]101
[777]102    GameState *retval=new GameState; //return value
103    retval->id=id++;
[1021]104    COUT(4) << "producing gamestate with id: " << retval->id << std::endl;
[777]105    // reserve a little memory and increase it later on
[1021]106    COUT(5) << "mallocing" << std::endl;
[777]107    retval->data = (unsigned char*)malloc(memsize);
[1021]108    COUT(5) << "malloced" << std::endl;
[514]109
[777]110    // offset of memory functions
111    int offset=0;
112    // go through all Synchronisables
[871]113    for(it = orxonox::ObjectList<Synchronisable>::start(); it; ++it){
[777]114      //std::cout << "gamestatemanager: in for loop" << std::endl;
115      //get size of the synchronisable
116      tempsize=it->getSize();
[1021]117//       COUT(5) << "size of temp gamestate: " << tempsize << std::endl;
[777]118      //COUT(2) << "size of synchronisable: " << tempsize << std::endl;
119      // add place for data and 3 ints (length,classid,objectid)
120      totalsize+=tempsize+3*sizeof(int);
121      //std::cout << "totalsize: " << totalsize << std::endl;
122      // allocate additional space
123      if(totalsize+tempsize>memsize){
124        if(tempsize<1000){
125          retval->data = (unsigned char *)realloc((void *)retval->data, totalsize+1000);
126          memsize+=1000;
127        } else {
128          retval->data = (unsigned char *)realloc((void *)retval->data, totalsize+1000);
129          memsize+=tempsize+1000;
130        }
131      }
[514]132
[777]133      // run Synchronisable::getData with offset and additional place for 3 ints in between (for ids and length)
134      sync=it->getData((retval->data)+offset+3*sizeof(int));
[1064]135      *(retval->data+offset)=(unsigned char)sync.length;
136      *(retval->data+offset+sizeof(int))=(unsigned char)sync.objectID;
137      *(retval->data+offset+2*sizeof(int))=(unsigned char)sync.classID;
[777]138      // increase data pointer
139      offset+=tempsize+3*sizeof(int);
[590]140    }
[1021]141    COUT(5) << "Gamestate size: " << totalsize << std::endl;
[777]142    retval->size=totalsize;
143    return retval;
144  }
[514]145
[1021]146  GameStateCompressed *GameStateManager::encode(GameState *a, GameState *b) {
[777]147    //GameState r = diff(a,b);
148    //r.diffed = true;
[1021]149    GameState *r = b;
150    r->diffed = false;
151    return compress_(r);
[332]152  }
[247]153
[1021]154  GameStateCompressed *GameStateManager::encode(GameState *a) {
[777]155    a->diffed=false;
156    return compress_(a);
157  }
[247]158
[1021]159  GameState *GameStateManager::diff(GameState *a, GameState *b) {
[777]160    unsigned char *ap = a->data, *bp = b->data;
161    int of=0; // pointers offset
162    int dest_length=0;
163    if(a->size>=b->size)
164      dest_length=a->size;
165    else
166      dest_length=b->size;
167    unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char));
168    while(of<a->size && of<b->size){
169      *(dp+of)=*(ap+of)^*(bp+of); // do the xor
170      ++of;
171    }
172    if(a->size!=b->size){ // do we have to fill up ?
173      unsigned char n=0;
174      if(a->size<b->size){
175        while(of<dest_length){
176          *(dp+of)=n^*(bp+of);
177          of++;
178        }
179      } else{
180        while(of<dest_length){
181          *(dp+of)=*(ap+of)^n;
182          of++;
183        }
[385]184      }
185    }
[1056]186
[1021]187    GameState *r = new GameState;
188    r->id = b->id;
189    r->size = dest_length;
190    r->diffed = true;
191    r->data = dp;
[777]192    return r;
[385]193  }
[332]194
[1021]195  GameStateCompressed *GameStateManager::compress_(GameState *a) {
196    COUT(5) << "compressing gamestate" << std::endl;
[777]197    int size = a->size;
198    uLongf buffer = (uLongf)((a->size + 12)*1.01)+1;
199    unsigned char* dest = (unsigned char*)malloc( buffer );
200    int retval;
201    //std::cout << "before ziped " << buffer << std::endl;
202    retval = compress( dest, &buffer, a->data, (uLong)size );
203    //std::cout << "after ziped " << buffer << std::endl;
[514]204
[777]205    switch ( retval ) {
[1021]206      case Z_OK: COUT(5) << "successfully compressed" << std::endl; break;
[1056]207      case Z_MEM_ERROR: COUT(1) << "not enough memory available in gamestate.compress" << std::endl;
[1021]208      return NULL;
209      case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer in gamestate.compress" << std::endl;
210      return NULL;
211      case Z_DATA_ERROR: COUT(2) << "decompress: data corrupted in gamestate.compress" << std::endl;
212      return NULL;
[777]213    }
[514]214
[1021]215    GameStateCompressed *compressedGamestate = new GameStateCompressed;
216    compressedGamestate->compsize = buffer;
217//     std::cout << "compressedGamestate.compsize = buffer; " << buffer << std::endl;
218    compressedGamestate->normsize = size;
219//     std::cout << "compressedGamestate.normsize = size; " << size << std::endl;
220    compressedGamestate->id = a->id;
221    compressedGamestate->data = dest;
222    compressedGamestate->diffed = a->diffed;
[514]223
[777]224    return compressedGamestate;
225  }
[385]226
[777]227  void GameStateManager::ackGameState(int clientID, int gamestateID) {
228    ClientInformation *temp = head_->findClient(clientID);
229    int curid = temp->getID();
230    // decrease usage of gamestate and save it
231    deleteUnusedGameState(curid);
232    //increase gamestateused
233    ++gameStateUsed.find(gamestateID)->second;
234    temp->setGamestateID(gamestateID);
235    /*
236    GameState *old = clientGameState[clientID];
237    deleteUnusedGameState(old);
238    clientGameState[clientID]=idGameState[gamestateID];*/
239  }
[385]240
[777]241  bool GameStateManager::deleteUnusedGameState(int gamestateID) {
242    int used = --(gameStateUsed.find(gamestateID)->second);
243    if(id-gamestateID>KEEP_GAMESTATES && used==0){
244      // delete gamestate
245      delete gameStateMap.find(gamestateID)->second;
246      gameStateMap.erase(gamestateID);
247      return true;
248    }
249    return false;
[422]250  }
[413]251
[385]252}
Note: See TracBrowser for help on using the repository browser.