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