| 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 | 
|---|
| 24 | *   Co-authors: | 
|---|
| 25 | *      ... | 
|---|
| 26 | * | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 |  | 
|---|
| 30 | #ifndef _Gamestate_H__ | 
|---|
| 31 | #define _Gamestate_H__ | 
|---|
| 32 |  | 
|---|
| 33 | #include "network/NetworkPrereqs.h" | 
|---|
| 34 |  | 
|---|
| 35 | #include <cassert> | 
|---|
| 36 | #include <cstring> | 
|---|
| 37 | #include <list> | 
|---|
| 38 | #include <vector> | 
|---|
| 39 |  | 
|---|
| 40 | #include "util/CRC32.h" | 
|---|
| 41 | #include "network/TrafficControl.h" | 
|---|
| 42 | #include "Packet.h" | 
|---|
| 43 |  | 
|---|
| 44 | namespace orxonox | 
|---|
| 45 | { | 
|---|
| 46 |  | 
|---|
| 47 | namespace packet | 
|---|
| 48 | { | 
|---|
| 49 |  | 
|---|
| 50 | static constexpr uint8_t GAMESTATE_MODE_SERVER = 0x1; | 
|---|
| 51 | static constexpr uint8_t GAMESTATE_MODE_CLIENT = 0x2; | 
|---|
| 52 |  | 
|---|
| 53 | class _NetworkExport GamestateHeader | 
|---|
| 54 | { | 
|---|
| 55 | public: | 
|---|
| 56 | GamestateHeader(){ data_=nullptr; } | 
|---|
| 57 | GamestateHeader(uint8_t* data) | 
|---|
| 58 | { assert(data); data_ = data; *(uint32_t*)data_ = Type::Gamestate; } | 
|---|
| 59 | /*GamestateHeader(uint8_t* data, GamestateHeader* h) | 
|---|
| 60 | { assert(data); data_=data; memcpy(data_, h->data_, getSize()); }*/ | 
|---|
| 61 | void setData(uint8_t* data) | 
|---|
| 62 | { assert(data); data_ = data; *(uint32_t*)data_ = Type::Gamestate; } | 
|---|
| 63 | static inline uint32_t getSize() | 
|---|
| 64 | { return 21; } | 
|---|
| 65 |  | 
|---|
| 66 | inline uint32_t getID() const | 
|---|
| 67 | { assert(data_); return *(uint32_t*)(data_+4); } | 
|---|
| 68 | inline void setID(uint32_t id) | 
|---|
| 69 | { assert(data_); *(uint32_t*)(data_+4) = id; } | 
|---|
| 70 |  | 
|---|
| 71 | inline uint32_t getBaseID() const | 
|---|
| 72 | { assert(data_); return *(uint32_t*)(data_+8); } | 
|---|
| 73 | inline void setBaseID(uint32_t id) | 
|---|
| 74 | { assert(data_); *(uint32_t*)(data_+8) = id; } | 
|---|
| 75 |  | 
|---|
| 76 | inline uint32_t getDataSize() const | 
|---|
| 77 | { assert(data_); return *(uint32_t*)(data_+12); } | 
|---|
| 78 | inline void setDataSize(uint32_t size) | 
|---|
| 79 | { assert(data_); *(uint32_t*)(data_+12) = size; } | 
|---|
| 80 |  | 
|---|
| 81 | inline uint32_t getCompSize() const | 
|---|
| 82 | { assert(data_); return *(uint32_t*)(data_+16); } | 
|---|
| 83 | inline void setCompSize(uint32_t size) | 
|---|
| 84 | { assert(data_); *(uint32_t*)(data_+16) = size; } | 
|---|
| 85 |  | 
|---|
| 86 | inline bool isDiffed() const | 
|---|
| 87 | { assert(data_); return *(int8_t*)(data_+20) & 0x1; } | 
|---|
| 88 | inline void setDiffed(bool b) | 
|---|
| 89 | { assert(data_); *(int8_t*)(data_+20) = (b<<0) | (*(int8_t*)(data_+20) & 0x6 ); } | 
|---|
| 90 |  | 
|---|
| 91 | inline bool isComplete() const | 
|---|
| 92 | { assert(data_); return *(int8_t*)(data_+20) & 0x2; } | 
|---|
| 93 | inline void setComplete(bool b) | 
|---|
| 94 | { assert(data_); *(int8_t*)(data_+20) = (b<<1) | (*(int8_t*)(data_+20) & 0x5 ); } | 
|---|
| 95 |  | 
|---|
| 96 | inline bool isCompressed() const | 
|---|
| 97 | { assert(data_); return *(int8_t*)(data_+20) & 0x4; } | 
|---|
| 98 | inline void setCompressed(bool b) | 
|---|
| 99 | { assert(data_); *(int8_t*)(data_+20) = (b<<2) | (*(int8_t*)(data_+20) & 0x3 ); } | 
|---|
| 100 |  | 
|---|
| 101 | inline void operator=(GamestateHeader& h) | 
|---|
| 102 | { assert(data_); assert(h.data_); memcpy( data_, h.data_, getSize()); } | 
|---|
| 103 | private: | 
|---|
| 104 | uint8_t* data_; | 
|---|
| 105 |  | 
|---|
| 106 | }; | 
|---|
| 107 |  | 
|---|
| 108 | /** | 
|---|
| 109 | @author Oliver Scheuss | 
|---|
| 110 | */ | 
|---|
| 111 | class _NetworkExport Gamestate: public Packet | 
|---|
| 112 | { | 
|---|
| 113 | public: | 
|---|
| 114 | Gamestate(); | 
|---|
| 115 | Gamestate(uint8_t *data, unsigned int clientID); | 
|---|
| 116 | Gamestate(uint8_t *data); | 
|---|
| 117 | Gamestate(const Gamestate& g); | 
|---|
| 118 |  | 
|---|
| 119 | ~Gamestate(); | 
|---|
| 120 |  | 
|---|
| 121 | bool collectData(int id, uint8_t mode=0x0); | 
|---|
| 122 | bool spreadData( uint8_t mode=0x0); | 
|---|
| 123 | inline uint32_t getID() const { return header_.getID(); } | 
|---|
| 124 | inline bool isDiffed() const { return header_.isDiffed(); } | 
|---|
| 125 | inline bool isCompressed() const { return header_.isCompressed(); } | 
|---|
| 126 | inline int32_t getBaseID() const { return header_.getBaseID(); } | 
|---|
| 127 | inline uint32_t getDataSize() const { return header_.getDataSize(); } | 
|---|
| 128 | Gamestate* diffVariables(Gamestate *base); | 
|---|
| 129 | //     Gamestate* diffData(Gamestate *base); | 
|---|
| 130 | //     Gamestate *undiff(Gamestate *base); | 
|---|
| 131 | //     Gamestate* doSelection(unsigned int clientID, unsigned int targetSize); | 
|---|
| 132 | bool compressData(); | 
|---|
| 133 | bool decompressData(); | 
|---|
| 134 | bool operator ==(packet::Gamestate gs); | 
|---|
| 135 |  | 
|---|
| 136 | // Packet functions | 
|---|
| 137 | private: | 
|---|
| 138 | //     void rawDiff( uint8_t* newdata, uint8_t* data, uint8_t* basedata, uint32_t datalength, uint32_t baselength); | 
|---|
| 139 | //     inline uint32_t findObject( const SynchronisableHeader& header, uint8_t* mem, uint32_t dataLength, uint32_t startPosition = 0 ); | 
|---|
| 140 | virtual uint32_t getSize() const override; | 
|---|
| 141 | virtual bool process(orxonox::Host* host) override; | 
|---|
| 142 | uint32_t calcGamestateSize(uint32_t id, uint8_t mode=0x0); | 
|---|
| 143 | //     inline void diffObject( uint8_t*& newData, uint8_t*& origData, uint8_t*& baseData, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes ); | 
|---|
| 144 | //     inline void copyObject( uint8_t*& newData, uint8_t*& origData, uint8_t*& baseData, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes ); | 
|---|
| 145 |  | 
|---|
| 146 | std::list<obj>          dataVector_; | 
|---|
| 147 | GamestateHeader         header_; | 
|---|
| 148 | std::vector<uint32_t>   sizes_; | 
|---|
| 149 | uint32_t                nrOfVariables_; | 
|---|
| 150 | }; | 
|---|
| 151 |  | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | #endif /* _Gamestate_H__ */ | 
|---|