Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 899 was 894, checked in by scheusso, 18 years ago

added more bugfixing and debug output

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