AINavigation.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. {
  158. getCtrl()->throwCallback("onReachDestination");
  159. getCtrl()->mMovement.mMoveState = AIController::ModeStop;
  160. }
  161. }
  162. bool AINavigation::setPathDestination(const Point3F& pos)
  163. {
  164. if (!mNavMesh)
  165. updateNavMesh();
  166. // If we can't find a mesh, just move regularly.
  167. if (!mNavMesh)
  168. {
  169. //setMoveDestination(pos);
  170. mControllerRef->throwCallback("onPathFailed");
  171. return false;
  172. }
  173. // Create a new path.
  174. NavPath* path = new NavPath();
  175. path->mMesh = mNavMesh;
  176. path->mFrom = mControllerRef->getAIInfo()->getPosition();
  177. path->mTo = pos;
  178. path->mFromSet = path->mToSet = true;
  179. path->mAlwaysRender = true;
  180. path->mLinkTypes = mControllerRef->mControllerData->mLinkTypes;
  181. path->mXray = true;
  182. // Paths plan automatically upon being registered.
  183. if (!path->registerObject())
  184. {
  185. delete path;
  186. return false;
  187. }
  188. if (path->success())
  189. {
  190. // Clear any current path we might have.
  191. clearPath();
  192. mControllerRef->clearCover();
  193. clearFollow();
  194. // Store new path.
  195. mPathData.path = path;
  196. mPathData.owned = true;
  197. // Skip node 0, which we are currently standing on.
  198. moveToNode(1);
  199. mControllerRef->throwCallback("onPathSuccess");
  200. return true;
  201. }
  202. else
  203. {
  204. // Just move normally if we can't path.
  205. //setMoveDestination(pos, true);
  206. //return;
  207. mControllerRef->throwCallback("onPathFailed");
  208. path->deleteObject();
  209. return false;
  210. }
  211. }
  212. void AINavigation::followObject()
  213. {
  214. if ((mControllerRef->getGoal()->mLastPos - mControllerRef->getAIInfo()->getPosition()).len() < mControllerRef->mControllerData->mMoveTolerance)
  215. return;
  216. if (setPathDestination(mControllerRef->getGoal()->getPosition()))
  217. {
  218. mControllerRef->clearCover();
  219. }
  220. }
  221. void AINavigation::followObject(SceneObject* obj, F32 radius)
  222. {
  223. mControllerRef->setGoal(obj, radius);
  224. followObject();
  225. }
  226. void AINavigation::clearFollow()
  227. {
  228. mControllerRef->clearGoal();
  229. }
  230. void AINavigation::followNavPath(NavPath* path)
  231. {
  232. // Get rid of our current path.
  233. clearPath();
  234. mControllerRef->clearCover();
  235. clearFollow();
  236. // Follow new path.
  237. mPathData.path = path;
  238. mPathData.owned = false;
  239. // Start from 0 since we might not already be there.
  240. moveToNode(0);
  241. }
  242. void AINavigation::clearPath()
  243. {
  244. // Only delete if we own the path.
  245. if (!mPathData.path.isNull() && mPathData.owned)
  246. mPathData.path->deleteObject();
  247. // Reset path data.
  248. mPathData = PathData();
  249. }
  250. DefineEngineMethod(AIController, setMoveDestination, void, (Point3F goal, bool slowDown), (true),
  251. "@brief Tells the AI to move to the location provided\n\n"
  252. "@param goal Coordinates in world space representing location to move to.\n"
  253. "@param slowDown A boolean value. If set to true, the bot will slow down "
  254. "when it gets within 5-meters of its move destination. If false, the bot "
  255. "will stop abruptly when it reaches the move destination. By default, this is true.\n\n"
  256. "@note Upon reaching a move destination, the bot will clear its move destination and "
  257. "calls to getMoveDestination will return \"0 0 0\"."
  258. "@see getMoveDestination()\n")
  259. {
  260. object->getNav()->setMoveDestination(goal, slowDown);
  261. }
  262. DefineEngineMethod(AIController, getMoveDestination, Point3F, (), ,
  263. "@brief Get the AIPlayer's current destination.\n\n"
  264. "@return Returns a point containing the \"x y z\" position "
  265. "of the AIPlayer's current move destination. If no move destination "
  266. "has yet been set, this returns \"0 0 0\"."
  267. "@see setMoveDestination()\n")
  268. {
  269. return object->getNav()->getMoveDestination();
  270. }
  271. DefineEngineMethod(AIController, setPathDestination, bool, (Point3F goal), ,
  272. "@brief Tells the AI to find a path to the location provided\n\n"
  273. "@param goal Coordinates in world space representing location to move to.\n"
  274. "@return True if a path was found.\n\n"
  275. "@see getPathDestination()\n"
  276. "@see setMoveDestination()\n")
  277. {
  278. return object->getNav()->setPathDestination(goal);
  279. }
  280. DefineEngineMethod(AIController, getPathDestination, Point3F, (), ,
  281. "@brief Get the AIPlayer's current pathfinding destination.\n\n"
  282. "@return Returns a point containing the \"x y z\" position "
  283. "of the AIPlayer's current path destination. If no path destination "
  284. "has yet been set, this returns \"0 0 0\"."
  285. "@see setPathDestination()\n")
  286. {
  287. return object->getNav()->getPathDestination();
  288. }
  289. DefineEngineMethod(AIController, followNavPath, void, (SimObjectId obj), ,
  290. "@brief Tell the AIPlayer to follow a path.\n\n"
  291. "@param obj ID of a NavPath object for the character to follow.")
  292. {
  293. NavPath* path;
  294. if (Sim::findObject(obj, path))
  295. object->getNav()->followNavPath(path);
  296. }
  297. DefineEngineMethod(AIController, followObject, void, (SimObjectId obj, F32 radius), ,
  298. "@brief Tell the AIPlayer to follow another object.\n\n"
  299. "@param obj ID of the object to follow.\n"
  300. "@param radius Maximum distance we let the target escape to.")
  301. {
  302. SceneObject* follow;
  303. object->getNav()->clearPath();
  304. object->clearCover();
  305. object->getNav()->clearFollow();
  306. if (Sim::findObject(obj, follow))
  307. object->getNav()->followObject(follow, radius);
  308. }
  309. DefineEngineMethod(AIController, repath, void, (), ,
  310. "@brief Tells the AI to re-plan its path. Does nothing if the character "
  311. "has no path, or if it is following a mission path.\n\n")
  312. {
  313. object->getNav()->repath();
  314. }
  315. DefineEngineMethod(AIController, findNavMesh, S32, (), ,
  316. "@brief Get the NavMesh object this AIPlayer is currently using.\n\n"
  317. "@return The ID of the NavPath object this character is using for "
  318. "pathfinding. This is determined by the character's location, "
  319. "navigation type and other factors. Returns -1 if no NavMesh is "
  320. "found.")
  321. {
  322. NavMesh* mesh = object->getNav()->getNavMesh();
  323. return mesh ? mesh->getId() : -1;
  324. }
  325. DefineEngineMethod(AIController, getNavMesh, S32, (), ,
  326. "@brief Return the NavMesh this AIPlayer is using to navigate.\n\n")
  327. {
  328. NavMesh* m = object->getNav()->getNavMesh();
  329. return m ? m->getId() : 0;
  330. }
  331. DefineEngineMethod(AIController, setNavSize, void, (const char* size), ,
  332. "@brief Set the size of NavMesh this character uses. One of \"Small\", \"Regular\" or \"Large\".")
  333. {
  334. if (!String::compare(size, "Small"))
  335. object->getNav()->setNavSize(AINavigation::Small);
  336. else if (!String::compare(size, "Regular"))
  337. object->getNav()->setNavSize(AINavigation::Regular);
  338. else if (!String::compare(size, "Large"))
  339. object->getNav()->setNavSize(AINavigation::Large);
  340. else
  341. Con::errorf("AIPlayer::setNavSize: no such size '%s'.", size);
  342. }
  343. DefineEngineMethod(AIController, getNavSize, const char*, (), ,
  344. "@brief Return the size of NavMesh this character uses for pathfinding.")
  345. {
  346. switch (object->getNav()->getNavSize())
  347. {
  348. case AINavigation::Small:
  349. return "Small";
  350. case AINavigation::Regular:
  351. return "Regular";
  352. case AINavigation::Large:
  353. return "Large";
  354. }
  355. return "";
  356. }