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.

6159 lines
170 KiB

  1. //========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include <stdarg.h>
  8. #include "baseflex.h"
  9. #include "entitylist.h"
  10. #include "choreoevent.h"
  11. #include "choreoactor.h"
  12. #include "choreochannel.h"
  13. #include "choreoscene.h"
  14. #include "studio.h"
  15. #include "networkstringtable_gamedll.h"
  16. #include "ai_basenpc.h"
  17. #include "engine/IEngineSound.h"
  18. #include "ai_navigator.h"
  19. #include "saverestore_utlvector.h"
  20. #include "ai_baseactor.h"
  21. #include "ai_criteria.h"
  22. #include "tier1/strtools.h"
  23. #include "checksum_crc.h"
  24. #include "SoundEmitterSystem/isoundemittersystembase.h"
  25. #include "utlbuffer.h"
  26. #include "tier0/icommandline.h"
  27. #include "sceneentity.h"
  28. #include "datacache/idatacache.h"
  29. #include "dt_utlvector_send.h"
  30. #include "ichoreoeventcallback.h"
  31. #include "scenefilecache/ISceneFileCache.h"
  32. #include "SceneCache.h"
  33. #include "scripted.h"
  34. #include "basemultiplayerplayer.h"
  35. #include "env_debughistory.h"
  36. #include "datacache/imdlcache.h"
  37. #include "recipientfilter.h"
  38. #include "team.h"
  39. #include "triggers.h"
  40. #include "mathlib/ssemath.h"
  41. #include "soundinfo.h"
  42. #include "eiface.h"
  43. #include "usermessages.h"
  44. #ifdef HL2_EPISODIC
  45. #include "npc_alyx_episodic.h"
  46. #endif // HL2_EPISODIC
  47. #ifdef PORTAL2
  48. #include "portal/weapon_physcannon.h"
  49. #endif
  50. // memdbgon must be the last include file in a .cpp file!!!
  51. #include "tier0/memdbgon.h"
  52. extern ISoundEmitterSystemBase *soundemitterbase;
  53. extern ISceneFileCache *scenefilecache;
  54. extern ConVar scene_print;
  55. // From SoundEmitterSystem.cpp
  56. bool GetCaptionHash( char const *pchStringName, bool bWarnIfMissing, unsigned int &hash );
  57. bool CanEmitCaption( unsigned int hash );
  58. class CSceneEntity;
  59. class CBaseFlex;
  60. // VCDS are loaded from their compiled/binary format (much faster)
  61. // Requies vcds be saved as compiled assets
  62. //#define COMPILED_VCDS 1
  63. static ConVar scene_forcecombined( "scene_forcecombined", "0", 0, "When playing back, force use of combined .wav files even in english." );
  64. static ConVar scene_maxcaptionradius( "scene_maxcaptionradius", "1200", 0, "Only show closed captions if recipient is within this many units of speaking actor (0==disabled)." );
  65. ConVar scene_clientplayback( "scene_clientplayback", "1", 0, "Play all vcds on the clients." );
  66. // Assume sound system is 100 msec lagged (only used if we can't find snd_mixahead cvar!)
  67. #define SOUND_SYSTEM_LATENCY_DEFAULT ( 0.1f )
  68. // Think every 50 msec (FIXME: Try 10hz?)
  69. #define SCENE_THINK_INTERVAL 0.001 // FIXME: make scene's think in concert with their npc's
  70. #define FINDNAMEDENTITY_MAX_ENTITIES 32 // max number of entities to be considered for random entity selection in FindNamedEntity
  71. // List of the last 5 lines of speech from NPCs for bug reports
  72. static recentNPCSpeech_t speechListSounds[ SPEECH_LIST_MAX_SOUNDS ] = { { 0, "", "" }, { 0, "", "" }, { 0, "", "" }, { 0, "", "" }, { 0, "", "" } };
  73. static int speechListIndex = 0;
  74. // Only allow scenes to change their pitch within a range of values
  75. #define SCENE_MIN_PITCH 0.25f
  76. #define SCENE_MAX_PITCH 2.5f
  77. //===========================================================================================================
  78. // SCENE LIST MANAGER
  79. //===========================================================================================================
  80. #define SCENE_LIST_MANAGER_MAX_SCENES 16
  81. //-----------------------------------------------------------------------------
  82. // Purpose: Entity that manages a list of scenes
  83. //-----------------------------------------------------------------------------
  84. class CSceneListManager : public CLogicalEntity
  85. {
  86. DECLARE_CLASS( CSceneListManager, CLogicalEntity );
  87. public:
  88. DECLARE_DATADESC();
  89. virtual void Activate( void );
  90. void ShutdownList( void );
  91. void SceneStarted( CBaseEntity *pSceneOrManager );
  92. void AddListManager( CSceneListManager *pManager );
  93. void RemoveScene( int iIndex );
  94. // Inputs
  95. void InputShutdown( inputdata_t &inputdata );
  96. private:
  97. CUtlVector< CHandle< CSceneListManager > > m_hListManagers;
  98. string_t m_iszScenes[SCENE_LIST_MANAGER_MAX_SCENES];
  99. EHANDLE m_hScenes[SCENE_LIST_MANAGER_MAX_SCENES];
  100. };
  101. //-----------------------------------------------------------------------------
  102. // Purpose: This class exists solely to call think on all scene entities in a deterministic order
  103. //-----------------------------------------------------------------------------
  104. class CSceneManager : public CBaseEntity
  105. {
  106. DECLARE_CLASS( CSceneManager, CBaseEntity );
  107. DECLARE_DATADESC();
  108. public:
  109. virtual void Spawn()
  110. {
  111. BaseClass::Spawn();
  112. SetNextThink( gpGlobals->curtime );
  113. }
  114. virtual int ObjectCaps( void ) { return BaseClass::ObjectCaps() | FCAP_DONT_SAVE; }
  115. virtual void Think();
  116. void ClearAllScenes();
  117. void AddSceneEntity( CSceneEntity *scene );
  118. void RemoveSceneEntity( CSceneEntity *scene );
  119. void ActivateScene( CSceneEntity *scene );
  120. void DeactivateScene( CSceneEntity *scene );
  121. void QueueRestoredSound( CBaseFlex *actor, char const *soundname, soundlevel_t soundlevel, float time_in_past );
  122. void OnClientActive( CBasePlayer *player );
  123. void RemoveActorFromScenes( CBaseFlex *pActor, bool bInstancedOnly, bool bNonIdleOnly, const char *pszThisSceneOnly );
  124. void PauseActorsScenes( CBaseFlex *pActor, bool bInstancedOnly );
  125. bool IsInInterruptableScenes( CBaseFlex *pActor );
  126. void ResumeActorsScenes( CBaseFlex *pActor, bool bInstancedOnly );
  127. void QueueActorsScenesToResume( CBaseFlex *pActor, bool bInstancedOnly );
  128. bool IsRunningScriptedScene( CBaseFlex *pActor, bool bIgnoreInstancedScenes );
  129. bool IsRunningScriptedSceneAndNotPaused( CBaseFlex *pActor, bool bIgnoreInstancedScenes );
  130. bool IsRunningScriptedSceneWithSpeech( CBaseFlex *pActor, bool bIgnoreInstancedScenes );
  131. bool IsRunningScriptedSceneWithSpeechAndNotPaused( CBaseFlex *pActor, bool bIgnoreInstancedScenes );
  132. private:
  133. struct CRestoreSceneSound
  134. {
  135. CRestoreSceneSound()
  136. {
  137. actor = NULL;
  138. soundname[ 0 ] = NULL;
  139. soundlevel = SNDLVL_NORM;
  140. time_in_past = 0.0f;
  141. }
  142. CHandle< CBaseFlex > actor;
  143. char soundname[ 128 ];
  144. soundlevel_t soundlevel;
  145. float time_in_past;
  146. };
  147. CUtlVector< CHandle< CSceneEntity > > m_ActiveScenes;
  148. CUtlVector< CHandle< CSceneEntity > > m_Scenes;
  149. CUtlVector< CRestoreSceneSound > m_QueuedSceneSounds;
  150. };
  151. //---------------------------------------------------------
  152. // Save/Restore
  153. //---------------------------------------------------------
  154. BEGIN_DATADESC( CSceneManager )
  155. DEFINE_UTLVECTOR( m_ActiveScenes, FIELD_EHANDLE ),
  156. DEFINE_UTLVECTOR( m_Scenes, FIELD_EHANDLE ),
  157. // DEFINE_FIELD( m_QueuedSceneSounds, CUtlVector < CRestoreSceneSound > ), // Don't save/restore this, it's created and used by OnRestore only
  158. END_DATADESC()
  159. #ifdef DISABLE_DEBUG_HISTORY
  160. #define LocalScene_Printf Scene_Printf
  161. #else
  162. //-----------------------------------------------------------------------------
  163. // Purpose:
  164. // Input : *pFormat -
  165. // ... -
  166. // Output : static void
  167. //-----------------------------------------------------------------------------
  168. void LocalScene_Printf( const char *pFormat, ... )
  169. {
  170. va_list marker;
  171. char msg[8192];
  172. va_start(marker, pFormat);
  173. Q_vsnprintf(msg, sizeof(msg), pFormat, marker);
  174. va_end(marker);
  175. Scene_Printf( "%s", msg );
  176. ADD_DEBUG_HISTORY( HISTORY_SCENE_PRINT, UTIL_VarArgs( "(%0.2f) %s", gpGlobals->curtime, msg ) );
  177. }
  178. #endif
  179. //-----------------------------------------------------------------------------
  180. // Purpose:
  181. // Input : *filename -
  182. // **buffer -
  183. // Output : Returns true on success, false on failure.
  184. //-----------------------------------------------------------------------------
  185. bool CopySceneFileIntoMemory( char const *pFilename, byte **pBuffer, int *pSize )
  186. {
  187. size_t bufSize = scenefilecache->GetSceneBufferSize( pFilename );
  188. if ( bufSize > 0 )
  189. {
  190. *pBuffer = new byte[bufSize];
  191. *pSize = bufSize;
  192. return scenefilecache->GetSceneData( pFilename, (byte *)(*pBuffer), bufSize );
  193. }
  194. *pBuffer = 0;
  195. *pSize = 0;
  196. return false;
  197. }
  198. //-----------------------------------------------------------------------------
  199. // Purpose:
  200. //-----------------------------------------------------------------------------
  201. void FreeSceneFileMemory( byte *buffer )
  202. {
  203. delete[] buffer;
  204. }
  205. //-----------------------------------------------------------------------------
  206. // Binary compiled VCDs get their strings from a pool
  207. //-----------------------------------------------------------------------------
  208. class CChoreoStringPool : public IChoreoStringPool
  209. {
  210. public:
  211. short FindOrAddString( const char *pString )
  212. {
  213. // huh?, no compilation at run time, only fetches
  214. Assert( 0 );
  215. return -1;
  216. }
  217. bool GetString( short stringId, char *buff, int buffSize )
  218. {
  219. // fetch from compiled pool
  220. const char *pString = scenefilecache->GetSceneString( stringId );
  221. if ( !pString )
  222. {
  223. V_strncpy( buff, "", buffSize );
  224. return false;
  225. }
  226. V_strncpy( buff, pString, buffSize );
  227. return true;
  228. }
  229. };
  230. CChoreoStringPool g_ChoreoStringPool;
  231. //-----------------------------------------------------------------------------
  232. // Purpose: Singleton scene manager. Created by first placed scene or recreated it it's deleted for some unknown reason
  233. // Output : CSceneManager
  234. //-----------------------------------------------------------------------------
  235. CSceneManager *GetSceneManager()
  236. {
  237. // Create it if it doesn't exist
  238. static CHandle< CSceneManager > s_SceneManager;
  239. if ( s_SceneManager == NULL )
  240. {
  241. s_SceneManager = ( CSceneManager * )CreateEntityByName( "scene_manager" );
  242. Assert( s_SceneManager );
  243. if ( s_SceneManager )
  244. {
  245. s_SceneManager->Spawn();
  246. }
  247. }
  248. Assert( s_SceneManager );
  249. return s_SceneManager;
  250. }
  251. //-----------------------------------------------------------------------------
  252. // Purpose:
  253. // Input : *player -
  254. //-----------------------------------------------------------------------------
  255. void SceneManager_ClientActive( CBasePlayer *player )
  256. {
  257. Assert( GetSceneManager() );
  258. if ( GetSceneManager() )
  259. {
  260. GetSceneManager()->OnClientActive( player );
  261. }
  262. }
  263. //-----------------------------------------------------------------------------
  264. // Purpose: FIXME, need to deal with save/restore
  265. //-----------------------------------------------------------------------------
  266. class CSceneEntity : public CPointEntity, public IChoreoEventCallback
  267. {
  268. friend class CInstancedSceneEntity;
  269. friend class CSceneManager;
  270. public:
  271. enum
  272. {
  273. SCENE_ACTION_UNKNOWN = 0,
  274. SCENE_ACTION_CANCEL,
  275. SCENE_ACTION_RESUME,
  276. };
  277. enum
  278. {
  279. SCENE_BUSYACTOR_DEFAULT = 0,
  280. SCENE_BUSYACTOR_WAIT,
  281. SCENE_BUSYACTOR_INTERRUPT,
  282. SCENE_BUSYACTOR_INTERRUPT_CANCEL,
  283. };
  284. DECLARE_CLASS( CSceneEntity, CPointEntity );
  285. DECLARE_SERVERCLASS();
  286. // script description
  287. DECLARE_ENT_SCRIPTDESC();
  288. CSceneEntity( void );
  289. ~CSceneEntity( void );
  290. // From IChoreoEventCallback
  291. virtual void StartEvent( float currenttime, CChoreoScene *scene, CChoreoEvent *event );
  292. virtual void EndEvent( float currenttime, CChoreoScene *scene, CChoreoEvent *event );
  293. virtual void ProcessEvent( float currenttime, CChoreoScene *scene, CChoreoEvent *event );
  294. virtual bool CheckEvent( float currenttime, CChoreoScene *scene, CChoreoEvent *event );
  295. virtual int UpdateTransmitState();
  296. virtual int ShouldTransmit( const CCheckTransmitInfo *pInfo );
  297. void SetRecipientFilter( IRecipientFilter *filter );
  298. virtual void Activate();
  299. virtual void Precache( void );
  300. virtual void Spawn( void );
  301. virtual void UpdateOnRemove( void );
  302. virtual void OnRestore();
  303. virtual void OnLoaded();
  304. virtual int DrawDebugTextOverlays();
  305. DECLARE_DATADESC();
  306. virtual void OnSceneFinished( bool canceled, bool fireoutput );
  307. virtual void DoThink( float frametime );
  308. virtual void PauseThink( void );
  309. bool IsPlayingBack() const { return m_bIsPlayingBack; }
  310. bool IsPaused() const { return m_bPaused; }
  311. bool IsMultiplayer() const { return m_bMultiplayer; }
  312. bool IsInterruptable();
  313. virtual void ClearInterrupt();
  314. virtual void CheckInterruptCompletion();
  315. virtual bool InterruptThisScene( CSceneEntity *otherScene );
  316. void RequestCompletionNotification( CSceneEntity *otherScene );
  317. virtual void NotifyOfCompletion( CSceneEntity *interruptor );
  318. void AddListManager( CSceneListManager *pManager );
  319. void ClearActivatorTargets( void );
  320. void SetBreakOnNonIdle( bool bBreakOnNonIdle ) { m_bBreakOnNonIdle = bBreakOnNonIdle; }
  321. bool ShouldBreakOnNonIdle( void ) { return m_bBreakOnNonIdle; }
  322. // Inputs
  323. void InputStartPlayback( inputdata_t &inputdata );
  324. void InputPausePlayback( inputdata_t &inputdata );
  325. void InputResumePlayback( inputdata_t &inputdata );
  326. void InputCancelPlayback( inputdata_t &inputdata );
  327. void InputCancelAtNextInterrupt( inputdata_t &inputdata );
  328. void InputPitchShiftPlayback( inputdata_t &inputdata );
  329. void InputTriggerEvent( inputdata_t &inputdata );
  330. // If the scene is playing, finds an actor in the scene who can respond to the specified concept token
  331. void InputInterjectResponse( inputdata_t &inputdata );
  332. // If this scene is waiting on an actor, give up and quit trying.
  333. void InputStopWaitingForActor( inputdata_t &inputdata );
  334. virtual void StartPlayback( void );
  335. virtual void PausePlayback( void );
  336. virtual void ResumePlayback( void );
  337. virtual void CancelPlayback( void );
  338. virtual void PitchShiftPlayback( float fPitch );
  339. virtual void QueueResumePlayback( void );
  340. bool ValidScene() const;
  341. // Scene load/unload
  342. static CChoreoScene *LoadScene( const char *filename, IChoreoEventCallback *pCallback );
  343. void UnloadScene( void );
  344. struct SpeakEventSound_t
  345. {
  346. CUtlSymbol m_Symbol;
  347. float m_flStartTime;
  348. };
  349. static bool SpeakEventSoundLessFunc( const SpeakEventSound_t& lhs, const SpeakEventSound_t& rhs );
  350. bool GetSoundNameForPlayer( CChoreoEvent *event, CBasePlayer *player, char *buf, size_t buflen );
  351. void BuildSortedSpeakEventSoundsPrefetchList(
  352. CChoreoScene *scene,
  353. CUtlSymbolTable& table,
  354. CUtlRBTree< SpeakEventSound_t >& soundnames,
  355. float timeOffset );
  356. void PrefetchSpeakEventSounds( CUtlSymbolTable& table, CUtlRBTree< SpeakEventSound_t >& soundnames );
  357. // Event handlers
  358. virtual void DispatchStartExpression( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event );
  359. virtual void DispatchEndExpression( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event );
  360. virtual void DispatchStartFlexAnimation( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event );
  361. virtual void DispatchEndFlexAnimation( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event );
  362. virtual void DispatchStartGesture( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event );
  363. virtual void DispatchEndGesture( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event );
  364. virtual void DispatchStartLookAt( CChoreoScene *scene, CBaseFlex *actor, CBaseEntity *actor2, CChoreoEvent *event );
  365. virtual void DispatchEndLookAt( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event );
  366. virtual void DispatchStartMoveTo( CChoreoScene *scene, CBaseFlex *actor, CBaseEntity *actor2, CChoreoEvent *event );
  367. virtual void DispatchEndMoveTo( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event );
  368. virtual void DispatchStartSpeak( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event, soundlevel_t iSoundlevel );
  369. virtual void DispatchEndSpeak( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event );
  370. virtual void DispatchStartFace( CChoreoScene *scene, CBaseFlex *actor, CBaseEntity *actor2, CChoreoEvent *event );
  371. virtual void DispatchEndFace( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event );
  372. virtual void DispatchStartSequence( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event );
  373. virtual void DispatchEndSequence( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event );
  374. virtual void DispatchStartSubScene( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event );
  375. virtual void DispatchStartInterrupt( CChoreoScene *scene, CChoreoEvent *event );
  376. virtual void DispatchEndInterrupt( CChoreoScene *scene, CChoreoEvent *event );
  377. virtual void DispatchStartGeneric( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event );
  378. virtual void DispatchEndGeneric( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event );
  379. // NPC can play interstitial vcds (such as responding to the player doing something during a scene)
  380. virtual void DispatchStartPermitResponses( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event );
  381. virtual void DispatchEndPermitResponses( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event );
  382. // Global events
  383. virtual void DispatchProcessLoop( CChoreoScene *scene, CChoreoEvent *event );
  384. virtual void DispatchPauseScene( CChoreoScene *scene, const char *parameters );
  385. virtual void DispatchStopPoint( CChoreoScene *scene, const char *parameters );
  386. virtual float EstimateLength( void );
  387. void CancelIfSceneInvolvesActor( CBaseEntity *pActor );
  388. bool InvolvesActor( CBaseEntity *pActor ); // NOTE: returns false if scene hasn't loaded yet
  389. void GenerateSoundScene( CBaseFlex *pActor, const char *soundname );
  390. virtual float GetPostSpeakDelay() { return 1.0; }
  391. bool HasUnplayedSpeech( void );
  392. bool HasFlexAnimation( void );
  393. void SetCurrentTime( float t, bool forceClientSync );
  394. void InputScriptPlayerDeath( inputdata_t &inputdata );
  395. void InputSetTarget1( inputdata_t &inputdata );
  396. void InputSetTarget2( inputdata_t &inputdata );
  397. void InputSetTarget3( inputdata_t &inputdata );
  398. void InputSetTarget4( inputdata_t &inputdata );
  399. void AddBroadcastTeamTarget( int nTeamIndex );
  400. void RemoveBroadcastTeamTarget( int nTeamIndex );
  401. // Data
  402. public:
  403. string_t m_iszSceneFile;
  404. string_t m_iszResumeSceneFile;
  405. EHANDLE m_hWaitingForThisResumeScene;
  406. bool m_bWaitingForResumeScene;
  407. string_t m_iszTarget1;
  408. string_t m_iszTarget2;
  409. string_t m_iszTarget3;
  410. string_t m_iszTarget4;
  411. string_t m_iszTarget5;
  412. string_t m_iszTarget6;
  413. string_t m_iszTarget7;
  414. string_t m_iszTarget8;
  415. EHANDLE m_hTarget1;
  416. EHANDLE m_hTarget2;
  417. EHANDLE m_hTarget3;
  418. EHANDLE m_hTarget4;
  419. EHANDLE m_hTarget5;
  420. EHANDLE m_hTarget6;
  421. EHANDLE m_hTarget7;
  422. EHANDLE m_hTarget8;
  423. CNetworkVar( bool, m_bIsPlayingBack );
  424. CNetworkVar( bool, m_bPaused );
  425. CNetworkVar( bool, m_bMultiplayer );
  426. CNetworkVar( float, m_flForceClientTime );
  427. float m_flCurrentTime;
  428. float m_flFrameTime;
  429. bool m_bCancelAtNextInterrupt;
  430. float m_fPitch;
  431. bool m_bAutomated;
  432. int m_nAutomatedAction;
  433. float m_flAutomationDelay;
  434. float m_flAutomationTime;
  435. // A pause from an input require s another input to unpause (it's a hard pause)
  436. bool m_bPausedViaInput;
  437. // Moved for cache [9/22/2010 tom]
  438. // // Waiting for the actor to be able to speak.
  439. // bool m_bWaitingForActor;
  440. //
  441. // // Waiting for a point at which we can interrupt our actors
  442. // bool m_bWaitingForInterrupt;
  443. bool m_bInterruptedActorsScenes;
  444. bool m_bBreakOnNonIdle;
  445. public:
  446. virtual CBaseFlex *FindNamedActor( int index );
  447. virtual CBaseFlex *FindNamedActor( CChoreoActor *pChoreoActor );
  448. virtual CBaseFlex *FindNamedActor( const char *name );
  449. virtual CBaseEntity *FindNamedEntity( const char *name, CBaseEntity *pActor = NULL, bool bBaseFlexOnly = false, bool bUseClear = false );
  450. CBaseEntity *FindNamedTarget( string_t iszTarget, bool bBaseFlexOnly = false );
  451. virtual CBaseEntity *FindNamedEntityClosest( const char *name, CBaseEntity *pActor = NULL, bool bBaseFlexOnly = false, bool bUseClear = false, const char *pszSecondary = NULL );
  452. HSCRIPT ScriptFindNamedEntity( const char *name );
  453. bool ScriptLoadSceneFromString( const char * pszFilename, const char *pszData );
  454. private:
  455. CUtlVector< CHandle< CBaseFlex > > m_hActorList;
  456. CUtlVector< CHandle< CBaseEntity > > m_hRemoveActorList;
  457. private:
  458. inline void SetRestoring( bool bRestoring );
  459. // Prevent derived classed from using this!
  460. virtual void Think( void ) {};
  461. void ClearSceneEvents( CChoreoScene *scene, bool canceled );
  462. void ClearSchedules( CChoreoScene *scene );
  463. float GetSoundSystemLatency( void );
  464. void PrecacheScene( CChoreoScene *scene );
  465. CChoreoScene *GenerateSceneForSound( CBaseFlex *pFlexActor, const char *soundname );
  466. bool CheckActors();
  467. void PrefetchAnimBlocks( CChoreoScene *scene );
  468. bool ShouldNetwork() const;
  469. // Set if we tried to async the scene but the FS returned that the data was not loadable
  470. bool m_bSceneMissing;
  471. // Moved for cache [9/22/2010 tom]
  472. // CChoreoScene *m_pScene;
  473. CNetworkVar( int, m_nSceneStringIndex );
  474. COutputEvent m_OnStart;
  475. COutputEvent m_OnCompletion;
  476. COutputEvent m_OnCanceled;
  477. COutputEvent m_OnTrigger1;
  478. COutputEvent m_OnTrigger2;
  479. COutputEvent m_OnTrigger3;
  480. COutputEvent m_OnTrigger4;
  481. COutputEvent m_OnTrigger5;
  482. COutputEvent m_OnTrigger6;
  483. COutputEvent m_OnTrigger7;
  484. COutputEvent m_OnTrigger8;
  485. COutputEvent m_OnTrigger9;
  486. COutputEvent m_OnTrigger10;
  487. COutputEvent m_OnTrigger11;
  488. COutputEvent m_OnTrigger12;
  489. COutputEvent m_OnTrigger13;
  490. COutputEvent m_OnTrigger14;
  491. COutputEvent m_OnTrigger15;
  492. COutputEvent m_OnTrigger16;
  493. CChoreoScene *m_pScene;
  494. // Waiting for the actor to be able to speak.
  495. bool m_bWaitingForActor;
  496. // Waiting for a point at which we can interrupt our actors
  497. bool m_bWaitingForInterrupt;
  498. int m_nInterruptCount;
  499. bool m_bInterrupted;
  500. CHandle< CSceneEntity > m_hInterruptScene;
  501. bool m_bCompletedEarly;
  502. bool m_bInterruptSceneFinished;
  503. CUtlVector< CHandle< CSceneEntity > > m_hNotifySceneCompletion;
  504. CUtlVector< CHandle< CSceneListManager > > m_hListManagers;
  505. bool m_bRestoring;
  506. bool m_bGenerated;
  507. string_t m_iszSoundName;
  508. CHandle< CBaseFlex > m_hActor;
  509. EHANDLE m_hActivator;
  510. int m_BusyActor;
  511. int m_iPlayerDeathBehavior;
  512. CRecipientFilter *m_pRecipientFilter;
  513. public:
  514. void SetBackground( bool bIsBackground );
  515. bool IsBackground( void );
  516. };
  517. LINK_ENTITY_TO_CLASS( logic_choreographed_scene, CSceneEntity );
  518. LINK_ENTITY_TO_CLASS( scripted_scene, CSceneEntity );
  519. IMPLEMENT_SERVERCLASS_ST_NOBASE( CSceneEntity, DT_SceneEntity )
  520. SendPropInt(SENDINFO(m_nSceneStringIndex),MAX_CHOREO_SCENES_STRING_BITS,SPROP_UNSIGNED),
  521. SendPropBool(SENDINFO(m_bIsPlayingBack)),
  522. SendPropBool(SENDINFO(m_bPaused)),
  523. SendPropBool(SENDINFO(m_bMultiplayer)),
  524. SendPropFloat(SENDINFO(m_flForceClientTime)),
  525. SendPropUtlVector(
  526. SENDINFO_UTLVECTOR( m_hActorList ),
  527. MAX_ACTORS_IN_SCENE, // max elements
  528. SendPropEHandle( NULL, 0 ) ),
  529. END_SEND_TABLE()
  530. BEGIN_DATADESC( CSceneEntity )
  531. // Keys
  532. DEFINE_KEYFIELD( m_iszSceneFile, FIELD_STRING, "SceneFile" ),
  533. DEFINE_KEYFIELD( m_iszResumeSceneFile, FIELD_STRING, "ResumeSceneFile" ),
  534. DEFINE_FIELD( m_hWaitingForThisResumeScene, FIELD_EHANDLE ),
  535. DEFINE_FIELD( m_bWaitingForResumeScene, FIELD_BOOLEAN ),
  536. DEFINE_KEYFIELD( m_iszTarget1, FIELD_STRING, "target1" ),
  537. DEFINE_KEYFIELD( m_iszTarget2, FIELD_STRING, "target2" ),
  538. DEFINE_KEYFIELD( m_iszTarget3, FIELD_STRING, "target3" ),
  539. DEFINE_KEYFIELD( m_iszTarget4, FIELD_STRING, "target4" ),
  540. DEFINE_KEYFIELD( m_iszTarget5, FIELD_STRING, "target5" ),
  541. DEFINE_KEYFIELD( m_iszTarget6, FIELD_STRING, "target6" ),
  542. DEFINE_KEYFIELD( m_iszTarget7, FIELD_STRING, "target7" ),
  543. DEFINE_KEYFIELD( m_iszTarget8, FIELD_STRING, "target8" ),
  544. DEFINE_KEYFIELD( m_BusyActor, FIELD_INTEGER, "busyactor" ),
  545. DEFINE_FIELD( m_hTarget1, FIELD_EHANDLE ),
  546. DEFINE_FIELD( m_hTarget2, FIELD_EHANDLE ),
  547. DEFINE_FIELD( m_hTarget3, FIELD_EHANDLE ),
  548. DEFINE_FIELD( m_hTarget4, FIELD_EHANDLE ),
  549. DEFINE_FIELD( m_hTarget5, FIELD_EHANDLE ),
  550. DEFINE_FIELD( m_hTarget6, FIELD_EHANDLE ),
  551. DEFINE_FIELD( m_hTarget7, FIELD_EHANDLE ),
  552. DEFINE_FIELD( m_hTarget8, FIELD_EHANDLE ),
  553. DEFINE_FIELD( m_bIsPlayingBack, FIELD_BOOLEAN ),
  554. DEFINE_FIELD( m_bPaused, FIELD_BOOLEAN ),
  555. DEFINE_FIELD( m_flCurrentTime, FIELD_FLOAT ), // relative, not absolute time
  556. DEFINE_FIELD( m_flForceClientTime, FIELD_FLOAT ),
  557. DEFINE_FIELD( m_flFrameTime, FIELD_FLOAT ), // last frametime
  558. DEFINE_FIELD( m_bCancelAtNextInterrupt, FIELD_BOOLEAN ),
  559. DEFINE_FIELD( m_fPitch, FIELD_FLOAT ),
  560. DEFINE_FIELD( m_bAutomated, FIELD_BOOLEAN ),
  561. DEFINE_FIELD( m_nAutomatedAction, FIELD_INTEGER ),
  562. DEFINE_FIELD( m_flAutomationDelay, FIELD_FLOAT ),
  563. DEFINE_FIELD( m_flAutomationTime, FIELD_FLOAT ), // relative, not absolute time
  564. DEFINE_FIELD( m_bPausedViaInput, FIELD_BOOLEAN ),
  565. DEFINE_FIELD( m_bWaitingForActor, FIELD_BOOLEAN ),
  566. DEFINE_FIELD( m_bWaitingForInterrupt, FIELD_BOOLEAN ),
  567. DEFINE_FIELD( m_bInterruptedActorsScenes, FIELD_BOOLEAN ),
  568. DEFINE_FIELD( m_bBreakOnNonIdle, FIELD_BOOLEAN ),
  569. DEFINE_UTLVECTOR( m_hActorList, FIELD_EHANDLE ),
  570. DEFINE_UTLVECTOR( m_hRemoveActorList, FIELD_EHANDLE ),
  571. // DEFINE_FIELD( m_pScene, FIELD_XXXX ) // Special processing used for this
  572. // These are set up in the constructor
  573. // DEFINE_FIELD( m_bRestoring, FIELD_BOOLEAN ),
  574. DEFINE_FIELD( m_nInterruptCount, FIELD_INTEGER ),
  575. DEFINE_FIELD( m_bInterrupted, FIELD_BOOLEAN ),
  576. DEFINE_FIELD( m_hInterruptScene, FIELD_EHANDLE ),
  577. DEFINE_FIELD( m_bCompletedEarly, FIELD_BOOLEAN ),
  578. DEFINE_FIELD( m_bInterruptSceneFinished, FIELD_BOOLEAN ),
  579. DEFINE_FIELD( m_bGenerated, FIELD_BOOLEAN ),
  580. DEFINE_FIELD( m_iszSoundName, FIELD_STRING ),
  581. DEFINE_FIELD( m_hActor, FIELD_EHANDLE ),
  582. DEFINE_FIELD( m_hActivator, FIELD_EHANDLE ),
  583. // DEFINE_FIELD( m_bSceneMissing, FIELD_BOOLEAN ),
  584. DEFINE_UTLVECTOR( m_hNotifySceneCompletion, FIELD_EHANDLE ),
  585. DEFINE_UTLVECTOR( m_hListManagers, FIELD_EHANDLE ),
  586. DEFINE_FIELD( m_bMultiplayer, FIELD_BOOLEAN ),
  587. // DEFINE_FIELD( m_nSceneStringIndex, FIELD_INTEGER ),
  588. // DEFINE_FIELD( m_pRecipientFilter, IRecipientFilter* ), // Multiplayer only
  589. // Inputs
  590. DEFINE_INPUTFUNC( FIELD_VOID, "Start", InputStartPlayback ),
  591. DEFINE_INPUTFUNC( FIELD_VOID, "Pause", InputPausePlayback ),
  592. DEFINE_INPUTFUNC( FIELD_VOID, "Resume", InputResumePlayback ),
  593. DEFINE_INPUTFUNC( FIELD_VOID, "Cancel", InputCancelPlayback ),
  594. DEFINE_INPUTFUNC( FIELD_VOID, "CancelAtNextInterrupt", InputCancelAtNextInterrupt ),
  595. DEFINE_INPUTFUNC( FIELD_FLOAT, "PitchShift", InputPitchShiftPlayback ),
  596. DEFINE_INPUTFUNC( FIELD_STRING, "InterjectResponse", InputInterjectResponse ),
  597. DEFINE_INPUTFUNC( FIELD_VOID, "StopWaitingForActor", InputStopWaitingForActor ),
  598. DEFINE_INPUTFUNC( FIELD_INTEGER, "Trigger", InputTriggerEvent ),
  599. DEFINE_KEYFIELD( m_iPlayerDeathBehavior, FIELD_INTEGER, "onplayerdeath" ),
  600. DEFINE_INPUTFUNC( FIELD_VOID, "ScriptPlayerDeath", InputScriptPlayerDeath ),
  601. DEFINE_INPUTFUNC( FIELD_STRING, "SetTarget1", InputSetTarget1 ),
  602. DEFINE_INPUTFUNC( FIELD_STRING, "SetTarget2", InputSetTarget2 ),
  603. DEFINE_INPUTFUNC( FIELD_STRING, "SetTarget3", InputSetTarget3 ),
  604. DEFINE_INPUTFUNC( FIELD_STRING, "SetTarget4", InputSetTarget4 ),
  605. // Outputs
  606. DEFINE_OUTPUT( m_OnStart, "OnStart"),
  607. DEFINE_OUTPUT( m_OnCompletion, "OnCompletion"),
  608. DEFINE_OUTPUT( m_OnCanceled, "OnCanceled"),
  609. DEFINE_OUTPUT( m_OnTrigger1, "OnTrigger1"),
  610. DEFINE_OUTPUT( m_OnTrigger2, "OnTrigger2"),
  611. DEFINE_OUTPUT( m_OnTrigger3, "OnTrigger3"),
  612. DEFINE_OUTPUT( m_OnTrigger4, "OnTrigger4"),
  613. DEFINE_OUTPUT( m_OnTrigger5, "OnTrigger5"),
  614. DEFINE_OUTPUT( m_OnTrigger6, "OnTrigger6"),
  615. DEFINE_OUTPUT( m_OnTrigger7, "OnTrigger7"),
  616. DEFINE_OUTPUT( m_OnTrigger8, "OnTrigger8"),
  617. DEFINE_OUTPUT( m_OnTrigger9, "OnTrigger9"),
  618. DEFINE_OUTPUT( m_OnTrigger10, "OnTrigger10"),
  619. DEFINE_OUTPUT( m_OnTrigger11, "OnTrigger11"),
  620. DEFINE_OUTPUT( m_OnTrigger12, "OnTrigger12"),
  621. DEFINE_OUTPUT( m_OnTrigger13, "OnTrigger13"),
  622. DEFINE_OUTPUT( m_OnTrigger14, "OnTrigger14"),
  623. DEFINE_OUTPUT( m_OnTrigger15, "OnTrigger15"),
  624. DEFINE_OUTPUT( m_OnTrigger16, "OnTrigger16"),
  625. END_DATADESC()
  626. BEGIN_ENT_SCRIPTDESC( CSceneEntity, CBaseEntity, "Choreographed scene which controls animation and/or dialog on one or more actors." )
  627. DEFINE_SCRIPTFUNC( EstimateLength, "Returns length of this scene in seconds." )
  628. DEFINE_SCRIPTFUNC( IsPlayingBack, "If this scene is currently playing." )
  629. DEFINE_SCRIPTFUNC( IsPaused, "If this scene is currently paused." )
  630. DEFINE_SCRIPTFUNC( AddBroadcastTeamTarget, "Adds a team (by index) to the broadcast list" )
  631. DEFINE_SCRIPTFUNC( RemoveBroadcastTeamTarget, "Removes a team (by index) from the broadcast list" )
  632. DEFINE_SCRIPTFUNC_NAMED( ScriptFindNamedEntity, "FindNamedEntity", "given an entity reference, such as !target, get actual entity from scene object" )
  633. DEFINE_SCRIPTFUNC_NAMED( ScriptLoadSceneFromString, "LoadSceneFromString", "given a dummy scene name and a vcd string, load the scene" )
  634. END_SCRIPTDESC();
  635. //-----------------------------------------------------------------------------
  636. // Purpose:
  637. //-----------------------------------------------------------------------------
  638. CSceneEntity::CSceneEntity( void )
  639. {
  640. m_bWaitingForActor = false;
  641. m_bWaitingForInterrupt = false;
  642. m_bInterruptedActorsScenes = false;
  643. m_bIsPlayingBack = false;
  644. m_bPaused = false;
  645. m_bMultiplayer = false;
  646. m_fPitch = 1.0f;
  647. m_iszSceneFile = NULL_STRING;
  648. m_iszResumeSceneFile = NULL_STRING;
  649. m_hWaitingForThisResumeScene = NULL;
  650. m_bWaitingForResumeScene = false;
  651. SetCurrentTime( 0.0f, false );
  652. m_bCancelAtNextInterrupt = false;
  653. m_bAutomated = false;
  654. m_nAutomatedAction = SCENE_ACTION_UNKNOWN;
  655. m_flAutomationDelay = 0.0f;
  656. m_flAutomationTime = 0.0f;
  657. m_bPausedViaInput = false;
  658. ClearInterrupt();
  659. m_pScene = NULL;
  660. m_bCompletedEarly = false;
  661. m_BusyActor = SCENE_BUSYACTOR_DEFAULT;
  662. }
  663. //-----------------------------------------------------------------------------
  664. // Purpose:
  665. //-----------------------------------------------------------------------------
  666. CSceneEntity::~CSceneEntity( void )
  667. {
  668. delete m_pRecipientFilter;
  669. m_pRecipientFilter = NULL;
  670. }
  671. //-----------------------------------------------------------------------------
  672. // Purpose: Resets time such that the client version of the .vcd is also updated, if appropriate
  673. // Input : t -
  674. // forceClientSync - forces new timestamp down to client .dll via networking
  675. //-----------------------------------------------------------------------------
  676. void CSceneEntity::SetCurrentTime( float t, bool bForceClientSync )
  677. {
  678. m_flCurrentTime = t;
  679. if ( gpGlobals->maxClients == 1 || bForceClientSync )
  680. {
  681. m_flForceClientTime = t;
  682. }
  683. }
  684. //-----------------------------------------------------------------------------
  685. // Purpose:
  686. //-----------------------------------------------------------------------------
  687. void CSceneEntity::UpdateOnRemove( void )
  688. {
  689. UnloadScene();
  690. BaseClass::UpdateOnRemove();
  691. if ( GetSceneManager() )
  692. {
  693. GetSceneManager()->RemoveSceneEntity( this );
  694. }
  695. }
  696. //-----------------------------------------------------------------------------
  697. // Purpose:
  698. // Input : *actor -
  699. // *soundname -
  700. // Output : CChoreoScene
  701. //-----------------------------------------------------------------------------
  702. CChoreoScene *CSceneEntity::GenerateSceneForSound( CBaseFlex *pFlexActor, const char *soundname )
  703. {
  704. float duration = CBaseEntity::GetSoundDuration( soundname, pFlexActor ? STRING( pFlexActor->GetModelName() ) : NULL );
  705. if( duration <= 0.0f )
  706. {
  707. Warning( "CSceneEntity::GenerateSceneForSound: Couldn't determine duration of %s\n", soundname );
  708. return NULL;
  709. }
  710. CChoreoScene *scene = new CChoreoScene( this );
  711. if ( !scene )
  712. {
  713. Warning( "CSceneEntity::GenerateSceneForSound: Failed to allocated new scene!!!\n" );
  714. }
  715. else
  716. {
  717. scene->SetPrintFunc( LocalScene_Printf );
  718. CChoreoActor *actor = scene->AllocActor();
  719. CChoreoChannel *channel = scene->AllocChannel();
  720. CChoreoEvent *event = scene->AllocEvent();
  721. Assert( actor );
  722. Assert( channel );
  723. Assert( event );
  724. if ( !actor || !channel || !event )
  725. {
  726. Warning( "CSceneEntity::GenerateSceneForSound: Alloc of actor, channel, or event failed!!!\n" );
  727. delete scene;
  728. return NULL;
  729. }
  730. // Set us up the actorz
  731. actor->SetName( "!self" ); // Could be pFlexActor->GetName()?
  732. actor->SetActive( true );
  733. // Set us up the channelz
  734. channel->SetName( STRING( m_iszSceneFile ) );
  735. channel->SetActor( actor );
  736. // Add to actor
  737. actor->AddChannel( channel );
  738. // Set us up the eventz
  739. event->SetType( CChoreoEvent::SPEAK );
  740. event->SetName( soundname );
  741. event->SetParameters( soundname );
  742. event->SetStartTime( 0.0f );
  743. event->SetUsingRelativeTag( false );
  744. event->SetEndTime( duration );
  745. event->SnapTimes();
  746. // Add to channel
  747. channel->AddEvent( event );
  748. // Point back to our owners
  749. event->SetChannel( channel );
  750. event->SetActor( actor );
  751. }
  752. return scene;
  753. }
  754. //-----------------------------------------------------------------------------
  755. // Purpose:
  756. //-----------------------------------------------------------------------------
  757. void CSceneEntity::Activate()
  758. {
  759. if ( m_bGenerated && !m_pScene )
  760. {
  761. m_pScene = GenerateSceneForSound( m_hActor, STRING( m_iszSoundName ) );
  762. if ( GetSceneManager() )
  763. {
  764. GetSceneManager()->ActivateScene( this );
  765. }
  766. }
  767. BaseClass::Activate();
  768. if ( GetSceneManager() )
  769. {
  770. GetSceneManager()->AddSceneEntity( this );
  771. }
  772. }
  773. //-----------------------------------------------------------------------------
  774. // Purpose:
  775. // Output : float
  776. //-----------------------------------------------------------------------------
  777. float CSceneEntity::GetSoundSystemLatency( void )
  778. {
  779. return engine->GetLatencyForChoreoSounds();
  780. }
  781. //-----------------------------------------------------------------------------
  782. // Purpose:
  783. // Input : *scene -
  784. //-----------------------------------------------------------------------------
  785. void CSceneEntity::PrecacheScene( CChoreoScene *scene )
  786. {
  787. Assert( scene );
  788. // Iterate events and precache necessary resources
  789. for ( int i = 0; i < scene->GetNumEvents(); i++ )
  790. {
  791. CChoreoEvent *event = scene->GetEvent( i );
  792. if ( !event )
  793. continue;
  794. // load any necessary data
  795. switch (event->GetType() )
  796. {
  797. default:
  798. break;
  799. case CChoreoEvent::SPEAK:
  800. {
  801. // Defined in SoundEmitterSystem.cpp
  802. // NOTE: The script entries associated with .vcds are forced to preload to avoid
  803. // loading hitches during triggering
  804. PrecacheScriptSound( event->GetParameters() );
  805. if ( event->GetCloseCaptionType() == CChoreoEvent::CC_MASTER &&
  806. event->GetNumSlaves() > 0 )
  807. {
  808. char tok[ CChoreoEvent::MAX_CCTOKEN_STRING ];
  809. if ( event->GetPlaybackCloseCaptionToken( tok, sizeof( tok ) ) )
  810. {
  811. PrecacheScriptSound( tok );
  812. }
  813. }
  814. }
  815. break;
  816. case CChoreoEvent::SUBSCENE:
  817. {
  818. // Only allow a single level of subscenes for now
  819. if ( !scene->IsSubScene() )
  820. {
  821. CChoreoScene *subscene = event->GetSubScene();
  822. if ( !subscene )
  823. {
  824. subscene = LoadScene( event->GetParameters(), this );
  825. subscene->SetSubScene( true );
  826. event->SetSubScene( subscene );
  827. // Now precache it's resources, if any
  828. PrecacheScene( subscene );
  829. }
  830. }
  831. }
  832. break;
  833. }
  834. }
  835. }
  836. //-----------------------------------------------------------------------------
  837. // Purpose:
  838. //-----------------------------------------------------------------------------
  839. void CSceneEntity::Precache( void )
  840. {
  841. if ( m_bGenerated )
  842. return;
  843. if ( m_iszSceneFile == NULL_STRING )
  844. return;
  845. if ( m_iszResumeSceneFile != NULL_STRING )
  846. {
  847. PrecacheInstancedScene( STRING( m_iszResumeSceneFile ) );
  848. }
  849. PrecacheInstancedScene( STRING( m_iszSceneFile ) );
  850. }
  851. //-----------------------------------------------------------------------------
  852. // Purpose:
  853. // Input : *pActor -
  854. // *soundname -
  855. //-----------------------------------------------------------------------------
  856. void CSceneEntity::GenerateSoundScene( CBaseFlex *pActor, const char *soundname )
  857. {
  858. m_bGenerated = true;
  859. m_iszSoundName = MAKE_STRING( soundname );
  860. m_hActor = pActor;
  861. }
  862. //-----------------------------------------------------------------------------
  863. // Purpose:
  864. // Output : Returns true on success, false on failure.
  865. //-----------------------------------------------------------------------------
  866. bool CSceneEntity::HasUnplayedSpeech( void )
  867. {
  868. if ( m_pScene )
  869. return m_pScene->HasUnplayedSpeech();
  870. return false;
  871. }
  872. //-----------------------------------------------------------------------------
  873. // Purpose:
  874. // Output : Returns true on success, false on failure.
  875. //-----------------------------------------------------------------------------
  876. bool CSceneEntity::HasFlexAnimation( void )
  877. {
  878. if ( m_pScene )
  879. return m_pScene->HasFlexAnimation();
  880. return false;
  881. }
  882. //-----------------------------------------------------------------------------
  883. // Purpose:
  884. // Output :
  885. //-----------------------------------------------------------------------------
  886. void CSceneEntity::SetBackground( bool bIsBackground )
  887. {
  888. if ( m_pScene )
  889. {
  890. m_pScene->SetBackground( bIsBackground );
  891. }
  892. }
  893. //-----------------------------------------------------------------------------
  894. // Purpose:
  895. // Output : Returns true on success, false on failure.
  896. //-----------------------------------------------------------------------------
  897. bool CSceneEntity::IsBackground( void )
  898. {
  899. if ( m_pScene )
  900. return m_pScene->IsBackground( );
  901. return false;
  902. }
  903. //-----------------------------------------------------------------------------
  904. // Purpose:
  905. //-----------------------------------------------------------------------------
  906. void CSceneEntity::OnRestore()
  907. {
  908. BaseClass::OnRestore();
  909. // Fix saved games that have their pitch set to zero
  910. if ( m_fPitch < SCENE_MIN_PITCH || m_fPitch > SCENE_MAX_PITCH )
  911. m_fPitch = 1.0f;
  912. if ( !m_bIsPlayingBack )
  913. return;
  914. if ( !m_pScene )
  915. {
  916. m_pScene = LoadScene( STRING( m_iszSceneFile ), this );
  917. if ( !m_pScene )
  918. {
  919. m_bSceneMissing = true;
  920. return;
  921. }
  922. if ( GetSceneManager() )
  923. {
  924. GetSceneManager()->ActivateScene( this );
  925. }
  926. OnLoaded();
  927. if ( ShouldNetwork() )
  928. {
  929. m_nSceneStringIndex = g_pStringTableClientSideChoreoScenes->AddString( CBaseEntity::IsServer(), STRING( m_iszSceneFile ) );
  930. }
  931. UpdateTransmitState();
  932. }
  933. else
  934. {
  935. if ( GetSceneManager() )
  936. {
  937. GetSceneManager()->ActivateScene( this );
  938. }
  939. }
  940. m_bSceneMissing = false;
  941. int i;
  942. for ( i = 0 ; i < m_pScene->GetNumActors(); i++ )
  943. {
  944. CBaseFlex *pTestActor = FindNamedActor( i );
  945. if ( !pTestActor )
  946. continue;
  947. if ( !pTestActor->MyCombatCharacterPointer() )
  948. continue;
  949. // Needed?
  950. //if ( !pTestActor->MyCombatCharacterPointer()->IsAlive() )
  951. // return;
  952. pTestActor->StartChoreoScene( m_pScene );
  953. }
  954. float dt = SCENE_THINK_INTERVAL;
  955. bool paused = m_bPaused;
  956. m_bPaused = false;
  957. // roll back slightly so that pause events still trigger
  958. m_pScene->ResetSimulation( true, m_flCurrentTime - SCENE_THINK_INTERVAL, m_flCurrentTime );
  959. m_pScene->SetTime( m_flCurrentTime - SCENE_THINK_INTERVAL );
  960. SetCurrentTime( m_flCurrentTime, true );
  961. // Robin: This causes a miscount of any interrupt events in the scene.
  962. // All the variables are saved/restored properly, so we can safely leave them alone.
  963. //ClearInterrupt();
  964. SetRestoring( true );
  965. DoThink( dt );
  966. SetRestoring( false );
  967. if ( paused )
  968. {
  969. PausePlayback();
  970. }
  971. NetworkProp()->NetworkStateForceUpdate();
  972. }
  973. //-----------------------------------------------------------------------------
  974. //-----------------------------------------------------------------------------
  975. void CSceneEntity::SetRestoring( bool bRestoring )
  976. {
  977. m_bRestoring = bRestoring;
  978. if ( m_pScene )
  979. {
  980. m_pScene->SetRestoring( bRestoring );
  981. }
  982. }
  983. //-----------------------------------------------------------------------------
  984. // Purpose:
  985. //-----------------------------------------------------------------------------
  986. void CSceneEntity::Spawn( void )
  987. {
  988. Precache();
  989. }
  990. void CSceneEntity::PauseThink( void )
  991. {
  992. if ( !m_pScene )
  993. return;
  994. // Stay paused if pause occurred from interrupt
  995. if ( m_bInterrupted )
  996. return;
  997. // If entity I/O paused the scene, then it'll have to resume/cancel the scene...
  998. if ( m_bPausedViaInput )
  999. {
  1000. // If we're waiting for a resume scene to finish, continue when it's done
  1001. if ( m_bWaitingForResumeScene && !m_hWaitingForThisResumeScene )
  1002. {
  1003. // Resume scene has finished, stop waiting for it
  1004. m_bWaitingForResumeScene = false;
  1005. }
  1006. else
  1007. {
  1008. return;
  1009. }
  1010. }
  1011. if ( !m_bAutomated )
  1012. {
  1013. // FIXME: Game code should check for AI waiting conditions being met, etc.
  1014. //
  1015. //
  1016. //
  1017. bool bAllFinished = m_pScene->CheckEventCompletion( );
  1018. if ( bAllFinished )
  1019. {
  1020. // Perform action
  1021. switch ( m_nAutomatedAction )
  1022. {
  1023. case SCENE_ACTION_RESUME:
  1024. ResumePlayback();
  1025. break;
  1026. case SCENE_ACTION_CANCEL:
  1027. LocalScene_Printf( "%s : PauseThink canceling playback\n", STRING( m_iszSceneFile ) );
  1028. CancelPlayback();
  1029. break;
  1030. default:
  1031. ResumePlayback();
  1032. break;
  1033. }
  1034. // Reset
  1035. m_bAutomated = false;
  1036. m_nAutomatedAction = SCENE_ACTION_UNKNOWN;
  1037. m_flAutomationTime = 0.0f;
  1038. m_flAutomationDelay = 0.0f;
  1039. m_bPausedViaInput = false;
  1040. }
  1041. return;
  1042. }
  1043. // Otherwise, see if resume/cancel is automatic and act accordingly if enough time
  1044. // has passed
  1045. m_flAutomationTime += (gpGlobals->frametime);
  1046. if ( m_flAutomationDelay > 0.0f &&
  1047. m_flAutomationTime < m_flAutomationDelay )
  1048. return;
  1049. // Perform action
  1050. switch ( m_nAutomatedAction )
  1051. {
  1052. case SCENE_ACTION_RESUME:
  1053. LocalScene_Printf( "%s : Automatically resuming playback\n", STRING( m_iszSceneFile ) );
  1054. ResumePlayback();
  1055. break;
  1056. case SCENE_ACTION_CANCEL:
  1057. LocalScene_Printf( "%s : Automatically canceling playback\n", STRING( m_iszSceneFile ) );
  1058. CancelPlayback();
  1059. break;
  1060. default:
  1061. LocalScene_Printf( "%s : Unknown action %i, automatically resuming playback\n", STRING( m_iszSceneFile ), m_nAutomatedAction );
  1062. ResumePlayback();
  1063. break;
  1064. }
  1065. // Reset
  1066. m_bAutomated = false;
  1067. m_nAutomatedAction = SCENE_ACTION_UNKNOWN;
  1068. m_flAutomationTime = 0.0f;
  1069. m_flAutomationDelay = 0.0f;
  1070. m_bPausedViaInput = false;
  1071. }
  1072. //-----------------------------------------------------------------------------
  1073. // Purpose:
  1074. //-----------------------------------------------------------------------------
  1075. void CSceneEntity::DispatchPauseScene( CChoreoScene *scene, const char *parameters )
  1076. {
  1077. // Don't pause during restore, since we'll be restoring the pause state already
  1078. if ( m_bRestoring )
  1079. return;
  1080. // FIXME: Hook this up to AI, etc. somehow, perhaps poll each actor for conditions using
  1081. // scene resume condition iterator
  1082. PausePlayback();
  1083. char token[1024];
  1084. m_bPausedViaInput = false;
  1085. m_bAutomated = false;
  1086. m_nAutomatedAction = SCENE_ACTION_UNKNOWN;
  1087. m_flAutomationDelay = 0.0f;
  1088. m_flAutomationTime = 0.0f;
  1089. // Check for auto resume/cancel
  1090. const char *buffer = parameters;
  1091. buffer = engine->ParseFile( buffer, token, sizeof( token ) );
  1092. if ( !stricmp( token, "automate" ) )
  1093. {
  1094. buffer = engine->ParseFile( buffer, token, sizeof( token ) );
  1095. if ( !stricmp( token, "Cancel" ) )
  1096. {
  1097. m_nAutomatedAction = SCENE_ACTION_CANCEL;
  1098. }
  1099. else if ( !stricmp( token, "Resume" ) )
  1100. {
  1101. m_nAutomatedAction = SCENE_ACTION_RESUME;
  1102. }
  1103. if ( m_nAutomatedAction != SCENE_ACTION_UNKNOWN )
  1104. {
  1105. buffer = engine->ParseFile( buffer, token, sizeof( token ) );
  1106. m_flAutomationDelay = (float)atof( token );
  1107. if ( m_flAutomationDelay > 0.0f )
  1108. {
  1109. // Success
  1110. m_bAutomated = true;
  1111. m_flAutomationTime = 0.0f;
  1112. }
  1113. }
  1114. }
  1115. }
  1116. //-----------------------------------------------------------------------------
  1117. // Purpose:
  1118. // Input : *scene -
  1119. // *event -
  1120. //-----------------------------------------------------------------------------
  1121. void CSceneEntity::DispatchProcessLoop( CChoreoScene *scene, CChoreoEvent *event )
  1122. {
  1123. // Don't restore this event since it's implied in the current "state" of the scene timer, etc.
  1124. if ( m_bRestoring )
  1125. return;
  1126. Assert( scene );
  1127. Assert( event->GetType() == CChoreoEvent::LOOP );
  1128. float backtime = (float)atof( event->GetParameters() );
  1129. bool process = true;
  1130. int counter = event->GetLoopCount();
  1131. if ( counter != -1 )
  1132. {
  1133. int remaining = event->GetNumLoopsRemaining();
  1134. if ( remaining <= 0 )
  1135. {
  1136. process = false;
  1137. }
  1138. else
  1139. {
  1140. event->SetNumLoopsRemaining( --remaining );
  1141. }
  1142. }
  1143. if ( !process )
  1144. return;
  1145. scene->LoopToTime( backtime );
  1146. SetCurrentTime( backtime, true );
  1147. }
  1148. //-----------------------------------------------------------------------------
  1149. // Purpose: Flag the scene as already "completed"
  1150. // Input : *scene -
  1151. // *parameters -
  1152. //-----------------------------------------------------------------------------
  1153. void CSceneEntity::DispatchStopPoint( CChoreoScene *scene, const char *parameters )
  1154. {
  1155. if ( m_bCompletedEarly )
  1156. {
  1157. Assert( 0 );
  1158. Warning( "Scene '%s' with two stop point events!\n", STRING( m_iszSceneFile ) );
  1159. return;
  1160. }
  1161. // Fire completion trigger early
  1162. m_bCompletedEarly = true;
  1163. m_OnCompletion.FireOutput( this, this, 0 );
  1164. }
  1165. //-----------------------------------------------------------------------------
  1166. // Purpose:
  1167. // Output : Returns true on success, false on failure.
  1168. //-----------------------------------------------------------------------------
  1169. bool CSceneEntity::IsInterruptable()
  1170. {
  1171. return ( m_nInterruptCount > 0 ) ? true : false;
  1172. }
  1173. //-----------------------------------------------------------------------------
  1174. // Purpose:
  1175. // Input : *scene -
  1176. // *actor -
  1177. // *event -
  1178. //-----------------------------------------------------------------------------
  1179. void CSceneEntity::DispatchStartInterrupt( CChoreoScene *scene, CChoreoEvent *event )
  1180. {
  1181. // Don't re-interrupt during restore
  1182. if ( m_bRestoring )
  1183. return;
  1184. // If we're supposed to cancel at our next interrupt point, cancel now
  1185. if ( m_bCancelAtNextInterrupt )
  1186. {
  1187. m_bCancelAtNextInterrupt = false;
  1188. LocalScene_Printf( "%s : cancelled via interrupt\n", STRING( m_iszSceneFile ) );
  1189. CancelPlayback();
  1190. return;
  1191. }
  1192. ++m_nInterruptCount;
  1193. }
  1194. //-----------------------------------------------------------------------------
  1195. // Purpose:
  1196. // Input : *scene -
  1197. // *actor -
  1198. // *event -
  1199. //-----------------------------------------------------------------------------
  1200. void CSceneEntity::DispatchEndInterrupt( CChoreoScene *scene, CChoreoEvent *event )
  1201. {
  1202. // Don't re-interrupt during restore
  1203. if ( m_bRestoring )
  1204. return;
  1205. --m_nInterruptCount;
  1206. if ( m_nInterruptCount < 0 )
  1207. {
  1208. m_nInterruptCount = 0;
  1209. }
  1210. }
  1211. //-----------------------------------------------------------------------------
  1212. // Purpose:
  1213. // Input : *actor -
  1214. // *event -
  1215. //-----------------------------------------------------------------------------
  1216. void CSceneEntity::DispatchStartExpression( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event )
  1217. {
  1218. actor->AddSceneEvent( scene, event, NULL, this );
  1219. }
  1220. //-----------------------------------------------------------------------------
  1221. // Purpose:
  1222. // Input : *actor -
  1223. // *event -
  1224. //-----------------------------------------------------------------------------
  1225. void CSceneEntity::DispatchEndExpression( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event )
  1226. {
  1227. actor->RemoveSceneEvent( scene, event, false );
  1228. }
  1229. //-----------------------------------------------------------------------------
  1230. // Purpose:
  1231. // Input : *actor -
  1232. // *event -
  1233. //-----------------------------------------------------------------------------
  1234. void CSceneEntity::DispatchStartFlexAnimation( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event )
  1235. {
  1236. actor->AddSceneEvent( scene, event, NULL, this );
  1237. }
  1238. //-----------------------------------------------------------------------------
  1239. // Purpose:
  1240. // Input : *actor -
  1241. // *event -
  1242. //-----------------------------------------------------------------------------
  1243. void CSceneEntity::DispatchEndFlexAnimation( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event )
  1244. {
  1245. actor->RemoveSceneEvent( scene, event, false );
  1246. }
  1247. //-----------------------------------------------------------------------------
  1248. // Purpose:
  1249. // Input : *actor -
  1250. // *parameters -
  1251. //-----------------------------------------------------------------------------
  1252. void CSceneEntity::DispatchStartGesture( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event )
  1253. {
  1254. // Ingore null gestures
  1255. if ( !Q_stricmp( event->GetName(), "NULL" ) )
  1256. return;
  1257. actor->AddSceneEvent( scene, event, NULL, this );
  1258. }
  1259. //-----------------------------------------------------------------------------
  1260. // Purpose:
  1261. // Input : *actor -
  1262. // *parameters -
  1263. //-----------------------------------------------------------------------------
  1264. void CSceneEntity::DispatchEndGesture( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event )
  1265. {
  1266. // Ingore null gestures
  1267. if ( !Q_stricmp( event->GetName(), "NULL" ) )
  1268. return;
  1269. actor->RemoveSceneEvent( scene, event, m_bRestoring );
  1270. }
  1271. //-----------------------------------------------------------------------------
  1272. // Purpose:
  1273. // Input : *actor -
  1274. // *parameters -
  1275. //-----------------------------------------------------------------------------
  1276. void CSceneEntity::DispatchStartGeneric( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event )
  1277. {
  1278. CBaseEntity *pTarget = FindNamedEntity( event->GetParameters2( ) );
  1279. actor->AddSceneEvent( scene, event, pTarget, this );
  1280. }
  1281. //-----------------------------------------------------------------------------
  1282. // Purpose:
  1283. // Input : *actor -
  1284. // *parameters -
  1285. //-----------------------------------------------------------------------------
  1286. void CSceneEntity::DispatchEndGeneric( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event )
  1287. {
  1288. actor->RemoveSceneEvent( scene, event, m_bRestoring );
  1289. }
  1290. //-----------------------------------------------------------------------------
  1291. // Purpose:
  1292. // Input : *actor -
  1293. // *actor2 -
  1294. //-----------------------------------------------------------------------------
  1295. void CSceneEntity::DispatchStartLookAt( CChoreoScene *scene, CBaseFlex *actor, CBaseEntity *actor2, CChoreoEvent *event )
  1296. {
  1297. actor->AddSceneEvent( scene, event, actor2, this );
  1298. }
  1299. void CSceneEntity::DispatchEndLookAt( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event )
  1300. {
  1301. actor->RemoveSceneEvent( scene, event, m_bRestoring );
  1302. }
  1303. //-----------------------------------------------------------------------------
  1304. // Purpose: Move to spot/actor
  1305. // FIXME: Need to allow this to take arbitrary amount of time and pause playback
  1306. // while waiting for actor to move into position
  1307. // Input : *actor -
  1308. // *parameters -
  1309. //-----------------------------------------------------------------------------
  1310. void CSceneEntity::DispatchStartMoveTo( CChoreoScene *scene, CBaseFlex *actor, CBaseEntity *actor2, CChoreoEvent *event )
  1311. {
  1312. actor->AddSceneEvent( scene, event, actor2, this );
  1313. }
  1314. void CSceneEntity::DispatchEndMoveTo( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event )
  1315. {
  1316. actor->RemoveSceneEvent( scene, event, m_bRestoring );
  1317. }
  1318. //-----------------------------------------------------------------------------
  1319. // Purpose:
  1320. // Input : *token -
  1321. // listener -
  1322. // soundorigins -
  1323. // Output : Returns true on success, false on failure.
  1324. //-----------------------------------------------------------------------------
  1325. bool AttenuateCaption( const char *token, const Vector& listener, CUtlVector< Vector >& soundorigins )
  1326. {
  1327. if ( scene_maxcaptionradius.GetFloat() <= 0.0f )
  1328. {
  1329. return false;
  1330. }
  1331. int c = soundorigins.Count();
  1332. if ( c <= 0 )
  1333. {
  1334. return false;
  1335. }
  1336. float maxdistSqr = scene_maxcaptionradius.GetFloat() * scene_maxcaptionradius.GetFloat();
  1337. for ( int i = 0; i < c; ++i )
  1338. {
  1339. const Vector& org = soundorigins[ i ];
  1340. float distSqr = ( org - listener ).LengthSqr();
  1341. if ( distSqr <= maxdistSqr )
  1342. {
  1343. return false;
  1344. }
  1345. }
  1346. // All sound sources too far, don't show caption...
  1347. return true;
  1348. }
  1349. //-----------------------------------------------------------------------------
  1350. // Purpose:
  1351. // Input : *event - which event
  1352. // player - which recipient
  1353. // buf, buflen: where to put the data
  1354. // Output : Returns true if the sound should be played/prefetched
  1355. //-----------------------------------------------------------------------------
  1356. bool CSceneEntity::GetSoundNameForPlayer( CChoreoEvent *event, CBasePlayer *player, char *buf, size_t buflen )
  1357. {
  1358. Assert( event );
  1359. Assert( player );
  1360. Assert( buf );
  1361. Assert( buflen > 0 );
  1362. bool ismasterevent = true;
  1363. char tok[ CChoreoEvent::MAX_CCTOKEN_STRING ];
  1364. bool validtoken = false;
  1365. tok[ 0 ] = 0;
  1366. if ( event->GetCloseCaptionType() == CChoreoEvent::CC_SLAVE ||
  1367. event->GetCloseCaptionType() == CChoreoEvent::CC_DISABLED )
  1368. {
  1369. ismasterevent = false;
  1370. }
  1371. else
  1372. {
  1373. validtoken = event->GetPlaybackCloseCaptionToken( tok, sizeof( tok ) );
  1374. }
  1375. // For english users, assume we emit the sound normally
  1376. Q_strncpy( buf, event->GetParameters(), buflen );
  1377. bool usingEnglish = true;
  1378. char const *cvarvalue = engine->GetClientConVarValue( player->entindex(), "english" );
  1379. if ( cvarvalue && *cvarvalue && Q_atoi( cvarvalue ) != 1 )
  1380. {
  1381. usingEnglish = false;
  1382. }
  1383. // This makes it like they are running in another language
  1384. if ( scene_forcecombined.GetBool() )
  1385. {
  1386. usingEnglish = false;
  1387. }
  1388. if ( usingEnglish )
  1389. {
  1390. // English sounds always play
  1391. return true;
  1392. }
  1393. if ( ismasterevent )
  1394. {
  1395. // Master event sounds always play too (master will be the combined .wav)
  1396. if ( validtoken )
  1397. {
  1398. Q_strncpy( buf, tok, buflen );
  1399. }
  1400. return true;
  1401. }
  1402. // Slave events don't play any sound...
  1403. return false;
  1404. }
  1405. static CAI_BaseActor *ToBaseActor( CBaseFlex *actor )
  1406. {
  1407. return dynamic_cast<CAI_BaseActor*>( actor );
  1408. }
  1409. //-----------------------------------------------------------------------------
  1410. // Purpose: Playback sound file that contains phonemes
  1411. // Input : *actor -
  1412. // *parameters -
  1413. //-----------------------------------------------------------------------------
  1414. void CSceneEntity::DispatchStartSpeak( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event, soundlevel_t iSoundlevel )
  1415. {
  1416. // Emit sound
  1417. if ( actor )
  1418. {
  1419. CPASAttenuationFilter filter( actor, iSoundlevel );
  1420. // Right now we need to broadcast GLaDOS to all players and then selectively mute the channels
  1421. // if she's only speaking with one player. Because this isn't a general pattern, we've encased
  1422. // it here in an ifdef
  1423. #ifdef PORTAL2
  1424. filter.AddAllPlayers();
  1425. filter.MakeReliable();
  1426. #endif // PORTAL2
  1427. if ( m_pRecipientFilter )
  1428. {
  1429. int filterCount = filter.GetRecipientCount();
  1430. int recipientPlayerCount = m_pRecipientFilter->GetRecipientCount();
  1431. for ( int i = filterCount-1; i >= 0; --i )
  1432. {
  1433. int playerindex = filter.GetRecipientIndex( i );
  1434. bool bFound = false;
  1435. for ( int j = 0; j < recipientPlayerCount; ++j )
  1436. {
  1437. if ( m_pRecipientFilter->GetRecipientIndex(j) == playerindex )
  1438. {
  1439. bFound = true;
  1440. break;
  1441. }
  1442. }
  1443. if ( !bFound )
  1444. {
  1445. filter.RemoveRecipientByPlayerIndex( playerindex );
  1446. }
  1447. }
  1448. }
  1449. // Compensate the sound time, so although we sent the sound later
  1450. // The sound time needs to take this in account or the first samples of the sound will be cut.
  1451. // However due to errors, this is not as important as choreo will start from beginning anyway,
  1452. // we had some issues where short sounds (like 0.2 s) would be played after 0.1s, or even completely skipped.
  1453. float fLatency = scene->GetSoundFileStartupLatency();
  1454. float time_in_past = ( m_flCurrentTime - event->GetStartTime() ) + fLatency;
  1455. float soundtime = gpGlobals->curtime - time_in_past;
  1456. if ( m_bRestoring )
  1457. {
  1458. // Need to queue sounds on restore because the player has not yet connected
  1459. GetSceneManager()->QueueRestoredSound( actor, event->GetParameters(), iSoundlevel, time_in_past );
  1460. actor->AddSceneEvent( scene, event, NULL, this );
  1461. return;
  1462. }
  1463. // Add padding to prevent any other talker talking right after I'm done, because I might
  1464. // be continuing speaking with another scene.
  1465. float flDuration = event->GetDuration() - time_in_past;
  1466. float fPostSpeakDelay = GetPostSpeakDelay() - fLatency;
  1467. CAI_BaseActor *pBaseActor = ToBaseActor( actor );
  1468. if ( pBaseActor )
  1469. {
  1470. pBaseActor->NoteSpeaking( flDuration, fPostSpeakDelay );
  1471. }
  1472. else if ( actor->IsNPC() )
  1473. {
  1474. GetSpeechSemaphore( actor->MyNPCPointer() )->Acquire( flDuration + fPostSpeakDelay, actor );
  1475. }
  1476. EmitSound_t es;
  1477. es.m_nChannel = CHAN_VOICE;
  1478. es.m_flVolume = 1;
  1479. es.m_SoundLevel = iSoundlevel;
  1480. // Only specify exact delay in single player
  1481. es.m_flSoundTime = ( gpGlobals->maxClients == 1 ) ? soundtime : 0.0f;
  1482. es.m_nFlags = SND_UPDATE_DELAY_FOR_CHOREO; // We want to track the delay, so we can adjust choreo accordingly
  1483. // This will also remove any delay when we play the sound.
  1484. // TODO: Consider using two different flags for the sound engine.
  1485. if ( scene->ShouldIgnorePhonemes() )
  1486. {
  1487. es.m_nFlags |= SND_IGNORE_PHONEMES;
  1488. }
  1489. // No CC since we do it manually
  1490. // FIXME: This will change
  1491. es.m_bEmitCloseCaption = false;
  1492. // Captions only route to host player (there is only one closecaptioning HUD)
  1493. filter.RemoveSplitScreenPlayers();
  1494. int c = filter.GetRecipientCount();
  1495. for ( int i = 0; i < c; ++i )
  1496. {
  1497. int playerindex = filter.GetRecipientIndex( i );
  1498. CBasePlayer *player = UTIL_PlayerByIndex( playerindex );
  1499. if ( !player )
  1500. continue;
  1501. CSingleUserRecipientFilter filter2( player );
  1502. char soundname[ 512 ];
  1503. if ( !GetSoundNameForPlayer( event, player, soundname, sizeof( soundname ) ) )
  1504. {
  1505. continue;
  1506. }
  1507. es.m_pSoundName = soundname;
  1508. // keep track of the last few sounds played for bug reports
  1509. speechListSounds[ speechListIndex ].time = gpGlobals->curtime;
  1510. Q_strncpy( speechListSounds[ speechListIndex ].name, soundname, sizeof( speechListSounds[ 0 ].name ) );
  1511. Q_strncpy( speechListSounds[ speechListIndex ].sceneName, ( scene ) ? scene->GetFilename() : "", sizeof( speechListSounds[ 0 ].sceneName ) );
  1512. speechListIndex++;
  1513. if ( speechListIndex >= SPEECH_LIST_MAX_SOUNDS )
  1514. {
  1515. speechListIndex = 0;
  1516. }
  1517. // Warning( "Speak %s\n", soundname );
  1518. if ( m_fPitch != 1.0f )
  1519. {
  1520. if ( es.m_nPitch )
  1521. es.m_nPitch = static_cast<float>( es.m_nPitch ) * m_fPitch;
  1522. else
  1523. es.m_nPitch = 100.0f * m_fPitch;
  1524. es.m_nFlags |= SND_CHANGE_PITCH;
  1525. }
  1526. #if defined ( PORTAL2 )
  1527. if ( GameRules()->IsMultiplayer() == false && GetPlayerHoldingEntity( actor ) )
  1528. {
  1529. // HACK: Don't attenuate player held object sounds
  1530. // This is to fix bugs when walking through portals with the sphere npc.
  1531. es.m_SoundLevel = SNDLVL_NONE;
  1532. }
  1533. #endif
  1534. int nGuid = EmitSound( filter2, actor->entindex(), es );
  1535. if ( nGuid == 0 )
  1536. {
  1537. Warning( "Could not play sound '%s'. Check that the sound exists.\n", soundname );
  1538. }
  1539. actor->AddSceneEvent( scene, event, NULL, this );
  1540. }
  1541. // Close captioning only on master token no matter what...
  1542. if ( event->GetCloseCaptionType() == CChoreoEvent::CC_MASTER )
  1543. {
  1544. char tok[ CChoreoEvent::MAX_CCTOKEN_STRING ];
  1545. bool validtoken = event->GetPlaybackCloseCaptionToken( tok, sizeof( tok ) );
  1546. if ( validtoken )
  1547. {
  1548. char lowercase[ 256 ];
  1549. Q_strncpy( lowercase, tok, sizeof( lowercase ) );
  1550. Q_strlower( lowercase );
  1551. // Remove any players who don't want close captions
  1552. CBaseEntity::RemoveRecipientsIfNotCloseCaptioning( filter );
  1553. // Certain events are marked "don't attenuate", (breencast), skip those here
  1554. if ( !event->IsSuppressingCaptionAttenuation() &&
  1555. ( filter.GetRecipientCount() > 0 ) )
  1556. {
  1557. int c = filter.GetRecipientCount();
  1558. for ( int i = c - 1 ; i >= 0; --i )
  1559. {
  1560. CBasePlayer *player = UTIL_PlayerByIndex( filter.GetRecipientIndex( i ) );
  1561. if ( !player )
  1562. continue;
  1563. Vector playerOrigin = player->GetAbsOrigin();
  1564. if ( iSoundlevel != SNDLVL_NONE && AttenuateCaption( lowercase, playerOrigin, es.m_UtlVecSoundOrigin ) )
  1565. {
  1566. // If the player has a view entity, measure the distance to that
  1567. if ( !player->GetViewEntity() || AttenuateCaption( lowercase, player->GetViewEntity()->GetAbsOrigin(), es.m_UtlVecSoundOrigin ) )
  1568. {
  1569. filter.RemoveRecipient( player );
  1570. }
  1571. }
  1572. }
  1573. }
  1574. // Anyone left?
  1575. if ( filter.GetRecipientCount() > 0 )
  1576. {
  1577. float endtime = event->GetLastSlaveEndTime();
  1578. float durationShort = event->GetDuration();
  1579. float durationLong = endtime - event->GetStartTime();
  1580. float duration = MAX( durationShort, durationLong );
  1581. char const *pszActorModel = STRING( actor->GetModelName() );
  1582. gender_t gender = soundemitterbase->GetActorGender( pszActorModel );
  1583. char lowercase_nogender[ 256 ];
  1584. Q_strncpy( lowercase_nogender, lowercase, sizeof( lowercase_nogender ) );
  1585. bool bTriedGender = false;
  1586. if ( gender == GENDER_MALE )
  1587. {
  1588. Q_strncat( lowercase, "_male", sizeof( lowercase ), COPY_ALL_CHARACTERS );
  1589. bTriedGender = true;
  1590. }
  1591. else if ( gender == GENDER_FEMALE )
  1592. {
  1593. Q_strncat( lowercase, "_female", sizeof( lowercase ), COPY_ALL_CHARACTERS );
  1594. bTriedGender = true;
  1595. }
  1596. unsigned int hash = 0u;
  1597. bool bFound = GetCaptionHash( lowercase, true, hash );
  1598. // if not found, try the no-gender version
  1599. if ( !bFound && bTriedGender )
  1600. {
  1601. bFound = GetCaptionHash( lowercase_nogender, true, hash );
  1602. }
  1603. if ( bFound )
  1604. {
  1605. if ( CanEmitCaption( hash ) )
  1606. {
  1607. CCSUsrMsg_CloseCaption msg;
  1608. msg.set_hash( hash );
  1609. msg.set_duration( clamp( (int)( duration * 10.0f ), 0, 65535 ) );
  1610. msg.set_from_player( 0 );
  1611. // Send caption and duration hint down to client
  1612. SendUserMessage( filter, CS_UM_CloseCaption, msg );
  1613. }
  1614. }
  1615. }
  1616. }
  1617. }
  1618. }
  1619. }
  1620. void CSceneEntity::DispatchEndSpeak( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event )
  1621. {
  1622. actor->RemoveSceneEvent( scene, event, m_bRestoring );
  1623. }
  1624. //-----------------------------------------------------------------------------
  1625. // Purpose:
  1626. // Input : *actor -
  1627. // *actor2 -
  1628. //-----------------------------------------------------------------------------
  1629. void CSceneEntity::DispatchStartFace( CChoreoScene *scene, CBaseFlex *actor, CBaseEntity *actor2, CChoreoEvent *event )
  1630. {
  1631. actor->AddSceneEvent( scene, event, actor2, this );
  1632. }
  1633. //-----------------------------------------------------------------------------
  1634. // Purpose:
  1635. // Input : *actor -
  1636. // *actor2 -
  1637. //-----------------------------------------------------------------------------
  1638. void CSceneEntity::DispatchEndFace( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event )
  1639. {
  1640. actor->RemoveSceneEvent( scene, event, m_bRestoring );
  1641. }
  1642. //-----------------------------------------------------------------------------
  1643. // Purpose:
  1644. // Input : *actor -
  1645. //-----------------------------------------------------------------------------
  1646. void CSceneEntity::DispatchStartSequence( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event )
  1647. {
  1648. actor->AddSceneEvent( scene, event, NULL, this );
  1649. }
  1650. //-----------------------------------------------------------------------------
  1651. // Purpose:
  1652. // Input : *actor -
  1653. //-----------------------------------------------------------------------------
  1654. void CSceneEntity::DispatchEndSequence( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event )
  1655. {
  1656. actor->RemoveSceneEvent( scene, event, m_bRestoring );
  1657. }
  1658. //-----------------------------------------------------------------------------
  1659. // Purpose: NPC can play interstitial vcds (such as responding to the player doing something during a scene)
  1660. // Input : *scene -
  1661. // *actor -
  1662. // *event -
  1663. //-----------------------------------------------------------------------------
  1664. void CSceneEntity::DispatchStartPermitResponses( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event )
  1665. {
  1666. actor->SetPermitResponse( gpGlobals->curtime + event->GetDuration() );
  1667. }
  1668. //-----------------------------------------------------------------------------
  1669. // Purpose:
  1670. // Input : *scene -
  1671. // *actor -
  1672. // *event -
  1673. //-----------------------------------------------------------------------------
  1674. void CSceneEntity::DispatchEndPermitResponses( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event )
  1675. {
  1676. actor->SetPermitResponse( 0 );
  1677. }
  1678. //-----------------------------------------------------------------------------
  1679. // Purpose:
  1680. //-----------------------------------------------------------------------------
  1681. float CSceneEntity::EstimateLength( void )
  1682. {
  1683. if ( !m_pScene )
  1684. {
  1685. return GetSceneDuration( STRING( m_iszSceneFile ) );
  1686. }
  1687. return m_pScene->FindStopTime();
  1688. }
  1689. //-----------------------------------------------------------------------------
  1690. // Purpose:
  1691. // NOTE: returns false if scene hasn't loaded yet
  1692. //-----------------------------------------------------------------------------
  1693. void CSceneEntity::CancelIfSceneInvolvesActor( CBaseEntity *pActor )
  1694. {
  1695. if ( InvolvesActor( pActor ) )
  1696. {
  1697. LocalScene_Printf( "%s : cancelled for '%s'\n", STRING( m_iszSceneFile ), pActor->GetDebugName() );
  1698. CancelPlayback();
  1699. }
  1700. }
  1701. //-----------------------------------------------------------------------------
  1702. // Purpose:
  1703. // NOTE: returns false if scene hasn't loaded yet
  1704. //-----------------------------------------------------------------------------
  1705. bool CSceneEntity::InvolvesActor( CBaseEntity *pActor )
  1706. {
  1707. if ( !m_pScene )
  1708. return false;
  1709. for ( int i = 0 ; i < m_pScene->GetNumActors(); i++ )
  1710. {
  1711. CBaseFlex *pTestActor = FindNamedActor( i );
  1712. if ( !pTestActor )
  1713. continue;
  1714. if ( pTestActor == pActor )
  1715. return true;
  1716. }
  1717. return false;
  1718. }
  1719. //-----------------------------------------------------------------------------
  1720. // Purpose:
  1721. //-----------------------------------------------------------------------------
  1722. void CSceneEntity::DoThink( float frametime )
  1723. {
  1724. CheckInterruptCompletion();
  1725. if ( m_bWaitingForActor || m_bWaitingForInterrupt )
  1726. {
  1727. // Try to start playback.
  1728. StartPlayback();
  1729. }
  1730. if ( !m_pScene )
  1731. return;
  1732. if ( !m_bIsPlayingBack )
  1733. return;
  1734. // catch bad pitch shifting from old save games
  1735. Assert( m_fPitch >= SCENE_MIN_PITCH && m_fPitch <= SCENE_MAX_PITCH );
  1736. m_fPitch = clamp( m_fPitch, SCENE_MIN_PITCH, SCENE_MAX_PITCH );
  1737. if ( m_bPaused )
  1738. {
  1739. PauseThink();
  1740. return;
  1741. }
  1742. // Msg("%.2f %s\n", gpGlobals->curtime, STRING( m_iszSceneFile ) );
  1743. //Msg( "SV: %d, %f for %s\n", gpGlobals->tickcount, m_flCurrentTime, m_pScene->GetFilename() );
  1744. m_flFrameTime = frametime;
  1745. m_pScene->SetSoundFileStartupLatency( GetSoundSystemLatency() );
  1746. // Tell scene to go
  1747. m_pScene->Think( m_flCurrentTime );
  1748. // Did we get to the end
  1749. if ( !m_bPaused )
  1750. {
  1751. // Drive simulation time for scene
  1752. SetCurrentTime( m_flCurrentTime + m_flFrameTime * m_fPitch, false );
  1753. if ( m_pScene->SimulationFinished() )
  1754. {
  1755. OnSceneFinished( false, true );
  1756. // Stop them from doing anything special
  1757. ClearSchedules( m_pScene );
  1758. }
  1759. }
  1760. else
  1761. {
  1762. // Drive simulation time for scene
  1763. SetCurrentTime( m_pScene->GetTime(), true );
  1764. }
  1765. }
  1766. //-----------------------------------------------------------------------------
  1767. // Purpose: Input handlers
  1768. //-----------------------------------------------------------------------------
  1769. void CSceneEntity::InputStartPlayback( inputdata_t &inputdata )
  1770. {
  1771. // Already playing, ignore
  1772. if ( m_bIsPlayingBack )
  1773. return;
  1774. // Already waiting on someone.
  1775. if ( m_bWaitingForActor || m_bWaitingForInterrupt )
  1776. return;
  1777. ClearActivatorTargets();
  1778. m_hActivator = inputdata.pActivator;
  1779. StartPlayback();
  1780. }
  1781. void CSceneEntity::InputPausePlayback( inputdata_t &inputdata )
  1782. {
  1783. PausePlayback();
  1784. m_bPausedViaInput = true;
  1785. }
  1786. void CSceneEntity::InputResumePlayback( inputdata_t &inputdata )
  1787. {
  1788. ResumePlayback();
  1789. }
  1790. void CSceneEntity::InputCancelPlayback( inputdata_t &inputdata )
  1791. {
  1792. LocalScene_Printf( "%s : cancelled via input\n", STRING( m_iszSceneFile ) );
  1793. CancelPlayback();
  1794. }
  1795. void CSceneEntity::InputScriptPlayerDeath( inputdata_t &inputdata )
  1796. {
  1797. if ( m_iPlayerDeathBehavior == SCRIPT_CANCEL )
  1798. {
  1799. LocalScene_Printf( "%s : cancelled via player death\n", STRING( m_iszSceneFile ) );
  1800. CancelPlayback();
  1801. }
  1802. }
  1803. void CSceneEntity::InputSetTarget1( inputdata_t &inputdata )
  1804. {
  1805. m_iszTarget1 = MAKE_STRING( inputdata.value.String() );
  1806. m_hActorList.Purge();
  1807. NetworkProp()->NetworkStateForceUpdate();
  1808. m_hTarget1 = FindNamedTarget( m_iszTarget1, false );
  1809. }
  1810. void CSceneEntity::InputSetTarget2( inputdata_t &inputdata )
  1811. {
  1812. m_iszTarget2 = MAKE_STRING( inputdata.value.String() );
  1813. m_hActorList.Purge();
  1814. NetworkProp()->NetworkStateForceUpdate();
  1815. m_hTarget2 = FindNamedTarget( m_iszTarget2, false );
  1816. }
  1817. void CSceneEntity::InputSetTarget3( inputdata_t &inputdata )
  1818. {
  1819. m_iszTarget3 = MAKE_STRING( inputdata.value.String() );
  1820. m_hActorList.Purge();
  1821. NetworkProp()->NetworkStateForceUpdate();
  1822. m_hTarget3 = FindNamedTarget( m_iszTarget3, false );
  1823. }
  1824. void CSceneEntity::InputSetTarget4( inputdata_t &inputdata )
  1825. {
  1826. m_iszTarget4 = MAKE_STRING( inputdata.value.String() );
  1827. m_hActorList.Purge();
  1828. NetworkProp()->NetworkStateForceUpdate();
  1829. m_hTarget4 = FindNamedTarget( m_iszTarget4, false );
  1830. }
  1831. void CSceneEntity::InputCancelAtNextInterrupt( inputdata_t &inputdata )
  1832. {
  1833. // If we're currently in an interruptable point, interrupt immediately
  1834. if ( IsInterruptable() )
  1835. {
  1836. LocalScene_Printf( "%s : cancelled via input at interrupt point\n", STRING( m_iszSceneFile ) );
  1837. CancelPlayback();
  1838. return;
  1839. }
  1840. // Otherwise, cancel when we next hit an interrupt point
  1841. m_bCancelAtNextInterrupt = true;
  1842. }
  1843. void CSceneEntity::InputPitchShiftPlayback( inputdata_t &inputdata )
  1844. {
  1845. PitchShiftPlayback( inputdata.value.Float() );
  1846. }
  1847. void CSceneEntity::InputTriggerEvent( inputdata_t &inputdata )
  1848. {
  1849. CBaseEntity *pActivator = this; // at some point, find this from the inputdata
  1850. switch ( inputdata.value.Int() )
  1851. {
  1852. case 1:
  1853. m_OnTrigger1.FireOutput( pActivator, this, 0 );
  1854. break;
  1855. case 2:
  1856. m_OnTrigger2.FireOutput( pActivator, this, 0 );
  1857. break;
  1858. case 3:
  1859. m_OnTrigger3.FireOutput( pActivator, this, 0 );
  1860. break;
  1861. case 4:
  1862. m_OnTrigger4.FireOutput( pActivator, this, 0 );
  1863. break;
  1864. case 5:
  1865. m_OnTrigger5.FireOutput( pActivator, this, 0 );
  1866. break;
  1867. case 6:
  1868. m_OnTrigger6.FireOutput( pActivator, this, 0 );
  1869. break;
  1870. case 7:
  1871. m_OnTrigger7.FireOutput( pActivator, this, 0 );
  1872. break;
  1873. case 8:
  1874. m_OnTrigger8.FireOutput( pActivator, this, 0 );
  1875. break;
  1876. case 9:
  1877. m_OnTrigger9.FireOutput( pActivator, this, 0 );
  1878. break;
  1879. case 10:
  1880. m_OnTrigger10.FireOutput( pActivator, this, 0 );
  1881. break;
  1882. case 11:
  1883. m_OnTrigger11.FireOutput( pActivator, this, 0 );
  1884. break;
  1885. case 12:
  1886. m_OnTrigger12.FireOutput( pActivator, this, 0 );
  1887. break;
  1888. case 13:
  1889. m_OnTrigger13.FireOutput( pActivator, this, 0 );
  1890. break;
  1891. case 14:
  1892. m_OnTrigger14.FireOutput( pActivator, this, 0 );
  1893. break;
  1894. case 15:
  1895. m_OnTrigger15.FireOutput( pActivator, this, 0 );
  1896. break;
  1897. case 16:
  1898. m_OnTrigger16.FireOutput( pActivator, this, 0 );
  1899. break;
  1900. }
  1901. }
  1902. struct NPCInterjection
  1903. {
  1904. AI_Response response;
  1905. CAI_BaseActor *npc;
  1906. };
  1907. //-----------------------------------------------------------------------------
  1908. // Purpose:
  1909. // Input : &inputdata -
  1910. //-----------------------------------------------------------------------------
  1911. void CSceneEntity::InputInterjectResponse( inputdata_t &inputdata )
  1912. {
  1913. // Not currently playing a scene
  1914. if ( !m_pScene )
  1915. {
  1916. return;
  1917. }
  1918. CUtlVector< CAI_BaseActor * > candidates;
  1919. int i;
  1920. for ( i = 0 ; i < m_pScene->GetNumActors(); i++ )
  1921. {
  1922. CBaseFlex *pTestActor = FindNamedActor( i );
  1923. if ( !pTestActor )
  1924. continue;
  1925. CAI_BaseActor *pBaseActor = ToBaseActor( pTestActor );
  1926. if ( !pBaseActor )
  1927. continue;
  1928. if ( !pBaseActor->IsAlive() )
  1929. continue;
  1930. candidates.AddToTail( pBaseActor );
  1931. }
  1932. int c = candidates.Count();
  1933. if ( !c )
  1934. {
  1935. return;
  1936. }
  1937. int useIndex = 0;
  1938. if ( !m_bIsPlayingBack )
  1939. {
  1940. // Use any actor if not playing a scene
  1941. useIndex = RandomInt( 0, c - 1 );
  1942. }
  1943. else
  1944. {
  1945. CUtlVector< NPCInterjection > validResponses;
  1946. char modifiers[ 512 ];
  1947. Q_snprintf( modifiers, sizeof( modifiers ), "scene:%s", STRING( GetEntityName() ) );
  1948. AIConcept_t concept(inputdata.value.String());
  1949. for ( int i = 0; i < c; i++ )
  1950. {
  1951. CAI_BaseActor *npc = candidates[ i ];
  1952. Assert( npc );
  1953. AI_CriteriaSet set;
  1954. npc->GatherCriteria( &set, concept, modifiers );
  1955. AI_Response response;
  1956. if ( !npc->FindResponse( response, concept, &set ) )
  1957. continue;
  1958. float duration = npc->GetResponseDuration( &response );
  1959. // Couldn't look it up
  1960. if ( duration <= 0.0f )
  1961. continue;
  1962. if ( !npc->PermitResponse( duration ) )
  1963. {
  1964. continue;
  1965. }
  1966. //
  1967. NPCInterjection inter;
  1968. inter.response = response;
  1969. inter.npc = npc;
  1970. validResponses.AddToTail( inter );
  1971. }
  1972. int rcount = validResponses.Count();
  1973. if ( rcount >= 1 )
  1974. {
  1975. int slot = RandomInt( 0, rcount - 1 );
  1976. for ( int i = 0; i < rcount; i++ )
  1977. {
  1978. NPCInterjection *pInterjection = &validResponses[ i ];
  1979. if ( i == slot )
  1980. {
  1981. pInterjection->npc->SpeakDispatchResponse( inputdata.value.String(), &pInterjection->response, NULL );
  1982. }
  1983. else
  1984. {
  1985. // delete pInterjection->response;
  1986. }
  1987. }
  1988. }
  1989. }
  1990. }
  1991. //-----------------------------------------------------------------------------
  1992. //-----------------------------------------------------------------------------
  1993. void CSceneEntity::InputStopWaitingForActor( inputdata_t &inputdata )
  1994. {
  1995. if( m_bIsPlayingBack )
  1996. {
  1997. // Already started.
  1998. return;
  1999. }
  2000. m_bWaitingForActor = false;
  2001. }
  2002. bool CSceneEntity::CheckActors()
  2003. {
  2004. Assert( m_pScene );
  2005. if ( !m_pScene )
  2006. return false;
  2007. int i;
  2008. for ( i = 0 ; i < m_pScene->GetNumActors(); i++ )
  2009. {
  2010. CBaseFlex *pTestActor = FindNamedActor( i );
  2011. if ( !pTestActor )
  2012. continue;
  2013. if ( !pTestActor->MyCombatCharacterPointer() )
  2014. continue;
  2015. if ( !pTestActor->MyCombatCharacterPointer()->IsAlive() )
  2016. return false;
  2017. if ( m_BusyActor == SCENE_BUSYACTOR_WAIT )
  2018. {
  2019. CAI_BaseNPC *pActor = pTestActor->MyNPCPointer();
  2020. if ( pActor )
  2021. {
  2022. bool bShouldWait = false;
  2023. if ( hl2_episodic.GetBool() )
  2024. {
  2025. // Episodic waits until the NPC is fully finished with any .vcd with speech in it
  2026. if ( IsRunningScriptedSceneWithSpeech( pActor ) )
  2027. {
  2028. bShouldWait = true;
  2029. }
  2030. #ifdef HL2_EPISODIC
  2031. // HACK: Alyx cannot play scenes when she's in the middle of transitioning
  2032. if ( pActor->IsInAVehicle() )
  2033. {
  2034. CNPC_Alyx *pAlyx = dynamic_cast<CNPC_Alyx *>(pActor);
  2035. if ( pAlyx != NULL && ( pAlyx->GetPassengerState() == PASSENGER_STATE_ENTERING || pAlyx->GetPassengerState() == PASSENGER_STATE_EXITING ) )
  2036. {
  2037. bShouldWait = true;
  2038. }
  2039. }
  2040. #endif // HL2_EPISODIC
  2041. }
  2042. if ( pActor->GetExpresser() && pActor->GetExpresser()->IsSpeaking() )
  2043. {
  2044. bShouldWait = true;
  2045. }
  2046. if ( bShouldWait )
  2047. {
  2048. // One of the actors for this scene is talking already.
  2049. // Try again next think.
  2050. m_bWaitingForActor = true;
  2051. return false;
  2052. }
  2053. }
  2054. }
  2055. else if ( m_BusyActor == SCENE_BUSYACTOR_INTERRUPT || m_BusyActor == SCENE_BUSYACTOR_INTERRUPT_CANCEL )
  2056. {
  2057. CBaseCombatCharacter *pActor = pTestActor->MyCombatCharacterPointer();
  2058. if ( pActor && !IsInInterruptableScenes( pActor ) )
  2059. {
  2060. // One of the actors is in a scene that's not at an interrupt point.
  2061. // Wait until the scene finishes or an interrupt point is reached.
  2062. m_bWaitingForInterrupt = true;
  2063. return false;
  2064. }
  2065. if ( m_BusyActor == SCENE_BUSYACTOR_INTERRUPT_CANCEL )
  2066. {
  2067. // Cancel existing scenes
  2068. RemoveActorFromScriptedScenes( pActor, false );
  2069. }
  2070. else
  2071. {
  2072. // Pause existing scenes
  2073. PauseActorsScriptedScenes( pActor, false );
  2074. m_bInterruptedActorsScenes = true;
  2075. }
  2076. }
  2077. pTestActor->StartChoreoScene( m_pScene );
  2078. }
  2079. return true;
  2080. }
  2081. static ConVar scene_async_prefetch_spew( "scene_async_prefetch_spew", "0", 0, "Display async .ani file loading info." );
  2082. void CSceneEntity::PrefetchAnimBlocks( CChoreoScene *scene )
  2083. {
  2084. Assert( scene );
  2085. // Build a fast lookup, too
  2086. CUtlMap< CChoreoActor *, CBaseFlex *> actorMap( 0, 0, DefLessFunc( CChoreoActor * ) );
  2087. int spew = scene_async_prefetch_spew.GetInt();
  2088. int resident = 0;
  2089. int checked = 0;
  2090. MDLCACHE_CRITICAL_SECTION();
  2091. // Iterate events and precache necessary resources
  2092. for ( int i = 0; i < scene->GetNumEvents(); i++ )
  2093. {
  2094. CChoreoEvent *event = scene->GetEvent( i );
  2095. if ( !event )
  2096. continue;
  2097. // load any necessary data
  2098. switch ( event->GetType() )
  2099. {
  2100. default:
  2101. break;
  2102. case CChoreoEvent::SEQUENCE:
  2103. case CChoreoEvent::GESTURE:
  2104. {
  2105. CChoreoActor *actor = event->GetActor();
  2106. if ( actor )
  2107. {
  2108. CBaseFlex *pActor = NULL;
  2109. int idx = actorMap.Find( actor );
  2110. if ( idx == actorMap.InvalidIndex() )
  2111. {
  2112. pActor = FindNamedActor( actor );
  2113. idx = actorMap.Insert( actor, pActor );
  2114. }
  2115. else
  2116. {
  2117. pActor = actorMap[ idx ];
  2118. }
  2119. if ( pActor )
  2120. {
  2121. int seq = pActor->LookupSequence( event->GetParameters() );
  2122. if ( seq >= 0 )
  2123. {
  2124. CStudioHdr *pStudioHdr = pActor->GetModelPtr();
  2125. if ( pStudioHdr )
  2126. {
  2127. // Now look up the animblock
  2128. mstudioseqdesc_t &seqdesc = pStudioHdr->pSeqdesc( seq );
  2129. for ( int i = 0 ; i < seqdesc.groupsize[ 0 ] ; ++i )
  2130. {
  2131. for ( int j = 0; j < seqdesc.groupsize[ 1 ]; ++j )
  2132. {
  2133. int animation = seqdesc.anim( i, j );
  2134. int baseanimation = pStudioHdr->iRelativeAnim( seq, animation );
  2135. mstudioanimdesc_t &animdesc = pStudioHdr->pAnimdesc( baseanimation );
  2136. ++checked;
  2137. if ( spew != 0 )
  2138. {
  2139. Msg( "%s checking block %d\n", pStudioHdr->pszName(), animdesc.animblock );
  2140. }
  2141. // Async load the animation
  2142. int iFrame = 0;
  2143. const byte *panim = animdesc.pAnim( &iFrame );
  2144. if ( panim )
  2145. {
  2146. ++resident;
  2147. if ( spew > 1 )
  2148. {
  2149. Msg( "%s:%s[%i:%i] was resident\n", pStudioHdr->pszName(), animdesc.pszName(), i, j );
  2150. }
  2151. }
  2152. else
  2153. {
  2154. if ( spew != 0 )
  2155. {
  2156. Msg( "%s:%s[%i:%i] async load\n", pStudioHdr->pszName(), animdesc.pszName(), i, j );
  2157. }
  2158. }
  2159. }
  2160. }
  2161. }
  2162. }
  2163. }
  2164. }
  2165. }
  2166. break;
  2167. }
  2168. }
  2169. if ( !spew || checked <= 0 )
  2170. return;
  2171. Msg( "%d of %d animations resident\n", resident, checked );
  2172. }
  2173. void CSceneEntity::OnLoaded()
  2174. {
  2175. m_bMultiplayer = gpGlobals->maxClients > 1;
  2176. }
  2177. //-----------------------------------------------------------------------------
  2178. // Purpose: Initiate scene playback
  2179. //-----------------------------------------------------------------------------
  2180. void CSceneEntity::StartPlayback( void )
  2181. {
  2182. if ( !m_pScene )
  2183. {
  2184. if ( m_bSceneMissing )
  2185. return;
  2186. m_pScene = LoadScene( STRING( m_iszSceneFile ), this );
  2187. if ( !m_pScene )
  2188. {
  2189. DevMsg( "%s missing from scenes.image\n", STRING( m_iszSceneFile ) );
  2190. m_bSceneMissing = true;
  2191. return;
  2192. }
  2193. if ( GetSceneManager() )
  2194. {
  2195. GetSceneManager()->ActivateScene( this );
  2196. }
  2197. OnLoaded();
  2198. if ( ShouldNetwork() )
  2199. {
  2200. m_nSceneStringIndex = g_pStringTableClientSideChoreoScenes->AddString( CBaseEntity::IsServer(), STRING( m_iszSceneFile ) );
  2201. }
  2202. UpdateTransmitState();
  2203. }
  2204. if ( m_bIsPlayingBack )
  2205. return;
  2206. // Make sure actors are alive and able to handle this scene now, otherwise
  2207. // we'll wait for them to show up
  2208. if ( !CheckActors() )
  2209. {
  2210. return;
  2211. }
  2212. m_bCompletedEarly = false;
  2213. m_bWaitingForActor = false;
  2214. m_bWaitingForInterrupt = false;
  2215. m_bIsPlayingBack = true;
  2216. NetworkProp()->NetworkStateForceUpdate();
  2217. m_bPaused = false;
  2218. SetCurrentTime( 0.0f, true );
  2219. m_pScene->ResetSimulation();
  2220. ClearInterrupt();
  2221. // Put face back in neutral pose
  2222. ClearSceneEvents( m_pScene, false );
  2223. m_OnStart.FireOutput( this, this, 0 );
  2224. // Aysnchronously load speak sounds
  2225. CUtlSymbolTable prefetchSoundSymbolTable;
  2226. CUtlRBTree< SpeakEventSound_t > soundnames( 0, 0, SpeakEventSoundLessFunc );
  2227. BuildSortedSpeakEventSoundsPrefetchList( m_pScene, prefetchSoundSymbolTable, soundnames, 0.0f );
  2228. PrefetchSpeakEventSounds( prefetchSoundSymbolTable, soundnames );
  2229. // Tell any managers we're within that we've started
  2230. int c = m_hListManagers.Count();
  2231. for ( int i = 0; i < c; i++ )
  2232. {
  2233. if ( m_hListManagers[i] )
  2234. {
  2235. m_hListManagers[i]->SceneStarted( this );
  2236. }
  2237. }
  2238. PrefetchAnimBlocks( m_pScene );
  2239. }
  2240. //-----------------------------------------------------------------------------
  2241. // Purpose: Static method used to sort by event start time
  2242. //-----------------------------------------------------------------------------
  2243. bool CSceneEntity::SpeakEventSoundLessFunc( const SpeakEventSound_t& lhs, const SpeakEventSound_t& rhs )
  2244. {
  2245. return lhs.m_flStartTime < rhs.m_flStartTime;
  2246. }
  2247. //-----------------------------------------------------------------------------
  2248. // Purpose: Prefetches the list of sounds build by BuildSortedSpeakEventSoundsPrefetchList
  2249. //-----------------------------------------------------------------------------
  2250. void CSceneEntity::PrefetchSpeakEventSounds( CUtlSymbolTable& table, CUtlRBTree< SpeakEventSound_t >& soundnames )
  2251. {
  2252. for ( int i = soundnames.FirstInorder(); i != soundnames.InvalidIndex() ; i = soundnames.NextInorder( i ) )
  2253. {
  2254. SpeakEventSound_t& sound = soundnames[ i ];
  2255. // Look it up in the string table
  2256. char const *soundname = table.String( sound.m_Symbol );
  2257. // Warning( "Prefetch %s\n", soundname );
  2258. PrefetchScriptSound( soundname );
  2259. }
  2260. }
  2261. //-----------------------------------------------------------------------------
  2262. // Purpose: Builds list of sounds sorted by start time for prefetching
  2263. //-----------------------------------------------------------------------------
  2264. void CSceneEntity::BuildSortedSpeakEventSoundsPrefetchList(
  2265. CChoreoScene *scene,
  2266. CUtlSymbolTable& table,
  2267. CUtlRBTree< SpeakEventSound_t >& soundnames,
  2268. float timeOffset )
  2269. {
  2270. Assert( scene );
  2271. // Iterate events and precache necessary resources
  2272. for ( int i = 0; i < scene->GetNumEvents(); i++ )
  2273. {
  2274. CChoreoEvent *event = scene->GetEvent( i );
  2275. if ( !event )
  2276. continue;
  2277. // load any necessary data
  2278. switch (event->GetType() )
  2279. {
  2280. default:
  2281. break;
  2282. case CChoreoEvent::SPEAK:
  2283. {
  2284. // NOTE: The script entries associated with .vcds are forced to preload to avoid
  2285. // loading hitches during triggering
  2286. char soundname[ CChoreoEvent::MAX_CCTOKEN_STRING ];
  2287. Q_strncpy( soundname, event->GetParameters(), sizeof( soundname ) );
  2288. if ( event->GetCloseCaptionType() == CChoreoEvent::CC_MASTER )
  2289. {
  2290. event->GetPlaybackCloseCaptionToken( soundname, sizeof( soundname ) );
  2291. }
  2292. // In single player, try to use the combined or regular .wav files as needed
  2293. if ( gpGlobals->maxClients == 1 )
  2294. {
  2295. CBasePlayer *player = UTIL_GetLocalPlayer();
  2296. if ( player && !GetSoundNameForPlayer( event, player, soundname, sizeof( soundname ) ) )
  2297. {
  2298. // Skip to next event
  2299. continue;
  2300. }
  2301. }
  2302. /*
  2303. else
  2304. {
  2305. // UNDONE: Probably need some other solution in multiplayer... (not sure how to "prefetch" on certain players
  2306. // with one sound, but not prefetch the same sound for others...)
  2307. }
  2308. */
  2309. SpeakEventSound_t ses;
  2310. ses.m_Symbol = table.AddString( soundname );
  2311. ses.m_flStartTime = timeOffset + event->GetStartTime();
  2312. soundnames.Insert( ses );
  2313. }
  2314. break;
  2315. case CChoreoEvent::SUBSCENE:
  2316. {
  2317. // Only allow a single level of subscenes for now
  2318. if ( !scene->IsSubScene() )
  2319. {
  2320. CChoreoScene *subscene = event->GetSubScene();
  2321. if ( !subscene )
  2322. {
  2323. subscene = LoadScene( event->GetParameters(), this );
  2324. subscene->SetSubScene( true );
  2325. event->SetSubScene( subscene );
  2326. // Now precache it's resources, if any
  2327. BuildSortedSpeakEventSoundsPrefetchList( subscene, table, soundnames, event->GetStartTime() );
  2328. }
  2329. }
  2330. }
  2331. break;
  2332. }
  2333. }
  2334. }
  2335. //-----------------------------------------------------------------------------
  2336. // Purpose:
  2337. //-----------------------------------------------------------------------------
  2338. void CSceneEntity::PausePlayback( void )
  2339. {
  2340. if ( !m_bIsPlayingBack )
  2341. return;
  2342. if ( m_bPaused )
  2343. return;
  2344. m_bPaused = true;
  2345. }
  2346. //-----------------------------------------------------------------------------
  2347. // Purpose:
  2348. //-----------------------------------------------------------------------------
  2349. void CSceneEntity::ResumePlayback( void )
  2350. {
  2351. if ( !m_bIsPlayingBack )
  2352. return;
  2353. if ( !m_bPaused )
  2354. return;
  2355. Assert( m_pScene );
  2356. if ( !m_pScene )
  2357. {
  2358. // This should never happen!!!!
  2359. return;
  2360. }
  2361. // FIXME: Iterate using m_pScene->IterateResumeConditionEvents and
  2362. // only resume if the event conditions have all been satisfied
  2363. // FIXME: Just resume for now
  2364. m_pScene->ResumeSimulation();
  2365. m_bPaused = false;
  2366. m_bPausedViaInput = false;
  2367. }
  2368. //-----------------------------------------------------------------------------
  2369. // Purpose:
  2370. //-----------------------------------------------------------------------------
  2371. void CSceneEntity::CancelPlayback( void )
  2372. {
  2373. if ( !m_bIsPlayingBack )
  2374. return;
  2375. m_bIsPlayingBack = false;
  2376. m_bPaused = false;
  2377. m_OnCanceled.FireOutput( this, this, 0 );
  2378. LocalScene_Printf( "%s : %8.2f: canceled\n", STRING( m_iszSceneFile ), m_flCurrentTime );
  2379. OnSceneFinished( true, false );
  2380. }
  2381. void CSceneEntity::PitchShiftPlayback( float fPitch )
  2382. {
  2383. fPitch = clamp( fPitch, SCENE_MIN_PITCH, SCENE_MAX_PITCH );
  2384. m_fPitch = fPitch;
  2385. if ( !m_pScene )
  2386. return;
  2387. for ( int iActor = 0 ; iActor < m_pScene->GetNumActors(); ++iActor )
  2388. {
  2389. CBaseFlex *pTestActor = FindNamedActor( iActor );
  2390. if ( !pTestActor )
  2391. continue;
  2392. char szBuff[ 256 ];
  2393. if ( m_pScene->GetPlayingSoundName( szBuff, sizeof( szBuff ) ) )
  2394. {
  2395. CPASAttenuationFilter filter( pTestActor );
  2396. EmitSound_t params;
  2397. params.m_pSoundName = szBuff;
  2398. params.m_nPitch = 100.0f * fPitch;
  2399. params.m_nFlags = SND_CHANGE_PITCH;
  2400. #if defined ( PORTAL2 )
  2401. if ( GameRules()->IsMultiplayer() == false && GetPlayerHoldingEntity( pTestActor ) )
  2402. {
  2403. // HACK: Don't attenuate player held object sounds
  2404. // This is to fix bugs when walking through portals with the sphere npc.
  2405. params.m_SoundLevel = SNDLVL_NONE;
  2406. }
  2407. #endif
  2408. pTestActor->EmitSound( filter, pTestActor->entindex(), params );
  2409. }
  2410. }
  2411. }
  2412. //-----------------------------------------------------------------------------
  2413. // Purpose: Start a resume scene, if we have one, and resume playing when it finishes
  2414. //-----------------------------------------------------------------------------
  2415. void CSceneEntity::QueueResumePlayback( void )
  2416. {
  2417. // Do we have a resume scene?
  2418. if ( m_iszResumeSceneFile != NULL_STRING )
  2419. {
  2420. bool bStartedScene = false;
  2421. // If it has ".vcd" somewhere in the string, try using it as a scene file first
  2422. if ( Q_stristr( STRING(m_iszResumeSceneFile), ".vcd" ) )
  2423. {
  2424. bStartedScene = InstancedScriptedScene( NULL, STRING(m_iszResumeSceneFile), &m_hWaitingForThisResumeScene, 0, false ) != 0;
  2425. }
  2426. // HACKHACK: For now, get the first target, and see if we can find a response for him
  2427. if ( !bStartedScene )
  2428. {
  2429. CBaseFlex *pActor = FindNamedActor( 0 );
  2430. if ( pActor )
  2431. {
  2432. CAI_BaseActor *pBaseActor = ToBaseActor( pActor );
  2433. if ( pBaseActor )
  2434. {
  2435. AI_Response result ;
  2436. AIConcept_t tmpConcept( STRING( m_iszResumeSceneFile ) );
  2437. if ( pBaseActor->FindResponse( result, tmpConcept, NULL ) )
  2438. {
  2439. char response[ 256 ];
  2440. result.GetResponse( response, sizeof( response ) );
  2441. bStartedScene = InstancedScriptedScene( NULL, response, &m_hWaitingForThisResumeScene, 0, false ) != 0;
  2442. }
  2443. }
  2444. }
  2445. }
  2446. // If we started a scene/response, wait for it to finish
  2447. if ( bStartedScene )
  2448. {
  2449. m_bWaitingForResumeScene = true;
  2450. }
  2451. else
  2452. {
  2453. // Failed to create the scene. Resume immediately.
  2454. ResumePlayback();
  2455. }
  2456. }
  2457. else
  2458. {
  2459. // No resume scene, so just resume immediately
  2460. ResumePlayback();
  2461. }
  2462. }
  2463. //-----------------------------------------------------------------------------
  2464. // Purpose: Query whether the scene actually loaded. Only meaninful after Spawn()
  2465. //-----------------------------------------------------------------------------
  2466. bool CSceneEntity::ValidScene() const
  2467. {
  2468. return ( m_pScene != NULL );
  2469. }
  2470. //-----------------------------------------------------------------------------
  2471. // Purpose:
  2472. // Input : *pActor -
  2473. // *scene -
  2474. // *event -
  2475. //-----------------------------------------------------------------------------
  2476. void CSceneEntity::DispatchStartSubScene( CChoreoScene *scene, CBaseFlex *pActor, CChoreoEvent *event)
  2477. {
  2478. if ( !scene->IsSubScene() )
  2479. {
  2480. CChoreoScene *subscene = event->GetSubScene();
  2481. if ( !subscene )
  2482. {
  2483. Assert( 0 );
  2484. /*
  2485. subscene = LoadScene( event->GetParameters() );
  2486. subscene->SetSubScene( true );
  2487. event->SetSubScene( subscene );
  2488. */
  2489. }
  2490. if ( subscene )
  2491. {
  2492. subscene->ResetSimulation();
  2493. }
  2494. }
  2495. }
  2496. //-----------------------------------------------------------------------------
  2497. // Purpose: All events are leading edge triggered
  2498. // Input : currenttime -
  2499. // *event -
  2500. //-----------------------------------------------------------------------------
  2501. void CSceneEntity::StartEvent( float currenttime, CChoreoScene *scene, CChoreoEvent *event )
  2502. {
  2503. Assert( event );
  2504. if ( !Q_stricmp( event->GetName(), "NULL" ) )
  2505. {
  2506. LocalScene_Printf( "%s : %8.2f: ignored %s\n", STRING( m_iszSceneFile ), currenttime, event->GetDescription() );
  2507. return;
  2508. }
  2509. CBaseFlex *pActor = NULL;
  2510. CChoreoActor *actor = event->GetActor();
  2511. if ( actor && (event->GetType() != CChoreoEvent::SCRIPT) && (event->GetType() != CChoreoEvent::CAMERA) )
  2512. {
  2513. pActor = FindNamedActor( actor );
  2514. if (pActor == NULL)
  2515. {
  2516. Warning( "CSceneEntity %s unable to find actor named \"%s\"\n", STRING(GetEntityName()), actor->GetName() );
  2517. return;
  2518. }
  2519. }
  2520. LocalScene_Printf( "%s : %8.2f: start %s\n", STRING( m_iszSceneFile ), currenttime, event->GetDescription() );
  2521. switch ( event->GetType() )
  2522. {
  2523. case CChoreoEvent::SUBSCENE:
  2524. {
  2525. if ( pActor && !IsMultiplayer() )
  2526. {
  2527. DispatchStartSubScene( scene, pActor, event );
  2528. }
  2529. }
  2530. break;
  2531. case CChoreoEvent::EXPRESSION:
  2532. {
  2533. if ( pActor && !IsMultiplayer() )
  2534. {
  2535. DispatchStartExpression( scene, pActor, event );
  2536. }
  2537. }
  2538. break;
  2539. case CChoreoEvent::FLEXANIMATION:
  2540. {
  2541. if ( pActor && !IsMultiplayer() )
  2542. {
  2543. DispatchStartFlexAnimation( scene, pActor, event );
  2544. }
  2545. }
  2546. break;
  2547. case CChoreoEvent::LOOKAT:
  2548. {
  2549. if ( pActor && !IsMultiplayer() )
  2550. {
  2551. CBaseEntity *pActor2 = FindNamedEntity( event->GetParameters( ), pActor );
  2552. if ( pActor2 )
  2553. {
  2554. // Huh?
  2555. DispatchStartLookAt( scene, pActor, pActor2, event );
  2556. }
  2557. else
  2558. {
  2559. Warning( "CSceneEntity %s unable to find actor named \"%s\"\n", STRING(GetEntityName()), event->GetParameters() );
  2560. }
  2561. }
  2562. }
  2563. break;
  2564. case CChoreoEvent::SPEAK:
  2565. {
  2566. if ( pActor )
  2567. {
  2568. // Speaking is edge triggered
  2569. // FIXME: dB hack. soundlevel needs to be moved into inside of wav?
  2570. soundlevel_t iSoundlevel = SNDLVL_TALKING;
  2571. if (event->GetParameters2())
  2572. {
  2573. iSoundlevel = (soundlevel_t)atoi( event->GetParameters2() );
  2574. }
  2575. DispatchStartSpeak( scene, pActor, event, iSoundlevel );
  2576. }
  2577. }
  2578. break;
  2579. case CChoreoEvent::MOVETO:
  2580. {
  2581. // FIXME: make sure moveto's aren't edge triggered
  2582. if ( !event->HasEndTime() )
  2583. {
  2584. event->SetEndTime( event->GetStartTime() + 1.0 );
  2585. }
  2586. if ( pActor && !IsMultiplayer() )
  2587. {
  2588. CBaseEntity *pActor2 = NULL;
  2589. if ( event->GetParameters3( ) && strlen( event->GetParameters3( ) ) > 0 )
  2590. {
  2591. pActor2 = FindNamedEntityClosest( event->GetParameters( ), pActor, false, true, event->GetParameters3( ) );
  2592. }
  2593. else
  2594. {
  2595. pActor2 = FindNamedEntity( event->GetParameters( ), pActor, false, true );
  2596. }
  2597. if ( pActor2 )
  2598. {
  2599. DispatchStartMoveTo( scene, pActor, pActor2, event );
  2600. }
  2601. else
  2602. {
  2603. Warning( "CSceneEntity %s unable to find actor named \"%s\"\n", STRING(GetEntityName()), event->GetParameters() );
  2604. }
  2605. }
  2606. }
  2607. break;
  2608. case CChoreoEvent::FACE:
  2609. {
  2610. if ( pActor && !IsMultiplayer() )
  2611. {
  2612. CBaseEntity *pActor2 = FindNamedEntity( event->GetParameters( ), pActor );
  2613. if ( pActor2 )
  2614. {
  2615. DispatchStartFace( scene, pActor, pActor2, event );
  2616. }
  2617. else
  2618. {
  2619. Warning( "CSceneEntity %s unable to find actor named \"%s\"\n", STRING(GetEntityName()), event->GetParameters() );
  2620. }
  2621. }
  2622. }
  2623. break;
  2624. case CChoreoEvent::GESTURE:
  2625. {
  2626. if ( pActor )
  2627. {
  2628. DispatchStartGesture( scene, pActor, event );
  2629. }
  2630. }
  2631. break;
  2632. case CChoreoEvent::GENERIC:
  2633. {
  2634. // If the first token in the parameters is "debugtext", print the rest of the text
  2635. if ( event->GetParameters() && StringHasPrefixCaseSensitive( event->GetParameters(), "debugtext" ) )
  2636. {
  2637. const char *pszText = event->GetParameters() + 10;
  2638. hudtextparms_s tTextParam;
  2639. tTextParam.x = -1;
  2640. tTextParam.y = 0.65;
  2641. tTextParam.effect = 0;
  2642. tTextParam.r1 = 255;
  2643. tTextParam.g1 = 170;
  2644. tTextParam.b1 = 0;
  2645. tTextParam.a1 = 255;
  2646. tTextParam.r2 = 255;
  2647. tTextParam.g2 = 170;
  2648. tTextParam.b2 = 0;
  2649. tTextParam.a2 = 255;
  2650. tTextParam.fadeinTime = 0;
  2651. tTextParam.fadeoutTime = 0;
  2652. tTextParam.holdTime = 3.1;
  2653. tTextParam.fxTime = 0;
  2654. tTextParam.channel = 1;
  2655. UTIL_HudMessageAll( tTextParam, pszText );
  2656. break;
  2657. }
  2658. if ( pActor )
  2659. {
  2660. DispatchStartGeneric( scene, pActor, event );
  2661. }
  2662. }
  2663. break;
  2664. case CChoreoEvent::CAMERA:
  2665. {
  2666. // begin the camera shot
  2667. const char *pszShotType = event->GetParameters();
  2668. CBaseEntity *pActor1 = FindNamedEntity( event->GetParameters2( ), pActor );
  2669. CBaseEntity *pActor2 = FindNamedEntity( event->GetParameters3( ), pActor );
  2670. float duration = event->GetDuration();
  2671. // grab any camera we find in the map
  2672. // TODO: find camera that is nearest this scene entity?
  2673. CTriggerCamera *pCamera = (CTriggerCamera *)gEntList.FindEntityByClassname( NULL, "point_viewcontrol" );
  2674. if ( !pCamera )
  2675. {
  2676. Warning( "CSceneEntity %s unable to find a camera (point_viewcontrol) in this map!\n", STRING(GetEntityName()) );
  2677. }
  2678. else
  2679. {
  2680. pCamera->StartCameraShot( pszShotType, this, pActor1, pActor2, duration );
  2681. }
  2682. }
  2683. break;
  2684. case CChoreoEvent::SCRIPT:
  2685. {
  2686. // NOTE: this is only used by auto-generated vcds to embed script commands to map entities.
  2687. // vscript call - param1 is entity name, param2 is function name, param3 is function parameter string
  2688. // calls a vscript function defined on the scope of the named CBaseEntity object/actor.
  2689. // script call is of the format FunctionName(pActor, pThisSceneEntity, pszScriptParameters, duration)
  2690. const char *pszActorName = event->GetParameters();
  2691. const char *pszFunctionName = event->GetParameters2();
  2692. const char *pszScriptParameters = event->GetParameters3();
  2693. float duration = event->GetDuration();
  2694. // TODO: should be new method CBaseEntity::CallScriptFunctionParams()
  2695. CBaseEntity *pEntity = (CBaseEntity *)gEntList.FindEntityByName( NULL, pszActorName );
  2696. //START_VMPROFILE
  2697. if ( !pEntity )
  2698. {
  2699. Warning( "CSceneEntity::SCRIPT event - unable to find entity named '%s' in this map!\n", pszActorName );
  2700. }
  2701. else
  2702. {
  2703. if( !pEntity->ValidateScriptScope() )
  2704. {
  2705. DevMsg("\n***\nCChoreoEvent::SCRIPT - FAILED to create private ScriptScope. ABORTING script call\n***\n");
  2706. break;
  2707. }
  2708. HSCRIPT hFunc = pEntity->m_ScriptScope.LookupFunction( pszFunctionName );
  2709. if( hFunc )
  2710. {
  2711. pEntity->m_ScriptScope.Call( hFunc, NULL, ToHScript(this), pszScriptParameters, duration );
  2712. pEntity->m_ScriptScope.ReleaseFunction( hFunc );
  2713. //UPDATE_VMPROFILE
  2714. }
  2715. else
  2716. {
  2717. Warning("CSceneEntity::SCRIPT event - '%s' entity has no script function '%s' defined!\n", pszActorName,pszFunctionName);
  2718. }
  2719. }
  2720. }
  2721. break;
  2722. case CChoreoEvent::FIRETRIGGER:
  2723. {
  2724. // Don't re-fire triggers during restore, the entities should already reflect all such state...
  2725. if ( m_bRestoring )
  2726. {
  2727. break;
  2728. }
  2729. CBaseEntity *pActivator = pActor;
  2730. if (!pActivator)
  2731. {
  2732. pActivator = this;
  2733. }
  2734. // FIXME: how do I decide who fired it??
  2735. switch( atoi( event->GetParameters() ) )
  2736. {
  2737. case 1:
  2738. m_OnTrigger1.FireOutput( pActivator, this, 0 );
  2739. break;
  2740. case 2:
  2741. m_OnTrigger2.FireOutput( pActivator, this, 0 );
  2742. break;
  2743. case 3:
  2744. m_OnTrigger3.FireOutput( pActivator, this, 0 );
  2745. break;
  2746. case 4:
  2747. m_OnTrigger4.FireOutput( pActivator, this, 0 );
  2748. break;
  2749. case 5:
  2750. m_OnTrigger5.FireOutput( pActivator, this, 0 );
  2751. break;
  2752. case 6:
  2753. m_OnTrigger6.FireOutput( pActivator, this, 0 );
  2754. break;
  2755. case 7:
  2756. m_OnTrigger7.FireOutput( pActivator, this, 0 );
  2757. break;
  2758. case 8:
  2759. m_OnTrigger8.FireOutput( pActivator, this, 0 );
  2760. break;
  2761. case 9:
  2762. m_OnTrigger9.FireOutput( pActivator, this, 0 );
  2763. break;
  2764. case 10:
  2765. m_OnTrigger10.FireOutput( pActivator, this, 0 );
  2766. break;
  2767. case 11:
  2768. m_OnTrigger11.FireOutput( pActivator, this, 0 );
  2769. break;
  2770. case 12:
  2771. m_OnTrigger12.FireOutput( pActivator, this, 0 );
  2772. break;
  2773. case 13:
  2774. m_OnTrigger13.FireOutput( pActivator, this, 0 );
  2775. break;
  2776. case 14:
  2777. m_OnTrigger14.FireOutput( pActivator, this, 0 );
  2778. break;
  2779. case 15:
  2780. m_OnTrigger15.FireOutput( pActivator, this, 0 );
  2781. break;
  2782. case 16:
  2783. m_OnTrigger16.FireOutput( pActivator, this, 0 );
  2784. break;
  2785. }
  2786. }
  2787. break;
  2788. case CChoreoEvent::SEQUENCE:
  2789. {
  2790. if ( pActor )
  2791. {
  2792. DispatchStartSequence( scene, pActor, event );
  2793. }
  2794. }
  2795. break;
  2796. case CChoreoEvent::SECTION:
  2797. {
  2798. if ( IsMultiplayer() )
  2799. break;
  2800. // Pauses scene playback
  2801. DispatchPauseScene( scene, event->GetParameters() );
  2802. }
  2803. break;
  2804. case CChoreoEvent::LOOP:
  2805. {
  2806. DispatchProcessLoop( scene, event );
  2807. }
  2808. break;
  2809. case CChoreoEvent::INTERRUPT:
  2810. {
  2811. if ( IsMultiplayer() )
  2812. break;
  2813. DispatchStartInterrupt( scene, event );
  2814. }
  2815. break;
  2816. case CChoreoEvent::STOPPOINT:
  2817. {
  2818. #ifndef PORTAL2
  2819. if ( IsMultiplayer() )
  2820. break;
  2821. #endif
  2822. DispatchStopPoint( scene, event->GetParameters() );
  2823. }
  2824. break;
  2825. case CChoreoEvent::PERMIT_RESPONSES:
  2826. {
  2827. if ( IsMultiplayer() )
  2828. break;
  2829. DispatchStartPermitResponses( scene, pActor, event );
  2830. }
  2831. break;
  2832. default:
  2833. {
  2834. // FIXME: Unhandled event
  2835. // Assert(0);
  2836. }
  2837. break;
  2838. }
  2839. }
  2840. //-----------------------------------------------------------------------------
  2841. // Purpose:
  2842. // Input : currenttime -
  2843. // *event -
  2844. //-----------------------------------------------------------------------------
  2845. void CSceneEntity::EndEvent( float currenttime, CChoreoScene *scene, CChoreoEvent *event )
  2846. {
  2847. Assert( event );
  2848. if ( !Q_stricmp( event->GetName(), "NULL" ) )
  2849. {
  2850. return;
  2851. }
  2852. CBaseFlex *pActor = NULL;
  2853. CChoreoActor *actor = event->GetActor();
  2854. if ( actor )
  2855. {
  2856. pActor = FindNamedActor( actor );
  2857. }
  2858. LocalScene_Printf( "%s : %8.2f: finish %s\n", STRING( m_iszSceneFile ), currenttime, event->GetDescription() );
  2859. switch ( event->GetType() )
  2860. {
  2861. case CChoreoEvent::EXPRESSION:
  2862. {
  2863. if ( pActor && !IsMultiplayer() )
  2864. {
  2865. DispatchEndExpression( scene, pActor, event );
  2866. }
  2867. }
  2868. break;
  2869. case CChoreoEvent::SPEAK:
  2870. {
  2871. if ( pActor )
  2872. {
  2873. DispatchEndSpeak( scene, pActor, event );
  2874. }
  2875. }
  2876. break;
  2877. case CChoreoEvent::FLEXANIMATION:
  2878. {
  2879. if ( pActor && !IsMultiplayer() )
  2880. {
  2881. DispatchEndFlexAnimation( scene, pActor, event );
  2882. }
  2883. }
  2884. break;
  2885. case CChoreoEvent::LOOKAT:
  2886. {
  2887. if ( pActor && !IsMultiplayer() )
  2888. {
  2889. DispatchEndLookAt( scene, pActor, event );
  2890. }
  2891. }
  2892. break;
  2893. case CChoreoEvent::GESTURE:
  2894. {
  2895. if ( pActor )
  2896. {
  2897. DispatchEndGesture( scene, pActor, event );
  2898. }
  2899. }
  2900. break;
  2901. case CChoreoEvent::GENERIC:
  2902. {
  2903. // If the first token in the parameters is "debugtext", we printed it and we're done
  2904. if ( event->GetParameters() && StringHasPrefixCaseSensitive( event->GetParameters(), "debugtext" ) )
  2905. break;
  2906. if ( pActor )
  2907. {
  2908. DispatchEndGeneric( scene, pActor, event );
  2909. }
  2910. }
  2911. break;
  2912. case CChoreoEvent::CAMERA:
  2913. {
  2914. // call the end of camera or call a dispatch function
  2915. }
  2916. break;
  2917. case CChoreoEvent::SCRIPT:
  2918. {
  2919. // call the end of script or call a dispatch function
  2920. }
  2921. break;
  2922. case CChoreoEvent::SEQUENCE:
  2923. {
  2924. if ( pActor )
  2925. {
  2926. DispatchEndSequence( scene, pActor, event );
  2927. }
  2928. }
  2929. break;
  2930. case CChoreoEvent::FACE:
  2931. {
  2932. if ( pActor && !IsMultiplayer() )
  2933. {
  2934. DispatchEndFace( scene, pActor, event );
  2935. }
  2936. }
  2937. break;
  2938. case CChoreoEvent::MOVETO:
  2939. {
  2940. if ( pActor && !IsMultiplayer() )
  2941. {
  2942. DispatchEndMoveTo( scene, pActor, event );
  2943. }
  2944. }
  2945. break;
  2946. case CChoreoEvent::SUBSCENE:
  2947. {
  2948. if ( IsMultiplayer() )
  2949. break;
  2950. CChoreoScene *subscene = event->GetSubScene();
  2951. if ( subscene )
  2952. {
  2953. subscene->ResetSimulation();
  2954. }
  2955. }
  2956. break;
  2957. case CChoreoEvent::INTERRUPT:
  2958. {
  2959. if ( IsMultiplayer() )
  2960. break;
  2961. DispatchEndInterrupt( scene, event );
  2962. }
  2963. break;
  2964. case CChoreoEvent::PERMIT_RESPONSES:
  2965. {
  2966. if ( IsMultiplayer() )
  2967. break;
  2968. DispatchEndPermitResponses( scene, pActor, event );
  2969. }
  2970. break;
  2971. default:
  2972. break;
  2973. }
  2974. }
  2975. //-----------------------------------------------------------------------------
  2976. // Purpose: Only spew one time per missing scene!!!
  2977. // Input : *scenename -
  2978. //-----------------------------------------------------------------------------
  2979. void MissingSceneWarning( char const *scenename )
  2980. {
  2981. static CUtlSymbolTable missing;
  2982. // Make sure we only show the message once
  2983. if ( UTL_INVAL_SYMBOL == missing.Find( scenename ) )
  2984. {
  2985. missing.AddString( scenename );
  2986. Warning( "Scene '%s' missing!\n", scenename );
  2987. }
  2988. }
  2989. bool CSceneEntity::ShouldNetwork() const
  2990. {
  2991. if ( m_bMultiplayer )
  2992. {
  2993. if ( m_pScene &&
  2994. ( m_pScene->HasEventsOfType( CChoreoEvent::FLEXANIMATION ) ||
  2995. m_pScene->HasEventsOfType( CChoreoEvent::EXPRESSION )||
  2996. m_pScene->HasEventsOfType( CChoreoEvent::GESTURE ) ||
  2997. m_pScene->HasEventsOfType( CChoreoEvent::SEQUENCE ) ) )
  2998. {
  2999. return true;
  3000. }
  3001. }
  3002. else
  3003. {
  3004. if ( m_pScene &&
  3005. ( m_pScene->HasEventsOfType( CChoreoEvent::FLEXANIMATION ) ||
  3006. m_pScene->HasEventsOfType( CChoreoEvent::EXPRESSION ) ) )
  3007. {
  3008. return true;
  3009. }
  3010. }
  3011. return false;
  3012. }
  3013. CChoreoScene *CSceneEntity::LoadScene( const char *filename, IChoreoEventCallback *pCallback )
  3014. {
  3015. DevMsg( 2, "Blocking load of scene from '%s'\n", filename );
  3016. char loadfile[MAX_PATH];
  3017. Q_strncpy( loadfile, filename, sizeof( loadfile ) );
  3018. // Many vcd's in CSGO have a '.' in the filename, which breaks this call
  3019. // We're going to assume that all filenames have the correct extension
  3020. // Q_SetExtension( loadfile, ".vcd", sizeof( loadfile ) );
  3021. Q_FixSlashes( loadfile );
  3022. // binary compiled vcd
  3023. byte *pBuffer;
  3024. int fileSize;
  3025. if ( !CopySceneFileIntoMemory( loadfile, &pBuffer, &fileSize ) )
  3026. {
  3027. MissingSceneWarning( loadfile );
  3028. return NULL;
  3029. }
  3030. CChoreoScene *pScene = new CChoreoScene( NULL );
  3031. CUtlBuffer buf( pBuffer, fileSize, CUtlBuffer::READ_ONLY );
  3032. if ( !pScene->RestoreFromBinaryBuffer( buf, loadfile, &g_ChoreoStringPool ) )
  3033. {
  3034. Warning( "CSceneEntity::LoadScene: Unable to load binary scene '%s'\n", loadfile );
  3035. delete pScene;
  3036. pScene = NULL;
  3037. }
  3038. else
  3039. {
  3040. pScene->SetPrintFunc( LocalScene_Printf );
  3041. pScene->SetEventCallbackInterface( pCallback );
  3042. }
  3043. FreeSceneFileMemory( pBuffer );
  3044. return pScene;
  3045. }
  3046. CChoreoScene *BlockingLoadScene( const char *filename )
  3047. {
  3048. return CSceneEntity::LoadScene( filename, NULL );
  3049. }
  3050. //-----------------------------------------------------------------------------
  3051. // Purpose: vscript - create a scene directly from a buffer containing
  3052. // a vcd description, and load it into the scene entity.
  3053. //-----------------------------------------------------------------------------
  3054. bool CSceneEntity::ScriptLoadSceneFromString( const char * pszFilename, const char *pszData )
  3055. {
  3056. CChoreoScene *pScene = new CChoreoScene( NULL );
  3057. // CSceneTokenProcessor SceneTokenProcessor;
  3058. // SceneTokenProcessor.SetBuffer( pszData );
  3059. g_TokenProcessor.SetBuffer( (char *)pszData );
  3060. if ( !pScene->ParseFromBuffer( pszFilename, &g_TokenProcessor ) ) //&SceneTokenProcessor ) )
  3061. {
  3062. Warning( "CSceneEntity::LoadSceneFromString: Unable to parse scene data '%s'\n", pszFilename );
  3063. delete pScene;
  3064. pScene = NULL;
  3065. }
  3066. else
  3067. {
  3068. pScene->SetPrintFunc( LocalScene_Printf );
  3069. pScene->SetEventCallbackInterface( this );
  3070. // precache all sounds for the newly constructed scene
  3071. PrecacheScene( pScene );
  3072. }
  3073. if ( pScene != NULL )
  3074. {
  3075. // release prior scene if present
  3076. UnloadScene();
  3077. m_pScene = pScene;
  3078. if ( GetSceneManager() )
  3079. {
  3080. GetSceneManager()->ActivateScene( this );
  3081. }
  3082. return true;
  3083. }
  3084. else
  3085. {
  3086. return false;
  3087. }
  3088. }
  3089. //-----------------------------------------------------------------------------
  3090. // Purpose:
  3091. //-----------------------------------------------------------------------------
  3092. void CSceneEntity::UnloadScene( void )
  3093. {
  3094. if ( m_pScene )
  3095. {
  3096. ClearSceneEvents( m_pScene, false );
  3097. for ( int i = 0 ; i < m_pScene->GetNumActors(); i++ )
  3098. {
  3099. CBaseFlex *pTestActor = FindNamedActor( i );
  3100. if ( !pTestActor )
  3101. continue;
  3102. pTestActor->RemoveChoreoScene( m_pScene );
  3103. }
  3104. }
  3105. delete m_pScene;
  3106. m_pScene = NULL;
  3107. if ( GetSceneManager() )
  3108. {
  3109. GetSceneManager()->DeactivateScene( this );
  3110. }
  3111. }
  3112. //-----------------------------------------------------------------------------
  3113. // Purpose: Called every frame that an event is active (Start/EndEvent as also
  3114. // called)
  3115. // Input : *event -
  3116. // Output : Returns true on success, false on failure.
  3117. //-----------------------------------------------------------------------------
  3118. void CSceneEntity::ProcessEvent( float currenttime, CChoreoScene *scene, CChoreoEvent *event )
  3119. {
  3120. switch ( event->GetType() )
  3121. {
  3122. case CChoreoEvent::SUBSCENE:
  3123. {
  3124. Assert( event->GetType() == CChoreoEvent::SUBSCENE );
  3125. CChoreoScene *subscene = event->GetSubScene();
  3126. if ( !subscene )
  3127. return;
  3128. if ( subscene->SimulationFinished() )
  3129. return;
  3130. // Have subscenes think for appropriate time
  3131. subscene->Think( m_flFrameTime );
  3132. }
  3133. break;
  3134. default:
  3135. break;
  3136. }
  3137. return;
  3138. }
  3139. //-----------------------------------------------------------------------------
  3140. // Purpose: Called for events that are part of a pause condition
  3141. // Input : *event -
  3142. // Output : Returns true on event completed, false on non-completion.
  3143. //-----------------------------------------------------------------------------
  3144. bool CSceneEntity::CheckEvent( float currenttime, CChoreoScene *scene, CChoreoEvent *event )
  3145. {
  3146. switch ( event->GetType() )
  3147. {
  3148. case CChoreoEvent::SUBSCENE:
  3149. {
  3150. }
  3151. break;
  3152. default:
  3153. {
  3154. CBaseFlex *pActor = NULL;
  3155. CChoreoActor *actor = event->GetActor();
  3156. if ( actor )
  3157. {
  3158. pActor = FindNamedActor( actor );
  3159. if (pActor == NULL)
  3160. {
  3161. Warning( "CSceneEntity %s unable to find actor \"%s\"\n", STRING(GetEntityName()), actor->GetName() );
  3162. return true;
  3163. }
  3164. }
  3165. if (pActor)
  3166. {
  3167. return pActor->CheckSceneEvent( currenttime, scene, event );
  3168. }
  3169. }
  3170. break;
  3171. }
  3172. return true;
  3173. }
  3174. //-----------------------------------------------------------------------------
  3175. // Purpose: Get a sticky version of a named actor
  3176. // Input : CChoreoActor
  3177. // Output : CBaseFlex
  3178. //-----------------------------------------------------------------------------
  3179. CBaseFlex *CSceneEntity::FindNamedActor( int index )
  3180. {
  3181. if (m_hActorList.Count() == 0)
  3182. {
  3183. m_hActorList.SetCount( m_pScene->GetNumActors() );
  3184. NetworkProp()->NetworkStateForceUpdate();
  3185. }
  3186. if ( !m_hActorList.IsValidIndex( index ) )
  3187. {
  3188. DevWarning( "Scene %s has %d actors, but scene entity only has %d actors\n", m_pScene->GetFilename(), m_pScene->GetNumActors(), m_hActorList.Count() );
  3189. return NULL;
  3190. }
  3191. CBaseFlex *pActor = m_hActorList[ index ];
  3192. if (pActor == NULL || !pActor->IsAlive() )
  3193. {
  3194. CChoreoActor *pChoreoActor = m_pScene->GetActor( index );
  3195. if ( !pChoreoActor )
  3196. return NULL;
  3197. pActor = FindNamedActor( pChoreoActor->GetName() );
  3198. if (pActor)
  3199. {
  3200. // save who we found so we'll use them again
  3201. m_hActorList[ index ] = pActor;
  3202. NetworkProp()->NetworkStateForceUpdate();
  3203. }
  3204. }
  3205. return pActor;
  3206. }
  3207. //-----------------------------------------------------------------------------
  3208. // Purpose: Get a sticky version of a named actor
  3209. // Input : CChoreoActor
  3210. // Output : CBaseFlex
  3211. //-----------------------------------------------------------------------------
  3212. CBaseFlex *CSceneEntity::FindNamedActor( CChoreoActor *pChoreoActor )
  3213. {
  3214. int index = m_pScene->FindActorIndex( pChoreoActor );
  3215. if (index >= 0)
  3216. {
  3217. return FindNamedActor( index );
  3218. }
  3219. return NULL;
  3220. }
  3221. //-----------------------------------------------------------------------------
  3222. // Purpose: Search for an actor by name, make sure it can do face poses
  3223. // Input : *name -
  3224. // Output : CBaseFlex
  3225. //-----------------------------------------------------------------------------
  3226. CBaseFlex *CSceneEntity::FindNamedActor( const char *name )
  3227. {
  3228. CBaseEntity *entity = FindNamedEntity( name, NULL, true );
  3229. if ( !entity )
  3230. {
  3231. // Couldn't find actor!
  3232. return NULL;
  3233. }
  3234. // Make sure it can actually do facial animation, etc.
  3235. CBaseFlex *flexEntity = dynamic_cast< CBaseFlex * >( entity );
  3236. if ( !flexEntity )
  3237. {
  3238. // That actor was not a CBaseFlex!
  3239. return NULL;
  3240. }
  3241. return flexEntity;
  3242. }
  3243. //-----------------------------------------------------------------------------
  3244. // Purpose: Find an entity specified by a target name
  3245. // Input : *name -
  3246. // Output : CBaseEntity
  3247. //-----------------------------------------------------------------------------
  3248. CBaseEntity *CSceneEntity::FindNamedTarget( string_t iszTarget, bool bBaseFlexOnly )
  3249. {
  3250. if ( !stricmp( STRING(iszTarget), "!activator" ) )
  3251. return m_hActivator;
  3252. // If we don't have a wildcard in the target, just return the first entity found
  3253. if ( !strchr( STRING(iszTarget), '*' ) )
  3254. return gEntList.FindEntityByName( NULL, iszTarget );
  3255. CBaseEntity *pTarget = NULL;
  3256. while ( (pTarget = gEntList.FindEntityByName( pTarget, iszTarget )) != NULL )
  3257. {
  3258. if ( bBaseFlexOnly )
  3259. {
  3260. // Make sure it can actually do facial animation, etc.
  3261. if ( dynamic_cast< CBaseFlex * >( pTarget ) )
  3262. return pTarget;
  3263. }
  3264. else
  3265. {
  3266. return pTarget;
  3267. }
  3268. }
  3269. // Failed to find one
  3270. return NULL;
  3271. }
  3272. //-----------------------------------------------------------------------------
  3273. // Purpose: Filters entities only if they're clear
  3274. //-----------------------------------------------------------------------------
  3275. class CSceneFindMarkFilter : public IEntityFindFilter
  3276. {
  3277. public:
  3278. void SetActor( CBaseEntity *pActor )
  3279. {
  3280. m_hActor = pActor;
  3281. }
  3282. bool ShouldFindEntity( CBaseEntity *pEntity )
  3283. {
  3284. if ( !m_hActor )
  3285. return true;
  3286. // If we find no truly valid marks, we'll just use the first.
  3287. if ( !m_hEntityFound.Get() )
  3288. {
  3289. m_hEntityFound = pEntity;
  3290. }
  3291. // We only want marks that are clear
  3292. trace_t tr;
  3293. Vector vecOrigin = pEntity->GetAbsOrigin();
  3294. AI_TraceHull( vecOrigin, vecOrigin, m_hActor->WorldAlignMins(), m_hActor->WorldAlignMaxs(), MASK_SOLID, m_hActor, COLLISION_GROUP_NONE, &tr );
  3295. if ( tr.startsolid )
  3296. {
  3297. return false;
  3298. }
  3299. m_hEntityFound = pEntity;
  3300. return true;
  3301. }
  3302. CBaseEntity *GetFilterResult( void )
  3303. {
  3304. return m_hEntityFound;
  3305. }
  3306. private:
  3307. EHANDLE m_hActor;
  3308. // To maintain backwards compatability, store off the first mark
  3309. // we find. If we find no truly valid marks, we'll just use the first.
  3310. EHANDLE m_hEntityFound;
  3311. };
  3312. //-----------------------------------------------------------------------------
  3313. // Purpose: Finds the entity nearest to both entities, and is clear
  3314. //-----------------------------------------------------------------------------
  3315. class CSceneFindNearestMarkFilter : public IEntityFindFilter
  3316. {
  3317. public:
  3318. CSceneFindNearestMarkFilter( const CBaseEntity *pActor, const Vector &vecPos2, float flMaxRadius = MAX_TRACE_LENGTH )
  3319. {
  3320. m_vecPos2 = vecPos2;
  3321. m_flMaxSegmentDistance = flMaxRadius;
  3322. m_flNearestToTarget = flMaxRadius;
  3323. m_pNearestToTarget = NULL;
  3324. m_flNearestToActor = flMaxRadius;
  3325. m_pNearestToActor = NULL;
  3326. m_hActor = pActor;
  3327. if (pActor)
  3328. {
  3329. m_vecPos1 = pActor->GetAbsOrigin();
  3330. m_flMaxSegmentDistance = MIN( flMaxRadius, (m_vecPos1 - m_vecPos2).Length() + 1.0 );
  3331. if (m_flMaxSegmentDistance <= 1.0)
  3332. {
  3333. // must be closest to self
  3334. m_flMaxSegmentDistance = MIN( flMaxRadius, MAX_TRACE_LENGTH );
  3335. }
  3336. }
  3337. }
  3338. bool ShouldFindEntity( CBaseEntity *pEntity )
  3339. {
  3340. if ( !m_hActor )
  3341. return true;
  3342. // If we find no truly valid marks, we'll just use the first.
  3343. if ( m_pNearestToActor == NULL )
  3344. {
  3345. m_pNearestToActor = pEntity;
  3346. }
  3347. // We only want marks that are clear
  3348. trace_t tr;
  3349. Vector vecOrigin = pEntity->GetAbsOrigin();
  3350. AI_TraceHull( vecOrigin, vecOrigin, m_hActor->WorldAlignMins(), m_hActor->WorldAlignMaxs(), MASK_SOLID, m_hActor, COLLISION_GROUP_NONE, &tr );
  3351. if ( !tr.startsolid || tr.m_pEnt == m_hActor)
  3352. {
  3353. float dist1 = (m_vecPos1 - pEntity->GetAbsOrigin()).Length();
  3354. float dist2 = (m_vecPos2 - pEntity->GetAbsOrigin()).Length();
  3355. /*
  3356. char text[256];
  3357. Q_snprintf( text, sizeof( text ), "%.0f : %.0f", dist1, dist2 );
  3358. NDebugOverlay::Text( pEntity->GetAbsOrigin() + Vector( 0, 0, 8 ), text, false, 5.0f );
  3359. */
  3360. // find the point closest to the actor
  3361. if (dist1 <= m_flNearestToActor)
  3362. {
  3363. m_pNearestToActor = pEntity;
  3364. m_flNearestToActor = dist2;
  3365. }
  3366. // find that node that's closest to both, but the distance to it from the actor isn't farther than
  3367. // the distance to the second node. This should keep the actor from walking past their point of interest
  3368. if (dist1 <= m_flMaxSegmentDistance && dist2 <= m_flMaxSegmentDistance && dist2 < m_flNearestToTarget)
  3369. {
  3370. m_pNearestToTarget = pEntity;
  3371. m_flNearestToTarget = dist2;
  3372. }
  3373. }
  3374. return false;
  3375. }
  3376. CBaseEntity *GetFilterResult( void )
  3377. {
  3378. if (m_pNearestToTarget)
  3379. return m_pNearestToTarget;
  3380. return m_pNearestToActor;
  3381. }
  3382. private:
  3383. EHANDLE m_hActor;
  3384. Vector m_vecPos1;
  3385. Vector m_vecPos2;
  3386. float m_flMaxSegmentDistance;
  3387. float m_flNearestToTarget;
  3388. CBaseEntity *m_pNearestToTarget;
  3389. float m_flNearestToActor;
  3390. CBaseEntity *m_pNearestToActor;
  3391. };
  3392. HSCRIPT CSceneEntity::ScriptFindNamedEntity( const char *name )
  3393. {
  3394. return ToHScript(FindNamedEntity( name, NULL, false, false ));
  3395. }
  3396. //-----------------------------------------------------------------------------
  3397. // Purpose: Search for an actor by name, make sure it can do face poses
  3398. // Input : *name -
  3399. // Output : CBaseFlex
  3400. //-----------------------------------------------------------------------------
  3401. CBaseEntity *CSceneEntity::FindNamedEntity( const char *name, CBaseEntity *pActor, bool bBaseFlexOnly, bool bUseClear )
  3402. {
  3403. CBaseEntity *entity = NULL;
  3404. if ( !stricmp( name, "Player" ) || !stricmp( name, "!player" ))
  3405. {
  3406. entity = ( gpGlobals->maxClients == 1 ) ? ( CBaseEntity * )UTIL_GetLocalPlayer() : NULL;
  3407. }
  3408. else if ( !stricmp( name, "!target1" ) )
  3409. {
  3410. if (m_hTarget1 == NULL)
  3411. {
  3412. m_hTarget1 = FindNamedTarget( m_iszTarget1, bBaseFlexOnly );
  3413. }
  3414. return m_hTarget1;
  3415. }
  3416. else if ( !stricmp( name, "!target2" ) )
  3417. {
  3418. if (m_hTarget2 == NULL)
  3419. {
  3420. m_hTarget2 = FindNamedTarget( m_iszTarget2, bBaseFlexOnly );
  3421. }
  3422. return m_hTarget2;
  3423. }
  3424. else if ( !stricmp( name, "!target3" ) )
  3425. {
  3426. if (m_hTarget3 == NULL)
  3427. {
  3428. m_hTarget3 = FindNamedTarget( m_iszTarget3, bBaseFlexOnly );
  3429. }
  3430. return m_hTarget3;
  3431. }
  3432. else if ( !stricmp( name, "!target4" ) )
  3433. {
  3434. if (m_hTarget4 == NULL)
  3435. {
  3436. m_hTarget4 = FindNamedTarget( m_iszTarget4, bBaseFlexOnly );
  3437. }
  3438. return m_hTarget4;
  3439. }
  3440. else if ( !stricmp( name, "!target5" ) )
  3441. {
  3442. if (m_hTarget5 == NULL)
  3443. {
  3444. m_hTarget5 = FindNamedTarget( m_iszTarget5, bBaseFlexOnly );
  3445. }
  3446. return m_hTarget5;
  3447. }
  3448. else if ( !stricmp( name, "!target6" ) )
  3449. {
  3450. if (m_hTarget6 == NULL)
  3451. {
  3452. m_hTarget6 = FindNamedTarget( m_iszTarget6, bBaseFlexOnly );
  3453. }
  3454. return m_hTarget6;
  3455. }
  3456. else if ( !stricmp( name, "!target7" ) )
  3457. {
  3458. if (m_hTarget7 == NULL)
  3459. {
  3460. m_hTarget7 = FindNamedTarget( m_iszTarget7, bBaseFlexOnly );
  3461. }
  3462. return m_hTarget7;
  3463. }
  3464. else if ( !stricmp( name, "!target8" ) )
  3465. {
  3466. if (m_hTarget8 == NULL)
  3467. {
  3468. m_hTarget8 = FindNamedTarget( m_iszTarget8, bBaseFlexOnly );
  3469. }
  3470. return m_hTarget8;
  3471. }
  3472. else if (pActor && pActor->MyNPCPointer())
  3473. {
  3474. CSceneFindMarkFilter *pFilter = NULL;
  3475. if ( bUseClear )
  3476. {
  3477. pFilter = new CSceneFindMarkFilter();
  3478. pFilter->SetActor( pActor );
  3479. }
  3480. entity = pActor->MyNPCPointer()->FindNamedEntity( name, pFilter );
  3481. if ( !entity && pFilter )
  3482. {
  3483. entity = pFilter->GetFilterResult();
  3484. }
  3485. }
  3486. else
  3487. {
  3488. // search for up to 32 entities with the same name and choose one randomly
  3489. CBaseEntity *entityList[ FINDNAMEDENTITY_MAX_ENTITIES ];
  3490. int iCount;
  3491. entity = NULL;
  3492. for( iCount = 0; iCount < FINDNAMEDENTITY_MAX_ENTITIES; iCount++ )
  3493. {
  3494. entity = gEntList.FindEntityByName( entity, name, NULL, pActor );
  3495. if ( !entity )
  3496. {
  3497. break;
  3498. }
  3499. entityList[ iCount ] = entity;
  3500. }
  3501. if ( iCount > 0 )
  3502. {
  3503. entity = entityList[ RandomInt( 0, iCount - 1 ) ];
  3504. }
  3505. else
  3506. {
  3507. entity = NULL;
  3508. }
  3509. }
  3510. return entity;
  3511. }
  3512. //-----------------------------------------------------------------------------
  3513. // Purpose: Search for an actor by name, make sure it can do face poses
  3514. // Input : *name -
  3515. // Output : CBaseFlex
  3516. //-----------------------------------------------------------------------------
  3517. CBaseEntity *CSceneEntity::FindNamedEntityClosest( const char *name, CBaseEntity *pActor, bool bBaseFlexOnly, bool bUseClear, const char *pszSecondary )
  3518. {
  3519. CBaseEntity *entity = NULL;
  3520. if ( !stricmp( name, "!activator" ) )
  3521. {
  3522. return m_hActivator;
  3523. }
  3524. else if ( !stricmp( name, "Player" ) || !stricmp( name, "!player" ))
  3525. {
  3526. entity = ( gpGlobals->maxClients == 1 ) ? ( CBaseEntity * )UTIL_GetLocalPlayer() : NULL;
  3527. return entity;
  3528. }
  3529. else if ( !stricmp( name, "!target1" ) )
  3530. {
  3531. name = STRING( m_iszTarget1 );
  3532. }
  3533. else if ( !stricmp( name, "!target2" ) )
  3534. {
  3535. name = STRING( m_iszTarget2 );
  3536. }
  3537. else if ( !stricmp( name, "!target3" ) )
  3538. {
  3539. name = STRING( m_iszTarget3 );
  3540. }
  3541. else if ( !stricmp( name, "!target4" ) )
  3542. {
  3543. name = STRING( m_iszTarget4 );
  3544. }
  3545. else if ( !stricmp( name, "!target5" ) )
  3546. {
  3547. name = STRING( m_iszTarget5 );
  3548. }
  3549. else if ( !stricmp( name, "!target6" ) )
  3550. {
  3551. name = STRING( m_iszTarget6 );
  3552. }
  3553. else if ( !stricmp( name, "!target7" ) )
  3554. {
  3555. name = STRING( m_iszTarget7 );
  3556. }
  3557. if (pActor && pActor->MyNPCPointer())
  3558. {
  3559. if (pszSecondary && strlen( pszSecondary ) > 0)
  3560. {
  3561. CBaseEntity *pActor2 = FindNamedEntityClosest( pszSecondary, pActor, false, false, NULL );
  3562. if (pActor2)
  3563. {
  3564. CSceneFindNearestMarkFilter *pFilter = new CSceneFindNearestMarkFilter( pActor, pActor2->GetAbsOrigin() );
  3565. entity = pActor->MyNPCPointer()->FindNamedEntity( name, pFilter );
  3566. if (!entity && pFilter)
  3567. {
  3568. entity = pFilter->GetFilterResult();
  3569. }
  3570. }
  3571. }
  3572. if (!entity)
  3573. {
  3574. CSceneFindMarkFilter *pFilter = NULL;
  3575. if ( bUseClear )
  3576. {
  3577. pFilter = new CSceneFindMarkFilter();
  3578. pFilter->SetActor( pActor );
  3579. }
  3580. entity = pActor->MyNPCPointer()->FindNamedEntity( name, pFilter );
  3581. if (!entity && pFilter)
  3582. {
  3583. entity = pFilter->GetFilterResult();
  3584. }
  3585. }
  3586. }
  3587. else
  3588. {
  3589. // search for up to 32 entities with the same name and choose one randomly
  3590. int iCount;
  3591. entity = NULL;
  3592. CBaseEntity *current = NULL;
  3593. for( iCount = 0; iCount < FINDNAMEDENTITY_MAX_ENTITIES; iCount++ )
  3594. {
  3595. current = gEntList.FindEntityByName( current, name, NULL, pActor );
  3596. if ( current )
  3597. {
  3598. if (RandomInt( 0, iCount ) == 0)
  3599. entity = current;
  3600. }
  3601. }
  3602. entity = NULL;
  3603. }
  3604. return entity;
  3605. }
  3606. //-----------------------------------------------------------------------------
  3607. // Purpose: Remove all "scene" expressions from all actors in this scene
  3608. //-----------------------------------------------------------------------------
  3609. void CSceneEntity::ClearSceneEvents( CChoreoScene *scene, bool canceled )
  3610. {
  3611. if ( !m_pScene )
  3612. return;
  3613. LocalScene_Printf( "%s : %8.2f: clearing events\n", STRING( m_iszSceneFile ), m_flCurrentTime );
  3614. int i;
  3615. for ( i = 0 ; i < m_pScene->GetNumActors(); i++ )
  3616. {
  3617. CBaseFlex *pActor = FindNamedActor( i );
  3618. if ( !pActor )
  3619. continue;
  3620. // Clear any existing expressions
  3621. pActor->ClearSceneEvents( scene, canceled );
  3622. }
  3623. // Iterate events and precache necessary resources
  3624. for ( i = 0; i < scene->GetNumEvents(); i++ )
  3625. {
  3626. CChoreoEvent *event = scene->GetEvent( i );
  3627. if ( !event )
  3628. continue;
  3629. // load any necessary data
  3630. switch (event->GetType() )
  3631. {
  3632. default:
  3633. break;
  3634. case CChoreoEvent::SUBSCENE:
  3635. {
  3636. // Only allow a single level of subscenes for now
  3637. if ( !scene->IsSubScene() )
  3638. {
  3639. CChoreoScene *subscene = event->GetSubScene();
  3640. if ( subscene )
  3641. {
  3642. ClearSceneEvents( subscene, canceled );
  3643. }
  3644. }
  3645. }
  3646. break;
  3647. }
  3648. }
  3649. }
  3650. //-----------------------------------------------------------------------------
  3651. // Purpose: Remove all imposed schedules from all actors in this scene
  3652. //-----------------------------------------------------------------------------
  3653. void CSceneEntity::ClearSchedules( CChoreoScene *scene )
  3654. {
  3655. if ( !m_pScene )
  3656. return;
  3657. int i;
  3658. for ( i = 0 ; i < m_pScene->GetNumActors(); i++ )
  3659. {
  3660. CBaseFlex *pActor = FindNamedActor( i );
  3661. if ( !pActor )
  3662. continue;
  3663. CAI_BaseNPC *pNPC = pActor->MyNPCPointer();
  3664. if ( pNPC )
  3665. {
  3666. /*
  3667. if ( pNPC->IsCurSchedule( SCHED_SCENE_GENERIC ) )
  3668. pNPC->ClearSchedule( "Scene entity clearing all actor schedules" );
  3669. */
  3670. }
  3671. else
  3672. {
  3673. pActor->ResetSequence( pActor->SelectWeightedSequence( ACT_IDLE ) );
  3674. pActor->SetCycle( 0 );
  3675. }
  3676. // Clear any existing expressions
  3677. }
  3678. // Iterate events and precache necessary resources
  3679. for ( i = 0; i < scene->GetNumEvents(); i++ )
  3680. {
  3681. CChoreoEvent *event = scene->GetEvent( i );
  3682. if ( !event )
  3683. continue;
  3684. // load any necessary data
  3685. switch (event->GetType() )
  3686. {
  3687. default:
  3688. break;
  3689. case CChoreoEvent::SUBSCENE:
  3690. {
  3691. // Only allow a single level of subscenes for now
  3692. if ( !scene->IsSubScene() )
  3693. {
  3694. CChoreoScene *subscene = event->GetSubScene();
  3695. if ( subscene )
  3696. {
  3697. ClearSchedules( subscene );
  3698. }
  3699. }
  3700. }
  3701. break;
  3702. }
  3703. }
  3704. }
  3705. //-----------------------------------------------------------------------------
  3706. // Purpose: If we are currently interruptable, pause this scene and wait for the other
  3707. // scene to finish
  3708. // Input : *otherScene -
  3709. //-----------------------------------------------------------------------------
  3710. bool CSceneEntity::InterruptThisScene( CSceneEntity *otherScene )
  3711. {
  3712. Assert( otherScene );
  3713. if ( !IsInterruptable() )
  3714. {
  3715. return false;
  3716. }
  3717. // Already interrupted
  3718. if ( m_bInterrupted )
  3719. {
  3720. return false;
  3721. }
  3722. m_bInterrupted = true;
  3723. m_hInterruptScene = otherScene;
  3724. // Ask other scene to tell us when it's finished or canceled
  3725. otherScene->RequestCompletionNotification( this );
  3726. PausePlayback();
  3727. return true;
  3728. }
  3729. /*
  3730. void scene_interrupt( const CCommand &args )
  3731. {
  3732. if ( args.ArgC() != 3 )
  3733. return;
  3734. const char *scene1 = args[1];
  3735. const char *scene2 = args[2];
  3736. CSceneEntity *s1 = dynamic_cast< CSceneEntity * >( gEntList.FindEntityByName( NULL, scene1 ) );
  3737. CSceneEntity *s2 = dynamic_cast< CSceneEntity * >( gEntList.FindEntityByName( NULL, scene2 ) );
  3738. if ( !s1 || !s2 )
  3739. return;
  3740. if ( s1->InterruptThisScene( s2 ) )
  3741. {
  3742. s2->StartPlayback();
  3743. }
  3744. }
  3745. static ConCommand interruptscene( "int", scene_interrupt, "interrupt scene 1 with scene 2.", FCVAR_CHEAT );
  3746. */
  3747. //-----------------------------------------------------------------------------
  3748. // Purpose:
  3749. //-----------------------------------------------------------------------------
  3750. void CSceneEntity::CheckInterruptCompletion()
  3751. {
  3752. if ( !m_bInterrupted )
  3753. return;
  3754. // If the interruptor goes away it's the same as having that scene finish up...
  3755. if ( m_hInterruptScene != NULL &&
  3756. !m_bInterruptSceneFinished )
  3757. {
  3758. return;
  3759. }
  3760. m_bInterrupted = false;
  3761. m_hInterruptScene = NULL;
  3762. ResumePlayback();
  3763. }
  3764. //-----------------------------------------------------------------------------
  3765. // Purpose:
  3766. //-----------------------------------------------------------------------------
  3767. void CSceneEntity::ClearInterrupt()
  3768. {
  3769. m_nInterruptCount = 0;
  3770. m_bInterrupted = false;
  3771. m_hInterruptScene = NULL;
  3772. }
  3773. //-----------------------------------------------------------------------------
  3774. // Purpose: Another scene is asking us to notify upon completion
  3775. // Input : *notify -
  3776. //-----------------------------------------------------------------------------
  3777. void CSceneEntity::RequestCompletionNotification( CSceneEntity *notify )
  3778. {
  3779. CHandle< CSceneEntity > h;
  3780. h = notify;
  3781. // Only add it once
  3782. if ( m_hNotifySceneCompletion.Find( h ) == m_hNotifySceneCompletion.InvalidIndex() )
  3783. {
  3784. m_hNotifySceneCompletion.AddToTail( h );
  3785. }
  3786. }
  3787. //-----------------------------------------------------------------------------
  3788. // Purpose: An interrupt scene has finished or been canceled, we can resume once we pick up this state in CheckInterruptCompletion
  3789. // Input : *interruptor -
  3790. //-----------------------------------------------------------------------------
  3791. void CSceneEntity::NotifyOfCompletion( CSceneEntity *interruptor )
  3792. {
  3793. Assert( m_bInterrupted );
  3794. Assert( m_hInterruptScene == interruptor );
  3795. m_bInterruptSceneFinished = true;
  3796. CheckInterruptCompletion();
  3797. }
  3798. //-----------------------------------------------------------------------------
  3799. // Purpose:
  3800. //-----------------------------------------------------------------------------
  3801. void CSceneEntity::AddListManager( CSceneListManager *pManager )
  3802. {
  3803. CHandle< CSceneListManager > h;
  3804. h = pManager;
  3805. // Only add it once
  3806. if ( m_hListManagers.Find( h ) == m_hListManagers.InvalidIndex() )
  3807. {
  3808. m_hListManagers.AddToTail( h );
  3809. }
  3810. }
  3811. //-----------------------------------------------------------------------------
  3812. // Purpose: Clear any targets that a referencing !activator
  3813. //-----------------------------------------------------------------------------
  3814. void CSceneEntity::ClearActivatorTargets( void )
  3815. {
  3816. if ( !stricmp( STRING(m_iszTarget1), "!activator" ) )
  3817. {
  3818. // We need to clear out actors so they're re-evaluated
  3819. m_hActorList.Purge();
  3820. NetworkProp()->NetworkStateForceUpdate();
  3821. m_hTarget1 = NULL;
  3822. }
  3823. if ( !stricmp( STRING(m_iszTarget2), "!activator" ) )
  3824. {
  3825. // We need to clear out actors so they're re-evaluated
  3826. m_hActorList.Purge();
  3827. NetworkProp()->NetworkStateForceUpdate();
  3828. m_hTarget2 = NULL;
  3829. }
  3830. if ( !stricmp( STRING(m_iszTarget3), "!activator" ) )
  3831. {
  3832. // We need to clear out actors so they're re-evaluated
  3833. m_hActorList.Purge();
  3834. NetworkProp()->NetworkStateForceUpdate();
  3835. m_hTarget3 = NULL;
  3836. }
  3837. if ( !stricmp( STRING(m_iszTarget4), "!activator" ) )
  3838. {
  3839. // We need to clear out actors so they're re-evaluated
  3840. m_hActorList.Purge();
  3841. NetworkProp()->NetworkStateForceUpdate();
  3842. m_hTarget4 = NULL;
  3843. }
  3844. if ( !stricmp( STRING(m_iszTarget5), "!activator" ) )
  3845. {
  3846. // We need to clear out actors so they're re-evaluated
  3847. m_hActorList.Purge();
  3848. NetworkProp()->NetworkStateForceUpdate();
  3849. m_hTarget5 = NULL;
  3850. }
  3851. if ( !stricmp( STRING(m_iszTarget6), "!activator" ) )
  3852. {
  3853. // We need to clear out actors so they're re-evaluated
  3854. m_hActorList.Purge();
  3855. NetworkProp()->NetworkStateForceUpdate();
  3856. m_hTarget6 = NULL;
  3857. }
  3858. if ( !stricmp( STRING(m_iszTarget7), "!activator" ) )
  3859. {
  3860. // We need to clear out actors so they're re-evaluated
  3861. m_hActorList.Purge();
  3862. NetworkProp()->NetworkStateForceUpdate();
  3863. m_hTarget7 = NULL;
  3864. }
  3865. if ( !stricmp( STRING(m_iszTarget8), "!activator" ) )
  3866. {
  3867. // We need to clear out actors so they're re-evaluated
  3868. m_hActorList.Purge();
  3869. NetworkProp()->NetworkStateForceUpdate();
  3870. m_hTarget8 = NULL;
  3871. }
  3872. }
  3873. //-----------------------------------------------------------------------------
  3874. // Purpose: Called when a scene is completed or canceled
  3875. //-----------------------------------------------------------------------------
  3876. void CSceneEntity::OnSceneFinished( bool canceled, bool fireoutput )
  3877. {
  3878. if ( !m_pScene )
  3879. return;
  3880. LocalScene_Printf( "%s : %8.2f: finished\n", STRING( m_iszSceneFile ), m_flCurrentTime );
  3881. // Notify any listeners
  3882. int c = m_hNotifySceneCompletion.Count();
  3883. int i;
  3884. for ( i = 0; i < c; i++ )
  3885. {
  3886. CSceneEntity *ent = m_hNotifySceneCompletion[ i ].Get();
  3887. if ( !ent )
  3888. continue;
  3889. ent->NotifyOfCompletion( this );
  3890. }
  3891. m_hNotifySceneCompletion.RemoveAll();
  3892. // Clear simulation
  3893. m_pScene->ResetSimulation();
  3894. m_bIsPlayingBack = false;
  3895. m_bPaused = false;
  3896. SetCurrentTime( 0.0f, false );
  3897. // Clear interrupt state if we were interrupted for some reason
  3898. ClearInterrupt();
  3899. if ( fireoutput && !m_bCompletedEarly)
  3900. {
  3901. m_OnCompletion.FireOutput( this, this, 0 );
  3902. }
  3903. {
  3904. CBaseFlex *pFlex = FindNamedActor( 0 ) ;
  3905. if ( pFlex )
  3906. {
  3907. CBaseMultiplayerPlayer *pAsPlayer = dynamic_cast<CBaseMultiplayerPlayer *>(pFlex);
  3908. if (pAsPlayer)
  3909. {
  3910. CAI_Expresser *pExpresser = pAsPlayer->GetExpresser();
  3911. pExpresser->OnSpeechFinished();
  3912. }
  3913. else if ( CAI_BaseActor *pActor = ToBaseActor( pFlex ) )
  3914. {
  3915. CAI_Expresser *pExpresser = pActor->GetExpresser();
  3916. pExpresser->OnSpeechFinished();
  3917. }
  3918. }
  3919. }
  3920. // Put face back in neutral pose
  3921. ClearSceneEvents( m_pScene, canceled );
  3922. for ( i = 0 ; i < m_pScene->GetNumActors(); i++ )
  3923. {
  3924. CBaseFlex *pTestActor = FindNamedActor( i );
  3925. if ( !pTestActor )
  3926. continue;
  3927. pTestActor->RemoveChoreoScene( m_pScene, canceled );
  3928. // If we interrupted the actor's previous scenes, resume them
  3929. if ( m_bInterruptedActorsScenes )
  3930. {
  3931. QueueActorsScriptedScenesToResume( pTestActor, false );
  3932. }
  3933. }
  3934. }
  3935. //-----------------------------------------------------------------------------
  3936. // Should we transmit it to the client?
  3937. //-----------------------------------------------------------------------------
  3938. int CSceneEntity::UpdateTransmitState()
  3939. {
  3940. if ( !ShouldNetwork() )
  3941. {
  3942. return SetTransmitState( FL_EDICT_DONTSEND );
  3943. }
  3944. if ( m_pRecipientFilter )
  3945. {
  3946. return SetTransmitState( FL_EDICT_FULLCHECK );
  3947. }
  3948. return SetTransmitState( FL_EDICT_ALWAYS );
  3949. }
  3950. //-----------------------------------------------------------------------------
  3951. // Purpose: Which clients should we be transmitting to?
  3952. //-----------------------------------------------------------------------------
  3953. int CSceneEntity::ShouldTransmit( const CCheckTransmitInfo *pInfo )
  3954. {
  3955. int result = BaseClass::ShouldTransmit( pInfo );
  3956. // if we have excluded them via our recipient filter, don't send
  3957. if ( m_pRecipientFilter && result != FL_EDICT_DONTSEND )
  3958. {
  3959. bool bFound = false;
  3960. // If we can't find them in the recipient list, exclude
  3961. int i;
  3962. for ( i=0; i<m_pRecipientFilter->GetRecipientCount();i++ )
  3963. {
  3964. int iRecipient = m_pRecipientFilter->GetRecipientIndex(i);
  3965. CBasePlayer *player = static_cast< CBasePlayer * >( CBaseEntity::Instance( iRecipient ) );
  3966. if ( player && ( player->edict() == pInfo->m_pClientEnt ||
  3967. player->IsSplitScreenUserOnEdict( pInfo->m_pClientEnt ) ) )
  3968. {
  3969. bFound = true;
  3970. break;
  3971. }
  3972. }
  3973. if ( !bFound )
  3974. {
  3975. result = FL_EDICT_DONTSEND;
  3976. }
  3977. }
  3978. return result;
  3979. }
  3980. void CSceneEntity::SetRecipientFilter( IRecipientFilter *filter )
  3981. {
  3982. // create a copy of this filter
  3983. if ( filter )
  3984. {
  3985. m_pRecipientFilter = new CRecipientFilter();
  3986. m_pRecipientFilter->CopyFrom( (CRecipientFilter &)( *filter ) );
  3987. }
  3988. }
  3989. //-----------------------------------------------------------------------------
  3990. //-----------------------------------------------------------------------------
  3991. int CSceneEntity::DrawDebugTextOverlays()
  3992. {
  3993. int nOffset = BaseClass::DrawDebugTextOverlays();
  3994. if (m_debugOverlays & OVERLAY_TEXT_BIT)
  3995. {
  3996. char tempstr[512];
  3997. Q_snprintf( tempstr, sizeof( tempstr ), "Playing back: %s", m_bIsPlayingBack ? "yes" : "no" );
  3998. EntityText( nOffset, tempstr, 0 );
  3999. nOffset++;
  4000. Q_snprintf( tempstr, sizeof( tempstr ), "Paused: %s", m_bPaused ? ( m_bPausedViaInput ? "yes - via input" : "yes" ) : "no" );
  4001. EntityText( nOffset, tempstr, 0 );
  4002. nOffset++;
  4003. }
  4004. return nOffset;
  4005. }
  4006. //-----------------------------------------------------------------------------
  4007. // Purpose: Adds a player (by index) to the recipient filter
  4008. //-----------------------------------------------------------------------------
  4009. void CSceneEntity::AddBroadcastTeamTarget( int nTeamIndex )
  4010. {
  4011. if ( m_pRecipientFilter == NULL )
  4012. {
  4013. CRecipientFilter filter;
  4014. SetRecipientFilter( &filter );
  4015. }
  4016. CTeam *pTeam = GetGlobalTeam( nTeamIndex );
  4017. Assert( pTeam );
  4018. if ( pTeam == NULL )
  4019. return;
  4020. m_pRecipientFilter->AddRecipientsByTeam( pTeam );
  4021. }
  4022. //-----------------------------------------------------------------------------
  4023. // Purpose: Removes a player (by index) from the recipient filter
  4024. //-----------------------------------------------------------------------------
  4025. void CSceneEntity::RemoveBroadcastTeamTarget( int nTeamIndex )
  4026. {
  4027. if ( m_pRecipientFilter == NULL )
  4028. {
  4029. CRecipientFilter filter;
  4030. SetRecipientFilter( &filter );
  4031. }
  4032. CTeam *pTeam = GetGlobalTeam( nTeamIndex );
  4033. Assert( pTeam );
  4034. if ( pTeam == NULL )
  4035. return;
  4036. m_pRecipientFilter->RemoveRecipientsByTeam( pTeam );
  4037. }
  4038. //-----------------------------------------------------------------------------
  4039. // Purpose:
  4040. // Input :
  4041. // Output :
  4042. //-----------------------------------------------------------------------------
  4043. class CInstancedSceneEntity : public CSceneEntity
  4044. {
  4045. DECLARE_DATADESC();
  4046. DECLARE_CLASS( CInstancedSceneEntity, CSceneEntity );
  4047. public:
  4048. EHANDLE m_hOwner;
  4049. bool m_bHadOwner;
  4050. float m_flPostSpeakDelay;
  4051. float m_flPreDelay;
  4052. char m_szInstanceFilename[ CChoreoScene::MAX_SCENE_FILENAME ];
  4053. bool m_bIsBackground;
  4054. virtual void StartPlayback( void );
  4055. virtual void DoThink( float frametime );
  4056. virtual CBaseFlex *FindNamedActor( const char *name );
  4057. virtual CBaseEntity *FindNamedEntity( const char *name );
  4058. virtual float GetPostSpeakDelay() { return m_flPostSpeakDelay; }
  4059. virtual void SetPostSpeakDelay( float flDelay ) { m_flPostSpeakDelay = flDelay; }
  4060. virtual float GetPreDelay() { return m_flPreDelay; }
  4061. virtual void SetPreDelay( float flDelay ) { m_flPreDelay = flDelay; }
  4062. virtual void OnLoaded();
  4063. virtual void DispatchStartMoveTo( CChoreoScene *scene, CBaseFlex *actor, CBaseEntity *actor2, CChoreoEvent *event )
  4064. {
  4065. if (PassThrough( actor )) BaseClass::DispatchStartMoveTo( scene, actor, actor2, event );
  4066. };
  4067. virtual void DispatchEndMoveTo( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event )
  4068. {
  4069. if (PassThrough( actor )) BaseClass::DispatchEndMoveTo( scene, actor, event );
  4070. };
  4071. virtual void DispatchStartFace( CChoreoScene *scene, CBaseFlex *actor, CBaseEntity *actor2, CChoreoEvent *event )
  4072. {
  4073. if (PassThrough( actor )) BaseClass::DispatchStartFace( scene, actor, actor2, event );
  4074. };
  4075. virtual void DispatchEndFace( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event )
  4076. {
  4077. if (PassThrough( actor )) BaseClass::DispatchEndFace( scene, actor, event );
  4078. };
  4079. #if !defined( PORTAL2 )
  4080. virtual void DispatchStartSequence( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event ) { /* suppress */ };
  4081. virtual void DispatchEndSequence( CChoreoScene *scene, CBaseFlex *actor, CChoreoEvent *event ) { /* suppress */ };
  4082. virtual void DispatchPauseScene( CChoreoScene *scene, const char *parameters ) { /* suppress */ };
  4083. #endif // !PORTAL2
  4084. void OnRestore();
  4085. virtual float EstimateLength( void );
  4086. private:
  4087. bool PassThrough( CBaseFlex *actor );
  4088. };
  4089. LINK_ENTITY_TO_CLASS( instanced_scripted_scene, CInstancedSceneEntity );
  4090. //---------------------------------------------------------
  4091. // Save/Restore
  4092. //---------------------------------------------------------
  4093. BEGIN_DATADESC( CInstancedSceneEntity )
  4094. DEFINE_FIELD( m_hOwner, FIELD_EHANDLE ),
  4095. DEFINE_FIELD( m_bHadOwner, FIELD_BOOLEAN ),
  4096. DEFINE_FIELD( m_flPostSpeakDelay, FIELD_FLOAT ),
  4097. DEFINE_FIELD( m_flPreDelay, FIELD_FLOAT ),
  4098. DEFINE_AUTO_ARRAY( m_szInstanceFilename, FIELD_CHARACTER ),
  4099. DEFINE_FIELD( m_bIsBackground, FIELD_BOOLEAN ),
  4100. END_DATADESC()
  4101. int SceneNameAutocomplete( char const *partial, char commands[ COMMAND_COMPLETION_MAXITEMS ][ COMMAND_COMPLETION_ITEM_LENGTH ] )
  4102. {
  4103. // chop the command off the begining of the string
  4104. char *commandName = "scene_playvcd";
  4105. int numMatches = 0;
  4106. partial += Q_strlen( commandName ) + 1;
  4107. int partialLength = Q_strlen( partial );
  4108. // split the current path into the directory and the file.
  4109. char dirName[ 512 ];
  4110. Q_snprintf( dirName, sizeof( dirName ), "%sjunk", partial );
  4111. Q_ExtractFilePath( dirName, dirName, sizeof( dirName ) );
  4112. partial += Q_strlen( dirName );
  4113. partialLength = Q_strlen( partial );
  4114. // now rebuild our path so we can pick up any directory that might have been entered.
  4115. char path[ 512 ];
  4116. Q_snprintf( path, sizeof( path ), "scenes/%s%s*.*", dirName, partial );
  4117. FileFindHandle_t findHandle;
  4118. char txtFilenameNoExtension[ MAX_PATH ];
  4119. const char *txtFilename = filesystem->FindFirstEx( path, "MOD", &findHandle );
  4120. while ( txtFilename )
  4121. {
  4122. if ( txtFilename[0] != '.' ) // skip the parent and current directories "." and ".."
  4123. {
  4124. Q_FileBase( txtFilename, txtFilenameNoExtension, sizeof( txtFilenameNoExtension ) );
  4125. if ( !Q_strnicmp( txtFilenameNoExtension, partial, partialLength ) )
  4126. {
  4127. if ( filesystem->FindIsDirectory( findHandle ) )
  4128. {
  4129. // Add the directory name with a slash
  4130. Q_snprintf( commands[ numMatches++ ], COMMAND_COMPLETION_ITEM_LENGTH, "%s %s%s/", commandName, dirName, txtFilenameNoExtension );
  4131. }
  4132. else
  4133. {
  4134. // Add the file name to the autocomplete array
  4135. Q_snprintf( commands[ numMatches++ ], COMMAND_COMPLETION_ITEM_LENGTH, "%s %s%s", commandName, dirName, txtFilenameNoExtension );
  4136. }
  4137. if ( numMatches == COMMAND_COMPLETION_MAXITEMS )
  4138. {
  4139. filesystem->FindClose( findHandle );
  4140. return numMatches;
  4141. }
  4142. }
  4143. }
  4144. txtFilename = filesystem->FindNext( findHandle );
  4145. }
  4146. filesystem->FindClose( findHandle );
  4147. return numMatches;
  4148. }
  4149. CON_COMMAND_F_COMPLETION( scene_playvcd, "Play the given VCD as an instanced scripted scene.", FCVAR_CHEAT, SceneNameAutocomplete )
  4150. {
  4151. char vcdPath[ 512 ];
  4152. Q_snprintf( vcdPath, sizeof( vcdPath ), "scenes/%s.vcd", args[1] );
  4153. InstancedScriptedScene( NULL, vcdPath );
  4154. }
  4155. //-----------------------------------------------------------------------------
  4156. // Purpose: create a one-shot scene, no movement, sequences, etc.
  4157. // Input :
  4158. // Output :
  4159. //-----------------------------------------------------------------------------
  4160. float InstancedScriptedScene( CBaseFlex *pActor, const char *pszScene, EHANDLE *phSceneEnt,
  4161. float flPostDelay, bool bIsBackground, AI_Response *response,
  4162. bool bMultiplayer, IRecipientFilter *filter /* = NULL */ )
  4163. {
  4164. VPROF( "InstancedScriptedScene" );
  4165. CInstancedSceneEntity *pScene = (CInstancedSceneEntity *)CBaseEntity::CreateNoSpawn( "instanced_scripted_scene", vec3_origin, vec3_angle );
  4166. // This code expands any $gender tags into male or female tags based on the gender of the actor (based on his/her .mdl)
  4167. if ( pActor )
  4168. {
  4169. pActor->GenderExpandString( pszScene, pScene->m_szInstanceFilename, sizeof( pScene->m_szInstanceFilename ) );
  4170. }
  4171. else
  4172. {
  4173. Q_strncpy( pScene->m_szInstanceFilename, pszScene, sizeof( pScene->m_szInstanceFilename ) );
  4174. }
  4175. pScene->m_iszSceneFile = MAKE_STRING( pScene->m_szInstanceFilename );
  4176. // FIXME: I should set my output to fire something that kills me....
  4177. // FIXME: add a proper initialization function
  4178. pScene->m_hOwner = pActor;
  4179. pScene->m_bHadOwner = pActor != NULL;
  4180. pScene->m_bMultiplayer = bMultiplayer;
  4181. pScene->SetPostSpeakDelay( flPostDelay );
  4182. DispatchSpawn( pScene );
  4183. pScene->Activate();
  4184. pScene->m_bIsBackground = bIsBackground;
  4185. pScene->SetBackground( bIsBackground );
  4186. pScene->SetRecipientFilter( filter );
  4187. if ( response )
  4188. {
  4189. float flPreDelay = response->GetPreDelay();
  4190. if ( flPreDelay )
  4191. {
  4192. pScene->SetPreDelay( flPreDelay );
  4193. }
  4194. }
  4195. pScene->StartPlayback();
  4196. if ( response )
  4197. {
  4198. // If the response wants us to abort on NPC state switch, remember that
  4199. pScene->SetBreakOnNonIdle( response->ShouldBreakOnNonIdle() );
  4200. }
  4201. if ( phSceneEnt )
  4202. {
  4203. *phSceneEnt = pScene;
  4204. }
  4205. return pScene->EstimateLength();
  4206. }
  4207. //-----------------------------------------------------------------------------
  4208. // Purpose:
  4209. // Input : *pActor -
  4210. // *soundnmame -
  4211. // *phSceneEnt -
  4212. // Output : float
  4213. //-----------------------------------------------------------------------------
  4214. float InstancedAutoGeneratedSoundScene( CBaseFlex *pActor, char const *soundname, EHANDLE *phSceneEnt /*= NULL*/ )
  4215. {
  4216. if ( !pActor )
  4217. {
  4218. Warning( "InstancedAutoGeneratedSoundScene: Expecting non-NULL pActor for sound %s\n", soundname );
  4219. return 0;
  4220. }
  4221. CInstancedSceneEntity *pScene = (CInstancedSceneEntity *)CBaseEntity::CreateNoSpawn( "instanced_scripted_scene", vec3_origin, vec3_angle );
  4222. Q_strncpy( pScene->m_szInstanceFilename, UTIL_VarArgs( "AutoGenerated(%s)", soundname ), sizeof( pScene->m_szInstanceFilename ) );
  4223. pScene->m_iszSceneFile = MAKE_STRING( pScene->m_szInstanceFilename );
  4224. pScene->m_hOwner = pActor;
  4225. pScene->m_bHadOwner = pActor != NULL;
  4226. pScene->GenerateSoundScene( pActor, soundname );
  4227. pScene->Spawn();
  4228. pScene->Activate();
  4229. pScene->StartPlayback();
  4230. if ( phSceneEnt )
  4231. {
  4232. *phSceneEnt = pScene;
  4233. }
  4234. return pScene->EstimateLength();
  4235. }
  4236. //-----------------------------------------------------------------------------
  4237. void StopScriptedScene( CBaseFlex *pActor, EHANDLE hSceneEnt )
  4238. {
  4239. CBaseEntity *pEntity = hSceneEnt;
  4240. CSceneEntity *pScene = dynamic_cast<CSceneEntity *>(pEntity);
  4241. if ( pScene )
  4242. {
  4243. LocalScene_Printf( "%s : stop scripted scene\n", STRING( pScene->m_iszSceneFile ) );
  4244. pScene->CancelPlayback();
  4245. }
  4246. }
  4247. //-----------------------------------------------------------------------------
  4248. // Purpose:
  4249. // Input : *pszScene -
  4250. // Output : float
  4251. //-----------------------------------------------------------------------------
  4252. float GetSceneDuration( char const *pszScene )
  4253. {
  4254. unsigned int msecs = 0;
  4255. SceneCachedData_t cachedData;
  4256. if ( scenefilecache->GetSceneCachedData( pszScene, &cachedData ) )
  4257. {
  4258. msecs = cachedData.msecs;
  4259. }
  4260. return (float)msecs * 0.001f;
  4261. }
  4262. //-----------------------------------------------------------------------------
  4263. // Purpose:
  4264. // Input : *pszScene -
  4265. // Output : int
  4266. //-----------------------------------------------------------------------------
  4267. int GetSceneSpeechCount( char const *pszScene )
  4268. {
  4269. SceneCachedData_t cachedData;
  4270. if ( scenefilecache->GetSceneCachedData( pszScene, &cachedData ) )
  4271. {
  4272. return cachedData.numSounds;
  4273. }
  4274. return 0;
  4275. }
  4276. class CScenePrecacheSystem : public CAutoGameSystem
  4277. {
  4278. public:
  4279. CScenePrecacheSystem() : CAutoGameSystem( "CScenePrecacheSystem" ), m_RepeatCounts( 0, 0, DefLessFunc( int ) )
  4280. {
  4281. }
  4282. // Level init, shutdown
  4283. virtual void LevelShutdownPreEntity()
  4284. {
  4285. m_RepeatCounts.Purge();
  4286. }
  4287. bool ShouldPrecache( char const *pszScene )
  4288. {
  4289. int hash = HashStringCaselessConventional( pszScene );
  4290. int slot = m_RepeatCounts.Find( hash );
  4291. if ( slot != m_RepeatCounts.InvalidIndex() )
  4292. {
  4293. m_RepeatCounts[ slot ]++;
  4294. return false;
  4295. }
  4296. m_RepeatCounts.Insert( hash, 0 );
  4297. return true;
  4298. }
  4299. private:
  4300. CUtlMap< int, int > m_RepeatCounts;
  4301. };
  4302. static CScenePrecacheSystem g_ScenePrecacheSystem;
  4303. //-----------------------------------------------------------------------------
  4304. // Purpose: Used for precaching instanced scenes
  4305. // Input : *pszScene -
  4306. //-----------------------------------------------------------------------------
  4307. void PrecacheInstancedScene( char const *pszScene )
  4308. {
  4309. static int nMakingReslists = -1;
  4310. if ( !g_ScenePrecacheSystem.ShouldPrecache( pszScene ) )
  4311. return;
  4312. if ( nMakingReslists == -1 )
  4313. {
  4314. nMakingReslists = CommandLine()->FindParm( "-makereslists" ) > 0 ? 1 : 0;
  4315. }
  4316. if ( nMakingReslists == 1 )
  4317. {
  4318. // Just stat the file to add to reslist
  4319. g_pFullFileSystem->Size( pszScene );
  4320. }
  4321. // verify existence, cache is pre-populated, should be there
  4322. SceneCachedData_t sceneData;
  4323. if ( !scenefilecache->GetSceneCachedData( pszScene, &sceneData ) )
  4324. {
  4325. // Scenes are sloppy and don't always exist.
  4326. // A scene that is not in the pre-built cache image, but on disk, is a true error.
  4327. if ( IsGameConsole() && ( g_pFullFileSystem->GetDVDMode() != DVDMODE_STRICT ) && g_pFullFileSystem->FileExists( pszScene, "GAME" ) )
  4328. {
  4329. Warning( "PrecacheInstancedScene: Missing scene '%s' from scene image cache.\nRebuild scene image cache!\n", pszScene );
  4330. }
  4331. }
  4332. else
  4333. {
  4334. for ( int i = 0; i < sceneData.numSounds; ++i )
  4335. {
  4336. short stringId = scenefilecache->GetSceneCachedSound( sceneData.sceneId, i );
  4337. CBaseEntity::PrecacheScriptSound( scenefilecache->GetSceneString( stringId ) );
  4338. }
  4339. }
  4340. g_pStringTableClientSideChoreoScenes->AddString( CBaseEntity::IsServer(), pszScene );
  4341. }
  4342. HSCRIPT ScriptCreateSceneEntity( const char* pszScene )
  4343. {
  4344. if ( IsEntityCreationAllowedInScripts() == false )
  4345. {
  4346. Warning( "VScript error: A script attempted to create a scene entity mid-game. Entity creation from scripts is only allowed during map init.\n" );
  4347. return NULL;
  4348. }
  4349. g_pScriptVM->RegisterClass( GetScriptDescForClass( CSceneEntity ) );
  4350. CSceneEntity *pScene = (CSceneEntity *)CBaseEntity::CreateNoSpawn( "logic_choreographed_scene", vec3_origin, vec3_angle );
  4351. if ( pScene )
  4352. {
  4353. pScene->m_iszSceneFile = AllocPooledString( pszScene );
  4354. DispatchSpawn( pScene );
  4355. }
  4356. return ToHScript( pScene );
  4357. }
  4358. //-----------------------------------------------------------------------------
  4359. // Purpose:
  4360. //-----------------------------------------------------------------------------
  4361. void CInstancedSceneEntity::StartPlayback( void )
  4362. {
  4363. // Wait until our pre delay is over
  4364. if ( GetPreDelay() )
  4365. return;
  4366. BaseClass::StartPlayback();
  4367. }
  4368. //-----------------------------------------------------------------------------
  4369. // Purpose:
  4370. // Input :
  4371. // Output :
  4372. //-----------------------------------------------------------------------------
  4373. void CInstancedSceneEntity::DoThink( float frametime )
  4374. {
  4375. CheckInterruptCompletion();
  4376. if ( m_flPreDelay > 0 )
  4377. {
  4378. m_flPreDelay = MAX( 0, m_flPreDelay - frametime );
  4379. StartPlayback();
  4380. if ( !m_bIsPlayingBack )
  4381. return;
  4382. }
  4383. if ( !m_pScene || !m_bIsPlayingBack || ( m_bHadOwner && m_hOwner == NULL ) )
  4384. {
  4385. UTIL_Remove( this );
  4386. return;
  4387. }
  4388. // catch bad pitch shifting from old save games
  4389. Assert( m_fPitch >= SCENE_MIN_PITCH && m_fPitch <= SCENE_MAX_PITCH );
  4390. m_fPitch = clamp( m_fPitch, SCENE_MIN_PITCH, SCENE_MAX_PITCH );
  4391. if ( m_bPaused )
  4392. {
  4393. PauseThink();
  4394. return;
  4395. }
  4396. float dt = frametime;
  4397. m_pScene->SetSoundFileStartupLatency( GetSoundSystemLatency() );
  4398. // Tell scene to go
  4399. m_pScene->Think( m_flCurrentTime );
  4400. // Drive simulation time for scene
  4401. SetCurrentTime( m_flCurrentTime + dt * m_fPitch, false );
  4402. // Did we get to the end
  4403. if ( m_pScene->SimulationFinished() || m_bCompletedEarly )
  4404. {
  4405. OnSceneFinished( false, false );
  4406. UTIL_Remove( this );
  4407. }
  4408. }
  4409. //-----------------------------------------------------------------------------
  4410. // Purpose: Search for an actor by name, make sure it can do face poses
  4411. // Input : *name -
  4412. // Output : CBaseFlex
  4413. //-----------------------------------------------------------------------------
  4414. CBaseFlex *CInstancedSceneEntity::FindNamedActor( const char *name )
  4415. {
  4416. if ( m_pScene->GetNumActors() == 1 || stricmp( name, "!self" ) == 0 )
  4417. {
  4418. if ( m_hOwner != NULL )
  4419. {
  4420. CBaseCombatCharacter *pCharacter = m_hOwner->MyCombatCharacterPointer();
  4421. if ( pCharacter )
  4422. {
  4423. return pCharacter;
  4424. }
  4425. }
  4426. }
  4427. return BaseClass::FindNamedActor( name );
  4428. }
  4429. //-----------------------------------------------------------------------------
  4430. // Purpose: Search for an actor by name, make sure it can do face poses
  4431. // Input : *name -
  4432. // Output : CBaseFlex
  4433. //-----------------------------------------------------------------------------
  4434. CBaseEntity *CInstancedSceneEntity::FindNamedEntity( const char *name )
  4435. {
  4436. CBaseEntity *pOther = NULL;
  4437. if (m_hOwner != NULL)
  4438. {
  4439. CAI_BaseNPC *npc = m_hOwner->MyNPCPointer();
  4440. if (npc)
  4441. {
  4442. pOther = npc->FindNamedEntity( name );
  4443. }
  4444. else if ( m_hOwner->MyCombatCharacterPointer() )
  4445. {
  4446. pOther = m_hOwner;
  4447. }
  4448. }
  4449. if (!pOther)
  4450. {
  4451. pOther = BaseClass::FindNamedEntity( name );
  4452. }
  4453. return pOther;
  4454. }
  4455. //-----------------------------------------------------------------------------
  4456. // Purpose: Suppress certain events when it's instanced since they're can cause odd problems
  4457. // Input : actor
  4458. // Output : true - the event should happen, false - it shouldn't
  4459. //-----------------------------------------------------------------------------
  4460. bool CInstancedSceneEntity::PassThrough( CBaseFlex *actor )
  4461. {
  4462. if (!actor)
  4463. return false;
  4464. CAI_BaseNPC *myNpc = actor->MyNPCPointer( );
  4465. if (!myNpc)
  4466. return false;
  4467. if (myNpc->IsCurSchedule( SCHED_SCENE_GENERIC ))
  4468. {
  4469. return true;
  4470. }
  4471. if (myNpc->GetCurSchedule())
  4472. {
  4473. CAI_ScheduleBits testBits;
  4474. myNpc->GetCurSchedule()->GetInterruptMask( &testBits );
  4475. if (testBits.IsBitSet( COND_IDLE_INTERRUPT ))
  4476. {
  4477. return true;
  4478. }
  4479. }
  4480. LocalScene_Printf( "%s : event suppressed\n", STRING( m_iszSceneFile ) );
  4481. return false;
  4482. }
  4483. //-----------------------------------------------------------------------------
  4484. void CInstancedSceneEntity::OnRestore()
  4485. {
  4486. if ( m_bHadOwner && !m_hOwner )
  4487. {
  4488. // probably just came back from a level transition
  4489. UTIL_Remove( this );
  4490. return;
  4491. }
  4492. // reset background state
  4493. if ( m_pScene )
  4494. {
  4495. m_pScene->SetBackground( m_bIsBackground );
  4496. }
  4497. BaseClass::OnRestore();
  4498. }
  4499. //-----------------------------------------------------------------------------
  4500. // Purpose:
  4501. //-----------------------------------------------------------------------------
  4502. float CInstancedSceneEntity::EstimateLength( void )
  4503. {
  4504. return (BaseClass::EstimateLength() + GetPreDelay());
  4505. }
  4506. void CInstancedSceneEntity::OnLoaded()
  4507. {
  4508. BaseClass::OnLoaded();
  4509. SetBackground( m_bIsBackground );
  4510. }
  4511. bool g_bClientFlex = true;
  4512. LINK_ENTITY_TO_CLASS( scene_manager, CSceneManager );
  4513. //-----------------------------------------------------------------------------
  4514. // Purpose:
  4515. //-----------------------------------------------------------------------------
  4516. void CSceneManager::Think()
  4517. {
  4518. // Latch this only once per frame...
  4519. g_bClientFlex = scene_clientflex.GetBool();
  4520. // The manager is always thinking at 20 hz
  4521. SetNextThink( gpGlobals->curtime + SCENE_THINK_INTERVAL );
  4522. float frameTime = ( gpGlobals->curtime - GetLastThink() );
  4523. frameTime = MIN( 0.1, frameTime );
  4524. // stop if AI is disabled
  4525. if (CAI_BaseNPC::m_nDebugBits & bits_debugDisableAI)
  4526. return;
  4527. bool needCleanupPass = false;
  4528. int c = m_ActiveScenes.Count();
  4529. for ( int i = 0; i < c; i++ )
  4530. {
  4531. CSceneEntity *scene = m_ActiveScenes[ i ].Get();
  4532. if ( !scene )
  4533. {
  4534. needCleanupPass = true;
  4535. continue;
  4536. }
  4537. if ( IsX360() || IsPS3() )
  4538. {
  4539. if ( i+1 < c )
  4540. {
  4541. PREFETCH360( &scene->m_pScene, 0 );
  4542. }
  4543. }
  4544. scene->DoThink( frameTime );
  4545. if ( m_ActiveScenes.Count() < c )
  4546. {
  4547. // Scene removed self while thinking. Adjust iteration.
  4548. c = m_ActiveScenes.Count();
  4549. i--;
  4550. }
  4551. }
  4552. // Now delete any invalid ones
  4553. if ( needCleanupPass )
  4554. {
  4555. for ( int i = c - 1; i >= 0; i-- )
  4556. {
  4557. CSceneEntity *scene = m_Scenes[ i ].Get();
  4558. if ( scene )
  4559. continue;
  4560. m_Scenes.Remove( i );
  4561. }
  4562. c = m_ActiveScenes.Count();
  4563. for ( int i = c - 1; i >= 0; i-- )
  4564. {
  4565. CSceneEntity *scene = m_ActiveScenes[ i ].Get();
  4566. if ( scene )
  4567. continue;
  4568. m_ActiveScenes.Remove( i );
  4569. }
  4570. }
  4571. }
  4572. //-----------------------------------------------------------------------------
  4573. // Purpose:
  4574. //-----------------------------------------------------------------------------
  4575. void CSceneManager::ClearAllScenes()
  4576. {
  4577. m_Scenes.RemoveAll();
  4578. }
  4579. //-----------------------------------------------------------------------------
  4580. // Purpose:
  4581. // Input : *scene -
  4582. //-----------------------------------------------------------------------------
  4583. void CSceneManager::AddSceneEntity( CSceneEntity *scene )
  4584. {
  4585. CHandle< CSceneEntity > h;
  4586. h = scene;
  4587. // Already added/activated
  4588. if ( m_Scenes.Find( h ) != m_Scenes.InvalidIndex() )
  4589. {
  4590. return;
  4591. }
  4592. m_Scenes.AddToTail( h );
  4593. if ( scene->m_pScene )
  4594. {
  4595. ActivateScene( scene );
  4596. }
  4597. }
  4598. //-----------------------------------------------------------------------------
  4599. // Purpose:
  4600. // Input : *scene -
  4601. //-----------------------------------------------------------------------------
  4602. void CSceneManager::RemoveSceneEntity( CSceneEntity *scene )
  4603. {
  4604. DeactivateScene( scene );
  4605. CHandle< CSceneEntity > h;
  4606. h = scene;
  4607. m_Scenes.FindAndRemove( h );
  4608. }
  4609. //-----------------------------------------------------------------------------
  4610. // Purpose:
  4611. // Input : *scene -
  4612. //-----------------------------------------------------------------------------
  4613. void CSceneManager::ActivateScene( CSceneEntity *scene )
  4614. {
  4615. AddSceneEntity( scene );
  4616. CHandle< CSceneEntity > h;
  4617. h = scene;
  4618. // Already added/activated
  4619. if ( m_ActiveScenes.Find( h ) != m_ActiveScenes.InvalidIndex() )
  4620. {
  4621. return;
  4622. }
  4623. m_ActiveScenes.AddToTail( h );
  4624. }
  4625. //-----------------------------------------------------------------------------
  4626. // Purpose:
  4627. // Input : *scene -
  4628. //-----------------------------------------------------------------------------
  4629. void CSceneManager::DeactivateScene( CSceneEntity *scene )
  4630. {
  4631. CHandle< CSceneEntity > h;
  4632. h = scene;
  4633. m_ActiveScenes.FindAndRemove( h );
  4634. }
  4635. //-----------------------------------------------------------------------------
  4636. // Purpose:
  4637. // Input : *player -
  4638. //-----------------------------------------------------------------------------
  4639. void CSceneManager::OnClientActive( CBasePlayer *player )
  4640. {
  4641. int c = m_QueuedSceneSounds.Count();
  4642. for ( int i = 0; i < c; i++ )
  4643. {
  4644. CRestoreSceneSound *sound = &m_QueuedSceneSounds[ i ];
  4645. if ( sound->actor == NULL )
  4646. continue;
  4647. #if !SEND_SOUND_TIME
  4648. // Blow off sounds too far in past to encode over networking layer
  4649. if ( fabs( 1000.0f * sound->time_in_past ) > MAX_SOUND_DELAY_MSEC )
  4650. continue;
  4651. #endif
  4652. CPASAttenuationFilter filter( sound->actor );
  4653. EmitSound_t es;
  4654. es.m_nChannel = CHAN_VOICE;
  4655. es.m_flVolume = 1;
  4656. es.m_pSoundName = sound->soundname;
  4657. es.m_SoundLevel = sound->soundlevel;
  4658. es.m_flSoundTime = gpGlobals->curtime - sound->time_in_past;
  4659. #if defined ( PORTAL2 )
  4660. if ( GameRules()->IsMultiplayer() == false && GetPlayerHoldingEntity( sound->actor ) )
  4661. {
  4662. // HACK: Don't attenuate player held object sounds
  4663. // This is to fix bugs when walking through portals with the sphere npc.
  4664. es.m_SoundLevel = SNDLVL_NONE;
  4665. }
  4666. #endif
  4667. EmitSound( filter, sound->actor->entindex(), es );
  4668. }
  4669. m_QueuedSceneSounds.RemoveAll();
  4670. }
  4671. //-----------------------------------------------------------------------------
  4672. // Purpose: Stops scenes involving the specified actor
  4673. //-----------------------------------------------------------------------------
  4674. void CSceneManager::RemoveActorFromScenes( CBaseFlex *pActor, bool bInstancedOnly, bool bNonIdleOnly, const char *pszThisSceneOnly )
  4675. {
  4676. int c = m_Scenes.Count();
  4677. for ( int i = 0; i < c; i++ )
  4678. {
  4679. CSceneEntity *pScene = m_Scenes[ i ].Get();
  4680. if ( !pScene )
  4681. {
  4682. continue;
  4683. }
  4684. // If only stopping instanced scenes, then skip it if it can't cast to an instanced scene
  4685. if ( bInstancedOnly &&
  4686. ( dynamic_cast< CInstancedSceneEntity * >( pScene ) == NULL ) )
  4687. {
  4688. continue;
  4689. }
  4690. if ( bNonIdleOnly && !pScene->ShouldBreakOnNonIdle() )
  4691. continue;
  4692. if ( pScene->InvolvesActor( pActor ) )
  4693. {
  4694. if ( pszThisSceneOnly && pszThisSceneOnly[0] )
  4695. {
  4696. if ( Q_strcmp( pszThisSceneOnly, STRING(pScene->m_iszSceneFile) ) )
  4697. continue;
  4698. }
  4699. if ( scene_print.GetBool() )
  4700. {
  4701. LocalScene_Printf( "%s : removed for '%s'\n", STRING( pScene->m_iszSceneFile ), pActor ? pActor->GetDebugName() : "NULL" );
  4702. }
  4703. pScene->CancelPlayback();
  4704. }
  4705. }
  4706. }
  4707. //-----------------------------------------------------------------------------
  4708. // Purpose: Pause scenes involving the specified actor
  4709. //-----------------------------------------------------------------------------
  4710. void CSceneManager::PauseActorsScenes( CBaseFlex *pActor, bool bInstancedOnly )
  4711. {
  4712. int c = m_Scenes.Count();
  4713. for ( int i = 0; i < c; i++ )
  4714. {
  4715. CSceneEntity *pScene = m_Scenes[ i ].Get();
  4716. if ( !pScene )
  4717. {
  4718. continue;
  4719. }
  4720. // If only stopping instanced scenes, then skip it if it can't cast to an instanced scene
  4721. if ( bInstancedOnly &&
  4722. ( dynamic_cast< CInstancedSceneEntity * >( pScene ) == NULL ) )
  4723. {
  4724. continue;
  4725. }
  4726. if ( pScene->InvolvesActor( pActor ) && pScene->IsPlayingBack() )
  4727. {
  4728. LocalScene_Printf( "Pausing actor %s scripted scene: %s\n", pActor->GetDebugName(), STRING( pScene->m_iszSceneFile ) );
  4729. variant_t emptyVariant;
  4730. pScene->AcceptInput( "Pause", pScene, pScene, emptyVariant, 0 );
  4731. }
  4732. }
  4733. }
  4734. //-----------------------------------------------------------------------------
  4735. // Purpose: Return true if this Actor is only in scenes that are interruptable right now
  4736. //-----------------------------------------------------------------------------
  4737. bool CSceneManager::IsInInterruptableScenes( CBaseFlex *pActor )
  4738. {
  4739. int c = m_Scenes.Count();
  4740. for ( int i = 0; i < c; i++ )
  4741. {
  4742. CSceneEntity *pScene = m_Scenes[ i ].Get();
  4743. if ( !pScene )
  4744. continue;
  4745. //Ignore background scenes since they're harmless.
  4746. if ( pScene->IsBackground() == true )
  4747. continue;
  4748. if ( pScene->InvolvesActor( pActor ) && pScene->IsPlayingBack() )
  4749. {
  4750. if ( pScene->IsInterruptable() == false )
  4751. return false;
  4752. }
  4753. }
  4754. return true;
  4755. }
  4756. //-----------------------------------------------------------------------------
  4757. // Purpose: Resume any paused scenes involving the specified actor
  4758. //-----------------------------------------------------------------------------
  4759. void CSceneManager::ResumeActorsScenes( CBaseFlex *pActor, bool bInstancedOnly )
  4760. {
  4761. int c = m_Scenes.Count();
  4762. for ( int i = 0; i < c; i++ )
  4763. {
  4764. CSceneEntity *pScene = m_Scenes[ i ].Get();
  4765. if ( !pScene )
  4766. {
  4767. continue;
  4768. }
  4769. // If only stopping instanced scenes, then skip it if it can't cast to an instanced scene
  4770. if ( bInstancedOnly &&
  4771. ( dynamic_cast< CInstancedSceneEntity * >( pScene ) == NULL ) )
  4772. {
  4773. continue;
  4774. }
  4775. if ( pScene->InvolvesActor( pActor ) && pScene->IsPlayingBack() )
  4776. {
  4777. LocalScene_Printf( "Resuming actor %s scripted scene: %s\n", pActor->GetDebugName(), STRING( pScene->m_iszSceneFile ) );
  4778. variant_t emptyVariant;
  4779. pScene->AcceptInput( "Resume", pScene, pScene, emptyVariant, 0 );
  4780. }
  4781. }
  4782. }
  4783. //-----------------------------------------------------------------------------
  4784. // Purpose: Set all paused, in-playback scenes to resume when the actor is ready
  4785. //-----------------------------------------------------------------------------
  4786. void CSceneManager::QueueActorsScenesToResume( CBaseFlex *pActor, bool bInstancedOnly )
  4787. {
  4788. int c = m_Scenes.Count();
  4789. for ( int i = 0; i < c; i++ )
  4790. {
  4791. CSceneEntity *pScene = m_Scenes[ i ].Get();
  4792. if ( !pScene )
  4793. {
  4794. continue;
  4795. }
  4796. // If only stopping instanced scenes, then skip it if it can't cast to an instanced scene
  4797. if ( bInstancedOnly &&
  4798. ( dynamic_cast< CInstancedSceneEntity * >( pScene ) == NULL ) )
  4799. {
  4800. continue;
  4801. }
  4802. if ( pScene->InvolvesActor( pActor ) && pScene->IsPlayingBack() && pScene->IsPaused() )
  4803. {
  4804. pScene->QueueResumePlayback();
  4805. }
  4806. }
  4807. }
  4808. //-----------------------------------------------------------------------------
  4809. // Purpose: returns if there are scenes involving the specified actor
  4810. //-----------------------------------------------------------------------------
  4811. bool CSceneManager::IsRunningScriptedScene( CBaseFlex *pActor, bool bIgnoreInstancedScenes )
  4812. {
  4813. int c = m_Scenes.Count();
  4814. for ( int i = 0; i < c; i++ )
  4815. {
  4816. CSceneEntity *pScene = m_Scenes[ i ].Get();
  4817. if ( !pScene ||
  4818. !pScene->IsPlayingBack() ||
  4819. ( bIgnoreInstancedScenes && dynamic_cast<CInstancedSceneEntity *>(pScene) != NULL )
  4820. )
  4821. {
  4822. continue;
  4823. }
  4824. if ( pScene->InvolvesActor( pActor ) )
  4825. {
  4826. return true;
  4827. }
  4828. }
  4829. return false;
  4830. }
  4831. bool CSceneManager::IsRunningScriptedSceneAndNotPaused( CBaseFlex *pActor, bool bIgnoreInstancedScenes )
  4832. {
  4833. int c = m_Scenes.Count();
  4834. for ( int i = 0; i < c; i++ )
  4835. {
  4836. CSceneEntity *pScene = m_Scenes[ i ].Get();
  4837. if ( !pScene ||
  4838. !pScene->IsPlayingBack() ||
  4839. pScene->IsPaused() ||
  4840. ( bIgnoreInstancedScenes && dynamic_cast<CInstancedSceneEntity *>(pScene) != NULL )
  4841. )
  4842. {
  4843. continue;
  4844. }
  4845. if ( pScene->InvolvesActor( pActor ) )
  4846. {
  4847. return true;
  4848. }
  4849. }
  4850. return false;
  4851. }
  4852. //-----------------------------------------------------------------------------
  4853. // Purpose:
  4854. // Input : *pActor -
  4855. // Output : Returns true on success, false on failure.
  4856. //-----------------------------------------------------------------------------
  4857. bool CSceneManager::IsRunningScriptedSceneWithSpeech( CBaseFlex *pActor, bool bIgnoreInstancedScenes )
  4858. {
  4859. int c = m_Scenes.Count();
  4860. for ( int i = 0; i < c; i++ )
  4861. {
  4862. CSceneEntity *pScene = m_Scenes[ i ].Get();
  4863. if ( !pScene ||
  4864. !pScene->IsPlayingBack() ||
  4865. ( bIgnoreInstancedScenes && dynamic_cast<CInstancedSceneEntity *>(pScene) != NULL )
  4866. )
  4867. {
  4868. continue;
  4869. }
  4870. if ( pScene->InvolvesActor( pActor ) )
  4871. {
  4872. if ( pScene->HasUnplayedSpeech() )
  4873. return true;
  4874. }
  4875. }
  4876. return false;
  4877. }
  4878. bool CSceneManager::IsRunningScriptedSceneWithSpeechAndNotPaused( CBaseFlex *pActor, bool bIgnoreInstancedScenes )
  4879. {
  4880. int c = m_Scenes.Count();
  4881. for ( int i = 0; i < c; i++ )
  4882. {
  4883. CSceneEntity *pScene = m_Scenes[ i ].Get();
  4884. if ( !pScene ||
  4885. !pScene->IsPlayingBack() ||
  4886. pScene->IsPaused() ||
  4887. ( bIgnoreInstancedScenes && dynamic_cast<CInstancedSceneEntity *>(pScene) != NULL )
  4888. )
  4889. {
  4890. continue;
  4891. }
  4892. if ( pScene->InvolvesActor( pActor ) )
  4893. {
  4894. if ( pScene->HasUnplayedSpeech() )
  4895. return true;
  4896. }
  4897. }
  4898. return false;
  4899. }
  4900. //-----------------------------------------------------------------------------
  4901. // Purpose:
  4902. // Input : *actor -
  4903. // *soundname -
  4904. // soundlevel -
  4905. // soundtime -
  4906. //-----------------------------------------------------------------------------
  4907. void CSceneManager::QueueRestoredSound( CBaseFlex *actor, char const *soundname, soundlevel_t soundlevel, float time_in_past )
  4908. {
  4909. CRestoreSceneSound e;
  4910. e.actor = actor;
  4911. Q_strncpy( e.soundname, soundname, sizeof( e.soundname ) );
  4912. e.soundlevel = soundlevel;
  4913. e.time_in_past = time_in_past;
  4914. m_QueuedSceneSounds.AddToTail( e );
  4915. }
  4916. //-----------------------------------------------------------------------------
  4917. //-----------------------------------------------------------------------------
  4918. void RemoveActorFromScriptedScenes( CBaseFlex *pActor, bool instancedscenesonly, bool nonidlescenesonly, const char *pszThisSceneOnly )
  4919. {
  4920. GetSceneManager()->RemoveActorFromScenes( pActor, instancedscenesonly, nonidlescenesonly, pszThisSceneOnly );
  4921. }
  4922. //-----------------------------------------------------------------------------
  4923. //-----------------------------------------------------------------------------
  4924. void PauseActorsScriptedScenes( CBaseFlex *pActor, bool instancedscenesonly )
  4925. {
  4926. GetSceneManager()->PauseActorsScenes( pActor, instancedscenesonly );
  4927. }
  4928. //-----------------------------------------------------------------------------
  4929. //-----------------------------------------------------------------------------
  4930. bool IsInInterruptableScenes( CBaseFlex *pActor )
  4931. {
  4932. return GetSceneManager()->IsInInterruptableScenes( pActor );
  4933. }
  4934. //-----------------------------------------------------------------------------
  4935. //-----------------------------------------------------------------------------
  4936. void ResumeActorsScriptedScenes( CBaseFlex *pActor, bool instancedscenesonly )
  4937. {
  4938. GetSceneManager()->ResumeActorsScenes( pActor, instancedscenesonly );
  4939. }
  4940. //-----------------------------------------------------------------------------
  4941. //-----------------------------------------------------------------------------
  4942. void QueueActorsScriptedScenesToResume( CBaseFlex *pActor, bool instancedscenesonly )
  4943. {
  4944. GetSceneManager()->QueueActorsScenesToResume( pActor, instancedscenesonly );
  4945. }
  4946. //-----------------------------------------------------------------------------
  4947. //-----------------------------------------------------------------------------
  4948. bool IsRunningScriptedScene( CBaseFlex *pActor, bool bIgnoreInstancedScenes )
  4949. {
  4950. return GetSceneManager()->IsRunningScriptedScene( pActor, bIgnoreInstancedScenes );
  4951. }
  4952. bool IsRunningScriptedSceneAndNotPaused( CBaseFlex *pActor, bool bIgnoreInstancedScenes )
  4953. {
  4954. return GetSceneManager()->IsRunningScriptedSceneAndNotPaused( pActor, bIgnoreInstancedScenes );
  4955. }
  4956. //-----------------------------------------------------------------------------
  4957. //-----------------------------------------------------------------------------
  4958. bool IsRunningScriptedSceneWithSpeech( CBaseFlex *pActor, bool bIgnoreInstancedScenes )
  4959. {
  4960. return GetSceneManager()->IsRunningScriptedSceneWithSpeech( pActor, bIgnoreInstancedScenes );
  4961. }
  4962. bool IsRunningScriptedSceneWithSpeechAndNotPaused( CBaseFlex *pActor, bool bIgnoreInstancedScenes )
  4963. {
  4964. return GetSceneManager()->IsRunningScriptedSceneWithSpeechAndNotPaused( pActor, bIgnoreInstancedScenes );
  4965. }
  4966. //===========================================================================================================
  4967. // SCENE LIST MANAGER
  4968. //===========================================================================================================
  4969. LINK_ENTITY_TO_CLASS( logic_scene_list_manager, CSceneListManager );
  4970. BEGIN_DATADESC( CSceneListManager )
  4971. DEFINE_UTLVECTOR( m_hListManagers, FIELD_EHANDLE ),
  4972. // Keys
  4973. DEFINE_KEYFIELD( m_iszScenes[0], FIELD_STRING, "scene0" ),
  4974. DEFINE_KEYFIELD( m_iszScenes[1], FIELD_STRING, "scene1" ),
  4975. DEFINE_KEYFIELD( m_iszScenes[2], FIELD_STRING, "scene2" ),
  4976. DEFINE_KEYFIELD( m_iszScenes[3], FIELD_STRING, "scene3" ),
  4977. DEFINE_KEYFIELD( m_iszScenes[4], FIELD_STRING, "scene4" ),
  4978. DEFINE_KEYFIELD( m_iszScenes[5], FIELD_STRING, "scene5" ),
  4979. DEFINE_KEYFIELD( m_iszScenes[6], FIELD_STRING, "scene6" ),
  4980. DEFINE_KEYFIELD( m_iszScenes[7], FIELD_STRING, "scene7" ),
  4981. DEFINE_KEYFIELD( m_iszScenes[8], FIELD_STRING, "scene8" ),
  4982. DEFINE_KEYFIELD( m_iszScenes[9], FIELD_STRING, "scene9" ),
  4983. DEFINE_KEYFIELD( m_iszScenes[10], FIELD_STRING, "scene10" ),
  4984. DEFINE_KEYFIELD( m_iszScenes[11], FIELD_STRING, "scene11" ),
  4985. DEFINE_KEYFIELD( m_iszScenes[12], FIELD_STRING, "scene12" ),
  4986. DEFINE_KEYFIELD( m_iszScenes[13], FIELD_STRING, "scene13" ),
  4987. DEFINE_KEYFIELD( m_iszScenes[14], FIELD_STRING, "scene14" ),
  4988. DEFINE_KEYFIELD( m_iszScenes[15], FIELD_STRING, "scene15" ),
  4989. DEFINE_FIELD( m_hScenes[0], FIELD_EHANDLE ),
  4990. DEFINE_FIELD( m_hScenes[1], FIELD_EHANDLE ),
  4991. DEFINE_FIELD( m_hScenes[2], FIELD_EHANDLE ),
  4992. DEFINE_FIELD( m_hScenes[3], FIELD_EHANDLE ),
  4993. DEFINE_FIELD( m_hScenes[4], FIELD_EHANDLE ),
  4994. DEFINE_FIELD( m_hScenes[5], FIELD_EHANDLE ),
  4995. DEFINE_FIELD( m_hScenes[6], FIELD_EHANDLE ),
  4996. DEFINE_FIELD( m_hScenes[7], FIELD_EHANDLE ),
  4997. DEFINE_FIELD( m_hScenes[8], FIELD_EHANDLE ),
  4998. DEFINE_FIELD( m_hScenes[9], FIELD_EHANDLE ),
  4999. DEFINE_FIELD( m_hScenes[10], FIELD_EHANDLE ),
  5000. DEFINE_FIELD( m_hScenes[11], FIELD_EHANDLE ),
  5001. DEFINE_FIELD( m_hScenes[12], FIELD_EHANDLE ),
  5002. DEFINE_FIELD( m_hScenes[13], FIELD_EHANDLE ),
  5003. DEFINE_FIELD( m_hScenes[14], FIELD_EHANDLE ),
  5004. DEFINE_FIELD( m_hScenes[15], FIELD_EHANDLE ),
  5005. // Inputs
  5006. DEFINE_INPUTFUNC( FIELD_VOID, "Shutdown", InputShutdown ),
  5007. END_DATADESC()
  5008. //-----------------------------------------------------------------------------
  5009. // Purpose:
  5010. //-----------------------------------------------------------------------------
  5011. void CSceneListManager::Activate( void )
  5012. {
  5013. BaseClass::Activate();
  5014. // Hook up scenes, but not after loading a game because they're saved.
  5015. if ( gpGlobals->eLoadType != MapLoad_LoadGame )
  5016. {
  5017. for ( int i = 0; i < SCENE_LIST_MANAGER_MAX_SCENES; i++ )
  5018. {
  5019. if ( m_iszScenes[i] != NULL_STRING )
  5020. {
  5021. m_hScenes[i] = gEntList.FindEntityByName( NULL, STRING(m_iszScenes[i]) );
  5022. if ( m_hScenes[i] )
  5023. {
  5024. CSceneEntity *pScene = dynamic_cast<CSceneEntity*>(m_hScenes[i].Get());
  5025. if ( pScene )
  5026. {
  5027. pScene->AddListManager( this );
  5028. }
  5029. else
  5030. {
  5031. CSceneListManager *pList = dynamic_cast<CSceneListManager*>(m_hScenes[i].Get());
  5032. if ( pList )
  5033. {
  5034. pList->AddListManager( this );
  5035. }
  5036. else
  5037. {
  5038. Warning( "%s(%s) found an entity that wasn't a logic_choreographed_scene or logic_scene_list_manager in slot %d, named %s\n", GetDebugName(), GetClassname(), i, STRING(m_iszScenes[i]) );
  5039. m_hScenes[i] = NULL;
  5040. }
  5041. }
  5042. }
  5043. else
  5044. {
  5045. Warning( "%s(%s) could not find scene %d, named %s\n", GetDebugName(), GetClassname(), i, STRING(m_iszScenes[i]) );
  5046. }
  5047. }
  5048. }
  5049. }
  5050. }
  5051. //-----------------------------------------------------------------------------
  5052. // Purpose: A scene or manager in our list has started playing.
  5053. // Remove all scenes earlier in the list.
  5054. //-----------------------------------------------------------------------------
  5055. void CSceneListManager::SceneStarted( CBaseEntity *pSceneOrManager )
  5056. {
  5057. // Move backwards and call remove on all scenes / managers earlier in the list to the fired one
  5058. bool bFoundStart = false;
  5059. for ( int i = SCENE_LIST_MANAGER_MAX_SCENES-1; i >= 0; i-- )
  5060. {
  5061. if ( !m_hScenes[i] )
  5062. continue;
  5063. if ( bFoundStart )
  5064. {
  5065. RemoveScene( i );
  5066. }
  5067. else if ( m_hScenes[i] == pSceneOrManager )
  5068. {
  5069. bFoundStart = true;
  5070. }
  5071. }
  5072. // Tell any managers we're within that we've started a scene
  5073. if ( bFoundStart )
  5074. {
  5075. int c = m_hListManagers.Count();
  5076. for ( int i = 0; i < c; i++ )
  5077. {
  5078. if ( m_hListManagers[i] )
  5079. {
  5080. m_hListManagers[i]->SceneStarted( this );
  5081. }
  5082. }
  5083. }
  5084. }
  5085. //-----------------------------------------------------------------------------
  5086. // Purpose:
  5087. //-----------------------------------------------------------------------------
  5088. void CSceneListManager::AddListManager( CSceneListManager *pManager )
  5089. {
  5090. CHandle< CSceneListManager > h;
  5091. h = pManager;
  5092. // Only add it once
  5093. if ( m_hListManagers.Find( h ) == m_hListManagers.InvalidIndex() )
  5094. {
  5095. m_hListManagers.AddToTail( h );
  5096. }
  5097. }
  5098. //-----------------------------------------------------------------------------
  5099. // Purpose: Shut down all scenes, and then remove this entity
  5100. //-----------------------------------------------------------------------------
  5101. void CSceneListManager::InputShutdown( inputdata_t &inputdata )
  5102. {
  5103. ShutdownList();
  5104. }
  5105. //-----------------------------------------------------------------------------
  5106. // Purpose:
  5107. //-----------------------------------------------------------------------------
  5108. void CSceneListManager::ShutdownList( void )
  5109. {
  5110. for ( int i = 0; i < SCENE_LIST_MANAGER_MAX_SCENES; i++ )
  5111. {
  5112. if ( m_hScenes[i] )
  5113. {
  5114. RemoveScene(i);
  5115. }
  5116. }
  5117. UTIL_Remove( this );
  5118. }
  5119. //-----------------------------------------------------------------------------
  5120. // Purpose:
  5121. //-----------------------------------------------------------------------------
  5122. void CSceneListManager::RemoveScene( int iIndex )
  5123. {
  5124. CSceneEntity *pScene = dynamic_cast<CSceneEntity*>(m_hScenes[iIndex].Get());
  5125. if ( pScene )
  5126. {
  5127. // Remove the scene
  5128. UTIL_Remove( pScene );
  5129. return;
  5130. }
  5131. // Tell the list manager to shut down all scenes
  5132. CSceneListManager *pList = dynamic_cast<CSceneListManager*>(m_hScenes[iIndex].Get());
  5133. if ( pList )
  5134. {
  5135. pList->ShutdownList();
  5136. }
  5137. }
  5138. void ReloadSceneFromDisk( CBaseEntity *ent )
  5139. {
  5140. CSceneEntity *scene = dynamic_cast< CSceneEntity * >( ent );
  5141. if ( !scene )
  5142. return;
  5143. Assert( 0 );
  5144. }
  5145. // Purpose:
  5146. // Input : *ent -
  5147. // Output : char const
  5148. //-----------------------------------------------------------------------------
  5149. char const *GetSceneFilename( CBaseEntity *ent )
  5150. {
  5151. CSceneEntity *scene = dynamic_cast< CSceneEntity * >( ent );
  5152. if ( !scene )
  5153. return "";
  5154. return STRING( scene->m_iszSceneFile );
  5155. }
  5156. //-----------------------------------------------------------------------------
  5157. // Purpose: Return a list of the last 5 lines of speech from NPCs for bug reports
  5158. // Input :
  5159. // Output : speech - last 5 sound files played as speech
  5160. // returns the number of sounds in the returned list
  5161. //-----------------------------------------------------------------------------
  5162. int GetRecentNPCSpeech( recentNPCSpeech_t speech[ SPEECH_LIST_MAX_SOUNDS ] )
  5163. {
  5164. int i;
  5165. int num;
  5166. int index;
  5167. // clear out the output list
  5168. for( i = 0; i < SPEECH_LIST_MAX_SOUNDS; i++ )
  5169. {
  5170. speech[ i ].time = 0.0f;
  5171. speech[ i ].name[ 0 ] = 0;
  5172. speech[ i ].sceneName[ 0 ] = 0;
  5173. }
  5174. // copy the sound names into the list in order they were played
  5175. num = 0;
  5176. index = speechListIndex;
  5177. for( i = 0; i < SPEECH_LIST_MAX_SOUNDS; i++ )
  5178. {
  5179. if ( speechListSounds[ index ].name[ 0 ] )
  5180. {
  5181. // only copy names that are not zero length
  5182. speech[ num ] = speechListSounds[ index ];
  5183. num++;
  5184. }
  5185. index++;
  5186. if ( index >= SPEECH_LIST_MAX_SOUNDS )
  5187. {
  5188. index = 0;
  5189. }
  5190. }
  5191. return num;
  5192. }
  5193. //-----------------------------------------------------------------------------
  5194. // Purpose: Displays a list of the last 5 lines of speech from NPCs
  5195. // Input :
  5196. // Output :
  5197. //-----------------------------------------------------------------------------
  5198. static void ListRecentNPCSpeech( void )
  5199. {
  5200. if ( !UTIL_IsCommandIssuedByServerAdmin() )
  5201. return;
  5202. recentNPCSpeech_t speech[ SPEECH_LIST_MAX_SOUNDS ];
  5203. int num;
  5204. int i;
  5205. // get any sounds that were spoken by NPCs recently
  5206. num = GetRecentNPCSpeech( speech );
  5207. Msg( "Recent NPC speech:\n" );
  5208. for( i = 0; i < num; i++ )
  5209. {
  5210. Msg( " time: %6.3f sound name: %s scene: %s\n", speech[ i ].time, speech[ i ].name, speech[ i ].sceneName );
  5211. }
  5212. Msg( "Current time: %6.3f\n", gpGlobals->curtime );
  5213. }
  5214. static ConCommand ListRecentNPCSpeechCmd( "listRecentNPCSpeech", ListRecentNPCSpeech, "Displays a list of the last 5 lines of speech from NPCs.", FCVAR_DONTRECORD|FCVAR_GAMEDLL );
  5215. CON_COMMAND( scene_flush, "Flush all .vcds from the cache and reload from disk." )
  5216. {
  5217. if ( !UTIL_IsCommandIssuedByServerAdmin() )
  5218. return;
  5219. Msg( "Reloading\n" );
  5220. scenefilecache->Reload();
  5221. Msg( " done\n" );
  5222. }