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.

126 lines
3.9 KiB

  1. // BehaviorMoveTo.h
  2. // Move to a potentially far away position
  3. // Author: Michael Booth, June 2007
  4. //========= Copyright Valve Corporation, All rights reserved. ============//
  5. #ifndef _BEHAVIOR_MOVE_TO_H_
  6. #define _BEHAVIOR_MOVE_TO_H_
  7. //----------------------------------------------------------------------------------------------
  8. /**
  9. * Move to a potentially far away position, using path planning.
  10. */
  11. template < typename Actor, typename PathCost >
  12. class BehaviorMoveTo : public Action< Actor >
  13. {
  14. public:
  15. BehaviorMoveTo( const Vector &goal, Action< Actor > *successAction = NULL, Action< Actor > *failAction = NULL );
  16. virtual ActionResult< Actor > OnStart( Actor *me, Action< Actor > *priorAction );
  17. virtual ActionResult< Actor > Update( Actor *me, float interval );
  18. virtual EventDesiredResult< Actor > OnMoveToSuccess( Actor *me, const Path *path );
  19. virtual EventDesiredResult< Actor > OnMoveToFailure( Actor *me, const Path *path, MoveToFailureType reason );
  20. virtual bool ComputePath( Actor *me, const Vector &goal, PathFollower *path );
  21. virtual const char *GetName( void ) const { return "BehaviorMoveTo"; }
  22. private:
  23. Vector m_goal;
  24. PathFollower m_path;
  25. Action< Actor > *m_successAction;
  26. Action< Actor > *m_failAction;
  27. };
  28. //----------------------------------------------------------------------------------------------
  29. template < typename Actor, typename PathCost >
  30. inline BehaviorMoveTo< Actor, PathCost >::BehaviorMoveTo( const Vector &goal, Action< Actor > *successAction, Action< Actor > *failAction )
  31. {
  32. m_goal = goal;
  33. m_path.Invalidate();
  34. m_successAction = successAction;
  35. m_failAction = failAction;
  36. }
  37. //----------------------------------------------------------------------------------------------
  38. template < typename Actor, typename PathCost >
  39. inline bool BehaviorMoveTo< Actor, PathCost >::ComputePath( Actor *me, const Vector &goal, PathFollower *path )
  40. {
  41. PathCost cost( me );
  42. return path->Compute( me, goal, cost );
  43. }
  44. //----------------------------------------------------------------------------------------------
  45. template < typename Actor, typename PathCost >
  46. inline ActionResult< Actor > BehaviorMoveTo< Actor, PathCost >::OnStart( Actor *me, Action< Actor > *priorAction )
  47. {
  48. if ( !this->ComputePath( me, m_goal, &m_path ) )
  49. {
  50. if ( m_failAction )
  51. {
  52. return this->ChangeTo( m_failAction, "No path to goal" );
  53. }
  54. return this->Done( "No path to goal" );
  55. }
  56. return this->Continue();
  57. }
  58. //----------------------------------------------------------------------------------------------
  59. template < typename Actor, typename PathCost >
  60. inline ActionResult< Actor > BehaviorMoveTo< Actor, PathCost >::Update( Actor *me, float interval )
  61. {
  62. // if path became invalid during last tick for any reason, we're done
  63. if ( !m_path.IsValid() )
  64. {
  65. if ( m_failAction )
  66. {
  67. return this->ChangeTo( m_failAction, "Path is invalid" );
  68. }
  69. return this->Done( "Path is invalid" );
  70. }
  71. // move along path - success/fail event handlers will exit behavior when goal is reached
  72. m_path.Update( me );
  73. return this->Continue();
  74. }
  75. //----------------------------------------------------------------------------------------------
  76. template < typename Actor, typename PathCost >
  77. inline EventDesiredResult< Actor > BehaviorMoveTo< Actor, PathCost >::OnMoveToSuccess( Actor *me, const Path *path )
  78. {
  79. if ( m_successAction )
  80. {
  81. return this->TryChangeTo( m_successAction, RESULT_CRITICAL, "OnMoveToSuccess" );
  82. }
  83. return this->TryDone( RESULT_CRITICAL, "OnMoveToSuccess" );
  84. }
  85. //----------------------------------------------------------------------------------------------
  86. template < typename Actor, typename PathCost >
  87. inline EventDesiredResult< Actor > BehaviorMoveTo< Actor, PathCost >::OnMoveToFailure( Actor *me, const Path *path, MoveToFailureType reason )
  88. {
  89. if ( m_failAction )
  90. {
  91. return this->TryChangeTo( m_failAction, RESULT_CRITICAL, "OnMoveToFailure" );
  92. }
  93. return this->TryDone( RESULT_CRITICAL, "OnMoveToFailure" );
  94. }
  95. #endif // _BEHAVIOR_MOVE_TO_H_