Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp5/src/network/packet/Gamestate.h @ 3210

Last change on this file since 3210 was 3209, checked in by rgrieder, 15 years ago

Cleanup in network plus a few dependency reductions (no enet-function inlines, using enum PacketFlag instead of the enet version)

  • Property svn:eol-style set to native
File size: 4.2 KB
Line 
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 <list>
37
38#include "util/CRC32.h"
39#include "network/TrafficControl.h"
40#include "Packet.h"
41
42namespace orxonox {
43
44namespace packet {
45
46class _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
94};
95
96/**
97        @author Oliver Scheuss
98*/
99class _NetworkExport Gamestate: public Packet{
100  public:
101    Gamestate();
102    Gamestate(uint8_t *data, unsigned int clientID);
103    Gamestate(uint8_t *data);
104    Gamestate(const Gamestate& g);
105
106    ~Gamestate();
107
108    bool collectData(int id, uint8_t mode=0x0);
109    bool spreadData( uint8_t mode=0x0);
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(); }
114    Gamestate *diff(Gamestate *base);
115    Gamestate *undiff(Gamestate *base);
116    Gamestate* doSelection(unsigned int clientID, unsigned int targetSize);
117    bool compressData();
118    bool decompressData();
119    bool operator ==(packet::Gamestate gs);
120
121    // Packet functions
122  private:
123    void rawDiff( uint8_t* newdata, uint8_t* data, uint8_t* basedata, uint32_t datalength, uint32_t baselength);
124    virtual uint32_t getSize() const;
125    virtual inline bool process();
126
127  private:
128    uint32_t calcGamestateSize(int32_t id, uint8_t mode=0x0);
129    std::list<obj> dataVector_;
130    GamestateHeader* header_;
131};
132
133}
134
135}
136
137#endif /* _Gamestate_H__ */
Note: See TracBrowser for help on using the repository browser.