aiPlayer.h

Engine/source/T3D/aiPlayer.h

More...

Classes:

Detailed Description

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