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