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.

116 lines
2.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. // simple_bot.h
  3. // A mininal example of a NextBotCombatCharacter (ie: non-player) bot
  4. // Michael Booth, February 2009
  5. #ifndef SIMPLE_BOT_H
  6. #define SIMPLE_BOT_H
  7. #include "NextBot.h"
  8. #include "NextBotBehavior.h"
  9. #include "NextBotGroundLocomotion.h"
  10. #include "Path/NextBotPathFollow.h"
  11. //----------------------------------------------------------------------------
  12. /**
  13. * A Simple Bot
  14. */
  15. class CSimpleBot : public NextBotCombatCharacter
  16. {
  17. public:
  18. DECLARE_CLASS( CSimpleBot, NextBotCombatCharacter );
  19. CSimpleBot();
  20. virtual ~CSimpleBot();
  21. virtual void Precache();
  22. virtual void Spawn( void );
  23. // INextBot
  24. DECLARE_INTENTION_INTERFACE( CSimpleBot )
  25. virtual NextBotGroundLocomotion *GetLocomotionInterface( void ) const { return m_locomotor; }
  26. private:
  27. NextBotGroundLocomotion *m_locomotor;
  28. };
  29. //--------------------------------------------------------------------------------------------------------------
  30. /**
  31. * Functor used with the A* algorithm of NavAreaBuildPath() to determine the "cost" of moving from one area to another.
  32. * "Cost" is generally the weighted distance between the centers of the areas. If you want the bot
  33. * to avoid an area/ladder/elevator, increase the cost. If you want to disallow an area/ladder/elevator, return -1.
  34. */
  35. class CSimpleBotPathCost : public IPathCost
  36. {
  37. public:
  38. CSimpleBotPathCost( CSimpleBot *me )
  39. {
  40. m_me = me;
  41. }
  42. // return the cost (weighted distance between) of moving from "fromArea" to "area", or -1 if the move is not allowed
  43. virtual float operator()( CNavArea *area, CNavArea *fromArea, const CNavLadder *ladder, const CFuncElevator *elevator, float length ) const
  44. {
  45. if ( fromArea == NULL )
  46. {
  47. // first area in path, no cost
  48. return 0.0f;
  49. }
  50. else
  51. {
  52. if ( !m_me->GetLocomotionInterface()->IsAreaTraversable( area ) )
  53. {
  54. // our locomotor says we can't move here
  55. return -1.0f;
  56. }
  57. // compute distance traveled along path so far
  58. float dist;
  59. if ( ladder )
  60. {
  61. dist = ladder->m_length;
  62. }
  63. else if ( length > 0.0 )
  64. {
  65. // optimization to avoid recomputing length
  66. dist = length;
  67. }
  68. else
  69. {
  70. dist = ( area->GetCenter() - fromArea->GetCenter() ).Length();
  71. }
  72. float cost = dist + fromArea->GetCostSoFar();
  73. // check height change
  74. float deltaZ = fromArea->ComputeAdjacentConnectionHeightChange( area );
  75. if ( deltaZ >= m_me->GetLocomotionInterface()->GetStepHeight() )
  76. {
  77. if ( deltaZ >= m_me->GetLocomotionInterface()->GetMaxJumpHeight() )
  78. {
  79. // too high to reach
  80. return -1.0f;
  81. }
  82. // jumping is slower than flat ground
  83. const float jumpPenalty = 5.0f;
  84. cost += jumpPenalty * dist;
  85. }
  86. else if ( deltaZ < -m_me->GetLocomotionInterface()->GetDeathDropHeight() )
  87. {
  88. // too far to drop
  89. return -1.0f;
  90. }
  91. return cost;
  92. }
  93. }
  94. CSimpleBot *m_me;
  95. };
  96. #endif // SIMPLE_BOT_H