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.

110 lines
3.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "tf_passtime_ball.h"
  9. #include "tf_passtime_logic.h"
  10. #include "passtime_convars.h"
  11. #include "passtime_ballcontroller_playerseek.h"
  12. #include "tf_player.h"
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. namespace {
  16. const int kPriority = 1; // same as homing, so they stack
  17. }
  18. //-----------------------------------------------------------------------------
  19. CPasstimeBallControllerPlayerSeek::CPasstimeBallControllerPlayerSeek()
  20. : CPasstimeBallController( kPriority ) // low priority
  21. , m_fEnableTime( 0 )
  22. {}
  23. //-----------------------------------------------------------------------------
  24. bool CPasstimeBallControllerPlayerSeek::Apply( CPasstimeBall *ball )
  25. {
  26. if ( gpGlobals->curtime < m_fEnableTime )
  27. return false;
  28. return Seek( ball, FindTarget( ball->GetThrower(), ball->GetAbsOrigin() ) );
  29. }
  30. //-----------------------------------------------------------------------------
  31. CTFPlayer *CPasstimeBallControllerPlayerSeek::FindTarget( CTFPlayer *pIgnorePlayer, const Vector& ballOrigin ) const
  32. {
  33. CTFPlayer *pTarget = 0;
  34. float closestPlayerDist = tf_passtime_ball_seek_range.GetFloat();
  35. closestPlayerDist *= closestPlayerDist; // avoid some sqrts
  36. // Treat this trace exactly like radius damage
  37. CTraceFilterIgnorePlayers traceFilter( 0, COLLISION_GROUP_PLAYER_MOVEMENT );
  38. for ( int i = 1; i <= MAX_PLAYERS; i++ )
  39. {
  40. CTFPlayer *pPlayer = ToTFPlayer( UTIL_PlayerByIndex( i ) );
  41. if ( !pPlayer )
  42. continue; // wat
  43. if ( pPlayer == pIgnorePlayer )
  44. continue;
  45. const Vector& eyepos = pPlayer->WorldSpaceCenter();
  46. const float dist = (eyepos - ballOrigin).LengthSqr();
  47. if ( dist >= closestPlayerDist )
  48. continue; // to far away
  49. if ( pPlayer->m_Shared.InCond( TF_COND_DISGUISED ) )
  50. continue; // don't seek any disguised people
  51. if ( !g_pPasstimeLogic->BCanPlayerPickUpBall( pPlayer ) )
  52. continue; // can't pick it up
  53. trace_t trace;
  54. UTIL_TraceLine( ballOrigin, eyepos, MASK_PLAYERSOLID, &traceFilter, &trace );
  55. if ( trace.fraction != 1 )
  56. continue; // occluded
  57. // chicken dinner
  58. pTarget = pPlayer;
  59. closestPlayerDist = dist;
  60. }
  61. return pTarget;
  62. }
  63. extern float GetCurrentGravity( void ); // tf_gamerules.h
  64. //-----------------------------------------------------------------------------
  65. bool CPasstimeBallControllerPlayerSeek::Seek( CPasstimeBall *ball, CTFPlayer *pTarget ) const
  66. {
  67. if ( !pTarget )
  68. return false;
  69. // taken from ballcontroller_homing
  70. IPhysicsObject *pPhys = ball->VPhysicsGetObject();
  71. Vector ballpos;
  72. pPhys->GetPosition( &ballpos, 0 );
  73. Vector targetvel = pTarget->EyePosition() - ballpos;
  74. targetvel.NormalizeInPlace();
  75. targetvel *= pTarget->TeamFortress_CalculateMaxSpeed() *
  76. tf_passtime_ball_seek_speed_factor.GetFloat();
  77. Vector currentvel;
  78. pPhys->GetVelocity( &currentvel, 0 );
  79. Vector steer = targetvel - currentvel;
  80. pPhys->AddVelocity( &steer, 0 );
  81. return true;
  82. }
  83. //-----------------------------------------------------------------------------
  84. void CPasstimeBallControllerPlayerSeek::OnBallSpawned( CPasstimeBall *ball )
  85. {
  86. // NOTE: this hack is the only thing that prevents players from picking up
  87. // balls immediately after they were thrown (including by nearby unrelated
  88. // players)
  89. m_fEnableTime = gpGlobals->curtime + 0.25f;
  90. }