Counter Strike : Global Offensive Source Code
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.

410 lines
12 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef CHOREOSCENE_H
  8. #define CHOREOSCENE_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. class CChoreoEvent;
  13. class CChoreoChannel;
  14. class CChoreoActor;
  15. class IChoreoEventCallback;
  16. class CEventRelativeTag;
  17. class CUtlBuffer;
  18. class CFlexAnimationTrack;
  19. class ISceneTokenProcessor;
  20. class IChoreoStringPool;
  21. #include "tier1/utlvector.h"
  22. #include "tier1/utldict.h"
  23. #include "bitvec.h"
  24. #include "expressionsample.h"
  25. #include "choreoevent.h"
  26. #define DEFAULT_SCENE_FPS 60
  27. #define MIN_SCENE_FPS 10
  28. #define MAX_SCENE_FPS 240
  29. #define SCENE_BINARY_TAG MAKEID( 'b', 'v', 'c', 'd' )
  30. #define SCENE_BINARY_VERSION 0x04
  31. //-----------------------------------------------------------------------------
  32. // Purpose: Container for choreographed scene of events for actors
  33. //-----------------------------------------------------------------------------
  34. class CChoreoScene : public ICurveDataAccessor
  35. {
  36. public:
  37. // Construction
  38. CChoreoScene( IChoreoEventCallback *callback );
  39. ~CChoreoScene( void );
  40. // Assignment
  41. CChoreoScene& operator=(const CChoreoScene& src );
  42. // ICurveDataAccessor methods
  43. virtual float GetDuration() { return FindStopTime(); };
  44. virtual bool CurveHasEndTime();
  45. virtual int GetDefaultCurveType();
  46. // Binary serialization
  47. bool SaveBinary( char const *pszBinaryFileName, char const *pPathID, unsigned int nTextVersionCRC, IChoreoStringPool *pStringPool );
  48. void SaveToBinaryBuffer( CUtlBuffer& buf, unsigned int nTextVersionCRC, IChoreoStringPool *pStringPool );
  49. bool RestoreFromBinaryBuffer( CUtlBuffer& buf, char const *filename, IChoreoStringPool *pStringPool );
  50. static bool GetCRCFromBinaryBuffer( CUtlBuffer& buf, unsigned int& crc );
  51. // We do some things differently while restoring from a save.
  52. inline void SetRestoring( bool bRestoring );
  53. inline bool IsRestoring();
  54. enum
  55. {
  56. MAX_SCENE_FILENAME = 128,
  57. };
  58. // Event callback handler
  59. void SetEventCallbackInterface( IChoreoEventCallback *callback );
  60. // Loading
  61. bool ParseFromBuffer( char const *pFilename, ISceneTokenProcessor *tokenizer );
  62. void SetPrintFunc( void ( *pfn )( const char *fmt, ... ) );
  63. // Saving
  64. bool SaveToFile( const char *filename );
  65. bool ExportMarkedToFile( const char *filename );
  66. void MarkForSaveAll( bool mark );
  67. // Merges two .vcd's together, returns true if any data was merged
  68. bool Merge( CChoreoScene *other );
  69. static void FileSaveFlexAnimationTrack( CUtlBuffer& buf, int level, CFlexAnimationTrack *track, int nDefaultCurveType );
  70. static void FileSaveFlexAnimations( CUtlBuffer& buf, int level, CChoreoEvent *e );
  71. static void FileSaveRamp( CUtlBuffer& buf, int level, CChoreoEvent *e );
  72. void FileSaveSceneRamp( CUtlBuffer& buf, int level );
  73. static void FileSaveScaleSettings( CUtlBuffer& buf, int level, CChoreoScene *scene );
  74. static void ParseFlexAnimations( ISceneTokenProcessor *tokenizer, CChoreoEvent *e, bool removeold = true );
  75. static void ParseRamp( ISceneTokenProcessor *tokenizer, CChoreoEvent *e );
  76. static void ParseSceneRamp( ISceneTokenProcessor *tokenizer, CChoreoScene *scene );
  77. static void ParseScaleSettings( ISceneTokenProcessor *tokenizer, CChoreoScene *scene );
  78. static void ParseEdgeInfo( ISceneTokenProcessor *tokenizer, EdgeInfo_t *edgeinfo );
  79. // Debugging
  80. void SceneMsg( PRINTF_FORMAT_STRING const char *pFormat, ... );
  81. void Print( void );
  82. // Sound system needs to have sounds pre-queued by this much time
  83. void SetSoundFileStartupLatency( float time );
  84. float GetSoundFileStartupLatency() const;
  85. // Simulation
  86. void Think( float curtime );
  87. // Retrieves time in simulation
  88. float GetTime( void );
  89. // Retrieves start/stop time for looped/debug scene
  90. void GetSceneTimes( float& start, float& end );
  91. void SetTime( float t );
  92. void LoopToTime( float t );
  93. // Has simulation finished
  94. bool SimulationFinished( void );
  95. // Has the last speech event in the scene already fired
  96. bool SpeechFinished( void ) const;
  97. // Reset simulation
  98. void ResetSimulation( bool forward = true, float starttime = 0.0f, float endtime = 0.0f );
  99. // Find time at which last simulation event is triggered
  100. float FindStopTime( void );
  101. // Find time at which last SPEAK event is complete
  102. float FindLastSpeakTime( void ) const;
  103. void ResumeSimulation( void );
  104. // Have all the pause events happened
  105. bool CheckEventCompletion( void );
  106. // Find named actor in scene data
  107. CChoreoActor *FindActor( const char *name );
  108. // Remove actor from scene
  109. void RemoveActor( CChoreoActor *actor );
  110. // Find index for actor
  111. int FindActorIndex( CChoreoActor *actor );
  112. // Swap actors in the data
  113. void SwapActors( int a1, int a2 );
  114. // General data access
  115. int GetNumEvents( void );
  116. CChoreoEvent *GetEvent( int event );
  117. int GetNumActors( void );
  118. CChoreoActor *GetActor( int actor );
  119. int GetNumChannels( void );
  120. CChoreoChannel *GetChannel( int channel );
  121. // Object allocation/destruction
  122. void DeleteReferencedObjects( CChoreoActor *actor );
  123. void DeleteReferencedObjects( CChoreoChannel *channel );
  124. void DeleteReferencedObjects( CChoreoEvent *event );
  125. CChoreoActor *AllocActor( void );
  126. CChoreoChannel *AllocChannel( void );
  127. CChoreoEvent *AllocEvent( void );
  128. void AddEventToScene( CChoreoEvent *event );
  129. void AddActorToScene( CChoreoActor *actor );
  130. void AddChannelToScene( CChoreoChannel *channel );
  131. // Fixup simulation times for channel gestures
  132. void ReconcileGestureTimes( void );
  133. // Go through all elements and update relative tags, removing any orphaned
  134. // tags and updating the timestamp of normal tags
  135. void ReconcileTags( void );
  136. CEventRelativeTag *FindTagByName( const char *wavname, const char *name );
  137. CChoreoEvent *FindTargetingEvent( const char *wavname, const char *name );
  138. // Used by UI to provide target actor names
  139. char const *GetMapname( void );
  140. void SetMapname( const char *name );
  141. void ExportEvents( const char *filename, CUtlVector< CChoreoEvent * >& events );
  142. void ImportEvents( ISceneTokenProcessor *tokenizer, CChoreoActor *actor, CChoreoChannel *channel );
  143. // Subscene support
  144. void SetSubScene( bool sub );
  145. bool IsSubScene( void ) const;
  146. int GetSceneFPS( void ) const;
  147. void SetSceneFPS( int fps );
  148. bool IsUsingFrameSnap( void ) const;
  149. void SetUsingFrameSnap( bool snap );
  150. float SnapTime( float t );
  151. int GetSceneRampCount( void ) { return m_SceneRamp.GetCount(); };
  152. CExpressionSample *GetSceneRamp( int index ) { return m_SceneRamp.Get( index ); };
  153. CExpressionSample *AddSceneRamp( float time, float value, bool selected ) { return m_SceneRamp.Add( time, value, selected ); };
  154. void DeleteSceneRamp( int index ) { m_SceneRamp.Delete( index ); };
  155. void ClearSceneRamp( void ) { m_SceneRamp.Clear(); };
  156. void ResortSceneRamp( void ) { m_SceneRamp.Resort( this ); };
  157. CCurveData *GetSceneRamp( void ) { return &m_SceneRamp; };
  158. // Global intensity for scene
  159. float GetSceneRampIntensity( float time ) { return m_SceneRamp.GetIntensity( this, time ); }
  160. int GetTimeZoom( char const *tool );
  161. void SetTimeZoom( char const *tool, int tz );
  162. int TimeZoomFirst();
  163. int TimeZoomNext( int i );
  164. int TimeZoomInvalid() const;
  165. char const *TimeZoomName( int i );
  166. void ReconcileCloseCaption();
  167. char const *GetFilename() const;
  168. void SetFileName( char const *fn );
  169. bool GetPlayingSoundName( char *pchBuff, int iBuffLength );
  170. bool HasUnplayedSpeech();
  171. bool HasFlexAnimation();
  172. void SetBackground( bool bIsBackground );
  173. bool IsBackground( void );
  174. void ClearPauseEventDependencies();
  175. bool HasEventsOfType( CChoreoEvent::EVENTTYPE type ) const;
  176. void RemoveEventsExceptTypes( int* typeList, int count );
  177. void IgnorePhonemes( bool bIgnore );
  178. bool ShouldIgnorePhonemes() const;
  179. // This is set by the engine to signify that we're not modifying the data and
  180. // therefore we can precompute the end time
  181. static bool s_bEditingDisabled;
  182. private:
  183. // Simulation stuff
  184. enum
  185. {
  186. IN_RANGE = 0,
  187. BEFORE_RANGE,
  188. AFTER_RANGE
  189. };
  190. int IsTimeInRange( float t, float starttime, float endtime );
  191. typedef enum
  192. {
  193. PROCESSING_TYPE_IGNORE = 0,
  194. PROCESSING_TYPE_START,
  195. PROCESSING_TYPE_START_RESUMECONDITION,
  196. PROCESSING_TYPE_CONTINUE,
  197. PROCESSING_TYPE_STOP,
  198. } PROCESSING_TYPE;
  199. struct ActiveList
  200. {
  201. PROCESSING_TYPE pt;
  202. CChoreoEvent *e;
  203. };
  204. static bool EventLess( const CChoreoScene::ActiveList &al0, const CChoreoScene::ActiveList &al1 );
  205. int EventThink( CChoreoEvent *e,
  206. float frame_start_time,
  207. float frame_end_time,
  208. bool playing_forward, PROCESSING_TYPE& disposition );
  209. // Prints to debug console, etc
  210. void choreoprintf( int level, PRINTF_FORMAT_STRING const char *fmt, ... );
  211. // Initialize scene
  212. void Init( IChoreoEventCallback *callback );
  213. float FindAdjustedStartTime( void );
  214. float FindAdjustedEndTime( void );
  215. CChoreoEvent *FindPauseBetweenTimes( float starttime, float endtime );
  216. // Parse scenes from token buffer
  217. CChoreoEvent *ParseEvent( CChoreoActor *actor, CChoreoChannel *channel );
  218. CChoreoChannel *ParseChannel( CChoreoActor *actor );
  219. CChoreoActor *ParseActor( void );
  220. void ParseFPS( void );
  221. void ParseSnap( void );
  222. void ParseIgnorePhonemes( void );
  223. // Map file for retrieving named objects
  224. void ParseMapname( void );
  225. // When previewing actor in hlfaceposer, this is the model to associate
  226. void ParseFacePoserModel( CChoreoActor *actor );
  227. // Print to printfunc
  228. void PrintEvent( int level, CChoreoEvent *e );
  229. void PrintChannel( int level, CChoreoChannel *c );
  230. void PrintActor( int level, CChoreoActor *a );
  231. // File I/O
  232. public:
  233. static void FilePrintf( CUtlBuffer& buf, int level, PRINTF_FORMAT_STRING const char *fmt, ... );
  234. private:
  235. void FileSaveEvent( CUtlBuffer& buf, int level, CChoreoEvent *e );
  236. void FileSaveChannel( CUtlBuffer& buf, int level, CChoreoChannel *c );
  237. void FileSaveActor( CUtlBuffer& buf, int level, CChoreoActor *a );
  238. void FileSaveHeader( CUtlBuffer& buf );
  239. // Object destruction
  240. void DestroyActor( CChoreoActor *actor );
  241. void DestroyChannel( CChoreoChannel *channel );
  242. void DestroyEvent( CChoreoEvent *event );
  243. void AddPauseEventDependency( CChoreoEvent *pauseEvent, CChoreoEvent *suppressed );
  244. void InternalDetermineEventTypes();
  245. // Global object storage
  246. CUtlVector < CChoreoEvent * > m_Events;
  247. CUtlVector < CChoreoActor * > m_Actors;
  248. CUtlVector < CChoreoChannel * > m_Channels;
  249. // These are just pointers, the actual objects are in m_Events
  250. CUtlVector < CChoreoEvent * > m_ResumeConditions;
  251. // These are just pointers, the actual objects are in m_Events
  252. CUtlVector < CChoreoEvent * > m_ActiveResumeConditions;
  253. // These are just pointers, the actual objects are in m_Events
  254. CUtlVector < CChoreoEvent * > m_PauseEvents;
  255. // Current simulation time
  256. float m_flCurrentTime;
  257. float m_flStartTime;
  258. float m_flEndTime;
  259. bool m_bRecalculateSceneTimes;
  260. float m_flEarliestTime;
  261. float m_flLatestTime;
  262. int m_nActiveEvents;
  263. // Wave file playback needs to issue play commands a bit ahead of time
  264. // in order to hit exact marks
  265. float m_flSoundSystemLatency;
  266. // Scene's linger a bit after finishing to let blends reset themselves
  267. float m_flLastActiveTime;
  268. // Print callback function
  269. void ( *m_pfnPrint )( PRINTF_FORMAT_STRING const char *fmt, ... );
  270. IChoreoEventCallback *m_pIChoreoEventCallback;
  271. ISceneTokenProcessor *m_pTokenizer;
  272. enum
  273. {
  274. MAX_MAPNAME = 128
  275. };
  276. char m_szMapname[ MAX_MAPNAME ];
  277. int m_nSceneFPS;
  278. CCurveData m_SceneRamp;
  279. CUtlDict< int, int > m_TimeZoomLookup;
  280. char m_szFileName[ MAX_SCENE_FILENAME ];
  281. CBitVec< CChoreoEvent::NUM_TYPES > m_bitvecHasEventOfType;
  282. // tag to suppress vcd when others are playing
  283. bool m_bIsBackground : 1;
  284. bool m_bIgnorePhonemes : 1;
  285. bool m_bSubScene : 1;
  286. bool m_bUseFrameSnap : 1;
  287. bool m_bRestoring : 1;
  288. int m_nLastPauseEvent;
  289. // This only gets updated if it's loaded from a buffer which means we're not in an editor
  290. float m_flPrecomputedStopTime;
  291. };
  292. bool CChoreoScene::IsRestoring()
  293. {
  294. return m_bRestoring;
  295. }
  296. void CChoreoScene::SetRestoring( bool bRestoring )
  297. {
  298. m_bRestoring = bRestoring;
  299. }
  300. abstract_class IChoreoStringPool
  301. {
  302. public:
  303. virtual short FindOrAddString( const char *pString ) = 0;
  304. virtual bool GetString( short stringId, char *buff, int buffSize ) = 0;
  305. };
  306. CChoreoScene *ChoreoLoadScene(
  307. char const *filename,
  308. IChoreoEventCallback *callback,
  309. ISceneTokenProcessor *tokenizer,
  310. void ( *pfn ) ( PRINTF_FORMAT_STRING const char *fmt, ... ) );
  311. bool IsBufferBinaryVCD( char *pBuffer, int bufferSize );
  312. #endif // CHOREOSCENE_H