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.

148 lines
3.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #include "game.h"
  10. #include "ai_looktarget.h"
  11. // Mothballing this entity to get rid of it. info_hint does its job better (sjb)
  12. //LINK_ENTITY_TO_CLASS( ai_looktarget, CAI_LookTarget );
  13. BEGIN_DATADESC( CAI_LookTarget )
  14. // Keyfields
  15. DEFINE_KEYFIELD( m_bDisabled, FIELD_BOOLEAN, "StartDisabled" ),
  16. DEFINE_KEYFIELD( m_iContext, FIELD_INTEGER, "context" ),
  17. DEFINE_KEYFIELD( m_iPriority, FIELD_INTEGER, "priority" ),
  18. DEFINE_KEYFIELD( m_flMaxDist, FIELD_FLOAT, "maxdist" ),
  19. // Fields
  20. DEFINE_FIELD( m_flTimeNextAvailable, FIELD_TIME ),
  21. END_DATADESC()
  22. //---------------------------------------------------------
  23. //---------------------------------------------------------
  24. int CAI_LookTarget::DrawDebugTextOverlays(void)
  25. {
  26. int text_offset = BaseClass::DrawDebugTextOverlays();
  27. if (m_debugOverlays & OVERLAY_BBOX_BIT)
  28. {
  29. int color = random->RandomInt( 50, 255 );
  30. NDebugOverlay::Cross3D( GetAbsOrigin(), 12, color, color, color, false, 0.1 );
  31. }
  32. if (m_debugOverlays & OVERLAY_TEXT_BIT)
  33. {
  34. char tempstr[512];
  35. if( !IsEnabled() )
  36. {
  37. Q_snprintf(tempstr,sizeof(tempstr),"DISABLED" );
  38. EntityText(text_offset,tempstr,0);
  39. text_offset++;
  40. }
  41. if( IsEligible( NULL ) )
  42. {
  43. Q_snprintf(tempstr,sizeof(tempstr),"Eligible" );
  44. EntityText(text_offset,tempstr,0);
  45. text_offset++;
  46. }
  47. else
  48. {
  49. Q_snprintf(tempstr,sizeof(tempstr),"NOT Eligible for selection");
  50. EntityText(text_offset,tempstr,0);
  51. text_offset++;
  52. }
  53. }
  54. return text_offset;
  55. }
  56. //---------------------------------------------------------
  57. //---------------------------------------------------------
  58. bool CAI_LookTarget::IsEligible( CBaseEntity *pLooker )
  59. {
  60. if( !IsEnabled() )
  61. return false;
  62. if( !IsAvailable() )
  63. return false;
  64. if( pLooker )
  65. {
  66. float maxdistsqr = m_flMaxDist * m_flMaxDist;
  67. Vector vecPos = GetAbsOrigin();
  68. if( vecPos.DistToSqr( pLooker->WorldSpaceCenter() ) > maxdistsqr )
  69. {
  70. return false;
  71. }
  72. }
  73. return true;
  74. }
  75. //---------------------------------------------------------
  76. // Someone's reserving this entity because they're going
  77. // to attempt to look at it for flDuration seconds. We'll
  78. // make it unavailable to anyone else for that time.
  79. //---------------------------------------------------------
  80. void CAI_LookTarget::Reserve( float flDuration )
  81. {
  82. m_flTimeNextAvailable = gpGlobals->curtime + flDuration;
  83. if( HasSpawnFlags( SF_LOOKTARGET_ONLYONCE ) )
  84. {
  85. // No one will look at this again.
  86. Disable();
  87. }
  88. }
  89. //---------------------------------------------------------
  90. //---------------------------------------------------------
  91. CAI_LookTarget *CAI_LookTarget::GetFirstLookTarget()
  92. {
  93. CBaseEntity *pEnt;
  94. string_t iszLookTarget = FindPooledString( "ai_looktarget" );
  95. if( iszLookTarget == NULL_STRING )
  96. {
  97. return NULL;
  98. }
  99. pEnt = gEntList.FirstEnt();
  100. while( pEnt && pEnt->m_iClassname != iszLookTarget )
  101. {
  102. pEnt = gEntList.NextEnt( pEnt );
  103. }
  104. return (CAI_LookTarget*)pEnt;
  105. }
  106. //---------------------------------------------------------
  107. //---------------------------------------------------------
  108. CAI_LookTarget *CAI_LookTarget::GetNextLookTarget( CAI_LookTarget *pCurrentTarget )
  109. {
  110. CBaseEntity *pEnt;
  111. string_t iszLookTarget = FindPooledString( "ai_looktarget" );
  112. if( iszLookTarget == NULL_STRING )
  113. {
  114. return NULL;
  115. }
  116. pEnt = gEntList.NextEnt( pCurrentTarget );
  117. while( pEnt && pEnt->m_iClassname != iszLookTarget )
  118. {
  119. pEnt = gEntList.NextEnt( pEnt );
  120. }
  121. return (CAI_LookTarget*)pEnt;
  122. }