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.

65 lines
1.8 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. // nav_pathfind.h
  9. // Path-finding mechanisms using the Navigation Mesh
  10. // Author: Michael S. Booth ([email protected]), January 2003
  11. #ifndef _CS_NAV_PATHFIND_H_
  12. #define _CS_NAV_PATHFIND_H_
  13. #include "nav_pathfind.h"
  14. //--------------------------------------------------------------------------------------------------------------
  15. /**
  16. * Compute travel distance along shortest path from startPos to goalPos.
  17. * Return -1 if can't reach endPos from goalPos.
  18. */
  19. template< typename CostFunctor >
  20. float NavAreaTravelDistance( const Vector &startPos, const Vector &goalPos, CostFunctor &costFunc )
  21. {
  22. CNavArea *startArea = TheNavMesh->GetNearestNavArea( startPos );
  23. if (startArea == NULL)
  24. {
  25. return -1.0f;
  26. }
  27. // compute path between areas using given cost heuristic
  28. CNavArea *goalArea = NULL;
  29. if (NavAreaBuildPath( startArea, NULL, &goalPos, costFunc, &goalArea ) == false)
  30. {
  31. return -1.0f;
  32. }
  33. // compute distance along path
  34. if (goalArea->GetParent() == NULL)
  35. {
  36. // both points are in the same area - return euclidean distance
  37. return (goalPos - startPos).Length();
  38. }
  39. else
  40. {
  41. CNavArea *area;
  42. float distance;
  43. // goalPos is assumed to be inside goalArea (or very close to it) - skip to next area
  44. area = goalArea->GetParent();
  45. distance = (goalPos - area->GetCenter()).Length();
  46. for( ; area->GetParent(); area = area->GetParent() )
  47. {
  48. distance += (area->GetCenter() - area->GetParent()->GetCenter()).Length();
  49. }
  50. // add in distance to startPos
  51. distance += (startPos - area->GetCenter()).Length();
  52. return distance;
  53. }
  54. }
  55. #endif // _CS_NAV_PATHFIND_H_