gameConnection.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 IDisplayDevice;
  45. class SFXProfile;
  46. class MatrixF;
  47. class MatrixF;
  48. class Point3F;
  49. class MoveManager;
  50. class MoveList;
  51. struct Move;
  52. struct AuthInfo;
  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. F32 mVisibleGhostDistance;
  65. private:
  66. U32 mLastControlRequestTime;
  67. S32 mDataBlockModifiedKey;
  68. S32 mMaxDataBlockModifiedKey;
  69. /// @name Client side first/third person
  70. /// @{
  71. ///
  72. bool mFirstPerson; ///< Are we currently first person or not.
  73. bool mUpdateFirstPerson; ///< Set to notify client or server of first person change.
  74. bool mUpdateCameraFov; ///< Set to notify server of camera FOV change.
  75. F32 mCameraFov; ///< Current camera fov (in degrees).
  76. F32 mCameraPos; ///< Current camera pos (0-1).
  77. F32 mCameraSpeed; ///< Camera in/out speed.
  78. IDisplayDevice* mDisplayDevice; ///< Optional client display device that imposes rendering properties.
  79. /// @}
  80. /// @name Client side control scheme that may be referenced by control objects
  81. /// @{
  82. bool mUpdateControlScheme; ///< Set to notify client or server of control scheme change
  83. bool mAbsoluteRotation; ///< Use absolute rotation values from client, likely through ExtendedMove
  84. bool mAddYawToAbsRot; ///< Add relative yaw control to the absolute rotation calculation. Only useful with mAbsoluteRotation.
  85. bool mAddPitchToAbsRot; ///< Add relative pitch control to the absolute rotation calculation. Only useful with mAbsoluteRotation.
  86. /// @}
  87. public:
  88. /// @name Protocol Versions
  89. ///
  90. /// Protocol versions are used to indicated changes in network traffic.
  91. /// These could be changes in how any object transmits or processes
  92. /// network information. You can specify backwards compatibility by
  93. /// specifying a MinRequireProtocolVersion. If the client
  94. /// protocol is >= this min value, the connection is accepted.
  95. ///
  96. /// Torque (V12) SDK 1.0 uses protocol = 1
  97. ///
  98. /// Torque SDK 1.1 uses protocol = 2
  99. /// Torque SDK 1.4 uses protocol = 12
  100. /// @{
  101. static const U32 CurrentProtocolVersion;
  102. static const U32 MinRequiredProtocolVersion;
  103. /// @}
  104. /// Configuration
  105. enum Constants {
  106. BlockTypeMove = NetConnectionBlockTypeCount,
  107. GameConnectionBlockTypeCount,
  108. MaxConnectArgs = 16,
  109. DataBlocksDone = NumConnectionMessages,
  110. DataBlocksDownloadDone,
  111. };
  112. /// Set connection arguments; these are passed to the server when we connect.
  113. void setConnectArgs(U32 argc, const char **argv);
  114. /// Set the server password to use when we join.
  115. void setJoinPassword(const char *password);
  116. /// @name Event Handling
  117. /// @{
  118. virtual void onTimedOut();
  119. virtual void onConnectTimedOut();
  120. virtual void onDisconnect(const char *reason);
  121. virtual void onConnectionRejected(const char *reason);
  122. virtual void onConnectionEstablished(bool isInitiator);
  123. virtual void handleStartupError(const char *errorString);
  124. /// @}
  125. /// @name Packet I/O
  126. /// @{
  127. virtual void writeConnectRequest(BitStream *stream);
  128. virtual bool readConnectRequest(BitStream *stream, const char **errorString);
  129. virtual void writeConnectAccept(BitStream *stream);
  130. virtual bool readConnectAccept(BitStream *stream, const char **errorString);
  131. /// @}
  132. bool canRemoteCreate();
  133. void setVisibleGhostDistance(F32 dist);
  134. F32 getVisibleGhostDistance();
  135. private:
  136. /// @name Connection State
  137. /// This data is set with setConnectArgs() and setJoinPassword(), and
  138. /// sent across the wire when we connect.
  139. /// @{
  140. U32 mConnectArgc;
  141. char *mConnectArgv[MaxConnectArgs];
  142. char *mJoinPassword;
  143. /// @}
  144. protected:
  145. struct GamePacketNotify : public NetConnection::PacketNotify
  146. {
  147. S32 cameraFov;
  148. GamePacketNotify();
  149. };
  150. PacketNotify *allocNotify();
  151. bool mControlForceMismatch;
  152. Vector<SimDataBlock *> mDataBlockLoadList;
  153. public:
  154. MoveList *mMoveList;
  155. protected:
  156. bool mAIControlled;
  157. AuthInfo * mAuthInfo;
  158. static S32 mLagThresholdMS;
  159. S32 mLastPacketTime;
  160. bool mLagging;
  161. /// @name Flashing
  162. ////
  163. /// Note, these variables are not networked, they are for the local connection only.
  164. /// @{
  165. F32 mDamageFlash;
  166. F32 mWhiteOut;
  167. F32 mBlackOut;
  168. S32 mBlackOutTimeMS;
  169. S32 mBlackOutStartTimeMS;
  170. bool mFadeToBlack;
  171. /// @}
  172. /// @name Packet I/O
  173. /// @{
  174. void readPacket (BitStream *bstream);
  175. void writePacket (BitStream *bstream, PacketNotify *note);
  176. void packetReceived (PacketNotify *note);
  177. void packetDropped (PacketNotify *note);
  178. void connectionError (const char *errorString);
  179. void writeDemoStartBlock (ResizeBitStream *stream);
  180. bool readDemoStartBlock (BitStream *stream);
  181. void handleRecordedBlock (U32 type, U32 size, void *data);
  182. /// @}
  183. void ghostWriteExtra(NetObject *,BitStream *);
  184. void ghostReadExtra(NetObject *,BitStream *, bool newGhost);
  185. void ghostPreRead(NetObject *, bool newGhost);
  186. virtual void onEndGhosting();
  187. public:
  188. DECLARE_CONOBJECT(GameConnection);
  189. void handleConnectionMessage(U32 message, U32 sequence, U32 ghostCount);
  190. void preloadDataBlock(SimDataBlock *block);
  191. void fileDownloadSegmentComplete();
  192. void preloadNextDataBlock(bool hadNew);
  193. static void consoleInit();
  194. void setDisconnectReason(const char *reason);
  195. GameConnection();
  196. ~GameConnection();
  197. bool onAdd();
  198. void onRemove();
  199. static GameConnection *getConnectionToServer()
  200. {
  201. return dynamic_cast<GameConnection*>((NetConnection *) mServerConnection);
  202. }
  203. static GameConnection *getLocalClientConnection()
  204. {
  205. return dynamic_cast<GameConnection*>((NetConnection *) mLocalClientConnection);
  206. }
  207. /// @name Control object
  208. /// @{
  209. ///
  210. void setControlObject(GameBase *);
  211. GameBase* getControlObject() { return mControlObject; }
  212. const GameBase* getControlObject() const { return mControlObject; }
  213. void setCameraObject(GameBase *);
  214. GameBase* getCameraObject();
  215. bool getControlCameraTransform(F32 dt,MatrixF* mat);
  216. bool getControlCameraVelocity(Point3F *vel);
  217. /// Returns the head transform for the control object, using supplemental information
  218. /// from the provided IDisplayDevice
  219. bool getControlCameraHeadTransform(IDisplayDevice *display, MatrixF *transform);
  220. /// Returns the eye transforms for the control object, using supplemental information
  221. /// from the provided IDisplayDevice.
  222. bool getControlCameraEyeTransforms(IDisplayDevice *display, MatrixF *transforms);
  223. bool getControlCameraDefaultFov(F32 *fov);
  224. bool getControlCameraFov(F32 *fov);
  225. bool setControlCameraFov(F32 fov);
  226. bool isValidControlCameraFov(F32 fov);
  227. // Used by editor
  228. bool isControlObjectRotDampedCamera();
  229. void setFirstPerson(bool firstPerson);
  230. bool hasDisplayDevice() const { return mDisplayDevice != NULL; }
  231. IDisplayDevice* getDisplayDevice() const { return mDisplayDevice; }
  232. void setDisplayDevice(IDisplayDevice* display) { if (mDisplayDevice) mDisplayDevice->setDrawCanvas(NULL); mDisplayDevice = display; }
  233. void clearDisplayDevice() { mDisplayDevice = NULL; }
  234. void setControlSchemeParameters(bool absoluteRotation, bool addYawToAbsRot, bool addPitchToAbsRot);
  235. bool getControlSchemeAbsoluteRotation() {return mAbsoluteRotation;}
  236. bool getControlSchemeAddYawToAbsRot() {return mAddYawToAbsRot;}
  237. bool getControlSchemeAddPitchToAbsRot() {return mAddPitchToAbsRot;}
  238. /// @}
  239. void detectLag();
  240. /// @name Datablock management
  241. /// @{
  242. S32 getDataBlockModifiedKey () { return mDataBlockModifiedKey; }
  243. void setDataBlockModifiedKey (S32 key) { mDataBlockModifiedKey = key; }
  244. S32 getMaxDataBlockModifiedKey () { return mMaxDataBlockModifiedKey; }
  245. void setMaxDataBlockModifiedKey (S32 key) { mMaxDataBlockModifiedKey = key; }
  246. /// Return the datablock sequence number that this game connection is on.
  247. /// The datablock sequence number is synchronized to the mission sequence number
  248. /// on each datablock transmission.
  249. U32 getDataBlockSequence() { return mDataBlockSequence; }
  250. /// Set the datablock sequence number.
  251. void setDataBlockSequence(U32 seq) { mDataBlockSequence = seq; }
  252. /// @}
  253. /// @name Fade control
  254. /// @{
  255. F32 getDamageFlash() const { return mDamageFlash; }
  256. F32 getWhiteOut() const { return mWhiteOut; }
  257. void setBlackOut(bool fadeToBlack, S32 timeMS);
  258. F32 getBlackOut();
  259. /// @}
  260. /// @name Authentication
  261. ///
  262. /// This is remnant code from Tribes 2.
  263. /// @{
  264. void setAuthInfo(const AuthInfo *info);
  265. const AuthInfo *getAuthInfo();
  266. /// @}
  267. /// @name Sound
  268. /// @{
  269. void play2D(SFXProfile *profile);
  270. void play3D(SFXProfile *profile, const MatrixF *transform);
  271. /// @}
  272. /// @name Misc.
  273. /// @{
  274. bool isFirstPerson() const { return mCameraPos == 0; }
  275. bool isAIControlled() { return mAIControlled; }
  276. void doneScopingScene();
  277. void demoPlaybackComplete();
  278. void setMissionCRC(U32 crc) { mMissionCRC = crc; }
  279. U32 getMissionCRC() { return(mMissionCRC); }
  280. /// @}
  281. static Signal<void(F32)> smFovUpdate;
  282. static Signal<void()> smPlayingDemo;
  283. protected:
  284. DECLARE_CALLBACK( void, onConnectionTimedOut, () );
  285. DECLARE_CALLBACK( void, onConnectionAccepted, () );
  286. DECLARE_CALLBACK( void, onConnectRequestTimedOut, () );
  287. DECLARE_CALLBACK( void, onConnectionDropped, (const char* reason) );
  288. DECLARE_CALLBACK( void, onConnectRequestRejected, (const char* reason) );
  289. DECLARE_CALLBACK( void, onConnectionError, (const char* errorString) );
  290. DECLARE_CALLBACK( void, onDrop, (const char* disconnectReason) );
  291. DECLARE_CALLBACK( void, initialControlSet, () );
  292. DECLARE_CALLBACK( void, onControlObjectChange, () );
  293. DECLARE_CALLBACK( void, setLagIcon, (bool state) );
  294. DECLARE_CALLBACK( void, onDataBlocksDone, (U32 sequence) );
  295. DECLARE_CALLBACK( void, onFlash, (bool state) );
  296. };
  297. #endif