123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- var t = require;
- var e = module;
- var i = exports;
- Object.defineProperty(i, "__esModule", {value: !0}),
- (i.default = class {
- constructor(t) {
- (this.COST_STRAIGHT = 10),
- (this.COST_DIAGONAL = 14),
- (this.maxStep = 1e3),
- (this._round = [
- [0, -1],
- [1, -1],
- [1, 0],
- [1, 1],
- [0, 1],
- [-1, 1],
- [-1, 0],
- [-1, -1]
- ]),
- (this.handle = -1),
- (this.optimize = !0),
- (this._roadNodes = t);
- }
- seekPath(t, e) {
- if (
- ((this._startNode = t),
- (this._currentNode = t),
- (this._targetNode = e),
- !this._startNode || !this._targetNode)
- )
- return [];
- if (1 == this._targetNode.value) return [];
- (this._openlist = []), (this._closelist = []);
- for (var i = 0; ; ) {
- if (i > this.maxStep) return [];
- if ((i++, this.searchRoundNodes(this._currentNode), 0 == this._openlist.length)) return [];
- if (
- (this._openlist.sort(this.sortNode),
- (this._currentNode = this._openlist.shift()),
- this._currentNode == this._targetNode)
- )
- return this.getPath();
- this._closelist.push(this._currentNode);
- }
- return [];
- }
- seekPath2(t, e) {
- if (
- ((this._startNode = t),
- (this._currentNode = t),
- (this._targetNode = e),
- !this._startNode || !this._targetNode)
- )
- return [];
- (this._openlist = []), (this._closelist = []);
- for (var i = 0, s = null; ; ) {
- if (i > this.maxStep) return this.seekPath(t, s);
- if ((i++, this.searchRoundNodes(this._currentNode), 0 == this._openlist.length))
- return this.seekPath(t, s);
- if (
- (this._openlist.sort(this.sortNode),
- (this._currentNode = this._openlist.shift()),
- (null == s || this._currentNode.h < s.h) && (s = this._currentNode),
- this._currentNode == this._targetNode)
- )
- return this.getPath();
- this._closelist.push(this._currentNode);
- }
- return this.seekPath(t, s);
- }
- sortNode(t, e) {
- return t.f < e.f ? -1 : t.f > e.f ? 1 : 0;
- }
- getPath() {
- for (var t = [], e = this._targetNode; e != this._startNode; ) t.unshift(e), (e = e.parent);
- if ((t.unshift(this._startNode), !this.optimize)) return t;
- for (var i = 1; i < t.length - 1; i++) {
- var s = t[i - 1],
- o = t[i],
- a = t[i + 1],
- n = o.cx == s.cx && o.cx == a.cx,
- r = o.cy == s.cy && o.cy == a.cy,
- o = ((o.cx - s.cx) / (o.cy - s.cy)) * ((a.cx - o.cx) / (a.cy - o.cy)) == 1;
- (n || r || o) && (t.splice(i, 1), i--);
- }
- return t;
- }
- testSeekPathStep(t, e, i, s, o = 100) {
- var a;
- (this._startNode = t),
- (this._currentNode = t),
- (this._targetNode = e),
- 1 != this._targetNode.value &&
- ((this._openlist = []),
- (this._closelist = []),
- (a = 0),
- clearInterval(this.handle),
- (this.handle = setInterval(() => {
- a > this.maxStep
- ? clearInterval(this.handle)
- : (a++,
- this.searchRoundNodes(this._currentNode),
- 0 != this._openlist.length
- ? (this._openlist.sort(this.sortNode),
- (this._currentNode = this._openlist.shift()),
- this._currentNode == this._targetNode
- ? (clearInterval(this.handle),
- i.apply(s, [
- this._startNode,
- this._targetNode,
- this._currentNode,
- this._openlist,
- this._closelist,
- this.getPath()
- ]))
- : (this._closelist.push(this._currentNode),
- i.apply(s, [
- this._startNode,
- this._targetNode,
- this._currentNode,
- this._openlist,
- this._closelist,
- null
- ])))
- : clearInterval(this.handle));
- }, o)));
- }
- searchRoundNodes(t) {
- for (var e = 0; e < this._round.length; e++) {
- var i = t.cx + this._round[e][0],
- s = t.cy + this._round[e][1],
- s = this._roadNodes[i + "_" + s];
- null == s ||
- s == this._startNode ||
- 1 == s.value ||
- this.isInCloseList(s) ||
- this.inInCorner(s) ||
- this.setNodeF(s);
- }
- }
- setNodeF(t) {
- var e =
- t.cx == this._currentNode.cx || t.cy == this._currentNode.cy
- ? this._currentNode.g + this.COST_STRAIGHT
- : this._currentNode.g + this.COST_DIAGONAL;
- if (this.isInOpenList(t)) {
- if (!(e < t.g)) return;
- t.g = e;
- } else (t.g = e), this._openlist.push(t);
- (t.parent = this._currentNode),
- (t.h =
- (Math.abs(this._targetNode.cx - t.cx) + Math.abs(this._targetNode.cy - t.cy)) * this.COST_STRAIGHT),
- (t.f = t.g + t.h);
- }
- isInOpenList(t) {
- return -1 != this._openlist.indexOf(t);
- }
- isInCloseList(t) {
- return -1 != this._closelist.indexOf(t);
- }
- inInCorner(t) {
- if (t.cx == this._currentNode.cx || t.cy == this._currentNode.cy) return !1;
- var e = this._roadNodes[this._currentNode.cx + "_" + t.cy],
- t = this._roadNodes[t.cx + "_" + this._currentNode.cy];
- return null == e || 0 != e.value || null == t || 0 != t.value;
- }
- dispose() {
- (this._roadNodes = null), (this._round = null);
- }
- });
|