DtNavMeshQuery.cs 141 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458
  1. /*
  2. Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
  3. recast4j copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org
  4. DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com
  5. This software is provided 'as-is', without any express or implied
  6. warranty. In no event will the authors be held liable for any damages
  7. arising from the use of this software.
  8. Permission is granted to anyone to use this software for any purpose,
  9. including commercial applications, and to alter it and redistribute it
  10. freely, subject to the following restrictions:
  11. 1. The origin of this software must not be misrepresented; you must not
  12. claim that you wrote the original software. If you use this software
  13. in a product, an acknowledgment in the product documentation would be
  14. appreciated but is not required.
  15. 2. Altered source versions must be plainly marked as such, and must not be
  16. misrepresented as being the original software.
  17. 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. using System;
  20. using System.Collections.Generic;
  21. using DotRecast.Core;
  22. namespace DotRecast.Detour
  23. {
  24. using static RcMath;
  25. using static DtNode;
  26. public class DtNavMeshQuery
  27. {
  28. /**
  29. * Use raycasts during pathfind to "shortcut" (raycast still consider costs) Options for
  30. * NavMeshQuery::initSlicedFindPath and updateSlicedFindPath
  31. */
  32. public const int DT_FINDPATH_ANY_ANGLE = 0x02;
  33. /** Raycast should calculate movement cost along the ray and fill RaycastHit::cost */
  34. public const int DT_RAYCAST_USE_COSTS = 0x01;
  35. /// Vertex flags returned by findStraightPath.
  36. /** The vertex is the start position in the path. */
  37. public const int DT_STRAIGHTPATH_START = 0x01;
  38. /** The vertex is the end position in the path. */
  39. public const int DT_STRAIGHTPATH_END = 0x02;
  40. /** The vertex is the start of an off-mesh connection. */
  41. public const int DT_STRAIGHTPATH_OFFMESH_CONNECTION = 0x04;
  42. /// Options for findStraightPath.
  43. public const int DT_STRAIGHTPATH_AREA_CROSSINGS = 0x01;
  44. /// < Add a vertex at every polygon edge crossing
  45. /// where area changes.
  46. public const int DT_STRAIGHTPATH_ALL_CROSSINGS = 0x02;
  47. /// < Add a vertex at every polygon edge crossing.
  48. protected readonly DtNavMesh m_nav;
  49. protected readonly DtNodePool m_nodePool;
  50. protected readonly DtNodeQueue m_openList;
  51. protected DtQueryData m_query;
  52. /// < Sliced query state.
  53. public DtNavMeshQuery(DtNavMesh nav)
  54. {
  55. m_nav = nav;
  56. m_nodePool = new DtNodePool();
  57. m_openList = new DtNodeQueue();
  58. }
  59. /// Returns random location on navmesh.
  60. /// Polygons are chosen weighted by area. The search runs in linear related to number of polygon.
  61. /// @param[in] filter The polygon filter to apply to the query.
  62. /// @param[in] frand Function returning a random number [0..1).
  63. /// @param[out] randomRef The reference id of the random location.
  64. /// @param[out] randomPt The random location.
  65. /// @returns The status flags for the query.
  66. public DtStatus FindRandomPoint(IDtQueryFilter filter, IRcRand frand, out long randomRef, out RcVec3f randomPt)
  67. {
  68. randomRef = 0;
  69. randomPt = RcVec3f.Zero;
  70. if (null == filter || null == frand)
  71. {
  72. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  73. }
  74. // Randomly pick one tile. Assume that all tiles cover roughly the same area.
  75. DtMeshTile tile = null;
  76. float tsum = 0.0f;
  77. for (int i = 0; i < m_nav.GetMaxTiles(); i++)
  78. {
  79. DtMeshTile mt = m_nav.GetTile(i);
  80. if (mt == null || mt.data == null || mt.data.header == null)
  81. {
  82. continue;
  83. }
  84. // Choose random tile using reservoir sampling.
  85. float area = 1.0f; // Could be tile area too.
  86. tsum += area;
  87. float u = frand.Next();
  88. if (u * tsum <= area)
  89. {
  90. tile = mt;
  91. }
  92. }
  93. if (tile == null)
  94. {
  95. return DtStatus.DT_FAILURE;
  96. }
  97. // Randomly pick one polygon weighted by polygon area.
  98. DtPoly poly = null;
  99. long polyRef = 0;
  100. long @base = m_nav.GetPolyRefBase(tile);
  101. float areaSum = 0.0f;
  102. for (int i = 0; i < tile.data.header.polyCount; ++i)
  103. {
  104. DtPoly p = tile.data.polys[i];
  105. // Do not return off-mesh connection polygons.
  106. if (p.GetPolyType() != DtPoly.DT_POLYTYPE_GROUND)
  107. {
  108. continue;
  109. }
  110. // Must pass filter
  111. long refs = @base | (long)i;
  112. if (!filter.PassFilter(refs, tile, p))
  113. {
  114. continue;
  115. }
  116. // Calc area of the polygon.
  117. float polyArea = 0.0f;
  118. for (int j = 2; j < p.vertCount; ++j)
  119. {
  120. int va = p.verts[0] * 3;
  121. int vb = p.verts[j - 1] * 3;
  122. int vc = p.verts[j] * 3;
  123. polyArea += DtUtils.TriArea2D(tile.data.verts, va, vb, vc);
  124. }
  125. // Choose random polygon weighted by area, using reservoir sampling.
  126. areaSum += polyArea;
  127. float u = frand.Next();
  128. if (u * areaSum <= polyArea)
  129. {
  130. poly = p;
  131. polyRef = refs;
  132. }
  133. }
  134. if (poly == null)
  135. {
  136. return DtStatus.DT_FAILURE;
  137. }
  138. // Randomly pick point on polygon.
  139. float[] verts = new float[3 * m_nav.GetMaxVertsPerPoly()];
  140. float[] areas = new float[m_nav.GetMaxVertsPerPoly()];
  141. Array.Copy(tile.data.verts, poly.verts[0] * 3, verts, 0, 3);
  142. for (int j = 1; j < poly.vertCount; ++j)
  143. {
  144. Array.Copy(tile.data.verts, poly.verts[j] * 3, verts, j * 3, 3);
  145. }
  146. float s = frand.Next();
  147. float t = frand.Next();
  148. var pt = DtUtils.RandomPointInConvexPoly(verts, poly.vertCount, areas, s, t);
  149. ClosestPointOnPoly(polyRef, pt, out var closest, out var _);
  150. randomRef = polyRef;
  151. randomPt = closest;
  152. return DtStatus.DT_SUCCSESS;
  153. }
  154. /**
  155. * Returns random location on navmesh within the reach of specified location. Polygons are chosen weighted by area.
  156. * The search runs in linear related to number of polygon. The location is not exactly constrained by the circle,
  157. * but it limits the visited polygons.
  158. *
  159. * @param startRef
  160. * The reference id of the polygon where the search starts.
  161. * @param centerPos
  162. * The center of the search circle. [(x, y, z)]
  163. * @param maxRadius
  164. * @param filter
  165. * The polygon filter to apply to the query.
  166. * @param frand
  167. * Function returning a random number [0..1).
  168. * @return Random location
  169. */
  170. public DtStatus FindRandomPointAroundCircle(long startRef, RcVec3f centerPos, float maxRadius,
  171. IDtQueryFilter filter, IRcRand frand, out long randomRef, out RcVec3f randomPt)
  172. {
  173. return FindRandomPointAroundCircle(startRef, centerPos, maxRadius, filter, frand, DtNoOpDtPolygonByCircleConstraint.Shared, out randomRef, out randomPt);
  174. }
  175. /**
  176. * Returns random location on navmesh within the reach of specified location. Polygons are chosen weighted by area.
  177. * The search runs in linear related to number of polygon. The location is strictly constrained by the circle.
  178. *
  179. * @param startRef
  180. * The reference id of the polygon where the search starts.
  181. * @param centerPos
  182. * The center of the search circle. [(x, y, z)]
  183. * @param maxRadius
  184. * @param filter
  185. * The polygon filter to apply to the query.
  186. * @param frand
  187. * Function returning a random number [0..1).
  188. * @return Random location
  189. */
  190. public DtStatus FindRandomPointWithinCircle(long startRef, RcVec3f centerPos, float maxRadius,
  191. IDtQueryFilter filter, IRcRand frand, out long randomRef, out RcVec3f randomPt)
  192. {
  193. return FindRandomPointAroundCircle(startRef, centerPos, maxRadius, filter, frand, DtStrictDtPolygonByCircleConstraint.Shared, out randomRef, out randomPt);
  194. }
  195. public DtStatus FindRandomPointAroundCircle(long startRef, RcVec3f centerPos, float maxRadius,
  196. IDtQueryFilter filter, IRcRand frand, IDtPolygonByCircleConstraint constraint,
  197. out long randomRef, out RcVec3f randomPt)
  198. {
  199. randomRef = startRef;
  200. randomPt = centerPos;
  201. // Validate input
  202. if (!m_nav.IsValidPolyRef(startRef) || !RcVec3f.IsFinite(centerPos) || maxRadius < 0
  203. || !float.IsFinite(maxRadius) || null == filter || null == frand)
  204. {
  205. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  206. }
  207. m_nav.GetTileAndPolyByRefUnsafe(startRef, out var startTile, out var startPoly);
  208. if (!filter.PassFilter(startRef, startTile, startPoly))
  209. {
  210. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  211. }
  212. m_nodePool.Clear();
  213. m_openList.Clear();
  214. DtNode startNode = m_nodePool.GetNode(startRef);
  215. startNode.pos = centerPos;
  216. startNode.pidx = 0;
  217. startNode.cost = 0;
  218. startNode.total = 0;
  219. startNode.id = startRef;
  220. startNode.flags = DT_NODE_OPEN;
  221. m_openList.Push(startNode);
  222. DtStatus status = DtStatus.DT_SUCCSESS;
  223. float radiusSqr = maxRadius * maxRadius;
  224. float areaSum = 0.0f;
  225. DtPoly randomPoly = null;
  226. long randomPolyRef = 0;
  227. float[] randomPolyVerts = null;
  228. while (!m_openList.IsEmpty())
  229. {
  230. DtNode bestNode = m_openList.Pop();
  231. bestNode.flags &= ~DT_NODE_OPEN;
  232. bestNode.flags |= DT_NODE_CLOSED;
  233. // Get poly and tile.
  234. // The API input has been checked already, skip checking internal data.
  235. long bestRef = bestNode.id;
  236. m_nav.GetTileAndPolyByRefUnsafe(bestRef, out var bestTile, out var bestPoly);
  237. // Place random locations on on ground.
  238. if (bestPoly.GetPolyType() == DtPoly.DT_POLYTYPE_GROUND)
  239. {
  240. // Calc area of the polygon.
  241. float polyArea = 0.0f;
  242. float[] polyVerts = new float[bestPoly.vertCount * 3];
  243. for (int j = 0; j < bestPoly.vertCount; ++j)
  244. {
  245. Array.Copy(bestTile.data.verts, bestPoly.verts[j] * 3, polyVerts, j * 3, 3);
  246. }
  247. float[] constrainedVerts = constraint.Apply(polyVerts, centerPos, maxRadius);
  248. if (constrainedVerts != null)
  249. {
  250. int vertCount = constrainedVerts.Length / 3;
  251. for (int j = 2; j < vertCount; ++j)
  252. {
  253. int va = 0;
  254. int vb = (j - 1) * 3;
  255. int vc = j * 3;
  256. polyArea += DtUtils.TriArea2D(constrainedVerts, va, vb, vc);
  257. }
  258. // Choose random polygon weighted by area, using reservoir sampling.
  259. areaSum += polyArea;
  260. float u = frand.Next();
  261. if (u * areaSum <= polyArea)
  262. {
  263. randomPoly = bestPoly;
  264. randomPolyRef = bestRef;
  265. randomPolyVerts = constrainedVerts;
  266. }
  267. }
  268. }
  269. // Get parent poly and tile.
  270. long parentRef = 0;
  271. if (bestNode.pidx != 0)
  272. {
  273. parentRef = m_nodePool.GetNodeAtIdx(bestNode.pidx).id;
  274. }
  275. for (int i = bestTile.polyLinks[bestPoly.index]; i != DtNavMesh.DT_NULL_LINK; i = bestTile.links[i].next)
  276. {
  277. DtLink link = bestTile.links[i];
  278. long neighbourRef = link.refs;
  279. // Skip invalid neighbours and do not follow back to parent.
  280. if (neighbourRef == 0 || neighbourRef == parentRef)
  281. {
  282. continue;
  283. }
  284. // Expand to neighbour
  285. m_nav.GetTileAndPolyByRefUnsafe(neighbourRef, out var neighbourTile, out var neighbourPoly);
  286. // Do not advance if the polygon is excluded by the filter.
  287. if (!filter.PassFilter(neighbourRef, neighbourTile, neighbourPoly))
  288. {
  289. continue;
  290. }
  291. // Find edge and calc distance to the edge.
  292. var ppStatus = GetPortalPoints(bestRef, bestPoly, bestTile, neighbourRef,
  293. neighbourPoly, neighbourTile, out var va, out var vb);
  294. if (ppStatus.Failed())
  295. {
  296. continue;
  297. }
  298. // If the circle is not touching the next polygon, skip it.
  299. var distSqr = DtUtils.DistancePtSegSqr2D(centerPos, va, vb, out var tesg);
  300. if (distSqr > radiusSqr)
  301. {
  302. continue;
  303. }
  304. DtNode neighbourNode = m_nodePool.GetNode(neighbourRef);
  305. if (null == neighbourNode)
  306. {
  307. status |= DtStatus.DT_OUT_OF_NODES;
  308. continue;
  309. }
  310. if ((neighbourNode.flags & DtNode.DT_NODE_CLOSED) != 0)
  311. {
  312. continue;
  313. }
  314. // Cost
  315. if (neighbourNode.flags == 0)
  316. {
  317. neighbourNode.pos = RcVec3f.Lerp(va, vb, 0.5f);
  318. }
  319. float total = bestNode.total + RcVec3f.Distance(bestNode.pos, neighbourNode.pos);
  320. // The node is already in open list and the new result is worse, skip.
  321. if ((neighbourNode.flags & DtNode.DT_NODE_OPEN) != 0 && total >= neighbourNode.total)
  322. {
  323. continue;
  324. }
  325. neighbourNode.id = neighbourRef;
  326. neighbourNode.flags = (neighbourNode.flags & ~DtNode.DT_NODE_CLOSED);
  327. neighbourNode.pidx = m_nodePool.GetNodeIdx(bestNode);
  328. neighbourNode.total = total;
  329. if ((neighbourNode.flags & DtNode.DT_NODE_OPEN) != 0)
  330. {
  331. m_openList.Modify(neighbourNode);
  332. }
  333. else
  334. {
  335. neighbourNode.flags = DtNode.DT_NODE_OPEN;
  336. m_openList.Push(neighbourNode);
  337. }
  338. }
  339. }
  340. if (randomPoly == null)
  341. {
  342. return DtStatus.DT_FAILURE;
  343. }
  344. // Randomly pick point on polygon.
  345. float s = frand.Next();
  346. float t = frand.Next();
  347. float[] areas = new float[randomPolyVerts.Length / 3];
  348. RcVec3f pt = DtUtils.RandomPointInConvexPoly(randomPolyVerts, randomPolyVerts.Length / 3, areas, s, t);
  349. ClosestPointOnPoly(randomPolyRef, pt, out var closest, out var _);
  350. randomRef = randomPolyRef;
  351. randomPt = closest;
  352. return status;
  353. }
  354. //////////////////////////////////////////////////////////////////////////////////////////
  355. /// @par
  356. ///
  357. /// Uses the detail polygons to find the surface height. (Most accurate.)
  358. ///
  359. /// @p pos does not have to be within the bounds of the polygon or navigation mesh.
  360. ///
  361. /// See ClosestPointOnPolyBoundary() for a limited but faster option.
  362. ///
  363. /// Finds the closest point on the specified polygon.
  364. /// @param[in] ref The reference id of the polygon.
  365. /// @param[in] pos The position to check. [(x, y, z)]
  366. /// @param[out] closest
  367. /// @param[out] posOverPoly
  368. /// @returns The status flags for the query.
  369. public DtStatus ClosestPointOnPoly(long refs, RcVec3f pos, out RcVec3f closest, out bool posOverPoly)
  370. {
  371. closest = pos;
  372. posOverPoly = false;
  373. if (!m_nav.IsValidPolyRef(refs) || !RcVec3f.IsFinite(pos))
  374. {
  375. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  376. }
  377. m_nav.ClosestPointOnPoly(refs, pos, out closest, out posOverPoly);
  378. return DtStatus.DT_SUCCSESS;
  379. }
  380. /// @par
  381. ///
  382. /// Much faster than ClosestPointOnPoly().
  383. ///
  384. /// If the provided position lies within the polygon's xz-bounds (above or below),
  385. /// then @p pos and @p closest will be equal.
  386. ///
  387. /// The height of @p closest will be the polygon boundary. The height detail is not used.
  388. ///
  389. /// @p pos does not have to be within the bounds of the polybon or the navigation mesh.
  390. ///
  391. /// Returns a point on the boundary closest to the source point if the source point is outside the
  392. /// polygon's xz-bounds.
  393. /// @param[in] ref The reference id to the polygon.
  394. /// @param[in] pos The position to check. [(x, y, z)]
  395. /// @param[out] closest The closest point. [(x, y, z)]
  396. /// @returns The status flags for the query.
  397. public DtStatus ClosestPointOnPolyBoundary(long refs, RcVec3f pos, out RcVec3f closest)
  398. {
  399. closest = pos;
  400. var status = m_nav.GetTileAndPolyByRef(refs, out var tile, out var poly);
  401. if (status.Failed())
  402. {
  403. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  404. }
  405. if (tile == null || !RcVec3f.IsFinite(pos))
  406. {
  407. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  408. }
  409. // Collect vertices.
  410. float[] verts = new float[m_nav.GetMaxVertsPerPoly() * 3];
  411. float[] edged = new float[m_nav.GetMaxVertsPerPoly()];
  412. float[] edget = new float[m_nav.GetMaxVertsPerPoly()];
  413. int nv = poly.vertCount;
  414. for (int i = 0; i < nv; ++i)
  415. {
  416. Array.Copy(tile.data.verts, poly.verts[i] * 3, verts, i * 3, 3);
  417. }
  418. if (DtUtils.DistancePtPolyEdgesSqr(pos, verts, nv, edged, edget))
  419. {
  420. closest = pos;
  421. }
  422. else
  423. {
  424. // Point is outside the polygon, dtClamp to nearest edge.
  425. float dmin = edged[0];
  426. int imin = 0;
  427. for (int i = 1; i < nv; ++i)
  428. {
  429. if (edged[i] < dmin)
  430. {
  431. dmin = edged[i];
  432. imin = i;
  433. }
  434. }
  435. int va = imin * 3;
  436. int vb = ((imin + 1) % nv) * 3;
  437. closest = RcVec3f.Lerp(verts, va, vb, edget[imin]);
  438. }
  439. return DtStatus.DT_SUCCSESS;
  440. }
  441. /// @par
  442. ///
  443. /// Will return #DT_FAILURE if the provided position is outside the xz-bounds
  444. /// of the polygon.
  445. ///
  446. /// Gets the height of the polygon at the provided position using the height detail. (Most accurate.)
  447. /// @param[in] ref The reference id of the polygon.
  448. /// @param[in] pos A position within the xz-bounds of the polygon. [(x, y, z)]
  449. /// @param[out] height The height at the surface of the polygon.
  450. /// @returns The status flags for the query.
  451. public DtStatus GetPolyHeight(long refs, RcVec3f pos, out float height)
  452. {
  453. height = default;
  454. var status = m_nav.GetTileAndPolyByRef(refs, out var tile, out var poly);
  455. if (status.Failed())
  456. {
  457. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  458. }
  459. if (!RcVec3f.IsFinite2D(pos))
  460. {
  461. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  462. }
  463. // We used to return success for offmesh connections, but the
  464. // getPolyHeight in DetourNavMesh does not do this, so special
  465. // case it here.
  466. if (poly.GetPolyType() == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION)
  467. {
  468. int i = poly.verts[0] * 3;
  469. var v0 = new RcVec3f { x = tile.data.verts[i], y = tile.data.verts[i + 1], z = tile.data.verts[i + 2] };
  470. i = poly.verts[1] * 3;
  471. var v1 = new RcVec3f { x = tile.data.verts[i], y = tile.data.verts[i + 1], z = tile.data.verts[i + 2] };
  472. DtUtils.DistancePtSegSqr2D(pos, v0, v1, out var t);
  473. height = v0.y + (v1.y - v0.y) * t;
  474. return DtStatus.DT_SUCCSESS;
  475. }
  476. if (!m_nav.GetPolyHeight(tile, poly, pos, out var h))
  477. {
  478. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  479. }
  480. height = h;
  481. return DtStatus.DT_SUCCSESS;
  482. }
  483. /// Finds the polygon nearest to the specified center point.
  484. /// [opt] means the specified parameter can be a null pointer, in that case the output parameter will not be set.
  485. ///
  486. /// @param[in] center The center of the search box. [(x, y, z)]
  487. /// @param[in] halfExtents The search distance along each axis. [(x, y, z)]
  488. /// @param[in] filter The polygon filter to apply to the query.
  489. /// @param[out] nearestRef The reference id of the nearest polygon. Will be set to 0 if no polygon is found.
  490. /// @param[out] nearestPt The nearest point on the polygon. Unchanged if no polygon is found. [opt] [(x, y, z)]
  491. /// @param[out] isOverPoly Set to true if the point's X/Z coordinate lies inside the polygon, false otherwise. Unchanged if no polygon is found. [opt]
  492. /// @returns The status flags for the query.
  493. public DtStatus FindNearestPoly(RcVec3f center, RcVec3f halfExtents, IDtQueryFilter filter,
  494. out long nearestRef, out RcVec3f nearestPt, out bool isOverPoly)
  495. {
  496. nearestRef = 0;
  497. nearestPt = center;
  498. isOverPoly = false;
  499. // Get nearby polygons from proximity grid.
  500. DtFindNearestPolyQuery query = new DtFindNearestPolyQuery(this, center);
  501. DtStatus status = QueryPolygons(center, halfExtents, filter, query);
  502. if (status.Failed())
  503. {
  504. return status;
  505. }
  506. nearestRef = query.NearestRef();
  507. nearestPt = query.NearestPt();
  508. isOverPoly = query.OverPoly();
  509. return DtStatus.DT_SUCCSESS;
  510. }
  511. // FIXME: (PP) duplicate?
  512. protected void QueryPolygonsInTile(DtMeshTile tile, RcVec3f qmin, RcVec3f qmax, IDtQueryFilter filter, IDtPolyQuery query)
  513. {
  514. if (tile.data.bvTree != null)
  515. {
  516. int nodeIndex = 0;
  517. var tbmin = tile.data.header.bmin;
  518. var tbmax = tile.data.header.bmax;
  519. float qfac = tile.data.header.bvQuantFactor;
  520. // Calculate quantized box
  521. int[] bmin = new int[3];
  522. int[] bmax = new int[3];
  523. // dtClamp query box to world box.
  524. float minx = Clamp(qmin.x, tbmin.x, tbmax.x) - tbmin.x;
  525. float miny = Clamp(qmin.y, tbmin.y, tbmax.y) - tbmin.y;
  526. float minz = Clamp(qmin.z, tbmin.z, tbmax.z) - tbmin.z;
  527. float maxx = Clamp(qmax.x, tbmin.x, tbmax.x) - tbmin.x;
  528. float maxy = Clamp(qmax.y, tbmin.y, tbmax.y) - tbmin.y;
  529. float maxz = Clamp(qmax.z, tbmin.z, tbmax.z) - tbmin.z;
  530. // Quantize
  531. bmin[0] = (int)(qfac * minx) & 0x7ffffffe;
  532. bmin[1] = (int)(qfac * miny) & 0x7ffffffe;
  533. bmin[2] = (int)(qfac * minz) & 0x7ffffffe;
  534. bmax[0] = (int)(qfac * maxx + 1) | 1;
  535. bmax[1] = (int)(qfac * maxy + 1) | 1;
  536. bmax[2] = (int)(qfac * maxz + 1) | 1;
  537. // Traverse tree
  538. long @base = m_nav.GetPolyRefBase(tile);
  539. int end = tile.data.header.bvNodeCount;
  540. while (nodeIndex < end)
  541. {
  542. DtBVNode node = tile.data.bvTree[nodeIndex];
  543. bool overlap = DtUtils.OverlapQuantBounds(bmin, bmax, node.bmin, node.bmax);
  544. bool isLeafNode = node.i >= 0;
  545. if (isLeafNode && overlap)
  546. {
  547. long refs = @base | (long)node.i;
  548. if (filter.PassFilter(refs, tile, tile.data.polys[node.i]))
  549. {
  550. query.Process(tile, tile.data.polys[node.i], refs);
  551. }
  552. }
  553. if (overlap || isLeafNode)
  554. {
  555. nodeIndex++;
  556. }
  557. else
  558. {
  559. int escapeIndex = -node.i;
  560. nodeIndex += escapeIndex;
  561. }
  562. }
  563. }
  564. else
  565. {
  566. RcVec3f bmin = new RcVec3f();
  567. RcVec3f bmax = new RcVec3f();
  568. long @base = m_nav.GetPolyRefBase(tile);
  569. for (int i = 0; i < tile.data.header.polyCount; ++i)
  570. {
  571. DtPoly p = tile.data.polys[i];
  572. // Do not return off-mesh connection polygons.
  573. if (p.GetPolyType() == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION)
  574. {
  575. continue;
  576. }
  577. long refs = @base | (long)i;
  578. if (!filter.PassFilter(refs, tile, p))
  579. {
  580. continue;
  581. }
  582. // Calc polygon bounds.
  583. int v = p.verts[0] * 3;
  584. bmin.Set(tile.data.verts, v);
  585. bmax.Set(tile.data.verts, v);
  586. for (int j = 1; j < p.vertCount; ++j)
  587. {
  588. v = p.verts[j] * 3;
  589. bmin.Min(tile.data.verts, v);
  590. bmax.Max(tile.data.verts, v);
  591. }
  592. if (DtUtils.OverlapBounds(qmin, qmax, bmin, bmax))
  593. {
  594. query.Process(tile, p, refs);
  595. }
  596. }
  597. }
  598. }
  599. /**
  600. * Finds polygons that overlap the search box.
  601. *
  602. * If no polygons are found, the function will return with a polyCount of zero.
  603. *
  604. * @param center
  605. * The center of the search box. [(x, y, z)]
  606. * @param halfExtents
  607. * The search distance along each axis. [(x, y, z)]
  608. * @param filter
  609. * The polygon filter to apply to the query.
  610. * @return The reference ids of the polygons that overlap the query box.
  611. */
  612. public DtStatus QueryPolygons(RcVec3f center, RcVec3f halfExtents, IDtQueryFilter filter, IDtPolyQuery query)
  613. {
  614. if (!RcVec3f.IsFinite(center) || !RcVec3f.IsFinite(halfExtents) || null == filter)
  615. {
  616. return DtStatus.DT_INVALID_PARAM;
  617. }
  618. // Find tiles the query touches.
  619. RcVec3f bmin = center.Subtract(halfExtents);
  620. RcVec3f bmax = center.Add(halfExtents);
  621. foreach (var t in QueryTiles(center, halfExtents))
  622. {
  623. QueryPolygonsInTile(t, bmin, bmax, filter, query);
  624. }
  625. return DtStatus.DT_SUCCSESS;
  626. }
  627. /**
  628. * Finds tiles that overlap the search box.
  629. */
  630. public IList<DtMeshTile> QueryTiles(RcVec3f center, RcVec3f halfExtents)
  631. {
  632. if (!RcVec3f.IsFinite(center) || !RcVec3f.IsFinite(halfExtents))
  633. {
  634. return RcImmutableArray<DtMeshTile>.Empty;
  635. }
  636. RcVec3f bmin = center.Subtract(halfExtents);
  637. RcVec3f bmax = center.Add(halfExtents);
  638. m_nav.CalcTileLoc(bmin, out var minx, out var miny);
  639. m_nav.CalcTileLoc(bmax, out var maxx, out var maxy);
  640. List<DtMeshTile> tiles = new List<DtMeshTile>();
  641. for (int y = miny; y <= maxy; ++y)
  642. {
  643. for (int x = minx; x <= maxx; ++x)
  644. {
  645. tiles.AddRange(m_nav.GetTilesAt(x, y));
  646. }
  647. }
  648. return tiles;
  649. }
  650. /**
  651. * Finds a path from the start polygon to the end polygon.
  652. *
  653. * If the end polygon cannot be reached through the navigation graph, the last polygon in the path will be the
  654. * nearest the end polygon.
  655. *
  656. * The start and end positions are used to calculate traversal costs. (The y-values impact the result.)
  657. *
  658. * @param startRef
  659. * The reference id of the start polygon.
  660. * @param endRef
  661. * The reference id of the end polygon.
  662. * @param startPos
  663. * A position within the start polygon. [(x, y, z)]
  664. * @param endPos
  665. * A position within the end polygon. [(x, y, z)]
  666. * @param filter
  667. * The polygon filter to apply to the query.
  668. * @return Found path
  669. */
  670. public DtStatus FindPath(long startRef, long endRef, RcVec3f startPos, RcVec3f endPos, IDtQueryFilter filter, ref List<long> path, DtFindPathOption fpo)
  671. {
  672. if (null == path)
  673. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  674. path.Clear();
  675. // Validate input
  676. if (!m_nav.IsValidPolyRef(startRef) || !m_nav.IsValidPolyRef(endRef) || !RcVec3f.IsFinite(startPos) || !RcVec3f.IsFinite(endPos) || null == filter)
  677. {
  678. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  679. }
  680. var heuristic = fpo.heuristic;
  681. var raycastLimit = fpo.raycastLimit;
  682. var options = fpo.options;
  683. float raycastLimitSqr = Sqr(raycastLimit);
  684. // trade quality with performance?
  685. if ((options & DT_FINDPATH_ANY_ANGLE) != 0 && raycastLimit < 0f)
  686. {
  687. // limiting to several times the character radius yields nice results. It is not sensitive
  688. // so it is enough to compute it from the first tile.
  689. DtMeshTile tile = m_nav.GetTileByRef(startRef);
  690. float agentRadius = tile.data.header.walkableRadius;
  691. raycastLimitSqr = Sqr(agentRadius * DtNavMesh.DT_RAY_CAST_LIMIT_PROPORTIONS);
  692. }
  693. if (startRef == endRef)
  694. {
  695. path.Add(startRef);
  696. return DtStatus.DT_SUCCSESS;
  697. }
  698. m_nodePool.Clear();
  699. m_openList.Clear();
  700. DtNode startNode = m_nodePool.GetNode(startRef);
  701. startNode.pos = startPos;
  702. startNode.pidx = 0;
  703. startNode.cost = 0;
  704. startNode.total = heuristic.GetCost(startPos, endPos);
  705. startNode.id = startRef;
  706. startNode.flags = DtNode.DT_NODE_OPEN;
  707. m_openList.Push(startNode);
  708. DtNode lastBestNode = startNode;
  709. float lastBestNodeCost = startNode.total;
  710. while (!m_openList.IsEmpty())
  711. {
  712. // Remove node from open list and put it in closed list.
  713. DtNode bestNode = m_openList.Pop();
  714. bestNode.flags &= ~DtNode.DT_NODE_OPEN;
  715. bestNode.flags |= DtNode.DT_NODE_CLOSED;
  716. // Reached the goal, stop searching.
  717. if (bestNode.id == endRef)
  718. {
  719. lastBestNode = bestNode;
  720. break;
  721. }
  722. // Get current poly and tile.
  723. // The API input has been checked already, skip checking internal data.
  724. long bestRef = bestNode.id;
  725. m_nav.GetTileAndPolyByRefUnsafe(bestRef, out var bestTile, out var bestPoly);
  726. // Get parent poly and tile.
  727. long parentRef = 0, grandpaRef = 0;
  728. DtMeshTile parentTile = null;
  729. DtPoly parentPoly = null;
  730. DtNode parentNode = null;
  731. if (bestNode.pidx != 0)
  732. {
  733. parentNode = m_nodePool.GetNodeAtIdx(bestNode.pidx);
  734. parentRef = parentNode.id;
  735. if (parentNode.pidx != 0)
  736. {
  737. grandpaRef = m_nodePool.GetNodeAtIdx(parentNode.pidx).id;
  738. }
  739. }
  740. if (parentRef != 0)
  741. {
  742. m_nav.GetTileAndPolyByRefUnsafe(parentRef, out parentTile, out parentPoly);
  743. }
  744. // decide whether to test raycast to previous nodes
  745. bool tryLOS = false;
  746. if ((options & DT_FINDPATH_ANY_ANGLE) != 0)
  747. {
  748. if ((parentRef != 0) && (raycastLimitSqr >= float.MaxValue
  749. || RcVec3f.DistSqr(parentNode.pos, bestNode.pos) < raycastLimitSqr))
  750. {
  751. tryLOS = true;
  752. }
  753. }
  754. for (int i = bestTile.polyLinks[bestPoly.index]; i != DtNavMesh.DT_NULL_LINK; i = bestTile.links[i].next)
  755. {
  756. long neighbourRef = bestTile.links[i].refs;
  757. // Skip invalid ids and do not expand back to where we came from.
  758. if (neighbourRef == 0 || neighbourRef == parentRef)
  759. {
  760. continue;
  761. }
  762. // Get neighbour poly and tile.
  763. // The API input has been checked already, skip checking internal data.
  764. m_nav.GetTileAndPolyByRefUnsafe(neighbourRef, out var neighbourTile, out var neighbourPoly);
  765. if (!filter.PassFilter(neighbourRef, neighbourTile, neighbourPoly))
  766. {
  767. continue;
  768. }
  769. // get the node
  770. DtNode neighbourNode = m_nodePool.GetNode(neighbourRef, 0);
  771. // do not expand to nodes that were already visited from the
  772. // same parent
  773. if (neighbourNode.pidx != 0 && neighbourNode.pidx == bestNode.pidx)
  774. {
  775. continue;
  776. }
  777. // If the node is visited the first time, calculate node position.
  778. var neighbourPos = neighbourNode.pos;
  779. var empStatus = neighbourRef == endRef
  780. ? GetEdgeIntersectionPoint(bestNode.pos, bestRef, bestPoly, bestTile,
  781. endPos, neighbourRef, neighbourPoly, neighbourTile,
  782. ref neighbourPos)
  783. : GetEdgeMidPoint(bestRef, bestPoly, bestTile,
  784. neighbourRef, neighbourPoly, neighbourTile,
  785. ref neighbourPos);
  786. // Calculate cost and heuristic.
  787. float cost = 0;
  788. float heuristicCost = 0;
  789. // raycast parent
  790. bool foundShortCut = false;
  791. List<long> shortcut = null;
  792. if (tryLOS)
  793. {
  794. var rayStatus = Raycast(parentRef, parentNode.pos, neighbourPos, filter,
  795. DT_RAYCAST_USE_COSTS, grandpaRef, out var rayHit);
  796. if (rayStatus.Succeeded())
  797. {
  798. foundShortCut = rayHit.t >= 1.0f;
  799. if (foundShortCut)
  800. {
  801. shortcut = rayHit.path;
  802. // shortcut found using raycast. Using shorter cost
  803. // instead
  804. cost = parentNode.cost + rayHit.pathCost;
  805. }
  806. }
  807. }
  808. // update move cost
  809. if (!foundShortCut)
  810. {
  811. float curCost = filter.GetCost(bestNode.pos, neighbourPos, parentRef, parentTile,
  812. parentPoly, bestRef, bestTile, bestPoly, neighbourRef, neighbourTile, neighbourPoly);
  813. cost = bestNode.cost + curCost;
  814. }
  815. // Special case for last node.
  816. if (neighbourRef == endRef)
  817. {
  818. // Cost
  819. float endCost = filter.GetCost(neighbourPos, endPos, bestRef, bestTile, bestPoly, neighbourRef,
  820. neighbourTile, neighbourPoly, 0L, null, null);
  821. cost = cost + endCost;
  822. }
  823. else
  824. {
  825. // Cost
  826. heuristicCost = heuristic.GetCost(neighbourPos, endPos);
  827. }
  828. float total = cost + heuristicCost;
  829. // The node is already in open list and the new result is worse, skip.
  830. if ((neighbourNode.flags & DtNode.DT_NODE_OPEN) != 0 && total >= neighbourNode.total)
  831. {
  832. continue;
  833. }
  834. // The node is already visited and process, and the new result is worse, skip.
  835. if ((neighbourNode.flags & DtNode.DT_NODE_CLOSED) != 0 && total >= neighbourNode.total)
  836. {
  837. continue;
  838. }
  839. // Add or update the node.
  840. neighbourNode.pidx = foundShortCut ? bestNode.pidx : m_nodePool.GetNodeIdx(bestNode);
  841. neighbourNode.id = neighbourRef;
  842. neighbourNode.flags = (neighbourNode.flags & ~DtNode.DT_NODE_CLOSED);
  843. neighbourNode.cost = cost;
  844. neighbourNode.total = total;
  845. neighbourNode.pos = neighbourPos;
  846. neighbourNode.shortcut = shortcut;
  847. if ((neighbourNode.flags & DtNode.DT_NODE_OPEN) != 0)
  848. {
  849. // Already in open, update node location.
  850. m_openList.Modify(neighbourNode);
  851. }
  852. else
  853. {
  854. // Put the node in open list.
  855. neighbourNode.flags |= DtNode.DT_NODE_OPEN;
  856. m_openList.Push(neighbourNode);
  857. }
  858. // Update nearest node to target so far.
  859. if (heuristicCost < lastBestNodeCost)
  860. {
  861. lastBestNodeCost = heuristicCost;
  862. lastBestNode = neighbourNode;
  863. }
  864. }
  865. }
  866. var status = GetPathToNode(lastBestNode, ref path);
  867. if (lastBestNode.id != endRef)
  868. {
  869. status |= DtStatus.DT_PARTIAL_RESULT;
  870. }
  871. return status;
  872. }
  873. /**
  874. * Intializes a sliced path query.
  875. *
  876. * Common use case: -# Call InitSlicedFindPath() to initialize the sliced path query. -# Call UpdateSlicedFindPath()
  877. * until it returns complete. -# Call FinalizeSlicedFindPath() to get the path.
  878. *
  879. * @param startRef
  880. * The reference id of the start polygon.
  881. * @param endRef
  882. * The reference id of the end polygon.
  883. * @param startPos
  884. * A position within the start polygon. [(x, y, z)]
  885. * @param endPos
  886. * A position within the end polygon. [(x, y, z)]
  887. * @param filter
  888. * The polygon filter to apply to the query.
  889. * @param options
  890. * query options (see: #FindPathOptions)
  891. * @return
  892. */
  893. public DtStatus InitSlicedFindPath(long startRef, long endRef, RcVec3f startPos, RcVec3f endPos, IDtQueryFilter filter, int options)
  894. {
  895. return InitSlicedFindPath(startRef, endRef, startPos, endPos, filter, options, DefaultQueryHeuristic.Default, -1.0f);
  896. }
  897. public DtStatus InitSlicedFindPath(long startRef, long endRef, RcVec3f startPos, RcVec3f endPos, IDtQueryFilter filter, int options, float raycastLimit)
  898. {
  899. return InitSlicedFindPath(startRef, endRef, startPos, endPos, filter, options, DefaultQueryHeuristic.Default, raycastLimit);
  900. }
  901. public DtStatus InitSlicedFindPath(long startRef, long endRef, RcVec3f startPos, RcVec3f endPos, IDtQueryFilter filter, int options, IQueryHeuristic heuristic, float raycastLimit)
  902. {
  903. // Init path state.
  904. m_query = new DtQueryData();
  905. m_query.status = DtStatus.DT_FAILURE;
  906. m_query.startRef = startRef;
  907. m_query.endRef = endRef;
  908. m_query.startPos = startPos;
  909. m_query.endPos = endPos;
  910. m_query.filter = filter;
  911. m_query.options = options;
  912. m_query.heuristic = heuristic;
  913. m_query.raycastLimitSqr = Sqr(raycastLimit);
  914. // Validate input
  915. if (!m_nav.IsValidPolyRef(startRef) || !m_nav.IsValidPolyRef(endRef) || !RcVec3f.IsFinite(startPos) || !RcVec3f.IsFinite(endPos) || null == filter)
  916. {
  917. return DtStatus.DT_INVALID_PARAM;
  918. }
  919. // trade quality with performance?
  920. if ((options & DT_FINDPATH_ANY_ANGLE) != 0 && raycastLimit < 0f)
  921. {
  922. // limiting to several times the character radius yields nice results. It is not sensitive
  923. // so it is enough to compute it from the first tile.
  924. DtMeshTile tile = m_nav.GetTileByRef(startRef);
  925. float agentRadius = tile.data.header.walkableRadius;
  926. m_query.raycastLimitSqr = Sqr(agentRadius * DtNavMesh.DT_RAY_CAST_LIMIT_PROPORTIONS);
  927. }
  928. if (startRef == endRef)
  929. {
  930. m_query.status = DtStatus.DT_SUCCSESS;
  931. return DtStatus.DT_SUCCSESS;
  932. }
  933. m_nodePool.Clear();
  934. m_openList.Clear();
  935. DtNode startNode = m_nodePool.GetNode(startRef);
  936. startNode.pos = startPos;
  937. startNode.pidx = 0;
  938. startNode.cost = 0;
  939. startNode.total = heuristic.GetCost(startPos, endPos);
  940. startNode.id = startRef;
  941. startNode.flags = DtNode.DT_NODE_OPEN;
  942. m_openList.Push(startNode);
  943. m_query.status = DtStatus.DT_IN_PROGRESS;
  944. m_query.lastBestNode = startNode;
  945. m_query.lastBestNodeCost = startNode.total;
  946. return m_query.status;
  947. }
  948. /**
  949. * Updates an in-progress sliced path query.
  950. *
  951. * @param maxIter
  952. * The maximum number of iterations to perform.
  953. * @return The status flags for the query.
  954. */
  955. public virtual DtStatus UpdateSlicedFindPath(int maxIter, out int doneIters)
  956. {
  957. doneIters = 0;
  958. if (!m_query.status.InProgress())
  959. {
  960. return m_query.status;
  961. }
  962. // Make sure the request is still valid.
  963. if (!m_nav.IsValidPolyRef(m_query.startRef) || !m_nav.IsValidPolyRef(m_query.endRef))
  964. {
  965. m_query.status = DtStatus.DT_FAILURE;
  966. return DtStatus.DT_FAILURE;
  967. }
  968. int iter = 0;
  969. while (iter < maxIter && !m_openList.IsEmpty())
  970. {
  971. iter++;
  972. // Remove node from open list and put it in closed list.
  973. DtNode bestNode = m_openList.Pop();
  974. bestNode.flags &= ~DtNode.DT_NODE_OPEN;
  975. bestNode.flags |= DtNode.DT_NODE_CLOSED;
  976. // Reached the goal, stop searching.
  977. if (bestNode.id == m_query.endRef)
  978. {
  979. m_query.lastBestNode = bestNode;
  980. var details = m_query.status & DtStatus.DT_STATUS_DETAIL_MASK;
  981. m_query.status = DtStatus.DT_SUCCSESS | details;
  982. doneIters = iter;
  983. return m_query.status;
  984. }
  985. // Get current poly and tile.
  986. // The API input has been checked already, skip checking internal
  987. // data.
  988. long bestRef = bestNode.id;
  989. var status = m_nav.GetTileAndPolyByRef(bestRef, out var bestTile, out var bestPoly);
  990. if (status.Failed())
  991. {
  992. // The polygon has disappeared during the sliced query, fail.
  993. m_query.status = DtStatus.DT_FAILURE;
  994. doneIters = iter;
  995. return m_query.status;
  996. }
  997. // Get parent and grand parent poly and tile.
  998. long parentRef = 0, grandpaRef = 0;
  999. DtMeshTile parentTile = null;
  1000. DtPoly parentPoly = null;
  1001. DtNode parentNode = null;
  1002. if (bestNode.pidx != 0)
  1003. {
  1004. parentNode = m_nodePool.GetNodeAtIdx(bestNode.pidx);
  1005. parentRef = parentNode.id;
  1006. if (parentNode.pidx != 0)
  1007. {
  1008. grandpaRef = m_nodePool.GetNodeAtIdx(parentNode.pidx).id;
  1009. }
  1010. }
  1011. if (parentRef != 0)
  1012. {
  1013. bool invalidParent = false;
  1014. status = m_nav.GetTileAndPolyByRef(parentRef, out parentTile, out parentPoly);
  1015. invalidParent = status.Failed();
  1016. if (invalidParent || (grandpaRef != 0 && !m_nav.IsValidPolyRef(grandpaRef)))
  1017. {
  1018. // The polygon has disappeared during the sliced query fail.
  1019. m_query.status = DtStatus.DT_FAILURE;
  1020. doneIters = iter;
  1021. return m_query.status;
  1022. }
  1023. }
  1024. // decide whether to test raycast to previous nodes
  1025. bool tryLOS = false;
  1026. if ((m_query.options & DT_FINDPATH_ANY_ANGLE) != 0)
  1027. {
  1028. if ((parentRef != 0) && (m_query.raycastLimitSqr >= float.MaxValue
  1029. || RcVec3f.DistSqr(parentNode.pos, bestNode.pos) < m_query.raycastLimitSqr))
  1030. {
  1031. tryLOS = true;
  1032. }
  1033. }
  1034. for (int i = bestTile.polyLinks[bestPoly.index]; i != DtNavMesh.DT_NULL_LINK; i = bestTile.links[i].next)
  1035. {
  1036. long neighbourRef = bestTile.links[i].refs;
  1037. // Skip invalid ids and do not expand back to where we came
  1038. // from.
  1039. if (neighbourRef == 0 || neighbourRef == parentRef)
  1040. {
  1041. continue;
  1042. }
  1043. // Get neighbour poly and tile.
  1044. // The API input has been checked already, skip checking internal
  1045. // data.
  1046. m_nav.GetTileAndPolyByRefUnsafe(neighbourRef, out var neighbourTile, out var neighbourPoly);
  1047. if (!m_query.filter.PassFilter(neighbourRef, neighbourTile, neighbourPoly))
  1048. {
  1049. continue;
  1050. }
  1051. // get the neighbor node
  1052. DtNode neighbourNode = m_nodePool.GetNode(neighbourRef, 0);
  1053. // do not expand to nodes that were already visited from the
  1054. // same parent
  1055. if (neighbourNode.pidx != 0 && neighbourNode.pidx == bestNode.pidx)
  1056. {
  1057. continue;
  1058. }
  1059. // If the node is visited the first time, calculate node
  1060. // position.
  1061. var neighbourPos = neighbourNode.pos;
  1062. var empStatus = neighbourRef == m_query.endRef
  1063. ? GetEdgeIntersectionPoint(bestNode.pos, bestRef, bestPoly, bestTile,
  1064. m_query.endPos, neighbourRef, neighbourPoly, neighbourTile,
  1065. ref neighbourPos)
  1066. : GetEdgeMidPoint(bestRef, bestPoly, bestTile,
  1067. neighbourRef, neighbourPoly, neighbourTile,
  1068. ref neighbourPos);
  1069. // Calculate cost and heuristic.
  1070. float cost = 0;
  1071. float heuristic = 0;
  1072. // raycast parent
  1073. bool foundShortCut = false;
  1074. List<long> shortcut = null;
  1075. if (tryLOS)
  1076. {
  1077. status = Raycast(parentRef, parentNode.pos, neighbourPos, m_query.filter, DT_RAYCAST_USE_COSTS, grandpaRef, out var rayHit);
  1078. if (status.Succeeded())
  1079. {
  1080. foundShortCut = rayHit.t >= 1.0f;
  1081. if (foundShortCut)
  1082. {
  1083. shortcut = rayHit.path;
  1084. // shortcut found using raycast. Using shorter cost
  1085. // instead
  1086. cost = parentNode.cost + rayHit.pathCost;
  1087. }
  1088. }
  1089. }
  1090. // update move cost
  1091. if (!foundShortCut)
  1092. {
  1093. // No shortcut found.
  1094. float curCost = m_query.filter.GetCost(bestNode.pos, neighbourPos, parentRef, parentTile,
  1095. parentPoly, bestRef, bestTile, bestPoly, neighbourRef, neighbourTile, neighbourPoly);
  1096. cost = bestNode.cost + curCost;
  1097. }
  1098. // Special case for last node.
  1099. if (neighbourRef == m_query.endRef)
  1100. {
  1101. float endCost = m_query.filter.GetCost(neighbourPos, m_query.endPos, bestRef, bestTile,
  1102. bestPoly, neighbourRef, neighbourTile, neighbourPoly, 0, null, null);
  1103. cost = cost + endCost;
  1104. heuristic = 0;
  1105. }
  1106. else
  1107. {
  1108. heuristic = m_query.heuristic.GetCost(neighbourPos, m_query.endPos);
  1109. }
  1110. float total = cost + heuristic;
  1111. // The node is already in open list and the new result is worse,
  1112. // skip.
  1113. if ((neighbourNode.flags & DtNode.DT_NODE_OPEN) != 0 && total >= neighbourNode.total)
  1114. {
  1115. continue;
  1116. }
  1117. // The node is already visited and process, and the new result
  1118. // is worse, skip.
  1119. if ((neighbourNode.flags & DtNode.DT_NODE_CLOSED) != 0 && total >= neighbourNode.total)
  1120. {
  1121. continue;
  1122. }
  1123. // Add or update the node.
  1124. neighbourNode.pidx = foundShortCut ? bestNode.pidx : m_nodePool.GetNodeIdx(bestNode);
  1125. neighbourNode.id = neighbourRef;
  1126. neighbourNode.flags = (neighbourNode.flags & ~DT_NODE_CLOSED);
  1127. neighbourNode.cost = cost;
  1128. neighbourNode.total = total;
  1129. neighbourNode.pos = neighbourPos;
  1130. neighbourNode.shortcut = shortcut;
  1131. if ((neighbourNode.flags & DtNode.DT_NODE_OPEN) != 0)
  1132. {
  1133. // Already in open, update node location.
  1134. m_openList.Modify(neighbourNode);
  1135. }
  1136. else
  1137. {
  1138. // Put the node in open list.
  1139. neighbourNode.flags |= DtNode.DT_NODE_OPEN;
  1140. m_openList.Push(neighbourNode);
  1141. }
  1142. // Update nearest node to target so far.
  1143. if (heuristic < m_query.lastBestNodeCost)
  1144. {
  1145. m_query.lastBestNodeCost = heuristic;
  1146. m_query.lastBestNode = neighbourNode;
  1147. }
  1148. }
  1149. }
  1150. // Exhausted all nodes, but could not find path.
  1151. if (m_openList.IsEmpty())
  1152. {
  1153. var details = m_query.status & DtStatus.DT_STATUS_DETAIL_MASK;
  1154. m_query.status = DtStatus.DT_SUCCSESS | details;
  1155. }
  1156. doneIters = iter;
  1157. return m_query.status;
  1158. }
  1159. /// Finalizes and returns the results of a sliced path query.
  1160. /// @param[out] path An ordered list of polygon references representing the path. (Start to end.)
  1161. /// [(polyRef) * @p pathCount]
  1162. /// @returns The status flags for the query.
  1163. public virtual DtStatus FinalizeSlicedFindPath(ref List<long> path)
  1164. {
  1165. if (null == path)
  1166. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  1167. path.Clear();
  1168. if (m_query.status.Failed())
  1169. {
  1170. // Reset query.
  1171. m_query = new DtQueryData();
  1172. return DtStatus.DT_FAILURE;
  1173. }
  1174. if (m_query.startRef == m_query.endRef)
  1175. {
  1176. // Special case: the search starts and ends at same poly.
  1177. path.Add(m_query.startRef);
  1178. }
  1179. else
  1180. {
  1181. // Reverse the path.
  1182. if (m_query.lastBestNode.id != m_query.endRef)
  1183. {
  1184. m_query.status |= DtStatus.DT_PARTIAL_RESULT;
  1185. }
  1186. GetPathToNode(m_query.lastBestNode, ref path);
  1187. }
  1188. var details = m_query.status & DtStatus.DT_STATUS_DETAIL_MASK;
  1189. // Reset query.
  1190. m_query = new DtQueryData();
  1191. return DtStatus.DT_SUCCSESS | details;
  1192. }
  1193. /// Finalizes and returns the results of an incomplete sliced path query, returning the path to the furthest
  1194. /// polygon on the existing path that was visited during the search.
  1195. /// @param[in] existing An array of polygon references for the existing path.
  1196. /// @param[in] existingSize The number of polygon in the @p existing array.
  1197. /// @param[out] path An ordered list of polygon references representing the path. (Start to end.)
  1198. /// [(polyRef) * @p pathCount]
  1199. /// @returns The status flags for the query.
  1200. public virtual DtStatus FinalizeSlicedFindPathPartial(List<long> existing, ref List<long> path)
  1201. {
  1202. if (null == path)
  1203. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  1204. path.Clear();
  1205. if (null == existing || existing.Count <= 0)
  1206. {
  1207. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  1208. }
  1209. if (m_query.status.Failed())
  1210. {
  1211. // Reset query.
  1212. m_query = new DtQueryData();
  1213. return DtStatus.DT_FAILURE;
  1214. }
  1215. if (m_query.startRef == m_query.endRef)
  1216. {
  1217. // Special case: the search starts and ends at same poly.
  1218. path.Add(m_query.startRef);
  1219. }
  1220. else
  1221. {
  1222. // Find furthest existing node that was visited.
  1223. DtNode node = null;
  1224. for (int i = existing.Count - 1; i >= 0; --i)
  1225. {
  1226. node = m_nodePool.FindNode(existing[i]);
  1227. if (node != null)
  1228. {
  1229. break;
  1230. }
  1231. }
  1232. if (node == null)
  1233. {
  1234. m_query.status |= DtStatus.DT_PARTIAL_RESULT;
  1235. node = m_query.lastBestNode;
  1236. }
  1237. GetPathToNode(node, ref path);
  1238. }
  1239. var details = m_query.status & DtStatus.DT_STATUS_DETAIL_MASK;
  1240. // Reset query.
  1241. m_query = new DtQueryData();
  1242. return DtStatus.DT_SUCCSESS | details;
  1243. }
  1244. protected DtStatus AppendVertex(RcVec3f pos, int flags, long refs, ref List<StraightPathItem> straightPath,
  1245. int maxStraightPath)
  1246. {
  1247. if (straightPath.Count > 0 && DtUtils.VEqual(straightPath[straightPath.Count - 1].pos, pos))
  1248. {
  1249. // The vertices are equal, update flags and poly.
  1250. straightPath[straightPath.Count - 1] = new StraightPathItem(straightPath[straightPath.Count - 1].pos, flags, refs);
  1251. }
  1252. else
  1253. {
  1254. if (straightPath.Count < maxStraightPath)
  1255. {
  1256. // Append new vertex.
  1257. straightPath.Add(new StraightPathItem(pos, flags, refs));
  1258. }
  1259. // If reached end of path or there is no space to append more vertices, return.
  1260. if (flags == DT_STRAIGHTPATH_END || straightPath.Count >= maxStraightPath)
  1261. {
  1262. return DtStatus.DT_SUCCSESS;
  1263. }
  1264. }
  1265. return DtStatus.DT_IN_PROGRESS;
  1266. }
  1267. protected DtStatus AppendPortals(int startIdx, int endIdx, RcVec3f endPos, List<long> path,
  1268. ref List<StraightPathItem> straightPath, int maxStraightPath, int options)
  1269. {
  1270. var startPos = straightPath[straightPath.Count - 1].pos;
  1271. // Append or update last vertex
  1272. DtStatus stat;
  1273. for (int i = startIdx; i < endIdx; i++)
  1274. {
  1275. // Calculate portal
  1276. long from = path[i];
  1277. var status = m_nav.GetTileAndPolyByRef(from, out var fromTile, out var fromPoly);
  1278. if (status.Failed())
  1279. {
  1280. return DtStatus.DT_FAILURE;
  1281. }
  1282. long to = path[i + 1];
  1283. status = m_nav.GetTileAndPolyByRef(to, out var toTile, out var toPoly);
  1284. if (status.Failed())
  1285. {
  1286. return DtStatus.DT_FAILURE;
  1287. }
  1288. var ppStatus = GetPortalPoints(from, fromPoly, fromTile, to, toPoly, toTile, out var left, out var right);
  1289. if (ppStatus.Failed())
  1290. {
  1291. break;
  1292. }
  1293. if ((options & DT_STRAIGHTPATH_AREA_CROSSINGS) != 0)
  1294. {
  1295. // Skip intersection if only area crossings are requested.
  1296. if (fromPoly.GetArea() == toPoly.GetArea())
  1297. {
  1298. continue;
  1299. }
  1300. }
  1301. // Append intersection
  1302. if (DtUtils.IntersectSegSeg2D(startPos, endPos, left, right, out var _, out var t))
  1303. {
  1304. var pt = RcVec3f.Lerp(left, right, t);
  1305. stat = AppendVertex(pt, 0, path[i + 1], ref straightPath, maxStraightPath);
  1306. if (!stat.InProgress())
  1307. {
  1308. return stat;
  1309. }
  1310. }
  1311. }
  1312. return DtStatus.DT_IN_PROGRESS;
  1313. }
  1314. /// @par
  1315. ///
  1316. /// This method peforms what is often called 'string pulling'.
  1317. ///
  1318. /// The start position is clamped to the first polygon in the path, and the
  1319. /// end position is clamped to the last. So the start and end positions should
  1320. /// normally be within or very near the first and last polygons respectively.
  1321. ///
  1322. /// The returned polygon references represent the reference id of the polygon
  1323. /// that is entered at the associated path position. The reference id associated
  1324. /// with the end point will always be zero. This allows, for example, matching
  1325. /// off-mesh link points to their representative polygons.
  1326. ///
  1327. /// If the provided result buffers are too small for the entire result set,
  1328. /// they will be filled as far as possible from the start toward the end
  1329. /// position.
  1330. ///
  1331. /// Finds the straight path from the start to the end position within the polygon corridor.
  1332. /// @param[in] startPos Path start position. [(x, y, z)]
  1333. /// @param[in] endPos Path end position. [(x, y, z)]
  1334. /// @param[in] path An array of polygon references that represent the path corridor.
  1335. /// @param[in] pathSize The number of polygons in the @p path array.
  1336. /// @param[out] straightPath Points describing the straight path. [(x, y, z) * @p straightPathCount].
  1337. /// @param[in] maxStraightPath The maximum number of points the straight path arrays can hold. [Limit: > 0]
  1338. /// @param[in] options Query options. (see: #dtStraightPathOptions)
  1339. /// @returns The status flags for the query.
  1340. public virtual DtStatus FindStraightPath(RcVec3f startPos, RcVec3f endPos, List<long> path,
  1341. ref List<StraightPathItem> straightPath,
  1342. int maxStraightPath, int options)
  1343. {
  1344. if (!RcVec3f.IsFinite(startPos) || !RcVec3f.IsFinite(endPos) || null == straightPath
  1345. || null == path || 0 == path.Count || path[0] == 0 || maxStraightPath <= 0)
  1346. {
  1347. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  1348. }
  1349. straightPath.Clear();
  1350. // TODO: Should this be callers responsibility?
  1351. var closestStartPosRes = ClosestPointOnPolyBoundary(path[0], startPos, out var closestStartPos);
  1352. if (closestStartPosRes.Failed())
  1353. {
  1354. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  1355. }
  1356. var closestEndPosRes = ClosestPointOnPolyBoundary(path[path.Count - 1], endPos, out var closestEndPos);
  1357. if (closestEndPosRes.Failed())
  1358. {
  1359. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  1360. }
  1361. // Add start point.
  1362. DtStatus stat = AppendVertex(closestStartPos, DT_STRAIGHTPATH_START, path[0], ref straightPath, maxStraightPath);
  1363. if (!stat.InProgress())
  1364. {
  1365. return stat;
  1366. }
  1367. if (path.Count > 1)
  1368. {
  1369. RcVec3f portalApex = closestStartPos;
  1370. RcVec3f portalLeft = portalApex;
  1371. RcVec3f portalRight = portalApex;
  1372. int apexIndex = 0;
  1373. int leftIndex = 0;
  1374. int rightIndex = 0;
  1375. int leftPolyType = 0;
  1376. int rightPolyType = 0;
  1377. long leftPolyRef = path[0];
  1378. long rightPolyRef = path[0];
  1379. for (int i = 0; i < path.Count; ++i)
  1380. {
  1381. RcVec3f left;
  1382. RcVec3f right;
  1383. int toType;
  1384. if (i + 1 < path.Count)
  1385. {
  1386. int fromType; // // fromType is ignored.
  1387. // Next portal.
  1388. var ppStatus = GetPortalPoints(path[i], path[i + 1], out left, out right, out fromType, out toType);
  1389. if (ppStatus.Failed())
  1390. {
  1391. // Failed to get portal points, in practice this means that path[i+1] is invalid polygon.
  1392. // Clamp the end point to path[i], and return the path so far.
  1393. var cpStatus = ClosestPointOnPolyBoundary(path[i], endPos, out closestEndPos);
  1394. if (cpStatus.Failed())
  1395. {
  1396. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  1397. }
  1398. // Append portals along the current straight path segment.
  1399. if ((options & (DT_STRAIGHTPATH_AREA_CROSSINGS | DT_STRAIGHTPATH_ALL_CROSSINGS)) != 0)
  1400. {
  1401. // Ignore status return value as we're just about to return anyway.
  1402. AppendPortals(apexIndex, i, closestEndPos, path, ref straightPath, maxStraightPath, options);
  1403. }
  1404. // Ignore status return value as we're just about to return anyway.
  1405. AppendVertex(closestEndPos, 0, path[i], ref straightPath, maxStraightPath);
  1406. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM | (straightPath.Count >= maxStraightPath ? DtStatus.DT_BUFFER_TOO_SMALL : DtStatus.DT_STATUS_NOTHING);
  1407. }
  1408. // If starting really close the portal, advance.
  1409. if (i == 0)
  1410. {
  1411. var distSqr = DtUtils.DistancePtSegSqr2D(portalApex, left, right, out var t);
  1412. if (distSqr < Sqr(0.001f))
  1413. {
  1414. continue;
  1415. }
  1416. }
  1417. }
  1418. else
  1419. {
  1420. // End of the path.
  1421. left = closestEndPos;
  1422. right = closestEndPos;
  1423. toType = DtPoly.DT_POLYTYPE_GROUND;
  1424. }
  1425. // Right vertex.
  1426. if (DtUtils.TriArea2D(portalApex, portalRight, right) <= 0.0f)
  1427. {
  1428. if (DtUtils.VEqual(portalApex, portalRight) || DtUtils.TriArea2D(portalApex, portalLeft, right) > 0.0f)
  1429. {
  1430. portalRight = right;
  1431. rightPolyRef = (i + 1 < path.Count) ? path[i + 1] : 0;
  1432. rightPolyType = toType;
  1433. rightIndex = i;
  1434. }
  1435. else
  1436. {
  1437. // Append portals along the current straight path segment.
  1438. if ((options & (DT_STRAIGHTPATH_AREA_CROSSINGS | DT_STRAIGHTPATH_ALL_CROSSINGS)) != 0)
  1439. {
  1440. stat = AppendPortals(apexIndex, leftIndex, portalLeft, path, ref straightPath, maxStraightPath, options);
  1441. if (!stat.InProgress())
  1442. {
  1443. return stat;
  1444. }
  1445. }
  1446. portalApex = portalLeft;
  1447. apexIndex = leftIndex;
  1448. int flags = 0;
  1449. if (leftPolyRef == 0)
  1450. {
  1451. flags = DT_STRAIGHTPATH_END;
  1452. }
  1453. else if (leftPolyType == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION)
  1454. {
  1455. flags = DT_STRAIGHTPATH_OFFMESH_CONNECTION;
  1456. }
  1457. long refs = leftPolyRef;
  1458. // Append or update vertex
  1459. stat = AppendVertex(portalApex, flags, refs, ref straightPath, maxStraightPath);
  1460. if (!stat.InProgress())
  1461. {
  1462. return stat;
  1463. }
  1464. portalLeft = portalApex;
  1465. portalRight = portalApex;
  1466. leftIndex = apexIndex;
  1467. rightIndex = apexIndex;
  1468. // Restart
  1469. i = apexIndex;
  1470. continue;
  1471. }
  1472. }
  1473. // Left vertex.
  1474. if (DtUtils.TriArea2D(portalApex, portalLeft, left) >= 0.0f)
  1475. {
  1476. if (DtUtils.VEqual(portalApex, portalLeft) || DtUtils.TriArea2D(portalApex, portalRight, left) < 0.0f)
  1477. {
  1478. portalLeft = left;
  1479. leftPolyRef = (i + 1 < path.Count) ? path[i + 1] : 0;
  1480. leftPolyType = toType;
  1481. leftIndex = i;
  1482. }
  1483. else
  1484. {
  1485. // Append portals along the current straight path segment.
  1486. if ((options & (DT_STRAIGHTPATH_AREA_CROSSINGS | DT_STRAIGHTPATH_ALL_CROSSINGS)) != 0)
  1487. {
  1488. stat = AppendPortals(apexIndex, rightIndex, portalRight, path, ref straightPath, maxStraightPath, options);
  1489. if (!stat.InProgress())
  1490. {
  1491. return stat;
  1492. }
  1493. }
  1494. portalApex = portalRight;
  1495. apexIndex = rightIndex;
  1496. int flags = 0;
  1497. if (rightPolyRef == 0)
  1498. {
  1499. flags = DT_STRAIGHTPATH_END;
  1500. }
  1501. else if (rightPolyType == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION)
  1502. {
  1503. flags = DT_STRAIGHTPATH_OFFMESH_CONNECTION;
  1504. }
  1505. long refs = rightPolyRef;
  1506. // Append or update vertex
  1507. stat = AppendVertex(portalApex, flags, refs, ref straightPath, maxStraightPath);
  1508. if (!stat.InProgress())
  1509. {
  1510. return stat;
  1511. }
  1512. portalLeft = portalApex;
  1513. portalRight = portalApex;
  1514. leftIndex = apexIndex;
  1515. rightIndex = apexIndex;
  1516. // Restart
  1517. i = apexIndex;
  1518. continue;
  1519. }
  1520. }
  1521. }
  1522. // Append portals along the current straight path segment.
  1523. if ((options & (DT_STRAIGHTPATH_AREA_CROSSINGS | DT_STRAIGHTPATH_ALL_CROSSINGS)) != 0)
  1524. {
  1525. stat = AppendPortals(apexIndex, path.Count - 1, closestEndPos, path, ref straightPath, maxStraightPath, options);
  1526. if (!stat.InProgress())
  1527. {
  1528. return stat;
  1529. }
  1530. }
  1531. }
  1532. // Ignore status return value as we're just about to return anyway.
  1533. AppendVertex(closestEndPos, DT_STRAIGHTPATH_END, 0, ref straightPath, maxStraightPath);
  1534. return DtStatus.DT_SUCCSESS | (straightPath.Count >= maxStraightPath ? DtStatus.DT_BUFFER_TOO_SMALL : DtStatus.DT_STATUS_NOTHING);
  1535. }
  1536. /// @par
  1537. ///
  1538. /// This method is optimized for small delta movement and a small number of
  1539. /// polygons. If used for too great a distance, the result set will form an
  1540. /// incomplete path.
  1541. ///
  1542. /// @p resultPos will equal the @p endPos if the end is reached.
  1543. /// Otherwise the closest reachable position will be returned.
  1544. ///
  1545. /// @p resultPos is not projected onto the surface of the navigation
  1546. /// mesh. Use #getPolyHeight if this is needed.
  1547. ///
  1548. /// This method treats the end position in the same manner as
  1549. /// the #raycast method. (As a 2D point.) See that method's documentation
  1550. /// for details.
  1551. ///
  1552. /// If the @p visited array is too small to hold the entire result set, it will
  1553. /// be filled as far as possible from the start position toward the end
  1554. /// position.
  1555. ///
  1556. /// Moves from the start to the end position constrained to the navigation mesh.
  1557. /// @param[in] startRef The reference id of the start polygon.
  1558. /// @param[in] startPos A position of the mover within the start polygon. [(x, y, x)]
  1559. /// @param[in] endPos The desired end position of the mover. [(x, y, z)]
  1560. /// @param[in] filter The polygon filter to apply to the query.
  1561. /// @param[out] resultPos The result position of the mover. [(x, y, z)]
  1562. /// @param[out] visited The reference ids of the polygons visited during the move.
  1563. /// @param[out] visitedCount The number of polygons visited during the move.
  1564. /// @param[in] maxVisitedSize The maximum number of polygons the @p visited array can hold.
  1565. /// @returns The status flags for the query.
  1566. public DtStatus MoveAlongSurface(long startRef, RcVec3f startPos, RcVec3f endPos,
  1567. IDtQueryFilter filter,
  1568. out RcVec3f resultPos, ref List<long> visited)
  1569. {
  1570. resultPos = RcVec3f.Zero;
  1571. if (null != visited)
  1572. visited.Clear();
  1573. // Validate input
  1574. if (!m_nav.IsValidPolyRef(startRef) || !RcVec3f.IsFinite(startPos)
  1575. || !RcVec3f.IsFinite(endPos) || null == filter)
  1576. {
  1577. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  1578. }
  1579. DtNodePool tinyNodePool = new DtNodePool();
  1580. DtNode startNode = tinyNodePool.GetNode(startRef);
  1581. startNode.pidx = 0;
  1582. startNode.cost = 0;
  1583. startNode.total = 0;
  1584. startNode.id = startRef;
  1585. startNode.flags = DtNode.DT_NODE_CLOSED;
  1586. LinkedList<DtNode> stack = new LinkedList<DtNode>();
  1587. stack.AddLast(startNode);
  1588. RcVec3f bestPos = new RcVec3f();
  1589. float bestDist = float.MaxValue;
  1590. DtNode bestNode = null;
  1591. bestPos = startPos;
  1592. // Search constraints
  1593. var searchPos = RcVec3f.Lerp(startPos, endPos, 0.5f);
  1594. float searchRadSqr = Sqr(RcVec3f.Distance(startPos, endPos) / 2.0f + 0.001f);
  1595. float[] verts = new float[m_nav.GetMaxVertsPerPoly() * 3];
  1596. while (0 < stack.Count)
  1597. {
  1598. // Pop front.
  1599. DtNode curNode = stack.First?.Value;
  1600. stack.RemoveFirst();
  1601. // Get poly and tile.
  1602. // The API input has been checked already, skip checking internal data.
  1603. long curRef = curNode.id;
  1604. m_nav.GetTileAndPolyByRefUnsafe(curRef, out var curTile, out var curPoly);
  1605. // Collect vertices.
  1606. int nverts = curPoly.vertCount;
  1607. for (int i = 0; i < nverts; ++i)
  1608. {
  1609. Array.Copy(curTile.data.verts, curPoly.verts[i] * 3, verts, i * 3, 3);
  1610. }
  1611. // If target is inside the poly, stop search.
  1612. if (DtUtils.PointInPolygon(endPos, verts, nverts))
  1613. {
  1614. bestNode = curNode;
  1615. bestPos = endPos;
  1616. break;
  1617. }
  1618. // Find wall edges and find nearest point inside the walls.
  1619. for (int i = 0, j = curPoly.vertCount - 1; i < curPoly.vertCount; j = i++)
  1620. {
  1621. // Find links to neighbours.
  1622. int MAX_NEIS = 8;
  1623. int nneis = 0;
  1624. long[] neis = new long[MAX_NEIS];
  1625. if ((curPoly.neis[j] & DtNavMesh.DT_EXT_LINK) != 0)
  1626. {
  1627. // Tile border.
  1628. for (int k = curTile.polyLinks[curPoly.index]; k != DtNavMesh.DT_NULL_LINK; k = curTile.links[k].next)
  1629. {
  1630. DtLink link = curTile.links[k];
  1631. if (link.edge == j)
  1632. {
  1633. if (link.refs != 0)
  1634. {
  1635. m_nav.GetTileAndPolyByRefUnsafe(link.refs, out var neiTile, out var neiPoly);
  1636. if (filter.PassFilter(link.refs, neiTile, neiPoly))
  1637. {
  1638. if (nneis < MAX_NEIS)
  1639. {
  1640. neis[nneis++] = link.refs;
  1641. }
  1642. }
  1643. }
  1644. }
  1645. }
  1646. }
  1647. else if (curPoly.neis[j] != 0)
  1648. {
  1649. int idx = curPoly.neis[j] - 1;
  1650. long refs = m_nav.GetPolyRefBase(curTile) | (long)idx;
  1651. if (filter.PassFilter(refs, curTile, curTile.data.polys[idx]))
  1652. {
  1653. // Internal edge, encode id.
  1654. neis[nneis++] = refs;
  1655. }
  1656. }
  1657. if (nneis == 0)
  1658. {
  1659. // Wall edge, calc distance.
  1660. int vj = j * 3;
  1661. int vi = i * 3;
  1662. var distSqr = DtUtils.DistancePtSegSqr2D(endPos, verts, vj, vi, out var tseg);
  1663. if (distSqr < bestDist)
  1664. {
  1665. // Update nearest distance.
  1666. bestPos = RcVec3f.Lerp(verts, vj, vi, tseg);
  1667. bestDist = distSqr;
  1668. bestNode = curNode;
  1669. }
  1670. }
  1671. else
  1672. {
  1673. for (int k = 0; k < nneis; ++k)
  1674. {
  1675. DtNode neighbourNode = tinyNodePool.GetNode(neis[k]);
  1676. // Skip if already visited.
  1677. if ((neighbourNode.flags & DtNode.DT_NODE_CLOSED) != 0)
  1678. {
  1679. continue;
  1680. }
  1681. // Skip the link if it is too far from search constraint.
  1682. // TODO: Maybe should use GetPortalPoints(), but this one is way faster.
  1683. int vj = j * 3;
  1684. int vi = i * 3;
  1685. var distSqr = DtUtils.DistancePtSegSqr2D(searchPos, verts, vj, vi, out var _);
  1686. if (distSqr > searchRadSqr)
  1687. {
  1688. continue;
  1689. }
  1690. // Mark as the node as visited and push to queue.
  1691. neighbourNode.pidx = tinyNodePool.GetNodeIdx(curNode);
  1692. neighbourNode.flags |= DtNode.DT_NODE_CLOSED;
  1693. stack.AddLast(neighbourNode);
  1694. }
  1695. }
  1696. }
  1697. }
  1698. if (bestNode != null)
  1699. {
  1700. // Reverse the path.
  1701. DtNode prev = null;
  1702. DtNode node = bestNode;
  1703. do
  1704. {
  1705. DtNode next = tinyNodePool.GetNodeAtIdx(node.pidx);
  1706. node.pidx = tinyNodePool.GetNodeIdx(prev);
  1707. prev = node;
  1708. node = next;
  1709. } while (node != null);
  1710. // Store result
  1711. node = prev;
  1712. do
  1713. {
  1714. visited.Add(node.id);
  1715. node = tinyNodePool.GetNodeAtIdx(node.pidx);
  1716. } while (node != null);
  1717. }
  1718. resultPos = bestPos;
  1719. return DtStatus.DT_SUCCSESS;
  1720. }
  1721. protected DtStatus GetPortalPoints(long from, long to, out RcVec3f left, out RcVec3f right, out int fromType, out int toType)
  1722. {
  1723. left = RcVec3f.Zero;
  1724. right = RcVec3f.Zero;
  1725. fromType = 0;
  1726. toType = 0;
  1727. var status = m_nav.GetTileAndPolyByRef(from, out var fromTile, out var fromPoly);
  1728. if (status.Failed())
  1729. {
  1730. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  1731. }
  1732. fromType = fromPoly.GetPolyType();
  1733. status = m_nav.GetTileAndPolyByRef(to, out var toTile, out var toPoly);
  1734. if (status.Failed())
  1735. {
  1736. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  1737. }
  1738. toType = toPoly.GetPolyType();
  1739. return GetPortalPoints(from, fromPoly, fromTile, to, toPoly, toTile, out left, out right);
  1740. }
  1741. // Returns portal points between two polygons.
  1742. protected DtStatus GetPortalPoints(long from, DtPoly fromPoly, DtMeshTile fromTile,
  1743. long to, DtPoly toPoly, DtMeshTile toTile,
  1744. out RcVec3f left, out RcVec3f right)
  1745. {
  1746. left = RcVec3f.Zero;
  1747. right = RcVec3f.Zero;
  1748. // Find the link that points to the 'to' polygon.
  1749. DtLink link = null;
  1750. for (int i = fromTile.polyLinks[fromPoly.index]; i != DtNavMesh.DT_NULL_LINK; i = fromTile.links[i].next)
  1751. {
  1752. if (fromTile.links[i].refs == to)
  1753. {
  1754. link = fromTile.links[i];
  1755. break;
  1756. }
  1757. }
  1758. if (link == null)
  1759. {
  1760. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  1761. }
  1762. // Handle off-mesh connections.
  1763. if (fromPoly.GetPolyType() == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION)
  1764. {
  1765. // Find link that points to first vertex.
  1766. for (int i = fromTile.polyLinks[fromPoly.index]; i != DtNavMesh.DT_NULL_LINK; i = fromTile.links[i].next)
  1767. {
  1768. if (fromTile.links[i].refs == to)
  1769. {
  1770. int v = fromTile.links[i].edge;
  1771. left.x = fromTile.data.verts[fromPoly.verts[v] * 3];
  1772. left.y = fromTile.data.verts[fromPoly.verts[v] * 3 + 1];
  1773. left.z = fromTile.data.verts[fromPoly.verts[v] * 3 + 2];
  1774. right.x = fromTile.data.verts[fromPoly.verts[v] * 3];
  1775. right.y = fromTile.data.verts[fromPoly.verts[v] * 3 + 1];
  1776. right.z = fromTile.data.verts[fromPoly.verts[v] * 3 + 2];
  1777. return DtStatus.DT_SUCCSESS;
  1778. }
  1779. }
  1780. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  1781. }
  1782. if (toPoly.GetPolyType() == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION)
  1783. {
  1784. for (int i = toTile.polyLinks[toPoly.index]; i != DtNavMesh.DT_NULL_LINK; i = toTile.links[i].next)
  1785. {
  1786. if (toTile.links[i].refs == from)
  1787. {
  1788. int v = toTile.links[i].edge;
  1789. left.x = toTile.data.verts[toPoly.verts[v] * 3];
  1790. left.y = toTile.data.verts[toPoly.verts[v] * 3 + 1];
  1791. left.z = toTile.data.verts[toPoly.verts[v] * 3 + 2];
  1792. right.x = toTile.data.verts[toPoly.verts[v] * 3];
  1793. right.y = toTile.data.verts[toPoly.verts[v] * 3 + 1];
  1794. right.z = toTile.data.verts[toPoly.verts[v] * 3 + 2];
  1795. return DtStatus.DT_SUCCSESS;
  1796. }
  1797. }
  1798. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  1799. }
  1800. // Find portal vertices.
  1801. int v0 = fromPoly.verts[link.edge];
  1802. int v1 = fromPoly.verts[(link.edge + 1) % fromPoly.vertCount];
  1803. left.x = fromTile.data.verts[v0 * 3];
  1804. left.y = fromTile.data.verts[v0 * 3 + 1];
  1805. left.z = fromTile.data.verts[v0 * 3 + 2];
  1806. right.x = fromTile.data.verts[v1 * 3];
  1807. right.y = fromTile.data.verts[v1 * 3 + 1];
  1808. right.z = fromTile.data.verts[v1 * 3 + 2];
  1809. // If the link is at tile boundary, dtClamp the vertices to
  1810. // the link width.
  1811. if (link.side != 0xff)
  1812. {
  1813. // Unpack portal limits.
  1814. if (link.bmin != 0 || link.bmax != 255)
  1815. {
  1816. float s = 1.0f / 255.0f;
  1817. float tmin = link.bmin * s;
  1818. float tmax = link.bmax * s;
  1819. left = RcVec3f.Lerp(fromTile.data.verts, v0 * 3, v1 * 3, tmin);
  1820. right = RcVec3f.Lerp(fromTile.data.verts, v0 * 3, v1 * 3, tmax);
  1821. }
  1822. }
  1823. return DtStatus.DT_SUCCSESS;
  1824. }
  1825. protected DtStatus GetEdgeMidPoint(long from, DtPoly fromPoly, DtMeshTile fromTile, long to,
  1826. DtPoly toPoly, DtMeshTile toTile, ref RcVec3f mid)
  1827. {
  1828. var ppStatus = GetPortalPoints(from, fromPoly, fromTile, to, toPoly, toTile, out var left, out var right);
  1829. if (ppStatus.Failed())
  1830. {
  1831. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  1832. }
  1833. mid.x = (left.x + right.x) * 0.5f;
  1834. mid.y = (left.y + right.y) * 0.5f;
  1835. mid.z = (left.z + right.z) * 0.5f;
  1836. return DtStatus.DT_SUCCSESS;
  1837. }
  1838. protected DtStatus GetEdgeIntersectionPoint(RcVec3f fromPos, long from, DtPoly fromPoly, DtMeshTile fromTile,
  1839. RcVec3f toPos, long to, DtPoly toPoly, DtMeshTile toTile,
  1840. ref RcVec3f pt)
  1841. {
  1842. var ppStatus = GetPortalPoints(from, fromPoly, fromTile, to, toPoly, toTile, out var left, out var right);
  1843. if (ppStatus.Failed())
  1844. {
  1845. return DtStatus.DT_FAILURE;
  1846. }
  1847. float t = 0.5f;
  1848. if (DtUtils.IntersectSegSeg2D(fromPos, toPos, left, right, out var _, out var t2))
  1849. {
  1850. t = Clamp(t2, 0.1f, 0.9f);
  1851. }
  1852. pt = RcVec3f.Lerp(left, right, t);
  1853. return DtStatus.DT_SUCCSESS;
  1854. }
  1855. /// @par
  1856. ///
  1857. /// This method is meant to be used for quick, short distance checks.
  1858. ///
  1859. /// If the path array is too small to hold the result, it will be filled as
  1860. /// far as possible from the start postion toward the end position.
  1861. ///
  1862. /// <b>Using the Hit Parameter t of RaycastHit</b>
  1863. ///
  1864. /// If the hit parameter is a very high value (FLT_MAX), then the ray has hit
  1865. /// the end position. In this case the path represents a valid corridor to the
  1866. /// end position and the value of @p hitNormal is undefined.
  1867. ///
  1868. /// If the hit parameter is zero, then the start position is on the wall that
  1869. /// was hit and the value of @p hitNormal is undefined.
  1870. ///
  1871. /// If 0 < t < 1.0 then the following applies:
  1872. ///
  1873. /// @code
  1874. /// distanceToHitBorder = distanceToEndPosition * t
  1875. /// hitPoint = startPos + (endPos - startPos) * t
  1876. /// @endcode
  1877. ///
  1878. /// <b>Use Case Restriction</b>
  1879. ///
  1880. /// The raycast ignores the y-value of the end position. (2D check.) This
  1881. /// places significant limits on how it can be used. For example:
  1882. ///
  1883. /// Consider a scene where there is a main floor with a second floor balcony
  1884. /// that hangs over the main floor. So the first floor mesh extends below the
  1885. /// balcony mesh. The start position is somewhere on the first floor. The end
  1886. /// position is on the balcony.
  1887. ///
  1888. /// The raycast will search toward the end position along the first floor mesh.
  1889. /// If it reaches the end position's xz-coordinates it will indicate FLT_MAX
  1890. /// (no wall hit), meaning it reached the end position. This is one example of why
  1891. /// this method is meant for short distance checks.
  1892. ///
  1893. /// Casts a 'walkability' ray along the surface of the navigation mesh from
  1894. /// the start position toward the end position.
  1895. /// @note A wrapper around Raycast(..., RaycastHit*). Retained for backward compatibility.
  1896. /// @param[in] startRef The reference id of the start polygon.
  1897. /// @param[in] startPos A position within the start polygon representing
  1898. /// the start of the ray. [(x, y, z)]
  1899. /// @param[in] endPos The position to cast the ray toward. [(x, y, z)]
  1900. /// @param[out] t The hit parameter. (FLT_MAX if no wall hit.)
  1901. /// @param[out] hitNormal The normal of the nearest wall hit. [(x, y, z)]
  1902. /// @param[in] filter The polygon filter to apply to the query.
  1903. /// @param[out] path The reference ids of the visited polygons. [opt]
  1904. /// @param[out] pathCount The number of visited polygons. [opt]
  1905. /// @param[in] maxPath The maximum number of polygons the @p path array can hold.
  1906. /// @returns The status flags for the query.
  1907. public DtStatus Raycast(long startRef, RcVec3f startPos, RcVec3f endPos, IDtQueryFilter filter, int options,
  1908. long prevRef, out DtRaycastHit hit)
  1909. {
  1910. hit = null;
  1911. // Validate input
  1912. if (!m_nav.IsValidPolyRef(startRef) || !RcVec3f.IsFinite(startPos) || !RcVec3f.IsFinite(endPos)
  1913. || null == filter || (prevRef != 0 && !m_nav.IsValidPolyRef(prevRef)))
  1914. {
  1915. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  1916. }
  1917. hit = new DtRaycastHit();
  1918. RcVec3f[] verts = new RcVec3f[m_nav.GetMaxVertsPerPoly() + 1];
  1919. RcVec3f curPos = RcVec3f.Zero;
  1920. RcVec3f lastPos = RcVec3f.Zero;
  1921. curPos = startPos;
  1922. var dir = endPos.Subtract(startPos);
  1923. DtMeshTile prevTile, tile, nextTile;
  1924. DtPoly prevPoly, poly, nextPoly;
  1925. // The API input has been checked already, skip checking internal data.
  1926. long curRef = startRef;
  1927. m_nav.GetTileAndPolyByRefUnsafe(curRef, out tile, out poly);
  1928. nextTile = prevTile = tile;
  1929. nextPoly = prevPoly = poly;
  1930. if (prevRef != 0)
  1931. {
  1932. m_nav.GetTileAndPolyByRefUnsafe(prevRef, out prevTile, out prevPoly);
  1933. }
  1934. while (curRef != 0)
  1935. {
  1936. // Cast ray against current polygon.
  1937. // Collect vertices.
  1938. int nv = 0;
  1939. for (int i = 0; i < poly.vertCount; ++i)
  1940. {
  1941. verts[nv] = RcVec3f.Of(tile.data.verts, poly.verts[i] * 3);
  1942. nv++;
  1943. }
  1944. bool intersects = DtUtils.IntersectSegmentPoly2D(startPos, endPos, verts, nv, out var tmin, out var tmax, out var segMin, out var segMax);
  1945. if (!intersects)
  1946. {
  1947. // Could not hit the polygon, keep the old t and report hit.
  1948. return DtStatus.DT_SUCCSESS;
  1949. }
  1950. hit.hitEdgeIndex = segMax;
  1951. // Keep track of furthest t so far.
  1952. if (tmax > hit.t)
  1953. {
  1954. hit.t = tmax;
  1955. }
  1956. // Store visited polygons.
  1957. hit.path.Add(curRef);
  1958. // Ray end is completely inside the polygon.
  1959. if (segMax == -1)
  1960. {
  1961. hit.t = float.MaxValue;
  1962. // add the cost
  1963. if ((options & DT_RAYCAST_USE_COSTS) != 0)
  1964. {
  1965. hit.pathCost += filter.GetCost(curPos, endPos, prevRef, prevTile, prevPoly, curRef, tile, poly,
  1966. curRef, tile, poly);
  1967. }
  1968. return DtStatus.DT_SUCCSESS;
  1969. }
  1970. // Follow neighbours.
  1971. long nextRef = 0;
  1972. for (int i = tile.polyLinks[poly.index]; i != DtNavMesh.DT_NULL_LINK; i = tile.links[i].next)
  1973. {
  1974. DtLink link = tile.links[i];
  1975. // Find link which contains this edge.
  1976. if (link.edge != segMax)
  1977. {
  1978. continue;
  1979. }
  1980. // Get pointer to the next polygon.
  1981. m_nav.GetTileAndPolyByRefUnsafe(link.refs, out nextTile, out nextPoly);
  1982. // Skip off-mesh connections.
  1983. if (nextPoly.GetPolyType() == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION)
  1984. {
  1985. continue;
  1986. }
  1987. // Skip links based on filter.
  1988. if (!filter.PassFilter(link.refs, nextTile, nextPoly))
  1989. {
  1990. continue;
  1991. }
  1992. // If the link is internal, just return the ref.
  1993. if (link.side == 0xff)
  1994. {
  1995. nextRef = link.refs;
  1996. break;
  1997. }
  1998. // If the link is at tile boundary,
  1999. // Check if the link spans the whole edge, and accept.
  2000. if (link.bmin == 0 && link.bmax == 255)
  2001. {
  2002. nextRef = link.refs;
  2003. break;
  2004. }
  2005. // Check for partial edge links.
  2006. int v0 = poly.verts[link.edge];
  2007. int v1 = poly.verts[(link.edge + 1) % poly.vertCount];
  2008. int left = v0 * 3;
  2009. int right = v1 * 3;
  2010. // Check that the intersection lies inside the link portal.
  2011. if (link.side == 0 || link.side == 4)
  2012. {
  2013. // Calculate link size.
  2014. const float s = 1.0f / 255.0f;
  2015. float lmin = tile.data.verts[left + 2]
  2016. + (tile.data.verts[right + 2] - tile.data.verts[left + 2]) * (link.bmin * s);
  2017. float lmax = tile.data.verts[left + 2]
  2018. + (tile.data.verts[right + 2] - tile.data.verts[left + 2]) * (link.bmax * s);
  2019. if (lmin > lmax)
  2020. {
  2021. (lmin, lmax) = (lmax, lmin);
  2022. }
  2023. // Find Z intersection.
  2024. float z = startPos.z + (endPos.z - startPos.z) * tmax;
  2025. if (z >= lmin && z <= lmax)
  2026. {
  2027. nextRef = link.refs;
  2028. break;
  2029. }
  2030. }
  2031. else if (link.side == 2 || link.side == 6)
  2032. {
  2033. // Calculate link size.
  2034. const float s = 1.0f / 255.0f;
  2035. float lmin = tile.data.verts[left]
  2036. + (tile.data.verts[right] - tile.data.verts[left]) * (link.bmin * s);
  2037. float lmax = tile.data.verts[left]
  2038. + (tile.data.verts[right] - tile.data.verts[left]) * (link.bmax * s);
  2039. if (lmin > lmax)
  2040. {
  2041. (lmin, lmax) = (lmax, lmin);
  2042. }
  2043. // Find X intersection.
  2044. float x = startPos.x + (endPos.x - startPos.x) * tmax;
  2045. if (x >= lmin && x <= lmax)
  2046. {
  2047. nextRef = link.refs;
  2048. break;
  2049. }
  2050. }
  2051. }
  2052. // add the cost
  2053. if ((options & DT_RAYCAST_USE_COSTS) != 0)
  2054. {
  2055. // compute the intersection point at the furthest end of the polygon
  2056. // and correct the height (since the raycast moves in 2d)
  2057. lastPos = curPos;
  2058. curPos = RcVec3f.Mad(startPos, dir, hit.t);
  2059. var e1 = verts[segMax];
  2060. var e2 = verts[(segMax + 1) % nv];
  2061. var eDir = e2.Subtract(e1);
  2062. var diff = curPos.Subtract(e1);
  2063. float s = Sqr(eDir.x) > Sqr(eDir.z) ? diff.x / eDir.x : diff.z / eDir.z;
  2064. curPos.y = e1.y + eDir.y * s;
  2065. hit.pathCost += filter.GetCost(lastPos, curPos, prevRef, prevTile, prevPoly, curRef, tile, poly,
  2066. nextRef, nextTile, nextPoly);
  2067. }
  2068. if (nextRef == 0)
  2069. {
  2070. // No neighbour, we hit a wall.
  2071. // Calculate hit normal.
  2072. int a = segMax;
  2073. int b = segMax + 1 < nv ? segMax + 1 : 0;
  2074. // int va = a * 3;
  2075. // int vb = b * 3;
  2076. float dx = verts[b].x - verts[a].x;
  2077. float dz = verts[b].z - verts[a].x;
  2078. hit.hitNormal.x = dz;
  2079. hit.hitNormal.y = 0;
  2080. hit.hitNormal.z = -dx;
  2081. hit.hitNormal.Normalize();
  2082. return DtStatus.DT_SUCCSESS;
  2083. }
  2084. // No hit, advance to neighbour polygon.
  2085. prevRef = curRef;
  2086. curRef = nextRef;
  2087. prevTile = tile;
  2088. tile = nextTile;
  2089. prevPoly = poly;
  2090. poly = nextPoly;
  2091. }
  2092. return DtStatus.DT_SUCCSESS;
  2093. }
  2094. /// @par
  2095. ///
  2096. /// At least one result array must be provided.
  2097. ///
  2098. /// The order of the result set is from least to highest cost to reach the polygon.
  2099. ///
  2100. /// A common use case for this method is to perform Dijkstra searches.
  2101. /// Candidate polygons are found by searching the graph beginning at the start polygon.
  2102. ///
  2103. /// If a polygon is not found via the graph search, even if it intersects the
  2104. /// search circle, it will not be included in the result set. For example:
  2105. ///
  2106. /// polyA is the start polygon.
  2107. /// polyB shares an edge with polyA. (Is adjacent.)
  2108. /// polyC shares an edge with polyB, but not with polyA
  2109. /// Even if the search circle overlaps polyC, it will not be included in the
  2110. /// result set unless polyB is also in the set.
  2111. ///
  2112. /// The value of the center point is used as the start position for cost
  2113. /// calculations. It is not projected onto the surface of the mesh, so its
  2114. /// y-value will effect the costs.
  2115. ///
  2116. /// Intersection tests occur in 2D. All polygons and the search circle are
  2117. /// projected onto the xz-plane. So the y-value of the center point does not
  2118. /// effect intersection tests.
  2119. ///
  2120. /// If the result arrays are to small to hold the entire result set, they will be
  2121. /// filled to capacity.
  2122. ///
  2123. ///@}
  2124. /// @name Dijkstra Search Functions
  2125. /// @{
  2126. /// Finds the polygons along the navigation graph that touch the specified circle.
  2127. /// @param[in] startRef The reference id of the polygon where the search starts.
  2128. /// @param[in] centerPos The center of the search circle. [(x, y, z)]
  2129. /// @param[in] radius The radius of the search circle.
  2130. /// @param[in] filter The polygon filter to apply to the query.
  2131. /// @param[out] resultRef The reference ids of the polygons touched by the circle. [opt]
  2132. /// @param[out] resultParent The reference ids of the parent polygons for each result.
  2133. /// Zero if a result polygon has no parent. [opt]
  2134. /// @param[out] resultCost The search cost from @p centerPos to the polygon. [opt]
  2135. /// @param[out] resultCount The number of polygons found. [opt]
  2136. /// @param[in] maxResult The maximum number of polygons the result arrays can hold.
  2137. /// @returns The status flags for the query.
  2138. public DtStatus FindPolysAroundCircle(long startRef, RcVec3f centerPos, float radius, IDtQueryFilter filter,
  2139. ref List<long> resultRef, ref List<long> resultParent, ref List<float> resultCost)
  2140. {
  2141. if (null != resultRef)
  2142. {
  2143. resultRef.Clear();
  2144. resultParent.Clear();
  2145. resultCost.Clear();
  2146. }
  2147. // Validate input
  2148. if (!m_nav.IsValidPolyRef(startRef) || !RcVec3f.IsFinite(centerPos) || radius < 0
  2149. || !float.IsFinite(radius) || null == filter)
  2150. {
  2151. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  2152. }
  2153. m_nodePool.Clear();
  2154. m_openList.Clear();
  2155. DtNode startNode = m_nodePool.GetNode(startRef);
  2156. startNode.pos = centerPos;
  2157. startNode.pidx = 0;
  2158. startNode.cost = 0;
  2159. startNode.total = 0;
  2160. startNode.id = startRef;
  2161. startNode.flags = DtNode.DT_NODE_OPEN;
  2162. m_openList.Push(startNode);
  2163. float radiusSqr = Sqr(radius);
  2164. while (!m_openList.IsEmpty())
  2165. {
  2166. DtNode bestNode = m_openList.Pop();
  2167. bestNode.flags &= ~DtNode.DT_NODE_OPEN;
  2168. bestNode.flags |= DtNode.DT_NODE_CLOSED;
  2169. // Get poly and tile.
  2170. // The API input has been checked already, skip checking internal data.
  2171. long bestRef = bestNode.id;
  2172. m_nav.GetTileAndPolyByRefUnsafe(bestRef, out var bestTile, out var bestPoly);
  2173. // Get parent poly and tile.
  2174. long parentRef = 0;
  2175. DtMeshTile parentTile = null;
  2176. DtPoly parentPoly = null;
  2177. if (bestNode.pidx != 0)
  2178. {
  2179. parentRef = m_nodePool.GetNodeAtIdx(bestNode.pidx).id;
  2180. }
  2181. if (parentRef != 0)
  2182. {
  2183. m_nav.GetTileAndPolyByRefUnsafe(parentRef, out parentTile, out parentPoly);
  2184. }
  2185. resultRef.Add(bestRef);
  2186. resultParent.Add(parentRef);
  2187. resultCost.Add(bestNode.total);
  2188. for (int i = bestTile.polyLinks[bestPoly.index]; i != DtNavMesh.DT_NULL_LINK; i = bestTile.links[i].next)
  2189. {
  2190. DtLink link = bestTile.links[i];
  2191. long neighbourRef = link.refs;
  2192. // Skip invalid neighbours and do not follow back to parent.
  2193. if (neighbourRef == 0 || neighbourRef == parentRef)
  2194. {
  2195. continue;
  2196. }
  2197. // Expand to neighbour
  2198. m_nav.GetTileAndPolyByRefUnsafe(neighbourRef, out var neighbourTile, out var neighbourPoly);
  2199. // Do not advance if the polygon is excluded by the filter.
  2200. if (!filter.PassFilter(neighbourRef, neighbourTile, neighbourPoly))
  2201. {
  2202. continue;
  2203. }
  2204. // Find edge and calc distance to the edge.
  2205. var ppStatus = GetPortalPoints(bestRef, bestPoly, bestTile, neighbourRef, neighbourPoly,
  2206. neighbourTile, out var va, out var vb);
  2207. if (ppStatus.Failed())
  2208. {
  2209. continue;
  2210. }
  2211. // If the circle is not touching the next polygon, skip it.
  2212. var distSqr = DtUtils.DistancePtSegSqr2D(centerPos, va, vb, out var _);
  2213. if (distSqr > radiusSqr)
  2214. {
  2215. continue;
  2216. }
  2217. DtNode neighbourNode = m_nodePool.GetNode(neighbourRef);
  2218. if ((neighbourNode.flags & DtNode.DT_NODE_CLOSED) != 0)
  2219. {
  2220. continue;
  2221. }
  2222. // Cost
  2223. if (neighbourNode.flags == 0)
  2224. {
  2225. neighbourNode.pos = RcVec3f.Lerp(va, vb, 0.5f);
  2226. }
  2227. float cost = filter.GetCost(bestNode.pos, neighbourNode.pos, parentRef, parentTile, parentPoly, bestRef,
  2228. bestTile, bestPoly, neighbourRef, neighbourTile, neighbourPoly);
  2229. float total = bestNode.total + cost;
  2230. // The node is already in open list and the new result is worse, skip.
  2231. if ((neighbourNode.flags & DtNode.DT_NODE_OPEN) != 0 && total >= neighbourNode.total)
  2232. {
  2233. continue;
  2234. }
  2235. neighbourNode.id = neighbourRef;
  2236. neighbourNode.pidx = m_nodePool.GetNodeIdx(bestNode);
  2237. neighbourNode.total = total;
  2238. if ((neighbourNode.flags & DtNode.DT_NODE_OPEN) != 0)
  2239. {
  2240. m_openList.Modify(neighbourNode);
  2241. }
  2242. else
  2243. {
  2244. neighbourNode.flags = DtNode.DT_NODE_OPEN;
  2245. m_openList.Push(neighbourNode);
  2246. }
  2247. }
  2248. }
  2249. return DtStatus.DT_SUCCSESS;
  2250. }
  2251. /// @par
  2252. ///
  2253. /// The order of the result set is from least to highest cost.
  2254. ///
  2255. /// At least one result array must be provided.
  2256. ///
  2257. /// A common use case for this method is to perform Dijkstra searches.
  2258. /// Candidate polygons are found by searching the graph beginning at the start
  2259. /// polygon.
  2260. ///
  2261. /// The same intersection test restrictions that apply to findPolysAroundCircle()
  2262. /// method apply to this method.
  2263. ///
  2264. /// The 3D centroid of the search polygon is used as the start position for cost
  2265. /// calculations.
  2266. ///
  2267. /// Intersection tests occur in 2D. All polygons are projected onto the
  2268. /// xz-plane. So the y-values of the vertices do not effect intersection tests.
  2269. ///
  2270. /// If the result arrays are is too small to hold the entire result set, they will
  2271. /// be filled to capacity.
  2272. ///
  2273. /// Finds the polygons along the naviation graph that touch the specified convex polygon.
  2274. /// @param[in] startRef The reference id of the polygon where the search starts.
  2275. /// @param[in] verts The vertices describing the convex polygon. (CCW)
  2276. /// [(x, y, z) * @p nverts]
  2277. /// @param[in] nverts The number of vertices in the polygon.
  2278. /// @param[in] filter The polygon filter to apply to the query.
  2279. /// @param[out] resultRef The reference ids of the polygons touched by the search polygon. [opt]
  2280. /// @param[out] resultParent The reference ids of the parent polygons for each result. Zero if a
  2281. /// result polygon has no parent. [opt]
  2282. /// @param[out] resultCost The search cost from the centroid point to the polygon. [opt]
  2283. /// @param[out] resultCount The number of polygons found.
  2284. /// @param[in] maxResult The maximum number of polygons the result arrays can hold.
  2285. /// @returns The status flags for the query.
  2286. public DtStatus FindPolysAroundShape(long startRef, RcVec3f[] verts, IDtQueryFilter filter,
  2287. ref List<long> resultRef, ref List<long> resultParent, ref List<float> resultCost)
  2288. {
  2289. resultRef.Clear();
  2290. resultParent.Clear();
  2291. resultCost.Clear();
  2292. // Validate input
  2293. int nverts = verts.Length;
  2294. if (!m_nav.IsValidPolyRef(startRef) || null == verts || nverts < 3 || null == filter)
  2295. {
  2296. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  2297. }
  2298. m_nodePool.Clear();
  2299. m_openList.Clear();
  2300. RcVec3f centerPos = RcVec3f.Zero;
  2301. for (int i = 0; i < nverts; ++i)
  2302. {
  2303. centerPos += verts[i];
  2304. }
  2305. float scale = 1.0f / nverts;
  2306. centerPos.x *= scale;
  2307. centerPos.y *= scale;
  2308. centerPos.z *= scale;
  2309. DtNode startNode = m_nodePool.GetNode(startRef);
  2310. startNode.pos = centerPos;
  2311. startNode.pidx = 0;
  2312. startNode.cost = 0;
  2313. startNode.total = 0;
  2314. startNode.id = startRef;
  2315. startNode.flags = DtNode.DT_NODE_OPEN;
  2316. m_openList.Push(startNode);
  2317. while (!m_openList.IsEmpty())
  2318. {
  2319. DtNode bestNode = m_openList.Pop();
  2320. bestNode.flags &= ~DtNode.DT_NODE_OPEN;
  2321. bestNode.flags |= DtNode.DT_NODE_CLOSED;
  2322. // Get poly and tile.
  2323. // The API input has been checked already, skip checking internal data.
  2324. long bestRef = bestNode.id;
  2325. m_nav.GetTileAndPolyByRefUnsafe(bestRef, out var bestTile, out var bestPoly);
  2326. // Get parent poly and tile.
  2327. long parentRef = 0;
  2328. DtMeshTile parentTile = null;
  2329. DtPoly parentPoly = null;
  2330. if (bestNode.pidx != 0)
  2331. {
  2332. parentRef = m_nodePool.GetNodeAtIdx(bestNode.pidx).id;
  2333. }
  2334. if (parentRef != 0)
  2335. {
  2336. m_nav.GetTileAndPolyByRefUnsafe(parentRef, out parentTile, out parentPoly);
  2337. }
  2338. resultRef.Add(bestRef);
  2339. resultParent.Add(parentRef);
  2340. resultCost.Add(bestNode.total);
  2341. for (int i = bestTile.polyLinks[bestPoly.index]; i != DtNavMesh.DT_NULL_LINK; i = bestTile.links[i].next)
  2342. {
  2343. DtLink link = bestTile.links[i];
  2344. long neighbourRef = link.refs;
  2345. // Skip invalid neighbours and do not follow back to parent.
  2346. if (neighbourRef == 0 || neighbourRef == parentRef)
  2347. {
  2348. continue;
  2349. }
  2350. // Expand to neighbour
  2351. m_nav.GetTileAndPolyByRefUnsafe(neighbourRef, out var neighbourTile, out var neighbourPoly);
  2352. // Do not advance if the polygon is excluded by the filter.
  2353. if (!filter.PassFilter(neighbourRef, neighbourTile, neighbourPoly))
  2354. {
  2355. continue;
  2356. }
  2357. // Find edge and calc distance to the edge.
  2358. var ppStatus = GetPortalPoints(bestRef, bestPoly, bestTile, neighbourRef, neighbourPoly,
  2359. neighbourTile, out var va, out var vb);
  2360. if (ppStatus.Failed())
  2361. {
  2362. continue;
  2363. }
  2364. // If the poly is not touching the edge to the next polygon, skip the connection it.
  2365. bool intersects = DtUtils.IntersectSegmentPoly2D(va, vb, verts, nverts, out var tmin, out var tmax, out var segMin, out var segMax);
  2366. if (!intersects)
  2367. {
  2368. continue;
  2369. }
  2370. if (tmin > 1.0f || tmax < 0.0f)
  2371. {
  2372. continue;
  2373. }
  2374. DtNode neighbourNode = m_nodePool.GetNode(neighbourRef);
  2375. if ((neighbourNode.flags & DtNode.DT_NODE_CLOSED) != 0)
  2376. {
  2377. continue;
  2378. }
  2379. // Cost
  2380. if (neighbourNode.flags == 0)
  2381. {
  2382. neighbourNode.pos = RcVec3f.Lerp(va, vb, 0.5f);
  2383. }
  2384. float cost = filter.GetCost(bestNode.pos, neighbourNode.pos, parentRef, parentTile, parentPoly, bestRef,
  2385. bestTile, bestPoly, neighbourRef, neighbourTile, neighbourPoly);
  2386. float total = bestNode.total + cost;
  2387. // The node is already in open list and the new result is worse, skip.
  2388. if ((neighbourNode.flags & DtNode.DT_NODE_OPEN) != 0 && total >= neighbourNode.total)
  2389. {
  2390. continue;
  2391. }
  2392. neighbourNode.id = neighbourRef;
  2393. neighbourNode.pidx = m_nodePool.GetNodeIdx(bestNode);
  2394. neighbourNode.total = total;
  2395. if ((neighbourNode.flags & DtNode.DT_NODE_OPEN) != 0)
  2396. {
  2397. m_openList.Modify(neighbourNode);
  2398. }
  2399. else
  2400. {
  2401. neighbourNode.flags = DtNode.DT_NODE_OPEN;
  2402. m_openList.Push(neighbourNode);
  2403. }
  2404. }
  2405. }
  2406. return DtStatus.DT_SUCCSESS;
  2407. }
  2408. /// @par
  2409. ///
  2410. /// This method is optimized for a small search radius and small number of result
  2411. /// polygons.
  2412. ///
  2413. /// Candidate polygons are found by searching the navigation graph beginning at
  2414. /// the start polygon.
  2415. ///
  2416. /// The same intersection test restrictions that apply to the findPolysAroundCircle
  2417. /// mehtod applies to this method.
  2418. ///
  2419. /// The value of the center point is used as the start point for cost calculations.
  2420. /// It is not projected onto the surface of the mesh, so its y-value will effect
  2421. /// the costs.
  2422. ///
  2423. /// Intersection tests occur in 2D. All polygons and the search circle are
  2424. /// projected onto the xz-plane. So the y-value of the center point does not
  2425. /// effect intersection tests.
  2426. ///
  2427. /// If the result arrays are is too small to hold the entire result set, they will
  2428. /// be filled to capacity.
  2429. ///
  2430. /// Finds the non-overlapping navigation polygons in the local neighbourhood around the center position.
  2431. /// @param[in] startRef The reference id of the polygon where the search starts.
  2432. /// @param[in] centerPos The center of the query circle. [(x, y, z)]
  2433. /// @param[in] radius The radius of the query circle.
  2434. /// @param[in] filter The polygon filter to apply to the query.
  2435. /// @param[out] resultRef The reference ids of the polygons touched by the circle.
  2436. /// @param[out] resultParent The reference ids of the parent polygons for each result.
  2437. /// @returns The status flags for the query.
  2438. public DtStatus FindLocalNeighbourhood(long startRef, RcVec3f centerPos, float radius,
  2439. IDtQueryFilter filter,
  2440. ref List<long> resultRef, ref List<long> resultParent)
  2441. {
  2442. // Validate input
  2443. if (!m_nav.IsValidPolyRef(startRef) || !RcVec3f.IsFinite(centerPos) || radius < 0
  2444. || !float.IsFinite(radius) || null == filter
  2445. || null == resultRef || null == resultParent)
  2446. {
  2447. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  2448. }
  2449. resultRef.Clear();
  2450. resultParent.Clear();
  2451. DtNodePool tinyNodePool = new DtNodePool();
  2452. DtNode startNode = tinyNodePool.GetNode(startRef);
  2453. startNode.pidx = 0;
  2454. startNode.id = startRef;
  2455. startNode.flags = DtNode.DT_NODE_CLOSED;
  2456. LinkedList<DtNode> stack = new LinkedList<DtNode>();
  2457. stack.AddLast(startNode);
  2458. resultRef.Add(startNode.id);
  2459. resultParent.Add(0L);
  2460. float radiusSqr = Sqr(radius);
  2461. float[] pa = new float[m_nav.GetMaxVertsPerPoly() * 3];
  2462. float[] pb = new float[m_nav.GetMaxVertsPerPoly() * 3];
  2463. while (0 < stack.Count)
  2464. {
  2465. // Pop front.
  2466. DtNode curNode = stack.First?.Value;
  2467. stack.RemoveFirst();
  2468. // Get poly and tile.
  2469. // The API input has been checked already, skip checking internal data.
  2470. long curRef = curNode.id;
  2471. m_nav.GetTileAndPolyByRefUnsafe(curRef, out var curTile, out var curPoly);
  2472. for (int i = curTile.polyLinks[curPoly.index]; i != DtNavMesh.DT_NULL_LINK; i = curTile.links[i].next)
  2473. {
  2474. DtLink link = curTile.links[i];
  2475. long neighbourRef = link.refs;
  2476. // Skip invalid neighbours.
  2477. if (neighbourRef == 0)
  2478. {
  2479. continue;
  2480. }
  2481. DtNode neighbourNode = tinyNodePool.GetNode(neighbourRef);
  2482. // Skip visited.
  2483. if ((neighbourNode.flags & DtNode.DT_NODE_CLOSED) != 0)
  2484. {
  2485. continue;
  2486. }
  2487. // Expand to neighbour
  2488. m_nav.GetTileAndPolyByRefUnsafe(neighbourRef, out var neighbourTile, out var neighbourPoly);
  2489. // Skip off-mesh connections.
  2490. if (neighbourPoly.GetPolyType() == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION)
  2491. {
  2492. continue;
  2493. }
  2494. // Do not advance if the polygon is excluded by the filter.
  2495. if (!filter.PassFilter(neighbourRef, neighbourTile, neighbourPoly))
  2496. {
  2497. continue;
  2498. }
  2499. // Find edge and calc distance to the edge.
  2500. var ppStatus = GetPortalPoints(curRef, curPoly, curTile, neighbourRef, neighbourPoly,
  2501. neighbourTile, out var va, out var vb);
  2502. if (ppStatus.Failed())
  2503. {
  2504. continue;
  2505. }
  2506. // If the circle is not touching the next polygon, skip it.
  2507. var distSqr = DtUtils.DistancePtSegSqr2D(centerPos, va, vb, out var _);
  2508. if (distSqr > radiusSqr)
  2509. {
  2510. continue;
  2511. }
  2512. // Mark node visited, this is done before the overlap test so that
  2513. // we will not visit the poly again if the test fails.
  2514. neighbourNode.flags |= DtNode.DT_NODE_CLOSED;
  2515. neighbourNode.pidx = tinyNodePool.GetNodeIdx(curNode);
  2516. // Check that the polygon does not collide with existing polygons.
  2517. // Collect vertices of the neighbour poly.
  2518. int npa = neighbourPoly.vertCount;
  2519. for (int k = 0; k < npa; ++k)
  2520. {
  2521. Array.Copy(neighbourTile.data.verts, neighbourPoly.verts[k] * 3, pa, k * 3, 3);
  2522. }
  2523. bool overlap = false;
  2524. for (int j = 0; j < resultRef.Count; ++j)
  2525. {
  2526. long pastRef = resultRef[j];
  2527. // Connected polys do not overlap.
  2528. bool connected = false;
  2529. for (int k = curTile.polyLinks[curPoly.index]; k != DtNavMesh.DT_NULL_LINK; k = curTile.links[k].next)
  2530. {
  2531. if (curTile.links[k].refs == pastRef)
  2532. {
  2533. connected = true;
  2534. break;
  2535. }
  2536. }
  2537. if (connected)
  2538. {
  2539. continue;
  2540. }
  2541. // Potentially overlapping.
  2542. m_nav.GetTileAndPolyByRefUnsafe(pastRef, out var pastTile, out var pastPoly);
  2543. // Get vertices and test overlap
  2544. int npb = pastPoly.vertCount;
  2545. for (int k = 0; k < npb; ++k)
  2546. {
  2547. Array.Copy(pastTile.data.verts, pastPoly.verts[k] * 3, pb, k * 3, 3);
  2548. }
  2549. if (DtUtils.OverlapPolyPoly2D(pa, npa, pb, npb))
  2550. {
  2551. overlap = true;
  2552. break;
  2553. }
  2554. }
  2555. if (overlap)
  2556. {
  2557. continue;
  2558. }
  2559. resultRef.Add(neighbourRef);
  2560. resultParent.Add(curRef);
  2561. stack.AddLast(neighbourNode);
  2562. }
  2563. }
  2564. return DtStatus.DT_SUCCSESS;
  2565. }
  2566. protected void InsertInterval(List<DtSegInterval> ints, int tmin, int tmax, long refs)
  2567. {
  2568. // Find insertion point.
  2569. int idx = 0;
  2570. while (idx < ints.Count)
  2571. {
  2572. if (tmax <= ints[idx].tmin)
  2573. {
  2574. break;
  2575. }
  2576. idx++;
  2577. }
  2578. // Store
  2579. ints.Insert(idx, new DtSegInterval(refs, tmin, tmax));
  2580. }
  2581. /// @par
  2582. ///
  2583. /// If the @p segmentRefs parameter is provided, then all polygon segments will be returned.
  2584. /// Otherwise only the wall segments are returned.
  2585. ///
  2586. /// A segment that is normally a portal will be included in the result set as a
  2587. /// wall if the @p filter results in the neighbor polygon becoomming impassable.
  2588. ///
  2589. /// The @p segmentVerts and @p segmentRefs buffers should normally be sized for the
  2590. /// maximum segments per polygon of the source navigation mesh.
  2591. ///
  2592. /// Returns the segments for the specified polygon, optionally including portals.
  2593. /// @param[in] ref The reference id of the polygon.
  2594. /// @param[in] filter The polygon filter to apply to the query.
  2595. /// @param[out] segmentVerts The segments. [(ax, ay, az, bx, by, bz) * segmentCount]
  2596. /// @param[out] segmentRefs The reference ids of each segment's neighbor polygon.
  2597. /// Or zero if the segment is a wall. [opt] [(parentRef) * @p segmentCount]
  2598. /// @param[out] segmentCount The number of segments returned.
  2599. /// @param[in] maxSegments The maximum number of segments the result arrays can hold.
  2600. /// @returns The status flags for the query.
  2601. public DtStatus GetPolyWallSegments(long refs, bool storePortals, IDtQueryFilter filter,
  2602. ref List<RcSegmentVert> segmentVerts, ref List<long> segmentRefs)
  2603. {
  2604. segmentVerts.Clear();
  2605. segmentRefs.Clear();
  2606. var status = m_nav.GetTileAndPolyByRef(refs, out var tile, out var poly);
  2607. if (status.Failed())
  2608. {
  2609. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  2610. }
  2611. if (null == filter)
  2612. {
  2613. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  2614. }
  2615. List<DtSegInterval> ints = new List<DtSegInterval>(16);
  2616. for (int i = 0, j = poly.vertCount - 1; i < poly.vertCount; j = i++)
  2617. {
  2618. // Skip non-solid edges.
  2619. ints.Clear();
  2620. if ((poly.neis[j] & DtNavMesh.DT_EXT_LINK) != 0)
  2621. {
  2622. // Tile border.
  2623. for (int k = tile.polyLinks[poly.index]; k != DtNavMesh.DT_NULL_LINK; k = tile.links[k].next)
  2624. {
  2625. DtLink link = tile.links[k];
  2626. if (link.edge == j)
  2627. {
  2628. if (link.refs != 0)
  2629. {
  2630. m_nav.GetTileAndPolyByRefUnsafe(link.refs, out var neiTile, out var neiPoly);
  2631. if (filter.PassFilter(link.refs, neiTile, neiPoly))
  2632. {
  2633. InsertInterval(ints, link.bmin, link.bmax, link.refs);
  2634. }
  2635. }
  2636. }
  2637. }
  2638. }
  2639. else
  2640. {
  2641. // Internal edge
  2642. long neiRef = 0;
  2643. if (poly.neis[j] != 0)
  2644. {
  2645. int idx = (poly.neis[j] - 1);
  2646. neiRef = m_nav.GetPolyRefBase(tile) | (long)idx;
  2647. if (!filter.PassFilter(neiRef, tile, tile.data.polys[idx]))
  2648. {
  2649. neiRef = 0;
  2650. }
  2651. }
  2652. // If the edge leads to another polygon and portals are not stored, skip.
  2653. if (neiRef != 0 && !storePortals)
  2654. {
  2655. continue;
  2656. }
  2657. int ivj = poly.verts[j] * 3;
  2658. int ivi = poly.verts[i] * 3;
  2659. var seg = new RcSegmentVert();
  2660. seg.vmin.Set(tile.data.verts, ivj);
  2661. seg.vmax.Set(tile.data.verts, ivi);
  2662. // Array.Copy(tile.data.verts, ivj, seg, 0, 3);
  2663. // Array.Copy(tile.data.verts, ivi, seg, 3, 3);
  2664. segmentVerts.Add(seg);
  2665. segmentRefs.Add(neiRef);
  2666. continue;
  2667. }
  2668. // Add sentinels
  2669. InsertInterval(ints, -1, 0, 0);
  2670. InsertInterval(ints, 255, 256, 0);
  2671. // Store segments.
  2672. int vj = poly.verts[j] * 3;
  2673. int vi = poly.verts[i] * 3;
  2674. for (int k = 1; k < ints.Count; ++k)
  2675. {
  2676. // Portal segment.
  2677. if (storePortals && ints[k].refs != 0)
  2678. {
  2679. float tmin = ints[k].tmin / 255.0f;
  2680. float tmax = ints[k].tmax / 255.0f;
  2681. var seg = new RcSegmentVert();
  2682. seg.vmin = RcVec3f.Lerp(tile.data.verts, vj, vi, tmin);
  2683. seg.vmax = RcVec3f.Lerp(tile.data.verts, vj, vi, tmax);
  2684. segmentVerts.Add(seg);
  2685. segmentRefs.Add(ints[k].refs);
  2686. }
  2687. // Wall segment.
  2688. int imin = ints[k - 1].tmax;
  2689. int imax = ints[k].tmin;
  2690. if (imin != imax)
  2691. {
  2692. float tmin = imin / 255.0f;
  2693. float tmax = imax / 255.0f;
  2694. var seg = new RcSegmentVert();
  2695. seg.vmin = RcVec3f.Lerp(tile.data.verts, vj, vi, tmin);
  2696. seg.vmax = RcVec3f.Lerp(tile.data.verts, vj, vi, tmax);
  2697. segmentVerts.Add(seg);
  2698. segmentRefs.Add(0L);
  2699. }
  2700. }
  2701. }
  2702. return DtStatus.DT_SUCCSESS;
  2703. }
  2704. /// @par
  2705. ///
  2706. /// @p hitPos is not adjusted using the height detail data.
  2707. ///
  2708. /// @p hitDist will equal the search radius if there is no wall within the
  2709. /// radius. In this case the values of @p hitPos and @p hitNormal are
  2710. /// undefined.
  2711. ///
  2712. /// The normal will become unpredicable if @p hitDist is a very small number.
  2713. ///
  2714. /// Finds the distance from the specified position to the nearest polygon wall.
  2715. /// @param[in] startRef The reference id of the polygon containing @p centerPos.
  2716. /// @param[in] centerPos The center of the search circle. [(x, y, z)]
  2717. /// @param[in] maxRadius The radius of the search circle.
  2718. /// @param[in] filter The polygon filter to apply to the query.
  2719. /// @param[out] hitDist The distance to the nearest wall from @p centerPos.
  2720. /// @param[out] hitPos The nearest position on the wall that was hit. [(x, y, z)]
  2721. /// @param[out] hitNormal The normalized ray formed from the wall point to the
  2722. /// source point. [(x, y, z)]
  2723. /// @returns The status flags for the query.
  2724. public virtual DtStatus FindDistanceToWall(long startRef, RcVec3f centerPos, float maxRadius,
  2725. IDtQueryFilter filter,
  2726. out float hitDist, out RcVec3f hitPos, out RcVec3f hitNormal)
  2727. {
  2728. hitDist = 0;
  2729. hitPos = RcVec3f.Zero;
  2730. hitNormal = RcVec3f.Zero;
  2731. // Validate input
  2732. if (!m_nav.IsValidPolyRef(startRef) || !RcVec3f.IsFinite(centerPos) || maxRadius < 0
  2733. || !float.IsFinite(maxRadius) || null == filter)
  2734. {
  2735. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  2736. }
  2737. m_nodePool.Clear();
  2738. m_openList.Clear();
  2739. DtNode startNode = m_nodePool.GetNode(startRef);
  2740. startNode.pos = centerPos;
  2741. startNode.pidx = 0;
  2742. startNode.cost = 0;
  2743. startNode.total = 0;
  2744. startNode.id = startRef;
  2745. startNode.flags = DtNode.DT_NODE_OPEN;
  2746. m_openList.Push(startNode);
  2747. float radiusSqr = Sqr(maxRadius);
  2748. var hasBestV = false;
  2749. var bestvj = RcVec3f.Zero;
  2750. var bestvi = RcVec3f.Zero;
  2751. var status = DtStatus.DT_SUCCSESS;
  2752. while (!m_openList.IsEmpty())
  2753. {
  2754. DtNode bestNode = m_openList.Pop();
  2755. bestNode.flags &= ~DtNode.DT_NODE_OPEN;
  2756. bestNode.flags |= DtNode.DT_NODE_CLOSED;
  2757. // Get poly and tile.
  2758. // The API input has been checked already, skip checking internal data.
  2759. long bestRef = bestNode.id;
  2760. m_nav.GetTileAndPolyByRefUnsafe(bestRef, out var bestTile, out var bestPoly);
  2761. // Get parent poly and tile.
  2762. long parentRef = 0;
  2763. if (bestNode.pidx != 0)
  2764. {
  2765. parentRef = m_nodePool.GetNodeAtIdx(bestNode.pidx).id;
  2766. }
  2767. // Hit test walls.
  2768. for (int i = 0, j = bestPoly.vertCount - 1; i < bestPoly.vertCount; j = i++)
  2769. {
  2770. // Skip non-solid edges.
  2771. if ((bestPoly.neis[j] & DtNavMesh.DT_EXT_LINK) != 0)
  2772. {
  2773. // Tile border.
  2774. bool solid = true;
  2775. for (int k = bestTile.polyLinks[bestPoly.index]; k != DtNavMesh.DT_NULL_LINK; k = bestTile.links[k].next)
  2776. {
  2777. DtLink link = bestTile.links[k];
  2778. if (link.edge == j)
  2779. {
  2780. if (link.refs != 0)
  2781. {
  2782. m_nav.GetTileAndPolyByRefUnsafe(link.refs, out var neiTile, out var neiPoly);
  2783. if (filter.PassFilter(link.refs, neiTile, neiPoly))
  2784. {
  2785. solid = false;
  2786. }
  2787. }
  2788. break;
  2789. }
  2790. }
  2791. if (!solid)
  2792. {
  2793. continue;
  2794. }
  2795. }
  2796. else if (bestPoly.neis[j] != 0)
  2797. {
  2798. // Internal edge
  2799. int idx = (bestPoly.neis[j] - 1);
  2800. long refs = m_nav.GetPolyRefBase(bestTile) | (long)idx;
  2801. if (filter.PassFilter(refs, bestTile, bestTile.data.polys[idx]))
  2802. {
  2803. continue;
  2804. }
  2805. }
  2806. // Calc distance to the edge.
  2807. int vj = bestPoly.verts[j] * 3;
  2808. int vi = bestPoly.verts[i] * 3;
  2809. var distSqr = DtUtils.DistancePtSegSqr2D(centerPos, bestTile.data.verts, vj, vi, out var tseg);
  2810. // Edge is too far, skip.
  2811. if (distSqr > radiusSqr)
  2812. {
  2813. continue;
  2814. }
  2815. // Hit wall, update radius.
  2816. radiusSqr = distSqr;
  2817. // Calculate hit pos.
  2818. hitPos.x = bestTile.data.verts[vj + 0] + (bestTile.data.verts[vi + 0] - bestTile.data.verts[vj + 0]) * tseg;
  2819. hitPos.y = bestTile.data.verts[vj + 1] + (bestTile.data.verts[vi + 1] - bestTile.data.verts[vj + 1]) * tseg;
  2820. hitPos.z = bestTile.data.verts[vj + 2] + (bestTile.data.verts[vi + 2] - bestTile.data.verts[vj + 2]) * tseg;
  2821. hasBestV = true;
  2822. bestvj = RcVec3f.Of(bestTile.data.verts, vj);
  2823. bestvi = RcVec3f.Of(bestTile.data.verts, vi);
  2824. }
  2825. for (int i = bestTile.polyLinks[bestPoly.index]; i != DtNavMesh.DT_NULL_LINK; i = bestTile.links[i].next)
  2826. {
  2827. DtLink link = bestTile.links[i];
  2828. long neighbourRef = link.refs;
  2829. // Skip invalid neighbours and do not follow back to parent.
  2830. if (neighbourRef == 0 || neighbourRef == parentRef)
  2831. {
  2832. continue;
  2833. }
  2834. // Expand to neighbour.
  2835. m_nav.GetTileAndPolyByRefUnsafe(neighbourRef, out var neighbourTile, out var neighbourPoly);
  2836. // Skip off-mesh connections.
  2837. if (neighbourPoly.GetPolyType() == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION)
  2838. {
  2839. continue;
  2840. }
  2841. // Calc distance to the edge.
  2842. int va = bestPoly.verts[link.edge] * 3;
  2843. int vb = bestPoly.verts[(link.edge + 1) % bestPoly.vertCount] * 3;
  2844. var distSqr = DtUtils.DistancePtSegSqr2D(centerPos, bestTile.data.verts, va, vb, out var tseg);
  2845. // If the circle is not touching the next polygon, skip it.
  2846. if (distSqr > radiusSqr)
  2847. {
  2848. continue;
  2849. }
  2850. if (!filter.PassFilter(neighbourRef, neighbourTile, neighbourPoly))
  2851. {
  2852. continue;
  2853. }
  2854. DtNode neighbourNode = m_nodePool.GetNode(neighbourRef);
  2855. if (null == neighbourNode)
  2856. {
  2857. status |= DtStatus.DT_OUT_OF_NODES;
  2858. continue;
  2859. }
  2860. if ((neighbourNode.flags & DtNode.DT_NODE_CLOSED) != 0)
  2861. {
  2862. continue;
  2863. }
  2864. // Cost
  2865. if (neighbourNode.flags == 0)
  2866. {
  2867. GetEdgeMidPoint(bestRef, bestPoly, bestTile,
  2868. neighbourRef, neighbourPoly, neighbourTile,
  2869. ref neighbourNode.pos);
  2870. }
  2871. float total = bestNode.total + RcVec3f.Distance(bestNode.pos, neighbourNode.pos);
  2872. // The node is already in open list and the new result is worse, skip.
  2873. if ((neighbourNode.flags & DtNode.DT_NODE_OPEN) != 0 && total >= neighbourNode.total)
  2874. {
  2875. continue;
  2876. }
  2877. neighbourNode.id = neighbourRef;
  2878. neighbourNode.flags = (neighbourNode.flags & ~DtNode.DT_NODE_CLOSED);
  2879. neighbourNode.pidx = m_nodePool.GetNodeIdx(bestNode);
  2880. neighbourNode.total = total;
  2881. if ((neighbourNode.flags & DtNode.DT_NODE_OPEN) != 0)
  2882. {
  2883. m_openList.Modify(neighbourNode);
  2884. }
  2885. else
  2886. {
  2887. neighbourNode.flags |= DtNode.DT_NODE_OPEN;
  2888. m_openList.Push(neighbourNode);
  2889. }
  2890. }
  2891. }
  2892. // Calc hit normal.
  2893. if (hasBestV)
  2894. {
  2895. var tangent = bestvi.Subtract(bestvj);
  2896. hitNormal.x = tangent.z;
  2897. hitNormal.y = 0;
  2898. hitNormal.z = -tangent.x;
  2899. hitNormal.Normalize();
  2900. }
  2901. hitDist = (float)Math.Sqrt(radiusSqr);
  2902. return status;
  2903. }
  2904. /// Returns true if the polygon reference is valid and passes the filter restrictions.
  2905. /// @param[in] ref The polygon reference to check.
  2906. /// @param[in] filter The filter to apply.
  2907. public bool IsValidPolyRef(long refs, IDtQueryFilter filter)
  2908. {
  2909. var status = m_nav.GetTileAndPolyByRef(refs, out var tile, out var poly);
  2910. if (status.Failed())
  2911. {
  2912. return false;
  2913. }
  2914. // If cannot pass filter, assume flags has changed and boundary is invalid.
  2915. if (!filter.PassFilter(refs, tile, poly))
  2916. {
  2917. return false;
  2918. }
  2919. return true;
  2920. }
  2921. /// Gets the navigation mesh the query object is using.
  2922. /// @return The navigation mesh the query object is using.
  2923. public DtNavMesh GetAttachedNavMesh()
  2924. {
  2925. return m_nav;
  2926. }
  2927. /**
  2928. * Gets a path from the explored nodes in the previous search.
  2929. *
  2930. * @param endRef
  2931. * The reference id of the end polygon.
  2932. * @returns An ordered list of polygon references representing the path. (Start to end.)
  2933. * @remarks The result of this function depends on the state of the query object. For that reason it should only be
  2934. * used immediately after one of the two Dijkstra searches, findPolysAroundCircle or findPolysAroundShape.
  2935. */
  2936. public DtStatus GetPathFromDijkstraSearch(long endRef, ref List<long> path)
  2937. {
  2938. if (!m_nav.IsValidPolyRef(endRef) || null == path)
  2939. {
  2940. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  2941. }
  2942. path.Clear();
  2943. List<DtNode> nodes = m_nodePool.FindNodes(endRef);
  2944. if (nodes.Count != 1)
  2945. {
  2946. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  2947. }
  2948. DtNode endNode = nodes[0];
  2949. if ((endNode.flags & DT_NODE_CLOSED) == 0)
  2950. {
  2951. return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM;
  2952. }
  2953. return GetPathToNode(endNode, ref path);
  2954. }
  2955. // Gets the path leading to the specified end node.
  2956. protected DtStatus GetPathToNode(DtNode endNode, ref List<long> path)
  2957. {
  2958. // Reverse the path.
  2959. DtNode curNode = endNode;
  2960. do
  2961. {
  2962. path.Add(curNode.id);
  2963. DtNode nextNode = m_nodePool.GetNodeAtIdx(curNode.pidx);
  2964. if (curNode.shortcut != null)
  2965. {
  2966. // remove potential duplicates from shortcut path
  2967. for (int i = curNode.shortcut.Count - 1; i >= 0; i--)
  2968. {
  2969. long id = curNode.shortcut[i];
  2970. if (id != curNode.id && id != nextNode.id)
  2971. {
  2972. path.Add(id);
  2973. }
  2974. }
  2975. }
  2976. curNode = nextNode;
  2977. } while (curNode != null);
  2978. path.Reverse();
  2979. return DtStatus.DT_SUCCSESS;
  2980. }
  2981. /**
  2982. * The closed list is the list of polygons that were fully evaluated during the last navigation graph search. (A* or
  2983. * Dijkstra)
  2984. */
  2985. public bool IsInClosedList(long refs)
  2986. {
  2987. if (m_nodePool == null)
  2988. {
  2989. return false;
  2990. }
  2991. foreach (DtNode n in m_nodePool.FindNodes(refs))
  2992. {
  2993. if ((n.flags & DT_NODE_CLOSED) != 0)
  2994. {
  2995. return true;
  2996. }
  2997. }
  2998. return false;
  2999. }
  3000. public DtNodePool GetNodePool()
  3001. {
  3002. return m_nodePool;
  3003. }
  3004. }
  3005. }