bugfix: Path.Mini was modified to only pathfinding on water, which broke nukes. Created AirPathfinder class to handle pathfinding for nukes

This commit is contained in:
evan
2025-04-23 13:04:33 -07:00
parent 13e2a02b2d
commit b13029591d
4 changed files with 69 additions and 83 deletions
+38 -1
View File
@@ -1,9 +1,46 @@
import { consolex } from "../Consolex";
import { Game } from "../game/Game";
import { TileRef } from "../game/GameMap";
import { GameMap, TileRef } from "../game/GameMap";
import { PseudoRandom } from "../PseudoRandom";
import { AStar, PathFindResultType, TileResult } from "./AStar";
import { MiniAStar } from "./MiniAStar";
export class AirPathFinder {
constructor(
private mg: GameMap,
private random: PseudoRandom,
) {}
nextTile(tile: TileRef, dst: TileRef): TileRef | true {
const x = this.mg.x(tile);
const y = this.mg.y(tile);
const dstX = this.mg.x(dst);
const dstY = this.mg.y(dst);
if (x === dstX && y === dstY) {
return true;
}
// Calculate next position
let nextX = x;
let nextY = y;
const ratio = Math.floor(1 + Math.abs(dstY - y) / (Math.abs(dstX - x) + 1));
if (this.random.chance(ratio) && x != dstX) {
if (x < dstX) nextX++;
else if (x > dstX) nextX--;
} else {
if (y < dstY) nextY++;
else if (y > dstY) nextY--;
}
if (nextX == x && nextY == y) {
return true;
}
return this.mg.ref(nextX, nextY);
}
}
export class PathFinder {
private curr: TileRef = null;
private dst: TileRef = null;