gameConnection.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. 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. private:
  134. /// @name Connection State
  135. /// This data is set with setConnectArgs() and setJoinPassword(), and
  136. /// sent across the wire when we connect.
  137. /// @{
  138. U32 mConnectArgc;
  139. char *mConnectArgv[MaxConnectArgs];
  140. char *mJoinPassword;
  141. /// @}
  142. protected:
  143. struct GamePacketNotify : public NetConnection::PacketNotify
  144. {
  145. S32 cameraFov;
  146. GamePacketNotify();
  147. };
  148. PacketNotify *allocNotify();
  149. bool mControlForceMismatch;
  150. Vector<SimDataBlock *> mDataBlockLoadList;
  151. public:
  152. MoveList *mMoveList;
  153. protected:
  154. bool mAIControlled;
  155. AuthInfo * mAuthInfo;
  156. static S32 mLagThresholdMS;
  157. S32 mLastPacketTime;
  158. bool mLagging;
  159. /// @name Flashing
  160. ////
  161. /// Note, these variables are not networked, they are for the local connection only.
  162. /// @{
  163. F32 mDamageFlash;
  164. F32 mWhiteOut;
  165. F32 mBlackOut;
  166. S32 mBlackOutTimeMS;
  167. S32 mBlackOutStartTimeMS;
  168. bool mFadeToBlack;
  169. /// @}
  170. /// @name Packet I/O
  171. /// @{
  172. void readPacket (BitStream *bstream);
  173. void writePacket (BitStream *bstream, PacketNotify *note);
  174. void packetReceived (PacketNotify *note);
  175. void packetDropped (PacketNotify *note);
  176. void connectionError (const char *errorString);
  177. void writeDemoStartBlock (ResizeBitStream *stream);
  178. bool readDemoStartBlock (BitStream *stream);
  179. void handleRecordedBlock (U32 type, U32 size, void *data);
  180. /// @}
  181. void ghostWriteExtra(NetObject *,BitStream *);
  182. void ghostReadExtra(NetObject *,BitStream *, bool newGhost);
  183. void ghostPreRead(NetObject *, bool newGhost);
  184. virtual void onEndGhosting();
  185. public:
  186. DECLARE_CONOBJECT(GameConnection);
  187. void handleConnectionMessage(U32 message, U32 sequence, U32 ghostCount);
  188. void preloadDataBlock(SimDataBlock *block);
  189. void fileDownloadSegmentComplete();
  190. void preloadNextDataBlock(bool hadNew);
  191. static void consoleInit();
  192. void setDisconnectReason(const char *reason);
  193. GameConnection();
  194. ~GameConnection();
  195. bool onAdd();
  196. void onRemove();
  197. static GameConnection *getConnectionToServer()
  198. {
  199. return dynamic_cast<GameConnection*>((NetConnection *) mServerConnection);
  200. }
  201. static GameConnection *getLocalClientConnection()
  202. {
  203. return dynamic_cast<GameConnection*>((NetConnection *) mLocalClientConnection);
  204. }
  205. /// @name Control object
  206. /// @{
  207. ///
  208. void setControlObject(GameBase *);
  209. GameBase* getControlObject() { return mControlObject; }
  210. const GameBase* getControlObject() const { return mControlObject; }
  211. void setCameraObject(GameBase *);
  212. GameBase* getCameraObject();
  213. bool getControlCameraTransform(F32 dt,MatrixF* mat);
  214. bool getControlCameraVelocity(Point3F *vel);
  215. bool getControlCameraDefaultFov(F32 *fov);
  216. bool getControlCameraFov(F32 *fov);
  217. bool setControlCameraFov(F32 fov);
  218. bool isValidControlCameraFov(F32 fov);
  219. // Used by editor
  220. bool isControlObjectRotDampedCamera();
  221. void setFirstPerson(bool firstPerson);
  222. bool hasDisplayDevice() const { return mDisplayDevice != NULL; }
  223. const IDisplayDevice* getDisplayDevice() const { return mDisplayDevice; }
  224. void setDisplayDevice(IDisplayDevice* display) { mDisplayDevice = display; }
  225. void clearDisplayDevice() { mDisplayDevice = NULL; }
  226. void setControlSchemeParameters(bool absoluteRotation, bool addYawToAbsRot, bool addPitchToAbsRot);
  227. bool getControlSchemeAbsoluteRotation() {return mAbsoluteRotation;}
  228. bool getControlSchemeAddYawToAbsRot() {return mAddYawToAbsRot;}
  229. bool getControlSchemeAddPitchToAbsRot() {return mAddPitchToAbsRot;}
  230. /// @}
  231. void detectLag();
  232. /// @name Datablock management
  233. /// @{
  234. S32 getDataBlockModifiedKey () { return mDataBlockModifiedKey; }
  235. void setDataBlockModifiedKey (S32 key) { mDataBlockModifiedKey = key; }
  236. S32 getMaxDataBlockModifiedKey () { return mMaxDataBlockModifiedKey; }
  237. void setMaxDataBlockModifiedKey (S32 key) { mMaxDataBlockModifiedKey = key; }
  238. /// Return the datablock sequence number that this game connection is on.
  239. /// The datablock sequence number is synchronized to the mission sequence number
  240. /// on each datablock transmission.
  241. U32 getDataBlockSequence() { return mDataBlockSequence; }
  242. /// Set the datablock sequence number.
  243. void setDataBlockSequence(U32 seq) { mDataBlockSequence = seq; }
  244. /// @}
  245. /// @name Fade control
  246. /// @{
  247. F32 getDamageFlash() const { return mDamageFlash; }
  248. F32 getWhiteOut() const { return mWhiteOut; }
  249. void setBlackOut(bool fadeToBlack, S32 timeMS);
  250. F32 getBlackOut();
  251. /// @}
  252. /// @name Authentication
  253. ///
  254. /// This is remnant code from Tribes 2.
  255. /// @{
  256. void setAuthInfo(const AuthInfo *info);
  257. const AuthInfo *getAuthInfo();
  258. /// @}
  259. /// @name Sound
  260. /// @{
  261. void play2D(SFXProfile *profile);
  262. void play3D(SFXProfile *profile, const MatrixF *transform);
  263. /// @}
  264. /// @name Misc.
  265. /// @{
  266. bool isFirstPerson() const { return mCameraPos == 0; }
  267. bool isAIControlled() { return mAIControlled; }
  268. void doneScopingScene();
  269. void demoPlaybackComplete();
  270. void setMissionCRC(U32 crc) { mMissionCRC = crc; }
  271. U32 getMissionCRC() { return(mMissionCRC); }
  272. /// @}
  273. static Signal<void(F32)> smFovUpdate;
  274. static Signal<void()> smPlayingDemo;
  275. protected:
  276. DECLARE_CALLBACK( void, onConnectionTimedOut, () );
  277. DECLARE_CALLBACK( void, onConnectionAccepted, () );
  278. DECLARE_CALLBACK( void, onConnectRequestTimedOut, () );
  279. DECLARE_CALLBACK( void, onConnectionDropped, (const char* reason) );
  280. DECLARE_CALLBACK( void, onConnectRequestRejected, (const char* reason) );
  281. DECLARE_CALLBACK( void, onConnectionError, (const char* errorString) );
  282. DECLARE_CALLBACK( void, onDrop, (const char* disconnectReason) );
  283. DECLARE_CALLBACK( void, initialControlSet, () );
  284. DECLARE_CALLBACK( void, onControlObjectChange, () );
  285. DECLARE_CALLBACK( void, setLagIcon, (bool state) );
  286. DECLARE_CALLBACK( void, onDataBlocksDone, (U32 sequence) );
  287. DECLARE_CALLBACK( void, onFlash, (bool state) );
  288. };
  289. #endif