mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-07 12:26:00 +00:00
thread_split: convert all tile to tileref
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user