gameConnection.h 12 KB

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