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.

183 lines
5.8 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #ifndef AI_PLANE_SOLVER_H
  8. #define AI_PLANE_SOLVER_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "utlvector.h"
  13. #include "ai_movesolver.h"
  14. #include "ehandle.h"
  15. #include "mathlib/vector.h"
  16. #include "simtimer.h"
  17. #include "ai_navtype.h"
  18. //-----------------------------------------------------------------------------
  19. // Forward declarations
  20. //-----------------------------------------------------------------------------
  21. class Vector2D;
  22. class CBaseEntity;
  23. struct edict_t;
  24. class CAI_BaseNPC;
  25. class CAI_Motor;
  26. class CAI_Navigator;
  27. struct AILocalMoveGoal_t;
  28. struct AIMoveTrace_t;
  29. //-------------------------------------
  30. enum AI_SuggestorResult_t
  31. {
  32. SR_NONE,
  33. SR_OK,
  34. SR_FAIL
  35. };
  36. DECLARE_POINTER_HANDLE( Obstacle_t );
  37. #define OBSTACLE_INVALID ( (Obstacle_t)0 )
  38. class CAI_PlaneSolver
  39. {
  40. public:
  41. // constructor
  42. CAI_PlaneSolver( CAI_BaseNPC *pNpc );
  43. // Attempt to find a valid move direction for the specifed goal
  44. bool Solve( const AILocalMoveGoal_t &goal, float distClear, Vector *pSolution );
  45. float CalcProbeDist( float speed );
  46. // Flush any cached results (e.g., hull changed, results not valid)
  47. void Reset();
  48. void AddObstacle( const Vector &pos, float radius, CBaseEntity *pEntity = NULL, AI_MoveSuggType_t type = AIMST_AVOID_OBJECT );
  49. bool HaveObstacles() { return ( m_Obstacles.Count() != 0 ) || ( s_GlobalObstacles.Count() != 0 ); }
  50. static Obstacle_t AddGlobalObstacle( const Vector &pos, float radius, CBaseEntity *pEntity = NULL, AI_MoveSuggType_t type = AIMST_AVOID_OBJECT );
  51. static void RemoveGlobalObstacle( Obstacle_t hObstacle );
  52. static void RemoveGlobalObstacles( void );
  53. static bool IsSegmentBlockedByGlobalObstacles( const Vector &vecStart, const Vector &vecEnd );
  54. private:
  55. enum
  56. {
  57. DEGREES_POSITIVE_ARC = 270,
  58. DEGREES_POSITIVE_ARC_CLOSE_OBSTRUCTION = 340,
  59. NUM_PROBES = 5
  60. };
  61. struct CircleObstacles_t
  62. {
  63. CircleObstacles_t( const Vector &center, float radius, CBaseEntity *pEntity, AI_MoveSuggType_t type )
  64. : center(center),
  65. radius(radius),
  66. hEntity(pEntity),
  67. type(type)
  68. {
  69. }
  70. Vector center;
  71. float radius;
  72. AI_MoveSuggType_t type;
  73. EHANDLE hEntity;
  74. };
  75. // How far ahead does the ground solver look (seconds)
  76. float GetLookaheadTime() { return 1.0; }
  77. // --------------------------------
  78. // For debugging purposes
  79. void VisualizeRegulations();
  80. void VisualizeSolution( const Vector &vecGoal, const Vector& vecActual );
  81. // --------------------------------
  82. bool MoveLimit( Navigation_t navType, const Vector &target, bool ignoreTransients, bool fCheckStep, AIMoveTrace_t *pMoveTrace );
  83. bool MoveLimit( Navigation_t navType, const Vector &target, bool ignoreTransients, bool fCheckStep, int contents, AIMoveTrace_t *pMoveTrace );
  84. //-----------------------------------------------------------------------------
  85. // Adjust the solution for fliers
  86. //-----------------------------------------------------------------------------
  87. void AdjustSolutionForFliers( const AILocalMoveGoal_t &goal, float flSolutionYaw, Vector *pSolution );
  88. // --------------------------------
  89. // Convenience accessors
  90. CAI_BaseNPC * GetNpc();
  91. CAI_Motor * GetMotor();
  92. const Vector & GetLocalOrigin();
  93. // --------------------------------
  94. void GenerateObstacleNpcs( const AILocalMoveGoal_t &goal, float probeDist );
  95. AI_SuggestorResult_t GenerateObstacleSuggestions( const AILocalMoveGoal_t &goal, const AIMoveTrace_t &directTrace, float distClear, float probeDist, float degreesToProbe, int nProbes );
  96. AI_SuggestorResult_t GenerateObstacleSuggestion( const AILocalMoveGoal_t &goal, float yawScanCenter, float probeDist, float spanPerProbe, int probeOffset );
  97. void GenerateSuggestionFromTrace( const AILocalMoveGoal_t &goal,
  98. const AIMoveTrace_t &moveTrace, float probeDist,
  99. float arcCenter, float arcSpan, int probeOffset );
  100. bool GenerateCircleObstacleSuggestions( const AILocalMoveGoal_t &moveGoal, float probeDist );
  101. void CalcYawsFromOffset( float yawScanCenter, float spanPerProbe, int probeOffset,
  102. float *pYawTest, float *pYawCenter );
  103. float CalculateRegulationWeight( const AIMoveTrace_t &moveTrace, float pctBlockedt );
  104. float AdjustRegulationWeight( CBaseEntity *pEntity, float weight );
  105. unsigned ComputeTurnBiasFlags( const AILocalMoveGoal_t &goal, const AIMoveTrace_t &directTrace );
  106. bool RunMoveSolver( const AILocalMoveGoal_t &goal, const AIMoveTrace_t &directTrace,
  107. float degreesPositiveArc, bool fDeterOscillation,
  108. Vector *pResult );
  109. bool DetectUnsolvable( const AILocalMoveGoal_t &goal );
  110. bool GenerateCircleObstacleSuggestion( const CircleObstacles_t &obstacle, const AILocalMoveGoal_t &moveGoal, float probeDist, const Vector& npcLoc, float radiusNpc );
  111. // --------------------------------
  112. CAI_BaseNPC * m_pNpc;
  113. Vector m_PrevTarget;
  114. bool m_fSolvedPrev;
  115. float m_PrevSolution;
  116. Vector m_PrevSolutionVector;
  117. float m_ClosestHaveBeenToCurrent;
  118. float m_TimeLastProgress;
  119. bool m_fCannotSolveCurrent;
  120. CSimTimer m_RefreshSamplesTimer;
  121. // --------------------------------
  122. CUtlVector<CircleObstacles_t> m_Obstacles;
  123. static CUtlFixedLinkedList<CircleObstacles_t> s_GlobalObstacles;
  124. // --------------------------------
  125. CAI_MoveSolver m_Solver;
  126. };
  127. //-------------------------------------
  128. inline void CAI_PlaneSolver::Reset()
  129. {
  130. m_RefreshSamplesTimer.Force();
  131. m_fSolvedPrev = false;
  132. m_PrevTarget.Init( FLT_MAX, FLT_MAX, FLT_MAX ),
  133. m_PrevSolution = 0;
  134. m_ClosestHaveBeenToCurrent = FLT_MAX;
  135. m_TimeLastProgress = FLT_MAX;
  136. m_fCannotSolveCurrent = false;
  137. }
  138. //=============================================================================
  139. #endif // AI_PLANE_SOLVER_H