aiPlayer.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  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. #include "platform/platform.h"
  23. #include "T3D/aiPlayer.h"
  24. #include "console/consoleInternal.h"
  25. #include "math/mMatrix.h"
  26. #include "T3D/gameBase/moveManager.h"
  27. #include "console/engineAPI.h"
  28. #include <cfloat>
  29. static U32 sAIPlayerLoSMask = TerrainObjectType | StaticShapeObjectType | StaticObjectType;
  30. IMPLEMENT_CO_NETOBJECT_V1(AIPlayer);
  31. ConsoleDocClass( AIPlayer,
  32. "@brief A Player object not controlled by conventional input, but by an AI engine.\n\n"
  33. "The AIPlayer provides a Player object that may be controlled from script. You control "
  34. "where the player moves and how fast. You may also set where the AIPlayer is aiming at "
  35. "-- either a location or another game object.\n\n"
  36. "The AIPlayer class does not have a datablock of its own. It makes use of the PlayerData "
  37. "datablock to define how it looks, etc. As the AIPlayer is an extension of the Player class "
  38. "it can mount objects and fire weapons, or mount vehicles and drive them.\n\n"
  39. "While the PlayerData datablock is used, there are a number of additional callbacks that are "
  40. "implemented by AIPlayer on the datablock. These are listed here:\n\n"
  41. "void onReachDestination(AIPlayer obj) \n"
  42. "Called when the player has reached its set destination using the setMoveDestination() method. "
  43. "The actual point at which this callback is called is when the AIPlayer is within the mMoveTolerance "
  44. "of the defined destination.\n\n"
  45. "void onMoveStuck(AIPlayer obj) \n"
  46. "While in motion, if an AIPlayer has moved less than moveStuckTolerance within a single tick, this "
  47. "callback is called. From here you could choose an alternate destination to get the AIPlayer moving "
  48. "again.\n\n"
  49. "void onTargetEnterLOS(AIPlayer obj) \n"
  50. "When an object is being aimed at (following a call to setAimObject()) and the targeted object enters "
  51. "the AIPlayer's line of sight, this callback is called. The LOS test is a ray from the AIPlayer's eye "
  52. "position to the center of the target's bounding box. The LOS ray test only checks against interiors, "
  53. "statis shapes, and terrain.\n\n"
  54. "void onTargetExitLOS(AIPlayer obj) \n"
  55. "When an object is being aimed at (following a call to setAimObject()) and the targeted object leaves "
  56. "the AIPlayer's line of sight, this callback is called. The LOS test is a ray from the AIPlayer's eye "
  57. "position to the center of the target's bounding box. The LOS ray test only checks against interiors, "
  58. "statis shapes, and terrain.\n\n"
  59. "@tsexample\n"
  60. "// Create the demo player object\n"
  61. "%player = new AiPlayer()\n"
  62. "{\n"
  63. " dataBlock = DemoPlayer;\n"
  64. " path = \"\";\n"
  65. "};\n"
  66. "@endtsexample\n\n"
  67. "@see Player for a list of all inherited functions, variables, and base description\n"
  68. "@ingroup AI\n"
  69. "@ingroup gameObjects\n");
  70. /**
  71. * Constructor
  72. */
  73. AIPlayer::AIPlayer()
  74. {
  75. mMoveDestination.set( 0.0f, 0.0f, 0.0f );
  76. mMoveSpeed = 1.0f;
  77. mMoveTolerance = 0.25f;
  78. mMoveStuckTolerance = 0.01f;
  79. mMoveStuckTestDelay = 30;
  80. mMoveStuckTestCountdown = 0;
  81. mMoveSlowdown = true;
  82. mMoveState = ModeStop;
  83. mAimObject = 0;
  84. mAimLocationSet = false;
  85. mTargetInLOS = false;
  86. mAimOffset = Point3F(0.0f, 0.0f, 0.0f);
  87. #ifdef TORQUE_NAVIGATION_ENABLED
  88. mJump = None;
  89. mNavSize = Regular;
  90. mLinkTypes = LinkData(AllFlags);
  91. #endif
  92. mIsAiControlled = true;
  93. for( S32 i = 0; i < MaxTriggerKeys; i ++ )
  94. mMoveTriggers[ i ] = false;
  95. }
  96. /**
  97. * Destructor
  98. */
  99. AIPlayer::~AIPlayer()
  100. {
  101. }
  102. void AIPlayer::initPersistFields()
  103. {
  104. addGroup( "AI" );
  105. addField( "mMoveTolerance", TypeF32, Offset( mMoveTolerance, AIPlayer ),
  106. "@brief Distance from destination before stopping.\n\n"
  107. "When the AIPlayer is moving to a given destination it will move to within "
  108. "this distance of the destination and then stop. By providing this tolerance "
  109. "it helps the AIPlayer from never reaching its destination due to minor obstacles, "
  110. "rounding errors on its position calculation, etc. By default it is set to 0.25.\n");
  111. addField( "moveStuckTolerance", TypeF32, Offset( mMoveStuckTolerance, AIPlayer ),
  112. "@brief Distance tolerance on stuck check.\n\n"
  113. "When the AIPlayer is moving to a given destination, if it ever moves less than "
  114. "this tolerance during a single tick, the AIPlayer is considered stuck. At this point "
  115. "the onMoveStuck() callback is called on the datablock.\n");
  116. addField( "moveStuckTestDelay", TypeS32, Offset( mMoveStuckTestDelay, AIPlayer ),
  117. "@brief The number of ticks to wait before testing if the AIPlayer is stuck.\n\n"
  118. "When the AIPlayer is asked to move, this property is the number of ticks to wait "
  119. "before the AIPlayer starts to check if it is stuck. This delay allows the AIPlayer "
  120. "to accelerate to full speed without its initial slow start being considered as stuck.\n"
  121. "@note Set to zero to have the stuck test start immediately.\n");
  122. endGroup( "AI" );
  123. #ifdef TORQUE_NAVIGATION_ENABLED
  124. addGroup("Pathfinding");
  125. addField("allowWalk", TypeBool, Offset(mLinkTypes.walk, AIPlayer),
  126. "Allow the character to walk on dry land.");
  127. addField("allowJump", TypeBool, Offset(mLinkTypes.jump, AIPlayer),
  128. "Allow the character to use jump links.");
  129. addField("allowDrop", TypeBool, Offset(mLinkTypes.drop, AIPlayer),
  130. "Allow the character to use drop links.");
  131. addField("allowSwim", TypeBool, Offset(mLinkTypes.swim, AIPlayer),
  132. "Allow the character tomove in water.");
  133. addField("allowLedge", TypeBool, Offset(mLinkTypes.ledge, AIPlayer),
  134. "Allow the character to jump ledges.");
  135. addField("allowClimb", TypeBool, Offset(mLinkTypes.climb, AIPlayer),
  136. "Allow the character to use climb links.");
  137. addField("allowTeleport", TypeBool, Offset(mLinkTypes.teleport, AIPlayer),
  138. "Allow the character to use teleporters.");
  139. endGroup("Pathfinding");
  140. #endif // TORQUE_NAVIGATION_ENABLED
  141. Parent::initPersistFields();
  142. }
  143. bool AIPlayer::onAdd()
  144. {
  145. if (!Parent::onAdd())
  146. return false;
  147. // Use the eye as the current position (see getAIMove)
  148. MatrixF eye;
  149. getEyeTransform(&eye);
  150. mLastLocation = eye.getPosition();
  151. return true;
  152. }
  153. void AIPlayer::onRemove()
  154. {
  155. #ifdef TORQUE_NAVIGATION_ENABLED
  156. clearPath();
  157. clearCover();
  158. clearFollow();
  159. #endif
  160. Parent::onRemove();
  161. }
  162. /**
  163. * Sets the speed at which this AI moves
  164. *
  165. * @param speed Speed to move, default player was 10
  166. */
  167. void AIPlayer::setMoveSpeed( F32 speed )
  168. {
  169. mMoveSpeed = getMax(0.0f, getMin( 1.0f, speed ));
  170. }
  171. /**
  172. * Stops movement for this AI
  173. */
  174. void AIPlayer::stopMove()
  175. {
  176. mMoveState = ModeStop;
  177. #ifdef TORQUE_NAVIGATION_ENABLED
  178. clearPath();
  179. clearCover();
  180. clearFollow();
  181. #endif
  182. }
  183. /**
  184. * Sets how far away from the move location is considered
  185. * "on target"
  186. *
  187. * @param tolerance Movement tolerance for error
  188. */
  189. void AIPlayer::setMoveTolerance( const F32 tolerance )
  190. {
  191. mMoveTolerance = getMax( 0.1f, tolerance );
  192. }
  193. /**
  194. * Sets the location for the bot to run to
  195. *
  196. * @param location Point to run to
  197. */
  198. void AIPlayer::setMoveDestination( const Point3F &location, bool slowdown )
  199. {
  200. mMoveDestination = location;
  201. mMoveState = ModeMove;
  202. mMoveSlowdown = slowdown;
  203. mMoveStuckTestCountdown = mMoveStuckTestDelay;
  204. }
  205. /**
  206. * Sets the object the bot is targeting
  207. *
  208. * @param targetObject The object to target
  209. */
  210. void AIPlayer::setAimObject( GameBase *targetObject )
  211. {
  212. mAimObject = targetObject;
  213. mTargetInLOS = false;
  214. mAimOffset = Point3F(0.0f, 0.0f, 0.0f);
  215. }
  216. /**
  217. * Sets the object the bot is targeting and an offset to add to target location
  218. *
  219. * @param targetObject The object to target
  220. * @param offset The offest from the target location to aim at
  221. */
  222. void AIPlayer::setAimObject(GameBase *targetObject, const Point3F& offset)
  223. {
  224. mAimObject = targetObject;
  225. mTargetInLOS = false;
  226. mAimOffset = offset;
  227. }
  228. /**
  229. * Sets the location for the bot to aim at
  230. *
  231. * @param location Point to aim at
  232. */
  233. void AIPlayer::setAimLocation( const Point3F &location )
  234. {
  235. mAimObject = 0;
  236. mAimLocationSet = true;
  237. mAimLocation = location;
  238. mAimOffset = Point3F(0.0f, 0.0f, 0.0f);
  239. }
  240. /**
  241. * Clears the aim location and sets it to the bot's
  242. * current destination so he looks where he's going
  243. */
  244. void AIPlayer::clearAim()
  245. {
  246. mAimObject = 0;
  247. mAimLocationSet = false;
  248. mAimOffset = Point3F(0.0f, 0.0f, 0.0f);
  249. }
  250. /**
  251. * Set the state of a movement trigger.
  252. *
  253. * @param slot The trigger slot to set
  254. * @param isSet set/unset the trigger
  255. */
  256. void AIPlayer::setMoveTrigger( U32 slot, const bool isSet )
  257. {
  258. if(slot >= MaxTriggerKeys)
  259. {
  260. Con::errorf("Attempting to set an invalid trigger slot (%i)", slot);
  261. }
  262. else
  263. {
  264. mMoveTriggers[ slot ] = isSet; // set the trigger
  265. setMaskBits(NoWarpMask); // force the client to updateMove
  266. }
  267. }
  268. /**
  269. * Get the state of a movement trigger.
  270. *
  271. * @param slot The trigger slot to query
  272. * @return True if the trigger is set, false if it is not set
  273. */
  274. bool AIPlayer::getMoveTrigger( U32 slot ) const
  275. {
  276. if(slot >= MaxTriggerKeys)
  277. {
  278. Con::errorf("Attempting to get an invalid trigger slot (%i)", slot);
  279. return false;
  280. }
  281. else
  282. {
  283. return mMoveTriggers[ slot ];
  284. }
  285. }
  286. /**
  287. * Clear the trigger state for all movement triggers.
  288. */
  289. void AIPlayer::clearMoveTriggers()
  290. {
  291. for( U32 i = 0; i < MaxTriggerKeys; i ++ )
  292. setMoveTrigger( i, false );
  293. }
  294. /**
  295. * This method calculates the moves for the AI player
  296. *
  297. * @param movePtr Pointer to move the move list into
  298. */
  299. bool AIPlayer::getAIMove(Move *movePtr)
  300. {
  301. *movePtr = NullMove;
  302. // Use the eye as the current position.
  303. MatrixF eye;
  304. getEyeTransform(&eye);
  305. Point3F location = eye.getPosition();
  306. Point3F rotation = getRotation();
  307. #ifdef TORQUE_NAVIGATION_ENABLED
  308. if(mDamageState == Enabled)
  309. {
  310. if(mMoveState != ModeStop)
  311. updateNavMesh();
  312. if(!mFollowData.object.isNull())
  313. {
  314. if(mPathData.path.isNull())
  315. {
  316. if((getPosition() - mFollowData.object->getPosition()).len() > mFollowData.radius)
  317. followObject(mFollowData.object, mFollowData.radius);
  318. }
  319. else
  320. {
  321. if((mPathData.path->mTo - mFollowData.object->getPosition()).len() > mFollowData.radius)
  322. repath();
  323. else if((getPosition() - mFollowData.object->getPosition()).len() < mFollowData.radius)
  324. {
  325. clearPath();
  326. mMoveState = ModeStop;
  327. }
  328. }
  329. }
  330. }
  331. #endif // TORQUE_NAVIGATION_ENABLED
  332. // Orient towards the aim point, aim object, or towards
  333. // our destination.
  334. if (mAimObject || mAimLocationSet || mMoveState != ModeStop)
  335. {
  336. // Update the aim position if we're aiming for an object
  337. if (mAimObject)
  338. mAimLocation = mAimObject->getPosition() + mAimOffset;
  339. else
  340. if (!mAimLocationSet)
  341. mAimLocation = mMoveDestination;
  342. F32 xDiff = mAimLocation.x - location.x;
  343. F32 yDiff = mAimLocation.y - location.y;
  344. if (!mIsZero(xDiff) || !mIsZero(yDiff))
  345. {
  346. // First do Yaw
  347. // use the cur yaw between -Pi and Pi
  348. F32 curYaw = rotation.z;
  349. while (curYaw > M_2PI_F)
  350. curYaw -= M_2PI_F;
  351. while (curYaw < -M_2PI_F)
  352. curYaw += M_2PI_F;
  353. // find the yaw offset
  354. F32 newYaw = mAtan2( xDiff, yDiff );
  355. F32 yawDiff = newYaw - curYaw;
  356. // make it between 0 and 2PI
  357. if( yawDiff < 0.0f )
  358. yawDiff += M_2PI_F;
  359. else if( yawDiff >= M_2PI_F )
  360. yawDiff -= M_2PI_F;
  361. // now make sure we take the short way around the circle
  362. if( yawDiff > M_PI_F )
  363. yawDiff -= M_2PI_F;
  364. else if( yawDiff < -M_PI_F )
  365. yawDiff += M_2PI_F;
  366. movePtr->yaw = yawDiff;
  367. // Next do pitch.
  368. if (!mAimObject && !mAimLocationSet)
  369. {
  370. // Level out if were just looking at our next way point.
  371. Point3F headRotation = getHeadRotation();
  372. movePtr->pitch = -headRotation.x;
  373. }
  374. else
  375. {
  376. // This should be adjusted to run from the
  377. // eye point to the object's center position. Though this
  378. // works well enough for now.
  379. F32 vertDist = mAimLocation.z - location.z;
  380. F32 horzDist = mSqrt(xDiff * xDiff + yDiff * yDiff);
  381. F32 newPitch = mAtan2( horzDist, vertDist ) - ( M_PI_F / 2.0f );
  382. if (mFabs(newPitch) > 0.01f)
  383. {
  384. Point3F headRotation = getHeadRotation();
  385. movePtr->pitch = newPitch - headRotation.x;
  386. }
  387. }
  388. }
  389. }
  390. else
  391. {
  392. // Level out if we're not doing anything else
  393. Point3F headRotation = getHeadRotation();
  394. movePtr->pitch = -headRotation.x;
  395. }
  396. // Move towards the destination
  397. if (mMoveState != ModeStop)
  398. {
  399. F32 xDiff = mMoveDestination.x - location.x;
  400. F32 yDiff = mMoveDestination.y - location.y;
  401. // Check if we should mMove, or if we are 'close enough'
  402. if (mFabs(xDiff) < mMoveTolerance && mFabs(yDiff) < mMoveTolerance)
  403. {
  404. mMoveState = ModeStop;
  405. onReachDestination();
  406. }
  407. else
  408. {
  409. // Build move direction in world space
  410. if (mIsZero(xDiff))
  411. movePtr->y = (location.y > mMoveDestination.y) ? -1.0f : 1.0f;
  412. else
  413. if (mIsZero(yDiff))
  414. movePtr->x = (location.x > mMoveDestination.x) ? -1.0f : 1.0f;
  415. else
  416. if (mFabs(xDiff) > mFabs(yDiff))
  417. {
  418. F32 value = mFabs(yDiff / xDiff);
  419. movePtr->y = (location.y > mMoveDestination.y) ? -value : value;
  420. movePtr->x = (location.x > mMoveDestination.x) ? -1.0f : 1.0f;
  421. }
  422. else
  423. {
  424. F32 value = mFabs(xDiff / yDiff);
  425. movePtr->x = (location.x > mMoveDestination.x) ? -value : value;
  426. movePtr->y = (location.y > mMoveDestination.y) ? -1.0f : 1.0f;
  427. }
  428. // Rotate the move into object space (this really only needs
  429. // a 2D matrix)
  430. Point3F newMove;
  431. MatrixF moveMatrix;
  432. moveMatrix.set(EulerF(0.0f, 0.0f, -(rotation.z + movePtr->yaw)));
  433. moveMatrix.mulV( Point3F( movePtr->x, movePtr->y, 0.0f ), &newMove );
  434. movePtr->x = newMove.x;
  435. movePtr->y = newMove.y;
  436. // Set movement speed. We'll slow down once we get close
  437. // to try and stop on the spot...
  438. if (mMoveSlowdown)
  439. {
  440. F32 speed = mMoveSpeed;
  441. F32 dist = mSqrt(xDiff*xDiff + yDiff*yDiff);
  442. F32 maxDist = 5.0f;
  443. if (dist < maxDist)
  444. speed *= dist / maxDist;
  445. movePtr->x *= speed;
  446. movePtr->y *= speed;
  447. mMoveState = ModeSlowing;
  448. }
  449. else
  450. {
  451. movePtr->x *= mMoveSpeed;
  452. movePtr->y *= mMoveSpeed;
  453. mMoveState = ModeMove;
  454. }
  455. if (mMoveStuckTestCountdown > 0)
  456. --mMoveStuckTestCountdown;
  457. else
  458. {
  459. // We should check to see if we are stuck...
  460. F32 locationDelta = (location - mLastLocation).len();
  461. if (locationDelta < mMoveStuckTolerance && mDamageState == Enabled)
  462. {
  463. // If we are slowing down, then it's likely that our location delta will be less than
  464. // our move stuck tolerance. Because we can be both slowing and stuck
  465. // we should TRY to check if we've moved. This could use better detection.
  466. if ( mMoveState != ModeSlowing || locationDelta == 0 )
  467. {
  468. mMoveState = ModeStuck;
  469. onStuck();
  470. }
  471. }
  472. }
  473. }
  474. }
  475. // Test for target location in sight if it's an object. The LOS is
  476. // run from the eye position to the center of the object's bounding,
  477. // which is not very accurate.
  478. if (mAimObject) {
  479. MatrixF eyeMat;
  480. getEyeTransform(&eyeMat);
  481. eyeMat.getColumn(3,&location);
  482. Point3F targetLoc = mAimObject->getBoxCenter();
  483. // This ray ignores non-static shapes. Cast Ray returns true
  484. // if it hit something.
  485. RayInfo dummy;
  486. if (getContainer()->castRay( location, targetLoc,
  487. StaticShapeObjectType | StaticObjectType |
  488. TerrainObjectType, &dummy)) {
  489. if (mTargetInLOS) {
  490. throwCallback( "onTargetExitLOS" );
  491. mTargetInLOS = false;
  492. }
  493. }
  494. else
  495. if (!mTargetInLOS) {
  496. throwCallback( "onTargetEnterLOS" );
  497. mTargetInLOS = true;
  498. }
  499. }
  500. // Replicate the trigger state into the move so that
  501. // triggers can be controlled from scripts.
  502. for( U32 i = 0; i < MaxTriggerKeys; i++ )
  503. movePtr->trigger[ i ] = mMoveTriggers[ i ];
  504. #ifdef TORQUE_NAVIGATION_ENABLED
  505. if(mJump == Now)
  506. {
  507. movePtr->trigger[2] = true;
  508. mJump = None;
  509. }
  510. else if(mJump == Ledge)
  511. {
  512. // If we're not touching the ground, jump!
  513. RayInfo info;
  514. if(!getContainer()->castRay(getPosition(), getPosition() - Point3F(0, 0, 0.4f), StaticShapeObjectType, &info))
  515. {
  516. movePtr->trigger[2] = true;
  517. mJump = None;
  518. }
  519. }
  520. #endif // TORQUE_NAVIGATION_ENABLED
  521. mLastLocation = location;
  522. return true;
  523. }
  524. /**
  525. * Utility function to throw callbacks. Callbacks always occure
  526. * on the datablock class.
  527. *
  528. * @param name Name of script function to call
  529. */
  530. void AIPlayer::throwCallback( const char *name )
  531. {
  532. Con::executef(getDataBlock(), name, getIdString());
  533. }
  534. /**
  535. * Called when we get within mMoveTolerance of our destination set using
  536. * setMoveDestination(). Only fires the script callback if we are at the end
  537. * of a pathfinding path, or have no pathfinding path.
  538. */
  539. void AIPlayer::onReachDestination()
  540. {
  541. #ifdef TORQUE_NAVIGATION_ENABLED
  542. if(!mPathData.path.isNull())
  543. {
  544. if(mPathData.index == mPathData.path->size() - 1)
  545. {
  546. // Handle looping paths.
  547. if(mPathData.path->mIsLooping)
  548. moveToNode(0);
  549. // Otherwise end path.
  550. else
  551. {
  552. clearPath();
  553. throwCallback("onReachDestination");
  554. }
  555. }
  556. else
  557. {
  558. moveToNode(mPathData.index + 1);
  559. // Throw callback every time if we're on a looping path.
  560. //if(mPathData.path->mIsLooping)
  561. //throwCallback("onReachDestination");
  562. }
  563. }
  564. else
  565. #endif
  566. throwCallback("onReachDestination");
  567. }
  568. /**
  569. * Called when we move less than mMoveStuckTolerance in a tick, signalling
  570. * that some obstacle is preventing us from getting where we need to go.
  571. */
  572. void AIPlayer::onStuck()
  573. {
  574. #ifdef TORQUE_NAVIGATION_ENABLED
  575. if(!mPathData.path.isNull())
  576. repath();
  577. else
  578. #endif
  579. throwCallback("onMoveStuck");
  580. }
  581. #ifdef TORQUE_NAVIGATION_ENABLED
  582. // --------------------------------------------------------------------------------------------
  583. // Pathfinding
  584. // --------------------------------------------------------------------------------------------
  585. void AIPlayer::clearPath()
  586. {
  587. // Only delete if we own the path.
  588. if(!mPathData.path.isNull() && mPathData.owned)
  589. mPathData.path->deleteObject();
  590. // Reset path data.
  591. mPathData = PathData();
  592. }
  593. void AIPlayer::clearCover()
  594. {
  595. // Notify cover that we are no longer on our way.
  596. if(!mCoverData.cover.isNull())
  597. mCoverData.cover->setOccupied(false);
  598. mCoverData = CoverData();
  599. }
  600. void AIPlayer::clearFollow()
  601. {
  602. mFollowData = FollowData();
  603. }
  604. void AIPlayer::moveToNode(S32 node)
  605. {
  606. if(mPathData.path.isNull())
  607. return;
  608. // -1 is shorthand for 'last path node'.
  609. if(node == -1)
  610. node = mPathData.path->size() - 1;
  611. // Consider slowing down on the last path node.
  612. setMoveDestination(mPathData.path->getNode(node), false);
  613. // Check flags for this segment.
  614. if(mPathData.index)
  615. {
  616. U16 flags = mPathData.path->getFlags(node - 1);
  617. // Jump if we must.
  618. if(flags & LedgeFlag)
  619. mJump = Ledge;
  620. else if(flags & JumpFlag)
  621. mJump = Now;
  622. else
  623. // Catch pathing errors.
  624. mJump = None;
  625. }
  626. // Store current index.
  627. mPathData.index = node;
  628. }
  629. bool AIPlayer::setPathDestination(const Point3F &pos)
  630. {
  631. // Pathfinding only happens on the server.
  632. if(!isServerObject())
  633. return false;
  634. if(!getNavMesh())
  635. updateNavMesh();
  636. // If we can't find a mesh, just move regularly.
  637. if(!getNavMesh())
  638. {
  639. //setMoveDestination(pos);
  640. return false;
  641. }
  642. // Create a new path.
  643. NavPath *path = new NavPath();
  644. path->mMesh = getNavMesh();
  645. path->mFrom = getPosition();
  646. path->mTo = pos;
  647. path->mFromSet = path->mToSet = true;
  648. path->mAlwaysRender = true;
  649. path->mLinkTypes = mLinkTypes;
  650. path->mXray = true;
  651. // Paths plan automatically upon being registered.
  652. if(!path->registerObject())
  653. {
  654. delete path;
  655. return false;
  656. }
  657. if(path->success())
  658. {
  659. // Clear any current path we might have.
  660. clearPath();
  661. clearCover();
  662. clearFollow();
  663. // Store new path.
  664. mPathData.path = path;
  665. mPathData.owned = true;
  666. // Skip node 0, which we are currently standing on.
  667. moveToNode(1);
  668. return true;
  669. }
  670. else
  671. {
  672. // Just move normally if we can't path.
  673. //setMoveDestination(pos, true);
  674. //return;
  675. //throwCallback("onPathFailed");
  676. path->deleteObject();
  677. return false;
  678. }
  679. }
  680. DefineEngineMethod(AIPlayer, setPathDestination, bool, (Point3F goal),,
  681. "@brief Tells the AI to find a path to the location provided\n\n"
  682. "@param goal Coordinates in world space representing location to move to.\n"
  683. "@return True if a path was found.\n\n"
  684. "@see getPathDestination()\n"
  685. "@see setMoveDestination()\n")
  686. {
  687. return object->setPathDestination(goal);
  688. }
  689. Point3F AIPlayer::getPathDestination() const
  690. {
  691. if(!mPathData.path.isNull())
  692. return mPathData.path->mTo;
  693. return Point3F(0, 0, 0);
  694. }
  695. DefineEngineMethod(AIPlayer, getPathDestination, Point3F, (),,
  696. "@brief Get the AIPlayer's current pathfinding destination.\n\n"
  697. "@return Returns a point containing the \"x y z\" position "
  698. "of the AIPlayer's current path destination. If no path destination "
  699. "has yet been set, this returns \"0 0 0\"."
  700. "@see setPathDestination()\n")
  701. {
  702. return object->getPathDestination();
  703. }
  704. void AIPlayer::followNavPath(NavPath *path)
  705. {
  706. if(!isServerObject())
  707. return;
  708. // Get rid of our current path.
  709. clearPath();
  710. clearCover();
  711. clearFollow();
  712. // Follow new path.
  713. mPathData.path = path;
  714. mPathData.owned = false;
  715. // Start from 0 since we might not already be there.
  716. moveToNode(0);
  717. }
  718. DefineEngineMethod(AIPlayer, followNavPath, void, (SimObjectId obj),,
  719. "@brief Tell the AIPlayer to follow a path.\n\n"
  720. "@param obj ID of a NavPath object for the character to follow.")
  721. {
  722. NavPath *path;
  723. if(Sim::findObject(obj, path))
  724. object->followNavPath(path);
  725. }
  726. void AIPlayer::followObject(SceneObject *obj, F32 radius)
  727. {
  728. if(!isServerObject())
  729. return;
  730. if ((mFollowData.lastPos - obj->getPosition()).len()<mMoveTolerance)
  731. return;
  732. if(setPathDestination(obj->getPosition()))
  733. {
  734. clearCover();
  735. mFollowData.object = obj;
  736. mFollowData.radius = radius;
  737. mFollowData.lastPos = obj->getPosition();
  738. }
  739. }
  740. DefineEngineMethod(AIPlayer, followObject, void, (SimObjectId obj, F32 radius),,
  741. "@brief Tell the AIPlayer to follow another object.\n\n"
  742. "@param obj ID of the object to follow.\n"
  743. "@param radius Maximum distance we let the target escape to.")
  744. {
  745. SceneObject *follow;
  746. if(Sim::findObject(obj, follow))
  747. object->followObject(follow, radius);
  748. }
  749. void AIPlayer::repath()
  750. {
  751. // Ineffectual if we don't have a path, or are using someone else's.
  752. if(mPathData.path.isNull() || !mPathData.owned)
  753. return;
  754. // If we're following, get their position.
  755. if(!mFollowData.object.isNull())
  756. mPathData.path->mTo = mFollowData.object->getPosition();
  757. // Update from position and replan.
  758. mPathData.path->mFrom = getPosition();
  759. mPathData.path->plan();
  760. // Move to first node (skip start pos).
  761. moveToNode(1);
  762. }
  763. DefineEngineMethod(AIPlayer, repath, void, (),,
  764. "@brief Tells the AI to re-plan its path. Does nothing if the character "
  765. "has no path, or if it is following a mission path.\n\n")
  766. {
  767. object->repath();
  768. }
  769. struct CoverSearch
  770. {
  771. Point3F loc;
  772. Point3F from;
  773. F32 dist;
  774. F32 best;
  775. CoverPoint *point;
  776. CoverSearch() : loc(0, 0, 0), from(0, 0, 0)
  777. {
  778. best = -FLT_MAX;
  779. point = NULL;
  780. dist = FLT_MAX;
  781. }
  782. };
  783. static void findCoverCallback(SceneObject *obj, void *key)
  784. {
  785. CoverPoint *p = dynamic_cast<CoverPoint*>(obj);
  786. if(!p || p->isOccupied())
  787. return;
  788. CoverSearch *s = static_cast<CoverSearch*>(key);
  789. Point3F dir = s->from - p->getPosition();
  790. dir.normalizeSafe();
  791. // Score first based on angle of cover point to enemy.
  792. F32 score = mDot(p->getNormal(), dir);
  793. // Score also based on distance from seeker.
  794. score -= (p->getPosition() - s->loc).len() / s->dist;
  795. // Finally, consider cover size.
  796. score += (p->getSize() + 1) / CoverPoint::NumSizes;
  797. score *= p->getQuality();
  798. if(score > s->best)
  799. {
  800. s->best = score;
  801. s->point = p;
  802. }
  803. }
  804. bool AIPlayer::findCover(const Point3F &from, F32 radius)
  805. {
  806. if(radius <= 0)
  807. return false;
  808. // Create a search state.
  809. CoverSearch s;
  810. s.loc = getPosition();
  811. s.dist = radius;
  812. // Direction we seek cover FROM.
  813. s.from = from;
  814. // Find cover points.
  815. Box3F box(radius * 2.0f);
  816. box.setCenter(getPosition());
  817. getContainer()->findObjects(box, MarkerObjectType, findCoverCallback, &s);
  818. // Go to cover!
  819. if(s.point)
  820. {
  821. // Calling setPathDestination clears cover...
  822. bool foundPath = setPathDestination(s.point->getPosition());
  823. // Now store the cover info.
  824. mCoverData.cover = s.point;
  825. s.point->setOccupied(true);
  826. return foundPath;
  827. }
  828. return false;
  829. }
  830. DefineEngineMethod(AIPlayer, findCover, S32, (Point3F from, F32 radius),,
  831. "@brief Tells the AI to find cover nearby.\n\n"
  832. "@param from Location to find cover from (i.e., enemy position).\n"
  833. "@param radius Distance to search for cover.\n"
  834. "@return Cover point ID if cover was found, -1 otherwise.\n\n")
  835. {
  836. if(object->findCover(from, radius))
  837. {
  838. CoverPoint* cover = object->getCover();
  839. return cover ? cover->getId() : -1;
  840. }
  841. else
  842. {
  843. return -1;
  844. }
  845. }
  846. NavMesh *AIPlayer::findNavMesh() const
  847. {
  848. // Search for NavMeshes that contain us entirely with the smallest possible
  849. // volume.
  850. NavMesh *mesh = NULL;
  851. SimSet *set = NavMesh::getServerSet();
  852. for(U32 i = 0; i < set->size(); i++)
  853. {
  854. NavMesh *m = static_cast<NavMesh*>(set->at(i));
  855. if(m->getWorldBox().isContained(getWorldBox()))
  856. {
  857. // Check that mesh size is appropriate.
  858. if(mMount.object) // Should use isMounted() but it's not const. Grr.
  859. {
  860. if(!m->mVehicles)
  861. continue;
  862. }
  863. else
  864. {
  865. if(getNavSize() == Small && !m->mSmallCharacters ||
  866. getNavSize() == Regular && !m->mRegularCharacters ||
  867. getNavSize() == Large && !m->mLargeCharacters)
  868. continue;
  869. }
  870. if(!mesh || m->getWorldBox().getVolume() < mesh->getWorldBox().getVolume())
  871. mesh = m;
  872. }
  873. }
  874. return mesh;
  875. }
  876. DefineEngineMethod(AIPlayer, findNavMesh, S32, (),,
  877. "@brief Get the NavMesh object this AIPlayer is currently using.\n\n"
  878. "@return The ID of the NavPath object this character is using for "
  879. "pathfinding. This is determined by the character's location, "
  880. "navigation type and other factors. Returns -1 if no NavMesh is "
  881. "found.")
  882. {
  883. NavMesh *mesh = object->getNavMesh();
  884. return mesh ? mesh->getId() : -1;
  885. }
  886. void AIPlayer::updateNavMesh()
  887. {
  888. NavMesh *old = mNavMesh;
  889. if(mNavMesh.isNull())
  890. mNavMesh = findNavMesh();
  891. else
  892. {
  893. if(!mNavMesh->getWorldBox().isContained(getWorldBox()))
  894. mNavMesh = findNavMesh();
  895. }
  896. // See if we need to update our path.
  897. if(mNavMesh != old && !mPathData.path.isNull())
  898. {
  899. setPathDestination(mPathData.path->mTo);
  900. }
  901. }
  902. DefineEngineMethod(AIPlayer, getNavMesh, S32, (),,
  903. "@brief Return the NavMesh this AIPlayer is using to navigate.\n\n")
  904. {
  905. NavMesh *m = object->getNavMesh();
  906. return m ? m->getId() : 0;
  907. }
  908. DefineEngineMethod(AIPlayer, setNavSize, void, (const char *size),,
  909. "@brief Set the size of NavMesh this character uses. One of \"Small\", \"Regular\" or \"Large\".")
  910. {
  911. if(!dStrcmp(size, "Small"))
  912. object->setNavSize(AIPlayer::Small);
  913. else if(!dStrcmp(size, "Regular"))
  914. object->setNavSize(AIPlayer::Regular);
  915. else if(!dStrcmp(size, "Large"))
  916. object->setNavSize(AIPlayer::Large);
  917. else
  918. Con::errorf("AIPlayer::setNavSize: no such size '%s'.", size);
  919. }
  920. DefineEngineMethod(AIPlayer, getNavSize, const char*, (),,
  921. "@brief Return the size of NavMesh this character uses for pathfinding.")
  922. {
  923. switch(object->getNavSize())
  924. {
  925. case AIPlayer::Small:
  926. return "Small";
  927. case AIPlayer::Regular:
  928. return "Regular";
  929. case AIPlayer::Large:
  930. return "Large";
  931. }
  932. return "";
  933. }
  934. #endif // TORQUE_NAVIGATION_ENABLED
  935. // --------------------------------------------------------------------------------------------
  936. // Console Functions
  937. // --------------------------------------------------------------------------------------------
  938. DefineEngineMethod( AIPlayer, stop, void, ( ),,
  939. "@brief Tells the AIPlayer to stop moving.\n\n")
  940. {
  941. object->stopMove();
  942. }
  943. DefineEngineMethod( AIPlayer, clearAim, void, ( ),,
  944. "@brief Use this to stop aiming at an object or a point.\n\n"
  945. "@see setAimLocation()\n"
  946. "@see setAimObject()\n")
  947. {
  948. object->clearAim();
  949. }
  950. DefineEngineMethod( AIPlayer, setMoveSpeed, void, ( F32 speed ),,
  951. "@brief Sets the move speed for an AI object.\n\n"
  952. "@param speed A speed multiplier between 0.0 and 1.0. "
  953. "This is multiplied by the AIPlayer's base movement rates (as defined in "
  954. "its PlayerData datablock)\n\n"
  955. "@see getMoveDestination()\n")
  956. {
  957. object->setMoveSpeed(speed);
  958. }
  959. DefineEngineMethod( AIPlayer, getMoveSpeed, F32, ( ),,
  960. "@brief Gets the move speed of an AI object.\n\n"
  961. "@return A speed multiplier between 0.0 and 1.0.\n\n"
  962. "@see setMoveSpeed()\n")
  963. {
  964. return object->getMoveSpeed();
  965. }
  966. DefineEngineMethod( AIPlayer, setMoveDestination, void, ( Point3F goal, bool slowDown ), ( true ),
  967. "@brief Tells the AI to move to the location provided\n\n"
  968. "@param goal Coordinates in world space representing location to move to.\n"
  969. "@param slowDown A boolean value. If set to true, the bot will slow down "
  970. "when it gets within 5-meters of its move destination. If false, the bot "
  971. "will stop abruptly when it reaches the move destination. By default, this is true.\n\n"
  972. "@note Upon reaching a move destination, the bot will clear its move destination and "
  973. "calls to getMoveDestination will return \"0 0 0\"."
  974. "@see getMoveDestination()\n")
  975. {
  976. object->setMoveDestination( goal, slowDown);
  977. }
  978. DefineEngineMethod( AIPlayer, getMoveDestination, Point3F, (),,
  979. "@brief Get the AIPlayer's current destination.\n\n"
  980. "@return Returns a point containing the \"x y z\" position "
  981. "of the AIPlayer's current move destination. If no move destination "
  982. "has yet been set, this returns \"0 0 0\"."
  983. "@see setMoveDestination()\n")
  984. {
  985. return object->getMoveDestination();
  986. }
  987. DefineEngineMethod( AIPlayer, setAimLocation, void, ( Point3F target ),,
  988. "@brief Tells the AIPlayer to aim at the location provided.\n\n"
  989. "@param target An \"x y z\" position in the game world to target.\n\n"
  990. "@see getAimLocation()\n")
  991. {
  992. object->setAimLocation(target);
  993. }
  994. DefineEngineMethod( AIPlayer, getAimLocation, Point3F, (),,
  995. "@brief Returns the point the AIPlayer is aiming at.\n\n"
  996. "This will reflect the position set by setAimLocation(), "
  997. "or the position of the object that the bot is now aiming at. "
  998. "If the bot is not aiming at anything, this value will "
  999. "change to whatever point the bot's current line-of-sight intercepts."
  1000. "@return World space coordinates of the object AI is aiming at. Formatted as \"X Y Z\".\n\n"
  1001. "@see setAimLocation()\n"
  1002. "@see setAimObject()\n")
  1003. {
  1004. return object->getAimLocation();
  1005. }
  1006. ConsoleDocFragment _setAimObject(
  1007. "@brief Sets the AIPlayer's target object. May optionally set an offset from target location\n\n"
  1008. "@param targetObject The object to target\n"
  1009. "@param offset Optional three-element offset vector which will be added to the position of the aim object.\n\n"
  1010. "@tsexample\n"
  1011. "// Without an offset\n"
  1012. "%ai.setAimObject(%target);\n\n"
  1013. "// With an offset\n"
  1014. "// Cause our AI object to aim at the target\n"
  1015. "// offset (0, 0, 1) so you don't aim at the target's feet\n"
  1016. "%ai.setAimObject(%target, \"0 0 1\");\n"
  1017. "@endtsexample\n\n"
  1018. "@see getAimLocation()\n"
  1019. "@see getAimObject()\n"
  1020. "@see clearAim()\n",
  1021. "AIPlayer",
  1022. "void setAimObject(GameBase targetObject, Point3F offset);"
  1023. );
  1024. DefineConsoleMethod( AIPlayer, setAimObject, void, ( const char * objName, Point3F offset ), (Point3F::Zero), "( GameBase obj, [Point3F offset] )"
  1025. "Sets the bot's target object. Optionally set an offset from target location."
  1026. "@hide")
  1027. {
  1028. // Find the target
  1029. GameBase *targetObject;
  1030. if( Sim::findObject( objName, targetObject ) )
  1031. {
  1032. object->setAimObject( targetObject, offset );
  1033. }
  1034. else
  1035. object->setAimObject( 0, offset );
  1036. }
  1037. DefineEngineMethod( AIPlayer, getAimObject, S32, (),,
  1038. "@brief Gets the object the AIPlayer is targeting.\n\n"
  1039. "@return Returns -1 if no object is being aimed at, "
  1040. "or the SimObjectID of the object the AIPlayer is aiming at.\n\n"
  1041. "@see setAimObject()\n")
  1042. {
  1043. GameBase* obj = object->getAimObject();
  1044. return obj? obj->getId(): -1;
  1045. }
  1046. bool AIPlayer::checkInLos(GameBase* target, bool _useMuzzle, bool _checkEnabled)
  1047. {
  1048. if (!isServerObject()) return false;
  1049. if (!target)
  1050. {
  1051. target = mAimObject.getPointer();
  1052. if (!target)
  1053. return false;
  1054. }
  1055. if (_checkEnabled)
  1056. {
  1057. if (target->getTypeMask() & ShapeBaseObjectType)
  1058. {
  1059. ShapeBase *shapeBaseCheck = static_cast<ShapeBase *>(target);
  1060. if (shapeBaseCheck)
  1061. if (shapeBaseCheck->getDamageState() != Enabled) return false;
  1062. }
  1063. else
  1064. return false;
  1065. }
  1066. RayInfo ri;
  1067. disableCollision();
  1068. S32 mountCount = target->getMountedObjectCount();
  1069. for (S32 i = 0; i < mountCount; i++)
  1070. {
  1071. target->getMountedObject(i)->disableCollision();
  1072. }
  1073. Point3F checkPoint ;
  1074. if (_useMuzzle)
  1075. getMuzzlePointAI(0, &checkPoint );
  1076. else
  1077. {
  1078. MatrixF eyeMat;
  1079. getEyeTransform(&eyeMat);
  1080. eyeMat.getColumn(3, &checkPoint );
  1081. }
  1082. bool hit = !gServerContainer.castRay(checkPoint, target->getBoxCenter(), sAIPlayerLoSMask, &ri);
  1083. enableCollision();
  1084. for (S32 i = 0; i < mountCount; i++)
  1085. {
  1086. target->getMountedObject(i)->enableCollision();
  1087. }
  1088. return hit;
  1089. }
  1090. DefineEngineMethod(AIPlayer, checkInLos, bool, (ShapeBase* obj, bool useMuzzle, bool checkEnabled),(NULL, false, false),
  1091. "@brief Check whether an object is in line of sight.\n"
  1092. "@obj Object to check. (If blank, it will check the current target).\n"
  1093. "@useMuzzle Use muzzle position. Otherwise use eye position. (defaults to false).\n"
  1094. "@checkEnabled check whether the object can take damage and if so is still alive.(Defaults to false)\n")
  1095. {
  1096. return object->checkInLos(obj, useMuzzle, checkEnabled);
  1097. }
  1098. bool AIPlayer::checkInFoV(GameBase* target, F32 camFov, bool _checkEnabled)
  1099. {
  1100. if (!isServerObject()) return false;
  1101. if (!target)
  1102. {
  1103. target = mAimObject.getPointer();
  1104. if (!target)
  1105. return false;
  1106. }
  1107. if (_checkEnabled)
  1108. {
  1109. if (target->getTypeMask() & ShapeBaseObjectType)
  1110. {
  1111. ShapeBase *shapeBaseCheck = static_cast<ShapeBase *>(target);
  1112. if (shapeBaseCheck)
  1113. if (shapeBaseCheck->getDamageState() != Enabled) return false;
  1114. }
  1115. else
  1116. return false;
  1117. }
  1118. MatrixF cam = getTransform();
  1119. Point3F camPos;
  1120. VectorF camDir;
  1121. cam.getColumn(3, &camPos);
  1122. cam.getColumn(1, &camDir);
  1123. camFov = mDegToRad(camFov) / 2;
  1124. Point3F shapePos = target->getBoxCenter();
  1125. VectorF shapeDir = shapePos - camPos;
  1126. // Test to see if it's within our viewcone, this test doesn't
  1127. // actually match the viewport very well, should consider
  1128. // projection and box test.
  1129. shapeDir.normalize();
  1130. F32 dot = mDot(shapeDir, camDir);
  1131. return (dot > mCos(camFov));
  1132. }
  1133. DefineEngineMethod(AIPlayer, checkInFoV, bool, (ShapeBase* obj, F32 fov, bool checkEnabled), (NULL, 45.0f, false),
  1134. "@brief Check whether an object is within a specified veiw cone.\n"
  1135. "@obj Object to check. (If blank, it will check the current target).\n"
  1136. "@fov view angle in degrees.(Defaults to 45)\n"
  1137. "@checkEnabled check whether the object can take damage and if so is still alive.(Defaults to false)\n")
  1138. {
  1139. return object->checkInFoV(obj, fov, checkEnabled);
  1140. }
  1141. DefineEngineMethod( AIPlayer, setMoveTrigger, void, ( U32 slot ),,
  1142. "@brief Sets a movement trigger on an AI object.\n\n"
  1143. "@param slot The trigger slot to set.\n"
  1144. "@see getMoveTrigger()\n"
  1145. "@see clearMoveTrigger()\n"
  1146. "@see clearMoveTriggers()\n")
  1147. {
  1148. object->setMoveTrigger( slot, true );
  1149. }
  1150. DefineEngineMethod( AIPlayer, clearMoveTrigger, void, ( U32 slot ),,
  1151. "@brief Clears a movement trigger on an AI object.\n\n"
  1152. "@param slot The trigger slot to set.\n"
  1153. "@see setMoveTrigger()\n"
  1154. "@see getMoveTrigger()\n"
  1155. "@see clearMoveTriggers()\n")
  1156. {
  1157. object->setMoveTrigger( slot, false );
  1158. }
  1159. DefineEngineMethod( AIPlayer, getMoveTrigger, bool, ( U32 slot ),,
  1160. "@brief Tests if a movement trigger on an AI object is set.\n\n"
  1161. "@param slot The trigger slot to check.\n"
  1162. "@return a boolean indicating if the trigger is set/unset.\n"
  1163. "@see setMoveTrigger()\n"
  1164. "@see clearMoveTrigger()\n"
  1165. "@see clearMoveTriggers()\n")
  1166. {
  1167. return object->getMoveTrigger( slot );
  1168. }
  1169. DefineEngineMethod( AIPlayer, clearMoveTriggers, void, ( ),,
  1170. "@brief Clear ALL movement triggers on an AI object.\n"
  1171. "@see setMoveTrigger()\n"
  1172. "@see getMoveTrigger()\n"
  1173. "@see clearMoveTrigger()\n")
  1174. {
  1175. object->clearMoveTriggers();
  1176. }