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.

118 lines
3.0 KiB

  1. // BehaviorBackUp.h
  2. // Back up for a short duration
  3. // Author: Michael Booth, March 2007
  4. //========= Copyright Valve Corporation, All rights reserved. ============//
  5. #ifndef _BEHAVIOR_BACK_UP_H_
  6. #define _BEHAVIOR_BACK_UP_H_
  7. //----------------------------------------------------------------------------------------------
  8. /**
  9. * Move backwards for a short duration away from a given position.
  10. * Useful to dislodge ourselves if we get stuck while following our path.
  11. */
  12. template < typename Actor >
  13. class BehaviorBackUp : public Action< Actor >
  14. {
  15. public:
  16. BehaviorBackUp( const Vector &avoidPos );
  17. virtual ActionResult< Actor > OnStart( Actor *me, Action< Actor > *priorAction );
  18. virtual ActionResult< Actor > Update( Actor *me, float interval );
  19. virtual EventDesiredResult< Actor > OnStuck( Actor *me );
  20. virtual const char *GetName( void ) const { return "BehaviorBackUp"; }
  21. private:
  22. CountdownTimer m_giveUpTimer;
  23. CountdownTimer m_backupTimer;
  24. CountdownTimer m_jumpTimer;
  25. Vector m_way;
  26. Vector m_avoidPos;
  27. };
  28. //----------------------------------------------------------------------------------------------
  29. template < typename Actor >
  30. inline BehaviorBackUp< Actor >::BehaviorBackUp( const Vector &avoidPos )
  31. {
  32. m_avoidPos = avoidPos;
  33. }
  34. //----------------------------------------------------------------------------------------------
  35. template < typename Actor >
  36. inline ActionResult< Actor > BehaviorBackUp< Actor >::OnStart( Actor *me, Action< Actor > *priorAction )
  37. {
  38. ILocomotion *mover = me->GetLocomotionInterface();
  39. // don't back off if we're on a ladder
  40. if ( mover && mover->IsUsingLadder() )
  41. {
  42. return Done();
  43. }
  44. float backupTime = RandomFloat( 0.3f, 0.5f );
  45. m_backupTimer.Start( backupTime );
  46. m_jumpTimer.Start( 1.5f * backupTime );
  47. m_giveUpTimer.Start( 2.5f * backupTime );
  48. m_way = me->GetPosition() - m_avoidPos;
  49. m_way.NormalizeInPlace();
  50. return Continue();
  51. }
  52. //----------------------------------------------------------------------------------------------
  53. template < typename Actor >
  54. inline ActionResult< Actor > BehaviorBackUp< Actor >::Update( Actor *me, float interval )
  55. {
  56. if ( m_giveUpTimer.IsElapsed() )
  57. {
  58. return Done();
  59. }
  60. // if ( m_jumpTimer.HasStarted() && m_jumpTimer.IsElapsed() )
  61. // {
  62. // me->GetLocomotionInterface()->Jump();
  63. // m_jumpTimer.Invalidate();
  64. // }
  65. ILocomotion *mover = me->GetLocomotionInterface();
  66. if ( mover )
  67. {
  68. Vector goal;
  69. if ( m_backupTimer.IsElapsed() )
  70. {
  71. // move towards bad spot
  72. goal = m_avoidPos; // me->GetPosition() - 100.0f * m_way;
  73. }
  74. else
  75. {
  76. // move away from bad spot
  77. goal = me->GetPosition() + 100.0f * m_way;
  78. }
  79. mover->Approach( goal );
  80. }
  81. return Continue();
  82. }
  83. //----------------------------------------------------------------------------------------------
  84. template < typename Actor >
  85. inline EventDesiredResult< Actor > BehaviorBackUp< Actor >::OnStuck( Actor *me )
  86. {
  87. return TryToSustain( RESULT_IMPORTANT, "Stuck while trying to back up" );
  88. }
  89. #endif // _BEHAVIOR_BACK_UP_H_