AStar returns cell instead of path

This commit is contained in:
evanpelle
2024-12-03 14:35:09 -08:00
parent 330b6b93cb
commit 4edd66dc1f
12 changed files with 57 additions and 59 deletions
+2 -1
View File
@@ -14,7 +14,7 @@ export class BattleshipExecution implements Execution {
private battleship: MutableUnit = null
private mg: MutableGame = null
private pathfinder = PathFinder.Serial(5000, t => t.isWater())
private pathfinder: PathFinder
private patrolTile: Tile;
private patrolCenterTile: Tile
@@ -31,6 +31,7 @@ export class BattleshipExecution implements Execution {
init(mg: MutableGame, ticks: number): void {
this.pathfinder = PathFinder.Serial(mg, 5000, t => t.isWater())
this._owner = mg.player(this.playerID)
this.mg = mg
this.patrolCenterTile = mg.tile(this.cell)
+3 -2
View File
@@ -1,4 +1,4 @@
import { Cell, Execution, MutableGame, MutablePlayer, MutableUnit, PlayerID, Tile, UnitType } from "../game/Game";
import { Cell, Execution, MutableGame, MutablePlayer, MutableUnit, PlayerID, TerrainType, Tile, UnitType } from "../game/Game";
import { PathFinder } from "../pathfinding/PathFinding";
import { PathFindResultType } from "../pathfinding/AStar";
import { SerialAStar } from "../pathfinding/SerialAStar";
@@ -14,7 +14,7 @@ export class DestroyerExecution implements Execution {
private mg: MutableGame = null
private target: MutableUnit = null
private pathfinder = PathFinder.Serial(5000, t => t.isWater())
private pathfinder: PathFinder
private patrolTile: Tile;
private patrolCenterTile: Tile
@@ -29,6 +29,7 @@ export class DestroyerExecution implements Execution {
init(mg: MutableGame, ticks: number): void {
this.pathfinder = PathFinder.Serial(mg, 5000, t => t.terrainType() == TerrainType.Ocean)
this._owner = mg.player(this.playerID)
this.mg = mg
this.patrolCenterTile = mg.tile(this.cell)
+2 -1
View File
@@ -16,7 +16,7 @@ export class NukeExecution implements Execution {
private nuke: MutableUnit
private dst: Tile
private pathFinder: PathFinder = PathFinder.Serial(10_000, () => true)
private pathFinder: PathFinder
constructor(
private type: UnitType.AtomBomb | UnitType.HydrogenBomb,
private senderID: PlayerID,
@@ -26,6 +26,7 @@ export class NukeExecution implements Execution {
init(mg: MutableGame, ticks: number): void {
this.mg = mg
this.pathFinder = PathFinder.Serial(mg, 10_000, () => true)
this.player = mg.player(this.senderID)
this.dst = this.mg.tile(this.cell)
}
+2 -2
View File
@@ -72,7 +72,7 @@ export class PortExecution implements Execution {
const aStar = this.computingPaths.get(port)
switch (aStar.compute()) {
case PathFindResultType.Completed:
this.portPaths.set(port, aStar.reconstructPath().map(sn => sn as Tile))
this.portPaths.set(port, aStar.reconstructPath().map(cell => this.mg.tile(cell)))
this.computingPaths.delete(port)
break
case PathFindResultType.Pending:
@@ -101,7 +101,7 @@ export class PortExecution implements Execution {
const port = this.random.randElement(portConnections)
const path = this.portPaths.get(port)
if (path != null) {
const pf = PathFinder.Parallel(this.worker, 30)
const pf = PathFinder.Parallel(this.mg, this.worker, 30)
this.mg.addExecution(new TradeShipExecution(this.player().id(), this.port, port, pf, path))
}
}
+2 -1
View File
@@ -5,7 +5,7 @@ import { PathFindResultType } from "../pathfinding/AStar";
export class ShellExecution implements Execution {
private active = true
private pathFinder = PathFinder.Serial(2000, () => true, 10)
private pathFinder: PathFinder
private shell: MutableUnit
constructor(private spawn: Tile, private _owner: MutablePlayer, private target: MutableUnit) {
@@ -13,6 +13,7 @@ export class ShellExecution implements Execution {
}
init(mg: MutableGame, ticks: number): void {
this.pathFinder = PathFinder.Serial(mg, 2000, () => true, 10)
}
tick(ticks: number): void {
+2 -1
View File
@@ -27,7 +27,7 @@ export class TransportShipExecution implements Execution {
private boat: MutableUnit
private pathFinder: PathFinder = PathFinder.Serial(10_000, t => t.isWater(), 2)
private pathFinder: PathFinder
constructor(
private attackerID: PlayerID,
@@ -43,6 +43,7 @@ export class TransportShipExecution implements Execution {
init(mg: MutableGame, ticks: number) {
this.lastMove = ticks
this.mg = mg
this.pathFinder = PathFinder.Serial(mg, 10_000, t => t.isWater(), 2)
this.attacker = mg.player(this.attackerID)
+1 -1
View File
@@ -2,7 +2,7 @@ import { Cell, TerrainType, Tile } from "../game/Game";
export interface AStar {
compute(): PathFindResultType
reconstructPath(): SearchNode[]
reconstructPath(): Cell[]
}
export enum PathFindResultType {
+12 -19
View File
@@ -31,29 +31,22 @@ export class MiniAStar implements AStar {
return this.aStar.compute()
}
reconstructPath(): SearchNode[] {
reconstructPath(): Cell[] {
const upscaled = upscalePath(this.aStar.reconstructPath())
.map(p => this.terrainMap.terrain(new Cell(p.x, p.y))) as SearchNode[]
upscaled.push(this.dst)
return upscaled
}
reconstructPathAsPoints(): Point[] {
const upscaled = upscalePath(this.aStar.reconstructPath())
upscaled.push({ x: this.dst.cell().x, y: this.dst.cell().y })
upscaled.push(this.dst.cell())
return upscaled
}
}
function upscalePath(path: SearchNode[], scaleFactor: number = 2): Point[] {
function upscalePath(path: Cell[], scaleFactor: number = 2): Cell[] {
// Scale up each point
const scaledPath = path.map(point => ({
x: point.cell().x * scaleFactor,
y: point.cell().y * scaleFactor
}));
const scaledPath = path.map(point => (new Cell(
point.x * scaleFactor,
point.y * scaleFactor
)));
const smoothPath: Point[] = [];
const smoothPath: Cell[] = [];
for (let i = 0; i < scaledPath.length - 1; i++) {
const current = scaledPath[i];
@@ -72,10 +65,10 @@ function upscalePath(path: SearchNode[], scaleFactor: number = 2): Point[] {
// Add intermediate points
for (let step = 1; step < steps; step++) {
smoothPath.push({
x: Math.round(current.x + (dx * step) / steps),
y: Math.round(current.y + (dy * step) / steps)
});
smoothPath.push(new Cell(
Math.round(current.x + (dx * step) / steps),
Math.round(current.y + (dy * step) / steps)
));
}
}
+10 -6
View File
@@ -1,4 +1,4 @@
import { Game, Tile } from "../game/Game";
import { Cell, Game, Tile } from "../game/Game";
import { manhattanDist } from "../Util";
import { AStar, PathFindResultType, TileResult } from "./AStar";
import { ParallelAStar, WorkerClient } from "../worker/WorkerClient";
@@ -9,17 +9,19 @@ export class PathFinder {
private curr: Tile = null
private dst: Tile = null
private path: Tile[]
private path: Cell[]
private aStar: AStar
private computeFinished = true
private constructor(
private game: Game,
private newAStar: (curr: Tile, dst: Tile) => AStar
) { }
public static Mini(game: Game, iterations: number, canMove: (t: Tile) => boolean, maxTries: number = 20) {
return new PathFinder(
game,
(curr: Tile, dst: Tile) => {
return new MiniAStar(
game.terrainMap(),
@@ -34,8 +36,9 @@ export class PathFinder {
)
}
public static Serial(iterations: number, canMove: (t: Tile) => boolean, maxTries: number = 20): PathFinder {
public static Serial(game: Game, iterations: number, canMove: (t: Tile) => boolean, maxTries: number = 20): PathFinder {
return new PathFinder(
game,
(curr: Tile, dst: Tile) => {
return new SerialAStar(
curr,
@@ -48,8 +51,9 @@ export class PathFinder {
)
}
public static Parallel(worker: WorkerClient, numTicks: number): PathFinder {
public static Parallel(game: Game, worker: WorkerClient, numTicks: number): PathFinder {
return new PathFinder(
game,
(curr: Tile, dst: Tile) => {
return worker.createParallelAStar(curr, dst, numTicks)
}
@@ -77,14 +81,14 @@ export class PathFinder {
this.computeFinished = false
return this.nextTile(curr, dst)
} else {
return { type: PathFindResultType.NextTile, tile: this.path.shift() }
return { type: PathFindResultType.NextTile, tile: this.game.tile(this.path.shift()) }
}
}
switch (this.aStar.compute()) {
case PathFindResultType.Completed:
this.computeFinished = true
this.path = this.aStar.reconstructPath() as Tile[]
this.path = this.aStar.reconstructPath()
// Remove the start tile
this.path.shift()
return this.nextTile(curr, dst)
+3 -2
View File
@@ -1,6 +1,7 @@
import { PriorityQueue } from "@datastructures-js/priority-queue";
import { AStar, SearchNode } from "./AStar";
import { PathFindResultType } from "./AStar";
import { Cell } from "../game/Game";
export class SerialAStar implements AStar {
@@ -114,7 +115,7 @@ export class SerialAStar implements AStar {
}
}
public reconstructPath(): SearchNode[] {
public reconstructPath(): Cell[] {
if (!this.meetingPoint) return [];
// Reconstruct path from start to meeting point
@@ -132,6 +133,6 @@ export class SerialAStar implements AStar {
fwdPath.push(current);
}
return fwdPath;
return fwdPath.map(sn => sn.cell());
}
}
+14 -19
View File
@@ -10,16 +10,11 @@ let searches = new PriorityQueue<Search>((a: Search, b: Search) => (a.deadline -
let processingInterval: number | null = null;
let isProcessingSearch = false
interface Point {
x: number
y: number
}
interface Search {
aStar: SerialAStar,
deadline: number
requestId: string,
end: Point
end: Cell
}
interface SearchRequest {
@@ -27,8 +22,8 @@ interface SearchRequest {
currentTick: number
// duration in ticks
duration: number
start: Point
end: Point
start: Cell
end: Cell
}
self.onmessage = (e) => {
@@ -81,7 +76,7 @@ function computeSearches() {
const search = searches.dequeue()
switch (search.aStar.compute()) {
case PathFindResultType.Completed:
const path = upscalePath(search.aStar.reconstructPath().map(sn => ({ x: sn.cell().x, y: sn.cell().y })))
const path = upscalePath(search.aStar.reconstructPath())
path.push(search.end)
self.postMessage({
type: 'pathFound',
@@ -107,14 +102,14 @@ function computeSearches() {
}
}
function upscalePath(path: Point[], scaleFactor: number = 2): Point[] {
function upscalePath(path: Cell[], scaleFactor: number = 2): Cell[] {
// Scale up each point
const scaledPath = path.map(point => ({
x: point.x * scaleFactor,
y: point.y * scaleFactor
}));
const scaledPath = path.map(point => (new Cell(
point.x * scaleFactor,
point.y * scaleFactor
)));
const smoothPath: Point[] = [];
const smoothPath: Cell[] = [];
for (let i = 0; i < scaledPath.length - 1; i++) {
const current = scaledPath[i];
@@ -133,10 +128,10 @@ function upscalePath(path: Point[], scaleFactor: number = 2): Point[] {
// Add intermediate points
for (let step = 1; step < steps; step++) {
smoothPath.push({
x: Math.round(current.x + (dx * step) / steps),
y: Math.round(current.y + (dy * step) / steps)
});
smoothPath.push(new Cell(
Math.round(current.x + (dx * step) / steps),
Math.round(current.y + (dy * step) / steps)
));
}
}
+4 -4
View File
@@ -46,7 +46,7 @@ export class WorkerClient {
}
}
export class ParallelAStar implements AStar {
private path: Tile[] | 'NOT_FOUND' | null = null;
private path: Cell[] | 'NOT_FOUND' | null = null;
private promise: Promise<void>;
constructor(
@@ -72,7 +72,7 @@ export class ParallelAStar implements AStar {
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)));
this.path = e.data.path
resolve();
} else if (e.data.type === 'pathNotFound') {
this.path = 'NOT_FOUND';
@@ -113,11 +113,11 @@ export class ParallelAStar implements AStar {
return PathFindResultType.Pending;
}
reconstructPath(): Tile[] {
reconstructPath(): Cell[] {
if (this.path == "NOT_FOUND" || this.path == null) {
throw Error(`cannot reconstruct path: ${this.path}`);
}
return this.path as Tile[];
return this.path
}
}