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