format codebase with prettier

This commit is contained in:
Evan
2025-01-30 19:46:36 -08:00
parent cd121a5cd4
commit 4ee37323f9
98 changed files with 12191 additions and 10234 deletions
+24 -20
View File
@@ -1,29 +1,33 @@
import { TileRef } from "../game/GameMap";
export interface AStar {
compute(): PathFindResultType
reconstructPath(): TileRef[]
compute(): PathFindResultType;
reconstructPath(): TileRef[];
}
export enum PathFindResultType {
NextTile,
Pending,
Completed,
PathNotFound
} export type TileResult = {
type: PathFindResultType.NextTile;
tile: TileRef;
} | {
type: PathFindResultType.Pending;
} | {
type: PathFindResultType.Completed;
tile: TileRef;
} | {
type: PathFindResultType.PathNotFound;
};
NextTile,
Pending,
Completed,
PathNotFound,
}
export type TileResult =
| {
type: PathFindResultType.NextTile;
tile: TileRef;
}
| {
type: PathFindResultType.Pending;
}
| {
type: PathFindResultType.Completed;
tile: TileRef;
}
| {
type: PathFindResultType.PathNotFound;
};
export interface Point {
x: number;
y: number;
x: number;
y: number;
}
+72 -69
View File
@@ -1,89 +1,92 @@
import { Cell, } from "../game/Game";
import { Cell } from "../game/Game";
import { GameMap, GameMapImpl, TileRef } from "../game/GameMap";
import { AStar, PathFindResultType, } from "./AStar";
import { AStar, PathFindResultType } from "./AStar";
import { SerialAStar } from "./SerialAStar";
// TODO: test this, get it work
export class MiniAStar implements AStar {
private aStar: SerialAStar;
private aStar: SerialAStar
constructor(
private gameMap: GameMap,
private miniMap: GameMap,
private src: TileRef,
private dst: TileRef,
private canMove: (t: TileRef) => boolean,
private iterations: number,
private maxTries: number,
) {
const miniSrc = this.miniMap.ref(
Math.floor(gameMap.x(src) / 2),
Math.floor(gameMap.y(src) / 2),
);
const miniDst = this.miniMap.ref(
Math.floor(gameMap.x(dst) / 2),
Math.floor(gameMap.y(dst) / 2),
);
this.aStar = new SerialAStar(
miniSrc,
miniDst,
canMove,
iterations,
maxTries,
this.miniMap,
);
}
constructor(
private gameMap: GameMap,
private miniMap: GameMap,
private src: TileRef,
private dst: TileRef,
private canMove: (t: TileRef) => boolean,
private iterations: number,
private maxTries: number
) {
const miniSrc = this.miniMap.ref(
Math.floor(gameMap.x(src) / 2),
Math.floor(gameMap.y(src) / 2)
)
const miniDst = this.miniMap.ref(
Math.floor(gameMap.x(dst) / 2),
Math.floor(gameMap.y(dst) / 2)
)
this.aStar = new SerialAStar(
miniSrc,
miniDst,
canMove,
iterations,
maxTries,
this.miniMap
)
}
compute(): PathFindResultType {
return this.aStar.compute()
}
reconstructPath(): TileRef[] {
const upscaled = upscalePath(this.aStar.reconstructPath().map(tr => new Cell(this.miniMap.x(tr), this.miniMap.y(tr))))
upscaled.push(new Cell(this.gameMap.x(this.dst), this.gameMap.y(this.dst)))
return upscaled.map(c => this.gameMap.ref(c.x, c.y))
}
compute(): PathFindResultType {
return this.aStar.compute();
}
reconstructPath(): TileRef[] {
const upscaled = upscalePath(
this.aStar
.reconstructPath()
.map((tr) => new Cell(this.miniMap.x(tr), this.miniMap.y(tr))),
);
upscaled.push(new Cell(this.gameMap.x(this.dst), this.gameMap.y(this.dst)));
return upscaled.map((c) => this.gameMap.ref(c.x, c.y));
}
}
function upscalePath(path: Cell[], scaleFactor: number = 2): Cell[] {
// Scale up each point
const scaledPath = path.map(point => (new Cell(
point.x * scaleFactor,
point.y * scaleFactor
)));
// Scale up each point
const scaledPath = path.map(
(point) => new Cell(point.x * scaleFactor, point.y * scaleFactor),
);
const smoothPath: Cell[] = [];
const smoothPath: Cell[] = [];
for (let i = 0; i < scaledPath.length - 1; i++) {
const current = scaledPath[i];
const next = scaledPath[i + 1];
for (let i = 0; i < scaledPath.length - 1; i++) {
const current = scaledPath[i];
const next = scaledPath[i + 1];
// Add the current point
smoothPath.push(current);
// Add the current point
smoothPath.push(current);
// Always interpolate between scaled points
const dx = next.x - current.x;
const dy = next.y - current.y;
// Always interpolate between scaled points
const dx = next.x - current.x;
const dy = next.y - current.y;
// Calculate number of steps needed
const distance = Math.max(Math.abs(dx), Math.abs(dy));
const steps = distance;
// Calculate number of steps needed
const distance = Math.max(Math.abs(dx), Math.abs(dy));
const steps = distance;
// Add intermediate points
for (let step = 1; step < steps; step++) {
smoothPath.push(new Cell(
Math.round(current.x + (dx * step) / steps),
Math.round(current.y + (dy * step) / steps)
));
}
// Add intermediate points
for (let step = 1; step < steps; step++) {
smoothPath.push(
new Cell(
Math.round(current.x + (dx * step) / steps),
Math.round(current.y + (dy * step) / steps),
),
);
}
}
// Add the last point
if (scaledPath.length > 0) {
smoothPath.push(scaledPath[scaledPath.length - 1]);
}
// Add the last point
if (scaledPath.length > 0) {
smoothPath.push(scaledPath[scaledPath.length - 1]);
}
return smoothPath;
}
return smoothPath;
}
+86 -86
View File
@@ -6,98 +6,98 @@ import { consolex } from "../Consolex";
import { TileRef } from "../game/GameMap";
export class PathFinder {
private curr: TileRef = null;
private dst: TileRef = null;
private path: TileRef[];
private aStar: AStar;
private computeFinished = true;
private curr: TileRef = null
private dst: TileRef = null
private path: TileRef[]
private aStar: AStar
private computeFinished = true
private constructor(
private game: Game,
private newAStar: (curr: TileRef, dst: TileRef) => AStar,
) {}
private constructor(
private game: Game,
private newAStar: (curr: TileRef, dst: TileRef) => AStar
) { }
public static Mini(
game: Game,
iterations: number,
canMoveOnLand: boolean,
maxTries: number = 20,
) {
return new PathFinder(game, (curr: TileRef, dst: TileRef) => {
return new MiniAStar(
game.map(),
game.miniMap(),
curr,
dst,
(tr: TileRef): boolean => {
if (canMoveOnLand) {
return true;
}
return game.miniMap().isOcean(tr);
},
iterations,
maxTries,
);
});
}
public static Mini(game: Game, iterations: number, canMoveOnLand: boolean, maxTries: number = 20) {
return new PathFinder(
game,
(curr: TileRef, dst: TileRef) => {
return new MiniAStar(
game.map(),
game.miniMap(),
curr,
dst,
(tr: TileRef): boolean => {
if (canMoveOnLand) {
return true
}
return game.miniMap().isOcean(tr)
},
iterations,
maxTries
)
}
)
nextTile(curr: TileRef, dst: TileRef, dist: number = 1): TileResult {
if (curr == null) {
consolex.error("curr is null");
}
if (dst == null) {
consolex.error("dst is null");
}
nextTile(curr: TileRef, dst: TileRef, dist: number = 1): TileResult {
if (curr == null) {
consolex.error('curr is null')
}
if (dst == null) {
consolex.error('dst is null')
}
if (this.game.manhattanDist(curr, dst) < 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 = this.newAStar(curr, dst)
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()
// 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 }
}
if (this.game.manhattanDist(curr, dst) < dist) {
return { type: PathFindResultType.Completed, tile: curr };
}
private shouldRecompute(curr: TileRef, dst: TileRef) {
if (this.path == null || this.curr == null || this.dst == null) {
return true
}
const dist = this.game.manhattanDist(curr, dst)
let tolerance = 10
if (dist > 50) {
tolerance = 10
} else if (dist > 25) {
tolerance = 5
} else if (dist > 10) {
tolerance = 3
} else {
tolerance = 0
}
if (this.game.manhattanDist(this.dst, dst) > tolerance) {
return true
}
return false
if (this.computeFinished) {
if (this.shouldRecompute(curr, dst)) {
this.curr = curr;
this.dst = dst;
this.path = null;
this.aStar = this.newAStar(curr, dst);
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();
// 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: TileRef, dst: TileRef) {
if (this.path == null || this.curr == null || this.dst == null) {
return true;
}
const dist = this.game.manhattanDist(curr, dst);
let tolerance = 10;
if (dist > 50) {
tolerance = 10;
} else if (dist > 25) {
tolerance = 5;
} else if (dist > 10) {
tolerance = 3;
} else {
tolerance = 0;
}
if (this.game.manhattanDist(this.dst, dst) > tolerance) {
return true;
}
return false;
}
}
+123 -116
View File
@@ -4,137 +4,144 @@ import { PathFindResultType } from "./AStar";
import { consolex } from "../Consolex";
import { GameMap, GameMapImpl, TileRef } from "../game/GameMap";
export class SerialAStar implements AStar {
private fwdOpenSet: PriorityQueue<{ tile: TileRef; fScore: number; }>;
private bwdOpenSet: PriorityQueue<{ tile: TileRef; fScore: number; }>;
private fwdCameFrom: Map<TileRef, TileRef>;
private bwdCameFrom: Map<TileRef, TileRef>;
private fwdGScore: Map<TileRef, number>;
private bwdGScore: Map<TileRef, number>;
private meetingPoint: TileRef | null;
public completed: boolean;
private fwdOpenSet: PriorityQueue<{ tile: TileRef; fScore: number }>;
private bwdOpenSet: PriorityQueue<{ tile: TileRef; fScore: number }>;
private fwdCameFrom: Map<TileRef, TileRef>;
private bwdCameFrom: Map<TileRef, TileRef>;
private fwdGScore: Map<TileRef, number>;
private bwdGScore: Map<TileRef, number>;
private meetingPoint: TileRef | null;
public completed: boolean;
constructor(
private src: TileRef,
private dst: TileRef,
private canMove: (t: TileRef) => boolean,
private iterations: number,
private maxTries: number,
private gameMap: GameMap
) {
this.fwdOpenSet = new PriorityQueue<{ tile: TileRef; fScore: number; }>(
(a, b) => a.fScore - b.fScore
);
this.bwdOpenSet = new PriorityQueue<{ tile: TileRef; fScore: number; }>(
(a, b) => a.fScore - b.fScore
);
this.fwdCameFrom = new Map<TileRef, TileRef>();
this.bwdCameFrom = new Map<TileRef, TileRef>();
this.fwdGScore = new Map<TileRef, number>();
this.bwdGScore = new Map<TileRef, number>();
this.meetingPoint = null;
this.completed = false;
constructor(
private src: TileRef,
private dst: TileRef,
private canMove: (t: TileRef) => boolean,
private iterations: number,
private maxTries: number,
private gameMap: GameMap,
) {
this.fwdOpenSet = new PriorityQueue<{ tile: TileRef; fScore: number }>(
(a, b) => a.fScore - b.fScore,
);
this.bwdOpenSet = new PriorityQueue<{ tile: TileRef; fScore: number }>(
(a, b) => a.fScore - b.fScore,
);
this.fwdCameFrom = new Map<TileRef, TileRef>();
this.bwdCameFrom = new Map<TileRef, TileRef>();
this.fwdGScore = new Map<TileRef, number>();
this.bwdGScore = new Map<TileRef, 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 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) });
// 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.expandTileRef(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.expandTileRef(bwdCurrent, false);
}
compute(): PathFindResultType {
if (this.completed) return PathFindResultType.Completed;
return this.completed
? PathFindResultType.Completed
: PathFindResultType.PathNotFound;
}
this.maxTries -= 1;
let iterations = this.iterations;
private expandTileRef(current: TileRef, isForward: boolean) {
for (const neighbor of this.gameMap.neighbors(current)) {
if (
neighbor != (isForward ? this.dst : this.src) &&
!this.canMove(neighbor)
)
continue;
while (!this.fwdOpenSet.isEmpty() && !this.bwdOpenSet.isEmpty()) {
iterations--;
if (iterations <= 0) {
if (this.maxTries <= 0) {
return PathFindResultType.PathNotFound;
}
return PathFindResultType.Pending;
}
const gScore = isForward ? this.fwdGScore : this.bwdGScore;
const openSet = isForward ? this.fwdOpenSet : this.bwdOpenSet;
const cameFrom = isForward ? this.fwdCameFrom : this.bwdCameFrom;
// 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;
}
let tentativeGScore = gScore.get(current)! + this.gameMap.cost(neighbor);
this.expandTileRef(fwdCurrent, true);
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 });
}
}
}
// 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;
}
private heuristic(a: TileRef, b: TileRef): number {
// TODO use wrapped
try {
return (
1.1 * Math.abs(this.gameMap.x(a) - this.gameMap.x(b)) +
Math.abs(this.gameMap.y(a) - this.gameMap.y(b))
);
} catch {
consolex.log("uh oh");
}
}
this.expandTileRef(bwdCurrent, false);
}
public reconstructPath(): TileRef[] {
if (!this.meetingPoint) return [];
return this.completed ? PathFindResultType.Completed : PathFindResultType.PathNotFound;
// Reconstruct path from start to meeting point
const fwdPath: TileRef[] = [this.meetingPoint];
let current = this.meetingPoint;
while (this.fwdCameFrom.has(current)) {
current = this.fwdCameFrom.get(current)!;
fwdPath.unshift(current);
}
private expandTileRef(current: TileRef, isForward: boolean) {
for (const neighbor of this.gameMap.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)! + this.gameMap.cost(neighbor);
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 });
}
}
// Reconstruct path from meeting point to goal
current = this.meetingPoint;
while (this.bwdCameFrom.has(current)) {
current = this.bwdCameFrom.get(current)!;
fwdPath.push(current);
}
private heuristic(a: TileRef, b: TileRef): number {
// TODO use wrapped
try {
return 1.1 * Math.abs(this.gameMap.x(a) - this.gameMap.x(b)) + Math.abs(this.gameMap.y(a) - this.gameMap.y(b));
} catch {
consolex.log('uh oh')
}
}
public reconstructPath(): TileRef[] {
if (!this.meetingPoint) return [];
// Reconstruct path from start to meeting point
const fwdPath: TileRef[] = [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
}
return fwdPath;
}
}