@@ -5,15 +5,13 @@ import { Chunk } from '@server/world/map/chunk';
5
5
import { Tile } from '@runejs/cache-parser' ;
6
6
import { Player } from '@server/world/actor/player/player' ;
7
7
import { logger } from '@runejs/logger' ;
8
- import { itemIds } from '@server/world/config/item-ids' ;
9
8
10
9
class Point {
11
10
12
11
private _parent : Point = null ;
13
12
private _cost : number = 0 ;
14
13
15
- public constructor ( private readonly _x : number , private readonly _y : number ,
16
- public readonly indexX : number , public readonly indexY : number ) {
14
+ public constructor ( private readonly _x : number , private readonly _y : number ) {
17
15
}
18
16
19
17
public equals ( point : Point ) : boolean {
@@ -64,8 +62,8 @@ export class Pathfinding {
64
62
65
63
private currentPoint : Point ;
66
64
private points : Point [ ] [ ] ;
67
- private closedPoints : Point [ ] = [ ] ;
68
- private openPoints : Point [ ] = [ ] ;
65
+ private closedPoints = new Set < Point > ( ) ;
66
+ private openPoints = new Set < Point > ( ) ;
69
67
public stopped = false ;
70
68
71
69
public constructor ( private actor : Actor ) {
@@ -110,6 +108,11 @@ export class Pathfinding {
110
108
111
109
try {
112
110
const path = this . pathTo ( position . x , position . y , options . pathingSearchRadius ) ;
111
+
112
+ if ( ! path ) {
113
+ return ;
114
+ }
115
+
113
116
const walkingQueue = this . actor . walkingQueue ;
114
117
115
118
if ( this . actor instanceof Player ) {
@@ -120,7 +123,7 @@ export class Pathfinding {
120
123
walkingQueue . valid = true ;
121
124
122
125
if ( options . ignoreDestination ) {
123
- // path.splice(path.length - 1, 1);
126
+ path . splice ( path . length - 1 , 1 ) ;
124
127
}
125
128
126
129
for ( const point of path ) {
@@ -162,50 +165,36 @@ export class Pathfinding {
162
165
const lowestY = position . y - searchRadius ;
163
166
const highestX = position . x + searchRadius ;
164
167
const highestY = position . y + searchRadius ;
165
- console . log ( highestX - lowestX , searchRadius * 2 ) ;
166
168
167
169
if ( destinationX < lowestX || destinationX > highestX || destinationY < lowestY || destinationY > highestY ) {
168
170
throw new Error ( `Out of range.` ) ;
169
171
}
170
172
171
- let destinationIndexX ;
172
- let destinationIndexY ;
173
- let startingIndexX ;
174
- let startingIndexY ;
173
+ let destinationIndexX = destinationX - position . x + searchRadius ;
174
+ let destinationIndexY = destinationY - position . y + searchRadius ;
175
+ let startingIndexX = searchRadius ;
176
+ let startingIndexY = searchRadius ;
175
177
176
178
const pointLen = searchRadius * 2 ;
177
179
178
180
if ( pointLen <= 0 ) {
179
181
throw new Error ( `Why is your search radius zero?` ) ;
180
182
}
181
183
182
- this . points = new Array ( pointLen ) . fill ( new Array ( pointLen ) ) ;
184
+ this . points = [ ... Array ( pointLen ) ] . map ( e => Array ( pointLen ) ) ;
183
185
184
186
for ( let x = 0 ; x < pointLen ; x ++ ) {
185
187
for ( let y = 0 ; y < pointLen ; y ++ ) {
186
- const worldX = lowestX + x ;
187
- const worldY = lowestY + y ;
188
- world . spawnWorldItem ( { itemId : itemIds . ashes , amount : 1 } ,
189
- new Position ( worldX , worldY , this . actor . position . level ) , this . actor as Player , 50 ) ;
190
-
191
- if ( worldX === position . x && worldY === position . y ) {
192
- startingIndexX = x ;
193
- startingIndexY = y ;
194
- }
195
-
196
- if ( worldX === destinationX && worldY === destinationY ) {
197
- destinationIndexX = x ;
198
- destinationIndexY = y ;
199
- }
200
-
201
- this . points [ x ] [ y ] = new Point ( worldX , worldY , x , y ) ;
188
+ this . points [ x ] [ y ] = new Point ( lowestX + x , lowestY + y ) ;
202
189
}
203
190
}
204
191
205
192
// Starting point
206
- this . openPoints . push ( this . points [ startingIndexX ] [ startingIndexY ] ) ;
193
+ this . openPoints = new Set < Point > ( ) ;
194
+ this . closedPoints = new Set < Point > ( ) ;
195
+ this . openPoints . add ( this . points [ startingIndexX ] [ startingIndexY ] ) ;
207
196
208
- while ( this . openPoints . length > 0 ) {
197
+ while ( this . openPoints . size > 0 ) {
209
198
if ( this . stopped ) {
210
199
return null ;
211
200
}
@@ -216,11 +205,13 @@ export class Pathfinding {
216
205
break ;
217
206
}
218
207
219
- this . openPoints . splice ( this . openPoints . findIndex ( p => p . equals ( this . currentPoint ) ) , 1 ) ;
220
- this . closedPoints . push ( this . currentPoint ) ;
208
+ this . openPoints . delete ( this . currentPoint ) ;
209
+ this . closedPoints . add ( this . currentPoint ) ;
221
210
222
211
const level = this . actor . position . level ;
223
- const { x, y, indexX, indexY } = this . currentPoint ;
212
+ const { x, y } = this . currentPoint ;
213
+ const indexX = x - lowestX ;
214
+ const indexY = y - lowestY ;
224
215
225
216
// North-West
226
217
if ( indexX > 0 && this . points [ indexX - 1 ] && indexY < this . points [ indexX - 1 ] . length - 1 ) {
@@ -277,33 +268,24 @@ export class Pathfinding {
277
268
}
278
269
}
279
270
280
- for ( let x = 0 ; x < pointLen ; x ++ ) {
281
- for ( let y = 0 ; y < pointLen ; y ++ ) {
282
- const point = this . points [ x ] [ y ] ;
283
- if ( point . parent ) {
284
- world . spawnWorldItem ( { itemId : itemIds . cabbage , amount : 1 } ,
285
- new Position ( point . x , point . y , this . actor . position . level ) , this . actor as Player , 50 ) ;
286
- }
287
- }
288
- }
289
-
290
271
const destinationPoint = this . points [ destinationIndexX ] [ destinationIndexY ] ;
291
272
292
273
if ( ! destinationPoint || ! destinationPoint . parent ) {
293
- throw new Error ( `Unable to find destination point.` ) ;
274
+ // throw new Error(`Unable to find destination point.`);
275
+ return null ;
294
276
}
295
277
296
278
// build path
297
279
const path : Point [ ] = [ ] ;
298
280
let point = destinationPoint ;
299
281
let iterations = 0 ;
300
282
301
- while ( ! point . equals ( this . points [ startingIndexX ] [ startingIndexY ] ) ) {
283
+ do {
302
284
if ( this . stopped ) {
303
285
return null ;
304
286
}
305
287
306
- path . push ( new Point ( point . x , point . y , point . indexX , point . indexY ) ) ;
288
+ path . push ( new Point ( point . x , point . y ) ) ;
307
289
point = point . parent ;
308
290
iterations ++ ;
309
291
@@ -314,10 +296,7 @@ export class Pathfinding {
314
296
if ( point === null ) {
315
297
break ;
316
298
}
317
- }
318
-
319
- const start = this . points [ startingIndexX ] [ startingIndexY ] ;
320
- path . push ( new Point ( start . x , start . y , start . indexX , start . indexY ) ) ;
299
+ } while ( ! point . equals ( this . points [ startingIndexX ] [ startingIndexY ] ) ) ;
321
300
322
301
return path . reverse ( ) ;
323
302
}
@@ -327,35 +306,36 @@ export class Pathfinding {
327
306
return ;
328
307
}
329
308
330
- const differenceX = this . currentPoint . x - point . x ;
331
- const differenceY = this . currentPoint . y - point . y ;
332
- const nextStepCost = this . currentPoint . cost + ( ( Math . abs ( differenceX ) + Math . abs ( differenceY ) ) * 10 ) ;
309
+ const nextStepCost = this . currentPoint . cost + this . calculateCostBetween ( this . currentPoint , point ) ;
333
310
334
311
if ( nextStepCost < point . cost ) {
335
- this . openPoints . splice ( this . openPoints . findIndex ( p => p . equals ( point ) ) ) ;
336
- this . closedPoints . splice ( this . closedPoints . findIndex ( p => p . equals ( point ) ) ) ;
312
+ this . openPoints . delete ( point ) ;
313
+ this . closedPoints . delete ( point ) ;
337
314
}
338
315
339
- if ( this . openPoints . findIndex ( p => p . equals ( point ) ) === - 1 && this . closedPoints . findIndex ( p => p . equals ( point ) ) === - 1 ) {
316
+ if ( ! this . openPoints . has ( point ) && ! this . closedPoints . has ( point ) ) {
340
317
point . parent = this . currentPoint ;
341
318
point . cost = nextStepCost ;
342
- this . openPoints . push ( point ) ;
319
+ this . openPoints . add ( point ) ;
343
320
}
344
321
}
345
322
323
+ private calculateCostBetween ( current : Point , destination : Point ) : number {
324
+ const deltaX = current . x - destination . x ;
325
+ const deltaY = current . y - destination . y ;
326
+ return ( Math . abs ( deltaX ) + Math . abs ( deltaY ) ) * 10 ;
327
+ }
328
+
346
329
private calculateBestPoint ( ) : Point {
347
330
let bestPoint : Point = null ;
348
331
349
- for ( const point of this . openPoints ) {
350
- if ( bestPoint === null ) {
332
+ this . openPoints . forEach ( point => {
333
+ if ( ! bestPoint ) {
351
334
bestPoint = point ;
352
- continue ;
353
- }
354
-
355
- if ( point . cost < bestPoint . cost ) {
335
+ } else if ( point . cost < bestPoint . cost ) {
356
336
bestPoint = point ;
357
337
}
358
- }
338
+ } ) ;
359
339
360
340
return bestPoint ;
361
341
}
0 commit comments