Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

made Model and WorldEntity synchronisable

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