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.

408 lines
12 KiB

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