[1711] | 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) 2008 |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[1701] | 29 | #include "Gamestate.h" |
---|
[2773] | 30 | #include <enet/enet.h> |
---|
| 31 | #include <zlib.h> |
---|
| 32 | #include <cassert> |
---|
[2662] | 33 | #include "../GamestateHandler.h" |
---|
| 34 | #include "../synchronisable/Synchronisable.h" |
---|
| 35 | #include "../TrafficControl.h" |
---|
| 36 | #include "core/Core.h" |
---|
[1763] | 37 | #include "core/CoreIncludes.h" |
---|
[1751] | 38 | #include "core/Iterator.h" |
---|
[1701] | 39 | |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | |
---|
[2171] | 43 | namespace orxonox { |
---|
[1701] | 44 | |
---|
| 45 | namespace packet { |
---|
| 46 | |
---|
[2662] | 47 | #define GAMESTATE_START(data) (data + GamestateHeader::getSize()) |
---|
[1740] | 48 | |
---|
[1907] | 49 | #define PACKET_FLAG_GAMESTATE ENET_PACKET_FLAG_RELIABLE |
---|
[2087] | 50 | |
---|
[2662] | 51 | |
---|
[1701] | 52 | Gamestate::Gamestate() |
---|
| 53 | { |
---|
[1907] | 54 | flags_ = flags_ | PACKET_FLAG_GAMESTATE; |
---|
[2662] | 55 | header_ = 0; |
---|
[1701] | 56 | } |
---|
| 57 | |
---|
[1907] | 58 | Gamestate::Gamestate(uint8_t *data, unsigned int clientID): |
---|
[1711] | 59 | Packet(data, clientID) |
---|
[1701] | 60 | { |
---|
[1907] | 61 | flags_ = flags_ | PACKET_FLAG_GAMESTATE; |
---|
[2662] | 62 | header_ = new GamestateHeader(data_); |
---|
[1701] | 63 | } |
---|
| 64 | |
---|
[1907] | 65 | Gamestate::Gamestate(uint8_t *data) |
---|
| 66 | { |
---|
| 67 | flags_ = flags_ | PACKET_FLAG_GAMESTATE; |
---|
| 68 | data_=data; |
---|
[2662] | 69 | header_ = new GamestateHeader(data_); |
---|
[1907] | 70 | } |
---|
[1701] | 71 | |
---|
[2662] | 72 | Gamestate::Gamestate(const Gamestate& g) : |
---|
| 73 | Packet( *(Packet*)&g ) |
---|
| 74 | { |
---|
| 75 | flags_ = flags_ | PACKET_FLAG_GAMESTATE; |
---|
| 76 | header_ = new GamestateHeader(data_); |
---|
| 77 | } |
---|
[1907] | 78 | |
---|
[2662] | 79 | |
---|
[1701] | 80 | Gamestate::~Gamestate() |
---|
| 81 | { |
---|
| 82 | } |
---|
| 83 | |
---|
[2171] | 84 | bool Gamestate::collectData(int id, uint8_t mode) |
---|
[1701] | 85 | { |
---|
[2662] | 86 | assert(this->header_==0); // make sure the header didn't exist before |
---|
| 87 | uint32_t tempsize=0, currentsize=0; |
---|
[1751] | 88 | assert(data_==0); |
---|
[2662] | 89 | uint32_t size = calcGamestateSize(id, mode); |
---|
[1740] | 90 | |
---|
[1701] | 91 | COUT(4) << "G.ST.Man: producing gamestate with id: " << id << std::endl; |
---|
| 92 | if(size==0) |
---|
| 93 | return false; |
---|
[2662] | 94 | data_ = new uint8_t[size + GamestateHeader::getSize()]; |
---|
[1701] | 95 | if(!data_){ |
---|
| 96 | COUT(2) << "GameStateManager: could not allocate memory" << std::endl; |
---|
| 97 | return false; |
---|
| 98 | } |
---|
[2662] | 99 | |
---|
| 100 | // create the header object |
---|
| 101 | header_ = new GamestateHeader(data_); |
---|
[2087] | 102 | |
---|
[1701] | 103 | //start collect data synchronisable by synchronisable |
---|
[1907] | 104 | uint8_t *mem=data_; |
---|
[2662] | 105 | mem += GamestateHeader::getSize(); |
---|
[2171] | 106 | ObjectList<Synchronisable>::iterator it; |
---|
| 107 | for(it = ObjectList<Synchronisable>::begin(); it; ++it){ |
---|
[2662] | 108 | |
---|
[2832] | 109 | // tempsize=it->getSize(id, mode); |
---|
| 110 | |
---|
| 111 | tempsize = it->getData(mem, id, mode); |
---|
| 112 | if ( it->doSync( id, mode ) ) |
---|
| 113 | dataMap_.push_back( obj(it->getObjectID(), it->getCreatorID(), tempsize, mem-data_) ); |
---|
| 114 | |
---|
[2662] | 115 | #ifndef NDEBUG |
---|
[1701] | 116 | if(currentsize+tempsize > size){ |
---|
[2171] | 117 | assert(0); // if we don't use multithreading this part shouldn't be neccessary |
---|
[1701] | 118 | // start allocate additional memory |
---|
| 119 | COUT(3) << "G.St.Man: need additional memory" << std::endl; |
---|
[2171] | 120 | ObjectList<Synchronisable>::iterator temp = it; |
---|
[2662] | 121 | uint32_t addsize=tempsize; |
---|
[1701] | 122 | while(++temp) |
---|
[1907] | 123 | addsize+=temp->getSize(id, mode); |
---|
[2662] | 124 | data_ = (uint8_t *)realloc(data_, GamestateHeader::getSize() + currentsize + addsize); |
---|
[1701] | 125 | if(!data_) |
---|
| 126 | return false; |
---|
| 127 | size = currentsize+addsize; |
---|
| 128 | }// stop allocate additional memory |
---|
[2662] | 129 | #endif |
---|
[2832] | 130 | // if(!it->getData(mem, id, mode)) |
---|
| 131 | // return false; // mem pointer gets automatically increased because of call by reference |
---|
[1701] | 132 | // increase size counter by size of current synchronisable |
---|
| 133 | currentsize+=tempsize; |
---|
| 134 | } |
---|
[1740] | 135 | |
---|
| 136 | |
---|
[1701] | 137 | //start write gamestate header |
---|
[2662] | 138 | header_->setDataSize( currentsize ); |
---|
| 139 | header_->setID( id ); |
---|
| 140 | header_->setDiffed( false ); |
---|
| 141 | header_->setComplete( true ); |
---|
| 142 | header_->setCompressed( false ); |
---|
[1701] | 143 | //stop write gamestate header |
---|
[1740] | 144 | |
---|
[1701] | 145 | COUT(5) << "G.ST.Man: Gamestate size: " << currentsize << std::endl; |
---|
| 146 | COUT(5) << "G.ST.Man: 'estimated' (and corrected) Gamestate size: " << size << std::endl; |
---|
| 147 | return true; |
---|
| 148 | } |
---|
| 149 | |
---|
[2171] | 150 | bool Gamestate::spreadData(uint8_t mode) |
---|
[1701] | 151 | { |
---|
[2662] | 152 | COUT(4) << "processing gamestate with id " << header_->getID() << endl; |
---|
[1751] | 153 | assert(data_); |
---|
[2662] | 154 | assert(!header_->isCompressed()); |
---|
| 155 | assert(!header_->isDiffed()); |
---|
| 156 | uint8_t *mem=data_+GamestateHeader::getSize(); |
---|
[1907] | 157 | Synchronisable *s; |
---|
[1740] | 158 | |
---|
[1907] | 159 | // update the data of the objects we received |
---|
[2662] | 160 | while(mem < data_+GamestateHeader::getSize()+header_->getDataSize()){ |
---|
| 161 | SynchronisableHeader objectheader(mem); |
---|
[1701] | 162 | |
---|
[2662] | 163 | s = Synchronisable::getSynchronisable( objectheader.getObjectID() ); |
---|
[1907] | 164 | if(!s) |
---|
[1701] | 165 | { |
---|
[2662] | 166 | if (!Core::isMaster()) |
---|
| 167 | { |
---|
| 168 | Synchronisable::fabricate(mem, mode); |
---|
| 169 | } |
---|
| 170 | else |
---|
| 171 | { |
---|
| 172 | mem += objectheader.getDataSize(); |
---|
| 173 | } |
---|
[1701] | 174 | } |
---|
[1907] | 175 | else |
---|
| 176 | { |
---|
| 177 | bool b = s->updateData(mem, mode); |
---|
| 178 | assert(b); |
---|
| 179 | } |
---|
[1701] | 180 | } |
---|
| 181 | |
---|
[2662] | 182 | // In debug mode, check first, whether there are no duplicate objectIDs |
---|
| 183 | #ifndef NDEBUG |
---|
| 184 | ObjectList<Synchronisable>::iterator it; |
---|
| 185 | for (it = ObjectList<Synchronisable>::begin(); it != ObjectList<Synchronisable>::end(); ++it) { |
---|
| 186 | if (it->getObjectID() == OBJECTID_UNKNOWN) { |
---|
| 187 | if (it->objectMode_ != 0x0) { |
---|
| 188 | COUT(0) << "Found object with OBJECTID_UNKNOWN on the client with objectMode != 0x0!" << std::endl; |
---|
| 189 | COUT(0) << "Possible reason for this error: Client created a synchronized object without the Server's approval." << std::endl; |
---|
| 190 | COUT(0) << "Objects class: " << it->getIdentifier()->getName() << std::endl; |
---|
| 191 | assert(false); |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | else { |
---|
| 195 | ObjectList<Synchronisable>::iterator it2; |
---|
| 196 | for (it2 = ObjectList<Synchronisable>::begin(); it2 != ObjectList<Synchronisable>::end(); ++it2) { |
---|
| 197 | if (it->getObjectID() == it2->getObjectID() && *it != *it2) { |
---|
| 198 | COUT(0) << "Found duplicate objectIDs on the client!" << std::endl |
---|
| 199 | << "Are you sure you don't create a Sychnronisable objcect with 'new' \ |
---|
| 200 | that doesn't have objectMode = 0x0?" << std::endl; |
---|
| 201 | assert(false); |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | #endif |
---|
| 207 | |
---|
[1701] | 208 | return true; |
---|
| 209 | } |
---|
| 210 | |
---|
[2662] | 211 | uint32_t Gamestate::getSize() const |
---|
[1701] | 212 | { |
---|
[1711] | 213 | assert(data_); |
---|
[2662] | 214 | if(header_->isCompressed()) |
---|
| 215 | return header_->getCompSize()+GamestateHeader::getSize(); |
---|
[1701] | 216 | else |
---|
| 217 | { |
---|
[2662] | 218 | return header_->getDataSize()+GamestateHeader::getSize(); |
---|
[1701] | 219 | } |
---|
| 220 | } |
---|
| 221 | |
---|
[1751] | 222 | bool Gamestate::operator==(packet::Gamestate gs){ |
---|
[2662] | 223 | uint8_t *d1 = data_+GamestateHeader::getSize(); |
---|
| 224 | uint8_t *d2 = gs.data_+GamestateHeader::getSize(); |
---|
[1751] | 225 | assert(!isCompressed()); |
---|
| 226 | assert(!gs.isCompressed()); |
---|
[2662] | 227 | while(d1<data_+header_->getDataSize()) |
---|
[1751] | 228 | { |
---|
| 229 | if(*d1!=*d2) |
---|
| 230 | return false; |
---|
| 231 | d1++; |
---|
| 232 | d2++; |
---|
| 233 | } |
---|
| 234 | return true; |
---|
| 235 | } |
---|
| 236 | |
---|
[1701] | 237 | bool Gamestate::process() |
---|
| 238 | { |
---|
[1705] | 239 | return GamestateHandler::addGamestate(this, getClientID()); |
---|
[1701] | 240 | } |
---|
| 241 | |
---|
[1907] | 242 | |
---|
| 243 | |
---|
[1701] | 244 | bool Gamestate::compressData() |
---|
| 245 | { |
---|
[2662] | 246 | assert(data_); |
---|
| 247 | assert(!header_->isCompressed()); |
---|
| 248 | uLongf buffer = (uLongf)(((header_->getDataSize() + 12)*1.01)+1); |
---|
[1701] | 249 | if(buffer==0) |
---|
| 250 | return false; |
---|
[1740] | 251 | |
---|
[2662] | 252 | uint8_t *ndata = new uint8_t[buffer+GamestateHeader::getSize()]; |
---|
| 253 | uint8_t *dest = ndata + GamestateHeader::getSize(); |
---|
| 254 | uint8_t *source = data_ + GamestateHeader::getSize(); |
---|
[1701] | 255 | int retval; |
---|
[2662] | 256 | retval = compress( dest, &buffer, source, (uLong)(header_->getDataSize()) ); |
---|
[1701] | 257 | switch ( retval ) { |
---|
| 258 | case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break; |
---|
[1751] | 259 | case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl; return false; |
---|
| 260 | case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl; return false; |
---|
| 261 | case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl; return false; |
---|
[1701] | 262 | } |
---|
| 263 | |
---|
| 264 | //copy and modify header |
---|
[2662] | 265 | GamestateHeader *temp = header_; |
---|
| 266 | header_ = new GamestateHeader(ndata, temp); |
---|
| 267 | delete temp; |
---|
[1701] | 268 | //delete old data |
---|
| 269 | delete[] data_; |
---|
| 270 | //save new data |
---|
| 271 | data_ = ndata; |
---|
[2662] | 272 | header_->setCompSize( buffer ); |
---|
| 273 | header_->setCompressed( true ); |
---|
| 274 | COUT(5) << "gamestate compress datasize: " << header_->getDataSize() << " compsize: " << header_->getCompSize() << std::endl; |
---|
[1701] | 275 | return true; |
---|
| 276 | } |
---|
| 277 | bool Gamestate::decompressData() |
---|
| 278 | { |
---|
[2662] | 279 | assert(data_); |
---|
| 280 | assert(header_->isCompressed()); |
---|
| 281 | COUT(4) << "GameStateClient: uncompressing gamestate. id: " << header_->getID() << ", baseid: " << header_->getBaseID() << ", datasize: " << header_->getDataSize() << ", compsize: " << header_->getCompSize() << std::endl; |
---|
| 282 | uint32_t datasize = header_->getDataSize(); |
---|
| 283 | uint32_t compsize = header_->getCompSize(); |
---|
| 284 | uint32_t bufsize; |
---|
[1907] | 285 | bufsize = datasize; |
---|
[1751] | 286 | assert(bufsize!=0); |
---|
[2662] | 287 | uint8_t *ndata = new uint8_t[bufsize + GamestateHeader::getSize()]; |
---|
| 288 | uint8_t *dest = ndata + GamestateHeader::getSize(); |
---|
| 289 | uint8_t *source = data_ + GamestateHeader::getSize(); |
---|
[1701] | 290 | int retval; |
---|
[1751] | 291 | uLongf length=bufsize; |
---|
| 292 | retval = uncompress( dest, &length, source, (uLong)compsize ); |
---|
[1701] | 293 | switch ( retval ) { |
---|
| 294 | case Z_OK: COUT(5) << "successfully decompressed" << std::endl; break; |
---|
| 295 | case Z_MEM_ERROR: COUT(1) << "not enough memory available" << std::endl; return false; |
---|
| 296 | case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer" << std::endl; return false; |
---|
| 297 | case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return false; |
---|
| 298 | } |
---|
[1752] | 299 | |
---|
[1701] | 300 | //copy over the header |
---|
[2662] | 301 | GamestateHeader *temp = header_; |
---|
| 302 | header_ = new GamestateHeader( data_, header_ ); |
---|
| 303 | delete temp; |
---|
[2087] | 304 | |
---|
| 305 | if (this->bDataENetAllocated_){ |
---|
| 306 | // Memory was allocated by ENet. --> We let it be since enet_packet_destroy will |
---|
| 307 | // deallocated it anyway. So data and packet stay together. |
---|
| 308 | this->bDataENetAllocated_ = false; |
---|
| 309 | } |
---|
| 310 | else{ |
---|
| 311 | // We allocated the memory in the first place (unlikely). So we destroy the old data |
---|
| 312 | // and overwrite it with the new decompressed data. |
---|
| 313 | delete[] this->data_; |
---|
| 314 | } |
---|
| 315 | |
---|
[1751] | 316 | //set new pointers |
---|
[1701] | 317 | data_ = ndata; |
---|
[2662] | 318 | header_->setCompressed( false ); |
---|
| 319 | assert(header_->getDataSize()==datasize); |
---|
| 320 | assert(header_->getCompSize()==compsize); |
---|
[1701] | 321 | return true; |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | Gamestate *Gamestate::diff(Gamestate *base) |
---|
| 325 | { |
---|
[2662] | 326 | assert(data_); |
---|
| 327 | assert(!header_->isCompressed()); |
---|
| 328 | assert(!header_->isDiffed()); |
---|
| 329 | GamestateHeader diffHeader(base->data_); |
---|
[1907] | 330 | uint8_t *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_); |
---|
[2662] | 331 | uint32_t of=0; // pointers offset |
---|
| 332 | uint32_t dest_length=0; |
---|
| 333 | dest_length=header_->getDataSize(); |
---|
[1701] | 334 | if(dest_length==0) |
---|
| 335 | return NULL; |
---|
[2662] | 336 | uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()]; |
---|
| 337 | uint8_t *dest = ndata + GamestateHeader::getSize(); |
---|
| 338 | while(of < diffHeader.getDataSize() && of < header_->getDataSize()){ |
---|
[1701] | 339 | *(dest+of)=*(basep+of)^*(gs+of); // do the xor |
---|
| 340 | ++of; |
---|
| 341 | } |
---|
[2662] | 342 | if(diffHeader.getDataSize()!=header_->getDataSize()){ |
---|
[1907] | 343 | uint8_t n=0; |
---|
[2662] | 344 | if(diffHeader.getDataSize() < header_->getDataSize()){ |
---|
[1701] | 345 | while(of<dest_length){ |
---|
| 346 | *(dest+of)=n^*(gs+of); |
---|
| 347 | of++; |
---|
| 348 | } |
---|
| 349 | } |
---|
| 350 | } |
---|
| 351 | |
---|
[1751] | 352 | Gamestate *g = new Gamestate(ndata, getClientID()); |
---|
[2662] | 353 | *(g->header_) = *header_; |
---|
| 354 | g->header_->setDiffed( true ); |
---|
| 355 | g->header_->setBaseID( base->getID() ); |
---|
[1751] | 356 | g->flags_=flags_; |
---|
| 357 | g->packetDirection_ = packetDirection_; |
---|
[1701] | 358 | return g; |
---|
| 359 | } |
---|
| 360 | |
---|
[2662] | 361 | Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){ |
---|
[1907] | 362 | assert(data_); |
---|
[2662] | 363 | std::list<obj>::iterator it; |
---|
[2087] | 364 | |
---|
[1907] | 365 | // allocate memory for new data |
---|
[2662] | 366 | uint8_t *gdata = new uint8_t[header_->getDataSize()+GamestateHeader::getSize()]; |
---|
[1907] | 367 | // create a gamestate out of it |
---|
| 368 | Gamestate *gs = new Gamestate(gdata); |
---|
[2662] | 369 | uint8_t *newdata = gdata + GamestateHeader::getSize(); |
---|
[1907] | 370 | uint8_t *origdata = GAMESTATE_START(data_); |
---|
[2087] | 371 | |
---|
[1907] | 372 | //copy the GamestateHeader |
---|
[2662] | 373 | assert(gs->header_); |
---|
| 374 | *(gs->header_) = *header_; |
---|
[2087] | 375 | |
---|
[2662] | 376 | uint32_t objectOffset; |
---|
| 377 | unsigned int objectsize, destsize=0; |
---|
| 378 | // TODO: Why is this variable not used? |
---|
| 379 | //Synchronisable *object; |
---|
[2087] | 380 | |
---|
[2662] | 381 | //call TrafficControl |
---|
| 382 | TrafficControl::getInstance()->processObjectList( clientID, header_->getID(), &dataMap_ ); |
---|
| 383 | |
---|
[1907] | 384 | //copy in the zeros |
---|
[2662] | 385 | for(it=dataMap_.begin(); it!=dataMap_.end();){ |
---|
| 386 | SynchronisableHeader oldobjectheader(origdata); |
---|
| 387 | SynchronisableHeader newobjectheader(newdata); |
---|
| 388 | if ( (*it).objSize == 0 ) |
---|
| 389 | { |
---|
| 390 | ++it; |
---|
| 391 | continue; |
---|
| 392 | } |
---|
| 393 | objectsize = oldobjectheader.getDataSize(); |
---|
| 394 | objectOffset=SynchronisableHeader::getSize(); //skip the size and the availableData variables in the objectheader |
---|
| 395 | if ( (*it).objID == oldobjectheader.getObjectID() ){ |
---|
| 396 | memcpy(newdata, origdata, objectsize); |
---|
| 397 | assert(newobjectheader.isDataAvailable()==true); |
---|
| 398 | ++it; |
---|
[1907] | 399 | }else{ |
---|
[2662] | 400 | newobjectheader = oldobjectheader; |
---|
| 401 | newobjectheader.setDataAvailable(false); |
---|
[2171] | 402 | memset(newdata+objectOffset, 0, objectsize-objectOffset); |
---|
[1907] | 403 | } |
---|
| 404 | newdata += objectsize; |
---|
| 405 | origdata += objectsize; |
---|
[2662] | 406 | destsize += objectsize; |
---|
[1907] | 407 | } |
---|
[2662] | 408 | #ifndef NDEBUG |
---|
| 409 | uint32_t origsize = destsize; |
---|
| 410 | while ( origsize < header_->getDataSize() ) |
---|
| 411 | { |
---|
| 412 | SynchronisableHeader oldobjectheader(origdata); |
---|
| 413 | objectsize = oldobjectheader.getDataSize(); |
---|
| 414 | origdata += objectsize; |
---|
| 415 | origsize += objectsize; |
---|
[1907] | 416 | } |
---|
[2662] | 417 | assert(origsize==header_->getDataSize()); |
---|
| 418 | assert(destsize!=0); |
---|
| 419 | #endif |
---|
| 420 | gs->header_->setDataSize( destsize ); |
---|
[1907] | 421 | return gs; |
---|
| 422 | } |
---|
| 423 | |
---|
[2087] | 424 | |
---|
[1701] | 425 | Gamestate *Gamestate::undiff(Gamestate *base) |
---|
| 426 | { |
---|
[2662] | 427 | assert(this && base);assert(data_); |
---|
| 428 | assert(header_->isDiffed()); |
---|
| 429 | assert(!header_->isCompressed() && !base->header_->isCompressed()); |
---|
[1907] | 430 | uint8_t *basep = GAMESTATE_START(base->data_); |
---|
| 431 | uint8_t *gs = GAMESTATE_START(this->data_); |
---|
[2662] | 432 | uint32_t of=0; // pointers offset |
---|
| 433 | uint32_t dest_length=0; |
---|
| 434 | dest_length=header_->getDataSize(); |
---|
[1701] | 435 | if(dest_length==0) |
---|
| 436 | return NULL; |
---|
[2662] | 437 | uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()]; |
---|
| 438 | uint8_t *dest = ndata + GamestateHeader::getSize(); |
---|
| 439 | while(of < base->header_->getDataSize() && of < header_->getDataSize()){ |
---|
[1701] | 440 | *(dest+of)=*(basep+of)^*(gs+of); // do the xor |
---|
| 441 | ++of; |
---|
| 442 | } |
---|
[2662] | 443 | if(base->header_->getDataSize()!=header_->getDataSize()){ |
---|
[1907] | 444 | uint8_t n=0; |
---|
[2662] | 445 | if(base->header_->getDataSize() < header_->getDataSize()){ |
---|
[1701] | 446 | while(of < dest_length){ |
---|
| 447 | *(dest+of)=n^*(gs+of); |
---|
| 448 | of++; |
---|
| 449 | } |
---|
| 450 | } |
---|
| 451 | } |
---|
[1751] | 452 | Gamestate *g = new Gamestate(ndata, getClientID()); |
---|
[2662] | 453 | assert(g->header_); |
---|
| 454 | *(g->header_) = *header_; |
---|
| 455 | g->header_->setDiffed( false ); |
---|
[1751] | 456 | g->flags_=flags_; |
---|
| 457 | g->packetDirection_ = packetDirection_; |
---|
| 458 | assert(!g->isDiffed()); |
---|
| 459 | assert(!g->isCompressed()); |
---|
[1701] | 460 | return g; |
---|
| 461 | } |
---|
| 462 | |
---|
| 463 | |
---|
[2662] | 464 | uint32_t Gamestate::calcGamestateSize(int32_t id, uint8_t mode) |
---|
[1701] | 465 | { |
---|
[2662] | 466 | uint32_t size=0; |
---|
[1701] | 467 | // get the start of the Synchronisable list |
---|
[2171] | 468 | ObjectList<Synchronisable>::iterator it; |
---|
[1701] | 469 | // get total size of gamestate |
---|
[2171] | 470 | for(it = ObjectList<Synchronisable>::begin(); it; ++it) |
---|
[1907] | 471 | size+=it->getSize(id, mode); // size of the actual data of the synchronisable |
---|
[1701] | 472 | return size; |
---|
| 473 | } |
---|
| 474 | |
---|
[2662] | 475 | } //namespace packet |
---|
| 476 | } //namespace orxonox |
---|