AStarRoadSeeker.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. var t = require;
  2. var e = module;
  3. var i = exports;
  4. Object.defineProperty(i, "__esModule", {value: !0}),
  5. (i.default = class {
  6. constructor(t) {
  7. (this.COST_STRAIGHT = 10),
  8. (this.COST_DIAGONAL = 14),
  9. (this.maxStep = 1e3),
  10. (this._round = [
  11. [0, -1],
  12. [1, -1],
  13. [1, 0],
  14. [1, 1],
  15. [0, 1],
  16. [-1, 1],
  17. [-1, 0],
  18. [-1, -1]
  19. ]),
  20. (this.handle = -1),
  21. (this.optimize = !0),
  22. (this._roadNodes = t);
  23. }
  24. seekPath(t, e) {
  25. if (
  26. ((this._startNode = t),
  27. (this._currentNode = t),
  28. (this._targetNode = e),
  29. !this._startNode || !this._targetNode)
  30. )
  31. return [];
  32. if (1 == this._targetNode.value) return [];
  33. (this._openlist = []), (this._closelist = []);
  34. for (var i = 0; ; ) {
  35. if (i > this.maxStep) return [];
  36. if ((i++, this.searchRoundNodes(this._currentNode), 0 == this._openlist.length)) return [];
  37. if (
  38. (this._openlist.sort(this.sortNode),
  39. (this._currentNode = this._openlist.shift()),
  40. this._currentNode == this._targetNode)
  41. )
  42. return this.getPath();
  43. this._closelist.push(this._currentNode);
  44. }
  45. return [];
  46. }
  47. seekPath2(t, e) {
  48. if (
  49. ((this._startNode = t),
  50. (this._currentNode = t),
  51. (this._targetNode = e),
  52. !this._startNode || !this._targetNode)
  53. )
  54. return [];
  55. (this._openlist = []), (this._closelist = []);
  56. for (var i = 0, s = null; ; ) {
  57. if (i > this.maxStep) return this.seekPath(t, s);
  58. if ((i++, this.searchRoundNodes(this._currentNode), 0 == this._openlist.length))
  59. return this.seekPath(t, s);
  60. if (
  61. (this._openlist.sort(this.sortNode),
  62. (this._currentNode = this._openlist.shift()),
  63. (null == s || this._currentNode.h < s.h) && (s = this._currentNode),
  64. this._currentNode == this._targetNode)
  65. )
  66. return this.getPath();
  67. this._closelist.push(this._currentNode);
  68. }
  69. return this.seekPath(t, s);
  70. }
  71. sortNode(t, e) {
  72. return t.f < e.f ? -1 : t.f > e.f ? 1 : 0;
  73. }
  74. getPath() {
  75. for (var t = [], e = this._targetNode; e != this._startNode; ) t.unshift(e), (e = e.parent);
  76. if ((t.unshift(this._startNode), !this.optimize)) return t;
  77. for (var i = 1; i < t.length - 1; i++) {
  78. var s = t[i - 1],
  79. o = t[i],
  80. a = t[i + 1],
  81. n = o.cx == s.cx && o.cx == a.cx,
  82. r = o.cy == s.cy && o.cy == a.cy,
  83. o = ((o.cx - s.cx) / (o.cy - s.cy)) * ((a.cx - o.cx) / (a.cy - o.cy)) == 1;
  84. (n || r || o) && (t.splice(i, 1), i--);
  85. }
  86. return t;
  87. }
  88. testSeekPathStep(t, e, i, s, o = 100) {
  89. var a;
  90. (this._startNode = t),
  91. (this._currentNode = t),
  92. (this._targetNode = e),
  93. 1 != this._targetNode.value &&
  94. ((this._openlist = []),
  95. (this._closelist = []),
  96. (a = 0),
  97. clearInterval(this.handle),
  98. (this.handle = setInterval(() => {
  99. a > this.maxStep
  100. ? clearInterval(this.handle)
  101. : (a++,
  102. this.searchRoundNodes(this._currentNode),
  103. 0 != this._openlist.length
  104. ? (this._openlist.sort(this.sortNode),
  105. (this._currentNode = this._openlist.shift()),
  106. this._currentNode == this._targetNode
  107. ? (clearInterval(this.handle),
  108. i.apply(s, [
  109. this._startNode,
  110. this._targetNode,
  111. this._currentNode,
  112. this._openlist,
  113. this._closelist,
  114. this.getPath()
  115. ]))
  116. : (this._closelist.push(this._currentNode),
  117. i.apply(s, [
  118. this._startNode,
  119. this._targetNode,
  120. this._currentNode,
  121. this._openlist,
  122. this._closelist,
  123. null
  124. ])))
  125. : clearInterval(this.handle));
  126. }, o)));
  127. }
  128. searchRoundNodes(t) {
  129. for (var e = 0; e < this._round.length; e++) {
  130. var i = t.cx + this._round[e][0],
  131. s = t.cy + this._round[e][1],
  132. s = this._roadNodes[i + "_" + s];
  133. null == s ||
  134. s == this._startNode ||
  135. 1 == s.value ||
  136. this.isInCloseList(s) ||
  137. this.inInCorner(s) ||
  138. this.setNodeF(s);
  139. }
  140. }
  141. setNodeF(t) {
  142. var e =
  143. t.cx == this._currentNode.cx || t.cy == this._currentNode.cy
  144. ? this._currentNode.g + this.COST_STRAIGHT
  145. : this._currentNode.g + this.COST_DIAGONAL;
  146. if (this.isInOpenList(t)) {
  147. if (!(e < t.g)) return;
  148. t.g = e;
  149. } else (t.g = e), this._openlist.push(t);
  150. (t.parent = this._currentNode),
  151. (t.h =
  152. (Math.abs(this._targetNode.cx - t.cx) + Math.abs(this._targetNode.cy - t.cy)) * this.COST_STRAIGHT),
  153. (t.f = t.g + t.h);
  154. }
  155. isInOpenList(t) {
  156. return -1 != this._openlist.indexOf(t);
  157. }
  158. isInCloseList(t) {
  159. return -1 != this._closelist.indexOf(t);
  160. }
  161. inInCorner(t) {
  162. if (t.cx == this._currentNode.cx || t.cy == this._currentNode.cy) return !1;
  163. var e = this._roadNodes[this._currentNode.cx + "_" + t.cy],
  164. t = this._roadNodes[t.cx + "_" + this._currentNode.cy];
  165. return null == e || 0 != e.value || null == t || 0 != t.value;
  166. }
  167. dispose() {
  168. (this._roadNodes = null), (this._round = null);
  169. }
  170. });