Files
bdu-source/src/Extensions/PathfindingBehavior/pathfindingruntimebehavior.js
2026-02-12 19:51:45 -07:00

3 lines
13 KiB
JavaScript

var gdjs;(function(l){const p=new l.Logger("Pathfinding behavior");class f extends l.RuntimeBehavior{constructor(e,s,n){super(e,s,n);this._path=[];this._pathFound=!1;this._speed=0;this._angularSpeed=0;this._distanceOnSegment=0;this._totalSegmentDistance=0;this._currentSegment=0;this._reachedEnd=!1;this._movementAngle=0;this._path===void 0||(this._path.length=0),this._allowDiagonals=s.allowDiagonals,this._acceleration=s.acceleration,this._maxSpeed=s.maxSpeed,this._angularMaxSpeed=s.angularMaxSpeed,this._rotateObject=s.rotateObject,this._angleOffset=s.angleOffset,this._cellWidth=s.cellWidth,this._cellHeight=s.cellHeight,this._gridOffsetX=s.gridOffsetX||0,this._gridOffsetY=s.gridOffsetY||0,this._extraBorder=s.extraBorder,this._smoothingMaxCellGap=s.smoothingMaxCellGap||0,this._manager=l.PathfindingObstaclesManager.getManager(e),this._searchContext=new l.PathfindingRuntimeBehavior.SearchContext(this._manager)}applyBehaviorOverriding(e){return e.allowDiagonals!==void 0&&this.allowDiagonals(e.allowDiagonals),e.acceleration!==void 0&&this.setAcceleration(e.acceleration),e.maxSpeed!==void 0&&this.setMaxSpeed(e.maxSpeed),e.angularMaxSpeed!==void 0&&this.setAngularMaxSpeed(e.angularMaxSpeed),e.rotateObject!==void 0&&this.setRotateObject(e.rotateObject),e.angleOffset!==void 0&&this.setAngleOffset(e.angleOffset),e.cellWidth!==void 0&&this.setCellWidth(e.cellWidth),e.cellHeight!==void 0&&this.setCellHeight(e.cellHeight),e.gridOffsetX!==void 0&&(this._gridOffsetX=e.gridOffsetX),e.gridOffsetY!==void 0&&(this._gridOffsetY=e.gridOffsetY),e.extraBorder!==void 0&&this.setExtraBorder(e.extraBorder),e.smoothingMaxCellGap!==void 0&&(this._smoothingMaxCellGap=e.smoothingMaxCellGap),!0}getNetworkSyncData(e){return{...super.getNetworkSyncData(e),props:{path:this._path,pf:this._pathFound,sp:this._speed,as:this._angularSpeed,cs:this._currentSegment,tss:this._totalSegmentDistance,re:this._reachedEnd,ma:this._movementAngle,dos:this._distanceOnSegment}}}updateFromNetworkSyncData(e,s){super.updateFromNetworkSyncData(e,s);const n=e.props;n.path!==void 0&&(this._path=n.path),n.pf!==void 0&&(this._pathFound=n.pf),n.sp!==void 0&&(this._speed=n.sp),n.as!==void 0&&(this._angularSpeed=n.as),n.cs!==void 0&&n.cs!==this._currentSegment&&(this._currentSegment=n.cs),n.tss!==void 0&&(this._totalSegmentDistance=n.tss),n.re!==void 0&&(this._reachedEnd=n.re),n.ma!==void 0&&(this._movementAngle=n.ma),n.dos!==void 0&&(this._distanceOnSegment=n.dos)}setCellWidth(e){this._cellWidth=e}getCellWidth(){return this._cellWidth}setCellHeight(e){this._cellHeight=e}getCellHeight(){return this._cellHeight}setGridOffsetX(e){this._gridOffsetX=e}getGridOffsetX(){return this._gridOffsetX}setGridOffsetY(e){this._gridOffsetY=e}getGridOffsetY(){return this._gridOffsetY}setAcceleration(e){this._acceleration=e}getAcceleration(){return this._acceleration}setMaxSpeed(e){this._maxSpeed=e}getMaxSpeed(){return this._maxSpeed}setSpeed(e){this._speed=e}getSpeed(){return this._speed}getMovementAngle(){return this._movementAngle}movementAngleIsAround(e,s){return Math.abs(l.evtTools.common.angleDifference(this._movementAngle,e))<=s}setAngularMaxSpeed(e){this._angularMaxSpeed=e}getAngularMaxSpeed(){return this._angularMaxSpeed}setAngleOffset(e){this._angleOffset=e}getAngleOffset(){return this._angleOffset}setExtraBorder(e){this._extraBorder=e}getExtraBorder(){return this._extraBorder}allowDiagonals(e){this._allowDiagonals=e}diagonalsAllowed(){return this._allowDiagonals}setRotateObject(e){this._rotateObject=e}isObjectRotated(){return this._rotateObject}getNodeX(e){return e<this._path.length?this._path[e][0]:0}getNodeY(e){return e<this._path.length?this._path[e][1]:0}getNextNodeIndex(){return this._currentSegment+1<this._path.length?this._currentSegment+1:this._path.length-1}getNodeCount(){return this._path.length}getNextNodeX(){return this._path.length===0?0:this._currentSegment+1<this._path.length?this._path[this._currentSegment+1][0]:this._path[this._path.length-1][0]}getNextNodeY(){return this._path.length===0?0:this._currentSegment+1<this._path.length?this._path[this._currentSegment+1][1]:this._path[this._path.length-1][1]}getLastNodeX(){return this._path.length<2?0:this._currentSegment<this._path.length-1?this._path[this._currentSegment][0]:this._path[this._path.length-1][0]}getLastNodeY(){return this._path.length<2?0:this._currentSegment<this._path.length-1?this._path[this._currentSegment][1]:this._path[this._path.length-1][1]}getDestinationX(){return this._path.length===0?0:this._path[this._path.length-1][0]}getDestinationY(){return this._path.length===0?0:this._path[this._path.length-1][1]}pathFound(){return this._pathFound}destinationReached(){return this._reachedEnd}moveTo(e,s,n){const t=this.owner,i=Math.round((s-this._gridOffsetX)/this._cellWidth),h=Math.round((n-this._gridOffsetY)/this._cellHeight),r=Math.round((t.getX()-this._gridOffsetX)/this._cellWidth),a=Math.round((t.getY()-this._gridOffsetY)/this._cellHeight);if(r==i&&a==h){this._path.length=0,this._path.push([t.getX(),t.getY()]),this._path.push([s,n]),this._enterSegment(0),this._pathFound=!0;return}if(this._searchContext.allowDiagonals(this._allowDiagonals),this._searchContext.setObstacles(this._manager),this._searchContext.setCellSize(this._cellWidth,this._cellHeight),this._searchContext.setGridOffset(this._gridOffsetX,this._gridOffsetY),this._searchContext.setStartPosition(t.getX(),t.getY()),this._searchContext.setObjectSize(t.getX()-t.getDrawableX()+this._extraBorder,t.getY()-t.getDrawableY()+this._extraBorder,t.getWidth()-(t.getX()-t.getDrawableX())+this._extraBorder,t.getHeight()-(t.getY()-t.getDrawableY())+this._extraBorder),this._searchContext.computePathTo(s,n)){let o=this._searchContext.getFinalNode(),_=0;for(;o;)_===this._path.length&&this._path.push([0,0]),this._path[_][0]=o.pos[0]*this._cellWidth+this._gridOffsetX,this._path[_][1]=o.pos[1]*this._cellHeight+this._gridOffsetY,o=o.parent,_++;if(this._path.length=_,this._path.reverse(),this._path[0][0]=t.getX(),this._path[0][1]=t.getY(),this._allowDiagonals&&this._smoothingMaxCellGap>0){l.pathfinding.simplifyPath(this._path,this._smoothingMaxCellGap*Math.min(this._cellWidth,this._cellHeight),l.PathfindingRuntimeBehavior._smoothingResultVertices,l.PathfindingRuntimeBehavior._smoothingWorkingVertices);let d=this._path;this._path=l.PathfindingRuntimeBehavior._smoothingResultVertices,l.PathfindingRuntimeBehavior._smoothingResultVertices=d}this._enterSegment(0),this._pathFound=!0;return}this._pathFound=!1}_enterSegment(e){if(this._path.length!==0)if(this._currentSegment=e,this._currentSegment<this._path.length-1){const s=this._path[this._currentSegment+1][0]-this._path[this._currentSegment][0],n=this._path[this._currentSegment+1][1]-this._path[this._currentSegment][1];this._totalSegmentDistance=Math.sqrt(s*s+n*n),this._distanceOnSegment=0,this._reachedEnd=!1,this._movementAngle=(l.toDegrees(Math.atan2(n,s))+360)%360}else this._reachedEnd=!0,this._speed=0}doStepPreEvents(e){if(this._path.length===0||this._reachedEnd)return;const s=this.owner.getElapsedTime()/1e3,n=this._speed;this._speed!==this._maxSpeed&&(this._speed+=this._acceleration*s,this._speed>this._maxSpeed&&(this._speed=this._maxSpeed)),this._angularSpeed=this._angularMaxSpeed,this._distanceOnSegment+=(this._speed+n)/2*s;const t=this._totalSegmentDistance-this._distanceOnSegment;t<=0&&this._currentSegment<this._path.length&&(this._enterSegment(this._currentSegment+1),this._distanceOnSegment=-t);let i=[0,0];this._currentSegment<this._path.length-1?(i[0]=l.evtTools.common.lerp(this._path[this._currentSegment][0],this._path[this._currentSegment+1][0],this._distanceOnSegment/this._totalSegmentDistance),i[1]=l.evtTools.common.lerp(this._path[this._currentSegment][1],this._path[this._currentSegment+1][1],this._distanceOnSegment/this._totalSegmentDistance),this._rotateObject&&this.owner.getAngle()!==this._movementAngle+this._angleOffset&&this.owner.rotateTowardAngle(this._movementAngle+this._angleOffset,this._angularSpeed)):i=this._path[this._path.length-1],this.owner.setX(i[0]),this.owner.setY(i[1])}doStepPostEvents(e){}static euclideanDistance(e,s){return Math.sqrt((e[0]-s[0])*(e[0]-s[0])+(e[1]-s[1])*(e[1]-s[1]))}static manhattanDistance(e,s){return Math.abs(e[0]-s[0])+Math.abs(e[1]-s[1])}}f._smoothingResultVertices=[],f._smoothingWorkingVertices=[],l.PathfindingRuntimeBehavior=f,l.registerBehavior("PathfindingBehavior::PathfindingBehavior",l.PathfindingRuntimeBehavior),function(s){class c{constructor(t,i){this.cost=0;this.smallestCost=-1;this.estimateCost=-1;this.parent=null;this.open=!0;this.pos=[t,i]}reinitialize(t,i){this.pos[0]=t,this.pos[1]=i,this.cost=0,this.smallestCost=-1,this.estimateCost=-1,this.parent=null,this.open=!0}}s.Node=c;class e{constructor(t){this._finalNode=null;this._destination=[0,0];this._start=[0,0];this._startX=0;this._startY=0;this._allowDiagonals=!0;this._maxComplexityFactor=50;this._cellWidth=20;this._cellHeight=20;this._gridOffsetX=0;this._gridOffsetY=0;this._leftBorder=0;this._rightBorder=0;this._topBorder=0;this._bottomBorder=0;this._allNodes=[];this._openNodes=[];this._closeObstacles=[];this._nodeCache=[];this._obstacles=t,this._distanceFunction=s.euclideanDistance}setObstacles(t){return this._obstacles=t,this}getFinalNode(){return this._finalNode}allowDiagonals(t){return this._allowDiagonals=t,this._distanceFunction=t?s.euclideanDistance:s.manhattanDistance,this}setStartPosition(t,i){return this._startX=t,this._startY=i,this}setObjectSize(t,i,h,r){return this._leftBorder=t,this._rightBorder=h,this._topBorder=i,this._bottomBorder=r,this}setCellSize(t,i){return this._cellWidth=t,this._cellHeight=i,this}setGridOffset(t,i){return this._gridOffsetX=t,this._gridOffsetY=i,this}computePathTo(t,i){if(this._obstacles===null){p.log("You tried to compute a path without specifying the obstacles");return}this._destination[0]=Math.round((t-this._gridOffsetX)/this._cellWidth),this._destination[1]=Math.round((i-this._gridOffsetY)/this._cellHeight),this._start[0]=Math.round((this._startX-this._gridOffsetX)/this._cellWidth),this._start[1]=Math.round((this._startY-this._gridOffsetY)/this._cellHeight),this._freeAllNodes();const h=this._getNode(this._start[0],this._start[1]);h.smallestCost=0,h.estimateCost=0+this._distanceFunction(this._start,this._destination),this._openNodes.length=0,this._openNodes.push(h);let r=0;const a=h.estimateCost*this._maxComplexityFactor;for(;this._openNodes.length!==0;){if(r++>a)return console.warn(`No path was found after covering ${a} cells.`),!1;const o=this._openNodes.shift();if(o.open=!1,o.pos[0]==this._destination[0]&&o.pos[1]==this._destination[1])return this._finalNode=o,!0;this._insertNeighbors(o)}return!1}_freeAllNodes(){if(this._nodeCache.length<=32e3){for(const t in this._allNodes)if(this._allNodes.hasOwnProperty(t)){const i=this._allNodes[t];for(const h in i)i.hasOwnProperty(h)&&this._nodeCache.push(i[h])}}this._allNodes=[]}_insertNeighbors(t){this._addOrUpdateNode(t.pos[0]+1,t.pos[1],t,1),this._addOrUpdateNode(t.pos[0]-1,t.pos[1],t,1),this._addOrUpdateNode(t.pos[0],t.pos[1]+1,t,1),this._addOrUpdateNode(t.pos[0],t.pos[1]-1,t,1),this._allowDiagonals&&(this._addOrUpdateNode(t.pos[0]+1,t.pos[1]+1,t,1.414213562),this._addOrUpdateNode(t.pos[0]+1,t.pos[1]-1,t,1.414213562),this._addOrUpdateNode(t.pos[0]-1,t.pos[1]-1,t,1.414213562),this._addOrUpdateNode(t.pos[0]-1,t.pos[1]+1,t,1.414213562))}_getNode(t,i){if(this._allNodes.hasOwnProperty(t)){if(this._allNodes[t].hasOwnProperty(i))return this._allNodes[t][i]}else this._allNodes[t]=[];let h;this._nodeCache.length!==0?(h=this._nodeCache.shift(),h.reinitialize(t,i)):h=new c(t,i);const r=t*this._cellWidth+this._gridOffsetX,a=i*this._cellHeight+this._gridOffsetY;let o=!1;const _=this._cellHeight>this._cellWidth?this._cellHeight*2:this._cellWidth*2;this._obstacles.getAllObstaclesAround(r,a,_,this._closeObstacles);for(let d=0;d<this._closeObstacles.length;++d){const g=this._closeObstacles[d].owner,u=Math.floor((g.getDrawableX()-this._rightBorder-this._gridOffsetX)/this._cellWidth),m=Math.floor((g.getDrawableY()-this._bottomBorder-this._gridOffsetY)/this._cellHeight),O=Math.ceil((g.getDrawableX()+g.getWidth()+this._leftBorder-this._gridOffsetX)/this._cellWidth),S=Math.ceil((g.getDrawableY()+g.getHeight()+this._topBorder-this._gridOffsetY)/this._cellHeight);if(u<t&&t<O&&m<i&&i<S)if(o=!0,this._closeObstacles[d].isImpassable()){h.cost=-1;break}else h.cost+=this._closeObstacles[d].getCost()}return o||(h.cost=1),this._allNodes[t][i]=h,h}_addOrUpdateNode(t,i,h,r){const a=this._getNode(t,i);if(!(!a.open||a.cost<0)&&(a.smallestCost===-1||a.smallestCost>h.smallestCost+(h.cost+a.cost)/2*r)){if(a.smallestCost!=-1){for(let o=0;o<this._openNodes.length;++o)if(this._openNodes[o].pos[0]==a.pos[0]&&this._openNodes[o].pos[1]==a.pos[1]){this._openNodes.splice(o,1);break}}if(a.smallestCost=h.smallestCost+(h.cost+a.cost)/2*r,a.parent=h,a.estimateCost=a.smallestCost+this._distanceFunction(a.pos,this._destination),this._openNodes.length===0||this._openNodes[this._openNodes.length-1].estimateCost<a.estimateCost)this._openNodes.push(a);else for(let o=0;o<this._openNodes.length;++o)if(this._openNodes[o].estimateCost>=a.estimateCost){this._openNodes.splice(o,0,a);break}}}}s.SearchContext=e}(f=l.PathfindingRuntimeBehavior||(l.PathfindingRuntimeBehavior={}))})(gdjs||(gdjs={}));
//# sourceMappingURL=pathfindingruntimebehavior.js.map