aiPlayer.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. static U32 sAIPlayerLoSMask = TerrainObjectType | StaticShapeObjectType | StaticObjectType;
  29. IMPLEMENT_CO_NETOBJECT_V1(AIPlayer);
  30. ConsoleDocClass( AIPlayer,
  31. "@brief A Player object not controlled by conventional input, but by an AI engine.\n\n"
  32. "The AIPlayer provides a Player object that may be controlled from script. You control "
  33. "where the player moves and how fast. You may also set where the AIPlayer is aiming at "
  34. "-- either a location or another game object.\n\n"
  35. "The AIPlayer class does not have a datablock of its own. It makes use of the PlayerData "
  36. "datablock to define how it looks, etc. As the AIPlayer is an extension of the Player class "
  37. "it can mount objects and fire weapons, or mount vehicles and drive them.\n\n"
  38. "While the PlayerData datablock is used, there are a number of additional callbacks that are "
  39. "implemented by AIPlayer on the datablock. These are listed here:\n\n"
  40. "void onReachDestination(AIPlayer obj) \n"
  41. "Called when the player has reached its set destination using the setMoveDestination() method. "
  42. "The actual point at which this callback is called is when the AIPlayer is within the mMoveTolerance "
  43. "of the defined destination.\n\n"
  44. "void onMoveStuck(AIPlayer obj) \n"
  45. "While in motion, if an AIPlayer has moved less than moveStuckTolerance within a single tick, this "
  46. "callback is called. From here you could choose an alternate destination to get the AIPlayer moving "
  47. "again.\n\n"
  48. "void onTargetEnterLOS(AIPlayer obj) \n"
  49. "When an object is being aimed at (following a call to setAimObject()) and the targeted object enters "
  50. "the AIPlayer's line of sight, this callback is called. The LOS test is a ray from the AIPlayer's eye "
  51. "position to the center of the target's bounding box. The LOS ray test only checks against interiors, "
  52. "statis shapes, and terrain.\n\n"
  53. "void onTargetExitLOS(AIPlayer obj) \n"
  54. "When an object is being aimed at (following a call to setAimObject()) and the targeted object leaves "
  55. "the AIPlayer's line of sight, this callback is called. The LOS test is a ray from the AIPlayer's eye "
  56. "position to the center of the target's bounding box. The LOS ray test only checks against interiors, "
  57. "statis shapes, and terrain.\n\n"
  58. "@tsexample\n"
  59. "// Create the demo player object\n"
  60. "%player = new AiPlayer()\n"
  61. "{\n"
  62. " dataBlock = DemoPlayer;\n"
  63. " path = \"\";\n"
  64. "};\n"
  65. "@endtsexample\n\n"
  66. "@see Player for a list of all inherited functions, variables, and base description\n"
  67. "@ingroup AI\n"
  68. "@ingroup gameObjects\n");
  69. /**
  70. * Constructor
  71. */
  72. AIPlayer::AIPlayer()
  73. {
  74. mMoveDestination.set( 0.0f, 0.0f, 0.0f );
  75. mMoveSpeed = 1.0f;
  76. mMoveTolerance = 0.25f;
  77. mMoveStuckTolerance = 0.01f;
  78. mMoveStuckTestDelay = 30;
  79. mMoveStuckTestCountdown = 0;
  80. mMoveSlowdown = true;
  81. mMoveState = ModeStop;
  82. mAimObject = 0;
  83. mAimLocationSet = false;
  84. mTargetInLOS = false;
  85. mAimOffset = Point3F(0.0f, 0.0f, 0.0f);
  86. mIsAiControlled = true;
  87. }
  88. /**
  89. * Destructor
  90. */
  91. AIPlayer::~AIPlayer()
  92. {
  93. }
  94. void AIPlayer::initPersistFields()
  95. {
  96. addGroup( "AI" );
  97. addField( "mMoveTolerance", TypeF32, Offset( mMoveTolerance, AIPlayer ),
  98. "@brief Distance from destination before stopping.\n\n"
  99. "When the AIPlayer is moving to a given destination it will move to within "
  100. "this distance of the destination and then stop. By providing this tolerance "
  101. "it helps the AIPlayer from never reaching its destination due to minor obstacles, "
  102. "rounding errors on its position calculation, etc. By default it is set to 0.25.\n");
  103. addField( "moveStuckTolerance", TypeF32, Offset( mMoveStuckTolerance, AIPlayer ),
  104. "@brief Distance tolerance on stuck check.\n\n"
  105. "When the AIPlayer is moving to a given destination, if it ever moves less than "
  106. "this tolerance during a single tick, the AIPlayer is considered stuck. At this point "
  107. "the onMoveStuck() callback is called on the datablock.\n");
  108. addField( "moveStuckTestDelay", TypeS32, Offset( mMoveStuckTestDelay, AIPlayer ),
  109. "@brief The number of ticks to wait before testing if the AIPlayer is stuck.\n\n"
  110. "When the AIPlayer is asked to move, this property is the number of ticks to wait "
  111. "before the AIPlayer starts to check if it is stuck. This delay allows the AIPlayer "
  112. "to accelerate to full speed without its initial slow start being considered as stuck.\n"
  113. "@note Set to zero to have the stuck test start immediately.\n");
  114. endGroup( "AI" );
  115. Parent::initPersistFields();
  116. }
  117. bool AIPlayer::onAdd()
  118. {
  119. if (!Parent::onAdd())
  120. return false;
  121. // Use the eye as the current position (see getAIMove)
  122. MatrixF eye;
  123. getEyeTransform(&eye);
  124. mLastLocation = eye.getPosition();
  125. return true;
  126. }
  127. /**
  128. * Sets the speed at which this AI moves
  129. *
  130. * @param speed Speed to move, default player was 10
  131. */
  132. void AIPlayer::setMoveSpeed( F32 speed )
  133. {
  134. mMoveSpeed = getMax(0.0f, getMin( 1.0f, speed ));
  135. }
  136. /**
  137. * Stops movement for this AI
  138. */
  139. void AIPlayer::stopMove()
  140. {
  141. mMoveState = ModeStop;
  142. }
  143. /**
  144. * Sets how far away from the move location is considered
  145. * "on target"
  146. *
  147. * @param tolerance Movement tolerance for error
  148. */
  149. void AIPlayer::setMoveTolerance( const F32 tolerance )
  150. {
  151. mMoveTolerance = getMax( 0.1f, tolerance );
  152. }
  153. /**
  154. * Sets the location for the bot to run to
  155. *
  156. * @param location Point to run to
  157. */
  158. void AIPlayer::setMoveDestination( const Point3F &location, bool slowdown )
  159. {
  160. mMoveDestination = location;
  161. mMoveState = ModeMove;
  162. mMoveSlowdown = slowdown;
  163. mMoveStuckTestCountdown = mMoveStuckTestDelay;
  164. }
  165. /**
  166. * Sets the object the bot is targeting
  167. *
  168. * @param targetObject The object to target
  169. */
  170. void AIPlayer::setAimObject( GameBase *targetObject )
  171. {
  172. mAimObject = targetObject;
  173. mTargetInLOS = false;
  174. mAimOffset = Point3F(0.0f, 0.0f, 0.0f);
  175. }
  176. /**
  177. * Sets the object the bot is targeting and an offset to add to target location
  178. *
  179. * @param targetObject The object to target
  180. * @param offset The offest from the target location to aim at
  181. */
  182. void AIPlayer::setAimObject( GameBase *targetObject, Point3F offset )
  183. {
  184. mAimObject = targetObject;
  185. mTargetInLOS = false;
  186. mAimOffset = offset;
  187. }
  188. /**
  189. * Sets the location for the bot to aim at
  190. *
  191. * @param location Point to aim at
  192. */
  193. void AIPlayer::setAimLocation( const Point3F &location )
  194. {
  195. mAimObject = 0;
  196. mAimLocationSet = true;
  197. mAimLocation = location;
  198. mAimOffset = Point3F(0.0f, 0.0f, 0.0f);
  199. }
  200. /**
  201. * Clears the aim location and sets it to the bot's
  202. * current destination so he looks where he's going
  203. */
  204. void AIPlayer::clearAim()
  205. {
  206. mAimObject = 0;
  207. mAimLocationSet = false;
  208. mAimOffset = Point3F(0.0f, 0.0f, 0.0f);
  209. }
  210. /**
  211. * This method calculates the moves for the AI player
  212. *
  213. * @param movePtr Pointer to move the move list into
  214. */
  215. bool AIPlayer::getAIMove(Move *movePtr)
  216. {
  217. *movePtr = NullMove;
  218. // Use the eye as the current position.
  219. MatrixF eye;
  220. getEyeTransform(&eye);
  221. Point3F location = eye.getPosition();
  222. Point3F rotation = getRotation();
  223. // Orient towards the aim point, aim object, or towards
  224. // our destination.
  225. if (mAimObject || mAimLocationSet || mMoveState != ModeStop)
  226. {
  227. // Update the aim position if we're aiming for an object
  228. if (mAimObject)
  229. mAimLocation = mAimObject->getPosition() + mAimOffset;
  230. else
  231. if (!mAimLocationSet)
  232. mAimLocation = mMoveDestination;
  233. F32 xDiff = mAimLocation.x - location.x;
  234. F32 yDiff = mAimLocation.y - location.y;
  235. if (!mIsZero(xDiff) || !mIsZero(yDiff))
  236. {
  237. // First do Yaw
  238. // use the cur yaw between -Pi and Pi
  239. F32 curYaw = rotation.z;
  240. while (curYaw > M_2PI_F)
  241. curYaw -= M_2PI_F;
  242. while (curYaw < -M_2PI_F)
  243. curYaw += M_2PI_F;
  244. // find the yaw offset
  245. F32 newYaw = mAtan2( xDiff, yDiff );
  246. F32 yawDiff = newYaw - curYaw;
  247. // make it between 0 and 2PI
  248. if( yawDiff < 0.0f )
  249. yawDiff += M_2PI_F;
  250. else if( yawDiff >= M_2PI_F )
  251. yawDiff -= M_2PI_F;
  252. // now make sure we take the short way around the circle
  253. if( yawDiff > M_PI_F )
  254. yawDiff -= M_2PI_F;
  255. else if( yawDiff < -M_PI_F )
  256. yawDiff += M_2PI_F;
  257. movePtr->yaw = yawDiff;
  258. // Next do pitch.
  259. if (!mAimObject && !mAimLocationSet)
  260. {
  261. // Level out if were just looking at our next way point.
  262. Point3F headRotation = getHeadRotation();
  263. movePtr->pitch = -headRotation.x;
  264. }
  265. else
  266. {
  267. // This should be adjusted to run from the
  268. // eye point to the object's center position. Though this
  269. // works well enough for now.
  270. F32 vertDist = mAimLocation.z - location.z;
  271. F32 horzDist = mSqrt(xDiff * xDiff + yDiff * yDiff);
  272. F32 newPitch = mAtan2( horzDist, vertDist ) - ( M_PI_F / 2.0f );
  273. if (mFabs(newPitch) > 0.01f)
  274. {
  275. Point3F headRotation = getHeadRotation();
  276. movePtr->pitch = newPitch - headRotation.x;
  277. }
  278. }
  279. }
  280. }
  281. else
  282. {
  283. // Level out if we're not doing anything else
  284. Point3F headRotation = getHeadRotation();
  285. movePtr->pitch = -headRotation.x;
  286. }
  287. // Move towards the destination
  288. if (mMoveState != ModeStop)
  289. {
  290. F32 xDiff = mMoveDestination.x - location.x;
  291. F32 yDiff = mMoveDestination.y - location.y;
  292. // Check if we should mMove, or if we are 'close enough'
  293. if (mFabs(xDiff) < mMoveTolerance && mFabs(yDiff) < mMoveTolerance)
  294. {
  295. mMoveState = ModeStop;
  296. throwCallback("onReachDestination");
  297. }
  298. else
  299. {
  300. // Build move direction in world space
  301. if (mIsZero(xDiff))
  302. movePtr->y = (location.y > mMoveDestination.y) ? -1.0f : 1.0f;
  303. else
  304. if (mIsZero(yDiff))
  305. movePtr->x = (location.x > mMoveDestination.x) ? -1.0f : 1.0f;
  306. else
  307. if (mFabs(xDiff) > mFabs(yDiff))
  308. {
  309. F32 value = mFabs(yDiff / xDiff);
  310. movePtr->y = (location.y > mMoveDestination.y) ? -value : value;
  311. movePtr->x = (location.x > mMoveDestination.x) ? -1.0f : 1.0f;
  312. }
  313. else
  314. {
  315. F32 value = mFabs(xDiff / yDiff);
  316. movePtr->x = (location.x > mMoveDestination.x) ? -value : value;
  317. movePtr->y = (location.y > mMoveDestination.y) ? -1.0f : 1.0f;
  318. }
  319. // Rotate the move into object space (this really only needs
  320. // a 2D matrix)
  321. Point3F newMove;
  322. MatrixF moveMatrix;
  323. moveMatrix.set(EulerF(0.0f, 0.0f, -(rotation.z + movePtr->yaw)));
  324. moveMatrix.mulV( Point3F( movePtr->x, movePtr->y, 0.0f ), &newMove );
  325. movePtr->x = newMove.x;
  326. movePtr->y = newMove.y;
  327. // Set movement speed. We'll slow down once we get close
  328. // to try and stop on the spot...
  329. if (mMoveSlowdown)
  330. {
  331. F32 speed = mMoveSpeed;
  332. F32 dist = mSqrt(xDiff*xDiff + yDiff*yDiff);
  333. F32 maxDist = 5.0f;
  334. if (dist < maxDist)
  335. speed *= dist / maxDist;
  336. movePtr->x *= speed;
  337. movePtr->y *= speed;
  338. mMoveState = ModeSlowing;
  339. }
  340. else
  341. {
  342. movePtr->x *= mMoveSpeed;
  343. movePtr->y *= mMoveSpeed;
  344. mMoveState = ModeMove;
  345. }
  346. if (mMoveStuckTestCountdown > 0)
  347. --mMoveStuckTestCountdown;
  348. else
  349. {
  350. // We should check to see if we are stuck...
  351. F32 locationDelta = (location - mLastLocation).len();
  352. if (locationDelta < mMoveStuckTolerance && mDamageState == Enabled)
  353. {
  354. // If we are slowing down, then it's likely that our location delta will be less than
  355. // our move stuck tolerance. Because we can be both slowing and stuck
  356. // we should TRY to check if we've moved. This could use better detection.
  357. if ( mMoveState != ModeSlowing || locationDelta == 0 )
  358. {
  359. mMoveState = ModeStuck;
  360. throwCallback("onMoveStuck");
  361. }
  362. }
  363. }
  364. }
  365. }
  366. // Test for target location in sight if it's an object. The LOS is
  367. // run from the eye position to the center of the object's bounding,
  368. // which is not very accurate.
  369. if (mAimObject)
  370. {
  371. if (checkInLos(mAimObject.getPointer()))
  372. {
  373. if (!mTargetInLOS)
  374. {
  375. throwCallback("onTargetEnterLOS");
  376. mTargetInLOS = true;
  377. }
  378. }
  379. else if (mTargetInLOS)
  380. {
  381. throwCallback("onTargetExitLOS");
  382. mTargetInLOS = false;
  383. }
  384. }
  385. // Replicate the trigger state into the move so that
  386. // triggers can be controlled from scripts.
  387. for( U32 i = 0; i < MaxMountedImages; i++ )
  388. movePtr->trigger[i] = getImageTriggerState(i);
  389. mLastLocation = location;
  390. return true;
  391. }
  392. /**
  393. * Utility function to throw callbacks. Callbacks always occure
  394. * on the datablock class.
  395. *
  396. * @param name Name of script function to call
  397. */
  398. void AIPlayer::throwCallback( const char *name )
  399. {
  400. Con::executef(getDataBlock(), name, getIdString());
  401. }
  402. // --------------------------------------------------------------------------------------------
  403. // Console Functions
  404. // --------------------------------------------------------------------------------------------
  405. DefineEngineMethod( AIPlayer, stop, void, ( ),,
  406. "@brief Tells the AIPlayer to stop moving.\n\n")
  407. {
  408. object->stopMove();
  409. }
  410. DefineEngineMethod( AIPlayer, clearAim, void, ( ),,
  411. "@brief Use this to stop aiming at an object or a point.\n\n"
  412. "@see setAimLocation()\n"
  413. "@see setAimObject()\n")
  414. {
  415. object->clearAim();
  416. }
  417. DefineEngineMethod( AIPlayer, setMoveSpeed, void, ( F32 speed ),,
  418. "@brief Sets the move speed for an AI object.\n\n"
  419. "@param speed A speed multiplier between 0.0 and 1.0. "
  420. "This is multiplied by the AIPlayer's base movement rates (as defined in "
  421. "its PlayerData datablock)\n\n"
  422. "@see getMoveDestination()\n")
  423. {
  424. object->setMoveSpeed(speed);
  425. }
  426. DefineEngineMethod( AIPlayer, getMoveSpeed, F32, ( ),,
  427. "@brief Gets the move speed of an AI object.\n\n"
  428. "@return A speed multiplier between 0.0 and 1.0.\n\n"
  429. "@see setMoveSpeed()\n")
  430. {
  431. return object->getMoveSpeed();
  432. }
  433. DefineEngineMethod( AIPlayer, setMoveDestination, void, ( Point3F goal, bool slowDown ), ( true ),
  434. "@brief Tells the AI to move to the location provided\n\n"
  435. "@param goal Coordinates in world space representing location to move to.\n"
  436. "@param slowDown A boolean value. If set to true, the bot will slow down "
  437. "when it gets within 5-meters of its move destination. If false, the bot "
  438. "will stop abruptly when it reaches the move destination. By default, this is true.\n\n"
  439. "@note Upon reaching a move destination, the bot will clear its move destination and "
  440. "calls to getMoveDestination will return \"0 0 0\"."
  441. "@see getMoveDestination()\n")
  442. {
  443. object->setMoveDestination( goal, slowDown);
  444. }
  445. DefineEngineMethod( AIPlayer, getMoveDestination, Point3F, (),,
  446. "@brief Get the AIPlayer's current destination.\n\n"
  447. "@return Returns a point containing the \"x y z\" position "
  448. "of the AIPlayer's current move destination. If no move destination "
  449. "has yet been set, this returns \"0 0 0\"."
  450. "@see setMoveDestination()\n")
  451. {
  452. return object->getMoveDestination();
  453. }
  454. DefineEngineMethod( AIPlayer, setAimLocation, void, ( Point3F target ),,
  455. "@brief Tells the AIPlayer to aim at the location provided.\n\n"
  456. "@param target An \"x y z\" position in the game world to target.\n\n"
  457. "@see getAimLocation()\n")
  458. {
  459. object->setAimLocation(target);
  460. }
  461. DefineEngineMethod( AIPlayer, getAimLocation, Point3F, (),,
  462. "@brief Returns the point the AIPlayer is aiming at.\n\n"
  463. "This will reflect the position set by setAimLocation(), "
  464. "or the position of the object that the bot is now aiming at. "
  465. "If the bot is not aiming at anything, this value will "
  466. "change to whatever point the bot's current line-of-sight intercepts."
  467. "@return World space coordinates of the object AI is aiming at. Formatted as \"X Y Z\".\n\n"
  468. "@see setAimLocation()\n"
  469. "@see setAimObject()\n")
  470. {
  471. return object->getAimLocation();
  472. }
  473. ConsoleDocFragment _setAimObject(
  474. "@brief Sets the AIPlayer's target object. May optionally set an offset from target location\n\n"
  475. "@param targetObject The object to target\n"
  476. "@param offset Optional three-element offset vector which will be added to the position of the aim object.\n\n"
  477. "@tsexample\n"
  478. "// Without an offset\n"
  479. "%ai.setAimObject(%target);\n\n"
  480. "// With an offset\n"
  481. "// Cause our AI object to aim at the target\n"
  482. "// offset (0, 0, 1) so you don't aim at the target's feet\n"
  483. "%ai.setAimObject(%target, \"0 0 1\");\n"
  484. "@endtsexample\n\n"
  485. "@see getAimLocation()\n"
  486. "@see getAimObject()\n"
  487. "@see clearAim()\n",
  488. "AIPlayer",
  489. "void setAimObject(GameBase targetObject, Point3F offset);"
  490. );
  491. DefineConsoleMethod( AIPlayer, setAimObject, void, ( const char * objName, Point3F offset ), (Point3F::Zero), "( GameBase obj, [Point3F offset] )"
  492. "Sets the bot's target object. Optionally set an offset from target location."
  493. "@hide")
  494. {
  495. // Find the target
  496. GameBase *targetObject;
  497. if( Sim::findObject( objName, targetObject ) )
  498. {
  499. object->setAimObject( targetObject, offset );
  500. }
  501. else
  502. object->setAimObject( 0, offset );
  503. }
  504. DefineEngineMethod( AIPlayer, getAimObject, S32, (),,
  505. "@brief Gets the object the AIPlayer is targeting.\n\n"
  506. "@return Returns -1 if no object is being aimed at, "
  507. "or the SimObjectID of the object the AIPlayer is aiming at.\n\n"
  508. "@see setAimObject()\n")
  509. {
  510. GameBase* obj = object->getAimObject();
  511. return obj? obj->getId(): -1;
  512. }
  513. bool AIPlayer::checkInLos(GameBase* target, bool _useMuzzle, bool _checkEnabled)
  514. {
  515. if (!isServerObject()) return false;
  516. if (!target)
  517. {
  518. target = mAimObject.getPointer();
  519. if (!target)
  520. return false;
  521. }
  522. if (_checkEnabled)
  523. {
  524. if (target->getTypeMask() & ShapeBaseObjectType)
  525. {
  526. ShapeBase *shapeBaseCheck = static_cast<ShapeBase *>(target);
  527. if (shapeBaseCheck)
  528. if (shapeBaseCheck->getDamageState() != Enabled) return false;
  529. }
  530. else
  531. return false;
  532. }
  533. RayInfo ri;
  534. disableCollision();
  535. S32 mountCount = target->getMountedObjectCount();
  536. for (S32 i = 0; i < mountCount; i++)
  537. {
  538. target->getMountedObject(i)->disableCollision();
  539. }
  540. Point3F checkPoint ;
  541. if (_useMuzzle)
  542. getMuzzlePointAI(0, &checkPoint );
  543. else
  544. {
  545. MatrixF eyeMat;
  546. getEyeTransform(&eyeMat);
  547. eyeMat.getColumn(3, &checkPoint );
  548. }
  549. bool hit = !gServerContainer.castRay(checkPoint, target->getBoxCenter(), sAIPlayerLoSMask, &ri);
  550. enableCollision();
  551. for (S32 i = 0; i < mountCount; i++)
  552. {
  553. target->getMountedObject(i)->enableCollision();
  554. }
  555. return hit;
  556. }
  557. DefineEngineMethod(AIPlayer, checkInLos, bool, (ShapeBase* obj, bool useMuzzle, bool checkEnabled),(NULL, false, false),
  558. "@brief Check whether an object is in line of sight.\n"
  559. "@obj Object to check. (If blank, it will check the current target).\n"
  560. "@useMuzzle Use muzzle position. Otherwise use eye position. (defaults to false).\n"
  561. "@checkEnabled check whether the object can take damage and if so is still alive.(Defaults to false)\n")
  562. {
  563. return object->checkInLos(obj, useMuzzle, checkEnabled);
  564. }
  565. bool AIPlayer::checkInFoV(GameBase* target, F32 camFov, bool _checkEnabled)
  566. {
  567. if (!isServerObject()) return false;
  568. if (!target)
  569. {
  570. target = mAimObject.getPointer();
  571. if (!target)
  572. return false;
  573. }
  574. if (_checkEnabled)
  575. {
  576. if (target->getTypeMask() & ShapeBaseObjectType)
  577. {
  578. ShapeBase *shapeBaseCheck = static_cast<ShapeBase *>(target);
  579. if (shapeBaseCheck)
  580. if (shapeBaseCheck->getDamageState() != Enabled) return false;
  581. }
  582. else
  583. return false;
  584. }
  585. MatrixF cam = getTransform();
  586. Point3F camPos;
  587. VectorF camDir;
  588. cam.getColumn(3, &camPos);
  589. cam.getColumn(1, &camDir);
  590. camFov = mDegToRad(camFov) / 2;
  591. Point3F shapePos = target->getBoxCenter();
  592. VectorF shapeDir = shapePos - camPos;
  593. // Test to see if it's within our viewcone, this test doesn't
  594. // actually match the viewport very well, should consider
  595. // projection and box test.
  596. shapeDir.normalize();
  597. F32 dot = mDot(shapeDir, camDir);
  598. return (dot > mCos(camFov));
  599. }
  600. DefineEngineMethod(AIPlayer, checkInFoV, bool, (ShapeBase* obj, F32 fov, bool checkEnabled), (NULL, 45.0f, false),
  601. "@brief Check whether an object is within a specified veiw cone.\n"
  602. "@obj Object to check. (If blank, it will check the current target).\n"
  603. "@fov view angle in degrees.(Defaults to 45)\n"
  604. "@checkEnabled check whether the object can take damage and if so is still alive.(Defaults to false)\n")
  605. {
  606. return object->checkInFoV(obj, fov, checkEnabled);
  607. }