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