use terrain tile for search

This commit is contained in:
evanpelle
2025-01-13 17:00:15 -08:00
committed by Evan
parent bf52754fb7
commit f0d9477e56
8 changed files with 67 additions and 58 deletions
-6
View File
@@ -22,12 +22,6 @@ export enum PathFindResultType {
type: PathFindResultType.PathNotFound;
};
export interface SearchNode {
cost(): number
cell(): Cell
neighbors(): SearchNode[]
type(): TerrainType
}
export interface Point {
x: number;
y: number;
+4 -4
View File
@@ -1,5 +1,5 @@
import { Cell, Game, TerrainMap, TerrainTile, TerrainType } from "../game/Game";
import { AStar, PathFindResultType, Point, SearchNode } from "./AStar";
import { AStar, PathFindResultType, } from "./AStar";
import { SerialAStar } from "./SerialAStar";
// TODO: test this, get it work
@@ -10,9 +10,9 @@ export class MiniAStar implements AStar {
constructor(
private terrainMap: TerrainMap,
private miniMap: TerrainMap,
private src: SearchNode,
private dst: SearchNode,
private canMove: (t: SearchNode) => boolean,
private src: TerrainTile,
private dst: TerrainTile,
private canMove: (t: TerrainTile) => boolean,
private iterations: number,
private maxTries: number
) {
+7 -7
View File
@@ -1,6 +1,6 @@
import { Cell, Game, TerrainTile, TerrainType, Tile } from "../game/Game";
import { manhattanDist } from "../Util";
import { AStar, PathFindResultType, SearchNode, TileResult } from "./AStar";
import { AStar, PathFindResultType, TileResult } from "./AStar";
import { SerialAStar } from "./SerialAStar";
import { MiniAStar } from "./MiniAStar";
import { consolex } from "../Consolex";
@@ -19,15 +19,15 @@ export class PathFinder {
) { }
public static Mini(game: Game, iterations: number, canMove: (s: SearchNode) => boolean, maxTries: number = 20) {
public static Mini(game: Game, iterations: number, canMove: (s: TerrainTile) => boolean, maxTries: number = 20) {
return new PathFinder(
game,
(curr: Tile, dst: Tile) => {
return new MiniAStar(
game.terrainMap(),
game.terrainMiniMap(),
curr,
dst,
curr.terrain(),
dst.terrain(),
canMove,
iterations,
maxTries
@@ -36,13 +36,13 @@ export class PathFinder {
)
}
public static Serial(game: Game, iterations: number, canMove: (t: Tile) => boolean, maxTries: number = 20): PathFinder {
public static Serial(game: Game, iterations: number, canMove: (t: TerrainTile) => boolean, maxTries: number = 20): PathFinder {
return new PathFinder(
game,
(curr: Tile, dst: Tile) => {
return new SerialAStar(
curr,
dst,
curr.terrain(),
dst.terrain(),
canMove,
iterations,
maxTries
+36 -36
View File
@@ -1,46 +1,46 @@
import { PriorityQueue } from "@datastructures-js/priority-queue";
import { AStar, SearchNode } from "./AStar";
import { AStar} from "./AStar";
import { PathFindResultType } from "./AStar";
import { Cell } from "../game/Game";
import { Cell, TerrainTile, TerrainTileKey } from "../game/Game";
import { consolex } from "../Consolex";
export class SerialAStar implements AStar {
private fwdOpenSet: PriorityQueue<{ tile: SearchNode; fScore: number; }>;
private bwdOpenSet: PriorityQueue<{ tile: SearchNode; fScore: number; }>;
private fwdCameFrom: Map<SearchNode, SearchNode>;
private bwdCameFrom: Map<SearchNode, SearchNode>;
private fwdGScore: Map<SearchNode, number>;
private bwdGScore: Map<SearchNode, number>;
private meetingPoint: SearchNode | null;
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;
public completed: boolean;
constructor(
private src: SearchNode,
private dst: SearchNode,
private canMove: (t: SearchNode) => boolean,
private src: TerrainTile,
private dst: TerrainTile,
private canMove: (t: TerrainTile) => boolean,
private iterations: number,
private maxTries: number
) {
this.fwdOpenSet = new PriorityQueue<{ tile: SearchNode; fScore: number; }>(
this.fwdOpenSet = new PriorityQueue<{ tile: TerrainTile; fScore: number; }>(
(a, b) => a.fScore - b.fScore
);
this.bwdOpenSet = new PriorityQueue<{ tile: SearchNode; fScore: number; }>(
this.bwdOpenSet = new PriorityQueue<{ tile: TerrainTile; fScore: number; }>(
(a, b) => a.fScore - b.fScore
);
this.fwdCameFrom = new Map<SearchNode, SearchNode>();
this.bwdCameFrom = new Map<SearchNode, SearchNode>();
this.fwdGScore = new Map<SearchNode, number>();
this.bwdGScore = new Map<SearchNode, number>();
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.meetingPoint = null;
this.completed = false;
// Initialize forward search
this.fwdGScore.set(src, 0);
this.fwdGScore.set(src.key(), 0);
this.fwdOpenSet.enqueue({ tile: src, fScore: this.heuristic(src, dst) });
// Initialize backward search
this.bwdGScore.set(dst, 0);
this.bwdGScore.set(dst.key(), 0);
this.bwdOpenSet.enqueue({ tile: dst, fScore: this.heuristic(dst, src) });
}
@@ -61,43 +61,43 @@ export class SerialAStar implements AStar {
// Process forward search
const fwdCurrent = this.fwdOpenSet.dequeue()!.tile;
if (this.bwdGScore.has(fwdCurrent)) {
if (this.bwdGScore.has(fwdCurrent.key())) {
// We found a meeting point!
this.meetingPoint = fwdCurrent;
this.completed = true;
return PathFindResultType.Completed;
}
this.expandSearchNode(fwdCurrent, true);
this.expandTerrainTile(fwdCurrent, true);
// Process backward search
const bwdCurrent = this.bwdOpenSet.dequeue()!.tile;
if (this.fwdGScore.has(bwdCurrent)) {
if (this.fwdGScore.has(bwdCurrent.key())) {
// We found a meeting point!
this.meetingPoint = bwdCurrent;
this.completed = true;
return PathFindResultType.Completed;
}
this.expandSearchNode(bwdCurrent, false);
this.expandTerrainTile(bwdCurrent, false);
}
return this.completed ? PathFindResultType.Completed : PathFindResultType.PathNotFound;
}
private expandSearchNode(current: SearchNode, isForward: boolean) {
private expandTerrainTile(current: TerrainTile, isForward: boolean) {
for (const neighbor of current.neighbors()) {
if (neighbor !== (isForward ? this.dst : this.src) && !this.canMove(neighbor)) continue;
if (!neighbor.equals(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)! + neighbor.cost();
let tentativeGScore = gScore.get(current.key())! + neighbor.cost();
if (!gScore.has(neighbor) || tentativeGScore < gScore.get(neighbor)!) {
cameFrom.set(neighbor, current);
gScore.set(neighbor, tentativeGScore);
if (!gScore.has(neighbor.key()) || tentativeGScore < gScore.get(neighbor.key())!) {
cameFrom.set(neighbor.key(), current);
gScore.set(neighbor.key(), tentativeGScore);
const fScore = tentativeGScore + this.heuristic(
neighbor,
isForward ? this.dst : this.src
@@ -107,7 +107,7 @@ export class SerialAStar implements AStar {
}
}
private heuristic(a: SearchNode, b: SearchNode): number {
private heuristic(a: TerrainTile, b: TerrainTile): number {
// TODO use wrapped
try {
return 1.1 * Math.abs(a.cell().x - b.cell().x) + Math.abs(a.cell().y - b.cell().y);
@@ -120,17 +120,17 @@ export class SerialAStar implements AStar {
if (!this.meetingPoint) return [];
// Reconstruct path from start to meeting point
const fwdPath: SearchNode[] = [this.meetingPoint];
const fwdPath: TerrainTile[] = [this.meetingPoint];
let current = this.meetingPoint;
while (this.fwdCameFrom.has(current)) {
current = this.fwdCameFrom.get(current)!;
while (this.fwdCameFrom.has(current.key())) {
current = this.fwdCameFrom.get(current.key())!;
fwdPath.unshift(current);
}
// Reconstruct path from meeting point to goal
current = this.meetingPoint;
while (this.bwdCameFrom.has(current)) {
current = this.bwdCameFrom.get(current)!;
while (this.bwdCameFrom.has(current.key())) {
current = this.bwdCameFrom.get(current.key())!;
fwdPath.push(current);
}