aiPlayer.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _AIPLAYER_H_
  23. #define _AIPLAYER_H_
  24. #ifndef _PLAYER_H_
  25. #include "T3D/player.h"
  26. #endif
  27. #ifdef TORQUE_NAVIGATION_ENABLED
  28. #include "navigation/navPath.h"
  29. #include "navigation/navMesh.h"
  30. #include "navigation/coverPoint.h"
  31. #endif // TORQUE_NAVIGATION_ENABLED
  32. class AIPlayer : public Player {
  33. typedef Player Parent;
  34. public:
  35. enum MoveState {
  36. ModeStop, // AI has stopped moving.
  37. ModeMove, // AI is currently moving.
  38. ModeStuck, // AI is stuck, but wants to move.
  39. ModeSlowing, // AI is slowing down as it reaches it's destination.
  40. };
  41. private:
  42. MoveState mMoveState;
  43. F32 mMoveSpeed;
  44. F32 mMoveTolerance; // Distance from destination before we stop
  45. Point3F mMoveDestination; // Destination for movement
  46. Point3F mLastLocation; // For stuck check
  47. F32 mMoveStuckTolerance; // Distance tolerance on stuck check
  48. S32 mMoveStuckTestDelay; // The number of ticks to wait before checking if the AI is stuck
  49. S32 mMoveStuckTestCountdown; // The current countdown until at AI starts to check if it is stuck
  50. bool mMoveSlowdown; // Slowdown as we near the destination
  51. SimObjectPtr<GameBase> mAimObject; // Object to point at, overrides location
  52. bool mAimLocationSet; // Has an aim location been set?
  53. Point3F mAimLocation; // Point to look at
  54. bool mTargetInLOS; // Is target object visible?
  55. Point3F mAimOffset;
  56. // move triggers
  57. bool mMoveTriggers[MaxTriggerKeys];
  58. // Utility Methods
  59. void throwCallback( const char *name );
  60. #ifdef TORQUE_NAVIGATION_ENABLED
  61. /// Should we jump?
  62. enum JumpStates {
  63. None, ///< No, don't jump.
  64. Now, ///< Jump immediately.
  65. Ledge, ///< Jump when we walk off a ledge.
  66. } mJump;
  67. /// Stores information about a path.
  68. struct PathData {
  69. /// Pointer to path object.
  70. SimObjectPtr<NavPath> path;
  71. /// Do we own our path? If so, we will delete it when finished.
  72. bool owned;
  73. /// Path node we're at.
  74. U32 index;
  75. /// Default constructor.
  76. PathData() : path(NULL)
  77. {
  78. owned = false;
  79. index = 0;
  80. }
  81. };
  82. /// Path we are currently following.
  83. PathData mPathData;
  84. /// Clear out the current path.
  85. void clearPath();
  86. /// Get the current path we're following.
  87. NavPath *getPath() { return mPathData.path; }
  88. /// Stores information about our cover.
  89. struct CoverData {
  90. /// Pointer to a cover point.
  91. SimObjectPtr<CoverPoint> cover;
  92. /// Default constructor.
  93. CoverData() : cover(NULL) {}
  94. };
  95. /// Current cover we're trying to get to.
  96. CoverData mCoverData;
  97. /// Stop searching for cover.
  98. void clearCover();
  99. /// Information about a target we're following.
  100. struct FollowData {
  101. /// Object to follow.
  102. SimObjectPtr<SceneObject> object;
  103. /// Distance at whcih to follow.
  104. F32 radius;
  105. Point3F lastPos;
  106. /// Default constructor.
  107. FollowData() : object(NULL)
  108. {
  109. radius = 5.0f;
  110. lastPos = Point3F::Zero;
  111. }
  112. };
  113. /// Current object we're following.
  114. FollowData mFollowData;
  115. /// Stop following me!
  116. void clearFollow();
  117. /// NavMesh we pathfind on.
  118. SimObjectPtr<NavMesh> mNavMesh;
  119. /// Move to the specified node in the current path.
  120. void moveToNode(S32 node);
  121. #endif // TORQUE_NAVIGATION_ENABLED
  122. protected:
  123. virtual void onReachDestination();
  124. virtual void onStuck();
  125. public:
  126. DECLARE_CONOBJECT( AIPlayer );
  127. AIPlayer();
  128. ~AIPlayer();
  129. static void initPersistFields();
  130. bool onAdd();
  131. void onRemove();
  132. virtual bool getAIMove( Move *move );
  133. // Targeting and aiming sets/gets
  134. void setAimObject( GameBase *targetObject );
  135. void setAimObject(GameBase *targetObject, const Point3F& offset);
  136. GameBase* getAimObject() const { return mAimObject; }
  137. void setAimLocation( const Point3F &location );
  138. Point3F getAimLocation() const { return mAimLocation; }
  139. void clearAim();
  140. bool checkInLos(GameBase* target = NULL, bool _useMuzzle = false, bool _checkEnabled = false);
  141. bool checkInFoV(GameBase* target = NULL, F32 camFov = 45.0f, bool _checkEnabled = false);
  142. // Movement sets/gets
  143. void setMoveSpeed( const F32 speed );
  144. F32 getMoveSpeed() const { return mMoveSpeed; }
  145. void setMoveTolerance( const F32 tolerance );
  146. F32 getMoveTolerance() const { return mMoveTolerance; }
  147. void setMoveDestination( const Point3F &location, bool slowdown );
  148. Point3F getMoveDestination() const { return mMoveDestination; }
  149. void stopMove();
  150. // Trigger sets/gets
  151. void setMoveTrigger( U32 slot, const bool isSet = true );
  152. bool getMoveTrigger( U32 slot ) const;
  153. void clearMoveTriggers();
  154. #ifdef TORQUE_NAVIGATION_ENABLED
  155. /// @name Pathfinding
  156. /// @{
  157. enum NavSize {
  158. Small,
  159. Regular,
  160. Large
  161. } mNavSize;
  162. void setNavSize(NavSize size) { mNavSize = size; updateNavMesh(); }
  163. NavSize getNavSize() const { return mNavSize; }
  164. bool setPathDestination(const Point3F &pos);
  165. Point3F getPathDestination() const;
  166. void followNavPath(NavPath *path);
  167. void followObject(SceneObject *obj, F32 radius);
  168. void repath();
  169. bool findCover(const Point3F &from, F32 radius);
  170. NavMesh *findNavMesh() const;
  171. void updateNavMesh();
  172. NavMesh *getNavMesh() const { return mNavMesh; }
  173. /// Get cover we are moving to.
  174. CoverPoint *getCover() { return mCoverData.cover; }
  175. /// Types of link we can use.
  176. LinkData mLinkTypes;
  177. /// @}
  178. #endif // TORQUE_NAVIGATION_ENABLED
  179. };
  180. #endif