AINavigation.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 "AINavigation.h"
  23. #include "AIController.h"
  24. AINavigation::AINavigation(AIController* controller)
  25. {
  26. mControllerRef = controller;
  27. mJump = None;
  28. mNavSize = Regular;
  29. }
  30. NavMesh* AINavigation::findNavMesh() const
  31. {
  32. GameBase* gbo = dynamic_cast<GameBase*>(mControllerRef->getAIInfo()->mObj.getPointer());
  33. // Search for NavMeshes that contain us entirely with the smallest possible
  34. // volume.
  35. NavMesh* mesh = NULL;
  36. SimSet* set = NavMesh::getServerSet();
  37. for (U32 i = 0; i < set->size(); i++)
  38. {
  39. NavMesh* m = static_cast<NavMesh*>(set->at(i));
  40. if (m->getWorldBox().isContained(gbo->getWorldBox()))
  41. {
  42. // Check that mesh size is appropriate.
  43. if (gbo->isMounted())
  44. {
  45. if (!m->mVehicles)
  46. continue;
  47. }
  48. else
  49. {
  50. if ((getNavSize() == Small && !m->mSmallCharacters) ||
  51. (getNavSize() == Regular && !m->mRegularCharacters) ||
  52. (getNavSize() == Large && !m->mLargeCharacters))
  53. continue;
  54. }
  55. if (!mesh || m->getWorldBox().getVolume() < mesh->getWorldBox().getVolume())
  56. mesh = m;
  57. }
  58. }
  59. return mesh;
  60. }
  61. void AINavigation::updateNavMesh()
  62. {
  63. GameBase* gbo = dynamic_cast<GameBase*>(mControllerRef->getAIInfo()->mObj.getPointer());
  64. NavMesh* old = mNavMesh;
  65. if (mNavMesh.isNull())
  66. mNavMesh = findNavMesh();
  67. else
  68. {
  69. if (!mNavMesh->getWorldBox().isContained(gbo->getWorldBox()))
  70. mNavMesh = findNavMesh();
  71. }
  72. // See if we need to update our path.
  73. if (mNavMesh != old && !mPathData.path.isNull())
  74. {
  75. setPathDestination(mPathData.path->mTo);
  76. }
  77. }
  78. void AINavigation::moveToNode(S32 node)
  79. {
  80. if (mPathData.path.isNull())
  81. return;
  82. // -1 is shorthand for 'last path node'.
  83. if (node == -1)
  84. node = mPathData.path->size() - 1;
  85. // Consider slowing down on the last path node.
  86. setMoveDestination(mPathData.path->getNode(node), false);
  87. // Check flags for this segment.
  88. if (mPathData.index)
  89. {
  90. U16 flags = mPathData.path->getFlags(node - 1);
  91. // Jump if we must.
  92. if (flags & LedgeFlag)
  93. mJump = Ledge;
  94. else if (flags & JumpFlag)
  95. mJump = Now;
  96. else
  97. // Catch pathing errors.
  98. mJump = None;
  99. }
  100. // Store current index.
  101. mPathData.index = node;
  102. }
  103. void AINavigation::repath()
  104. {
  105. // Ineffectual if we don't have a path, or are using someone else's.
  106. if (mPathData.path.isNull() || !mPathData.owned)
  107. return;
  108. if (!mControllerRef->getGoal()) return;
  109. // If we're following, get their position.
  110. mPathData.path->mTo = mControllerRef->getGoal()->getPosition();
  111. // Update from position and replan.
  112. mPathData.path->mFrom = mControllerRef->getAIInfo()->getPosition();
  113. mPathData.path->plan();
  114. // Move to first node (skip start pos).
  115. moveToNode(1);
  116. }
  117. Point3F AINavigation::getPathDestination() const
  118. {
  119. if (!mPathData.path.isNull())
  120. return mPathData.path->mTo;
  121. return Point3F(0, 0, 0);
  122. }
  123. void AINavigation::setMoveDestination(const Point3F& location, bool slowdown)
  124. {
  125. mMoveDestination = location;
  126. mControllerRef->mMovement.mMoveState = AIController::ModeMove;
  127. mControllerRef->mMovement.mMoveSlowdown = slowdown;
  128. mControllerRef->mMovement.mMoveStuckTestCountdown = mControllerRef->mControllerData->mMoveStuckTestDelay;
  129. }
  130. void AINavigation::onReachDestination()
  131. {
  132. #ifdef TORQUE_NAVIGATION_ENABLED
  133. if (!getPath().isNull())
  134. {
  135. if (mPathData.index == getPath()->size() - 1)
  136. {
  137. // Handle looping paths.
  138. if (getPath()->mIsLooping)
  139. moveToNode(0);
  140. // Otherwise end path.
  141. else
  142. {
  143. clearPath();
  144. getCtrl()->throwCallback("onReachDestination");
  145. }
  146. }
  147. else
  148. {
  149. moveToNode(mPathData.index + 1);
  150. // Throw callback every time if we're on a looping path.
  151. //if(mPathData.path->mIsLooping)
  152. //throwCallback("onReachDestination");
  153. }
  154. }
  155. else
  156. #endif
  157. getCtrl()->throwCallback("onReachDestination");
  158. getCtrl()->mMovement.mMoveState = AIController::ModeStop;
  159. }
  160. bool AINavigation::setPathDestination(const Point3F& pos)
  161. {
  162. if (!mNavMesh)
  163. updateNavMesh();
  164. // If we can't find a mesh, just move regularly.
  165. if (!mNavMesh)
  166. {
  167. //setMoveDestination(pos);
  168. mControllerRef->throwCallback("onPathFailed");
  169. return false;
  170. }
  171. // Create a new path.
  172. NavPath* path = new NavPath();
  173. path->mMesh = mNavMesh;
  174. path->mFrom = mControllerRef->getAIInfo()->getPosition();
  175. path->mTo = pos;
  176. path->mFromSet = path->mToSet = true;
  177. path->mAlwaysRender = true;
  178. path->mLinkTypes = mControllerRef->mControllerData->mLinkTypes;
  179. path->mXray = true;
  180. // Paths plan automatically upon being registered.
  181. if (!path->registerObject())
  182. {
  183. delete path;
  184. return false;
  185. }
  186. if (path->success())
  187. {
  188. // Clear any current path we might have.
  189. clearPath();
  190. mControllerRef->clearCover();
  191. clearFollow();
  192. // Store new path.
  193. mPathData.path = path;
  194. mPathData.owned = true;
  195. // Skip node 0, which we are currently standing on.
  196. moveToNode(1);
  197. mControllerRef->throwCallback("onPathSuccess");
  198. return true;
  199. }
  200. else
  201. {
  202. // Just move normally if we can't path.
  203. //setMoveDestination(pos, true);
  204. //return;
  205. mControllerRef->throwCallback("onPathFailed");
  206. path->deleteObject();
  207. return false;
  208. }
  209. }
  210. void AINavigation::followObject()
  211. {
  212. if ((mControllerRef->getGoal()->mLastPos - mControllerRef->getAIInfo()->getPosition()).len() < mControllerRef->mControllerData->mMoveTolerance)
  213. return;
  214. if (setPathDestination(mControllerRef->getGoal()->getPosition()))
  215. {
  216. mControllerRef->clearCover();
  217. }
  218. }
  219. void AINavigation::followObject(SceneObject* obj, F32 radius)
  220. {
  221. mControllerRef->setGoal(obj, radius);
  222. followObject();
  223. }
  224. void AINavigation::clearFollow()
  225. {
  226. mControllerRef->clearGoal();
  227. }
  228. void AINavigation::followNavPath(NavPath* path)
  229. {
  230. // Get rid of our current path.
  231. clearPath();
  232. mControllerRef->clearCover();
  233. clearFollow();
  234. // Follow new path.
  235. mPathData.path = path;
  236. mPathData.owned = false;
  237. // Start from 0 since we might not already be there.
  238. moveToNode(0);
  239. }
  240. void AINavigation::clearPath()
  241. {
  242. // Only delete if we own the path.
  243. if (!mPathData.path.isNull() && mPathData.owned)
  244. mPathData.path->deleteObject();
  245. // Reset path data.
  246. mPathData = PathData();
  247. }
  248. DefineEngineMethod(AIController, setMoveDestination, void, (Point3F goal, bool slowDown), (true),
  249. "@brief Tells the AI to move to the location provided\n\n"
  250. "@param goal Coordinates in world space representing location to move to.\n"
  251. "@param slowDown A boolean value. If set to true, the bot will slow down "
  252. "when it gets within 5-meters of its move destination. If false, the bot "
  253. "will stop abruptly when it reaches the move destination. By default, this is true.\n\n"
  254. "@note Upon reaching a move destination, the bot will clear its move destination and "
  255. "calls to getMoveDestination will return \"0 0 0\"."
  256. "@see getMoveDestination()\n")
  257. {
  258. object->getNav()->setMoveDestination(goal, slowDown);
  259. }
  260. DefineEngineMethod(AIController, getMoveDestination, Point3F, (), ,
  261. "@brief Get the AIPlayer's current destination.\n\n"
  262. "@return Returns a point containing the \"x y z\" position "
  263. "of the AIPlayer's current move destination. If no move destination "
  264. "has yet been set, this returns \"0 0 0\"."
  265. "@see setMoveDestination()\n")
  266. {
  267. return object->getNav()->getMoveDestination();
  268. }
  269. DefineEngineMethod(AIController, setPathDestination, bool, (Point3F goal), ,
  270. "@brief Tells the AI to find a path to the location provided\n\n"
  271. "@param goal Coordinates in world space representing location to move to.\n"
  272. "@return True if a path was found.\n\n"
  273. "@see getPathDestination()\n"
  274. "@see setMoveDestination()\n")
  275. {
  276. return object->getNav()->setPathDestination(goal);
  277. }
  278. DefineEngineMethod(AIController, getPathDestination, Point3F, (), ,
  279. "@brief Get the AIPlayer's current pathfinding destination.\n\n"
  280. "@return Returns a point containing the \"x y z\" position "
  281. "of the AIPlayer's current path destination. If no path destination "
  282. "has yet been set, this returns \"0 0 0\"."
  283. "@see setPathDestination()\n")
  284. {
  285. return object->getNav()->getPathDestination();
  286. }
  287. DefineEngineMethod(AIController, followNavPath, void, (SimObjectId obj), ,
  288. "@brief Tell the AIPlayer to follow a path.\n\n"
  289. "@param obj ID of a NavPath object for the character to follow.")
  290. {
  291. NavPath* path;
  292. if (Sim::findObject(obj, path))
  293. object->getNav()->followNavPath(path);
  294. }
  295. DefineEngineMethod(AIController, followObject, void, (SimObjectId obj, F32 radius), ,
  296. "@brief Tell the AIPlayer to follow another object.\n\n"
  297. "@param obj ID of the object to follow.\n"
  298. "@param radius Maximum distance we let the target escape to.")
  299. {
  300. SceneObject* follow;
  301. object->getNav()->clearPath();
  302. object->clearCover();
  303. object->getNav()->clearFollow();
  304. if (Sim::findObject(obj, follow))
  305. object->getNav()->followObject(follow, radius);
  306. }
  307. DefineEngineMethod(AIController, repath, void, (), ,
  308. "@brief Tells the AI to re-plan its path. Does nothing if the character "
  309. "has no path, or if it is following a mission path.\n\n")
  310. {
  311. object->getNav()->repath();
  312. }
  313. DefineEngineMethod(AIController, findNavMesh, S32, (), ,
  314. "@brief Get the NavMesh object this AIPlayer is currently using.\n\n"
  315. "@return The ID of the NavPath object this character is using for "
  316. "pathfinding. This is determined by the character's location, "
  317. "navigation type and other factors. Returns -1 if no NavMesh is "
  318. "found.")
  319. {
  320. NavMesh* mesh = object->getNav()->getNavMesh();
  321. return mesh ? mesh->getId() : -1;
  322. }
  323. DefineEngineMethod(AIController, getNavMesh, S32, (), ,
  324. "@brief Return the NavMesh this AIPlayer is using to navigate.\n\n")
  325. {
  326. NavMesh* m = object->getNav()->getNavMesh();
  327. return m ? m->getId() : 0;
  328. }
  329. DefineEngineMethod(AIController, setNavSize, void, (const char* size), ,
  330. "@brief Set the size of NavMesh this character uses. One of \"Small\", \"Regular\" or \"Large\".")
  331. {
  332. if (!String::compare(size, "Small"))
  333. object->getNav()->setNavSize(AINavigation::Small);
  334. else if (!String::compare(size, "Regular"))
  335. object->getNav()->setNavSize(AINavigation::Regular);
  336. else if (!String::compare(size, "Large"))
  337. object->getNav()->setNavSize(AINavigation::Large);
  338. else
  339. Con::errorf("AIPlayer::setNavSize: no such size '%s'.", size);
  340. }
  341. DefineEngineMethod(AIController, getNavSize, const char*, (), ,
  342. "@brief Return the size of NavMesh this character uses for pathfinding.")
  343. {
  344. switch (object->getNav()->getNavSize())
  345. {
  346. case AINavigation::Small:
  347. return "Small";
  348. case AINavigation::Regular:
  349. return "Regular";
  350. case AINavigation::Large:
  351. return "Large";
  352. }
  353. return "";
  354. }