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.

139 lines
3.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Hooks and classes for the support of humanoid NPCs with
  4. // groovy facial animation capabilities, aka, "Actors"
  5. //
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "AI_Interest_Target.h"
  9. // memdbgon must be the last include file in a .cpp file!!!
  10. #include "tier0/memdbgon.h"
  11. bool CAI_InterestTarget_t::IsThis( CBaseEntity *pThis )
  12. {
  13. return (pThis == m_hTarget);
  14. };
  15. const Vector &CAI_InterestTarget_t::GetPosition( void )
  16. {
  17. if (m_eType == LOOKAT_ENTITY && m_hTarget != NULL)
  18. {
  19. m_vecPosition = m_hTarget->EyePosition();
  20. }
  21. return m_vecPosition;
  22. };
  23. bool CAI_InterestTarget_t::IsActive( void )
  24. {
  25. if (m_flEndTime < gpGlobals->curtime) return false;
  26. if (m_eType == LOOKAT_ENTITY && m_hTarget == NULL) return false;
  27. return true;
  28. };
  29. float CAI_InterestTarget_t::Interest( void )
  30. {
  31. float t = (gpGlobals->curtime - m_flStartTime) / (m_flEndTime - m_flStartTime);
  32. if (t < 0.0f || t > 1.0f)
  33. return 0.0f;
  34. if (m_flRamp && t < 1 - m_flRamp)
  35. {
  36. //t = t / m_flRamp;
  37. t = 1.0 - ExponentialDecay( 0.2, m_flRamp, t );
  38. //t = 1.0 - ExponentialDecay( 0.01, 1 - m_flRamp, t );
  39. }
  40. else if (t > 1.0f - m_flRamp)
  41. {
  42. t = (1.0 - t) / m_flRamp;
  43. t = 3.0f * t * t - 2.0f * t * t * t;
  44. }
  45. else
  46. {
  47. t = 1.0f;
  48. }
  49. // ramp
  50. t *= m_flInterest;
  51. return t;
  52. }
  53. void CAI_InterestTarget::Add( CBaseEntity *pTarget, float flImportance, float flDuration, float flRamp )
  54. {
  55. int i;
  56. for (i = 0; i < Count(); i++)
  57. {
  58. CAI_InterestTarget_t &target = Element( i );
  59. if (target.m_hTarget == pTarget && target.m_flRamp == 0)
  60. {
  61. if (target.m_flStartTime == gpGlobals->curtime)
  62. {
  63. flImportance = MAX( flImportance, target.m_flInterest );
  64. }
  65. Remove( i );
  66. break;
  67. }
  68. }
  69. Add( CAI_InterestTarget_t::LOOKAT_ENTITY, pTarget, Vector( 0, 0, 0 ), flImportance, flDuration, flRamp );
  70. }
  71. void CAI_InterestTarget::Add( const Vector &vecPosition, float flImportance, float flDuration, float flRamp )
  72. {
  73. int i;
  74. for (i = 0; i < Count(); i++)
  75. {
  76. CAI_InterestTarget_t &target = Element( i );
  77. if (target.m_vecPosition == vecPosition)
  78. {
  79. Remove( i );
  80. break;
  81. }
  82. }
  83. Add( CAI_InterestTarget_t::LOOKAT_POSITION, NULL, vecPosition, flImportance, flDuration, flRamp );
  84. }
  85. void CAI_InterestTarget::Add( CBaseEntity *pTarget, const Vector &vecPosition, float flImportance, float flDuration, float flRamp )
  86. {
  87. int i;
  88. for (i = 0; i < Count(); i++)
  89. {
  90. CAI_InterestTarget_t &target = Element( i );
  91. if (target.m_hTarget == pTarget)
  92. {
  93. if (target.m_flStartTime == gpGlobals->curtime)
  94. {
  95. flImportance = MAX( flImportance, target.m_flInterest );
  96. }
  97. Remove( i );
  98. break;
  99. }
  100. }
  101. Add( CAI_InterestTarget_t::LOOKAT_BOTH, pTarget, vecPosition, flImportance, flDuration, flRamp );
  102. }
  103. void CAI_InterestTarget::Add( CAI_InterestTarget_t::CAI_InterestTarget_e type, CBaseEntity *pTarget, const Vector &vecPosition, float flImportance, float flDuration, float flRamp )
  104. {
  105. int i = AddToTail();
  106. CAI_InterestTarget_t &target = Element( i );
  107. target.m_eType = type;
  108. target.m_hTarget = pTarget;
  109. target.m_vecPosition = vecPosition;
  110. target.m_flInterest = flImportance;
  111. target.m_flStartTime = gpGlobals->curtime;
  112. target.m_flEndTime = gpGlobals->curtime + flDuration;
  113. target.m_flRamp = flRamp / flDuration;
  114. }