suyiScreen/public/htWeb/lib/plugin/ht-astar.js

1 line
9.5 KiB
JavaScript

!function(V,h,H){"use strict";var g=V.ht;g.Astar={},function(n){var _=n();V.AStar=_.astar,V.AStar.Graph=_.Graph}(function(){function G(b){for(var I=b,C=[];I.parent;)C.unshift(I),I=I.parent;return C}function L(){return new a(function(r){return r.f})}function _(E,o){o=o||{},this.nodes=[],this.diagonal=!!o.diagonal,this.grid=[];for(var y=0;y<E.length;y++){this.grid[y]=[];for(var W=0,x=E[y];W<x.length;W++){var C=new B(y,W,x[W]);this.grid[y][W]=C,this.nodes.push(C)}}this.$13x()}function B(B,$,n){this.x=B,this.y=$,this.weight=n}function a(g){this.content=[],this.scoreFunction=g}var Z={search:function(z,b,I,u){z.$6x(),u=u||{};var x=u.heuristic;x||(x=z.diagonal?Z.$4x.diagonal:Z.$4x.manhattan);var c=u.closest||!1,Q=u.punish,o=L(),f=b;for(b.h=x(b,I),z.$7x(b),o.push(b);o.size()>0;){var E=o.pop();if(E===I)return G(E);E.closed=!0;for(var W=z.neighbors(E),M=0,n=W.length;n>M;++M){var i=W[M];if(!i.closed&&!i.$9x()){var K=E.g+i.getCost(E,Q),r=i.visited;(!r||K<i.g)&&(i.visited=!0,i.parent=E,i.h=i.h||x(i,I),i.g=K,i.f=i.g+i.h,z.$7x(i),c&&(i.h<f.h||i.h===f.h&&i.g<f.g)&&(f=i),r?o.$10x(i):o.push(i))}}}return c?G(f):[]},$4x:{manhattan:function(p,K){var H=Math.abs(K.x-p.x),P=Math.abs(K.y-p.y);return H+P},diagonal:function(V,K){var u=1,$=Math.sqrt(2),P=Math.abs(K.x-V.x),R=Math.abs(K.y-V.y);return u*(P+R)+($-2*u)*Math.min(P,R)}},$5x:function(P){P.f=0,P.g=0,P.h=0,P.visited=!1,P.closed=!1,P.parent=null}};return _.prototype.$13x=function(){this.dirtyNodes=[];for(var L=0;L<this.nodes.length;L++)Z.$5x(this.nodes[L])},_.prototype.$6x=function(){for(var p=0;p<this.dirtyNodes.length;p++)Z.$5x(this.dirtyNodes[p]);this.dirtyNodes=[]},_.prototype.$7x=function(L){this.dirtyNodes.push(L)},_.prototype.neighbors=function(W){var b=[],t=W.x,r=W.y,C=this.grid;return C[t-1]&&C[t-1][r]&&b.push(C[t-1][r]),C[t+1]&&C[t+1][r]&&b.push(C[t+1][r]),C[t]&&C[t][r-1]&&b.push(C[t][r-1]),C[t]&&C[t][r+1]&&b.push(C[t][r+1]),this.diagonal&&(C[t-1]&&C[t-1][r-1]&&b.push(C[t-1][r-1]),C[t+1]&&C[t+1][r-1]&&b.push(C[t+1][r-1]),C[t-1]&&C[t-1][r+1]&&b.push(C[t-1][r+1]),C[t+1]&&C[t+1][r+1]&&b.push(C[t+1][r+1])),b},_.prototype.toString=function(){for(var e=[],P=this.grid,w=0;w<P.length;w++){for(var a=[],y=P[w],r=0;r<y.length;r++)a.push(y[r].weight);e.push(a.join(" "))}return e.join("\n")},B.prototype.toString=function(){return"["+this.x+" "+this.y+"]"},B.prototype.getCost=function(V,A){var d;if(d=V&&V.x!=this.x&&V.y!=this.y?1.41421*this.weight:this.weight,A){var S=V;if(!S)return d;var C=S.parent;if(!C)return d;if(Math.abs(C.x+this.x-2*S.x)>1e-5)return d+A;if(Math.abs(C.y+this.y-2*S.y)>1e-5)return d+A}return d},B.prototype.$9x=function(){return 0===this.weight},a.prototype={push:function(g){this.content.push(g),this.$11x(this.content.length-1)},pop:function(){var q=this.content[0],X=this.content.pop();return this.content.length>0&&(this.content[0]=X,this.$12x(0)),q},remove:function(o){var H=this.content.indexOf(o),z=this.content.pop();H!==this.content.length-1&&(this.content[H]=z,this.scoreFunction(z)<this.scoreFunction(o)?this.$11x(H):this.$12x(H))},size:function(){return this.content.length},$10x:function(j){this.$11x(this.content.indexOf(j))},$11x:function(s){for(var M=this.content[s];s>0;){var d=(s+1>>1)-1,Q=this.content[d];if(!(this.scoreFunction(M)<this.scoreFunction(Q)))break;this.content[d]=M,this.content[s]=Q,s=d}},$12x:function(a){for(var v=this.content.length,F=this.content[a],o=this.scoreFunction(F);;){var p,D=a+1<<1,r=D-1,W=null;if(v>r){var N=this.content[r];p=this.scoreFunction(N),o>p&&(W=r)}if(v>D){var A=this.content[D],Q=this.scoreFunction(A);(null===W?o:p)>Q&&(W=D)}if(null===W)break;this.content[a]=this.content[W],this.content[W]=F,a=W}}},{astar:Z,Graph:_}});var w=g.Astar.Finder=function(q,T){var D=this;D.gv=q,D.$13x(T),D.refreshMap()};w.prototype={},w.prototype.constructor=w,h.defineProperties(w.prototype,{_debugInfo:{get:function(){return this.gv._astarDebugInfo},set:function(q){this.gv._astarDebugInfo=q}}}),w.prototype.$13x=function(r){var Z=this,f=Z.$14x={};r=r||{};var T=r.gridSize||10;f.gridSizeX=r.gridSizeX||T,f.gridSizeY=r.gridSizeY||T,f.rect=r.rect,f.filter=r.filter,f.extendBlocks=r.extendBlocks||3,f.fastOverlap=r.fastOverlap===H?!0:r.fastOverlap,f.nodeRectExtend=r.nodeRectExtend||0,f.diagonal=r.diagonal===H?!0:r.diagonal,f.turnPunish=r.turnPunish===H?.1:r.turnPunish,f.simplify=r.simplify===H?!0:r.simplify,f.toGridCenter=r.toGridCenter===H?!1:r.toGridCenter,f.closest=r.closest===H?!0:r.closest},w.prototype.refreshMap=function(){var _,C=this,q=C.$14x,o=q.gridSizeX,U=q.gridSizeY,B=q.extendBlocks;q.rect?_=q.rect:(_=C.$15x(),g.Default.grow(_,o*B,U*B)),g.Default.grow(_,o,U);var f=Math.floor(_.x/o)*o,k=Math.floor(_.y/U)*U,d=Math.ceil(_.width/o),p=Math.ceil(_.height/U),L=d*o,N=p*U;C.$16x={x:f,y:k,w:L,h:N,gridX:o,gridY:U,xLen:d,yLen:p},C.$17x(),C.$21x()},w.prototype.$15x=function(){var k=this.gv;if(k instanceof g.graph.GraphView)return k.getContentRect();var N,x=g.Default.unionRect;return k.dm().each(function(F){k.isVisible(F)&&F.getRect&&(N=x(N,F.getRect()))}),N},w.prototype.$17x=function(){var P,K,s,t,c=this,L=c.$16x,l=L.xLen,Z=L.yLen;for(P=new Array(l),s=0;l>s;s++)for(P[s]=K=new Array(Z),t=Z;t--;)K[t]=1;var S=c.$14x.filter;for(c.gv.dm().each(function(m){S&&S(m)===!1||(!c._debugInfo||m!==c._debugInfo.grid&&m!=c._debugInfo.path)&&c.$1cw(P,m)}),s=0;l>s;s++)P[s][0]=1,P[s][Z-1]=1;for(t=0;Z>t;t++)P[0][t]=1,P[l-1][t]=1;c.grid=P;var b=new AStar.Graph(P,{diagonal:c.$14x.diagonal});c.graph=b},w.prototype.$18x=function(N,q,e,S,W,o,z,E){var P=this.$19x,O=P(e-N,W-z,S-q,o-E);if(1e-6>=O&&O>=-1e-6)return!1;var s=P(W-N,W-z,o-q,o-E)/O;if(0>s||s>1)return!1;var j=P(e-N,W-N,S-q,o-q)/O;return 0>j||j>1?!1:!0},w.prototype.$19x=function(C,q,j,X){return C*X-q*j},w.prototype.$1aw=function(V,K,h,J){for(var G=[-(K[1]-V[1]),K[0]-V[0]],P=G[0]*(h[0]-V[0])+G[1]*(h[1]-V[1])>=0,q=0,t=J.length;t>q;q+=2)if(G[0]*(J[q]-V[0])+G[1]*(J[q+1]-V[1])>=0===P)return!1;return!0},w.prototype.$1bw=function(A,h){for(var $,f,j=this,u=0,N=A.length;N>u;u+=2)if($=u===N-2?0:u+2,f=$===N-2?0:$+2,j.$1aw([A[u],A[u+1]],[A[$],A[$+1]],[A[f],A[f+1]],h))return!1;for(var u=0,N=h.length;N>u;u+=2)if($=u===N-2?0:u+2,f=$===N-2?0:$+2,j.$1aw([h[u],h[u+1]],[h[$],h[$+1]],[h[f],h[f+1]],A))return!1;return!0},w.prototype.$1cw=function(h,x){if(x.getRect){var r=this,$=x.getRect();g.Default.grow($,r.$14x.nodeRectExtend);var Q,k,m,c,z,B,e,X,j,L,_,p=r.$1dw({x:$.x,y:$.y}),J=r.$1dw({x:$.x+$.width,y:$.y+$.height}),H=r.$14x.fastOverlap;if(!H){var V=x.getCorners();e=[],V.forEach(function(x){e.push(x.x,x.y)}),L=r.$14x.gridSizeX/2,_=r.$14x.gridSizeY/2}for(k=Math.max(0,p.x),m=Math.min(J.x,h.length-1),z=Math.max(0,p.y),B=Math.min(J.y,h.length?h[0].length-1:-1),Q=k;m>=Q;Q++)for(c=z;B>=c;c++)if(H)h[Q][c]=0;else{j=r.$1ew({x:Q,y:c});var u=j.x-L,P=j.x+L,E=j.y-_,W=j.y+_;X=[u,E,u,W,P,W,P,E],r.$1bw(e,X)&&(h[Q][c]=0)}}},w.prototype.$1dw=function(G,A){var r=this,H=r.$16x,Q=(G.x-H.x)/H.gridX,v=(G.y-H.y)/H.gridY;return A!==!1&&(Q=Math.round(Q),v=Math.round(v)),{x:Q,y:v}},w.prototype.$1ew=function(w){var i=this,_=i.$16x;return{x:w.x*_.gridX+_.x,y:w.y*_.gridY+_.y}},w.prototype.$1fw=function(c){var a=this,A=a.$16x,z=A.xLen,U=A.yLen;if(c.x>=0&&c.x<z&&c.y>=0&&c.y<U)return c;var v={x:c.x,y:c.y};return v.x<0?v.x=0:v.x>=z&&(v.x=z-1),v.y<0?v.y=0:v.y>=U&&(v.y=U-1),v},w.prototype.findPath=function(p,I){var q,n,i,C,R=this,F=R.$1dw(p),S=R.$1dw(I);if(i=R.$1fw(F),C=R.$1fw(S),i!==F&&C!==S){var K=R.$16x,b=K.x-K.gridX/2,x=K.y-K.gridY/2,d=b+K.gridX*K.xLen,X=x+K.gridY*K.yLen;if(!(R.$18x(p.x,p.y,I.x,I.y,b,x,d,x)||R.$18x(p.x,p.y,I.x,I.y,d,x,d,X)||R.$18x(p.x,p.y,I.x,I.y,d,X,b,X)||R.$18x(p.x,p.y,I.x,I.y,b,X,b,x)))return[p,I]}i!==F&&(q=!0,F=i),C!==S&&(n=!0,S=C);var D=R.graph.grid[F.x][F.y],c=R.graph.grid[S.x][S.y],k=AStar.search(R.graph,D,c,{closest:R.$14x.closest,punish:R.$14x.turnPunish});if(!k||!k.length)return null;var j=[];j.push(p),q&&j.push(R.$1ew(k[0]));for(var O=1,J=k.length;J-1>O;O++)j.push(R.$1ew(k[O]));if(n)j.push(R.$1ew(k[J-1])),j.push(I);else{var H=k[J-1];S.x!==H.x||S.y!==H.y||R.$14x.toGridCenter?j.push(R.$1ew(H)):j.push(I)}return R.$14x.simplify&&(j=R.simplifyPath(j)),R.$20x=j,R.$21x(),j},w.prototype.debugOn=function(G){this.debugFlag=!0,this.debugSettings=G||{},this.$21x()},w.prototype.simplifyPath=function(B){var l=B.length;if(2>=l)return B;var R,D,t=[B[0],B[1]],i=t[0],I=t[1],h=1e-5;for(R=2;l>R;R++)D=B[R],Math.abs((I.x-i.x)*(D.y-i.y)-(I.y-i.y)*(D.x-i.x))<h?I=t[t.length-1]=D:(i=I,I=D,t.push(D));return t},w.prototype.$21x=function(){var W=this;if(W.$22x(),this.debugFlag){var c=W.grid;if(c){var _,G=W.gv;if(W.debugSettings.$16x!==!1){_=[];for(var t,I,u,f,P,M=[],b=W.$14x.gridSizeX/2,w=W.$14x.gridSizeY/2,o=c.length,l=o?c[0].length:0,Q=1;o-1>Q;Q++)for(var p=1;l-1>p;p++)c[Q][p]||(t=W.$1ew({x:Q,y:p}),I=t.x-b,u=t.x+b,f=t.y-w,P=t.y+w,_.push({x:I,y:f},{x:I,y:P},{x:u,y:P},{x:u,y:f}),M.push(1,2,2,2,5));var i=new g.Shape;i.s("shape.border.color","rgba(50, 50, 50, 0.2)"),i.s("shape.border.width",1),i.s("shape.background","rgba(200, 50, 100, 0.2)"),i.s("2d.editable",!1),i.s("2d.selectable",!1),i.setPoints(_),i.setSegments(M),W._debugInfo={grid:i},G.dm().add(i)}W.debugSettings.path!==!1&&(_=new g.Shape,W.$20x&&(_.setPoints(W.$20x),_.s("shape.border.color","red"),_.s("shape.border.width",1),_.s("2d.editable",!1),_.s("2d.selectable",!1),_.s("shape.background",null),W._debugInfo||(W._debugInfo={}),W._debugInfo.path=_,G.dm().add(_)))}}},w.prototype.debugOff=function(){this.debugFlag=!1,this.$22x()},w.prototype.$22x=function(){var g=this;if(g._debugInfo){var y=g._debugInfo.grid,i=g._debugInfo.path,h=g.gv;y&&h.dm().remove(y),i&&h.dm().remove(i),g._debugInfo=null}}}("undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:(0,eval)("this"),Object);