Counter Strike : Global Offensive Source Code
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.

115 lines
2.8 KiB

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