Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
5.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef CL_DEMO_H
  7. #define CL_DEMO_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "demofile.h"
  12. #include "cl_demoactionmanager.h"
  13. struct DemoCommandQueue
  14. {
  15. DemoCommandQueue()
  16. {
  17. tick = 0;
  18. }
  19. int tick;
  20. democmdinfo_t info;
  21. int filepos;
  22. };
  23. // When skipping forward through a replay it is important to stop
  24. // occasionally and process the backlog of packets. Otherwise if you
  25. // skip forward too far you will cause data structures to grow far
  26. // beyond their intended size. This can lead to overflows and out-of-memory
  27. // errors, and it can waste memory because some of these data structures
  28. // never release their memory after hitting a high-water mark.
  29. const unsigned nMaxConsecutiveSkipPackets = 100;
  30. class CDemoPlayer : public IDemoPlayer
  31. {
  32. public: // IDemoPlayer interface implementation:
  33. CDemoPlayer();
  34. ~CDemoPlayer();
  35. virtual CDemoFile *GetDemoFile();
  36. virtual bool StartPlayback( const char *filename, bool bAsTimeDemo );
  37. virtual void PausePlayback( float seconds );
  38. virtual void SkipToTick( int tick, bool bRelative, bool bPause );
  39. virtual void SetEndTick( int tick );
  40. virtual void ResumePlayback( void );
  41. virtual void StopPlayback( void );
  42. virtual int GetPlaybackStartTick( void );
  43. virtual int GetPlaybackTick( void );
  44. virtual float GetPlaybackTimeScale( void );
  45. virtual int GetTotalTicks( void );
  46. virtual bool IsPlayingBack( void );
  47. virtual bool IsPlaybackPaused( void );
  48. virtual bool IsPlayingTimeDemo( void );
  49. virtual bool IsSkipping( void );
  50. virtual bool CanSkipBackwards( void ) { return false; }
  51. virtual void SetPlaybackTimeScale( float timescale );
  52. virtual void InterpolateViewpoint(); // override viewpoint
  53. virtual netpacket_t *ReadPacket( void );
  54. virtual void ResetDemoInterpolation( void );
  55. virtual int GetProtocolVersion();
  56. virtual bool ShouldLoopDemos() { return true; }
  57. virtual void OnLastDemoInLoopPlayed() {}
  58. virtual bool IsLoading( void );
  59. public: // other public functions
  60. void MarkFrame( float flFPSVariability );
  61. void SetBenchframe( int tick, const char *filename );
  62. void ResyncDemoClock( void );
  63. bool CheckPausedPlayback( void );
  64. void WriteTimeDemoResults( void );
  65. bool ParseAheadForInterval( int curtick, int intervalticks );
  66. void InterpolateDemoCommand( int targettick, DemoCommandQueue& prev, DemoCommandQueue& next );
  67. protected:
  68. bool OverrideView( democmdinfo_t& info );
  69. virtual void OnStopCommand();
  70. public:
  71. CDemoFile m_DemoFile;
  72. int m_nStartTick; // For synchronizing playback during timedemo.
  73. int m_nPreviousTick;
  74. netpacket_t m_DemoPacket; // last read demo packet
  75. bool m_bPlayingBack; // true if demo playback
  76. bool m_bPlaybackPaused; // true if demo is paused right now
  77. float m_flAutoResumeTime; // how long do we pause demo playback
  78. float m_flPlaybackRateModifier;
  79. int m_nSkipToTick; // skip to tick ASAP, -1 = off
  80. int m_nEndTick; // if nonzero, stop playback once we reach this tick
  81. bool m_bLoading; // true if demo is loading
  82. unsigned m_nSkipPacketsPlayed; // Track consecutive skip packets returned to avoid excess
  83. // view origin/angle interpolation:
  84. CUtlVector< DemoCommandQueue > m_DestCmdInfo;
  85. democmdinfo_t m_LastCmdInfo;
  86. bool m_bInterpolateView;
  87. bool m_bResetInterpolation;
  88. // timedemo stuff:
  89. bool m_bTimeDemo; // ture if in timedemo mode
  90. int m_nTimeDemoStartFrame; // host_tickcount at start
  91. double m_flTimeDemoStartTime; // Sys_FloatTime() at second frame of timedemo
  92. float m_flTotalFPSVariability; // Frame rate variability
  93. int m_nTimeDemoCurrentFrame; // last frame we read a packet
  94. // benchframe stuff
  95. int m_nSnapshotTick;
  96. char m_SnapshotFilename[MAX_OSPATH];
  97. };
  98. class CDemoRecorder : public IDemoRecorder
  99. {
  100. public:
  101. ~CDemoRecorder();
  102. CDemoRecorder();
  103. CDemoFile *GetDemoFile( void );
  104. int GetRecordingTick( void );
  105. void StartRecording( const char *filename, bool bContinuously );
  106. void SetSignonState( int state );
  107. bool IsRecording( void );
  108. void PauseRecording( void );
  109. void ResumeRecording( void );
  110. void StopRecording( void );
  111. void RecordCommand( const char *cmdstring ); // record a console command
  112. void RecordUserInput( int cmdnumber ); // record a user input command
  113. void RecordMessages( bf_read &data, int bits ); // add messages to current packet
  114. void RecordPacket( void ); // packet finished, write all recorded stuff to file
  115. void RecordServerClasses( ServerClass *pClasses ); // packet finished, write all recorded stuff to file
  116. void RecordStringTables();
  117. void ResetDemoInterpolation( void );
  118. protected:
  119. void ResyncDemoClock( void );
  120. void StartupDemoFile( void );
  121. void StartupDemoHeader( void );
  122. void CloseDemoFile( void );
  123. void GetClientCmdInfo( democmdinfo_t& info );
  124. void WriteDemoCvars( void );
  125. void WriteBSPDecals( void );
  126. void WriteMessages( bf_write &message );
  127. bool ComputeNextIncrementalDemoFilename( char *name, int namesize );
  128. public:
  129. CDemoFile m_DemoFile;
  130. // For synchronizing playback during timedemo.
  131. int m_nStartTick; // host_tickcount when starting recoring
  132. // Name of demo file we are appending onto.
  133. char m_szDemoBaseName[ MAX_OSPATH ];
  134. // For demo file handle
  135. bool m_bIsDemoHeader; // true, if m_hDemoFile is the header file
  136. bool m_bCloseDemoFile; // if true, demo file will be closed ASAP
  137. bool m_bRecording; // true if recording
  138. bool m_bContinuously; // start new record after each
  139. int m_nDemoNumber; // demo count, increases each changelevel
  140. int m_nFrameCount; // # of demo frames in this segment.
  141. bf_write m_MessageData; // temp buffer for all network messages
  142. bool m_bResetInterpolation;
  143. };
  144. extern CDemoRecorder *g_pClientDemoRecorder;
  145. inline CDemoPlayer *ToClientDemoPlayer( IDemoPlayer *pDemoPlayer )
  146. {
  147. return static_cast< CDemoPlayer * >( pDemoPlayer );
  148. }
  149. #endif // CL_DEMO_H