DtPathQueue.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.Collections.Generic;
  20. using DotRecast.Core;
  21. namespace DotRecast.Detour.Crowd
  22. {
  23. public class DtPathQueue
  24. {
  25. private readonly DtCrowdConfig config;
  26. private readonly LinkedList<DtPathQuery> queue = new LinkedList<DtPathQuery>();
  27. public DtPathQueue(DtCrowdConfig config)
  28. {
  29. this.config = config;
  30. }
  31. public void Update(DtNavMesh navMesh)
  32. {
  33. // Update path request until there is nothing to update or up to maxIters pathfinder iterations has been
  34. // consumed.
  35. int iterCount = config.maxFindPathIterations;
  36. while (iterCount > 0)
  37. {
  38. DtPathQuery q = queue.First?.Value;
  39. if (q == null)
  40. {
  41. break;
  42. }
  43. queue.RemoveFirst();
  44. // Handle query start.
  45. if (q.result.status.IsEmpty())
  46. {
  47. q.navQuery = new DtNavMeshQuery(navMesh);
  48. q.result.status = q.navQuery.InitSlicedFindPath(q.startRef, q.endRef, q.startPos, q.endPos, q.filter, 0);
  49. }
  50. // Handle query in progress.
  51. if (q.result.status.InProgress())
  52. {
  53. q.result.status = q.navQuery.UpdateSlicedFindPath(iterCount, out var iters);
  54. iterCount -= iters;
  55. }
  56. if (q.result.status.Succeeded())
  57. {
  58. q.result.status = q.navQuery.FinalizeSlicedFindPath(ref q.result.path);
  59. }
  60. if (!(q.result.status.Failed() || q.result.status.Succeeded()))
  61. {
  62. queue.AddFirst(q);
  63. }
  64. }
  65. }
  66. public DtPathQueryResult Request(long startRef, long endRef, RcVec3f startPos, RcVec3f endPos, IDtQueryFilter filter)
  67. {
  68. if (queue.Count >= config.pathQueueSize)
  69. {
  70. return null;
  71. }
  72. DtPathQuery q = new DtPathQuery();
  73. q.startPos = startPos;
  74. q.startRef = startRef;
  75. q.endPos = endPos;
  76. q.endRef = endRef;
  77. q.filter = filter;
  78. queue.AddLast(q);
  79. return q.result;
  80. }
  81. }
  82. }