gameConnection.h 15 KB

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