thread_split: convert all tile to tileref

This commit is contained in:
Evan
2025-01-17 20:13:26 -08:00
parent c42cc2a9b4
commit f0f5bae79f
53 changed files with 1104 additions and 1405 deletions
+4 -4
View File
@@ -1,8 +1,8 @@
import { Cell, TerrainType, Tile } from "../game/Game";
import { TileRef } from "../game/GameMap";
export interface AStar {
compute(): PathFindResultType
reconstructPath(): Cell[]
reconstructPath(): TileRef[]
}
export enum PathFindResultType {
@@ -12,12 +12,12 @@ export enum PathFindResultType {
PathNotFound
} export type TileResult = {
type: PathFindResultType.NextTile;
tile: Tile;
tile: TileRef;
} | {
type: PathFindResultType.Pending;
} | {
type: PathFindResultType.Completed;
tile: Tile;
tile: TileRef;
} | {
type: PathFindResultType.PathNotFound;
};
+7 -8
View File
@@ -1,6 +1,5 @@
import { GameManager } from "../../server/GameManager";
import { Cell, Game, TerrainMap, TerrainType } from "../game/Game";
import { GameMapImpl, TileRef } from "../game/GameMap";
import { Cell, } from "../game/Game";
import { GameMap, GameMapImpl, TileRef } from "../game/GameMap";
import { AStar, PathFindResultType, } from "./AStar";
import { SerialAStar } from "./SerialAStar";
@@ -10,8 +9,8 @@ export class MiniAStar implements AStar {
private aStar: SerialAStar
constructor(
private gameMap: GameMapImpl,
private miniMap: GameMapImpl,
private gameMap: GameMap,
private miniMap: GameMap,
private src: TileRef,
private dst: TileRef,
private canMove: (t: TileRef) => boolean,
@@ -40,10 +39,10 @@ export class MiniAStar implements AStar {
return this.aStar.compute()
}
reconstructPath(): Cell[] {
const upscaled = upscalePath(this.aStar.reconstructPath())
reconstructPath(): TileRef[] {
const upscaled = upscalePath(this.aStar.reconstructPath().map(tr => new Cell(this.gameMap.x(tr), this.gameMap.y(tr))))
upscaled.push(new Cell(this.gameMap.x(this.dst), this.gameMap.y(this.dst)))
return upscaled
return upscaled.map(c => this.gameMap.ref(c.x, c.y))
}
}
+14 -17
View File
@@ -1,5 +1,4 @@
import { Cell, Game, TerrainTile, TerrainType, Tile } from "../game/Game";
import { manhattanDist } from "../Util";
import { Cell, Game } from "../game/Game";
import { AStar, PathFindResultType, TileResult } from "./AStar";
import { SerialAStar } from "./SerialAStar";
import { MiniAStar } from "./MiniAStar";
@@ -8,29 +7,27 @@ import { TileRef } from "../game/GameMap";
export class PathFinder {
private curr: Tile = null
private dst: Tile = null
private path: Cell[]
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: Tile, dst: Tile) => AStar
private newAStar: (curr: TileRef, dst: TileRef) => AStar
) { }
public static Mini(game: Game, iterations: number, canMoveOnLand: boolean, maxTries: number = 20) {
return new PathFinder(
game,
(curr: Tile, dst: Tile) => {
const currRef = game.map().ref(curr.cell().x, curr.cell().y)
const dstRef = game.map().ref(dst.cell().x, dst.cell().y)
(curr: TileRef, dst: TileRef) => {
return new MiniAStar(
game.map(),
game.miniMap(),
currRef,
dstRef,
curr,
dst,
(tr: TileRef): boolean => {
if (canMoveOnLand) {
return true
@@ -44,7 +41,7 @@ export class PathFinder {
)
}
nextTile(curr: Tile, dst: Tile, dist: number = 1): TileResult {
nextTile(curr: TileRef, dst: TileRef, dist: number = 1): TileResult {
if (curr == null) {
consolex.error('curr is null')
}
@@ -52,7 +49,7 @@ export class PathFinder {
consolex.error('dst is null')
}
if (manhattanDist(curr.cell(), dst.cell()) < dist) {
if (this.game.manhattanDist(curr, dst) < dist) {
return { type: PathFindResultType.Completed, tile: curr }
}
@@ -65,7 +62,7 @@ export class PathFinder {
this.computeFinished = false
return this.nextTile(curr, dst)
} else {
return { type: PathFindResultType.NextTile, tile: this.game.tile(this.path.shift()) }
return { type: PathFindResultType.NextTile, tile: this.path.shift() }
}
}
@@ -83,11 +80,11 @@ export class PathFinder {
}
}
private shouldRecompute(curr: Tile, dst: Tile) {
private shouldRecompute(curr: TileRef, dst: TileRef) {
if (this.path == null || this.curr == null || this.dst == null) {
return true
}
const dist = manhattanDist(curr.cell(), dst.cell())
const dist = this.game.manhattanDist(curr, dst)
let tolerance = 10
if (dist > 50) {
tolerance = 10
@@ -98,7 +95,7 @@ export class PathFinder {
} else {
tolerance = 0
}
if (manhattanDist(this.dst.cell(), dst.cell()) > tolerance) {
if (this.game.manhattanDist(this.dst, dst) > tolerance) {
return true
}
return false
+4 -5
View File
@@ -1,9 +1,8 @@
import { PriorityQueue } from "@datastructures-js/priority-queue";
import { AStar } from "./AStar";
import { PathFindResultType } from "./AStar";
import { Cell } from "../game/Game";
import { consolex } from "../Consolex";
import { GameMapImpl, TileRef } from "../game/GameMap";
import { GameMap, GameMapImpl, TileRef } from "../game/GameMap";
export class SerialAStar implements AStar {
@@ -22,7 +21,7 @@ export class SerialAStar implements AStar {
private canMove: (t: TileRef) => boolean,
private iterations: number,
private maxTries: number,
private gameMap: GameMapImpl
private gameMap: GameMap
) {
this.fwdOpenSet = new PriorityQueue<{ tile: TileRef; fScore: number; }>(
(a, b) => a.fScore - b.fScore
@@ -118,7 +117,7 @@ export class SerialAStar implements AStar {
}
}
public reconstructPath(): Cell[] {
public reconstructPath(): TileRef[] {
if (!this.meetingPoint) return [];
// Reconstruct path from start to meeting point
@@ -136,6 +135,6 @@ export class SerialAStar implements AStar {
fwdPath.push(current);
}
return fwdPath.map(sn => new Cell(this.gameMap.x(sn), this.gameMap.y(sn)));
return fwdPath
}
}