SessionState.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* Copyright (c) 2002-2012 Croteam Ltd.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of version 2 of the GNU General Public License as published by
  4. the Free Software Foundation
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along
  10. with this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  12. #ifndef SE_INCL_SESSIONSTATE_H
  13. #define SE_INCL_SESSIONSTATE_H
  14. #ifdef PRAGMA_ONCE
  15. #pragma once
  16. #endif
  17. #include <Engine/Base/Synchronization.h>
  18. #include <Engine/Templates/StaticStackArray.h>
  19. #include <Engine/Network/NetworkMessage.h>
  20. #include <Engine/Network/PlayerTarget.h>
  21. #include <Engine/Network/SessionSocket.h>
  22. #include <Engine/Base/Timer.h>
  23. #define DEBUG_SYNCSTREAMDUMPING 0
  24. #if DEBUG_SYNCSTREAMDUMPING
  25. /*
  26. * Obtain valid session dump memory stream
  27. */
  28. CTMemoryStream *GetDumpStream(void);
  29. /*
  30. * Clear session dump memory stream
  31. */
  32. void ClearDumpStream(void);
  33. #endif
  34. // checksum of world snapshot at given point in time - used for sync-checking
  35. class CSyncCheck {
  36. public:
  37. TIME sc_tmTick; // time of snapshot
  38. INDEX sc_iSequence; // sequence number last processed before this checksum
  39. ULONG sc_ulCRC; // checksum
  40. INDEX sc_iLevel; // checksum of level filename
  41. CSyncCheck(void) { sc_tmTick = -1.0f; sc_iSequence = -1; sc_ulCRC = 0; sc_iLevel = 0; }
  42. void Clear(void) { sc_tmTick = -1.0f; sc_iSequence = -1; sc_ulCRC = 0; sc_iLevel = 0; }
  43. };
  44. // info about an event that was predicted to happen
  45. class CPredictedEvent {
  46. public:
  47. TIME pe_tmTick;
  48. ULONG pe_ulEntityID;
  49. ULONG pe_ulTypeID;
  50. ULONG pe_ulEventID;
  51. CPredictedEvent(void);
  52. void Clear(void) {};
  53. };
  54. /*
  55. * Session state, manipulates local copy of the world
  56. */
  57. class ENGINE_API CSessionState {
  58. public:
  59. CStaticArray<CPlayerTarget> ses_apltPlayers; // client targets for all players in game
  60. CStaticStackArray<CPredictedEvent> ses_apeEvents; // for event prediction
  61. CTString ses_strMOTD; // MOTD as sent from the server
  62. INDEX ses_iLevel; // for counting level changes
  63. INDEX ses_iLastProcessedSequence; // sequence of last processed stream block
  64. CNetworkStream ses_nsGameStream; // stream of blocks from server
  65. // lerp params
  66. CTimerValue ses_tvInitialization; // exact moment when the session state was started
  67. TIME ses_tmInitializationTick; // tick when the session state was started
  68. // secondary lerp params for non-predicted movement
  69. CTimerValue ses_tvInitialization2; // exact moment when the session state was started
  70. TIME ses_tmInitializationTick2; // tick when the session state was started
  71. TIME ses_tmLastProcessedTick; // last tick when all actions were processed
  72. TIME ses_tmPredictionHeadTick; // newest tick that was ever predicted
  73. TIME ses_tmLastSyncCheck; // last time sync-check was generated
  74. TIME ses_tmLastPredictionProcessed; // for determining when to do a new prediction cycle
  75. INDEX ses_iMissingSequence; // first missing sequence
  76. CTimerValue ses_tvResendTime; // timer for missing sequence retransmission
  77. TIME ses_tmResendTimeout; // timeout value for increasing the request interval
  78. CTimerValue ses_tvMessageReceived; // exact moment when the session state was started
  79. TIME ses_tmLastDemoSequence; // synchronization timer for demo playing
  80. ULONG ses_ulRandomSeed; // seed for pseudo-random number generation
  81. ULONG ses_ulSpawnFlags; // spawn flags for current game
  82. TIME ses_tmSyncCheckFrequency; // frequency of sync-checking
  83. BOOL ses_iExtensiveSyncCheck; // set if syncheck should be extensive - for debugging purposes
  84. BOOL ses_bKeepingUpWithTime; // set if the session state is keeping up with the time
  85. TIME ses_tmLastUpdated;
  86. CListHead ses_lhRememberedLevels; // list of remembered levels
  87. BOOL ses_bAllowRandom; // set while random number generation is valid
  88. BOOL ses_bPredicting; // set if the game is currently doing prediction
  89. BOOL ses_bPause; // set while game is paused
  90. BOOL ses_bWantPause; // set while wanting to have paused
  91. BOOL ses_bGameFinished; // set when game has finished
  92. BOOL ses_bWaitingForServer; // wait for server after level change
  93. CTString ses_strDisconnected; // explanation of disconnection or empty string if not disconnected
  94. INDEX ses_ctMaxPlayers; // maximum number of players allowed in game
  95. BOOL ses_bWaitAllPlayers; // if set, wait for all players to join before starting
  96. FLOAT ses_fRealTimeFactor; // enables slower or faster time for special effects
  97. CTMemoryStream *ses_pstrm; // debug stream for sync check examination
  98. CSessionSocketParams ses_sspParams; // local copy of server-side parameters
  99. public:
  100. // network message waiters
  101. void Start_AtServer_t(void); // throw char *
  102. void Start_AtClient_t(INDEX ctLocalPlayers); // throw char *
  103. // Set lerping factor for current frame.
  104. void SetLerpFactor(CTimerValue tvNow);
  105. // notify entities of level change
  106. void SendLevelChangeNotification(class CEntityEvent &ee);
  107. // wait for a stream to come from server
  108. void WaitStream_t(CTMemoryStream &strmMessage, const CTString &strName, INDEX iMsgCode);
  109. // check if disconnected
  110. BOOL IsDisconnected(void);
  111. // print an incoming chat message to console
  112. void PrintChatMessage(ULONG ulFrom, const CTString &strFrom, const CTString &strMessage);
  113. public:
  114. /* Constructor. */
  115. CSessionState(void);
  116. /* Destructor. */
  117. ~CSessionState(void);
  118. // get a pseudo-random number (safe for network gaming)
  119. ULONG Rnd(void);
  120. /* Stop the session state. */
  121. void Stop(void);
  122. /* Start session state. */
  123. void Start_t(INDEX ctLocalPlayers); // throw char *
  124. // do physics for a game tick
  125. void HandleMovers(void);
  126. // do thinking for a game tick
  127. void HandleTimers(TIME tmCurrentTick);
  128. // do a warm-up run of the world for a few ticks
  129. void WarmUpWorld(void);
  130. // reset random number generator (always randomizes to same sequence!)
  131. void ResetRND(void);
  132. /* Process a game tick. */
  133. void ProcessGameTick(CNetworkMessage &nmMessage, TIME tmCurrentTick);
  134. /* Process a predicted game tick. */
  135. void ProcessPredictedGameTick(INDEX iPredictionStep, FLOAT fFactor, TIME tmCurrentTick);
  136. /* Process a gamestream block. */
  137. void ProcessGameStreamBlock(CNetworkMessage &nmMessage);
  138. /* Process all eventual avaliable gamestream blocks. */
  139. void ProcessGameStream(void);
  140. // flush prediction actions that were already processed
  141. void FlushProcessedPredictions(void);
  142. /* Find out how many prediction steps are currently pending. */
  143. INDEX GetPredictionStepsCount(void);
  144. /* Process all eventual avaliable prediction actions. */
  145. void ProcessPrediction(void);
  146. /* Get number of active players. */
  147. INDEX GetPlayersCount(void);
  148. /* Remember predictor positions of all players. */
  149. void RememberPlayerPredictorPositions(void);
  150. /* Get player position. */
  151. const FLOAT3D &GetPlayerPredictorPosition(INDEX iPlayer);
  152. // check an event for prediction, returns true if already predicted
  153. BOOL CheckEventPrediction(CEntity *pen, ULONG ulTypeID, ULONG ulEventID);
  154. // make synchronization test message and send it to server (if client), or add to buffer (if server)
  155. void MakeSynchronisationCheck(void);
  156. // create a checksum value for sync-check
  157. void ChecksumForSync(ULONG &ulCRC, INDEX iExtensiveSyncCheck);
  158. // dump sync data to text file
  159. void DumpSync_t(CTStream &strm, INDEX iExtensiveSyncCheck); // throw char *
  160. /* Read session state information from a stream. */
  161. void Read_t(CTStream *pstr); // throw char *
  162. void ReadWorldAndState_t(CTStream *pstr); // throw char *
  163. void ReadRememberedLevels_t(CTStream *pstr); // throw char *
  164. /* Write session state information into a stream. */
  165. void Write_t(CTStream *pstr); // throw char *
  166. void WriteWorldAndState_t(CTStream *pstr); // throw char *
  167. void WriteRememberedLevels_t(CTStream *pstr); // throw char *
  168. // remember current level
  169. void RememberCurrentLevel(const CTString &strFileName);
  170. // find a level if it is remembered
  171. class CRememberedLevel *FindRememberedLevel(const CTString &strFileName);
  172. // restore some old level
  173. void RestoreOldLevel(const CTString &strFileName);
  174. // forget all remembered levels
  175. void ForgetOldLevels(void);
  176. /* Session state loop. */
  177. void SessionStateLoop(void);
  178. /* Session sync dump functions. */
  179. void DumpSyncToFile_t(CTStream &strm, INDEX iExtensiveSyncCheck); // throw char *
  180. #if DEBUG_SYNCSTREAMDUMPING
  181. void DumpSyncToMemory(void);
  182. #endif
  183. };
  184. #endif /* include-once check. */