aiPlayer.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. F32 mAttackRadius; // Distance to trigger weaponry calcs
  46. Point3F mMoveDestination; // Destination for movement
  47. Point3F mLastLocation; // For stuck check
  48. F32 mMoveStuckTolerance; // Distance tolerance on stuck check
  49. S32 mMoveStuckTestDelay; // The number of ticks to wait before checking if the AI is stuck
  50. S32 mMoveStuckTestCountdown; // The current countdown until at AI starts to check if it is stuck
  51. bool mMoveSlowdown; // Slowdown as we near the destination
  52. SimObjectPtr<GameBase> mAimObject; // Object to point at, overrides location
  53. bool mAimLocationSet; // Has an aim location been set?
  54. Point3F mAimLocation; // Point to look at
  55. bool mTargetInLOS; // Is target object visible?
  56. Point3F mAimOffset;
  57. // move triggers
  58. bool mMoveTriggers[MaxTriggerKeys];
  59. // Utility Methods
  60. void throwCallback( const char *name );
  61. #ifdef TORQUE_NAVIGATION_ENABLED
  62. /// Should we jump?
  63. enum JumpStates {
  64. None, ///< No, don't jump.
  65. Now, ///< Jump immediately.
  66. Ledge, ///< Jump when we walk off a ledge.
  67. } mJump;
  68. /// Stores information about a path.
  69. struct PathData {
  70. /// Pointer to path object.
  71. SimObjectPtr<NavPath> path;
  72. /// Do we own our path? If so, we will delete it when finished.
  73. bool owned;
  74. /// Path node we're at.
  75. U32 index;
  76. /// Default constructor.
  77. PathData() : path(NULL)
  78. {
  79. owned = false;
  80. index = 0;
  81. }
  82. };
  83. /// Path we are currently following.
  84. PathData mPathData;
  85. /// Get the current path we're following.
  86. NavPath *getPath() { return mPathData.path; }
  87. /// Stores information about our cover.
  88. struct CoverData {
  89. /// Pointer to a cover point.
  90. SimObjectPtr<CoverPoint> cover;
  91. /// Default constructor.
  92. CoverData() : cover(NULL) {}
  93. };
  94. /// Current cover we're trying to get to.
  95. CoverData mCoverData;
  96. /// Information about a target we're following.
  97. struct FollowData {
  98. /// Object to follow.
  99. SimObjectPtr<SceneObject> object;
  100. /// Distance at whcih to follow.
  101. F32 radius;
  102. Point3F lastPos;
  103. /// Default constructor.
  104. FollowData() : object(NULL)
  105. {
  106. radius = 5.0f;
  107. lastPos = Point3F::Zero;
  108. }
  109. };
  110. /// Current object we're following.
  111. FollowData mFollowData;
  112. /// NavMesh we pathfind on.
  113. SimObjectPtr<NavMesh> mNavMesh;
  114. /// Move to the specified node in the current path.
  115. void moveToNode(S32 node);
  116. #endif // TORQUE_NAVIGATION_ENABLED
  117. protected:
  118. virtual void onReachDestination();
  119. virtual void onStuck();
  120. public:
  121. DECLARE_CONOBJECT( AIPlayer );
  122. AIPlayer();
  123. ~AIPlayer();
  124. static void initPersistFields();
  125. bool onAdd();
  126. void onRemove();
  127. virtual bool getAIMove( Move *move );
  128. virtual void updateMove(const Move *move);
  129. /// Clear out the current path.
  130. void clearPath();
  131. /// Stop searching for cover.
  132. void clearCover();
  133. /// Stop following me!
  134. void clearFollow();
  135. // Targeting and aiming sets/gets
  136. void setAimObject( GameBase *targetObject );
  137. void setAimObject(GameBase *targetObject, const Point3F& offset);
  138. GameBase* getAimObject() const { return mAimObject; }
  139. void setAimLocation( const Point3F &location );
  140. Point3F getAimLocation() const { return mAimLocation; }
  141. void clearAim();
  142. void getMuzzleVector(U32 imageSlot,VectorF* vec);
  143. bool checkInLos(GameBase* target = NULL, bool _useMuzzle = false, bool _checkEnabled = false);
  144. bool checkInFoV(GameBase* target = NULL, F32 camFov = 45.0f, bool _checkEnabled = false);
  145. F32 getTargetDistance(GameBase* target, bool _checkEnabled);
  146. // Movement sets/gets
  147. void setMoveSpeed( const F32 speed );
  148. F32 getMoveSpeed() const { return mMoveSpeed; }
  149. void setMoveTolerance( const F32 tolerance );
  150. F32 getMoveTolerance() const { return mMoveTolerance; }
  151. void setMoveDestination( const Point3F &location, bool slowdown );
  152. Point3F getMoveDestination() const { return mMoveDestination; }
  153. void stopMove();
  154. void setAiPose( S32 pose );
  155. S32 getAiPose();
  156. // Trigger sets/gets
  157. void setMoveTrigger( U32 slot, const bool isSet = true );
  158. bool getMoveTrigger( U32 slot ) const;
  159. void clearMoveTriggers();
  160. #ifdef TORQUE_NAVIGATION_ENABLED
  161. /// @name Pathfinding
  162. /// @{
  163. enum NavSize {
  164. Small,
  165. Regular,
  166. Large
  167. } mNavSize;
  168. void setNavSize(NavSize size) { mNavSize = size; updateNavMesh(); }
  169. NavSize getNavSize() const { return mNavSize; }
  170. bool setPathDestination(const Point3F &pos);
  171. Point3F getPathDestination() const;
  172. void followNavPath(NavPath *path);
  173. void followObject(SceneObject *obj, F32 radius);
  174. void repath();
  175. bool findCover(const Point3F &from, F32 radius);
  176. NavMesh *findNavMesh() const;
  177. void updateNavMesh();
  178. NavMesh *getNavMesh() const { return mNavMesh; }
  179. /// Get cover we are moving to.
  180. CoverPoint *getCover() { return mCoverData.cover; }
  181. /// Types of link we can use.
  182. LinkData mLinkTypes;
  183. /// @}
  184. #endif // TORQUE_NAVIGATION_ENABLED
  185. };
  186. #endif