refactor: move worker into worker file

This commit is contained in:
Evan
2024-11-29 15:57:39 -08:00
parent 020d193667
commit 4899196613
8 changed files with 37 additions and 38 deletions
-123
View File
@@ -1,123 +0,0 @@
import { TerrainTile, Tile, Game, GameMap, Cell } from "../game/Game";
import { AStar, PathFindResultType } from "./AStar";
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);
});
}
createParallelAStar(src: Tile, dst: Tile, numTicks: number): ParallelAStar {
if (!this.isInitialized) {
throw new Error('PathFinder not initialized');
}
return new ParallelAStar(this.game, this.worker, src, dst, numTicks);
}
cleanup() {
this.worker.terminate();
}
}
export class ParallelAStar implements AStar {
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> {
const requestId = crypto.randomUUID()
this.promise = new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject("Path timeout");
}, 100_000);
const handler = (e: MessageEvent) => {
if (e.data.requestId != requestId) {
return
}
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 if (e.data.type === 'pathNotFound') {
this.path = 'NOT_FOUND'
} else {
reject(e.data.reason || "Path not found");
}
};
this.worker.addEventListener('message', handler);
this.worker.postMessage({
type: 'findPath',
requestId: requestId,
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) {
if (this.path == 'NOT_FOUND') {
return PathFindResultType.PathNotFound
}
if (this.path != null) {
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[]
}
}
-101
View File
@@ -1,101 +0,0 @@
// pathfinding.ts
import { Cell, GameMap, TerrainMap, TerrainTile, TerrainType } from "../game/Game";
import { SearchNode } from "./AStar";
import { PathFindResultType } from "./AStar";
import { SerialAStar } from "./SerialAStar";
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: SerialAStar,
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, .1) as unknown as number;
}
function findPath(terrainMap: TerrainMap, req: SearchRequest) {
const aStar = new SerialAStar(
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)),
10_000,
req.duration,
);
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: 'pathNotFound',
requestId: search.requestId,
});
break
}
}
} finally {
isProcessingSearch = false
}
}
+3 -3
View File
@@ -1,7 +1,7 @@
import { Game, Tile } from "../game/Game";
import { manhattanDist } from "../Util";
import { AStar, PathFindResultType, TileResult } from "./AStar";
import { AsyncPathFinderCreator, ParallelAStar } from "./AsyncPathFinding";
import { ParallelAStar, WorkerClient } from "../worker/WorkerClient";
import { SerialAStar } from "./SerialAStar";
export class PathFinder {
@@ -29,10 +29,10 @@ export class PathFinder {
)
}
public static Parallel(creator: AsyncPathFinderCreator, numTicks: number): PathFinder {
public static Parallel(worker: WorkerClient, numTicks: number): PathFinder {
return new PathFinder(
(curr: Tile, dst: Tile) => {
return creator.createParallelAStar(curr, dst, numTicks)
return worker.createParallelAStar(curr, dst, numTicks)
}
)
}