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.

151 lines
6.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. // nav_node.h
  9. // Navigation Nodes are used when generating a Navigation Mesh by point sampling the map
  10. // Author: Michael S. Booth ([email protected]), January 2003
  11. #ifndef _NAV_NODE_H_
  12. #define _NAV_NODE_H_
  13. #include "nav.h"
  14. // If DEBUG_NAV_NODES is true, nav_show_nodes controls drawing node positions, and
  15. // nav_show_node_id allows you to show the IDs of nodes that didn't get used to create areas.
  16. #define DEBUG_NAV_NODES 1
  17. //--------------------------------------------------------------------------------------------------------------
  18. /**
  19. * Navigation Nodes.
  20. * These Nodes encapsulate world locations, and ways to get from one location to an adjacent one.
  21. * Note that these links are not necessarily commutative (falling off of a ledge, for example).
  22. */
  23. class CNavNode
  24. {
  25. public:
  26. CNavNode( const Vector &pos, const Vector &normal, CNavNode *parent, bool onDisplacement );
  27. ~CNavNode();
  28. static CNavNode *GetNode( const Vector &pos ); ///< return navigation node at the position, or NULL if none exists
  29. static void CleanupGeneration();
  30. CNavNode *GetConnectedNode( NavDirType dir ) const; ///< get navigation node connected in given direction, or NULL if cant go that way
  31. const Vector *GetPosition( void ) const;
  32. const Vector *GetNormal( void ) const { return &m_normal; }
  33. unsigned int GetID( void ) const { return m_id; }
  34. static CNavNode *GetFirst( void ) { return m_list; }
  35. static unsigned int GetListLength( void ) { return m_listLength; }
  36. CNavNode *GetNext( void ) { return m_next; }
  37. void Draw( void );
  38. void ConnectTo( CNavNode *node, NavDirType dir, float obstacleHeight, float flObstacleStartDist, float flObstacleEndDist ); ///< create a connection FROM this node TO the given node, in the given direction
  39. CNavNode *GetParent( void ) const;
  40. void MarkAsVisited( NavDirType dir ); ///< mark the given direction as having been visited
  41. BOOL HasVisited( NavDirType dir ); ///< return TRUE if the given direction has already been searched
  42. BOOL IsBiLinked( NavDirType dir ) const; ///< node is bidirectionally linked to another node in the given direction
  43. BOOL IsClosedCell( void ) const; ///< node is the NW corner of a bi-linked quad of nodes
  44. void Cover( void ) { m_isCovered = true; } ///< @todo Should pass in area that is covering
  45. BOOL IsCovered( void ) const { return m_isCovered; } ///< return true if this node has been covered by an area
  46. void AssignArea( CNavArea *area ); ///< assign the given area to this node
  47. CNavArea *GetArea( void ) const; ///< return associated area
  48. void SetAttributes( int bits ) { m_attributeFlags = bits; }
  49. int GetAttributes( void ) const { return m_attributeFlags; }
  50. float GetGroundHeightAboveNode( NavCornerType cornerType ) const; ///< return ground height above node in given corner direction (NUM_CORNERS for highest in any direction)
  51. bool IsBlockedInAnyDirection( void) const; ///< return true if the node is blocked in any direction
  52. bool IsOnDisplacement( void ) const { return m_isOnDisplacement; }
  53. private:
  54. CNavNode() {} // constructor used only for hash lookup
  55. friend class CNavMesh;
  56. bool TestForCrouchArea( NavCornerType cornerNum, const Vector& mins, const Vector& maxs, float *groundHeightAboveNode );
  57. void CheckCrouch( void );
  58. Vector m_pos; ///< position of this node in the world
  59. Vector m_normal; ///< surface normal at this location
  60. CNavNode *m_to[ NUM_DIRECTIONS ]; ///< links to north, south, east, and west. NULL if no link
  61. float m_obstacleHeight[ NUM_DIRECTIONS ]; ///< obstacle height (delta from nav node z position) that must be climbed to reach next node in this direction
  62. float m_obstacleStartDist[ NUM_DIRECTIONS ]; ///< distance along this direction to reach the beginning of the obstacle
  63. float m_obstacleEndDist[ NUM_DIRECTIONS ]; ///< distance along this direction to reach the end of the obstacle
  64. unsigned int m_id; ///< unique ID of this node
  65. int m_attributeFlags; ///< set of attribute bit flags (see NavAttributeType)
  66. static CNavNode *m_list; ///< the master list of all nodes for this map
  67. static unsigned int m_listLength;
  68. static unsigned int m_nextID;
  69. CNavNode *m_next; ///< next link in master list
  70. CNavNode *m_nextAtXY; ///< next link at a particular position
  71. // below are only needed when generating
  72. unsigned char m_visited; ///< flags for automatic node generation. If direction bit is clear, that direction hasn't been explored yet.
  73. CNavNode *m_parent; ///< the node prior to this in the search, which we pop back to when this node's search is done (a stack)
  74. bool m_isCovered; ///< true when this node is "covered" by a CNavArea
  75. CNavArea *m_area; ///< the area this node is contained within
  76. bool m_isBlocked[ NUM_CORNERS ];
  77. bool m_crouch[ NUM_CORNERS ];
  78. float m_groundHeightAboveNode[ NUM_CORNERS ];
  79. bool m_isOnDisplacement;
  80. };
  81. //--------------------------------------------------------------------------------------------------------------
  82. //
  83. // Inlines
  84. //
  85. inline CNavNode *CNavNode::GetConnectedNode( NavDirType dir ) const
  86. {
  87. return m_to[ dir ];
  88. }
  89. inline const Vector *CNavNode::GetPosition( void ) const
  90. {
  91. return &m_pos;
  92. }
  93. inline CNavNode *CNavNode::GetParent( void ) const
  94. {
  95. return m_parent;
  96. }
  97. inline void CNavNode::MarkAsVisited( NavDirType dir )
  98. {
  99. m_visited |= (1 << dir);
  100. }
  101. inline BOOL CNavNode::HasVisited( NavDirType dir )
  102. {
  103. if (m_visited & (1 << dir))
  104. return true;
  105. return false;
  106. }
  107. inline void CNavNode::AssignArea( CNavArea *area )
  108. {
  109. m_area = area;
  110. }
  111. inline CNavArea *CNavNode::GetArea( void ) const
  112. {
  113. return m_area;
  114. }
  115. inline bool CNavNode::IsBlockedInAnyDirection( void ) const
  116. {
  117. return m_isBlocked[ SOUTH_EAST ] || m_isBlocked[ SOUTH_WEST ] || m_isBlocked[ NORTH_EAST ] || m_isBlocked[ NORTH_WEST ];
  118. }
  119. #endif // _NAV_NODE_H_