use TileRef instead of TerrainTile for astar

This commit is contained in:
evanpelle
2025-02-01 12:05:11 -08:00
committed by Evan
parent 2a2f62436c
commit b22532d41f
16 changed files with 149 additions and 110 deletions
+42 -40
View File
@@ -1,46 +1,48 @@
import { PriorityQueue } from "@datastructures-js/priority-queue";
import { AStar} from "./AStar";
import { AStar } from "./AStar";
import { PathFindResultType } from "./AStar";
import { Cell, TerrainTile, TerrainTileKey } from "../game/Game";
import { Cell } from "../game/Game";
import { consolex } from "../Consolex";
import { GameMap, TileRef } from "../game/GameMap";
export class SerialAStar implements AStar {
private fwdOpenSet: PriorityQueue<{ tile: TerrainTile; fScore: number; }>;
private bwdOpenSet: PriorityQueue<{ tile: TerrainTile; fScore: number; }>;
private fwdCameFrom: Map<TerrainTileKey, TerrainTile>;
private bwdCameFrom: Map<TerrainTileKey, TerrainTile>;
private fwdGScore: Map<TerrainTileKey, number>;
private bwdGScore: Map<TerrainTileKey, number>;
private meetingPoint: TerrainTile | null;
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: TerrainTile,
private dst: TerrainTile,
private canMove: (t: TerrainTile) => boolean,
private src: TileRef,
private dst: TileRef,
private canMove: (t: TileRef) => boolean,
private iterations: number,
private maxTries: number
private maxTries: number,
private gameMap: GameMap
) {
this.fwdOpenSet = new PriorityQueue<{ tile: TerrainTile; fScore: number; }>(
this.fwdOpenSet = new PriorityQueue<{ tile: TileRef; fScore: number; }>(
(a, b) => a.fScore - b.fScore
);
this.bwdOpenSet = new PriorityQueue<{ tile: TerrainTile; fScore: number; }>(
this.bwdOpenSet = new PriorityQueue<{ tile: TileRef; fScore: number; }>(
(a, b) => a.fScore - b.fScore
);
this.fwdCameFrom = new Map<TerrainTileKey, TerrainTile>();
this.bwdCameFrom = new Map<TerrainTileKey, TerrainTile>();
this.fwdGScore = new Map<TerrainTileKey, number>();
this.bwdGScore = new Map<TerrainTileKey, number>();
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.key(), 0);
this.fwdGScore.set(src, 0);
this.fwdOpenSet.enqueue({ tile: src, fScore: this.heuristic(src, dst) });
// Initialize backward search
this.bwdGScore.set(dst.key(), 0);
this.bwdGScore.set(dst, 0);
this.bwdOpenSet.enqueue({ tile: dst, fScore: this.heuristic(dst, src) });
}
@@ -61,43 +63,43 @@ export class SerialAStar implements AStar {
// Process forward search
const fwdCurrent = this.fwdOpenSet.dequeue()!.tile;
if (this.bwdGScore.has(fwdCurrent.key())) {
if (this.bwdGScore.has(fwdCurrent)) {
// We found a meeting point!
this.meetingPoint = fwdCurrent;
this.completed = true;
return PathFindResultType.Completed;
}
this.expandTerrainTile(fwdCurrent, true);
this.expandTileRef(fwdCurrent, true);
// Process backward search
const bwdCurrent = this.bwdOpenSet.dequeue()!.tile;
if (this.fwdGScore.has(bwdCurrent.key())) {
if (this.fwdGScore.has(bwdCurrent)) {
// We found a meeting point!
this.meetingPoint = bwdCurrent;
this.completed = true;
return PathFindResultType.Completed;
}
this.expandTerrainTile(bwdCurrent, false);
this.expandTileRef(bwdCurrent, false);
}
return this.completed ? PathFindResultType.Completed : PathFindResultType.PathNotFound;
}
private expandTerrainTile(current: TerrainTile, isForward: boolean) {
for (const neighbor of current.neighbors()) {
if (!neighbor.equals(isForward ? this.dst : this.src) && !this.canMove(neighbor)) continue;
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.key())! + neighbor.cost();
let tentativeGScore = gScore.get(current)! + this.gameMap.cost(neighbor);
if (!gScore.has(neighbor.key()) || tentativeGScore < gScore.get(neighbor.key())!) {
cameFrom.set(neighbor.key(), current);
gScore.set(neighbor.key(), tentativeGScore);
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
@@ -107,10 +109,10 @@ export class SerialAStar implements AStar {
}
}
private heuristic(a: TerrainTile, b: TerrainTile): number {
private heuristic(a: TileRef, b: TileRef): number {
// TODO use wrapped
try {
return 1.1 * Math.abs(a.cell().x - b.cell().x) + Math.abs(a.cell().y - b.cell().y);
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')
}
@@ -120,20 +122,20 @@ export class SerialAStar implements AStar {
if (!this.meetingPoint) return [];
// Reconstruct path from start to meeting point
const fwdPath: TerrainTile[] = [this.meetingPoint];
const fwdPath: TileRef[] = [this.meetingPoint];
let current = this.meetingPoint;
while (this.fwdCameFrom.has(current.key())) {
current = this.fwdCameFrom.get(current.key())!;
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.key())) {
current = this.bwdCameFrom.get(current.key())!;
while (this.bwdCameFrom.has(current)) {
current = this.bwdCameFrom.get(current)!;
fwdPath.push(current);
}
return fwdPath.map(sn => sn.cell());
return fwdPath.map(sn => new Cell(this.gameMap.x(sn), this.gameMap.y(sn)));
}
}