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.

91 lines
2.7 KiB

  1. // NextBotIntentionInterface.cpp
  2. // Interface for intentional thinking
  3. // Author: Michael Booth, November 2007
  4. //========= Copyright Valve Corporation, All rights reserved. ============//
  5. #include "cbase.h"
  6. #include "NextBotInterface.h"
  7. #include "NextBotIntentionInterface.h"
  8. // memdbgon must be the last include file in a .cpp file!!!
  9. #include "tier0/memdbgon.h"
  10. //------------------------------------------------------------------------------------------------------------------------
  11. /**
  12. * Given a subject, return the world space position we should aim at
  13. */
  14. Vector IIntention::SelectTargetPoint( const INextBot *me, const CBaseCombatCharacter *subject ) const
  15. {
  16. for ( INextBotEventResponder *sub = FirstContainedResponder(); sub; sub = NextContainedResponder( sub ) )
  17. {
  18. const IContextualQuery *query = dynamic_cast< const IContextualQuery * >( sub );
  19. if ( query )
  20. {
  21. // return the response of the first responder that gives a definitive answer
  22. Vector result = query->SelectTargetPoint( me, subject );
  23. if ( result != vec3_origin )
  24. {
  25. return result;
  26. }
  27. }
  28. }
  29. // no answer, use a reasonable position
  30. Vector threatMins, threatMaxs;
  31. subject->CollisionProp()->WorldSpaceAABB( &threatMins, &threatMaxs );
  32. Vector targetPoint = subject->GetAbsOrigin();
  33. targetPoint.z += 0.7f * ( threatMaxs.z - threatMins.z );
  34. return targetPoint;
  35. }
  36. //------------------------------------------------------------------------------------------------------------------------
  37. /**
  38. * Given two threats, decide which one is more dangerous
  39. */
  40. const CKnownEntity *IIntention::SelectMoreDangerousThreat( const INextBot *me, const CBaseCombatCharacter *subject, const CKnownEntity *threat1, const CKnownEntity *threat2 ) const
  41. {
  42. if ( !threat1 || threat1->IsObsolete() )
  43. {
  44. if ( threat2 && !threat2->IsObsolete() )
  45. return threat2;
  46. return NULL;
  47. }
  48. else if ( !threat2 || threat2->IsObsolete() )
  49. {
  50. return threat1;
  51. }
  52. for ( INextBotEventResponder *sub = FirstContainedResponder(); sub; sub = NextContainedResponder( sub ) )
  53. {
  54. const IContextualQuery *query = dynamic_cast< const IContextualQuery * >( sub );
  55. if ( query )
  56. {
  57. // return the response of the first responder that gives a definitive answer
  58. const CKnownEntity *result = query->SelectMoreDangerousThreat( me, subject, threat1, threat2 );
  59. if ( result )
  60. {
  61. return result;
  62. }
  63. }
  64. }
  65. // no specific decision was made - return closest threat as most dangerous
  66. float range1 = ( subject->GetAbsOrigin() - threat1->GetLastKnownPosition() ).LengthSqr();
  67. float range2 = ( subject->GetAbsOrigin() - threat2->GetLastKnownPosition() ).LengthSqr();
  68. if ( range1 < range2 )
  69. {
  70. return threat1;
  71. }
  72. return threat2;
  73. }