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