[6067] | 1 | /*! |
---|
| 2 | * @file entity_manager.h |
---|
| 3 | * Manages creation and destruction of entities |
---|
| 4 | */ |
---|
[6111] | 5 | |
---|
[6116] | 6 | #ifndef _NETWORK_GAME_MANGER |
---|
| 7 | #define _NETWORK_GAME_MANAGER |
---|
[6067] | 8 | |
---|
| 9 | /* include this file, it contains some default definitions */ |
---|
| 10 | #include "netdefs.h" |
---|
| 11 | |
---|
| 12 | /* include base_object.h since all classes are derived from this one */ |
---|
| 13 | #include "synchronizeable.h" |
---|
| 14 | |
---|
[6111] | 15 | |
---|
[6341] | 16 | class TiXmlElement; |
---|
[6111] | 17 | |
---|
| 18 | /** |
---|
| 19 | * protocol definition |
---|
| 20 | * |
---|
[6341] | 21 | * CREATE_ENTITY: CLASS_ID, UNIQUE_ID, OWNER |
---|
| 22 | * REMOVE_ENTITY: UNIQUE_ID |
---|
[6111] | 23 | * |
---|
[6341] | 24 | * CREATE_ENTITY_LIST: NUMBER, [CLASS_ID, UNIQUE_ID, OWNER][0..NUMBER] |
---|
| 25 | * REMOVE_ENTITY_LIST: NUMBER, [UNIQUE_ID][0..NUMBER] |
---|
[6111] | 26 | * |
---|
[6341] | 27 | * REQUEST_CREATE: CLASS_ID |
---|
| 28 | * REQUEST_REMOVE: UNIQUE_ID |
---|
[6111] | 29 | * |
---|
[6341] | 30 | * //REQUEST_CREATE_LIST: NUMBER, [CLASS_ID][0..NUMBER] |
---|
| 31 | * //REQUEST_CREATE_LIST: NUMBER, [UNIQUE_ID][0..NUMBER] |
---|
[6111] | 32 | * |
---|
[6341] | 33 | * REQUEST_ENTITY_LIST: //request the whole world :D |
---|
| 34 | * REQUEST_SYNC: UNIQUE_ID |
---|
| 35 | * //REQUEST_SYNC_LIST: NUMBER, [UNIQUE_ID][0..NUMBER] |
---|
[6111] | 36 | * |
---|
[6341] | 37 | * YOU_ARE_ENTITY: UNIQUE_ID |
---|
| 38 | * |
---|
[6111] | 39 | */ |
---|
| 40 | |
---|
[6341] | 41 | typedef enum NetworkGameManagerProtocol{ |
---|
| 42 | CREATE_ENTITY = 0, |
---|
| 43 | REMOVE_ENTITY, |
---|
| 44 | CREATE_ENTITY_LIST, |
---|
| 45 | REMOVE_ENTITY_LIST, |
---|
| 46 | REQUEST_CREATE, |
---|
| 47 | REQUEST_REMOVE, |
---|
| 48 | REQUEST_SYNC, |
---|
| 49 | YOU_ARE_ENTITY, |
---|
| 50 | REQUEST_ENTITY_LIST |
---|
| 51 | }; |
---|
[6111] | 52 | |
---|
[6341] | 53 | struct clientBuffer |
---|
| 54 | { |
---|
| 55 | int length; |
---|
| 56 | int maxLength; |
---|
| 57 | byte * buffer; |
---|
| 58 | }; |
---|
[6111] | 59 | |
---|
[6067] | 60 | /*! |
---|
| 61 | * a class that can create and remove entities |
---|
| 62 | */ |
---|
[6116] | 63 | class NetworkGameManager: public Synchronizeable |
---|
[6067] | 64 | { |
---|
| 65 | public: |
---|
[6116] | 66 | ~NetworkGameManager(); |
---|
[6111] | 67 | |
---|
[6341] | 68 | static NetworkGameManager* NetworkGameManager::getInstance() |
---|
| 69 | { if (!NetworkGameManager::singletonRef) NetworkGameManager::singletonRef = new NetworkGameManager(); return NetworkGameManager::singletonRef; } |
---|
| 70 | |
---|
| 71 | virtual int writeBytes(const byte* data, int length, int sender); |
---|
[6067] | 72 | virtual int readBytes(byte* data, int maxLength, int * reciever); |
---|
| 73 | virtual void writeDebug() const; |
---|
| 74 | virtual void readDebug() const; |
---|
[6111] | 75 | |
---|
[6341] | 76 | void createEntity( ClassID classID, int owner = 0 ); |
---|
[6412] | 77 | BaseObject* createEntity(const TiXmlElement* element); |
---|
[6341] | 78 | void removeEntity( int uniqueID ); |
---|
| 79 | void sendYouAre( int uniqueID, int userID ); |
---|
[6111] | 80 | |
---|
[6341] | 81 | void sync(int uniqueID, int owner); |
---|
[6111] | 82 | |
---|
[6341] | 83 | void sendEntityList(int userID); |
---|
[6111] | 84 | |
---|
[6067] | 85 | private: |
---|
[6341] | 86 | NetworkGameManager(); |
---|
[6111] | 87 | |
---|
[6341] | 88 | void requestCreateEntity(ClassID classID); |
---|
| 89 | void executeCreateEntity(ClassID classID, int uniqueID = 0, int owner = 0); |
---|
| 90 | |
---|
[6067] | 91 | void requestRemoveEntity(int uniqueID); |
---|
| 92 | void executeRemoveEntity(int uniqueID); |
---|
[6111] | 93 | |
---|
[6341] | 94 | void executeRequestSync( int uniqueID, int user ); |
---|
[6111] | 95 | |
---|
[6419] | 96 | BaseObject* doCreateEntity(ClassID classID, int uniqueID, int owner); |
---|
[6341] | 97 | void doRemoveEntity(int uniqueID); |
---|
| 98 | void doRequestSync(int uniqueID, int userID); |
---|
| 99 | void doYouAre( int uniqueID ); |
---|
[6111] | 100 | |
---|
[6341] | 101 | bool canCreateEntity(ClassID classID); |
---|
[6111] | 102 | |
---|
[6341] | 103 | void resizeBufferVector(int n); |
---|
[6111] | 104 | |
---|
[6341] | 105 | inline bool writeToClientBuffer( clientBuffer &cb, byte*data, int length ); |
---|
| 106 | inline bool writeToClientBuffer( clientBuffer &cb, byte b ); |
---|
| 107 | inline bool writeToClientBuffer( clientBuffer &cb, int i ); |
---|
| 108 | inline bool readFromClientBuffer( clientBuffer &cb, byte*data, int length ); |
---|
[6111] | 109 | |
---|
[6341] | 110 | //helper functions for writeBytes |
---|
| 111 | inline bool handleRequestCreate( int& i, const byte* data, int length, int sender ); |
---|
| 112 | inline bool handleRequestRemove( int& i, const byte* data, int length, int sender ); |
---|
| 113 | inline bool handleCreateEntity( int& i, const byte* data, int length, int sender ); |
---|
| 114 | inline bool handleRemoveEntity( int& i, const byte* data, int length, int sender ); |
---|
| 115 | inline bool handleCreateEntityList( int& i, const byte* data, int length, int sender ); |
---|
| 116 | inline bool handleRemoveEntityList( int& i, const byte* data, int length, int sender ); |
---|
| 117 | inline bool handleYouAreEntity( int& i, const byte* data, int length, int sender ); |
---|
| 118 | inline bool handleRequestSync( int& i, const byte* data, int length, int sender ); |
---|
| 119 | |
---|
[6111] | 120 | private: |
---|
[6341] | 121 | std::vector<clientBuffer> outBuffer; |
---|
| 122 | clientBuffer allOutBuffer; |
---|
| 123 | static NetworkGameManager* singletonRef; |
---|
| 124 | |
---|
| 125 | int newUniqueID; |
---|
| 126 | bool hasRequestedWorld; |
---|
[6067] | 127 | }; |
---|
| 128 | |
---|
| 129 | #endif /*_ENTITY_MANGER*/ |
---|