Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

forgot something

File size: 9.4 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  void GameStateManager::cleanup(){
70    /*unsigned int min_id=-1;
71    int temp_id=0;
72    ClientInformation *temp = head_;
73    while(temp){
74      if(temp->head)
75        continue;
76      temp_id=temp->getID();
77      if(temp_id<min_id)
78        min_id=temp_id;
79      temp=temp->next();
80    }*/ // probably not very efficient
81   
82    std::map<int,int>::iterator it = gameStateUsed.begin();
83    while(it!=gameStateUsed.end()){
84      if( (*it).second <= 0 ){
85        free(gameStateMap[(*it).first]->data);
86        delete gameStateMap[(*it).first];
87        gameStateMap.erase((*it).first);
88        gameStateUsed.erase(it++);
89      }else  //as soon as we got a used gamestate break here because we could use newer gamestates in future
90        break;
91    }
92  }
93
94  GameStateCompressed *GameStateManager::popGameState(int clientID) {
95    int gID = head_->findClient(clientID)->getGamestateID();
96    COUT(4) << "popgamestate: sending gstate id: " << id << "diffed from: " << gID << std::endl;
97    if(gID!=GAMESTATEID_INITIAL){
98      GameState *client = gameStateMap[gID];
99      GameState *server = reference;
100      //head_->findClient(clientID)->setGamestateID(id);
101      return encode(client, server);
102    } else {
103      GameState *server = reference;
104      //head_->findClient(clientID)->setGamestateID(id);
105      return encode(server);
106      // return an undiffed gamestate and set appropriate flags
107    }
108  }
109
110  /**
111  * This function goes through the whole list of synchronisables and
112  * saves all the synchronisables to a flat "list".
113  * @return struct of type gamestate containing the size of the whole gamestate and a pointer linking to the flat list
114  */
115  GameState *GameStateManager::getSnapshot(int id)
116  {
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) << "producing gamestate with id: " << retval->id << std::endl;
130    // reserve a little memory and increase it later on
131    COUT(5) << "mallocing" << std::endl;
132    retval->data = (unsigned char*)malloc(memsize);
133    COUT(5) << "malloced" << 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 << "gamestatemanager: in for loop" << std::endl;
140      //get size of the synchronisable
141      tempsize=it->getSize();
142//       COUT(5) << "size of temp gamestate: " << tempsize << std::endl;
143      //COUT(2) << "size of synchronisable: " << tempsize << std::endl;
144      // add place for data and 3 ints (length,classid,objectid)
145      totalsize+=tempsize+3*sizeof(int);
146      //std::cout << "totalsize: " << totalsize << std::endl;
147      // allocate additional space
148      if(totalsize+tempsize>memsize){
149        if(tempsize<1000){
150          retval->data = (unsigned char *)realloc((void *)retval->data, totalsize+1000);
151          memsize+=1000;
152        } else {
153          retval->data = (unsigned char *)realloc((void *)retval->data, totalsize+1000);
154          memsize+=tempsize+1000;
155        }
156      }
157
158      // run Synchronisable::getData with offset and additional place for 3 ints in between (for ids and length)
159      sync=it->getData((retval->data)+offset+3*sizeof(int));
160      memcpy(retval->data+offset, (void *)&sync.length, sizeof(int));
161      //*(retval->data+offset)=sync.length;
162      memcpy(retval->data+offset+sizeof(int), (void *)&sync.objectID, sizeof(int));
163      //*(retval->data+offset+sizeof(int))=sync.objectID;
164      memcpy(retval->data+offset+2*sizeof(int), (void *)&sync.classID, sizeof(int));
165      //*(retval->data+offset+2*sizeof(int))=sync.classID;
166      // increase data pointer
167      offset+=tempsize+3*sizeof(int);
168    }
169    COUT(5) << "Gamestate size: " << totalsize << std::endl;
170    retval->size=totalsize;
171    //#### bugfix
172    retval->diffed = false;
173    return retval;
174  }
175
176  //##### ADDED FOR TESTING PURPOSE #####
177  GameStateCompressed* GameStateManager::testCompress( GameState* g ) {
178    return compress_( g );
179  }
180
181  GameState* GameStateManager::testDiff( GameState* a, GameState* b ) {
182    return diff( a, b );
183  }
184  //##### END TESTING PURPOSE #####
185
186  GameStateCompressed *GameStateManager::encode(GameState *a, GameState *b) {
187    //GameState r = diff(a,b);
188    //r.diffed = true;
189    GameState *r = b;
190    r->diffed = false;
191    return compress_(r);
192  }
193
194  GameStateCompressed *GameStateManager::encode(GameState *a) {
195    a->diffed=false;
196    return compress_(a);
197  }
198
199  GameState *GameStateManager::diff(GameState *a, GameState *b) {
200    unsigned char *ap = a->data, *bp = b->data;
201    int of=0; // pointers offset
202    int dest_length=0;
203    if(a->size>=b->size)
204      dest_length=a->size;
205    else
206      dest_length=b->size;
207    unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char));
208    while(of<a->size && of<b->size){
209      *(dp+of)=*(ap+of)^*(bp+of); // do the xor
210      ++of;
211    }
212    if(a->size!=b->size){ // do we have to fill up ?
213      unsigned char n=0;
214      if(a->size<b->size){
215        while(of<dest_length){
216          *(dp+of)=n^*(bp+of);
217          of++;
218        }
219      } else{
220        while(of<dest_length){
221          *(dp+of)=*(ap+of)^n;
222          of++;
223        }
224      }
225    }
226   
227    GameState *r = new GameState;
228    r->id = b->id;
229    r->size = dest_length;
230    r->diffed = true;
231    r->base_id = a->id;
232    r->data = dp;
233    return r;
234  }
235
236  GameStateCompressed *GameStateManager::compress_(GameState *a) {
237    COUT(5) << "compressing gamestate" << std::endl;
238    int size = a->size;
239    uLongf buffer = (uLongf)((a->size + 12)*1.01)+1;
240    unsigned char* dest = (unsigned char*)malloc( buffer );
241    int retval;
242    //std::cout << "before ziped " << buffer << std::endl;
243    retval = compress( dest, &buffer, a->data, (uLong)size );
244    //std::cout << "after ziped " << buffer << std::endl;
245
246    switch ( retval ) {
247      case Z_OK: COUT(5) << "successfully compressed" << std::endl; break;
248      case Z_MEM_ERROR: COUT(1) << "not enough memory available in gamestate.compress" << std::endl; 
249      return NULL;
250      case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer in gamestate.compress" << std::endl;
251      return NULL;
252      case Z_DATA_ERROR: COUT(2) << "decompress: data corrupted in gamestate.compress" << std::endl;
253      return NULL;
254    }
255
256    GameStateCompressed *compressedGamestate = new GameStateCompressed;
257    compressedGamestate->compsize = buffer;
258//     std::cout << "compressedGamestate.compsize = buffer; " << buffer << std::endl;
259    compressedGamestate->normsize = size;
260//     std::cout << "compressedGamestate.normsize = size; " << size << std::endl;
261    compressedGamestate->id = a->id;
262    compressedGamestate->data = dest;
263    compressedGamestate->diffed = a->diffed;
264    compressedGamestate->base_id = a->base_id;
265
266    return compressedGamestate;
267  }
268
269  void GameStateManager::ackGameState(int clientID, int gamestateID) {
270    ClientInformation *temp = head_->findClient(clientID);
271    int curid = temp->getID();
272    // decrease usage of gamestate and save it
273    deleteUnusedGameState(curid);
274    //increase gamestateused
275    ++gameStateUsed.find(gamestateID)->second;
276    temp->setGamestateID(gamestateID);
277    /*
278    GameState *old = clientGameState[clientID];
279    deleteUnusedGameState(old);
280    clientGameState[clientID]=idGameState[gamestateID];*/
281  }
282
283  bool GameStateManager::deleteUnusedGameState(int gamestateID) {
284    int used = --(gameStateUsed.find(gamestateID)->second);
285    if(id-gamestateID>KEEP_GAMESTATES && used==0){
286      // delete gamestate
287      delete gameStateMap.find(gamestateID)->second;
288      gameStateMap.erase(gamestateID);
289      return true;
290    }
291    return false;
292  }
293
294}
Note: See TracBrowser for help on using the repository browser.