| [1701] | 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: | 
|---|
| [3084] | 23 |  *      Oliver Scheuss | 
|---|
| [1701] | 24 |  *   Co-authors: | 
|---|
 | 25 |  *      ... | 
|---|
 | 26 |  * | 
|---|
 | 27 |  */ | 
|---|
 | 28 |  | 
|---|
| [2087] | 29 |  | 
|---|
| [3214] | 30 | #ifndef _Gamestate_H__ | 
|---|
 | 31 | #define _Gamestate_H__ | 
|---|
| [2087] | 32 |  | 
|---|
| [2662] | 33 | #include "network/NetworkPrereqs.h" | 
|---|
| [2087] | 34 |  | 
|---|
| [2896] | 35 | #include <cassert> | 
|---|
| [3214] | 36 | #include <cstring> | 
|---|
 | 37 | #include <list> | 
|---|
 | 38 |  | 
|---|
| [1751] | 39 | #include "util/CRC32.h" | 
|---|
| [3214] | 40 | #include "network/TrafficControl.h" | 
|---|
 | 41 | #include "Packet.h" | 
|---|
| [1701] | 42 |  | 
|---|
| [2171] | 43 | namespace orxonox { | 
|---|
| [1701] | 44 |  | 
|---|
 | 45 | namespace packet { | 
|---|
| [1747] | 46 |  | 
|---|
| [2662] | 47 | class _NetworkExport GamestateHeader{ | 
|---|
 | 48 |   public: | 
|---|
| [3280] | 49 |     GamestateHeader(uint8_t *data){ assert(data); data_ = data; *(uint32_t*)data_ = Type::Gamestate; } | 
|---|
| [2662] | 50 |     GamestateHeader(uint8_t *data, GamestateHeader* h) | 
|---|
 | 51 |     { assert(data); data_=data; memcpy(data_, h->data_, getSize()); } | 
|---|
 | 52 |     static inline uint32_t getSize() | 
|---|
 | 53 |     { return 21; } | 
|---|
 | 54 |  | 
|---|
 | 55 |     inline int32_t getID() const | 
|---|
 | 56 |     { assert(data_); return *(int32_t*)(data_+4); } | 
|---|
 | 57 |     inline void setID(int32_t id) | 
|---|
 | 58 |     { assert(data_); *(int32_t*)(data_+4) = id; } | 
|---|
 | 59 |  | 
|---|
 | 60 |     inline int32_t getBaseID() const | 
|---|
 | 61 |     { assert(data_); return *(int32_t*)(data_+8); } | 
|---|
 | 62 |     inline void setBaseID(int32_t id) | 
|---|
 | 63 |     { assert(data_); *(int32_t*)(data_+8) = id; } | 
|---|
 | 64 |  | 
|---|
 | 65 |     inline uint32_t getDataSize() const | 
|---|
 | 66 |     { assert(data_); return *(uint32_t*)(data_+12); } | 
|---|
 | 67 |     inline void setDataSize(uint32_t size) | 
|---|
 | 68 |     { assert(data_); *(uint32_t*)(data_+12) = size; } | 
|---|
 | 69 |  | 
|---|
 | 70 |     inline uint32_t getCompSize() const | 
|---|
 | 71 |     { assert(data_); return *(uint32_t*)(data_+16); } | 
|---|
 | 72 |     inline void setCompSize(uint32_t size) | 
|---|
 | 73 |     { assert(data_); *(uint32_t*)(data_+16) = size; } | 
|---|
 | 74 |  | 
|---|
 | 75 |     inline bool isDiffed() const | 
|---|
 | 76 |     { assert(data_); return *(int8_t*)(data_+20) & 0x1; } | 
|---|
 | 77 |     inline void setDiffed(bool b) | 
|---|
 | 78 |     { assert(data_); *(int8_t*)(data_+20) = (b<<0) | (*(int8_t*)(data_+20) & 0x6 ); } | 
|---|
 | 79 |  | 
|---|
 | 80 |     inline bool isComplete() const | 
|---|
 | 81 |     { assert(data_); return *(int8_t*)(data_+20) & 0x2; } | 
|---|
 | 82 |     inline void setComplete(bool b) | 
|---|
 | 83 |     { assert(data_); *(int8_t*)(data_+20) = (b<<1) | (*(int8_t*)(data_+20) & 0x5 ); } | 
|---|
 | 84 |  | 
|---|
 | 85 |     inline bool isCompressed() const | 
|---|
 | 86 |     { assert(data_); return *(int8_t*)(data_+20) & 0x4; } | 
|---|
 | 87 |     inline void setCompressed(bool b) | 
|---|
 | 88 |     { assert(data_); *(int8_t*)(data_+20) = (b<<2) | (*(int8_t*)(data_+20) & 0x3 ); } | 
|---|
 | 89 |  | 
|---|
 | 90 |     inline void operator=(GamestateHeader& h) | 
|---|
 | 91 |     { assert(data_); assert(h.data_); memcpy( data_, h.data_, getSize()); } | 
|---|
 | 92 |   private: | 
|---|
 | 93 |     uint8_t *data_; | 
|---|
 | 94 |  | 
|---|
| [1701] | 95 | }; | 
|---|
 | 96 |  | 
|---|
 | 97 | /** | 
|---|
| [6073] | 98 |     @author Oliver Scheuss | 
|---|
| [1701] | 99 | */ | 
|---|
| [2087] | 100 | class _NetworkExport Gamestate: public Packet{ | 
|---|
| [1701] | 101 |   public: | 
|---|
 | 102 |     Gamestate(); | 
|---|
| [1907] | 103 |     Gamestate(uint8_t *data, unsigned int clientID); | 
|---|
 | 104 |     Gamestate(uint8_t *data); | 
|---|
| [2662] | 105 |     Gamestate(const Gamestate& g); | 
|---|
| [1747] | 106 |  | 
|---|
| [1701] | 107 |     ~Gamestate(); | 
|---|
| [1747] | 108 |  | 
|---|
| [2171] | 109 |     bool collectData(int id, uint8_t mode=0x0); | 
|---|
 | 110 |     bool spreadData( uint8_t mode=0x0); | 
|---|
| [2662] | 111 |     inline int32_t getID() const { return header_->getID(); } | 
|---|
 | 112 |     inline bool isDiffed() const { return header_->isDiffed(); } | 
|---|
 | 113 |     inline bool isCompressed() const { return header_->isCompressed(); } | 
|---|
 | 114 |     inline int32_t getBaseID() const { return header_->getBaseID(); } | 
|---|
| [1705] | 115 |     Gamestate *diff(Gamestate *base); | 
|---|
 | 116 |     Gamestate *undiff(Gamestate *base); | 
|---|
| [2662] | 117 |     Gamestate* doSelection(unsigned int clientID, unsigned int targetSize); | 
|---|
| [1705] | 118 |     bool compressData(); | 
|---|
 | 119 |     bool decompressData(); | 
|---|
| [3084] | 120 |     bool operator ==(packet::Gamestate gs); | 
|---|
| [1747] | 121 |  | 
|---|
| [1711] | 122 |     // Packet functions | 
|---|
| [1907] | 123 |   private: | 
|---|
| [3198] | 124 |     void rawDiff( uint8_t* newdata, uint8_t* data, uint8_t* basedata, uint32_t datalength, uint32_t baselength); | 
|---|
| [2662] | 125 |     virtual uint32_t getSize() const; | 
|---|
 | 126 |     virtual inline bool process(); | 
|---|
| [1701] | 127 |  | 
|---|
 | 128 |   private: | 
|---|
| [2662] | 129 |     uint32_t calcGamestateSize(int32_t id, uint8_t mode=0x0); | 
|---|
| [3084] | 130 |     std::list<obj> dataVector_; | 
|---|
| [2662] | 131 |     GamestateHeader* header_; | 
|---|
| [1701] | 132 | }; | 
|---|
 | 133 |  | 
|---|
 | 134 | } | 
|---|
 | 135 |  | 
|---|
 | 136 | } | 
|---|
 | 137 |  | 
|---|
| [3214] | 138 | #endif /* _Gamestate_H__ */ | 
|---|