mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-24 13:52:45 +00:00
AStar returns cell instead of path
This commit is contained in:
@@ -2,7 +2,7 @@ import { Cell, TerrainType, Tile } from "../game/Game";
|
||||
|
||||
export interface AStar {
|
||||
compute(): PathFindResultType
|
||||
reconstructPath(): SearchNode[]
|
||||
reconstructPath(): Cell[]
|
||||
}
|
||||
|
||||
export enum PathFindResultType {
|
||||
|
||||
@@ -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)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user