InvokeHelper.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #include "InvokeHelper.h"
  2. #include "DetourNavMesh.h"
  3. #include "DetourNavMeshQuery.h"
  4. #include <cstring>
  5. #include <unordered_map>
  6. #include "DetourCommon.h"
  7. static const int NAVMESHSET_MAGIC = 'M' << 24 | 'S' << 16 | 'E' << 8 | 'T'; //'MSET';
  8. static const int NAVMESHSET_VERSION = 1;
  9. struct NavMeshSetHeader
  10. {
  11. int magic;
  12. int version;
  13. int numTiles;
  14. dtNavMeshParams params;
  15. };
  16. struct NavMeshTileHeader
  17. {
  18. dtTileRef tileRef;
  19. int dataSize;
  20. };
  21. int32_t InitNav(const char* buffer, int32_t n, dtNavMesh*& navMesh)
  22. {
  23. int index = 0;
  24. // Read header.
  25. NavMeshSetHeader header;
  26. int count = sizeof(NavMeshSetHeader);
  27. if (index + count > n)
  28. {
  29. return -1;
  30. }
  31. memcpy(&header, buffer + index, count);
  32. index += count;
  33. if (header.magic != NAVMESHSET_MAGIC)
  34. {
  35. return -2;
  36. }
  37. if (header.version != NAVMESHSET_VERSION)
  38. {
  39. return -3;
  40. }
  41. dtNavMesh* mesh = dtAllocNavMesh();
  42. if (!mesh)
  43. {
  44. return -4;
  45. }
  46. dtStatus status = mesh->init(&header.params);
  47. if (dtStatusFailed(status))
  48. {
  49. return -5;
  50. }
  51. // Read tiles.
  52. for (int i = 0; i < header.numTiles; ++i)
  53. {
  54. NavMeshTileHeader tileHeader;
  55. count = sizeof(NavMeshTileHeader);
  56. if (index + count > n)
  57. {
  58. return -6;
  59. }
  60. memcpy(&tileHeader, buffer + index, count);
  61. index += count;
  62. if (!tileHeader.tileRef || !tileHeader.dataSize)
  63. break;
  64. unsigned char* data = (unsigned char*)dtAlloc(tileHeader.dataSize, DT_ALLOC_PERM);
  65. if (!data) break;
  66. memset(data, 0, tileHeader.dataSize);
  67. count = tileHeader.dataSize;
  68. if (index + count > n)
  69. {
  70. return -7;
  71. }
  72. memcpy(data, buffer + index, count);
  73. index += count;
  74. mesh->addTile(data, tileHeader.dataSize, DT_TILE_FREE_DATA, tileHeader.tileRef, 0);
  75. }
  76. navMesh = mesh;
  77. return 0;
  78. }
  79. static const int MAX_POLYS = 256;
  80. static const int MAX_SMOOTH = 2048;
  81. class NavMeshContex
  82. {
  83. public:
  84. dtNavMesh* navMesh;
  85. dtNavMeshQuery* navQuery;
  86. NavMeshContex()
  87. {
  88. }
  89. int32_t Init(const char* buffer, int32_t n)
  90. {
  91. int32_t ret = InitNav(buffer, n, navMesh);
  92. std::string s;
  93. if (ret != 0)
  94. {
  95. return -1;
  96. }
  97. navQuery = new dtNavMeshQuery();
  98. navQuery->init(navMesh, 2048);
  99. return 0;
  100. }
  101. ~NavMeshContex()
  102. {
  103. if (navQuery != nullptr)
  104. {
  105. dtFreeNavMeshQuery(navQuery);
  106. }
  107. if (navMesh != nullptr)
  108. {
  109. dtFreeNavMesh(navMesh);
  110. }
  111. }
  112. };
  113. NavMesh* NavMesh::instance = nullptr;
  114. NavMesh::NavMesh()
  115. {
  116. }
  117. NavMesh* NavMesh::GetInstace()
  118. {
  119. if (NavMesh::instance == nullptr)
  120. {
  121. NavMesh::instance = new NavMesh();
  122. }
  123. return NavMesh::instance;
  124. }
  125. NavMeshContex* NavMesh::New(int32_t id, const char* buffer, int32_t n)
  126. {
  127. NavMeshContex* navMeshContex = new NavMeshContex();
  128. int32_t ret = navMeshContex->Init(buffer, n);
  129. if (ret != 0)
  130. {
  131. delete navMeshContex;
  132. return nullptr;
  133. }
  134. navMeshContexs[id] = navMeshContex;
  135. return navMeshContex;
  136. }
  137. NavMeshContex* NavMesh::Get(int32_t id)
  138. {
  139. const auto it = navMeshContexs.find(id);
  140. if (it != navMeshContexs.end())
  141. {
  142. return it->second;
  143. }
  144. return nullptr;
  145. }
  146. void NavMesh::Clear()
  147. {
  148. for (auto kv : navMeshContexs)
  149. {
  150. delete kv.second;
  151. }
  152. navMeshContexs.clear();
  153. }
  154. NavMeshContex* RecastLoad(int32_t id, const char* buffer, int32_t n)
  155. {
  156. return NavMesh::GetInstace()->New(id, buffer, n);
  157. }
  158. NavMeshContex* RecastGet(int32_t id)
  159. {
  160. return NavMesh::GetInstace()->Get(id);
  161. }
  162. void RecastClear()
  163. {
  164. NavMesh::GetInstace()->Clear();
  165. }
  166. int32_t RecastFind(NavMeshContex* navMeshContex, float* extents, float* startPos, float* endPos, float* straightPath)
  167. {
  168. //FILE* fp = fopen("./test.log", "wb");
  169. if (navMeshContex == nullptr)
  170. {
  171. return -1;
  172. }
  173. if (startPos == nullptr)
  174. {
  175. return -2;
  176. }
  177. if (endPos == nullptr)
  178. {
  179. return -3;
  180. }
  181. if (straightPath == nullptr)
  182. {
  183. return -4;
  184. }
  185. if (extents == nullptr)
  186. {
  187. return -5;
  188. }
  189. //char ss[200];
  190. //int nn = sprintf(ss, "startPos,%f,%f,%f\n", startPos[0], startPos[1], startPos[2]);
  191. //fwrite(ss, nn, 1, fp);
  192. //fflush(fp);
  193. dtPolyRef startRef = 0;
  194. dtPolyRef endRef = 0;
  195. float startNearestPt[3];
  196. float endNearestPt[3];
  197. dtQueryFilter filter;
  198. filter.setIncludeFlags(0xffff);
  199. filter.setExcludeFlags(0);
  200. navMeshContex->navQuery->findNearestPoly(startPos, extents, &filter, &startRef, startNearestPt);
  201. navMeshContex->navQuery->findNearestPoly(endPos, extents, &filter, &endRef, endNearestPt);
  202. dtPolyRef polys[MAX_POLYS];
  203. int npolys;
  204. unsigned char straightPathFlags[MAX_POLYS];
  205. dtPolyRef straightPathPolys[MAX_POLYS];
  206. int nstraightPath = 0;
  207. navMeshContex->navQuery->findPath(startRef, endRef, startNearestPt, endNearestPt, &filter, polys, &npolys, MAX_POLYS);
  208. if (npolys)
  209. {
  210. float epos1[3];
  211. dtVcopy(epos1, endNearestPt);
  212. if (polys[npolys - 1] != endRef)
  213. {
  214. navMeshContex->navQuery->closestPointOnPoly(polys[npolys - 1], endNearestPt, epos1, 0);
  215. }
  216. navMeshContex->navQuery->findStraightPath(startNearestPt, endNearestPt, polys, npolys, straightPath, straightPathFlags, straightPathPolys, &nstraightPath, MAX_POLYS, DT_STRAIGHTPATH_ALL_CROSSINGS);
  217. }
  218. return nstraightPath;
  219. }
  220. int32_t RecastFindNearestPoint(NavMeshContex* navMeshContex, float* extents, float* startPos, float* nearestPos)
  221. {
  222. if (navMeshContex == nullptr)
  223. {
  224. return -1;
  225. }
  226. if (startPos == nullptr)
  227. {
  228. return -2;
  229. }
  230. if (nearestPos == nullptr)
  231. {
  232. return -3;
  233. }
  234. if (extents == nullptr)
  235. {
  236. return -5;
  237. }
  238. dtPolyRef startRef = 0;
  239. dtQueryFilter filter;
  240. filter.setIncludeFlags(0xffff);
  241. filter.setExcludeFlags(0);
  242. navMeshContex->navQuery->findNearestPoly(startPos, extents, &filter, &startRef, nearestPos);
  243. return startRef;
  244. }
  245. static float frand()
  246. {
  247. // return ((float)(rand() & 0xffff)/(float)0xffff);
  248. return (float)rand() / (float)RAND_MAX;
  249. }
  250. int32_t RecastFindRandomPoint(NavMeshContex* navMeshContex, float* pos)
  251. {
  252. if (navMeshContex == nullptr)
  253. {
  254. return -1;
  255. }
  256. if (pos == nullptr)
  257. {
  258. return -2;
  259. }
  260. dtQueryFilter filter;
  261. filter.setIncludeFlags(0xffff);
  262. filter.setExcludeFlags(0);
  263. dtPolyRef startRef = 0;
  264. return navMeshContex->navQuery->findRandomPoint(&filter, frand, &startRef, pos);
  265. }
  266. int32_t RecastFindRandomPointAroundCircle(NavMeshContex* navMeshContex, float* extents, const float* centerPos, const float maxRadius, float* pos)
  267. {
  268. if (navMeshContex == nullptr)
  269. {
  270. return -1;
  271. }
  272. if (pos == nullptr)
  273. {
  274. return -2;
  275. }
  276. dtQueryFilter filter;
  277. filter.setIncludeFlags(0xffff);
  278. filter.setExcludeFlags(0);
  279. dtPolyRef startRef = 0;
  280. dtPolyRef randomRef = 0;
  281. float startNearestPt[3];
  282. navMeshContex->navQuery->findNearestPoly(centerPos, extents, &filter, &startRef, startNearestPt);
  283. return navMeshContex->navQuery->findRandomPointAroundCircle(startRef, centerPos, maxRadius, &filter, frand, &randomRef, pos);
  284. }