gameConnection.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #ifndef _GAMECONNECTION_H_
  27. #define _GAMECONNECTION_H_
  28. #ifndef _SIMBASE_H_
  29. #include "console/simBase.h"
  30. #endif
  31. #ifndef _GAMEBASE_H_
  32. #include "T3D/gameBase/gameBase.h"
  33. #endif
  34. #ifndef _NETCONNECTION_H_
  35. #include "sim/netConnection.h"
  36. #endif
  37. #ifndef _MOVEMANAGER_H_
  38. #include "T3D/gameBase/moveManager.h"
  39. #endif
  40. #ifndef _BITVECTOR_H_
  41. #include "core/bitVector.h"
  42. #endif
  43. enum GameConnectionConstants
  44. {
  45. MaxClients = 126,
  46. DataBlockQueueCount = 16
  47. };
  48. class IDisplayDevice;
  49. class SFXProfile;
  50. #ifndef USE_TEMPLATE_MATRIX
  51. class MatrixF;
  52. #else
  53. template<typename DATA_TYPE, U32 rows, U32 cols> class Matrix;
  54. typedef Matrix<F32, 4, 4> MatrixF;
  55. #endif
  56. class Point3F;
  57. class MoveManager;
  58. class MoveList;
  59. struct Move;
  60. struct AuthInfo;
  61. // To disable datablock caching, remove or comment out the AFX_CAP_DATABLOCK_CACHE define below.
  62. // Also, at a minimum, the following script preferences should be set to false:
  63. // $pref::Client::EnableDatablockCache = false; (in arcane.fx/client/defaults.tscript)
  64. // $Pref::Server::EnableDatablockCache = false; (in arcane.fx/server/defaults.tscript)
  65. // Alternatively, all script code marked with "DATABLOCK CACHE CODE" can be removed or
  66. // commented out.
  67. //
  68. #define AFX_CAP_DATABLOCK_CACHE
  69. const F32 MinCameraFov = 1.f; ///< min camera FOV
  70. const F32 MaxCameraFov = 179.f; ///< max camera FOV
  71. class GameConnection : public NetConnection
  72. {
  73. private:
  74. typedef NetConnection Parent;
  75. SimObjectPtr<GameBase> mControlObject;
  76. SimObjectPtr<GameBase> mCameraObject;
  77. U32 mDataBlockSequence;
  78. char mDisconnectReason[256];
  79. U32 mMissionCRC; // crc of the current mission file from the server
  80. F32 mVisibleGhostDistance;
  81. private:
  82. U32 mLastControlRequestTime;
  83. S32 mDataBlockModifiedKey;
  84. S32 mMaxDataBlockModifiedKey;
  85. /// @name Client side first/third person
  86. /// @{
  87. ///
  88. bool mFirstPerson; ///< Are we currently first person or not.
  89. bool mUpdateFirstPerson; ///< Set to notify client or server of first person change.
  90. bool mUpdateCameraFov; ///< Set to notify server of camera FOV change.
  91. F32 mCameraFov; ///< Current camera fov (in degrees).
  92. F32 mCameraPos; ///< Current camera pos (0-1).
  93. F32 mCameraSpeed; ///< Camera in/out speed.
  94. IDisplayDevice* mDisplayDevice; ///< Optional client display device that imposes rendering properties.
  95. /// @}
  96. /// @name Client side control scheme that may be referenced by control objects
  97. /// @{
  98. bool mUpdateControlScheme; ///< Set to notify client or server of control scheme change
  99. bool mAbsoluteRotation; ///< Use absolute rotation values from client, likely through ExtendedMove
  100. bool mAddYawToAbsRot; ///< Add relative yaw control to the absolute rotation calculation. Only useful with mAbsoluteRotation.
  101. bool mAddPitchToAbsRot; ///< Add relative pitch control to the absolute rotation calculation. Only useful with mAbsoluteRotation.
  102. /// @}
  103. public:
  104. /// @name Protocol Versions
  105. ///
  106. /// Protocol versions are used to indicated changes in network traffic.
  107. /// These could be changes in how any object transmits or processes
  108. /// network information. You can specify backwards compatibility by
  109. /// specifying a MinRequireProtocolVersion. If the client
  110. /// protocol is >= this min value, the connection is accepted.
  111. ///
  112. /// Torque (V12) SDK 1.0 uses protocol = 1
  113. ///
  114. /// Torque SDK 1.1 uses protocol = 2
  115. /// Torque SDK 1.4 uses protocol = 12
  116. /// @{
  117. static const U32 CurrentProtocolVersion;
  118. static const U32 MinRequiredProtocolVersion;
  119. /// @}
  120. /// Configuration
  121. enum Constants {
  122. BlockTypeMove = NetConnectionBlockTypeCount,
  123. GameConnectionBlockTypeCount,
  124. MaxConnectArgs = 16,
  125. DataBlocksDone = NumConnectionMessages,
  126. DataBlocksDownloadDone,
  127. };
  128. /// Set connection arguments; these are passed to the server when we connect.
  129. void setConnectArgs(U32 argc, const char **argv);
  130. /// Set the server password to use when we join.
  131. void setJoinPassword(const char *password);
  132. /// @name Event Handling
  133. /// @{
  134. void onTimedOut() override;
  135. void onConnectTimedOut() override;
  136. void onDisconnect(const char *reason) override;
  137. void onConnectionRejected(const char *reason) override;
  138. void onConnectionEstablished(bool isInitiator) override;
  139. void handleStartupError(const char *errorString) override;
  140. /// @}
  141. /// @name Packet I/O
  142. /// @{
  143. void writeConnectRequest(BitStream *stream) override;
  144. bool readConnectRequest(BitStream *stream, const char **errorString) override;
  145. void writeConnectAccept(BitStream *stream) override;
  146. bool readConnectAccept(BitStream *stream, const char **errorString) override;
  147. /// @}
  148. bool canRemoteCreate() override;
  149. void setVisibleGhostDistance(F32 dist);
  150. F32 getVisibleGhostDistance();
  151. private:
  152. /// @name Connection State
  153. /// This data is set with setConnectArgs() and setJoinPassword(), and
  154. /// sent across the wire when we connect.
  155. /// @{
  156. U32 mConnectArgc;
  157. char *mConnectArgv[MaxConnectArgs];
  158. char *mJoinPassword;
  159. /// @}
  160. protected:
  161. struct GamePacketNotify : public NetConnection::PacketNotify
  162. {
  163. S32 cameraFov;
  164. GamePacketNotify();
  165. };
  166. PacketNotify *allocNotify() override;
  167. bool mControlForceMismatch;
  168. Vector<SimDataBlock *> mDataBlockLoadList;
  169. public:
  170. MoveList *mMoveList;
  171. protected:
  172. bool mAIControlled;
  173. AuthInfo * mAuthInfo;
  174. static S32 mLagThresholdMS;
  175. S32 mLastPacketTime;
  176. bool mLagging;
  177. /// @name Flashing
  178. ////
  179. /// Note, these variables are not networked, they are for the local connection only.
  180. /// @{
  181. F32 mDamageFlash;
  182. F32 mWhiteOut;
  183. F32 mBlackOut;
  184. S32 mBlackOutTimeMS;
  185. S32 mBlackOutStartTimeMS;
  186. bool mFadeToBlack;
  187. /// @}
  188. /// @name Packet I/O
  189. /// @{
  190. void readPacket (BitStream *bstream) override;
  191. void writePacket (BitStream *bstream, PacketNotify *note) override;
  192. void packetReceived (PacketNotify *note) override;
  193. void packetDropped (PacketNotify *note) override;
  194. void connectionError (const char *errorString) override;
  195. void writeDemoStartBlock (ResizeBitStream *stream) override;
  196. bool readDemoStartBlock (BitStream *stream) override;
  197. void handleRecordedBlock (U32 type, U32 size, void *data) override;
  198. /// @}
  199. void ghostWriteExtra(NetObject *,BitStream *) override;
  200. void ghostReadExtra(NetObject *,BitStream *, bool newGhost) override;
  201. void ghostPreRead(NetObject *, bool newGhost) override;
  202. void onEndGhosting() override;
  203. public:
  204. DECLARE_CONOBJECT(GameConnection);
  205. void handleConnectionMessage(U32 message, U32 sequence, U32 ghostCount) override;
  206. void preloadDataBlock(SimDataBlock *block);
  207. void fileDownloadSegmentComplete() override;
  208. void preloadNextDataBlock(bool hadNew);
  209. static void consoleInit();
  210. void setDisconnectReason(const char *reason);
  211. GameConnection();
  212. ~GameConnection();
  213. bool onAdd() override;
  214. void onRemove() override;
  215. static GameConnection *getConnectionToServer()
  216. {
  217. return dynamic_cast<GameConnection*>((NetConnection *) mServerConnection);
  218. }
  219. static GameConnection *getLocalClientConnection()
  220. {
  221. return dynamic_cast<GameConnection*>((NetConnection *) mLocalClientConnection);
  222. }
  223. /// @name Control object
  224. /// @{
  225. ///
  226. void setControlObject(GameBase *);
  227. GameBase* getControlObject() { return mControlObject; }
  228. const GameBase* getControlObject() const { return mControlObject; }
  229. void setCameraObject(GameBase *);
  230. GameBase* getCameraObject();
  231. bool getControlCameraTransform(F32 dt,MatrixF* mat);
  232. bool getControlCameraVelocity(Point3F *vel);
  233. /// Returns the head transform for the control object, using supplemental information
  234. /// from the provided IDisplayDevice
  235. bool getControlCameraHeadTransform(IDisplayDevice *display, MatrixF *transform);
  236. /// Returns the eye transforms for the control object, using supplemental information
  237. /// from the provided IDisplayDevice.
  238. bool getControlCameraEyeTransforms(IDisplayDevice *display, MatrixF *transforms);
  239. bool getControlCameraDefaultFov(F32 *fov);
  240. bool getControlCameraFov(F32 *fov);
  241. bool setControlCameraFov(F32 fov);
  242. bool isValidControlCameraFov(F32 fov);
  243. // Used by editor
  244. bool isControlObjectRotDampedCamera();
  245. void setFirstPerson(bool firstPerson);
  246. bool hasDisplayDevice() const { return mDisplayDevice != NULL; }
  247. IDisplayDevice* getDisplayDevice() const { return mDisplayDevice; }
  248. void setDisplayDevice(IDisplayDevice* display) { if (mDisplayDevice) mDisplayDevice->setDrawCanvas(NULL); mDisplayDevice = display; }
  249. void clearDisplayDevice() { mDisplayDevice = NULL; }
  250. void setControlSchemeParameters(bool absoluteRotation, bool addYawToAbsRot, bool addPitchToAbsRot);
  251. bool getControlSchemeAbsoluteRotation() {return mAbsoluteRotation;}
  252. bool getControlSchemeAddYawToAbsRot() {return mAddYawToAbsRot;}
  253. bool getControlSchemeAddPitchToAbsRot() {return mAddPitchToAbsRot;}
  254. /// @}
  255. void detectLag();
  256. /// @name Datablock management
  257. /// @{
  258. S32 getDataBlockModifiedKey () { return mDataBlockModifiedKey; }
  259. void setDataBlockModifiedKey (S32 key) { mDataBlockModifiedKey = key; }
  260. S32 getMaxDataBlockModifiedKey () { return mMaxDataBlockModifiedKey; }
  261. void setMaxDataBlockModifiedKey (S32 key) { mMaxDataBlockModifiedKey = key; }
  262. /// Return the datablock sequence number that this game connection is on.
  263. /// The datablock sequence number is synchronized to the mission sequence number
  264. /// on each datablock transmission.
  265. U32 getDataBlockSequence() { return mDataBlockSequence; }
  266. /// Set the datablock sequence number.
  267. void setDataBlockSequence(U32 seq) { mDataBlockSequence = seq; }
  268. /// @}
  269. /// @name Fade control
  270. /// @{
  271. F32 getDamageFlash() const { return mDamageFlash; }
  272. F32 getWhiteOut() const { return mWhiteOut; }
  273. void setBlackOut(bool fadeToBlack, S32 timeMS);
  274. F32 getBlackOut();
  275. /// @}
  276. /// @name Authentication
  277. ///
  278. /// This is remnant code from Tribes 2.
  279. /// @{
  280. void setAuthInfo(const AuthInfo *info);
  281. const AuthInfo *getAuthInfo();
  282. /// @}
  283. /// @name Sound
  284. /// @{
  285. void play2D(StringTableEntry assetId);
  286. void play3D(StringTableEntry assetId, const MatrixF *transform);
  287. /// @}
  288. /// @name Misc.
  289. /// @{
  290. bool isFirstPerson() const { return mCameraPos == 0; }
  291. bool isAIControlled() { return mAIControlled; }
  292. void doneScopingScene() override;
  293. void demoPlaybackComplete() override;
  294. void setMissionCRC(U32 crc) { mMissionCRC = crc; }
  295. U32 getMissionCRC() { return(mMissionCRC); }
  296. /// @}
  297. static Signal<void(F32)> smFovUpdate;
  298. static Signal<void()> smPlayingDemo;
  299. protected:
  300. DECLARE_CALLBACK( void, onConnectionTimedOut, () );
  301. DECLARE_CALLBACK( void, onConnectionAccepted, () );
  302. DECLARE_CALLBACK( void, onConnectRequestTimedOut, () );
  303. DECLARE_CALLBACK( void, onConnectionDropped, (const char* reason) );
  304. DECLARE_CALLBACK( void, onConnectRequestRejected, (const char* reason) );
  305. DECLARE_CALLBACK( void, onConnectionError, (const char* errorString) );
  306. DECLARE_CALLBACK( void, onDrop, (const char* disconnectReason) );
  307. DECLARE_CALLBACK( void, initialControlSet, () );
  308. DECLARE_CALLBACK( void, onControlObjectChange, () );
  309. DECLARE_CALLBACK( void, setLagIcon, (bool state) );
  310. DECLARE_CALLBACK( void, onDataBlocksDone, (U32 sequence) );
  311. DECLARE_CALLBACK( void, onFlash, (bool state) );
  312. #ifdef TORQUE_AFX_ENABLED
  313. // GameConnection is modified to keep track of object selections which are used in
  314. // spell targeting. This code stores the current object selection as well as the
  315. // current rollover object beneath the cursor. The rollover object is treated as a
  316. // pending object selection and actual object selection is usually made by promoting
  317. // the rollover object to the current object selection.
  318. private:
  319. SimObjectPtr<SceneObject> mRolloverObj;
  320. SimObjectPtr<SceneObject> mPreSelectedObj;
  321. SimObjectPtr<SceneObject> mSelectedObj;
  322. bool mChangedSelectedObj;
  323. U32 mPreSelectTimestamp;
  324. protected:
  325. void onDeleteNotify(SimObject*) override;
  326. public:
  327. void setRolloverObj(SceneObject*);
  328. SceneObject* getRolloverObj() { return mRolloverObj; }
  329. void setSelectedObj(SceneObject*, bool propagate_to_client=false);
  330. SceneObject* getSelectedObj() { return mSelectedObj; }
  331. void setPreSelectedObjFromRollover();
  332. void clearPreSelectedObj();
  333. void setSelectedObjFromPreSelected();
  334. // Flag is added to indicate when a client is fully connected or "zoned-in".
  335. // This information determines when AFX will startup active effects on a newly
  336. // added client.
  337. private:
  338. bool zoned_in;
  339. public:
  340. bool isZonedIn() const { return zoned_in; }
  341. void setZonedIn() { zoned_in = true; }
  342. #endif
  343. #ifdef AFX_CAP_DATABLOCK_CACHE
  344. private:
  345. static StringTableEntry server_cache_filename;
  346. static StringTableEntry client_cache_filename;
  347. static bool server_cache_on;
  348. static bool client_cache_on;
  349. BitStream* client_db_stream;
  350. U32 server_cache_CRC;
  351. public:
  352. void repackClientDatablock(BitStream*, S32 start_pos);
  353. void saveDatablockCache(bool on_server);
  354. void loadDatablockCache();
  355. bool loadDatablockCache_Begin();
  356. bool loadDatablockCache_Continue();
  357. void tempDisableStringBuffering(BitStream* bs) const;
  358. void restoreStringBuffering(BitStream* bs) const;
  359. void setServerCacheCRC(U32 crc) { server_cache_CRC = crc; }
  360. static void resetDatablockCache();
  361. static bool serverCacheEnabled() { return server_cache_on; }
  362. static bool clientCacheEnabled() { return client_cache_on; }
  363. static const char* serverCacheFilename() { return server_cache_filename; }
  364. static const char* clientCacheFilename() { return client_cache_filename; }
  365. #endif
  366. };
  367. #endif