mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-22 13:59:48 +00:00
created path finding web worker
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
import { PriorityQueue } from "@datastructures-js/priority-queue";
|
||||
import { SearchNode } from "../game/Game";
|
||||
import { PathFindResultType } from "./PathFinding";
|
||||
|
||||
|
||||
export class AStar {
|
||||
private fwdOpenSet: PriorityQueue<{ tile: SearchNode; fScore: number; }>;
|
||||
private bwdOpenSet: PriorityQueue<{ tile: SearchNode; fScore: number; }>;
|
||||
private fwdCameFrom: Map<SearchNode, SearchNode>;
|
||||
private bwdCameFrom: Map<SearchNode, SearchNode>;
|
||||
private fwdGScore: Map<SearchNode, number>;
|
||||
private bwdGScore: Map<SearchNode, number>;
|
||||
private meetingPoint: SearchNode | null;
|
||||
public completed: boolean;
|
||||
|
||||
constructor(
|
||||
private src: SearchNode,
|
||||
private dst: SearchNode,
|
||||
private canMove: (t: SearchNode) => boolean,
|
||||
private neighbors: (sn: SearchNode) => SearchNode[],
|
||||
private iterations: number,
|
||||
private maxTries: number
|
||||
) {
|
||||
this.fwdOpenSet = new PriorityQueue<{ tile: SearchNode; fScore: number; }>(
|
||||
(a, b) => a.fScore - b.fScore
|
||||
);
|
||||
this.bwdOpenSet = new PriorityQueue<{ tile: SearchNode; fScore: number; }>(
|
||||
(a, b) => a.fScore - b.fScore
|
||||
);
|
||||
this.fwdCameFrom = new Map<SearchNode, SearchNode>();
|
||||
this.bwdCameFrom = new Map<SearchNode, SearchNode>();
|
||||
this.fwdGScore = new Map<SearchNode, number>();
|
||||
this.bwdGScore = new Map<SearchNode, number>();
|
||||
this.meetingPoint = null;
|
||||
this.completed = false;
|
||||
|
||||
// Initialize forward search
|
||||
this.fwdGScore.set(src, 0);
|
||||
this.fwdOpenSet.enqueue({ tile: src, fScore: this.heuristic(src, dst) });
|
||||
|
||||
// Initialize backward search
|
||||
this.bwdGScore.set(dst, 0);
|
||||
this.bwdOpenSet.enqueue({ tile: dst, fScore: this.heuristic(dst, src) });
|
||||
}
|
||||
|
||||
compute(): PathFindResultType {
|
||||
if (this.completed) return PathFindResultType.Completed;
|
||||
|
||||
this.maxTries -= 1;
|
||||
let iterations = this.iterations;
|
||||
|
||||
while (!this.fwdOpenSet.isEmpty() && !this.bwdOpenSet.isEmpty()) {
|
||||
iterations--;
|
||||
if (iterations <= 0) {
|
||||
if (this.maxTries <= 0) {
|
||||
return PathFindResultType.PathNotFound;
|
||||
}
|
||||
return PathFindResultType.Pending;
|
||||
}
|
||||
|
||||
// Process forward search
|
||||
const fwdCurrent = this.fwdOpenSet.dequeue()!.tile;
|
||||
if (this.bwdGScore.has(fwdCurrent)) {
|
||||
// We found a meeting point!
|
||||
this.meetingPoint = fwdCurrent;
|
||||
this.completed = true;
|
||||
return PathFindResultType.Completed;
|
||||
}
|
||||
|
||||
this.expandSearchNode(fwdCurrent, true);
|
||||
|
||||
// Process backward search
|
||||
const bwdCurrent = this.bwdOpenSet.dequeue()!.tile;
|
||||
if (this.fwdGScore.has(bwdCurrent)) {
|
||||
// We found a meeting point!
|
||||
this.meetingPoint = bwdCurrent;
|
||||
this.completed = true;
|
||||
return PathFindResultType.Completed;
|
||||
}
|
||||
|
||||
this.expandSearchNode(bwdCurrent, false);
|
||||
}
|
||||
|
||||
return this.completed ? PathFindResultType.Completed : PathFindResultType.PathNotFound;
|
||||
}
|
||||
|
||||
private expandSearchNode(current: SearchNode, isForward: boolean) {
|
||||
for (const neighbor of this.neighbors(current)) {
|
||||
if (neighbor !== (isForward ? this.dst : this.src) && !this.canMove(neighbor)) continue;
|
||||
|
||||
const gScore = isForward ? this.fwdGScore : this.bwdGScore;
|
||||
const openSet = isForward ? this.fwdOpenSet : this.bwdOpenSet;
|
||||
const cameFrom = isForward ? this.fwdCameFrom : this.bwdCameFrom;
|
||||
|
||||
let tentativeGScore = gScore.get(current)! + neighbor.cost();
|
||||
|
||||
if (!gScore.has(neighbor) || tentativeGScore < gScore.get(neighbor)!) {
|
||||
cameFrom.set(neighbor, current);
|
||||
gScore.set(neighbor, tentativeGScore);
|
||||
const fScore = tentativeGScore + this.heuristic(
|
||||
neighbor,
|
||||
isForward ? this.dst : this.src
|
||||
);
|
||||
openSet.enqueue({ tile: neighbor, fScore: fScore });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private heuristic(a: SearchNode, b: SearchNode): number {
|
||||
// TODO use wrapped
|
||||
try {
|
||||
return 1.1 * Math.abs(a.cell().x - b.cell().x) + Math.abs(a.cell().y - b.cell().y);
|
||||
} catch {
|
||||
console.log('uh oh')
|
||||
}
|
||||
}
|
||||
|
||||
public reconstructPath(): SearchNode[] {
|
||||
if (!this.meetingPoint) return [];
|
||||
|
||||
// Reconstruct path from start to meeting point
|
||||
const fwdPath: SearchNode[] = [this.meetingPoint];
|
||||
let current = this.meetingPoint;
|
||||
while (this.fwdCameFrom.has(current)) {
|
||||
current = this.fwdCameFrom.get(current)!;
|
||||
fwdPath.unshift(current);
|
||||
}
|
||||
|
||||
// Reconstruct path from meeting point to goal
|
||||
current = this.meetingPoint;
|
||||
while (this.bwdCameFrom.has(current)) {
|
||||
current = this.bwdCameFrom.get(current)!;
|
||||
fwdPath.push(current);
|
||||
}
|
||||
|
||||
return fwdPath;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
import { TerrainTile, Tile, Game, GameMap, Cell } from "../game/Game";
|
||||
import { PathFindResultType } from "./PathFinding";
|
||||
|
||||
export class AsyncPathFinderCreator {
|
||||
private worker: Worker;
|
||||
private isInitialized = false;
|
||||
|
||||
constructor(private game: Game, private gameMap: GameMap) {
|
||||
// Create a new worker using webpack worker-loader
|
||||
// The import.meta.url ensures webpack can properly bundle the worker
|
||||
this.worker = new Worker(new URL('./PathFinder.worker.ts', import.meta.url));
|
||||
}
|
||||
|
||||
initialize(): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.worker.postMessage({
|
||||
type: 'init',
|
||||
gameMap: this.gameMap
|
||||
});
|
||||
|
||||
const handler = (e: MessageEvent) => {
|
||||
if (e.data.type === 'initialized') {
|
||||
this.worker.removeEventListener('message', handler);
|
||||
this.isInitialized = true;
|
||||
resolve();
|
||||
} else {
|
||||
this.worker.removeEventListener('message', handler);
|
||||
reject('Failed to initialize pathfinder');
|
||||
}
|
||||
};
|
||||
|
||||
this.worker.addEventListener('message', handler);
|
||||
});
|
||||
}
|
||||
|
||||
createPathFinder(src: Tile, dst: Tile, numTicks: number): AsyncPathFinder {
|
||||
if (!this.isInitialized) {
|
||||
throw new Error('PathFinder not initialized');
|
||||
}
|
||||
return new AsyncPathFinder(this.game, this.worker, src, dst, numTicks);
|
||||
}
|
||||
|
||||
cleanup() {
|
||||
this.worker.terminate();
|
||||
}
|
||||
}
|
||||
|
||||
// AsyncPathFinder.ts
|
||||
export class AsyncPathFinder {
|
||||
private path: Tile[] | 'NOT_FOUND' | null = null;
|
||||
private promise: Promise<void>;
|
||||
|
||||
constructor(
|
||||
private game: Game,
|
||||
private worker: Worker,
|
||||
private src: Tile,
|
||||
private dst: Tile,
|
||||
private numTicks: number
|
||||
) { }
|
||||
|
||||
findPath(): Promise<void> {
|
||||
this.promise = new Promise((resolve, reject) => {
|
||||
const timeout = setTimeout(() => {
|
||||
reject("Path timeout");
|
||||
}, 100_000);
|
||||
|
||||
const handler = (e: MessageEvent) => {
|
||||
clearTimeout(timeout);
|
||||
this.worker.removeEventListener('message', handler);
|
||||
|
||||
if (e.data.type === 'pathFound') {
|
||||
this.path = e.data.path.map(pos => this.game.tile(new Cell(pos.x, pos.y)));
|
||||
resolve();
|
||||
} else {
|
||||
reject(e.data.reason || "Path not found");
|
||||
}
|
||||
};
|
||||
|
||||
this.worker.addEventListener('message', handler);
|
||||
this.worker.postMessage({
|
||||
type: 'findPath',
|
||||
requestId: crypto.randomUUID(),
|
||||
currentTick: this.game.ticks(),
|
||||
duration: this.numTicks,
|
||||
start: { x: this.src.cell().x, y: this.src.cell().y },
|
||||
end: { x: this.dst.cell().x, y: this.dst.cell().y }
|
||||
});
|
||||
});
|
||||
|
||||
return this.promise;
|
||||
}
|
||||
|
||||
// TODO: rename to poll?
|
||||
compute(): PathFindResultType {
|
||||
if (this.promise == null) {
|
||||
this.findPath()
|
||||
}
|
||||
this.numTicks--;
|
||||
if (this.numTicks <= 0) {
|
||||
for (let i = 0; i < 1_000_000; i++) {
|
||||
if (this.path == 'NOT_FOUND') {
|
||||
return PathFindResultType.PathNotFound
|
||||
}
|
||||
if (this.path != null) {
|
||||
console.log('in Asyncclient: found a path!!')
|
||||
return PathFindResultType.Completed;
|
||||
}
|
||||
}
|
||||
throw new Error(`path not completed in time`)
|
||||
}
|
||||
return PathFindResultType.Pending;
|
||||
}
|
||||
|
||||
reconstructPath(): Tile[] {
|
||||
if (this.path == "NOT_FOUND" || this.path == null) {
|
||||
throw Error(`cannot reconstruct path: ${this.path}`)
|
||||
}
|
||||
return this.path as Tile[]
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
// pathfinding.ts
|
||||
import { Cell, GameMap, SearchNode, TerrainMap, TerrainTile, TerrainType } from "../game/Game";
|
||||
import { PathFindResultType } from "./PathFinding";
|
||||
import { AStar } from "./AStar";
|
||||
import { loadTerrainMap } from "../game/TerrainMapLoader";
|
||||
import { PriorityQueue } from "@datastructures-js/priority-queue";
|
||||
|
||||
let terrainMapPromise: Promise<TerrainMap>;
|
||||
let searches = new PriorityQueue<Search>((a: Search, b: Search) => (a.deadline - b.deadline))
|
||||
let processingInterval: number | null = null;
|
||||
let isProcessingSearch = false
|
||||
|
||||
|
||||
interface Search {
|
||||
aStar: AStar,
|
||||
deadline: number
|
||||
requestId: string
|
||||
}
|
||||
|
||||
interface SearchRequest {
|
||||
requestId: string
|
||||
currentTick: number
|
||||
// duration in ticks
|
||||
duration: number
|
||||
start: { x: number, y: number },
|
||||
end: { x: number, y: number }
|
||||
}
|
||||
|
||||
self.onmessage = (e) => {
|
||||
switch (e.data.type) {
|
||||
case 'init':
|
||||
initializeMap(e.data);
|
||||
break;
|
||||
case 'findPath':
|
||||
terrainMapPromise.then(tm => findPath(tm, e.data))
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
function initializeMap(data: { gameMap: GameMap }) {
|
||||
terrainMapPromise = loadTerrainMap(data.gameMap)
|
||||
self.postMessage({ type: 'initialized' });
|
||||
processingInterval = setInterval(computeSearches, .5) as unknown as number;
|
||||
}
|
||||
|
||||
function findPath(terrainMap: TerrainMap, req: SearchRequest) {
|
||||
const aStar = new AStar(
|
||||
terrainMap.terrain(new Cell(req.start.x, req.start.y)),
|
||||
terrainMap.terrain(new Cell(req.end.x, req.end.y)),
|
||||
(sn: SearchNode) => (sn as TerrainTile).terrainType() == TerrainType.Ocean,
|
||||
(sn: SearchNode): SearchNode[] => terrainMap.neighbors((sn as TerrainTile)),
|
||||
100_000,
|
||||
1000
|
||||
);
|
||||
|
||||
searches.enqueue({
|
||||
aStar: aStar,
|
||||
deadline: req.currentTick + req.duration,
|
||||
requestId: req.requestId
|
||||
})
|
||||
}
|
||||
|
||||
function computeSearches() {
|
||||
if (isProcessingSearch || searches.isEmpty()) {
|
||||
return
|
||||
}
|
||||
|
||||
isProcessingSearch = true
|
||||
|
||||
try {
|
||||
for (let i = 0; i < 10; i++) {
|
||||
if (searches.isEmpty()) {
|
||||
return
|
||||
}
|
||||
const search = searches.dequeue()
|
||||
switch (search.aStar.compute()) {
|
||||
case PathFindResultType.Completed:
|
||||
self.postMessage({
|
||||
type: 'pathFound',
|
||||
requestId: search.requestId,
|
||||
path: search.aStar.reconstructPath().map(sn => ({ x: sn.cell().x, y: sn.cell().y }))
|
||||
});
|
||||
break;
|
||||
|
||||
case PathFindResultType.Pending:
|
||||
searches.push(search)
|
||||
break
|
||||
case PathFindResultType.PathNotFound:
|
||||
console.warn(`worker: path not found to port`);
|
||||
self.postMessage({
|
||||
type: 'error',
|
||||
requestId: search.requestId,
|
||||
});
|
||||
break
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
isProcessingSearch = false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
import { Tile } from "../game/Game";
|
||||
import { manhattanDist } from "../Util";
|
||||
import { AStar } from "./AStar";
|
||||
|
||||
export enum PathFindResultType {
|
||||
NextTile,
|
||||
Pending,
|
||||
Completed,
|
||||
PathNotFound
|
||||
}
|
||||
|
||||
export type TileResult = {
|
||||
type: PathFindResultType.NextTile;
|
||||
tile: Tile
|
||||
} | {
|
||||
type: PathFindResultType.Pending;
|
||||
} | {
|
||||
type: PathFindResultType.Completed;
|
||||
tile: Tile
|
||||
} | {
|
||||
type: PathFindResultType.PathNotFound;
|
||||
}
|
||||
|
||||
|
||||
export class PathFinder {
|
||||
|
||||
private curr: Tile = null
|
||||
private dst: Tile = null
|
||||
private path: Tile[]
|
||||
private aStar: AStar
|
||||
private computeFinished = true
|
||||
|
||||
constructor(
|
||||
private iterations: number,
|
||||
private canMove: (t: Tile) => boolean,
|
||||
private maxTries: number = 20
|
||||
) { }
|
||||
|
||||
nextTile(curr: Tile, dst: Tile, dist: number = 1): TileResult {
|
||||
if (curr == null) {
|
||||
console.error('curr is null')
|
||||
}
|
||||
if (dst == null) {
|
||||
console.error('dst is null')
|
||||
}
|
||||
|
||||
if (manhattanDist(curr.cell(), dst.cell()) < dist) {
|
||||
return { type: PathFindResultType.Completed, tile: curr }
|
||||
}
|
||||
|
||||
if (this.computeFinished) {
|
||||
if (this.shouldRecompute(curr, dst)) {
|
||||
this.curr = curr
|
||||
this.dst = dst
|
||||
this.path = null
|
||||
this.aStar = new AStar(curr, dst, this.canMove, sn => ((sn as Tile).neighbors()), this.iterations, this.maxTries)
|
||||
this.computeFinished = false
|
||||
return this.nextTile(curr, dst)
|
||||
} else {
|
||||
return { type: PathFindResultType.NextTile, tile: this.path.shift() }
|
||||
}
|
||||
}
|
||||
|
||||
switch (this.aStar.compute()) {
|
||||
case PathFindResultType.Completed:
|
||||
this.computeFinished = true
|
||||
this.path = this.aStar.reconstructPath().map(sn => sn as Tile)
|
||||
// Remove the start tile
|
||||
this.path.shift()
|
||||
return this.nextTile(curr, dst)
|
||||
case PathFindResultType.Pending:
|
||||
return { type: PathFindResultType.Pending }
|
||||
case PathFindResultType.PathNotFound:
|
||||
return { type: PathFindResultType.PathNotFound }
|
||||
}
|
||||
}
|
||||
|
||||
private shouldRecompute(curr: Tile, dst: Tile) {
|
||||
if (this.path == null || this.curr == null || this.dst == null) {
|
||||
return true
|
||||
}
|
||||
const dist = manhattanDist(curr.cell(), dst.cell())
|
||||
let tolerance = 10
|
||||
if (dist > 50) {
|
||||
tolerance = 10
|
||||
} else if (dist > 25) {
|
||||
tolerance = 5
|
||||
} else if (dist > 10) {
|
||||
tolerance = 3
|
||||
} else {
|
||||
tolerance = 0
|
||||
}
|
||||
if (manhattanDist(this.dst.cell(), dst.cell()) > tolerance) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user