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.

219 lines
8.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Revision: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #ifndef ISPATIALPARTITION_H
  9. #define ISPATIALPARTITION_H
  10. #include "interface.h"
  11. //-----------------------------------------------------------------------------
  12. // forward declarations
  13. //-----------------------------------------------------------------------------
  14. class Vector;
  15. struct Ray_t;
  16. class IHandleEntity;
  17. #define INTERFACEVERSION_SPATIALPARTITION "SpatialPartition001"
  18. //-----------------------------------------------------------------------------
  19. // These are the various partition lists. Note some are server only, some
  20. // are client only
  21. //-----------------------------------------------------------------------------
  22. enum
  23. {
  24. PARTITION_ENGINE_SOLID_EDICTS = (1 << 0), // every edict_t that isn't SOLID_TRIGGER or SOLID_NOT (and static props)
  25. PARTITION_ENGINE_TRIGGER_EDICTS = (1 << 1), // every edict_t that IS SOLID_TRIGGER
  26. PARTITION_CLIENT_SOLID_EDICTS = (1 << 2),
  27. PARTITION_CLIENT_RESPONSIVE_EDICTS = (1 << 3), // these are client-side only objects that respond to being forces, etc.
  28. PARTITION_ENGINE_NON_STATIC_EDICTS = (1 << 4), // everything in solid & trigger except the static props, includes SOLID_NOTs
  29. PARTITION_CLIENT_STATIC_PROPS = (1 << 5),
  30. PARTITION_ENGINE_STATIC_PROPS = (1 << 6),
  31. PARTITION_CLIENT_NON_STATIC_EDICTS = (1 << 7), // everything except the static props
  32. };
  33. // Use this to look for all client edicts.
  34. #define PARTITION_ALL_CLIENT_EDICTS ( \
  35. PARTITION_CLIENT_NON_STATIC_EDICTS | \
  36. PARTITION_CLIENT_STATIC_PROPS | \
  37. PARTITION_CLIENT_RESPONSIVE_EDICTS | \
  38. PARTITION_CLIENT_SOLID_EDICTS \
  39. )
  40. // These are the only handles in the spatial partition that the game is controlling (everything but static props)
  41. // These masks are used to handle updating the dirty spatial partition list in each game DLL
  42. #define PARTITION_CLIENT_GAME_EDICTS (PARTITION_ALL_CLIENT_EDICTS & ~PARTITION_CLIENT_STATIC_PROPS)
  43. #define PARTITION_SERVER_GAME_EDICTS (PARTITION_ENGINE_SOLID_EDICTS|PARTITION_ENGINE_TRIGGER_EDICTS|PARTITION_ENGINE_NON_STATIC_EDICTS)
  44. //-----------------------------------------------------------------------------
  45. // Clients that want to know about all elements within a particular
  46. // volume must inherit from this
  47. //-----------------------------------------------------------------------------
  48. enum IterationRetval_t
  49. {
  50. ITERATION_CONTINUE = 0,
  51. ITERATION_STOP,
  52. };
  53. typedef unsigned short SpatialPartitionHandle_t;
  54. // A combination of the PARTITION_ flags above.
  55. typedef int SpatialPartitionListMask_t;
  56. typedef int SpatialTempHandle_t;
  57. //-----------------------------------------------------------------------------
  58. // Any search in the CSpatialPartition must use this to filter out entities it doesn't want.
  59. // You're forced to use listMasks because it can filter by listMasks really fast. Any other
  60. // filtering can be done by EnumElement.
  61. //-----------------------------------------------------------------------------
  62. class IPartitionEnumerator
  63. {
  64. public:
  65. virtual IterationRetval_t EnumElement( IHandleEntity *pHandleEntity ) = 0;
  66. // XXX(johns): This should have a virtual destructor, but would be ABI breaking (non-versioned interface implemented
  67. // by the game)
  68. // virtual ~IPartitionEnumerator(){}
  69. };
  70. //-----------------------------------------------------------------------------
  71. // Installs a callback to call right before a spatial partition query occurs
  72. //-----------------------------------------------------------------------------
  73. class IPartitionQueryCallback
  74. {
  75. public:
  76. virtual void OnPreQuery_V1() = 0;
  77. virtual void OnPreQuery( SpatialPartitionListMask_t listMask ) = 0;
  78. virtual void OnPostQuery( SpatialPartitionListMask_t listMask ) = 0;
  79. };
  80. //-----------------------------------------------------------------------------
  81. // This is the spatial partition manager, groups objects into buckets
  82. //-----------------------------------------------------------------------------
  83. enum
  84. {
  85. PARTITION_INVALID_HANDLE = (SpatialPartitionHandle_t)~0
  86. };
  87. abstract_class ISpatialPartition
  88. {
  89. public:
  90. // Add a virtual destructor to silence the clang warning.
  91. // This is harmless but not important since the only derived class
  92. // doesn't have a destructor.
  93. virtual ~ISpatialPartition() {}
  94. // Create/destroy a handle for this dude in our system. Destroy
  95. // will also remove it from all lists it happens to be in
  96. virtual SpatialPartitionHandle_t CreateHandle( IHandleEntity *pHandleEntity ) = 0;
  97. // A fast method of creating a handle + inserting into the tree in the right place
  98. virtual SpatialPartitionHandle_t CreateHandle( IHandleEntity *pHandleEntity,
  99. SpatialPartitionListMask_t listMask, const Vector& mins, const Vector& maxs ) = 0;
  100. virtual void DestroyHandle( SpatialPartitionHandle_t handle ) = 0;
  101. // Adds, removes an handle from a particular spatial partition list
  102. // There can be multiple partition lists; each has a unique id
  103. virtual void Insert( SpatialPartitionListMask_t listMask,
  104. SpatialPartitionHandle_t handle ) = 0;
  105. virtual void Remove( SpatialPartitionListMask_t listMask,
  106. SpatialPartitionHandle_t handle ) = 0;
  107. // Same as calling Remove() then Insert(). For performance-sensitive areas where you want to save a call.
  108. virtual void RemoveAndInsert( SpatialPartitionListMask_t removeMask, SpatialPartitionListMask_t insertMask,
  109. SpatialPartitionHandle_t handle ) = 0;
  110. // This will remove a particular handle from all lists
  111. virtual void Remove( SpatialPartitionHandle_t handle ) = 0;
  112. // Call this when an entity moves...
  113. virtual void ElementMoved( SpatialPartitionHandle_t handle,
  114. const Vector& mins, const Vector& maxs ) = 0;
  115. // A fast method to insert + remove a handle from the tree...
  116. // This is used to suppress collision of a single model..
  117. virtual SpatialTempHandle_t HideElement( SpatialPartitionHandle_t handle ) = 0;
  118. virtual void UnhideElement( SpatialPartitionHandle_t handle, SpatialTempHandle_t tempHandle ) = 0;
  119. // Installs callbacks to get called right before a query occurs
  120. virtual void InstallQueryCallback_V1( IPartitionQueryCallback *pCallback ) = 0;
  121. virtual void RemoveQueryCallback( IPartitionQueryCallback *pCallback ) = 0;
  122. // Gets all entities in a particular volume...
  123. // if coarseTest == true, it'll return all elements that are in
  124. // spatial partitions that intersect the box
  125. // if coarseTest == false, it'll return only elements that truly intersect
  126. virtual void EnumerateElementsInBox(
  127. SpatialPartitionListMask_t listMask,
  128. const Vector& mins,
  129. const Vector& maxs,
  130. bool coarseTest,
  131. IPartitionEnumerator* pIterator
  132. ) = 0;
  133. virtual void EnumerateElementsInSphere(
  134. SpatialPartitionListMask_t listMask,
  135. const Vector& origin,
  136. float radius,
  137. bool coarseTest,
  138. IPartitionEnumerator* pIterator
  139. ) = 0;
  140. virtual void EnumerateElementsAlongRay(
  141. SpatialPartitionListMask_t listMask,
  142. const Ray_t& ray,
  143. bool coarseTest,
  144. IPartitionEnumerator* pIterator
  145. ) = 0;
  146. virtual void EnumerateElementsAtPoint(
  147. SpatialPartitionListMask_t listMask,
  148. const Vector& pt,
  149. bool coarseTest,
  150. IPartitionEnumerator* pIterator
  151. ) = 0;
  152. // For debugging.... suppress queries on particular lists
  153. virtual void SuppressLists( SpatialPartitionListMask_t nListMask, bool bSuppress ) = 0;
  154. virtual SpatialPartitionListMask_t GetSuppressedLists() = 0;
  155. virtual void RenderAllObjectsInTree( float flTime ) = 0;
  156. virtual void RenderObjectsInPlayerLeafs( const Vector &vecPlayerMin, const Vector &vecPlayerMax, float flTime ) = 0;
  157. virtual void RenderLeafsForRayTraceStart( float flTime ) = 0;
  158. virtual void RenderLeafsForRayTraceEnd( void ) = 0;
  159. virtual void RenderLeafsForHullTraceStart( float flTime ) = 0;
  160. virtual void RenderLeafsForHullTraceEnd( void ) = 0;
  161. virtual void RenderLeafsForBoxStart( float flTime ) = 0;
  162. virtual void RenderLeafsForBoxEnd( void ) = 0;
  163. virtual void RenderLeafsForSphereStart( float flTime ) = 0;
  164. virtual void RenderLeafsForSphereEnd( void ) = 0;
  165. virtual void RenderObjectsInBox( const Vector &vecMin, const Vector &vecMax, float flTime ) = 0;
  166. virtual void RenderObjectsInSphere( const Vector &vecCenter, float flRadius, float flTime ) = 0;
  167. virtual void RenderObjectsAlongRay( const Ray_t& ray, float flTime ) = 0;
  168. virtual void ReportStats( const char *pFileName ) = 0;
  169. virtual void InstallQueryCallback( IPartitionQueryCallback *pCallback ) = 0;
  170. };
  171. #endif