g_navigator.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. ////////////////////////////////////////////////////////////////////////////////////////
  2. // RAVEN SOFTWARE - STAR WARS: JK II
  3. // (c) 2002 Activision
  4. //
  5. //
  6. //
  7. // NAVIGATOR
  8. // ---------
  9. // This file provides an interface to two actor related systems:
  10. // - Path Finding
  11. // - Steering
  12. //
  13. //
  14. //
  15. ////////////////////////////////////////////////////////////////////////////////////////
  16. #ifndef __G_NAVIGATOR__
  17. #define __G_NAVIGATOR__
  18. #define USENEWNAVSYSTEM 1
  19. #if !defined(RAVL_VEC_INC)
  20. #include "..\Ravl\CVec.h"
  21. #endif
  22. ////////////////////////////////////////////////////////////////////////////////////////
  23. // The NAV Namespace
  24. //
  25. // This namespace provides the public interface to the NPC Navigation and Pathfinding
  26. // system. This system is a bidirectional graph of nodes and weighted edges. Finding
  27. // a path from one node to another is accomplished with A*, and cached internally for
  28. // each actor who requests a path.
  29. ////////////////////////////////////////////////////////////////////////////////////////
  30. namespace NAV
  31. {
  32. typedef int TNodeHandle;
  33. typedef int TEdgeHandle;
  34. enum EPointType
  35. {
  36. PT_NONE = 0,
  37. PT_WAYNODE,
  38. PT_COMBATNODE,
  39. PT_GOALNODE,
  40. PT_MAX
  41. };
  42. ////////////////////////////////////////////////////////////////////////////////////
  43. // Save, Load, Construct
  44. ////////////////////////////////////////////////////////////////////////////////////
  45. bool LoadFromFile(const char *filename, int checksum);
  46. bool TestEdge( TNodeHandle NodeA, TNodeHandle NodeB, qboolean IsDebugEdge );
  47. bool LoadFromEntitiesAndSaveToFile(const char *filename, int checksum);
  48. void SpawnedPoint(gentity_t* ent, EPointType type=PT_WAYNODE);
  49. ////////////////////////////////////////////////////////////////////////////////////
  50. // Finding Nav Points
  51. ////////////////////////////////////////////////////////////////////////////////////
  52. TNodeHandle GetNearestNode(gentity_t* ent, bool forceRecalcNow=false, NAV::TNodeHandle goal=0);
  53. TNodeHandle GetNearestNode(const vec3_t& position, TNodeHandle previous=0, NAV::TNodeHandle goal=0, int ignoreEnt=ENTITYNUM_NONE, bool allowZOffset=false);
  54. TNodeHandle ChooseRandomNeighbor(TNodeHandle NodeHandle);
  55. TNodeHandle ChooseRandomNeighbor(TNodeHandle NodeHandle, const vec3_t& position, float maxDistance);
  56. TNodeHandle ChooseClosestNeighbor(TNodeHandle NodeHandle, const vec3_t& position);
  57. TNodeHandle ChooseFarthestNeighbor(TNodeHandle NodeHandle, const vec3_t& position);
  58. TNodeHandle ChooseFarthestNeighbor(gentity_t* actor, const vec3_t& target, float maxSafeDot);
  59. ////////////////////////////////////////////////////////////////////////////////////
  60. // Get The Location Of A Given Node Handle
  61. ////////////////////////////////////////////////////////////////////////////////////
  62. const vec3_t& GetNodePosition(TNodeHandle NodeHandle);
  63. void GetNodePosition(TNodeHandle NodeHandle, vec3_t& position);
  64. ////////////////////////////////////////////////////////////////////////////////////
  65. // Testing Nearness
  66. ////////////////////////////////////////////////////////////////////////////////////
  67. float EstimateCostToGoal(const vec3_t& position, TNodeHandle Goal);
  68. float EstimateCostToGoal(TNodeHandle Start, TNodeHandle Goal);
  69. bool OnSamePoint(gentity_t* actor, gentity_t* target);
  70. bool OnNeighboringPoints(TNodeHandle A, TNodeHandle B);
  71. bool OnNeighboringPoints(gentity_t* actor, gentity_t* target);
  72. bool OnNeighboringPoints(gentity_t* actor, const vec3_t& position);
  73. bool InSameRegion(gentity_t* actor, gentity_t* target);
  74. bool InSameRegion(gentity_t* actor, const vec3_t& position);
  75. bool InSameRegion(TNodeHandle A, TNodeHandle B);
  76. bool InSafeRadius(CVec3 at, TNodeHandle atNode, TNodeHandle targetNode=0);
  77. ////////////////////////////////////////////////////////////////////////////////////
  78. // Finding A Path
  79. ////////////////////////////////////////////////////////////////////////////////////
  80. bool GoTo(gentity_t* actor, TNodeHandle target, float MaxDangerLevel=1.0f);
  81. bool GoTo(gentity_t* actor, gentity_t* target, float MaxDangerLevel=1.0f);
  82. bool GoTo(gentity_t* actor, const vec3_t& position, float MaxDangerLevel=1.0f);
  83. bool FindPath(gentity_t* actor, TNodeHandle target, float MaxDangerLevel=1.0f);
  84. bool FindPath(gentity_t* actor, gentity_t* target, float MaxDangerLevel=1.0f);
  85. bool FindPath(gentity_t* actor, const vec3_t& position, float MaxDangerLevel=1.0f);
  86. bool SafePathExists(const CVec3& start, const CVec3& stop, const CVec3& danger, float dangerDistSq);
  87. bool HasPath(gentity_t* actor, TNodeHandle target=PT_NONE);
  88. void ClearPath(gentity_t* actor);
  89. bool UpdatePath(gentity_t* actor, TNodeHandle target=PT_NONE, float MaxDangerLevel=1.0f);
  90. float PathDangerLevel(gentity_t* actor);
  91. int PathNodesRemaining(gentity_t* actor);
  92. const vec3_t& NextPosition(gentity_t* actor);
  93. bool NextPosition(gentity_t* actor, CVec3& Position);
  94. bool NextPosition(gentity_t* actor, CVec3& Position, float& SlowingRadius, bool& Fly, bool& Jump);
  95. ////////////////////////////////////////////////////////////////////////////////////
  96. // Update One Or More Edges As A Result Of An Entity Getting Removed
  97. ////////////////////////////////////////////////////////////////////////////////////
  98. void WayEdgesNowClear(gentity_t* ent);
  99. ////////////////////////////////////////////////////////////////////////////////////
  100. // How Big Is The Given Ent
  101. ////////////////////////////////////////////////////////////////////////////////////
  102. unsigned int ClassifyEntSize(gentity_t* ent);
  103. void RegisterDangerSense(gentity_t* actor, int alertEventIndex);
  104. void DecayDangerSenses();
  105. ////////////////////////////////////////////////////////////////////////////////////
  106. // Debugging Information
  107. ////////////////////////////////////////////////////////////////////////////////////
  108. void ShowDebugInfo(const vec3_t& PlayerPosition, TNodeHandle PlayerWaypoint);
  109. void ShowStats();
  110. void TeleportTo(gentity_t* actor, const char* pointName);
  111. void TeleportTo(gentity_t* actor, int pointNum);
  112. }
  113. ////////////////////////////////////////////////////////////////////////////////////////
  114. // The STEER Namespace
  115. //
  116. // These functions allow access to the steering system.
  117. //
  118. // The Reset() and Finalize() functions MUST be called before and after any other steering
  119. // operations. Beyond that, all other steering operations can be called in any order
  120. // and any number of times. Once Finalize() is called, the results of all these
  121. // operations will be summed up and applied as accelleration to the actor's velocity.
  122. ////////////////////////////////////////////////////////////////////////////////////////
  123. namespace STEER
  124. {
  125. ////////////////////////////////////////////////////////////////////////////////////
  126. // Reset & Finalize
  127. //
  128. // Call these two operations before and after all other STEER operations. They
  129. // clear out and setup the thrust vector for use by the entity.
  130. ////////////////////////////////////////////////////////////////////////////////////
  131. void Activate(gentity_t* actor);
  132. void DeActivate(gentity_t* actor, usercmd_t* ucmd);
  133. bool Active(gentity_t* actor);
  134. ////////////////////////////////////////////////////////////////////////////////////
  135. // Master Functions
  136. ////////////////////////////////////////////////////////////////////////////////////
  137. bool GoTo(gentity_t* actor, gentity_t* target, float reachedRadius, bool avoidCollisions=true);
  138. bool GoTo(gentity_t* actor, const vec3_t& position, float reachedRadius, bool avoidCollisions=true);
  139. bool SafeToGoTo(gentity_t* actor, const vec3_t& targetPosition, int targetNode);
  140. ////////////////////////////////////////////////////////////////////////////////////
  141. // Stop
  142. //
  143. // Slow down and come to a stop.
  144. //
  145. ////////////////////////////////////////////////////////////////////////////////////
  146. float Stop(gentity_t* actor, float weight=1.0f);
  147. float MatchSpeed(gentity_t* actor, float speed, float weight=1.0f);
  148. ////////////////////////////////////////////////////////////////////////////////////
  149. // Seek & Flee
  150. //
  151. // These two operations form the root of all steering. They do simple
  152. // vector operations and add to the thrust vector.
  153. ////////////////////////////////////////////////////////////////////////////////////
  154. float Seek(gentity_t* actor, const CVec3& pos, float slowingDistance=0.0f, float weight=1.0f, float desiredSpeed=0.0f);
  155. float Flee(gentity_t* actor, const CVec3& pos, float weight=1.0f);
  156. ////////////////////////////////////////////////////////////////////////////////////
  157. // Persue & Evade
  158. //
  159. // Slightly more complicated than Seek & Flee, these operations predict the position
  160. // of the target entitiy.
  161. ////////////////////////////////////////////////////////////////////////////////////
  162. float Persue(gentity_t* actor, gentity_t* target, float slowingDistance);
  163. float Persue(gentity_t* actor, gentity_t* target, float slowingDistance, float offsetForward, float offsetRight=0.0f, float offsetUp=0.0f, bool relativeToTargetFacing=false);
  164. float Evade(gentity_t* actor, gentity_t* target);
  165. ////////////////////////////////////////////////////////////////////////////////////
  166. // Separation, Alignment, Cohesion
  167. //
  168. // These standard steering operations will apply thrust to achieve a group oriented
  169. // position or direction.
  170. ////////////////////////////////////////////////////////////////////////////////////
  171. float Separation(gentity_t* actor, float Scale=1.0f);
  172. float Alignment(gentity_t* actor, float Scale=1.0f);
  173. float Cohesion(gentity_t* actor, float Scale=1.0f);
  174. ////////////////////////////////////////////////////////////////////////////////////
  175. // Wander & Path
  176. //
  177. // By far the most common way to alter a character's thrust, path maintaines motion
  178. // along a navigational path (see NAV namespace), and a random wander path.
  179. ////////////////////////////////////////////////////////////////////////////////////
  180. float Path(gentity_t* actor);
  181. float Wander(gentity_t* actor);
  182. float FollowLeader(gentity_t* actor, gentity_t* leader, float dist);
  183. ////////////////////////////////////////////////////////////////////////////////////
  184. // Collision Avoidance
  185. //
  186. // Usually the last steering operation to call before finialization, this operation
  187. // attempts to avoid collisions with nearby entities and architecture by thrusing
  188. // away from them.
  189. ////////////////////////////////////////////////////////////////////////////////////
  190. float AvoidCollisions(gentity_t* actor, gentity_t* leader=0);
  191. gentity_t* SelectLeader(gentity_t* actor);
  192. ////////////////////////////////////////////////////////////////////////////////////
  193. // Blocked
  194. //
  195. // This function records whether AI is blocked while the steering is active
  196. ////////////////////////////////////////////////////////////////////////////////////
  197. void Blocked(gentity_t* actor, gentity_t* target);
  198. void Blocked(gentity_t* actor, const vec3_t& target);
  199. bool HasBeenBlockedFor(gentity_t* actor, int duration);
  200. ////////////////////////////////////////////////////////////////////////////////////
  201. // Reached
  202. //
  203. // A quick function to see if a target location has been reached by an actor
  204. ////////////////////////////////////////////////////////////////////////////////////
  205. bool Reached(gentity_t* actor, gentity_t* target, float targetRadius, bool flying=false);
  206. bool Reached(gentity_t* actor, NAV::TNodeHandle target, float targetRadius, bool flying=false);
  207. bool Reached(gentity_t* actor, const vec3_t& target, float targetRadius, bool flying=false);
  208. }
  209. #endif //__G_NAVIGATOR__