AstarHoneycombRoadSeeker.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 = 10),
  9. (this.maxStep = 1e3),
  10. (this._round1 = [
  11. [0, -1],
  12. [1, -1],
  13. [1, 0],
  14. [0, 1],
  15. [-1, 0],
  16. [-1, -1]
  17. ]),
  18. (this._round2 = [
  19. [0, -1],
  20. [1, 0],
  21. [1, 1],
  22. [0, 1],
  23. [-1, 1],
  24. [-1, 0]
  25. ]),
  26. (this.handle = -1),
  27. (this.optimize = !0),
  28. (this._roadNodes = t);
  29. }
  30. seekPath(t, e) {
  31. if (
  32. ((this._startNode = t),
  33. (this._currentNode = t),
  34. (this._targetNode = e),
  35. !this._startNode || !this._targetNode)
  36. )
  37. return [];
  38. if (1 == this._targetNode.value) return console.log("目标不可达到:"), [];
  39. (this._openlist = []), (this._closelist = []);
  40. for (var i = 0; ; ) {
  41. if (i > this.maxStep) return [];
  42. if ((i++, this.searchRoundNodes(this._currentNode), 0 == this._openlist.length)) return [];
  43. if (
  44. (this._openlist.sort(this.sortNode),
  45. (this._currentNode = this._openlist.shift()),
  46. this._currentNode == this._targetNode)
  47. )
  48. return this.getPath();
  49. this._closelist.push(this._currentNode);
  50. }
  51. return [];
  52. }
  53. seekPath2(t, e) {
  54. if (
  55. ((this._startNode = t),
  56. (this._currentNode = t),
  57. (this._targetNode = e),
  58. !this._startNode || !this._targetNode)
  59. )
  60. return [];
  61. (this._openlist = []), (this._closelist = []);
  62. for (var i = 0, s = null; ; ) {
  63. if (i > this.maxStep) return console.log("没找到目标计算步骤为:", i), this.seekPath(t, s);
  64. if ((i++, this.searchRoundNodes(this._currentNode), 0 == this._openlist.length))
  65. return console.log("没找到目标计算步骤为:", i), this.seekPath(t, s);
  66. if (
  67. (this._openlist.sort(this.sortNode),
  68. (this._currentNode = this._openlist.shift()),
  69. (null == s || this._currentNode.h < s.h) && (s = this._currentNode),
  70. this._currentNode == this._targetNode)
  71. )
  72. return console.log("找到目标计算步骤为:", i), this.getPath();
  73. this._closelist.push(this._currentNode);
  74. }
  75. return this.seekPath(t, s);
  76. }
  77. sortNode(t, e) {
  78. return t.f < e.f ? -1 : t.f > e.f ? 1 : 0;
  79. }
  80. getPath() {
  81. for (var t = [], e = this._targetNode; e != this._startNode; ) t.unshift(e), (e = e.parent);
  82. if ((t.unshift(this._startNode), !this.optimize)) return t;
  83. for (var i = 1; i < t.length - 1; i++) {
  84. var s = t[i - 1],
  85. o = t[i],
  86. a = t[i + 1],
  87. n = !1,
  88. r = null;
  89. (n = (r =
  90. 2 == Math.abs(a.cx - s.cx) && s.cy == a.cy
  91. ? o.cx % 2 == 0
  92. ? o.cy == s.cy
  93. ? this._roadNodes[o.cx + "_" + (o.cy + 1)]
  94. : this._roadNodes[o.cx + "_" + (o.cy - 1)]
  95. : o.cy == s.cy
  96. ? this._roadNodes[o.cx + "_" + (o.cy - 1)]
  97. : this._roadNodes[o.cx + "_" + (o.cy + 1)]
  98. : r)
  99. ? 1 != r.value
  100. : n) && (t.splice(i, 1), i--);
  101. }
  102. for (i = 1; i < t.length - 1; i++) {
  103. (s = t[i - 1]), (o = t[i]), (a = t[i + 1]);
  104. var l = o.cx == s.cx && o.cx == a.cx,
  105. h = o.cy == s.cy && o.cy == a.cy && s.cx % 2 == o.cx % 2 && o.cx % 2 == a.cx % 2,
  106. c =
  107. s.cy - Math.floor(s.cx / 2) == o.cy - Math.floor(o.cx / 2) &&
  108. o.cy - Math.floor(o.cx / 2) == a.cy - Math.floor(a.cx / 2),
  109. d =
  110. s.cy + Math.ceil(s.cx / 2) == o.cy + Math.ceil(o.cx / 2) &&
  111. o.cy + Math.ceil(o.cx / 2) == a.cy + Math.ceil(a.cx / 2);
  112. (l || h || c || d) && (t.splice(i, 1), i--);
  113. }
  114. return t;
  115. }
  116. testSeekPathStep(t, e, i, s, o = 100) {
  117. var a;
  118. (this._startNode = t),
  119. (this._currentNode = t),
  120. (this._targetNode = e),
  121. 1 != this._targetNode.value &&
  122. ((this._openlist = []),
  123. (this._closelist = []),
  124. (a = 0),
  125. clearInterval(this.handle),
  126. (this.handle = setInterval(
  127. () =>
  128. a > this.maxStep
  129. ? (console.log("没找到目标计算步骤为:", a), void clearInterval(this.handle))
  130. : (a++,
  131. this.searchRoundNodes(this._currentNode),
  132. 0 == this._openlist.length
  133. ? (console.log("没找到目标计算步骤为:", a), void clearInterval(this.handle))
  134. : (this._openlist.sort(this.sortNode),
  135. (this._currentNode = this._openlist.shift()),
  136. void (this._currentNode == this._targetNode
  137. ? (console.log("找到目标计算步骤为:", a),
  138. clearInterval(this.handle),
  139. i.apply(s, [
  140. this._startNode,
  141. this._targetNode,
  142. this._currentNode,
  143. this._openlist,
  144. this._closelist,
  145. this.getPath()
  146. ]))
  147. : (this._closelist.push(this._currentNode),
  148. i.apply(s, [
  149. this._startNode,
  150. this._targetNode,
  151. this._currentNode,
  152. this._openlist,
  153. this._closelist,
  154. null
  155. ]))))),
  156. o
  157. )));
  158. }
  159. searchRoundNodes(t) {
  160. for (var e = t.cx % 2 == 0 ? this._round1 : this._round2, i = 0; i < e.length; i++) {
  161. var s = t.cx + e[i][0],
  162. o = t.cy + e[i][1],
  163. o = this._roadNodes[s + "_" + o];
  164. null == o || o == this._startNode || 1 == o.value || this.isInCloseList(o) || this.setNodeF(o);
  165. }
  166. }
  167. setNodeF(t) {
  168. var e =
  169. t.cx == this._currentNode.cx || t.cy == this._currentNode.cy
  170. ? this._currentNode.g + this.COST_STRAIGHT
  171. : this._currentNode.g + this.COST_DIAGONAL;
  172. if (this.isInOpenList(t)) {
  173. if (!(e < t.g)) return;
  174. t.g = e;
  175. } else (t.g = e), this._openlist.push(t);
  176. (t.parent = this._currentNode),
  177. (t.h =
  178. (Math.abs(this._targetNode.cx - t.cx) + Math.abs(this._targetNode.cy - t.cy)) * this.COST_STRAIGHT),
  179. (t.f = t.g + t.h);
  180. }
  181. isInOpenList(t) {
  182. return -1 != this._openlist.indexOf(t);
  183. }
  184. isInCloseList(t) {
  185. return -1 != this._closelist.indexOf(t);
  186. }
  187. inInCorner(t) {
  188. if (t.cx == this._currentNode.cx || t.cy == this._currentNode.cy) return !1;
  189. var e = this._roadNodes[this._currentNode.cx + "_" + t.cy],
  190. t = this._roadNodes[t.cx + "_" + this._currentNode.cy];
  191. return null == e || 0 != e.value || null == t || 0 != t.value;
  192. }
  193. dispose() {
  194. (this._roadNodes = null), (this._round1 = null), (this._round2 = null);
  195. }
  196. });