Skip to content

Commit 95a3db4

Browse files
Fixing following finally
1 parent 3706893 commit 95a3db4

File tree

3 files changed

+57
-92
lines changed

3 files changed

+57
-92
lines changed

src/plugins/player/follow-player-plugin.ts

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ActionType, RunePlugin } from '@server/plugins/plugin';
22
import { playerAction } from '@server/world/actor/player/action/player-action';
33
import { Player } from '@server/world/actor/player/player';
4-
import { Pathfinding } from '@server/world/actor/pathfinding';
4+
55

66
async function pathTo(player: Player, otherPlayer: Player): Promise<boolean> {
77
const distance = Math.floor(otherPlayer.position.distanceBetween(player.position));
@@ -11,32 +11,19 @@ async function pathTo(player: Player, otherPlayer: Player): Promise<boolean> {
1111
throw new Error(`Distance too great!`);
1212
}
1313

14-
if(distance <= 1) {
15-
return Promise.resolve(false);
14+
let ignoreDestination = true;
15+
let desiredPosition = otherPlayer.position;
16+
if(otherPlayer.lastMovementPosition/* && otherPlayer.lastMovementPosition.distanceBetween(otherPlayer.position) < 1*/) {
17+
desiredPosition = otherPlayer.lastMovementPosition;
18+
ignoreDestination = false;
1619
}
1720

18-
//try {
19-
let ignoreDestination = true;
20-
let desiredPosition = otherPlayer.position;
21-
/*if(otherPlayer.lastMovementPosition && otherPlayer.lastMovementPosition.distanceBetween(otherPlayer.position) < 1) {
22-
desiredPosition = otherPlayer.lastMovementPosition;
23-
ignoreDestination = false;
24-
}*/
25-
26-
player.pathfinding.stopped = true;
27-
player.pathfinding = new Pathfinding(player);
28-
await player.pathfinding.walkTo(desiredPosition, {
29-
pathingSearchRadius: distance + 2,
30-
ignoreDestination
31-
});
21+
await player.pathfinding.walkTo(desiredPosition, {
22+
pathingSearchRadius: distance + 2,
23+
ignoreDestination
24+
});
3225

33-
return Promise.resolve(true);
34-
/*} catch(e) {
35-
player.clearFaceActor();
36-
player.actionsCancelled.next();
37-
logger.error('Pathing error:');
38-
logger.error(e && e.message ? e.message : 'Error while finding path.');
39-
}*/
26+
return Promise.resolve(true);
4027
}
4128

4229
export const action: playerAction = (details) => {

src/world/actor/pathfinding.ts

Lines changed: 44 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ import { Chunk } from '@server/world/map/chunk';
55
import { Tile } from '@runejs/cache-parser';
66
import { Player } from '@server/world/actor/player/player';
77
import { logger } from '@runejs/logger';
8-
import { itemIds } from '@server/world/config/item-ids';
98

109
class Point {
1110

1211
private _parent: Point = null;
1312
private _cost: number = 0;
1413

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) {
1715
}
1816

1917
public equals(point: Point): boolean {
@@ -64,8 +62,8 @@ export class Pathfinding {
6462

6563
private currentPoint: Point;
6664
private points: Point[][];
67-
private closedPoints: Point[] = [];
68-
private openPoints: Point[] = [];
65+
private closedPoints = new Set<Point>();
66+
private openPoints = new Set<Point>();
6967
public stopped = false;
7068

7169
public constructor(private actor: Actor) {
@@ -110,6 +108,11 @@ export class Pathfinding {
110108

111109
try {
112110
const path = this.pathTo(position.x, position.y, options.pathingSearchRadius);
111+
112+
if(!path) {
113+
return;
114+
}
115+
113116
const walkingQueue = this.actor.walkingQueue;
114117

115118
if(this.actor instanceof Player) {
@@ -120,7 +123,7 @@ export class Pathfinding {
120123
walkingQueue.valid = true;
121124

122125
if(options.ignoreDestination) {
123-
// path.splice(path.length - 1, 1);
126+
path.splice(path.length - 1, 1);
124127
}
125128

126129
for(const point of path) {
@@ -162,50 +165,36 @@ export class Pathfinding {
162165
const lowestY = position.y - searchRadius;
163166
const highestX = position.x + searchRadius;
164167
const highestY = position.y + searchRadius;
165-
console.log(highestX - lowestX, searchRadius * 2);
166168

167169
if(destinationX < lowestX || destinationX > highestX || destinationY < lowestY || destinationY > highestY) {
168170
throw new Error(`Out of range.`);
169171
}
170172

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;
175177

176178
const pointLen = searchRadius * 2;
177179

178180
if(pointLen <= 0) {
179181
throw new Error(`Why is your search radius zero?`);
180182
}
181183

182-
this.points = new Array(pointLen).fill(new Array(pointLen));
184+
this.points = [...Array(pointLen)].map(e => Array(pointLen));
183185

184186
for(let x = 0; x < pointLen; x++) {
185187
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);
202189
}
203190
}
204191

205192
// 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]);
207196

208-
while(this.openPoints.length > 0) {
197+
while(this.openPoints.size > 0) {
209198
if(this.stopped) {
210199
return null;
211200
}
@@ -216,11 +205,13 @@ export class Pathfinding {
216205
break;
217206
}
218207

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);
221210

222211
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;
224215

225216
// North-West
226217
if(indexX > 0 && this.points[indexX - 1] && indexY < this.points[indexX - 1].length - 1) {
@@ -277,33 +268,24 @@ export class Pathfinding {
277268
}
278269
}
279270

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-
290271
const destinationPoint = this.points[destinationIndexX][destinationIndexY];
291272

292273
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;
294276
}
295277

296278
// build path
297279
const path: Point[] = [];
298280
let point = destinationPoint;
299281
let iterations = 0;
300282

301-
while(!point.equals(this.points[startingIndexX][startingIndexY])) {
283+
do {
302284
if(this.stopped) {
303285
return null;
304286
}
305287

306-
path.push(new Point(point.x, point.y, point.indexX, point.indexY));
288+
path.push(new Point(point.x, point.y));
307289
point = point.parent;
308290
iterations++;
309291

@@ -314,10 +296,7 @@ export class Pathfinding {
314296
if(point === null) {
315297
break;
316298
}
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]));
321300

322301
return path.reverse();
323302
}
@@ -327,35 +306,36 @@ export class Pathfinding {
327306
return;
328307
}
329308

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);
333310

334311
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);
337314
}
338315

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)) {
340317
point.parent = this.currentPoint;
341318
point.cost = nextStepCost;
342-
this.openPoints.push(point);
319+
this.openPoints.add(point);
343320
}
344321
}
345322

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+
346329
private calculateBestPoint(): Point {
347330
let bestPoint: Point = null;
348331

349-
for(const point of this.openPoints) {
350-
if(bestPoint === null) {
332+
this.openPoints.forEach(point => {
333+
if(!bestPoint) {
351334
bestPoint = point;
352-
continue;
353-
}
354-
355-
if(point.cost < bestPoint.cost) {
335+
} else if(point.cost < bestPoint.cost) {
356336
bestPoint = point;
357337
}
358-
}
338+
});
359339

360340
return bestPoint;
361341
}

src/world/actor/walking-queue.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ export class WalkingQueue {
171171
return;
172172
}
173173

174+
this.actor.lastMovementPosition = this.actor.position;
174175
this.actor.position = walkPosition;
175176

176177
let runDir = -1;
@@ -186,6 +187,7 @@ export class WalkingQueue {
186187
runDir = this.calculateDirection(runDiffX, runDiffY);
187188

188189
if(runDir != -1) {
190+
this.actor.lastMovementPosition = this.actor.position;
189191
this.actor.position = runPosition;
190192
}
191193
} else {
@@ -204,10 +206,6 @@ export class WalkingQueue {
204206
this.actor.faceDirection = walkDir;
205207
}
206208

207-
if(this.queue.length !== 0) {
208-
this.actor.lastMovementPosition = this.actor.position;
209-
}
210-
211209
const newChunk = world.chunkManager.getChunkForWorldPosition(this.actor.position);
212210

213211
if(this.actor instanceof Player) {

0 commit comments

Comments
 (0)