11// src/utils/grid.utils.ts
22var makeSquare = ( layout ) => {
33 const maxLength = Math . max ( layout . length , ...layout . map ( ( row ) => row . length ) ) ;
4- const squareLayout = Array . from ( { length : maxLength } , ( ) =>
5- Array ( maxLength ) . fill ( null ) ,
4+ const squareLayout = Array . from (
5+ { length : maxLength } ,
6+ ( ) => Array ( maxLength ) . fill ( null )
67 ) ;
78 for ( let i = 0 ; i < layout . length ; i ++ ) {
89 for ( let j = 0 ; j < layout [ i ] . length ; j ++ ) {
@@ -13,8 +14,9 @@ var makeSquare = (layout) => {
1314} ;
1415var transpose = ( matrix ) => {
1516 const maxCols = Math . max ( ...matrix . map ( ( row ) => row . length ) ) ;
16- const transposed = Array . from ( { length : maxCols } , ( ) =>
17- Array ( matrix . length ) . fill ( null ) ,
17+ const transposed = Array . from (
18+ { length : maxCols } ,
19+ ( ) => Array ( matrix . length ) . fill ( null )
1820 ) ;
1921 for ( let i = 0 ; i < matrix . length ; i ++ ) {
2022 for ( let j = 0 ; j < matrix [ i ] . length ; j ++ ) {
@@ -92,7 +94,7 @@ var findPath = (startPoint, endPoint, grid, config) => {
9294 const orthogonalCostMultiplier = config . orthogonalCostMultiplier ?? 1 ;
9395 const maxJumpCost = config . maxJumpCost ?? 5 ;
9496 const maxIterations = config . maxIterations ?? 99999 ;
95- const jumpDiagonals = config . jumpDiagonals ?? false ;
97+ const jumpBlockedDiagonals = config . jumpBlockedDiagonals ?? false ;
9698 const index = ( point ) => {
9799 return point . y * grid . height + point . x ;
98100 } ;
@@ -110,7 +112,7 @@ var findPath = (startPoint, endPoint, grid, config) => {
110112 while ( true ) {
111113 const target = {
112114 x : src . x + dirX * jumpDistance ,
113- y : src . y + dirY * jumpDistance ,
115+ y : src . y + dirY * jumpDistance
114116 } ;
115117 if ( ! grid . isWalkable ( target ) ) {
116118 break ;
@@ -125,7 +127,7 @@ var findPath = (startPoint, endPoint, grid, config) => {
125127 if ( totalCost < visited [ targetIndex ] ) {
126128 visited [ targetIndex ] = totalCost ;
127129 queue . push (
128- new PathNode ( target , prevNode , totalCost , heuristic ( target ) ) ,
130+ new PathNode ( target , prevNode , totalCost , heuristic ( target ) )
129131 ) ;
130132 }
131133 prevPoint = target ;
@@ -137,24 +139,13 @@ var findPath = (startPoint, endPoint, grid, config) => {
137139 } ;
138140 const addDiagonal = ( prevNode , src , srcCost , dirX , dirY ) => {
139141 const target = { x : src . x + dirX , y : src . y + dirY } ;
140- const moveCost =
141- srcCost +
142- ( getMoveCostAt ( src , target ) ?? NOT_REACHED_COST ) * diagonalCostMultiplier ;
142+ const moveCost = srcCost + ( getMoveCostAt ( src , target ) ?? NOT_REACHED_COST ) * diagonalCostMultiplier ;
143143 const targetHeight = grid . getHeightAt ( target ) ;
144144 const aux1 = { x : src . x , y : src . y + dirY } ;
145145 const aux2 = { x : src . x + dirX , y : src . y } ;
146146 const targetIndex = index ( target ) ;
147- const canJumpDiagonals =
148- jumpDiagonals ||
149- ( grid . isWalkable ( aux1 ) &&
150- grid . isWalkable ( aux2 ) &&
151- targetHeight == grid . getHeightAt ( aux1 ) &&
152- targetHeight == grid . getHeightAt ( aux2 ) ) ;
153- if (
154- grid . isWalkable ( target ) &&
155- canJumpDiagonals &&
156- moveCost < visited [ targetIndex ]
157- ) {
147+ const canJumpDiagonals = jumpBlockedDiagonals || grid . isWalkable ( aux1 ) && grid . isWalkable ( aux2 ) && targetHeight == grid . getHeightAt ( aux1 ) && targetHeight == grid . getHeightAt ( aux2 ) ;
148+ if ( grid . isWalkable ( target ) && canJumpDiagonals && moveCost < visited [ targetIndex ] ) {
158149 visited [ targetIndex ] = moveCost ;
159150 queue . push ( new PathNode ( target , prevNode , moveCost , heuristic ( target ) ) ) ;
160151 }
@@ -253,24 +244,15 @@ var Grid = class _Grid {
253244 return new _Grid (
254245 this . width ,
255246 this . height ,
256- new Float32Array ( this . heightMatrix ) ,
247+ new Float32Array ( this . heightMatrix )
257248 ) ;
258249 }
259250 inBounds ( point ) {
260- return (
261- point . x >= 0 &&
262- point . x < this . width &&
263- point . y >= 0 &&
264- point . y < this . height
265- ) ;
251+ return point . x >= 0 && point . x < this . width && point . y >= 0 && point . y < this . height ;
266252 }
267253 isWalkable ( point ) {
268254 const heightAt = this . getHeightAt ( point ) ;
269- return (
270- this . inBounds ( point ) &&
271- heightAt !== null &&
272- heightAt !== NON_WALKABLE_HEIGHT
273- ) ;
255+ return this . inBounds ( point ) && heightAt !== null && heightAt !== NON_WALKABLE_HEIGHT ;
274256 }
275257 walkMatrix ( callback ) {
276258 for ( let y = 0 ; y < this . height ; y ++ ) {
@@ -291,15 +273,15 @@ var Grid = class _Grid {
291273 }
292274 return matrix ;
293275 }
294- findPath (
295- startPoint ,
296- endPoint ,
297- config = {
298- maxJumpCost : 5 ,
299- travelCosts : void 0 ,
300- } ,
301- ) {
276+ findPath ( startPoint , endPoint , config = {
277+ maxJumpCost : 5 ,
278+ travelCosts : void 0
279+ } ) {
302280 return findPath ( startPoint , endPoint , this , config ) ;
303281 }
304282} ;
305- export { Grid , makeSquare , transpose } ;
283+ export {
284+ Grid ,
285+ makeSquare ,
286+ transpose
287+ } ;
0 commit comments