[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 | * |
---|
[6665] | 21 | * CREATE_ENTITY: >> CLASS_ID, UNIQUE_ID, OWNER |
---|
| 22 | * REMOVE_ENTITY: >> UNIQUE_ID |
---|
[6111] | 23 | * |
---|
[6665] | 24 | * CREATE_ENTITY_LIST: >> NUMBER, [CLASS_ID, UNIQUE_ID, OWNER][0..NUMBER] |
---|
| 25 | * REMOVE_ENTITY_LIST: >> NUMBER, [UNIQUE_ID][0..NUMBER] |
---|
[6111] | 26 | * |
---|
[6665] | 27 | * REQUEST_CREATE: >> CLASS_ID |
---|
| 28 | * << [Sync Info] |
---|
| 29 | * REQUEST_REMOVE: >> UNIQUE_ID |
---|
| 30 | * << [Sync Info] |
---|
[6111] | 31 | * |
---|
[6341] | 32 | * //REQUEST_CREATE_LIST: NUMBER, [CLASS_ID][0..NUMBER] |
---|
| 33 | * //REQUEST_CREATE_LIST: NUMBER, [UNIQUE_ID][0..NUMBER] |
---|
[6111] | 34 | * |
---|
[6341] | 35 | * REQUEST_ENTITY_LIST: //request the whole world :D |
---|
[6665] | 36 | * REQUEST_SYNC: >> UNIQUE_ID |
---|
| 37 | * << [Sync Info] |
---|
[6341] | 38 | * //REQUEST_SYNC_LIST: NUMBER, [UNIQUE_ID][0..NUMBER] |
---|
[6111] | 39 | * |
---|
[6665] | 40 | * YOU_ARE_ENTITY: >> UNIQUE_ID |
---|
[6341] | 41 | * |
---|
[6665] | 42 | * REQUEST_PNODE_PATH >> UNIQUE_ID_START UNIQUE_ID_STOP |
---|
| 43 | * << UNIQUE_ID_1 UNIQUE_ID_2 UNIQUE_ID_3 ... UNIQUE_ID_N |
---|
| 44 | * |
---|
[6111] | 45 | */ |
---|
| 46 | |
---|
[6666] | 47 | typedef enum NetworkGameManagerProtocol { |
---|
[6665] | 48 | NET_CREATE_ENTITY = 0, |
---|
| 49 | NET_REMOVE_ENTITY, |
---|
| 50 | NET_CREATE_ENTITY_LIST, |
---|
| 51 | NET_REMOVE_ENTITY_LIST, |
---|
| 52 | NET_REQUEST_CREATE, |
---|
| 53 | NET_REQUEST_REMOVE, |
---|
| 54 | NET_REQUEST_SYNC, |
---|
| 55 | NET_YOU_ARE_ENTITY, |
---|
| 56 | NET_REQUEST_ENTITY_LIST, |
---|
| 57 | NET_REQUEST_PNODE_PATH, |
---|
| 58 | |
---|
| 59 | NET_NUMBER |
---|
[6341] | 60 | }; |
---|
[6111] | 61 | |
---|
[6341] | 62 | struct clientBuffer |
---|
| 63 | { |
---|
| 64 | int length; |
---|
| 65 | int maxLength; |
---|
| 66 | byte * buffer; |
---|
| 67 | }; |
---|
[6111] | 68 | |
---|
[6067] | 69 | /*! |
---|
| 70 | * a class that can create and remove entities |
---|
| 71 | */ |
---|
[6116] | 72 | class NetworkGameManager: public Synchronizeable |
---|
[6067] | 73 | { |
---|
| 74 | public: |
---|
[6116] | 75 | ~NetworkGameManager(); |
---|
[6111] | 76 | |
---|
[6341] | 77 | static NetworkGameManager* NetworkGameManager::getInstance() |
---|
| 78 | { if (!NetworkGameManager::singletonRef) NetworkGameManager::singletonRef = new NetworkGameManager(); return NetworkGameManager::singletonRef; } |
---|
| 79 | |
---|
| 80 | virtual int writeBytes(const byte* data, int length, int sender); |
---|
[6067] | 81 | virtual int readBytes(byte* data, int maxLength, int * reciever); |
---|
| 82 | virtual void writeDebug() const; |
---|
| 83 | virtual void readDebug() const; |
---|
[6111] | 84 | |
---|
[6498] | 85 | int createEntity( ClassID classID, int owner = 0 ); |
---|
[6424] | 86 | BaseObject* createEntity(const TiXmlElement* element); |
---|
[6341] | 87 | void removeEntity( int uniqueID ); |
---|
| 88 | void sendYouAre( int uniqueID, int userID ); |
---|
[6111] | 89 | |
---|
[6341] | 90 | void sync(int uniqueID, int owner); |
---|
[6111] | 91 | |
---|
[6341] | 92 | void sendEntityList(int userID); |
---|
[6111] | 93 | |
---|
[6067] | 94 | private: |
---|
[6341] | 95 | NetworkGameManager(); |
---|
[6111] | 96 | |
---|
[6341] | 97 | void requestCreateEntity(ClassID classID); |
---|
[6498] | 98 | int executeCreateEntity(ClassID classID, int uniqueID = 0, int owner = 0); |
---|
[6341] | 99 | |
---|
[6067] | 100 | void requestRemoveEntity(int uniqueID); |
---|
| 101 | void executeRemoveEntity(int uniqueID); |
---|
[6111] | 102 | |
---|
[6341] | 103 | void executeRequestSync( int uniqueID, int user ); |
---|
[6111] | 104 | |
---|
[6424] | 105 | BaseObject* doCreateEntity(ClassID classID, int uniqueID, int owner); |
---|
[6341] | 106 | void doRemoveEntity(int uniqueID); |
---|
| 107 | void doRequestSync(int uniqueID, int userID); |
---|
| 108 | void doYouAre( int uniqueID ); |
---|
[6111] | 109 | |
---|
[6341] | 110 | bool canCreateEntity(ClassID classID); |
---|
[6111] | 111 | |
---|
[6498] | 112 | bool signalNewPlayer(int userId); |
---|
| 113 | |
---|
[6341] | 114 | void resizeBufferVector(int n); |
---|
[6111] | 115 | |
---|
[6341] | 116 | inline bool writeToClientBuffer( clientBuffer &cb, byte*data, int length ); |
---|
| 117 | inline bool writeToClientBuffer( clientBuffer &cb, byte b ); |
---|
| 118 | inline bool writeToClientBuffer( clientBuffer &cb, int i ); |
---|
| 119 | inline bool readFromClientBuffer( clientBuffer &cb, byte*data, int length ); |
---|
[6111] | 120 | |
---|
[6341] | 121 | //helper functions for writeBytes |
---|
| 122 | inline bool handleRequestCreate( int& i, const byte* data, int length, int sender ); |
---|
| 123 | inline bool handleRequestRemove( int& i, const byte* data, int length, int sender ); |
---|
| 124 | inline bool handleCreateEntity( int& i, const byte* data, int length, int sender ); |
---|
| 125 | inline bool handleRemoveEntity( int& i, const byte* data, int length, int sender ); |
---|
| 126 | inline bool handleCreateEntityList( int& i, const byte* data, int length, int sender ); |
---|
| 127 | inline bool handleRemoveEntityList( int& i, const byte* data, int length, int sender ); |
---|
| 128 | inline bool handleYouAreEntity( int& i, const byte* data, int length, int sender ); |
---|
| 129 | inline bool handleRequestSync( int& i, const byte* data, int length, int sender ); |
---|
| 130 | |
---|
[6111] | 131 | private: |
---|
[6341] | 132 | std::vector<clientBuffer> outBuffer; |
---|
[6634] | 133 | //clientBuffer allOutBuffer; |
---|
[6341] | 134 | static NetworkGameManager* singletonRef; |
---|
| 135 | |
---|
| 136 | int newUniqueID; |
---|
| 137 | bool hasRequestedWorld; |
---|
[6067] | 138 | }; |
---|
| 139 | |
---|
| 140 | #endif /*_ENTITY_MANGER*/ |
---|