gameConnection.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _GAMECONNECTION_H_
  23. #define _GAMECONNECTION_H_
  24. #ifndef _SIMBASE_H_
  25. #include "console/simBase.h"
  26. #endif
  27. #ifndef _GAMEBASE_H_
  28. #include "T3D/gameBase/gameBase.h"
  29. #endif
  30. #ifndef _NETCONNECTION_H_
  31. #include "sim/netConnection.h"
  32. #endif
  33. #ifndef _MOVEMANAGER_H_
  34. #include "T3D/gameBase/moveManager.h"
  35. #endif
  36. #ifndef _BITVECTOR_H_
  37. #include "core/bitVector.h"
  38. #endif
  39. enum GameConnectionConstants
  40. {
  41. MaxClients = 126,
  42. DataBlockQueueCount = 16
  43. };
  44. class SFXProfile;
  45. class MatrixF;
  46. class MatrixF;
  47. class Point3F;
  48. class MoveManager;
  49. class MoveList;
  50. struct Move;
  51. struct AuthInfo;
  52. #define GameString TORQUE_APP_NAME
  53. const F32 MinCameraFov = 1.f; ///< min camera FOV
  54. const F32 MaxCameraFov = 179.f; ///< max camera FOV
  55. class GameConnection : public NetConnection
  56. {
  57. private:
  58. typedef NetConnection Parent;
  59. SimObjectPtr<GameBase> mControlObject;
  60. SimObjectPtr<GameBase> mCameraObject;
  61. U32 mDataBlockSequence;
  62. char mDisconnectReason[256];
  63. U32 mMissionCRC; // crc of the current mission file from the server
  64. private:
  65. U32 mLastControlRequestTime;
  66. S32 mDataBlockModifiedKey;
  67. S32 mMaxDataBlockModifiedKey;
  68. /// @name Client side first/third person
  69. /// @{
  70. ///
  71. bool mFirstPerson; ///< Are we currently first person or not.
  72. bool mUpdateFirstPerson; ///< Set to notify client or server of first person change.
  73. bool mUpdateCameraFov; ///< Set to notify server of camera FOV change.
  74. F32 mCameraFov; ///< Current camera fov (in degrees).
  75. F32 mCameraPos; ///< Current camera pos (0-1).
  76. F32 mCameraSpeed; ///< Camera in/out speed.
  77. /// @}
  78. public:
  79. /// @name Protocol Versions
  80. ///
  81. /// Protocol versions are used to indicated changes in network traffic.
  82. /// These could be changes in how any object transmits or processes
  83. /// network information. You can specify backwards compatibility by
  84. /// specifying a MinRequireProtocolVersion. If the client
  85. /// protocol is >= this min value, the connection is accepted.
  86. ///
  87. /// Torque (V12) SDK 1.0 uses protocol = 1
  88. ///
  89. /// Torque SDK 1.1 uses protocol = 2
  90. /// Torque SDK 1.4 uses protocol = 12
  91. /// @{
  92. static const U32 CurrentProtocolVersion;
  93. static const U32 MinRequiredProtocolVersion;
  94. /// @}
  95. /// Configuration
  96. enum Constants {
  97. BlockTypeMove = NetConnectionBlockTypeCount,
  98. GameConnectionBlockTypeCount,
  99. MaxConnectArgs = 16,
  100. DataBlocksDone = NumConnectionMessages,
  101. DataBlocksDownloadDone,
  102. };
  103. /// Set connection arguments; these are passed to the server when we connect.
  104. void setConnectArgs(U32 argc, const char **argv);
  105. /// Set the server password to use when we join.
  106. void setJoinPassword(const char *password);
  107. /// @name Event Handling
  108. /// @{
  109. virtual void onTimedOut();
  110. virtual void onConnectTimedOut();
  111. virtual void onDisconnect(const char *reason);
  112. virtual void onConnectionRejected(const char *reason);
  113. virtual void onConnectionEstablished(bool isInitiator);
  114. virtual void handleStartupError(const char *errorString);
  115. /// @}
  116. /// @name Packet I/O
  117. /// @{
  118. virtual void writeConnectRequest(BitStream *stream);
  119. virtual bool readConnectRequest(BitStream *stream, const char **errorString);
  120. virtual void writeConnectAccept(BitStream *stream);
  121. virtual bool readConnectAccept(BitStream *stream, const char **errorString);
  122. /// @}
  123. bool canRemoteCreate();
  124. private:
  125. /// @name Connection State
  126. /// This data is set with setConnectArgs() and setJoinPassword(), and
  127. /// sent across the wire when we connect.
  128. /// @{
  129. U32 mConnectArgc;
  130. char *mConnectArgv[MaxConnectArgs];
  131. char *mJoinPassword;
  132. /// @}
  133. protected:
  134. struct GamePacketNotify : public NetConnection::PacketNotify
  135. {
  136. S32 cameraFov;
  137. GamePacketNotify();
  138. };
  139. PacketNotify *allocNotify();
  140. bool mControlForceMismatch;
  141. Vector<SimDataBlock *> mDataBlockLoadList;
  142. public:
  143. MoveList *mMoveList;
  144. protected:
  145. bool mAIControlled;
  146. AuthInfo * mAuthInfo;
  147. static S32 mLagThresholdMS;
  148. S32 mLastPacketTime;
  149. bool mLagging;
  150. /// @name Flashing
  151. ////
  152. /// Note, these variables are not networked, they are for the local connection only.
  153. /// @{
  154. F32 mDamageFlash;
  155. F32 mWhiteOut;
  156. F32 mBlackOut;
  157. S32 mBlackOutTimeMS;
  158. S32 mBlackOutStartTimeMS;
  159. bool mFadeToBlack;
  160. /// @}
  161. /// @name Packet I/O
  162. /// @{
  163. void readPacket (BitStream *bstream);
  164. void writePacket (BitStream *bstream, PacketNotify *note);
  165. void packetReceived (PacketNotify *note);
  166. void packetDropped (PacketNotify *note);
  167. void connectionError (const char *errorString);
  168. void writeDemoStartBlock (ResizeBitStream *stream);
  169. bool readDemoStartBlock (BitStream *stream);
  170. void handleRecordedBlock (U32 type, U32 size, void *data);
  171. /// @}
  172. void ghostWriteExtra(NetObject *,BitStream *);
  173. void ghostReadExtra(NetObject *,BitStream *, bool newGhost);
  174. void ghostPreRead(NetObject *, bool newGhost);
  175. virtual void onEndGhosting();
  176. public:
  177. DECLARE_CONOBJECT(GameConnection);
  178. void handleConnectionMessage(U32 message, U32 sequence, U32 ghostCount);
  179. void preloadDataBlock(SimDataBlock *block);
  180. void fileDownloadSegmentComplete();
  181. void preloadNextDataBlock(bool hadNew);
  182. static void consoleInit();
  183. void setDisconnectReason(const char *reason);
  184. GameConnection();
  185. ~GameConnection();
  186. bool onAdd();
  187. void onRemove();
  188. static GameConnection *getConnectionToServer()
  189. {
  190. return dynamic_cast<GameConnection*>((NetConnection *) mServerConnection);
  191. }
  192. static GameConnection *getLocalClientConnection()
  193. {
  194. return dynamic_cast<GameConnection*>((NetConnection *) mLocalClientConnection);
  195. }
  196. /// @name Control object
  197. /// @{
  198. ///
  199. void setControlObject(GameBase *);
  200. GameBase* getControlObject() { return mControlObject; }
  201. const GameBase* getControlObject() const { return mControlObject; }
  202. void setCameraObject(GameBase *);
  203. GameBase* getCameraObject();
  204. bool getControlCameraTransform(F32 dt,MatrixF* mat);
  205. bool getControlCameraVelocity(Point3F *vel);
  206. bool getControlCameraDefaultFov(F32 *fov);
  207. bool getControlCameraFov(F32 *fov);
  208. bool setControlCameraFov(F32 fov);
  209. bool isValidControlCameraFov(F32 fov);
  210. // Used by editor
  211. bool isControlObjectRotDampedCamera();
  212. void setFirstPerson(bool firstPerson);
  213. /// @}
  214. void detectLag();
  215. /// @name Datablock management
  216. /// @{
  217. S32 getDataBlockModifiedKey () { return mDataBlockModifiedKey; }
  218. void setDataBlockModifiedKey (S32 key) { mDataBlockModifiedKey = key; }
  219. S32 getMaxDataBlockModifiedKey () { return mMaxDataBlockModifiedKey; }
  220. void setMaxDataBlockModifiedKey (S32 key) { mMaxDataBlockModifiedKey = key; }
  221. /// Return the datablock sequence number that this game connection is on.
  222. /// The datablock sequence number is synchronized to the mission sequence number
  223. /// on each datablock transmission.
  224. U32 getDataBlockSequence() { return mDataBlockSequence; }
  225. /// Set the datablock sequence number.
  226. void setDataBlockSequence(U32 seq) { mDataBlockSequence = seq; }
  227. /// @}
  228. /// @name Fade control
  229. /// @{
  230. F32 getDamageFlash() const { return mDamageFlash; }
  231. F32 getWhiteOut() const { return mWhiteOut; }
  232. void setBlackOut(bool fadeToBlack, S32 timeMS);
  233. F32 getBlackOut();
  234. /// @}
  235. /// @name Authentication
  236. ///
  237. /// This is remnant code from Tribes 2.
  238. /// @{
  239. void setAuthInfo(const AuthInfo *info);
  240. const AuthInfo *getAuthInfo();
  241. /// @}
  242. /// @name Sound
  243. /// @{
  244. void play2D(SFXProfile *profile);
  245. void play3D(SFXProfile *profile, const MatrixF *transform);
  246. /// @}
  247. /// @name Misc.
  248. /// @{
  249. bool isFirstPerson() const { return mCameraPos == 0; }
  250. bool isAIControlled() { return mAIControlled; }
  251. void doneScopingScene();
  252. void demoPlaybackComplete();
  253. void setMissionCRC(U32 crc) { mMissionCRC = crc; }
  254. U32 getMissionCRC() { return(mMissionCRC); }
  255. /// @}
  256. static Signal<void(F32)> smFovUpdate;
  257. static Signal<void()> smPlayingDemo;
  258. protected:
  259. DECLARE_CALLBACK( void, onConnectionTimedOut, () );
  260. DECLARE_CALLBACK( void, onConnectionAccepted, () );
  261. DECLARE_CALLBACK( void, onConnectRequestTimedOut, () );
  262. DECLARE_CALLBACK( void, onConnectionDropped, (const char* reason) );
  263. DECLARE_CALLBACK( void, onConnectRequestRejected, (const char* reason) );
  264. DECLARE_CALLBACK( void, onConnectionError, (const char* errorString) );
  265. DECLARE_CALLBACK( void, onDrop, (const char* disconnectReason) );
  266. DECLARE_CALLBACK( void, initialControlSet, () );
  267. DECLARE_CALLBACK( void, onControlObjectChange, () );
  268. DECLARE_CALLBACK( void, setLagIcon, (bool state) );
  269. DECLARE_CALLBACK( void, onDataBlocksDone, (U32 sequence) );
  270. DECLARE_CALLBACK( void, onFlash, (bool state) );
  271. };
  272. #endif