[1505] | 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, (C) 2007 |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #ifndef _Synchronisable_H__ |
---|
| 30 | #define _Synchronisable_H__ |
---|
| 31 | |
---|
[2211] | 32 | #include "network/NetworkPrereqs.h" |
---|
[1505] | 33 | |
---|
| 34 | #include <list> |
---|
[1907] | 35 | #include <map> |
---|
| 36 | #include <queue> |
---|
[2245] | 37 | #include <cassert> |
---|
| 38 | #include "util/Math.h" |
---|
| 39 | #include "util/mbool.h" |
---|
[1505] | 40 | #include "core/OrxonoxClass.h" |
---|
[2245] | 41 | // TODO: this has to be removed |
---|
| 42 | // #include <OgreLight.h> |
---|
| 43 | // #include "OrxonoxPrereqs.h" |
---|
| 44 | // ============================ |
---|
[1534] | 45 | #include "NetworkCallback.h" |
---|
[2245] | 46 | #include "SynchronisableVariable.h" |
---|
[1505] | 47 | |
---|
[2245] | 48 | /*#define REGISTERDATA(varname, ...) \ |
---|
[2171] | 49 | registerVariable((void*)&varname, sizeof(varname), DATA, __VA_ARGS__) |
---|
[2087] | 50 | #define REGISTERSTRING(stringname, ...) \ |
---|
[2245] | 51 | registerVariable(&stringname, stringname.length()+1, STRING, __VA_ARGS__)*/ |
---|
[1907] | 52 | |
---|
[2171] | 53 | namespace orxonox |
---|
[1505] | 54 | { |
---|
[2087] | 55 | |
---|
[2245] | 56 | namespace objectDirection{ |
---|
| 57 | enum objectdirection{ |
---|
[1907] | 58 | toclient=0x1, |
---|
| 59 | toserver=0x2, |
---|
[2245] | 60 | bidirectional=0x3 |
---|
[1907] | 61 | }; |
---|
| 62 | } |
---|
[2087] | 63 | |
---|
| 64 | struct _NetworkExport synchronisableHeader{ |
---|
[1907] | 65 | uint32_t size:31; |
---|
| 66 | bool dataAvailable:1; |
---|
| 67 | uint32_t objectID; |
---|
[2087] | 68 | uint32_t creatorID; |
---|
[1907] | 69 | uint32_t classID; |
---|
[1505] | 70 | }; |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | * This class is the base class of all the Objects in the universe that need to be synchronised over the network |
---|
[1907] | 75 | * Every class, that inherits from this class has to link the DATA THAT NEEDS TO BE SYNCHRONISED into the linked list. |
---|
[1505] | 76 | * @author Oliver Scheuss |
---|
| 77 | */ |
---|
[2171] | 78 | class _NetworkExport Synchronisable : virtual public OrxonoxClass{ |
---|
[1505] | 79 | public: |
---|
[1907] | 80 | friend class packet::Gamestate; |
---|
[2171] | 81 | // friend class Server; |
---|
[1505] | 82 | virtual ~Synchronisable(); |
---|
| 83 | |
---|
| 84 | static void setClient(bool b); |
---|
[2087] | 85 | |
---|
[2171] | 86 | static Synchronisable *fabricate(uint8_t*& mem, uint8_t mode=0x0); |
---|
[2309] | 87 | static bool deleteObject(uint32_t objectID); |
---|
| 88 | static Synchronisable *getSynchronisable(uint32_t objectID); |
---|
[1907] | 89 | static unsigned int getNumberOfDeletedObject(){ return deletedObjects_.size(); } |
---|
[2309] | 90 | static uint32_t popDeletedObject(){ uint32_t i = deletedObjects_.front(); deletedObjects_.pop(); return i; } |
---|
[2087] | 91 | |
---|
[2309] | 92 | inline uint32_t getObjectID(){return objectID;} |
---|
[2355] | 93 | inline unsigned int getCreatorID(){return creatorID;} |
---|
[2309] | 94 | inline uint32_t getClassID(){return classID;} |
---|
[2355] | 95 | inline unsigned int getPriority(){ return objectFrequency_;} |
---|
| 96 | |
---|
[1505] | 97 | protected: |
---|
[2171] | 98 | Synchronisable(BaseObject* creator); |
---|
[2245] | 99 | // void registerVariable(void *var, int size, variableType t, uint8_t mode=0x1, NetworkCallbackBase *cb=0); |
---|
| 100 | template <class T> void registerVariable(T& variable, uint8_t mode=0x1, NetworkCallbackBase *cb=0, bool bidirectional=false); |
---|
| 101 | template <class T> void unregisterVariable(T& var); |
---|
[2171] | 102 | void setObjectMode(uint8_t mode); |
---|
[2355] | 103 | void setObjectPriority(unsigned int freq){ objectFrequency_ = freq; } |
---|
[2087] | 104 | |
---|
| 105 | |
---|
[1505] | 106 | private: |
---|
[2309] | 107 | bool getData(uint8_t*& men, int32_t id, uint8_t mode=0x0); |
---|
| 108 | uint32_t getSize(int32_t id, uint8_t mode=0x0); |
---|
[2171] | 109 | bool updateData(uint8_t*& mem, uint8_t mode=0x0, bool forceCallback=false); |
---|
[1907] | 110 | bool isMyData(uint8_t* mem); |
---|
[2309] | 111 | bool doSelection(int32_t id); |
---|
| 112 | bool doSync(int32_t id, uint8_t mode=0x0); |
---|
[2087] | 113 | |
---|
[2309] | 114 | uint32_t objectID; |
---|
| 115 | uint32_t creatorID; |
---|
| 116 | uint32_t classID; |
---|
[2087] | 117 | |
---|
[2245] | 118 | std::list<SynchronisableVariableBase*> syncList; |
---|
[2171] | 119 | static uint8_t state_; // detemines wheter we are server (default) or client |
---|
[1505] | 120 | bool backsync_; // if true the variables with mode > 1 will be synchronised to server (client -> server) |
---|
[1751] | 121 | unsigned int objectFrequency_; |
---|
| 122 | int objectMode_; |
---|
[2309] | 123 | static std::map<uint32_t, Synchronisable *> objectMap_; |
---|
| 124 | static std::queue<uint32_t> deletedObjects_; |
---|
[1505] | 125 | }; |
---|
[2245] | 126 | |
---|
| 127 | template <class T> void Synchronisable::registerVariable(T& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional) |
---|
| 128 | { |
---|
| 129 | if (bidirectional) |
---|
| 130 | syncList.push_back(new SynchronisableVariableBidirectional<const T>(variable, mode, cb)); |
---|
| 131 | else |
---|
| 132 | syncList.push_back(new SynchronisableVariable<const T>(variable, mode, cb)); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | template <class T> void Synchronisable::unregisterVariable(T& var){ |
---|
| 136 | std::list<SynchronisableVariableBase*>::iterator it = syncList.begin(); |
---|
| 137 | while(it!=syncList.end()){ |
---|
| 138 | if( ((*it)->getReference()) == &var ){ |
---|
| 139 | delete (*it); |
---|
| 140 | syncList.erase(it); |
---|
| 141 | return; |
---|
| 142 | } |
---|
| 143 | else |
---|
| 144 | it++; |
---|
| 145 | } |
---|
| 146 | bool unregistered_nonexistent_variable = false; |
---|
| 147 | assert(unregistered_nonexistent_variable); //if we reach this point something went wrong: |
---|
| 148 | // the variable has not been registered before |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | // ================= Specialisation declarations |
---|
[2307] | 152 | template <> _NetworkExport void Synchronisable::registerVariable( const ColourValue& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
| 153 | template <> _NetworkExport void Synchronisable::registerVariable( ColourValue& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
| 154 | template <> _NetworkExport void Synchronisable::registerVariable( const Vector2& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
| 155 | template <> _NetworkExport void Synchronisable::registerVariable( Vector2& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
| 156 | template <> _NetworkExport void Synchronisable::registerVariable( const Vector3& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
| 157 | template <> _NetworkExport void Synchronisable::registerVariable( Vector3& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
| 158 | template <> _NetworkExport void Synchronisable::registerVariable( const Vector4& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
| 159 | template <> _NetworkExport void Synchronisable::registerVariable( Vector4& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
| 160 | template <> _NetworkExport void Synchronisable::registerVariable( mbool& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
| 161 | template <> _NetworkExport void Synchronisable::registerVariable( const Quaternion& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
| 162 | template <> _NetworkExport void Synchronisable::registerVariable( Quaternion& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
| 163 | // template <> _NetworkExport void Synchronisable::registerVariable( LODParticle::LOD& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
| 164 | // template <> _NetworkExport void Synchronisable::registerVariable( Ogre::Light::LightTypes& variable, uint8_t mode, NetworkCallbackBase* cb, bool bidirectional); |
---|
[1505] | 165 | } |
---|
| 166 | |
---|
| 167 | #endif /* _Synchronisable_H__ */ |
---|