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

1166 lines
30 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #include "cbase.h"
  9. #include "animation.h"
  10. #include "studio.h"
  11. #include "bone_setup.h"
  12. #include "ai_basenpc.h"
  13. #include "npcevent.h"
  14. #include "saverestore_utlvector.h"
  15. #include "dt_utlvector_send.h"
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include "tier0/memdbgon.h"
  18. extern ConVar ai_sequence_debug;
  19. BEGIN_SIMPLE_DATADESC( CAnimationLayer )
  20. // DEFINE_FIELD( m_pOwnerEntity, CBaseAnimatingOverlay ),
  21. DEFINE_FIELD( m_fFlags, FIELD_INTEGER ),
  22. DEFINE_FIELD( m_bSequenceFinished, FIELD_BOOLEAN ),
  23. DEFINE_FIELD( m_bLooping, FIELD_BOOLEAN ),
  24. DEFINE_FIELD( m_nSequence, FIELD_INTEGER ),
  25. DEFINE_FIELD( m_flCycle, FIELD_FLOAT ),
  26. DEFINE_FIELD( m_flPrevCycle, FIELD_FLOAT ),
  27. DEFINE_FIELD( m_flPlaybackRate, FIELD_FLOAT),
  28. DEFINE_FIELD( m_flWeight, FIELD_FLOAT),
  29. DEFINE_FIELD( m_flBlendIn, FIELD_FLOAT ),
  30. DEFINE_FIELD( m_flBlendOut, FIELD_FLOAT ),
  31. DEFINE_FIELD( m_flKillRate, FIELD_FLOAT ),
  32. DEFINE_FIELD( m_flKillDelay, FIELD_FLOAT ),
  33. DEFINE_CUSTOM_FIELD( m_nActivity, ActivityDataOps() ),
  34. DEFINE_FIELD( m_nPriority, FIELD_INTEGER ),
  35. DEFINE_FIELD( m_nOrder, FIELD_INTEGER ),
  36. DEFINE_FIELD( m_flLastEventCheck, FIELD_FLOAT ),
  37. DEFINE_FIELD( m_flLastAccess, FIELD_TIME ),
  38. DEFINE_FIELD( m_flLayerAnimtime, FIELD_FLOAT ),
  39. DEFINE_FIELD( m_flLayerFadeOuttime, FIELD_FLOAT ),
  40. END_DATADESC()
  41. BEGIN_DATADESC( CBaseAnimatingOverlay )
  42. DEFINE_UTLVECTOR( m_AnimOverlay, FIELD_EMBEDDED ),
  43. // DEFINE_FIELD( m_nActiveLayers, FIELD_INTEGER ),
  44. // DEFINE_FIELD( m_nActiveBaseLayers, FIELD_INTEGER ),
  45. END_DATADESC()
  46. #define ORDER_BITS 4
  47. #define WEIGHT_BITS 8
  48. BEGIN_SEND_TABLE_NOBASE(CAnimationLayer, DT_Animationlayer)
  49. SendPropInt (SENDINFO(m_nSequence), ANIMATION_SEQUENCE_BITS,SPROP_UNSIGNED),
  50. SendPropFloat (SENDINFO(m_flCycle), ANIMATION_CYCLE_BITS, SPROP_ROUNDDOWN, 0.0f, 1.0f),
  51. SendPropFloat (SENDINFO(m_flPrevCycle), ANIMATION_CYCLE_BITS, SPROP_ROUNDDOWN, 0.0f, 1.0f),
  52. SendPropFloat (SENDINFO(m_flWeight), WEIGHT_BITS, 0, 0.0f, 1.0f),
  53. SendPropInt (SENDINFO(m_nOrder), ORDER_BITS, SPROP_UNSIGNED),
  54. END_SEND_TABLE()
  55. BEGIN_SEND_TABLE_NOBASE( CBaseAnimatingOverlay, DT_OverlayVars )
  56. SendPropUtlVector(
  57. SENDINFO_UTLVECTOR( m_AnimOverlay ),
  58. CBaseAnimatingOverlay::MAX_OVERLAYS, // max elements
  59. SendPropDataTable( NULL, 0, &REFERENCE_SEND_TABLE( DT_Animationlayer ) ) )
  60. END_SEND_TABLE()
  61. IMPLEMENT_SERVERCLASS_ST( CBaseAnimatingOverlay, DT_BaseAnimatingOverlay )
  62. // These are in their own separate data table so CCSPlayer can exclude all of these.
  63. SendPropDataTable( "overlay_vars", 0, &REFERENCE_SEND_TABLE( DT_OverlayVars ) )
  64. END_SEND_TABLE()
  65. CAnimationLayer::CAnimationLayer( )
  66. {
  67. Init( NULL );
  68. }
  69. void CAnimationLayer::Init( CBaseAnimatingOverlay *pOverlay )
  70. {
  71. m_pOwnerEntity = pOverlay;
  72. m_fFlags = 0;
  73. m_flWeight = 0;
  74. m_flCycle = 0;
  75. m_flPrevCycle = 0;
  76. m_bSequenceFinished = false;
  77. m_nActivity = ACT_INVALID;
  78. m_nSequence = 0;
  79. m_nPriority = 0;
  80. m_nOrder.Set( CBaseAnimatingOverlay::MAX_OVERLAYS );
  81. m_flBlendIn = 0.0;
  82. m_flBlendOut = 0.0;
  83. m_flKillRate = 100.0;
  84. m_flKillDelay = 0.0;
  85. m_flPlaybackRate = 1.0;
  86. m_flLastEventCheck = 0.0;
  87. m_flLastAccess = gpGlobals->curtime;
  88. m_flLayerAnimtime = 0;
  89. m_flLayerFadeOuttime = 0;
  90. }
  91. //------------------------------------------------------------------------------
  92. // Purpose :
  93. // Input :
  94. // Output :
  95. //------------------------------------------------------------------------------
  96. void CAnimationLayer::StudioFrameAdvance( float flInterval, CBaseAnimating *pOwner )
  97. {
  98. float flCycleRate = pOwner->GetSequenceCycleRate( m_nSequence );
  99. m_flPrevCycle = m_flCycle;
  100. m_flCycle += flInterval * flCycleRate * m_flPlaybackRate;
  101. if (m_flCycle < 0.0)
  102. {
  103. if (m_bLooping)
  104. {
  105. m_flCycle -= (int)(m_flCycle);
  106. }
  107. else
  108. {
  109. m_flCycle = 0;
  110. }
  111. }
  112. else if (m_flCycle >= 1.0)
  113. {
  114. m_bSequenceFinished = true;
  115. if (m_bLooping)
  116. {
  117. m_flCycle -= (int)(m_flCycle);
  118. }
  119. else
  120. {
  121. m_flCycle = 1.0;
  122. }
  123. }
  124. if (IsAutoramp())
  125. {
  126. m_flWeight = 1;
  127. // blend in?
  128. if ( m_flBlendIn != 0.0f )
  129. {
  130. if (m_flCycle < m_flBlendIn)
  131. {
  132. m_flWeight = m_flCycle / m_flBlendIn;
  133. }
  134. }
  135. // blend out?
  136. if ( m_flBlendOut != 0.0f )
  137. {
  138. if (m_flCycle > 1.0 - m_flBlendOut)
  139. {
  140. m_flWeight = (1.0 - m_flCycle) / m_flBlendOut;
  141. }
  142. }
  143. m_flWeight = 3.0 * m_flWeight * m_flWeight - 2.0 * m_flWeight * m_flWeight * m_flWeight;
  144. if (m_nSequence == 0)
  145. m_flWeight = 0;
  146. }
  147. }
  148. //------------------------------------------------------------------------------
  149. bool CAnimationLayer::IsAbandoned( void )
  150. {
  151. if (IsActive() && !IsAutokill() && !IsKillMe() && m_flLastAccess > 0.0 && (gpGlobals->curtime - m_flLastAccess > 0.2))
  152. return true;
  153. else
  154. return false;
  155. }
  156. void CAnimationLayer::MarkActive( void )
  157. {
  158. m_flLastAccess = gpGlobals->curtime;
  159. }
  160. //------------------------------------------------------------------------------
  161. void CBaseAnimatingOverlay::VerifyOrder( void )
  162. {
  163. #ifdef _DEBUG
  164. int i, j;
  165. // test sorting of the layers
  166. int layer[MAX_OVERLAYS];
  167. int maxOrder = -1;
  168. for (i = 0; i < MAX_OVERLAYS; i++)
  169. {
  170. layer[i] = MAX_OVERLAYS;
  171. }
  172. for (i = 0; i < m_AnimOverlay.Count(); i++)
  173. {
  174. if (m_AnimOverlay[ i ].m_nOrder < MAX_OVERLAYS)
  175. {
  176. j = m_AnimOverlay[ i ].m_nOrder;
  177. Assert( layer[j] == MAX_OVERLAYS );
  178. layer[j] = i;
  179. if (j > maxOrder)
  180. maxOrder = j;
  181. }
  182. }
  183. // make sure they're sequential
  184. // Aim layers are allowed to have gaps, and we are moving aim blending to server
  185. // for ( i = 0; i <= maxOrder; i++ )
  186. // {
  187. // Assert( layer[i] != MAX_OVERLAYS);
  188. // }
  189. /*
  190. for ( i = 0; i < MAX_OVERLAYS; i++ )
  191. {
  192. int j = layer[i];
  193. if (j != MAX_OVERLAYS)
  194. {
  195. char tempstr[512];
  196. Q_snprintf( tempstr, sizeof( tempstr ),"%d : %d :%.2f :%d:%d:%.1f",
  197. j,
  198. m_AnimOverlay[ j ].m_nSequence,
  199. m_AnimOverlay[ j ].m_flWeight,
  200. m_AnimOverlay[ j ].IsActive(),
  201. m_AnimOverlay[ j ].IsKillMe(),
  202. m_AnimOverlay[ j ].m_flKillDelay
  203. );
  204. EntityText( i, tempstr, 0.1 );
  205. }
  206. }
  207. */
  208. #endif
  209. }
  210. //------------------------------------------------------------------------------
  211. // Purpose : advance the animation frame up to the current time
  212. // if an flInterval is passed in, only advance animation that number of seconds
  213. // Input :
  214. // Output :
  215. //------------------------------------------------------------------------------
  216. void CBaseAnimatingOverlay::StudioFrameAdvance ()
  217. {
  218. float flAdvance = GetAnimTimeInterval();
  219. VerifyOrder();
  220. BaseClass::StudioFrameAdvance();
  221. for ( int i = 0; i < m_AnimOverlay.Count(); i++ )
  222. {
  223. CAnimationLayer *pLayer = &m_AnimOverlay[i];
  224. if (pLayer->IsActive())
  225. {
  226. // Assert( !m_AnimOverlay[ i ].IsAbandoned() );
  227. if (pLayer->IsKillMe())
  228. {
  229. if (pLayer->m_flKillDelay > 0)
  230. {
  231. pLayer->m_flKillDelay -= flAdvance;
  232. pLayer->m_flKillDelay = clamp( pLayer->m_flKillDelay, 0.0f, 1.0f );
  233. }
  234. else if (pLayer->m_flWeight != 0.0f)
  235. {
  236. // give it at least one frame advance cycle to propagate 0.0 to client
  237. pLayer->m_flWeight -= pLayer->m_flKillRate * flAdvance;
  238. pLayer->m_flWeight = clamp( (float) pLayer->m_flWeight, 0.0f, 1.0f );
  239. }
  240. else
  241. {
  242. // shift the other layers down in order
  243. if (ai_sequence_debug.GetBool() == true && m_debugOverlays & OVERLAY_NPC_SELECTED_BIT)
  244. {
  245. Msg("removing %d (%d): %s : %5.3f (%.3f)\n", i, pLayer->m_nOrder.Get(), GetSequenceName( pLayer->m_nSequence ), pLayer->m_flCycle.Get(), pLayer->m_flWeight.Get() );
  246. }
  247. FastRemoveLayer( i );
  248. // needs at least one thing cycle dead to trigger sequence change
  249. pLayer->Dying();
  250. continue;
  251. }
  252. }
  253. pLayer->StudioFrameAdvance( flAdvance, this );
  254. if ( pLayer->m_bSequenceFinished && (pLayer->IsAutokill()) )
  255. {
  256. pLayer->m_flWeight = 0.0f;
  257. pLayer->KillMe();
  258. }
  259. }
  260. else if (pLayer->IsDying())
  261. {
  262. pLayer->Dead();
  263. }
  264. else if (pLayer->m_flWeight > 0.0)
  265. {
  266. // Now that the server blends, it is turning off layers all the time. Having a weight left over
  267. // when you're no longer marked as active is now harmless and commonplace. Just clean up.
  268. pLayer->Init( this );
  269. pLayer->Dying();
  270. }
  271. }
  272. if (ai_sequence_debug.GetBool() == true && m_debugOverlays & OVERLAY_NPC_SELECTED_BIT)
  273. {
  274. for ( int i = 0; i < m_AnimOverlay.Count(); i++ )
  275. {
  276. if (m_AnimOverlay[ i ].IsActive())
  277. {
  278. /*
  279. if (m_AnimOverlay[ i ].IsAbandoned())
  280. {
  281. Msg(" %d abandoned %.2f (%.2f)\n", i, gpGlobals->curtime, m_AnimOverlay[ i ].m_flLastAccess );
  282. }
  283. */
  284. Msg(" %d (%d): %s : %5.3f (%.3f)\n", i, m_AnimOverlay[ i ].m_nOrder.Get(), GetSequenceName( m_AnimOverlay[ i ].m_nSequence ), m_AnimOverlay[ i ].m_flCycle.Get(), m_AnimOverlay[ i ].m_flWeight.Get() );
  285. }
  286. }
  287. }
  288. VerifyOrder();
  289. }
  290. //=========================================================
  291. // DispatchAnimEvents
  292. //=========================================================
  293. void CBaseAnimatingOverlay::DispatchAnimEvents ( CBaseAnimating *eventHandler )
  294. {
  295. BaseClass::DispatchAnimEvents( eventHandler );
  296. for ( int i = 0; i < m_AnimOverlay.Count(); i++ )
  297. {
  298. if (m_AnimOverlay[ i ].IsActive())
  299. {
  300. m_AnimOverlay[ i ].DispatchAnimEvents( eventHandler, this );
  301. }
  302. }
  303. }
  304. void CAnimationLayer::DispatchAnimEvents( CBaseAnimating *eventHandler, CBaseAnimating *pOwner )
  305. {
  306. animevent_t event;
  307. CStudioHdr *pstudiohdr = pOwner->GetModelPtr( );
  308. if ( !pstudiohdr )
  309. {
  310. Assert(!"CBaseAnimating::DispatchAnimEvents: model missing");
  311. return;
  312. }
  313. if ( !pstudiohdr->SequencesAvailable() )
  314. {
  315. return;
  316. }
  317. if ( m_nSequence >= pstudiohdr->GetNumSeq() )
  318. return;
  319. // don't fire if here are no events
  320. if ( pstudiohdr->pSeqdesc( m_nSequence ).numevents == 0 )
  321. {
  322. return;
  323. }
  324. // look from when it last checked to some short time in the future
  325. float flCycleRate = pOwner->GetSequenceCycleRate( m_nSequence ) * m_flPlaybackRate;
  326. float flStart = m_flLastEventCheck;
  327. float flEnd = m_flCycle;
  328. if (!m_bLooping)
  329. {
  330. // fire off events early
  331. float flLastVisibleCycle = 1.0f - (pstudiohdr->pSeqdesc( m_nSequence ).fadeouttime) * flCycleRate;
  332. if (flEnd >= flLastVisibleCycle || flEnd < 0.0)
  333. {
  334. m_bSequenceFinished = true;
  335. flEnd = 1.0f;
  336. }
  337. }
  338. m_flLastEventCheck = flEnd;
  339. /*
  340. if (pOwner->m_debugOverlays & OVERLAY_NPC_SELECTED_BIT)
  341. {
  342. Msg( "%s:%s : checking %.2f %.2f (%d)\n", STRING(pOwner->GetModelName()), pstudiohdr->pSeqdesc( m_nSequence ).pszLabel(), flStart, flEnd, m_bSequenceFinished );
  343. }
  344. */
  345. // FIXME: does not handle negative framerates!
  346. int index = 0;
  347. while ( (index = GetAnimationEvent( pstudiohdr, m_nSequence, &event, flStart, flEnd, index ) ) != 0 )
  348. {
  349. event.pSource = pOwner;
  350. // calc when this event should happen
  351. if (flCycleRate > 0.0)
  352. {
  353. float flCycle = event.cycle;
  354. if (flCycle > m_flCycle)
  355. {
  356. flCycle = flCycle - 1.0;
  357. }
  358. event.eventtime = pOwner->m_flAnimTime + (flCycle - m_flCycle) / flCycleRate + pOwner->GetAnimTimeInterval();
  359. }
  360. // Msg( "dispatch %d (%d : %.2f)\n", index - 1, event.event, event.eventtime );
  361. eventHandler->HandleAnimEvent( &event );
  362. }
  363. }
  364. void CBaseAnimatingOverlay::GetSkeleton( CStudioHdr *pStudioHdr, Vector pos[], Quaternion q[], int boneMask )
  365. {
  366. if(!pStudioHdr)
  367. {
  368. Assert(!"CBaseAnimating::GetSkeleton() without a model");
  369. return;
  370. }
  371. if (!pStudioHdr->SequencesAvailable())
  372. {
  373. return;
  374. }
  375. IBoneSetup boneSetup( pStudioHdr, boneMask, GetPoseParameterArray() );
  376. boneSetup.InitPose( pos, q );
  377. boneSetup.AccumulatePose( pos, q, GetSequence(), GetCycle(), 1.0, gpGlobals->curtime, m_pIk );
  378. // sort the layers
  379. int layer[MAX_OVERLAYS] = {};
  380. int i;
  381. for (i = 0; i < m_AnimOverlay.Count(); i++)
  382. {
  383. layer[i] = MAX_OVERLAYS;
  384. }
  385. for (i = 0; i < m_AnimOverlay.Count(); i++)
  386. {
  387. CAnimationLayer &pLayer = m_AnimOverlay[i];
  388. if( (pLayer.m_flWeight > 0) && pLayer.IsActive() && pLayer.m_nOrder >= 0 && pLayer.m_nOrder < m_AnimOverlay.Count())
  389. {
  390. layer[pLayer.m_nOrder] = i;
  391. }
  392. }
  393. for (i = 0; i < m_AnimOverlay.Count(); i++)
  394. {
  395. if (layer[i] >= 0 && layer[i] < m_AnimOverlay.Count())
  396. {
  397. CAnimationLayer &pLayer = m_AnimOverlay[layer[i]];
  398. // UNDONE: Is it correct to use overlay weight for IK too?
  399. boneSetup.AccumulatePose( pos, q, pLayer.m_nSequence, pLayer.m_flCycle, pLayer.m_flWeight, gpGlobals->curtime, m_pIk );
  400. }
  401. }
  402. if ( m_pIk )
  403. {
  404. CIKContext auto_ik;
  405. auto_ik.Init( pStudioHdr, GetAbsAngles(), GetAbsOrigin(), gpGlobals->curtime, 0, boneMask );
  406. boneSetup.CalcAutoplaySequences( pos, q, gpGlobals->curtime, &auto_ik );
  407. }
  408. else
  409. {
  410. boneSetup.CalcAutoplaySequences( pos, q, gpGlobals->curtime, NULL );
  411. }
  412. boneSetup.CalcBoneAdj( pos, q, GetEncodedControllerArray() );
  413. }
  414. //-----------------------------------------------------------------------------
  415. // Purpose: zero's out all non-restore safe fields
  416. // Output :
  417. //-----------------------------------------------------------------------------
  418. void CBaseAnimatingOverlay::OnRestore( )
  419. {
  420. int i;
  421. // force order of unused layers to current MAX_OVERLAYS
  422. // and Tracker 48843 (Alyx skating after restore) restore the owner entity ptr (otherwise the network layer won't get NetworkStateChanged signals until the layer is re-Init()'ed
  423. for (i = 0; i < m_AnimOverlay.Count(); i++)
  424. {
  425. m_AnimOverlay[i].m_pOwnerEntity = this;
  426. if ( !m_AnimOverlay[i].IsActive())
  427. {
  428. m_AnimOverlay[i].m_nOrder.Set( MAX_OVERLAYS );
  429. }
  430. }
  431. // get rid of all layers that shouldn't be restored
  432. for (i = 0; i < m_AnimOverlay.Count(); i++)
  433. {
  434. if ( ( m_AnimOverlay[i].IsActive() && (m_AnimOverlay[i].m_fFlags & ANIM_LAYER_DONTRESTORE) ) ||
  435. ( GetModelPtr() && !IsValidSequence(m_AnimOverlay[i].m_nSequence) ) )
  436. {
  437. FastRemoveLayer( i );
  438. }
  439. }
  440. BaseClass::OnRestore();
  441. }
  442. //-----------------------------------------------------------------------------
  443. // Purpose:
  444. // Output : int
  445. //-----------------------------------------------------------------------------
  446. int CBaseAnimatingOverlay::AddGestureSequence( int sequence, bool autokill /*= true*/ )
  447. {
  448. int i = AddLayeredSequence( sequence, 0 );
  449. // No room?
  450. if ( IsValidLayer( i ) )
  451. {
  452. SetLayerAutokill( i, autokill );
  453. }
  454. return i;
  455. }
  456. //-----------------------------------------------------------------------------
  457. // Purpose:
  458. // Output : int
  459. //-----------------------------------------------------------------------------
  460. int CBaseAnimatingOverlay::AddGestureSequence( int nSequence, float flDuration, bool autokill /*= true*/ )
  461. {
  462. int iLayer = AddGestureSequence( nSequence, autokill );
  463. Assert( iLayer != -1 );
  464. if (iLayer >= 0 && flDuration > 0)
  465. {
  466. m_AnimOverlay[iLayer].m_flPlaybackRate = SequenceDuration( nSequence ) / flDuration;
  467. }
  468. return iLayer;
  469. }
  470. //------------------------------------------------------------------------------
  471. // Purpose :
  472. // Input :
  473. // Output :
  474. //------------------------------------------------------------------------------
  475. int CBaseAnimatingOverlay::AddGesture( Activity activity, bool autokill /*= true*/ )
  476. {
  477. if ( IsPlayingGesture( activity ) )
  478. {
  479. return FindGestureLayer( activity );
  480. }
  481. int seq = SelectWeightedSequence( activity );
  482. if ( seq <= 0 )
  483. {
  484. const char *actname = CAI_BaseNPC::GetActivityName( activity );
  485. DevMsg( "CBaseAnimatingOverlay::AddGesture: model %s missing activity %s\n", STRING(GetModelName()), actname );
  486. return -1;
  487. }
  488. int i = AddGestureSequence( seq, autokill );
  489. Assert( i != -1 );
  490. if ( i != -1 )
  491. {
  492. m_AnimOverlay[ i ].m_nActivity = activity;
  493. }
  494. return i;
  495. }
  496. int CBaseAnimatingOverlay::AddGesture( Activity activity, float flDuration, bool autokill /*= true*/ )
  497. {
  498. int iLayer = AddGesture( activity, autokill );
  499. SetLayerDuration( iLayer, flDuration );
  500. return iLayer;
  501. }
  502. //-----------------------------------------------------------------------------
  503. // Purpose:
  504. // Output : int
  505. //-----------------------------------------------------------------------------
  506. void CBaseAnimatingOverlay::SetLayerDuration( int iLayer, float flDuration )
  507. {
  508. if (IsValidLayer( iLayer ) && flDuration > 0)
  509. {
  510. m_AnimOverlay[iLayer].m_flPlaybackRate = SequenceDuration( m_AnimOverlay[iLayer].m_nSequence ) / flDuration;
  511. }
  512. }
  513. //-----------------------------------------------------------------------------
  514. // Purpose:
  515. // Output : int
  516. //-----------------------------------------------------------------------------
  517. float CBaseAnimatingOverlay::GetLayerDuration( int iLayer )
  518. {
  519. if (IsValidLayer( iLayer ))
  520. {
  521. if (m_AnimOverlay[iLayer].m_flPlaybackRate != 0.0f)
  522. {
  523. return (1.0 - m_AnimOverlay[iLayer].m_flCycle) * SequenceDuration( m_AnimOverlay[iLayer].m_nSequence ) / m_AnimOverlay[iLayer].m_flPlaybackRate;
  524. }
  525. return SequenceDuration( m_AnimOverlay[iLayer].m_nSequence );
  526. }
  527. return 0.0;
  528. }
  529. //-----------------------------------------------------------------------------
  530. // Purpose:
  531. // Output : int
  532. //-----------------------------------------------------------------------------
  533. int CBaseAnimatingOverlay::AddLayeredSequence( int sequence, int iPriority )
  534. {
  535. int i = AllocateLayer( iPriority );
  536. // No room?
  537. if ( IsValidLayer( i ) )
  538. {
  539. m_AnimOverlay[i].m_flCycle = 0;
  540. m_AnimOverlay[i].m_flPrevCycle = 0;
  541. m_AnimOverlay[i].m_flPlaybackRate = 1.0;
  542. m_AnimOverlay[i].m_nActivity = ACT_INVALID;
  543. m_AnimOverlay[i].m_nSequence = sequence;
  544. m_AnimOverlay[i].m_flWeight = 1.0f;
  545. m_AnimOverlay[i].m_flBlendIn = 0.0f;
  546. m_AnimOverlay[i].m_flBlendOut = 0.0f;
  547. m_AnimOverlay[i].m_bSequenceFinished = false;
  548. m_AnimOverlay[i].m_flLastEventCheck = 0;
  549. m_AnimOverlay[i].m_bLooping = ((GetSequenceFlags( GetModelPtr(), sequence ) & STUDIO_LOOPING) != 0);
  550. if (ai_sequence_debug.GetBool() == true && m_debugOverlays & OVERLAY_NPC_SELECTED_BIT)
  551. {
  552. Msg("%5.3f : adding %d (%d): %s : %5.3f (%.3f)\n", gpGlobals->curtime, i, m_AnimOverlay[ i ].m_nOrder.Get(), GetSequenceName( m_AnimOverlay[ i ].m_nSequence ), m_AnimOverlay[ i ].m_flCycle.Get(), m_AnimOverlay[ i ].m_flWeight.Get() );
  553. }
  554. }
  555. return i;
  556. }
  557. //-----------------------------------------------------------------------------
  558. // Purpose:
  559. // Output : int
  560. //-----------------------------------------------------------------------------
  561. bool CBaseAnimatingOverlay::IsValidLayer( int iLayer )
  562. {
  563. return (iLayer >= 0 && iLayer < m_AnimOverlay.Count() && m_AnimOverlay[iLayer].IsActive());
  564. }
  565. //-----------------------------------------------------------------------------
  566. // Purpose:
  567. // Output : int
  568. //-----------------------------------------------------------------------------
  569. int CBaseAnimatingOverlay::AllocateLayer( int iPriority )
  570. {
  571. int i;
  572. // look for an open slot and for existing layers that are lower priority
  573. int iNewOrder = 0;
  574. int iOpenLayer = -1;
  575. int iNumOpen = 0;
  576. for (i = 0; i < m_AnimOverlay.Count(); i++)
  577. {
  578. if ( m_AnimOverlay[i].IsActive() )
  579. {
  580. if (m_AnimOverlay[i].m_nPriority <= iPriority)
  581. {
  582. iNewOrder = MAX( iNewOrder, m_AnimOverlay[i].m_nOrder + 1 );
  583. }
  584. }
  585. else if (m_AnimOverlay[ i ].IsDying())
  586. {
  587. // skip
  588. }
  589. else if (iOpenLayer == -1)
  590. {
  591. iOpenLayer = i;
  592. }
  593. else
  594. {
  595. iNumOpen++;
  596. }
  597. }
  598. if (iOpenLayer == -1)
  599. {
  600. if (m_AnimOverlay.Count() >= MAX_OVERLAYS)
  601. {
  602. return -1;
  603. }
  604. iOpenLayer = m_AnimOverlay.AddToTail();
  605. m_AnimOverlay[iOpenLayer].Init( this );
  606. }
  607. // make sure there's always an empty unused layer so that history slots will be available on the client when it is used
  608. if (iNumOpen == 0)
  609. {
  610. if (m_AnimOverlay.Count() < MAX_OVERLAYS)
  611. {
  612. i = m_AnimOverlay.AddToTail();
  613. m_AnimOverlay[i].Init( this );
  614. }
  615. }
  616. for (i = 0; i < m_AnimOverlay.Count(); i++)
  617. {
  618. if ( m_AnimOverlay[i].m_nOrder >= iNewOrder && m_AnimOverlay[i].m_nOrder < MAX_OVERLAYS)
  619. {
  620. m_AnimOverlay[i].m_nOrder++;
  621. }
  622. }
  623. m_AnimOverlay[iOpenLayer].m_fFlags = ANIM_LAYER_ACTIVE;
  624. m_AnimOverlay[iOpenLayer].m_nOrder = iNewOrder;
  625. m_AnimOverlay[iOpenLayer].m_nPriority = iPriority;
  626. m_AnimOverlay[iOpenLayer].MarkActive();
  627. VerifyOrder();
  628. return iOpenLayer;
  629. }
  630. //-----------------------------------------------------------------------------
  631. // Purpose:
  632. // Output : int
  633. //-----------------------------------------------------------------------------
  634. void CBaseAnimatingOverlay::SetLayerPriority( int iLayer, int iPriority )
  635. {
  636. if (!IsValidLayer( iLayer ))
  637. {
  638. return;
  639. }
  640. if (m_AnimOverlay[iLayer].m_nPriority == iPriority)
  641. {
  642. return;
  643. }
  644. // look for an open slot and for existing layers that are lower priority
  645. int i;
  646. for (i = 0; i < m_AnimOverlay.Count(); i++)
  647. {
  648. if ( m_AnimOverlay[i].IsActive() )
  649. {
  650. if (m_AnimOverlay[i].m_nOrder > m_AnimOverlay[iLayer].m_nOrder)
  651. {
  652. m_AnimOverlay[i].m_nOrder--;
  653. }
  654. }
  655. }
  656. int iNewOrder = 0;
  657. for (i = 0; i < m_AnimOverlay.Count(); i++)
  658. {
  659. if ( i != iLayer && m_AnimOverlay[i].IsActive() )
  660. {
  661. if (m_AnimOverlay[i].m_nPriority <= iPriority)
  662. {
  663. iNewOrder = MAX( iNewOrder, m_AnimOverlay[i].m_nOrder + 1 );
  664. }
  665. }
  666. }
  667. for (i = 0; i < m_AnimOverlay.Count(); i++)
  668. {
  669. if ( i != iLayer && m_AnimOverlay[i].IsActive() )
  670. {
  671. if ( m_AnimOverlay[i].m_nOrder >= iNewOrder)
  672. {
  673. m_AnimOverlay[i].m_nOrder++;
  674. }
  675. }
  676. }
  677. m_AnimOverlay[iLayer].m_nOrder = iNewOrder;
  678. m_AnimOverlay[iLayer].m_nPriority = iPriority;
  679. m_AnimOverlay[iLayer].MarkActive( );
  680. VerifyOrder();
  681. return;
  682. }
  683. //-----------------------------------------------------------------------------
  684. // Purpose:
  685. // Input : activity -
  686. //-----------------------------------------------------------------------------
  687. int CBaseAnimatingOverlay::FindGestureLayer( Activity activity )
  688. {
  689. for (int i = 0; i < m_AnimOverlay.Count(); i++)
  690. {
  691. if ( !(m_AnimOverlay[i].IsActive()) )
  692. continue;
  693. if ( m_AnimOverlay[i].IsKillMe() )
  694. continue;
  695. if ( m_AnimOverlay[i].m_nActivity == ACT_INVALID )
  696. continue;
  697. if ( m_AnimOverlay[i].m_nActivity == activity )
  698. return i;
  699. }
  700. return -1;
  701. }
  702. //-----------------------------------------------------------------------------
  703. // Purpose:
  704. // Input : activity -
  705. // Output : Returns true on success, false on failure.
  706. //-----------------------------------------------------------------------------
  707. bool CBaseAnimatingOverlay::IsPlayingGesture( Activity activity )
  708. {
  709. return FindGestureLayer( activity ) != -1 ? true : false;
  710. }
  711. //-----------------------------------------------------------------------------
  712. // Purpose:
  713. // Input : activity -
  714. //-----------------------------------------------------------------------------
  715. void CBaseAnimatingOverlay::RestartGesture( Activity activity, bool addifmissing /*=true*/, bool autokill /*=true*/ )
  716. {
  717. int idx = FindGestureLayer( activity );
  718. if ( idx == -1 )
  719. {
  720. if ( addifmissing )
  721. {
  722. AddGesture( activity, autokill );
  723. }
  724. return;
  725. }
  726. m_AnimOverlay[ idx ].m_flCycle = 0.0f;
  727. m_AnimOverlay[ idx ].m_flPrevCycle = 0.0f;
  728. m_AnimOverlay[ idx ].m_flLastEventCheck = 0.0f;
  729. }
  730. //-----------------------------------------------------------------------------
  731. // Purpose:
  732. // Input : activity -
  733. //-----------------------------------------------------------------------------
  734. void CBaseAnimatingOverlay::RemoveGesture( Activity activity )
  735. {
  736. int iLayer = FindGestureLayer( activity );
  737. if ( iLayer == -1 )
  738. return;
  739. RemoveLayer( iLayer );
  740. }
  741. //-----------------------------------------------------------------------------
  742. // Purpose:
  743. //-----------------------------------------------------------------------------
  744. void CBaseAnimatingOverlay::RemoveAllGestures( void )
  745. {
  746. for (int i = 0; i < m_AnimOverlay.Count(); i++)
  747. {
  748. RemoveLayer( i );
  749. }
  750. }
  751. //-----------------------------------------------------------------------------
  752. // Purpose:
  753. //-----------------------------------------------------------------------------
  754. void CBaseAnimatingOverlay::SetLayerCycle( int iLayer, float flCycle )
  755. {
  756. if (!IsValidLayer( iLayer ))
  757. return;
  758. if (!m_AnimOverlay[iLayer].m_bLooping)
  759. {
  760. flCycle = clamp( flCycle, 0.0f, 1.0f );
  761. }
  762. m_AnimOverlay[iLayer].m_flCycle = flCycle;
  763. m_AnimOverlay[iLayer].MarkActive( );
  764. }
  765. //-----------------------------------------------------------------------------
  766. // Purpose:
  767. //-----------------------------------------------------------------------------
  768. void CBaseAnimatingOverlay::SetLayerCycle( int iLayer, float flCycle, float flPrevCycle )
  769. {
  770. if (!IsValidLayer( iLayer ))
  771. return;
  772. if (!m_AnimOverlay[iLayer].m_bLooping)
  773. {
  774. flCycle = clamp( flCycle, 0.0f, 1.0f );
  775. flPrevCycle = clamp( flPrevCycle, 0.0f, 1.0f );
  776. }
  777. m_AnimOverlay[iLayer].m_flCycle = flCycle;
  778. m_AnimOverlay[iLayer].m_flPrevCycle = flPrevCycle;
  779. m_AnimOverlay[iLayer].m_flLastEventCheck = flPrevCycle;
  780. m_AnimOverlay[iLayer].MarkActive( );
  781. }
  782. //-----------------------------------------------------------------------------
  783. // Purpose:
  784. //-----------------------------------------------------------------------------
  785. void CBaseAnimatingOverlay::SetLayerCycle( int iLayer, float flCycle, float flPrevCycle, float flLastEventCheck )
  786. {
  787. if (!IsValidLayer( iLayer ))
  788. return;
  789. if (!m_AnimOverlay[iLayer].m_bLooping)
  790. {
  791. flCycle = clamp( flCycle, 0.0f, 1.0f );
  792. flPrevCycle = clamp( flPrevCycle, 0.0f, 1.0f );
  793. }
  794. m_AnimOverlay[iLayer].m_flCycle = flCycle;
  795. m_AnimOverlay[iLayer].m_flPrevCycle = flPrevCycle;
  796. m_AnimOverlay[iLayer].m_flLastEventCheck = flLastEventCheck;
  797. m_AnimOverlay[iLayer].MarkActive( );
  798. }
  799. //-----------------------------------------------------------------------------
  800. // Purpose:
  801. //-----------------------------------------------------------------------------
  802. float CBaseAnimatingOverlay::GetLayerCycle( int iLayer )
  803. {
  804. if (!IsValidLayer( iLayer ))
  805. return 0.0;
  806. return m_AnimOverlay[iLayer].m_flCycle;
  807. }
  808. //-----------------------------------------------------------------------------
  809. // Purpose:
  810. //-----------------------------------------------------------------------------
  811. void CBaseAnimatingOverlay::SetLayerPlaybackRate( int iLayer, float flPlaybackRate )
  812. {
  813. if (!IsValidLayer( iLayer ))
  814. return;
  815. Assert( flPlaybackRate > -1.0 && flPlaybackRate < 40.0);
  816. m_AnimOverlay[iLayer].m_flPlaybackRate = flPlaybackRate;
  817. }
  818. //-----------------------------------------------------------------------------
  819. // Purpose:
  820. //-----------------------------------------------------------------------------
  821. void CBaseAnimatingOverlay::SetLayerWeight( int iLayer, float flWeight )
  822. {
  823. if (!IsValidLayer( iLayer ))
  824. return;
  825. flWeight = clamp( flWeight, 0.0f, 1.0f );
  826. m_AnimOverlay[iLayer].m_flWeight = flWeight;
  827. m_AnimOverlay[iLayer].MarkActive( );
  828. }
  829. //-----------------------------------------------------------------------------
  830. // Purpose:
  831. //-----------------------------------------------------------------------------
  832. float CBaseAnimatingOverlay::GetLayerWeight( int iLayer )
  833. {
  834. if (!IsValidLayer( iLayer ))
  835. return 0.0;
  836. return m_AnimOverlay[iLayer].m_flWeight;
  837. }
  838. //-----------------------------------------------------------------------------
  839. // Purpose:
  840. //-----------------------------------------------------------------------------
  841. void CBaseAnimatingOverlay::SetLayerBlendIn( int iLayer, float flBlendIn )
  842. {
  843. if (!IsValidLayer( iLayer ))
  844. return;
  845. m_AnimOverlay[iLayer].m_flBlendIn = flBlendIn;
  846. }
  847. //-----------------------------------------------------------------------------
  848. // Purpose:
  849. //-----------------------------------------------------------------------------
  850. void CBaseAnimatingOverlay::SetLayerBlendOut( int iLayer, float flBlendOut )
  851. {
  852. if (!IsValidLayer( iLayer ))
  853. return;
  854. m_AnimOverlay[iLayer].m_flBlendOut = flBlendOut;
  855. }
  856. //-----------------------------------------------------------------------------
  857. // Purpose:
  858. //-----------------------------------------------------------------------------
  859. void CBaseAnimatingOverlay::SetLayerAutokill( int iLayer, bool bAutokill )
  860. {
  861. if (!IsValidLayer( iLayer ))
  862. return;
  863. if (bAutokill)
  864. {
  865. m_AnimOverlay[iLayer].m_fFlags |= ANIM_LAYER_AUTOKILL;
  866. }
  867. else
  868. {
  869. m_AnimOverlay[iLayer].m_fFlags &= ~ANIM_LAYER_AUTOKILL;
  870. }
  871. }
  872. //-----------------------------------------------------------------------------
  873. // Purpose:
  874. //-----------------------------------------------------------------------------
  875. void CBaseAnimatingOverlay::SetLayerLooping( int iLayer, bool bLooping )
  876. {
  877. if (!IsValidLayer( iLayer ))
  878. return;
  879. m_AnimOverlay[iLayer].m_bLooping = bLooping;
  880. }
  881. //-----------------------------------------------------------------------------
  882. // Purpose:
  883. //-----------------------------------------------------------------------------
  884. void CBaseAnimatingOverlay::SetLayerNoRestore( int iLayer, bool bNoRestore )
  885. {
  886. if (!IsValidLayer( iLayer ))
  887. return;
  888. if (bNoRestore)
  889. {
  890. m_AnimOverlay[iLayer].m_fFlags |= ANIM_LAYER_DONTRESTORE;
  891. }
  892. else
  893. {
  894. m_AnimOverlay[iLayer].m_fFlags &= ~ANIM_LAYER_DONTRESTORE;
  895. }
  896. }
  897. //-----------------------------------------------------------------------------
  898. // Purpose:
  899. //-----------------------------------------------------------------------------
  900. Activity CBaseAnimatingOverlay::GetLayerActivity( int iLayer )
  901. {
  902. if (!IsValidLayer( iLayer ))
  903. {
  904. return ACT_INVALID;
  905. }
  906. return m_AnimOverlay[iLayer].m_nActivity;
  907. }
  908. //-----------------------------------------------------------------------------
  909. // Purpose:
  910. //-----------------------------------------------------------------------------
  911. int CBaseAnimatingOverlay::GetLayerSequence( int iLayer )
  912. {
  913. if (!IsValidLayer( iLayer ))
  914. {
  915. return ACT_INVALID;
  916. }
  917. return m_AnimOverlay[iLayer].m_nSequence;
  918. }
  919. //-----------------------------------------------------------------------------
  920. // Purpose:
  921. //-----------------------------------------------------------------------------
  922. void CBaseAnimatingOverlay::RemoveLayer( int iLayer, float flKillRate, float flKillDelay )
  923. {
  924. if (!IsValidLayer( iLayer ))
  925. return;
  926. if (flKillRate > 0)
  927. {
  928. m_AnimOverlay[iLayer].m_flKillRate = m_AnimOverlay[iLayer].m_flWeight / flKillRate;
  929. }
  930. else
  931. {
  932. m_AnimOverlay[iLayer].m_flKillRate = 100;
  933. }
  934. m_AnimOverlay[iLayer].m_flKillDelay = flKillDelay;
  935. m_AnimOverlay[iLayer].KillMe();
  936. }
  937. void CBaseAnimatingOverlay::FastRemoveLayer( int iLayer )
  938. {
  939. if (!IsValidLayer( iLayer ))
  940. return;
  941. // shift the other layers down in order
  942. for (int j = 0; j < m_AnimOverlay.Count(); j++ )
  943. {
  944. if ((m_AnimOverlay[ j ].IsActive()) && m_AnimOverlay[ j ].m_nOrder > m_AnimOverlay[ iLayer ].m_nOrder)
  945. {
  946. m_AnimOverlay[ j ].m_nOrder--;
  947. }
  948. }
  949. m_AnimOverlay[ iLayer ].Init( this );
  950. VerifyOrder();
  951. }
  952. CAnimationLayer *CBaseAnimatingOverlay::GetAnimOverlay( int iIndex )
  953. {
  954. iIndex = clamp( iIndex, 0, m_AnimOverlay.Count()-1 );
  955. return &m_AnimOverlay[iIndex];
  956. }
  957. void CBaseAnimatingOverlay::SetNumAnimOverlays( int num )
  958. {
  959. if ( m_AnimOverlay.Count() < num )
  960. {
  961. m_AnimOverlay.AddMultipleToTail( num - m_AnimOverlay.Count() );
  962. }
  963. else if ( m_AnimOverlay.Count() > num )
  964. {
  965. m_AnimOverlay.RemoveMultiple( num, m_AnimOverlay.Count() - num );
  966. }
  967. }
  968. bool CBaseAnimatingOverlay::HasActiveLayer( void )
  969. {
  970. for (int j = 0; j < m_AnimOverlay.Count(); j++ )
  971. {
  972. if ( m_AnimOverlay[ j ].IsActive() )
  973. return true;
  974. }
  975. return false;
  976. }
  977. //-----------------------------------------------------------------------------