navMeshTestTool.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. #include "navMeshTestTool.h"
  2. #include "navigation/guiNavEditorCtrl.h"
  3. #include "console/consoleTypes.h"
  4. #include "gfx/gfxDrawUtil.h"
  5. #include "scene/sceneManager.h"
  6. #include "math/mathUtils.h"
  7. #include "T3D/gameBase/gameConnection.h"
  8. #include "T3D/AI/AIController.h"
  9. IMPLEMENT_CONOBJECT(NavMeshTestTool);
  10. //// take control
  11. // GameConnection* conn = GameConnection::getConnectionToServer();
  12. // if (conn)
  13. // conn->setControlObject(static_cast<GameBase*>(obj));
  14. //}
  15. static void renderBoxOutline(const Box3F& box, const ColorI& col)
  16. {
  17. if (box != Box3F::Invalid)
  18. {
  19. GFXStateBlockDesc desc;
  20. desc.setCullMode(GFXCullNone);
  21. desc.setFillModeSolid();
  22. desc.setZReadWrite(true, false);
  23. desc.setBlend(true);
  24. GFX->getDrawUtil()->drawCube(desc, box, ColorI(col, 20));
  25. desc.setFillModeWireframe();
  26. desc.setBlend(false);
  27. GFX->getDrawUtil()->drawCube(desc, box, ColorI(col, 255));
  28. }
  29. }
  30. void NavMeshTestTool::spawnPlayer(const Point3F& position)
  31. {
  32. if (mSpawnClass.isEmpty() || mSpawnDatablock.isEmpty())
  33. {
  34. Con::warnf("NavMeshTestTool::spawnPlayer - spawn class/datablock not set!");
  35. return;
  36. }
  37. SimObject* spawned = Sim::spawnObject(mSpawnClass, mSpawnDatablock);
  38. SceneObject* obj = dynamic_cast<SceneObject*>(spawned);
  39. if (!obj)
  40. {
  41. Con::warnf("NavMeshTestTool::spawnPlayer - spawn failed or not a SceneObject");
  42. if (spawned)
  43. spawned->deleteObject();
  44. return;
  45. }
  46. obj->setPosition(position);
  47. SimObject* cleanup = Sim::findObject("MissionCleanup");
  48. if (cleanup)
  49. {
  50. SimGroup* missionCleanup = dynamic_cast<SimGroup*>(cleanup);
  51. missionCleanup->addObject(obj);
  52. }
  53. mPlayer = obj;
  54. Con::executef(this, "onPlayerSpawned", obj->getIdString());
  55. #ifdef TORQUE_NAVIGATION_ENABLED
  56. AIPlayer* asAIPlayer = dynamic_cast<AIPlayer*>(mPlayer.getPointer());
  57. if (asAIPlayer)
  58. {
  59. Con::executef(this, "onPlayerSelected");
  60. }
  61. else
  62. {
  63. ShapeBase* sbo = dynamic_cast<ShapeBase*>(mPlayer.getPointer());
  64. if (sbo && sbo->getAIController() && sbo->getAIController()->mControllerData)
  65. {
  66. Con::executef(this, "onPlayerSelected");
  67. }
  68. else
  69. {
  70. Con::executef(this, "onPlayerSelected");
  71. }
  72. }
  73. #else
  74. Con::executef(this, "onPlayerSelected");
  75. #endif
  76. }
  77. void NavMeshTestTool::drawAgent(duDebugDrawTorque& dd, const F32* pos, F32 r, F32 h, F32 c, const U32 col)
  78. {
  79. dd.depthMask(false);
  80. // Agent dimensions.
  81. duDebugDrawCylinderWire(&dd, pos[0] - r, pos[1] + 0.02f, pos[2] - r, pos[0] + r, pos[1] + h, pos[2] + r, col, 2.0f);
  82. duDebugDrawCircle(&dd, pos[0], pos[1] + c, pos[2], r, duRGBA(0, 0, 0, 64), 1.0f);
  83. U32 colb = duRGBA(0, 0, 0, 196);
  84. dd.begin(DU_DRAW_LINES);
  85. dd.vertex(pos[0], pos[1] - c, pos[2], colb);
  86. dd.vertex(pos[0], pos[1] + c, pos[2], colb);
  87. dd.vertex(pos[0] - r / 2, pos[1] + 0.02f, pos[2], colb);
  88. dd.vertex(pos[0] + r / 2, pos[1] + 0.02f, pos[2], colb);
  89. dd.vertex(pos[0], pos[1] + 0.02f, pos[2] - r / 2, colb);
  90. dd.vertex(pos[0], pos[1] + 0.02f, pos[2] + r / 2, colb);
  91. dd.end();
  92. dd.depthMask(true);
  93. }
  94. NavMeshTestTool::NavMeshTestTool()
  95. {
  96. mSpawnClass = String::EmptyString;
  97. mSpawnDatablock = String::EmptyString;
  98. mPlayer = NULL;
  99. mCurPlayer = NULL;
  100. mFollowObject = NULL;
  101. mCurFollowObject = NULL;
  102. mPathStart = Point3F::Max;
  103. mPathEnd = Point3F::Max;
  104. mTestPath = NULL;
  105. mLinkTypes = LinkData(AllFlags);
  106. mFilter.setIncludeFlags(mLinkTypes.getFlags());
  107. mFilter.setExcludeFlags(0);
  108. mSelectFollow = false;
  109. }
  110. void NavMeshTestTool::onActivated(const Gui3DMouseEvent& evt)
  111. {
  112. mSelectFollow = false;
  113. Con::executef(this, "onActivated");
  114. }
  115. void NavMeshTestTool::onDeactivated()
  116. {
  117. if (mTestPath != NULL) {
  118. mTestPath->deleteObject();
  119. mTestPath = NULL;
  120. }
  121. if (mPlayer != NULL)
  122. {
  123. mPlayer = NULL;
  124. Con::executef(this, "onPlayerDeselected");
  125. }
  126. mSelectFollow = false;
  127. Con::executef(this, "onDeactivated");
  128. }
  129. void NavMeshTestTool::on3DMouseDown(const Gui3DMouseEvent& evt)
  130. {
  131. if (mNavMesh.isNull())
  132. return;
  133. Point3F startPnt = evt.pos;
  134. Point3F endPnt = evt.pos + evt.vec * 1000.0f;
  135. RayInfo ri;
  136. bool shift = evt.modifier & SI_LSHIFT;
  137. bool ctrl = evt.modifier & SI_LCTRL;
  138. if (ctrl)
  139. {
  140. if (gServerContainer.castRay(startPnt, endPnt, StaticObjectType, &ri))
  141. {
  142. spawnPlayer(ri.point);
  143. }
  144. return;
  145. }
  146. if (shift)
  147. {
  148. mPlayer = NULL;
  149. Con::executef(this, "onPlayerDeselected");
  150. return;
  151. }
  152. if (gServerContainer.castRay(startPnt, endPnt, PlayerObjectType | VehicleObjectType, &ri))
  153. {
  154. if (!ri.object)
  155. return;
  156. if (mSelectFollow)
  157. {
  158. mFollowObject = ri.object;
  159. Con::executef(this, "onFollowSelected");
  160. mSelectFollow = false;
  161. return;
  162. }
  163. else
  164. {
  165. mPlayer = ri.object;
  166. }
  167. #ifdef TORQUE_NAVIGATION_ENABLED
  168. AIPlayer* asAIPlayer = dynamic_cast<AIPlayer*>(mPlayer.getPointer());
  169. if (asAIPlayer)
  170. {
  171. Con::executef(this, "onPlayerSelected");
  172. }
  173. else
  174. {
  175. ShapeBase* sbo = dynamic_cast<ShapeBase*>(mPlayer.getPointer());
  176. if (sbo && sbo->getAIController() && sbo->getAIController()->mControllerData)
  177. {
  178. Con::executef(this, "onPlayerSelected");
  179. }
  180. else
  181. {
  182. Con::executef(this, "onPlayerSelected");
  183. }
  184. }
  185. #else
  186. Con::executef(this, "onPlayerSelected");
  187. #endif
  188. }
  189. else if (gServerContainer.castRay(startPnt, endPnt, StaticObjectType, &ri))
  190. {
  191. if (mPlayer.isNull())
  192. {
  193. if (mPathStart != Point3F::Max && mPathEnd != Point3F::Max) // start a new path.
  194. {
  195. mPathStart = Point3F::Max;
  196. mPathEnd = Point3F::Max;
  197. }
  198. if (mPathStart != Point3F::Max)
  199. {
  200. mPathEnd = ri.point;
  201. mTestPath = new NavPath();
  202. mTestPath->mMesh = mNavMesh;
  203. mTestPath->mFrom = mPathStart;
  204. mTestPath->mTo = mPathEnd;
  205. mTestPath->mFromSet = mTestPath->mToSet = true;
  206. mTestPath->mAlwaysRender = true;
  207. mTestPath->mLinkTypes = mLinkTypes;
  208. mTestPath->mFilter = mFilter;
  209. mTestPath->mXray = true;
  210. // Paths plan automatically upon being registered.
  211. if (!mTestPath->registerObject())
  212. {
  213. delete mTestPath;
  214. return;
  215. }
  216. }
  217. else
  218. {
  219. mPathStart = ri.point;
  220. if (mTestPath != NULL) {
  221. mTestPath->deleteObject();
  222. mTestPath = NULL;
  223. }
  224. }
  225. }
  226. else
  227. {
  228. AIPlayer* asAIPlayer = dynamic_cast<AIPlayer*>(mPlayer.getPointer());
  229. if (asAIPlayer) //try direct
  230. {
  231. asAIPlayer->setPathDestination(ri.point);
  232. mPathStart = mPlayer->getPosition();
  233. mPathEnd = ri.point;
  234. }
  235. else
  236. {
  237. ShapeBase* sbo = dynamic_cast<ShapeBase*>(mPlayer.getPointer());
  238. if (sbo->getAIController())
  239. {
  240. if (sbo->getAIController()->mControllerData)
  241. {
  242. sbo->getAIController()->getNav()->setPathDestination(ri.point, true);
  243. mPathStart = mPlayer->getPosition();
  244. mPathEnd = ri.point;
  245. }
  246. }
  247. }
  248. }
  249. }
  250. }
  251. void NavMeshTestTool::on3DMouseMove(const Gui3DMouseEvent& evt)
  252. {
  253. if (mNavMesh.isNull())
  254. return;
  255. Point3F startPnt = evt.pos;
  256. Point3F endPnt = evt.pos + evt.vec * 1000.0f;
  257. RayInfo ri;
  258. if (gServerContainer.castRay(startPnt, endPnt, PlayerObjectType | VehicleObjectType, &ri))
  259. {
  260. if (mSelectFollow)
  261. mCurFollowObject = ri.object;
  262. else
  263. mCurPlayer = ri.object;
  264. }
  265. else
  266. {
  267. mCurFollowObject = NULL;
  268. mCurPlayer = NULL;
  269. }
  270. }
  271. void NavMeshTestTool::onRender3D()
  272. {
  273. if (mNavMesh.isNull())
  274. return;
  275. static const U32 startCol = duRGBA(128, 25, 0, 192);
  276. static const U32 endCol = duRGBA(51, 102, 0, 129);
  277. const F32 agentRadius = mNavMesh->mWalkableRadius;
  278. const F32 agentHeight = mNavMesh->mWalkableHeight;
  279. const F32 agentClimb = mNavMesh->mWalkableClimb;
  280. duDebugDrawTorque dd;
  281. dd.depthMask(false);
  282. if (mPathStart != Point3F::Max)
  283. {
  284. drawAgent(dd, DTStoRC(mPathStart), agentRadius, agentHeight, agentClimb, startCol);
  285. }
  286. if (mPathEnd != Point3F::Max)
  287. {
  288. drawAgent(dd, DTStoRC(mPathEnd), agentRadius, agentHeight, agentClimb, endCol);
  289. }
  290. dd.depthMask(true);
  291. mNavMesh->renderSearch(dd);
  292. dd.immediateRender();
  293. if (!mCurFollowObject.isNull())
  294. renderBoxOutline(mCurFollowObject->getWorldBox(), ColorI::LIGHT);
  295. if (!mCurPlayer.isNull())
  296. renderBoxOutline(mCurPlayer->getWorldBox(), ColorI::BLUE);
  297. if (!mPlayer.isNull())
  298. renderBoxOutline(mPlayer->getWorldBox(), ColorI::GREEN);
  299. if (!mFollowObject.isNull())
  300. renderBoxOutline(mFollowObject->getWorldBox(), ColorI::WHITE);
  301. }
  302. bool NavMeshTestTool::updateGuiInfo()
  303. {
  304. SimObject* statusbar;
  305. Sim::findObject("EditorGuiStatusBar", statusbar);
  306. GuiTextCtrl* selectionBar;
  307. Sim::findObject("EWorldEditorStatusBarSelection", selectionBar);
  308. String text;
  309. if (mPlayer)
  310. text = "LMB To Select move Destination. LSHIFT+LMB To Deselect Current Bot.";
  311. if (mCurPlayer != NULL && mCurPlayer != mPlayer)
  312. text = "LMB To select Bot.";
  313. if (mPlayer == NULL)
  314. {
  315. text = "LMB To place start/end for test path.";
  316. }
  317. if (mSpawnClass != String::EmptyString && mSpawnDatablock != String::EmptyString)
  318. text += " CTRL+LMB To spawn a new Bot.";
  319. if (mSelectFollow)
  320. text = "LMB To select Follow Target.";
  321. if (statusbar)
  322. Con::executef(statusbar, "setInfo", text.c_str());
  323. text = "";
  324. if (mPlayer)
  325. text = String::ToString("Bot Selected: %d", mPlayer->getId());
  326. if (selectionBar)
  327. selectionBar->setText(text);
  328. return true;
  329. }
  330. S32 NavMeshTestTool::getPlayerId()
  331. {
  332. return mPlayer.isNull() ? 0 : mPlayer->getId();
  333. }
  334. S32 NavMeshTestTool::getFollowObjectId()
  335. {
  336. return mFollowObject.isNull() ? 0 : mFollowObject->getId();
  337. }
  338. DefineEngineMethod(NavMeshTestTool, getPlayer, S32, (), ,
  339. "@brief Return the current player id.")
  340. {
  341. return object->getPlayerId();
  342. }
  343. DefineEngineMethod(NavMeshTestTool, getFollowObject, S32, (), ,
  344. "@brief Return the current follow object id.")
  345. {
  346. return object->getFollowObjectId();
  347. }
  348. DefineEngineMethod(NavMeshTestTool, setSpawnClass, void, (String className), , "")
  349. {
  350. object->setSpawnClass(className);
  351. }
  352. DefineEngineMethod(NavMeshTestTool, setSpawnDatablock, void, (String dbName), , "")
  353. {
  354. object->setSpawnDatablock(dbName);
  355. }
  356. DefineEngineMethod(NavMeshTestTool, followSelectMode, void, (), ,
  357. "@brief Set NavMeshTool to select a follow object.")
  358. {
  359. return object->followSelectMode();
  360. }