Orxonox  0.0.5 Codename: Arcturus
Gamestate.h
Go to the documentation of this file.
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 
54 {
55  public:
56  GamestateHeader(){ data_=nullptr; }
57  GamestateHeader(uint8_t* data)
58  { assert(data); data_ = data; *(Type*)data_ = Type::Gamestate; }
59  void setData(uint8_t* data)
60  { assert(data); data_ = data; *(Type*)data_ = Type::Gamestate; }
61  static inline uint32_t getSize()
62  { return 21; }
63 
64  inline uint32_t getID() const
65  { assert(data_); return *(uint32_t*)(data_+4); }
66  inline void setID(uint32_t id)
67  { assert(data_); *(uint32_t*)(data_+4) = id; }
68 
69  inline uint32_t getBaseID() const
70  { assert(data_); return *(uint32_t*)(data_+8); }
71  inline void setBaseID(uint32_t id)
72  { assert(data_); *(uint32_t*)(data_+8) = id; }
73 
74  inline uint32_t getDataSize() const
75  { assert(data_); return *(uint32_t*)(data_+12); }
76  inline void setDataSize(uint32_t size)
77  { assert(data_); *(uint32_t*)(data_+12) = size; }
78 
79  inline uint32_t getCompSize() const
80  { assert(data_); return *(uint32_t*)(data_+16); }
81  inline void setCompSize(uint32_t size)
82  { assert(data_); *(uint32_t*)(data_+16) = size; }
83 
84  inline bool isDiffed() const
85  { assert(data_); return *(int8_t*)(data_+20) & 0x1; }
86  inline void setDiffed(bool b)
87  { assert(data_); *(int8_t*)(data_+20) = (b<<0) | (*(int8_t*)(data_+20) & 0x6 ); }
88 
89  inline bool isComplete() const
90  { assert(data_); return *(int8_t*)(data_+20) & 0x2; }
91  inline void setComplete(bool b)
92  { assert(data_); *(int8_t*)(data_+20) = (b<<1) | (*(int8_t*)(data_+20) & 0x5 ); }
93 
94  inline bool isCompressed() const
95  { assert(data_); return *(int8_t*)(data_+20) & 0x4; }
96  inline void setCompressed(bool b)
97  { assert(data_); *(int8_t*)(data_+20) = (b<<2) | (*(int8_t*)(data_+20) & 0x3 ); }
98 
99  inline void operator=(GamestateHeader& h)
100  { assert(data_); assert(h.data_); memcpy( data_, h.data_, getSize()); }
101  private:
102  uint8_t* data_;
103 
104 };
105 
110 {
111  public:
112  Gamestate();
113  Gamestate(uint8_t *data, unsigned int clientID);
114  Gamestate(uint8_t *data);
115  Gamestate(const Gamestate& g);
116 
117  ~Gamestate();
118 
119  bool collectData(int id, uint8_t mode=0x0);
120  bool spreadData( uint8_t mode=0x0);
121  inline uint32_t getID() const { return header_.getID(); }
122  inline bool isDiffed() const { return header_.isDiffed(); }
123  inline bool isCompressed() const { return header_.isCompressed(); }
124  inline int32_t getBaseID() const { return header_.getBaseID(); }
125  inline uint32_t getDataSize() const { return header_.getDataSize(); }
126  Gamestate* diffVariables(Gamestate *base);
127 
128  bool compressData();
129  bool decompressData();
131 
132  // Packet functions
133  private:
134 
135  virtual uint32_t getSize() const override;
136  virtual bool process(orxonox::Host* host) override;
137  uint32_t calcGamestateSize(uint32_t id, uint8_t mode=0x0);
138 
139  std::list<obj> dataVector_;
141  std::vector<uint32_t> sizes_;
142  uint32_t nrOfVariables_;
143 };
144 
145 }
146 
147 }
148 
149 #endif /* _Gamestate_H__ */
static uint32_t getSize()
Definition: Gamestate.h:61
std::vector< uint32_t > sizes_
Definition: Gamestate.h:141
Shared library macros, enums, constants and forward declarations for the network library ...
bool isDiffed() const
Definition: Gamestate.h:122
uint32_t getDataSize() const
Definition: Gamestate.h:125
uint32_t getBaseID() const
Definition: Gamestate.h:69
void setDiffed(bool b)
Definition: Gamestate.h:86
void setData(uint8_t *data)
Definition: Gamestate.h:59
std::list< obj > dataVector_
Definition: Gamestate.h:139
uint8_t * data_
Definition: Gamestate.h:102
bool isDiffed() const
Definition: Gamestate.h:84
bool isComplete() const
Definition: Gamestate.h:89
uint32_t getID() const
Definition: Gamestate.h:64
uint32_t getCompSize() const
Definition: Gamestate.h:79
uint32_t getID() const
Definition: Gamestate.h:121
static constexpr uint8_t GAMESTATE_MODE_CLIENT
Definition: Gamestate.h:51
bool isCompressed() const
Definition: Gamestate.h:94
void setComplete(bool b)
Definition: Gamestate.h:91
Definition: Gamestate.h:109
int32_t getBaseID() const
Definition: Gamestate.h:124
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
#define _NetworkExport
Definition: NetworkPrereqs.h:59
Definition: Packet.h:61
void setBaseID(uint32_t id)
Definition: Gamestate.h:71
void setDataSize(uint32_t size)
Definition: Gamestate.h:76
void setCompressed(bool b)
Definition: Gamestate.h:96
GamestateHeader(uint8_t *data)
Definition: Gamestate.h:57
Type
Definition: Packet.h:46
GamestateHeader header_
Definition: Gamestate.h:140
GamestateHeader()
Definition: Gamestate.h:56
void setCompSize(uint32_t size)
Definition: Gamestate.h:81
constexpr bool operator==(bool x, tribool y)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: tribool.h:117
uint32_t getDataSize() const
Definition: Gamestate.h:74
Definition: Gamestate.h:53
void setID(uint32_t id)
Definition: Gamestate.h:66
void operator=(GamestateHeader &h)
Definition: Gamestate.h:99
bool isCompressed() const
Definition: Gamestate.h:123
static constexpr uint8_t GAMESTATE_MODE_SERVER
Definition: Gamestate.h:50
uint32_t nrOfVariables_
Definition: Gamestate.h:142
Base class of Server and Client This is the Base class of the Server and Client classes.
Definition: Host.h:52