aiPlayer.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #ifndef _AIPLAYER_H_
  27. #define _AIPLAYER_H_
  28. #ifndef _PLAYER_H_
  29. #include "T3D/player.h"
  30. #endif
  31. #ifdef TORQUE_NAVIGATION_ENABLED
  32. #include "navigation/navPath.h"
  33. #include "navigation/navMesh.h"
  34. #include "navigation/coverPoint.h"
  35. #endif // TORQUE_NAVIGATION_ENABLED
  36. class AIPlayer : public Player {
  37. typedef Player Parent;
  38. public:
  39. enum MoveState {
  40. ModeStop, // AI has stopped moving.
  41. ModeMove, // AI is currently moving.
  42. ModeStuck, // AI is stuck, but wants to move.
  43. ModeSlowing, // AI is slowing down as it reaches it's destination.
  44. };
  45. private:
  46. MoveState mMoveState;
  47. F32 mMoveSpeed;
  48. F32 mMoveTolerance; // Distance from destination before we stop
  49. F32 mAttackRadius; // Distance to trigger weaponry calcs
  50. Point3F mMoveDestination; // Destination for movement
  51. Point3F mLastLocation; // For stuck check
  52. F32 mMoveStuckTolerance; // Distance tolerance on stuck check
  53. S32 mMoveStuckTestDelay; // The number of ticks to wait before checking if the AI is stuck
  54. S32 mMoveStuckTestCountdown; // The current countdown until at AI starts to check if it is stuck
  55. bool mMoveSlowdown; // Slowdown as we near the destination
  56. SimObjectPtr<GameBase> mAimObject; // Object to point at, overrides location
  57. bool mAimLocationSet; // Has an aim location been set?
  58. Point3F mAimLocation; // Point to look at
  59. bool mTargetInLOS; // Is target object visible?
  60. Point3F mAimOffset;
  61. // move triggers
  62. bool mMoveTriggers[MaxTriggerKeys];
  63. // Utility Methods
  64. void throwCallback( const char *name );
  65. #ifdef TORQUE_NAVIGATION_ENABLED
  66. /// Should we jump?
  67. enum JumpStates {
  68. None, ///< No, don't jump.
  69. Now, ///< Jump immediately.
  70. Ledge, ///< Jump when we walk off a ledge.
  71. } mJump;
  72. /// Stores information about a path.
  73. struct PathData {
  74. /// Pointer to path object.
  75. SimObjectPtr<NavPath> path;
  76. /// Do we own our path? If so, we will delete it when finished.
  77. bool owned;
  78. /// Path node we're at.
  79. U32 index;
  80. /// Default constructor.
  81. PathData() : path(NULL)
  82. {
  83. owned = false;
  84. index = 0;
  85. }
  86. };
  87. /// Path we are currently following.
  88. PathData mPathData;
  89. /// Get the current path we're following.
  90. NavPath *getPath() { return mPathData.path; }
  91. /// Stores information about our cover.
  92. struct CoverData {
  93. /// Pointer to a cover point.
  94. SimObjectPtr<CoverPoint> cover;
  95. /// Default constructor.
  96. CoverData() : cover(NULL) {}
  97. };
  98. /// Current cover we're trying to get to.
  99. CoverData mCoverData;
  100. /// Information about a target we're following.
  101. struct FollowData {
  102. /// Object to follow.
  103. SimObjectPtr<SceneObject> object;
  104. /// Distance at whcih to follow.
  105. F32 radius;
  106. Point3F lastPos;
  107. /// Default constructor.
  108. FollowData() : object(NULL)
  109. {
  110. radius = 5.0f;
  111. lastPos = Point3F::Zero;
  112. }
  113. };
  114. /// Current object we're following.
  115. FollowData mFollowData;
  116. /// NavMesh we pathfind on.
  117. SimObjectPtr<NavMesh> mNavMesh;
  118. /// Move to the specified node in the current path.
  119. void moveToNode(S32 node);
  120. #endif // TORQUE_NAVIGATION_ENABLED
  121. protected:
  122. virtual void onReachDestination();
  123. virtual void onStuck();
  124. public:
  125. DECLARE_CONOBJECT( AIPlayer );
  126. AIPlayer();
  127. ~AIPlayer();
  128. static void initPersistFields();
  129. bool onAdd();
  130. void onRemove();
  131. virtual bool getAIMove( Move *move );
  132. virtual void updateMove(const Move *move);
  133. /// Clear out the current path.
  134. void clearPath();
  135. /// Stop searching for cover.
  136. void clearCover();
  137. /// Stop following me!
  138. void clearFollow();
  139. // Targeting and aiming sets/gets
  140. void setAimObject( GameBase *targetObject );
  141. void setAimObject(GameBase *targetObject, const Point3F& offset);
  142. GameBase* getAimObject() const { return mAimObject; }
  143. void setAimLocation( const Point3F &location );
  144. Point3F getAimLocation() const { return mAimLocation; }
  145. void clearAim();
  146. void getMuzzleVector(U32 imageSlot,VectorF* vec);
  147. bool checkInLos(GameBase* target = NULL, bool _useMuzzle = false, bool _checkEnabled = false);
  148. bool checkInFoV(GameBase* target = NULL, F32 camFov = 45.0f, bool _checkEnabled = false);
  149. F32 getTargetDistance(GameBase* target, bool _checkEnabled);
  150. // Movement sets/gets
  151. void setMoveSpeed( const F32 speed );
  152. F32 getMoveSpeed() const { return mMoveSpeed; }
  153. void setMoveTolerance( const F32 tolerance );
  154. F32 getMoveTolerance() const { return mMoveTolerance; }
  155. void setMoveDestination( const Point3F &location, bool slowdown );
  156. Point3F getMoveDestination() const { return mMoveDestination; }
  157. void stopMove();
  158. void setAiPose( S32 pose );
  159. S32 getAiPose();
  160. // Trigger sets/gets
  161. void setMoveTrigger( U32 slot, const bool isSet = true );
  162. bool getMoveTrigger( U32 slot ) const;
  163. void clearMoveTriggers();
  164. #ifdef TORQUE_NAVIGATION_ENABLED
  165. /// @name Pathfinding
  166. /// @{
  167. enum NavSize {
  168. Small,
  169. Regular,
  170. Large
  171. } mNavSize;
  172. void setNavSize(NavSize size) { mNavSize = size; updateNavMesh(); }
  173. NavSize getNavSize() const { return mNavSize; }
  174. bool setPathDestination(const Point3F &pos);
  175. Point3F getPathDestination() const;
  176. void followNavPath(NavPath *path);
  177. void followObject(SceneObject *obj, F32 radius);
  178. void repath();
  179. bool findCover(const Point3F &from, F32 radius);
  180. NavMesh *findNavMesh() const;
  181. void updateNavMesh();
  182. NavMesh *getNavMesh() const { return mNavMesh; }
  183. /// Get cover we are moving to.
  184. CoverPoint *getCover() { return mCoverData.cover; }
  185. /// Types of link we can use.
  186. LinkData mLinkTypes;
  187. /// @}
  188. #endif // TORQUE_NAVIGATION_ENABLED
  189. // New method, restartMove(), restores the AIPlayer to its normal move-state
  190. // following animation overrides from AFX. The tag argument is used to match
  191. // the latest override and prevents interruption of overlapping animation
  192. // overrides.
  193. // New method, saveMoveState(), stores the current movement state
  194. // so that it can be restored when restartMove() is called.
  195. // See related anim-clip changes in Player.[h,cc].
  196. private:
  197. S32 mMoveState_saved;
  198. public:
  199. void restartMove(U32 tag);
  200. void saveMoveState();
  201. };
  202. #endif