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