Merge pull request #10 from evanpelle/dev

Dev
This commit is contained in:
evanpelle
2024-12-03 20:21:36 -08:00
committed by GitHub
28 changed files with 184 additions and 142 deletions
-3
View File
@@ -1,3 +0,0 @@
[submodule "src/map/codec"]
path = src/map/codec
url = https://github.com/WarFrontIO/MapCodec
+1 -1
View File
@@ -1,6 +1,6 @@
MIT License
Copyright (c) 2024 WarFront.io Team
Copyright (c) 2024 OpenFront.io Team
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
+10 -2
View File
@@ -201,11 +201,19 @@
* have NPCs build destroyers and battleships DONE 11/30/2024
* add info view on top right DONE 11/30/2024
* add info view for units DONE 11/30/2024
* add defense post
* use mini A* for all pathfinding
* add defense post DONE 12/2/2024
* bugfix: when thread doesn't complete computation, do it in main DONE 12/3/2024
* bugfix: when trade ships captured don't render DONE 12/3/2024
* use mini A* for all pathfinding DONE 12/3/2024
* bugfix: gameStop not found error DONE 12/3/2024
* log stack traces & display them on screen DONE 12/3/2024
* record and replay games for debugging purposes
* bugfix: destroyers can't find path to dst and freeze
* record single player game stats
* add radiation from nuke
* add cities
* create behavior tests
* create perf test
* create alternate view to show friendly & enemy units
* spread out calculate clusters
* NPC has relations
+2 -2
View File
@@ -1,10 +1,10 @@
{
"name": "warfront-client",
"name": "openfront-client",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "warfront-client",
"name": "openfront-client",
"dependencies": {
"@datastructures-js/priority-queue": "^6.3.1",
"@types/dompurify": "^3.0.5",
+1 -1
View File
@@ -1,5 +1,5 @@
{
"name": "warfront-client",
"name": "openfront-client",
"scripts": {
"build-map": "node --loader ts-node/esm --experimental-specifier-resolution=node src/scripts/TerrainMapGenerator.ts",
"build-dev": "webpack --config webpack.config.js --mode development",
+7 -1
View File
@@ -173,7 +173,13 @@ export class GameRunner {
}
this.isProcessingTurn = true
this.gs.addExecution(...this.executor.createExecs(this.turns[this.currTurn]))
this.gs.executeNextTick()
try {
this.gs.executeNextTick()
} catch (error) {
const errorText = `Error: ${error.message}\nStack: ${error.stack}`;
alert("Game crashed! Error info copied to clipboard. Please paste this in your bug report in Discord.");
navigator.clipboard.writeText(errorText);
}
this.renderer.tick()
this.currTurn++
this.isProcessingTurn = false
+4 -4
View File
@@ -32,7 +32,9 @@ class Client {
const s = this.stopGame
window.addEventListener('beforeunload', function (event) {
console.log('Browser is closing');
s()
if (s != null) {
s()
}
});
setFavicon()
@@ -84,9 +86,7 @@ class Client {
}
private stopGame() {
if (this.gameStop != null) {
this.gameStop()
}
this?.gameStop?.()
}
private async handleLeaveLobby(event: CustomEvent) {
+9 -2
View File
@@ -84,7 +84,7 @@ export class TerritoryLayer implements Layer {
if (tile.defenseBonuses().filter(db => db.unit.owner() == owner).length > 0) {
this.paintCell(
tile.cell(),
colord({ r: 0, g: 0, b: 0 }),
this.theme.defendedBorderColor(owner.info()),
255
)
} else {
@@ -120,7 +120,14 @@ export class TerritoryLayer implements Layer {
unitEvent(event: UnitEvent) {
if (event.unit.type() == UnitType.DefensePost) {
bfs(event.unit.tile(), dist(event.unit.tile(), this.game.config().defensePostRange())).forEach(t => this.enqueue(t))
bfs(
event.unit.tile(),
dist(event.unit.tile(), this.game.config().defensePostRange())
).forEach(t => {
if (t.isBorder()) {
this.enqueue(t)
}
})
}
}
+1
View File
@@ -68,6 +68,7 @@ export interface Theme {
playerInfoColor(id: PlayerID): Colord;
territoryColor(playerInfo: PlayerInfo): Colord;
borderColor(playerInfo: PlayerInfo): Colord;
defendedBorderColor(playerInfo: PlayerInfo): Colord;
terrainColor(tile: Tile): Colord;
backgroundColor(): Colord;
font(): string;
+1 -1
View File
@@ -8,7 +8,7 @@ import { pastelTheme } from "./PastelTheme";
export class DefaultConfig implements Config {
defensePostRange(): number {
return 20
return 30
}
defensePostDefenseBonus(): number {
return 3
+6 -3
View File
@@ -5,7 +5,7 @@ export const devConfig = new class extends DefaultConfig {
unitInfo(type: UnitType): UnitInfo {
const info = super.unitInfo(type)
const oldCost = info.cost
info.cost = (p: Player) => oldCost(p) / 1000
info.cost = (p: Player) => oldCost(p) / 100000
return info
}
@@ -13,8 +13,8 @@ export const devConfig = new class extends DefaultConfig {
return 95
}
numSpawnPhaseTurns(): number {
// return 40
return 100
return 40
// return 100
}
gameCreationRate(): number {
return 10 * 1000
@@ -25,6 +25,9 @@ export const devConfig = new class extends DefaultConfig {
turnIntervalMs(): number {
return 100
}
tradeShipSpawnRate(): number {
return 10
}
// boatMaxDistance(): number {
// return 5000
// }
+9
View File
@@ -6,6 +6,7 @@ import {PseudoRandom} from "../PseudoRandom";
import {simpleHash} from "../Util";
export const pastelTheme = new class implements Theme {
private rand = new PseudoRandom(123)
private background = colord({r: 60, g: 60, b: 60});
@@ -131,6 +132,14 @@ export const pastelTheme = new class implements Theme {
b: Math.max(tc.b - 40, 0)
})
}
defendedBorderColor(playerInfo: PlayerInfo): Colord {
const bc = this.borderColor(playerInfo).rgba;
return colord({
r: Math.max(bc.r - 40, 0),
g: Math.max(bc.g - 40, 0),
b: Math.max(bc.b - 40, 0)
})
}
terrainColor(tile: Tile): Colord {
let mag = tile.magnitude()
+3 -2
View File
@@ -1,4 +1,4 @@
import { Cell, Execution, MutableGame, MutablePlayer, MutableUnit, PlayerID, Tile, UnitType } from "../game/Game";
import { Cell, Execution, MutableGame, MutablePlayer, MutableUnit, PlayerID, TerrainType, Tile, UnitType } from "../game/Game";
import { PathFinder } from "../pathfinding/PathFinding";
import { PathFindResultType } from "../pathfinding/AStar";
import { SerialAStar } from "../pathfinding/SerialAStar";
@@ -14,7 +14,7 @@ export class BattleshipExecution implements Execution {
private battleship: MutableUnit = null
private mg: MutableGame = null
private pathfinder = PathFinder.Serial(5000, t => t.isWater())
private pathfinder: PathFinder
private patrolTile: Tile;
private patrolCenterTile: Tile
@@ -31,6 +31,7 @@ export class BattleshipExecution implements Execution {
init(mg: MutableGame, ticks: number): void {
this.pathfinder = PathFinder.Mini(mg, 5000, t => t.terrainType() == TerrainType.Ocean)
this._owner = mg.player(this.playerID)
this.mg = mg
this.patrolCenterTile = mg.tile(this.cell)
+3 -2
View File
@@ -1,4 +1,4 @@
import { Cell, Execution, MutableGame, MutablePlayer, MutableUnit, PlayerID, Tile, UnitType } from "../game/Game";
import { Cell, Execution, MutableGame, MutablePlayer, MutableUnit, PlayerID, TerrainType, Tile, UnitType } from "../game/Game";
import { PathFinder } from "../pathfinding/PathFinding";
import { PathFindResultType } from "../pathfinding/AStar";
import { SerialAStar } from "../pathfinding/SerialAStar";
@@ -14,7 +14,7 @@ export class DestroyerExecution implements Execution {
private mg: MutableGame = null
private target: MutableUnit = null
private pathfinder = PathFinder.Serial(5000, t => t.isWater())
private pathfinder: PathFinder
private patrolTile: Tile;
private patrolCenterTile: Tile
@@ -29,6 +29,7 @@ export class DestroyerExecution implements Execution {
init(mg: MutableGame, ticks: number): void {
this.pathfinder = PathFinder.Mini(mg, 5000, t => t.terrainType() == TerrainType.Ocean)
this._owner = mg.player(this.playerID)
this.mg = mg
this.patrolCenterTile = mg.tile(this.cell)
+2 -1
View File
@@ -16,7 +16,7 @@ export class NukeExecution implements Execution {
private nuke: MutableUnit
private dst: Tile
private pathFinder: PathFinder = PathFinder.Serial(10_000, () => true)
private pathFinder: PathFinder
constructor(
private type: UnitType.AtomBomb | UnitType.HydrogenBomb,
private senderID: PlayerID,
@@ -26,6 +26,7 @@ export class NukeExecution implements Execution {
init(mg: MutableGame, ticks: number): void {
this.mg = mg
this.pathFinder = PathFinder.Mini(mg, 10_000, () => true)
this.player = mg.player(this.senderID)
this.dst = this.mg.tile(this.cell)
}
+4 -4
View File
@@ -1,4 +1,4 @@
import { AllPlayers, Cell, Execution, MutableGame, MutablePlayer, MutableUnit, Player, PlayerID, Tile, Unit, UnitType } from "../game/Game";
import { AllPlayers, Cell, Execution, MutableGame, MutablePlayer, MutableUnit, Player, PlayerID, TerrainType, Tile, Unit, UnitType } from "../game/Game";
import { PathFinder } from "../pathfinding/PathFinding";
import { PathFindResultType } from "../pathfinding/AStar";
import { SerialAStar } from "../pathfinding/SerialAStar";
@@ -72,7 +72,7 @@ export class PortExecution implements Execution {
const aStar = this.computingPaths.get(port)
switch (aStar.compute()) {
case PathFindResultType.Completed:
this.portPaths.set(port, aStar.reconstructPath().map(sn => sn as Tile))
this.portPaths.set(port, aStar.reconstructPath().map(cell => this.mg.tile(cell)))
this.computingPaths.delete(port)
break
case PathFindResultType.Pending:
@@ -83,7 +83,7 @@ export class PortExecution implements Execution {
}
continue
}
const asyncPF = this.worker.createParallelAStar(this.port.tile(), port.tile(), 100)
const asyncPF = this.worker.createParallelAStar(this.port.tile(), port.tile(), 25, [TerrainType.Ocean])
// console.log(`adding new port path from ${this.player().name()}:${this.port.tile().cell()} to ${port.owner().name()}:${port.tile().cell()}`)
this.computingPaths.set(port, asyncPF)
}
@@ -101,7 +101,7 @@ export class PortExecution implements Execution {
const port = this.random.randElement(portConnections)
const path = this.portPaths.get(port)
if (path != null) {
const pf = PathFinder.Parallel(this.worker, 30)
const pf = PathFinder.Parallel(this.mg, this.worker, 10)
this.mg.addExecution(new TradeShipExecution(this.player().id(), this.port, port, pf, path))
}
}
+2 -1
View File
@@ -5,7 +5,7 @@ import { PathFindResultType } from "../pathfinding/AStar";
export class ShellExecution implements Execution {
private active = true
private pathFinder = PathFinder.Serial(2000, () => true, 10)
private pathFinder: PathFinder
private shell: MutableUnit
constructor(private spawn: Tile, private _owner: MutablePlayer, private target: MutableUnit) {
@@ -13,6 +13,7 @@ export class ShellExecution implements Execution {
}
init(mg: MutableGame, ticks: number): void {
this.pathFinder = PathFinder.Mini(mg, 2000, () => true, 10)
}
tick(ticks: number): void {
+2
View File
@@ -79,6 +79,8 @@ export class TradeShipExecution implements Execution {
this.tradeShip.delete()
break
case PathFindResultType.Pending:
// Fire unit event to rerender.
this.tradeShip.move(this.tradeShip.tile())
break
case PathFindResultType.NextTile:
this.tradeShip.move(result.tile)
+3 -2
View File
@@ -1,4 +1,4 @@
import { Unit, Cell, Execution, MutableUnit, MutableGame, MutablePlayer, Player, PlayerID, TerraNullius, Tile, TileEvent, UnitType } from "../game/Game";
import { Unit, Cell, Execution, MutableUnit, MutableGame, MutablePlayer, Player, PlayerID, TerraNullius, Tile, TileEvent, UnitType, TerrainType } from "../game/Game";
import { and, bfs, manhattanDistWrapped, sourceDstOceanShore, targetTransportTile } from "../Util";
import { AttackExecution } from "./AttackExecution";
import { DisplayMessageEvent, MessageType } from "../../client/graphics/layers/EventsDisplay";
@@ -27,7 +27,7 @@ export class TransportShipExecution implements Execution {
private boat: MutableUnit
private pathFinder: PathFinder = PathFinder.Serial(10_000, t => t.isWater(), 2)
private pathFinder: PathFinder
constructor(
private attackerID: PlayerID,
@@ -43,6 +43,7 @@ export class TransportShipExecution implements Execution {
init(mg: MutableGame, ticks: number) {
this.lastMove = ticks
this.mg = mg
this.pathFinder = PathFinder.Mini(mg, 10_000, t => t.terrainType() == TerrainType.Ocean, 2)
this.attacker = mg.player(this.attackerID)
+1 -1
View File
@@ -435,7 +435,7 @@ export class PlayerImpl implements MutablePlayer {
}
hash(): number {
return simpleHash(this.id()) * (this.population() + this.numTilesOwned());
return simpleHash(this.id()) * (this.population() + this.numTilesOwned()) + this._units.reduce((acc, unit) => acc + unit.hash(), 0)
}
toString(): string {
return `Player:{name:${this.info().name},clientID:${this.info().clientID},isAlive:${this.isAlive()},troops:${this._troops},numTileOwned:${this.numTilesOwned()}}]`;
+4
View File
@@ -20,6 +20,10 @@ export class TileImpl implements Tile {
private readonly _terrain: TerrainTileImpl
) { }
terrainType(): TerrainType {
return this._terrain.type
}
defenseBonus(player: Player): number {
if (this.owner() == player) {
throw Error(`cannot get defense bonus of tile already owned by player`)
+6 -1
View File
@@ -1,3 +1,4 @@
import { simpleHash } from "../Util";
import { MutableUnit, Tile, TerraNullius, UnitType, Player, UnitInfo } from "./Game";
import { GameImpl } from "./GameImpl";
import { PlayerImpl } from "./PlayerImpl";
@@ -20,7 +21,7 @@ export class UnitImpl implements MutableUnit {
}
move(tile: Tile): void {
if(tile == null) {
if (tile == null) {
throw new Error("tile cannot be null")
}
const oldTile = this._tile;
@@ -57,4 +58,8 @@ export class UnitImpl implements MutableUnit {
isActive(): boolean {
return this._active;
}
hash(): number {
return this.tile().cell().x + this.tile().cell().y + simpleHash(this.type())
}
}
+3 -2
View File
@@ -1,8 +1,8 @@
import { Cell, Tile } from "../game/Game";
import { Cell, TerrainType, Tile } from "../game/Game";
export interface AStar {
compute(): PathFindResultType
reconstructPath(): SearchNode[]
reconstructPath(): Cell[]
}
export enum PathFindResultType {
@@ -26,6 +26,7 @@ export interface SearchNode {
cost(): number
cell(): Cell
neighbors(): SearchNode[]
terrainType(): TerrainType
}
export interface Point {
x: number;
+13 -20
View File
@@ -21,7 +21,7 @@ export class MiniAStar implements AStar {
this.aStar = new SerialAStar(
miniSrc,
miniDst,
(t => (t as TerrainTile).terrainType() == TerrainType.Ocean),
canMove,
iterations,
maxTries
)
@@ -31,29 +31,22 @@ export class MiniAStar implements AStar {
return this.aStar.compute()
}
reconstructPath(): SearchNode[] {
reconstructPath(): Cell[] {
const upscaled = upscalePath(this.aStar.reconstructPath())
.map(p => this.terrainMap.terrain(new Cell(p.x, p.y))) as SearchNode[]
upscaled.push(this.dst)
return upscaled
}
reconstructPathAsPoints(): Point[] {
const upscaled = upscalePath(this.aStar.reconstructPath())
upscaled.push({ x: this.dst.cell().x, y: this.dst.cell().y })
upscaled.push(this.dst.cell())
return upscaled
}
}
function upscalePath(path: SearchNode[], scaleFactor: number = 2): Point[] {
function upscalePath(path: Cell[], scaleFactor: number = 2): Cell[] {
// Scale up each point
const scaledPath = path.map(point => ({
x: point.cell().x * scaleFactor,
y: point.cell().y * scaleFactor
}));
const scaledPath = path.map(point => (new Cell(
point.x * scaleFactor,
point.y * scaleFactor
)));
const smoothPath: Point[] = [];
const smoothPath: Cell[] = [];
for (let i = 0; i < scaledPath.length - 1; i++) {
const current = scaledPath[i];
@@ -72,10 +65,10 @@ function upscalePath(path: SearchNode[], scaleFactor: number = 2): Point[] {
// Add intermediate points
for (let step = 1; step < steps; step++) {
smoothPath.push({
x: Math.round(current.x + (dx * step) / steps),
y: Math.round(current.y + (dy * step) / steps)
});
smoothPath.push(new Cell(
Math.round(current.x + (dx * step) / steps),
Math.round(current.y + (dy * step) / steps)
));
}
}
+16 -9
View File
@@ -1,6 +1,6 @@
import { Game, Tile } from "../game/Game";
import { Cell, Game, TerrainTile, TerrainType, Tile } from "../game/Game";
import { manhattanDist } from "../Util";
import { AStar, PathFindResultType, TileResult } from "./AStar";
import { AStar, PathFindResultType, SearchNode, TileResult } from "./AStar";
import { ParallelAStar, WorkerClient } from "../worker/WorkerClient";
import { SerialAStar } from "./SerialAStar";
import { MiniAStar } from "./MiniAStar";
@@ -9,17 +9,19 @@ export class PathFinder {
private curr: Tile = null
private dst: Tile = null
private path: Tile[]
private path: Cell[]
private aStar: AStar
private computeFinished = true
private constructor(
private game: Game,
private newAStar: (curr: Tile, dst: Tile) => AStar
) { }
public static Mini(game: Game, iterations: number, canMove: (t: Tile) => boolean, maxTries: number = 20) {
public static Mini(game: Game, iterations: number, canMove: (s: SearchNode) => boolean, maxTries: number = 20) {
return new PathFinder(
game,
(curr: Tile, dst: Tile) => {
return new MiniAStar(
game.terrainMap(),
@@ -34,8 +36,9 @@ export class PathFinder {
)
}
public static Serial(iterations: number, canMove: (t: Tile) => boolean, maxTries: number = 20): PathFinder {
public static Serial(game: Game, iterations: number, canMove: (t: Tile) => boolean, maxTries: number = 20): PathFinder {
return new PathFinder(
game,
(curr: Tile, dst: Tile) => {
return new SerialAStar(
curr,
@@ -48,10 +51,14 @@ export class PathFinder {
)
}
public static Parallel(worker: WorkerClient, numTicks: number): PathFinder {
public static Parallel(game: Game, worker: WorkerClient, numTicks: number, ...types: TerrainType[]): PathFinder {
if (types.length == 0) {
types = [TerrainType.Ocean]
}
return new PathFinder(
game,
(curr: Tile, dst: Tile) => {
return worker.createParallelAStar(curr, dst, numTicks)
return worker.createParallelAStar(curr, dst, numTicks, types)
}
)
}
@@ -77,14 +84,14 @@ export class PathFinder {
this.computeFinished = false
return this.nextTile(curr, dst)
} else {
return { type: PathFindResultType.NextTile, tile: this.path.shift() }
return { type: PathFindResultType.NextTile, tile: this.game.tile(this.path.shift()) }
}
}
switch (this.aStar.compute()) {
case PathFindResultType.Completed:
this.computeFinished = true
this.path = this.aStar.reconstructPath() as Tile[]
this.path = this.aStar.reconstructPath()
// Remove the start tile
this.path.shift()
return this.nextTile(curr, dst)
+3 -2
View File
@@ -1,6 +1,7 @@
import { PriorityQueue } from "@datastructures-js/priority-queue";
import { AStar, SearchNode } from "./AStar";
import { PathFindResultType } from "./AStar";
import { Cell } from "../game/Game";
export class SerialAStar implements AStar {
@@ -114,7 +115,7 @@ export class SerialAStar implements AStar {
}
}
public reconstructPath(): SearchNode[] {
public reconstructPath(): Cell[] {
if (!this.meetingPoint) return [];
// Reconstruct path from start to meeting point
@@ -132,6 +133,6 @@ export class SerialAStar implements AStar {
fwdPath.push(current);
}
return fwdPath;
return fwdPath.map(sn => sn.cell());
}
}
+26 -61
View File
@@ -3,23 +3,22 @@ import { Cell, GameMap, TerrainMap, TerrainTile, TerrainType } from "../game/Gam
import { createMiniMap, loadTerrainMap } from "../game/TerrainMapLoader";
import { PriorityQueue } from "@datastructures-js/priority-queue";
import { SerialAStar } from "../pathfinding/SerialAStar";
import { PathFindResultType, SearchNode } from "../pathfinding/AStar";
import { AStar, PathFindResultType, SearchNode } from "../pathfinding/AStar";
import { MiniAStar } from "../pathfinding/MiniAStar";
let terrainMapPromise: Promise<TerrainMap>;
let terrainMapPromise: Promise<{
terrainMap: TerrainMap,
miniMap: TerrainMap
}> | null = null;
let searches = new PriorityQueue<Search>((a: Search, b: Search) => (a.deadline - b.deadline))
let processingInterval: number | null = null;
let isProcessingSearch = false
interface Point {
x: number
y: number
}
interface Search {
aStar: SerialAStar,
aStar: AStar,
deadline: number
requestId: string,
end: Point
end: Cell
}
interface SearchRequest {
@@ -27,8 +26,8 @@ interface SearchRequest {
currentTick: number
// duration in ticks
duration: number
start: Point
end: Point
start: Cell
end: Cell
}
self.onmessage = (e) => {
@@ -37,22 +36,31 @@ self.onmessage = (e) => {
initializeMap(e.data);
break;
case 'findPath':
terrainMapPromise.then(tm => findPath(tm, e.data))
terrainMapPromise.then(tm => findPath(tm.terrainMap, tm.miniMap, e.data))
break;
}
};
function initializeMap(data: { gameMap: GameMap }) {
terrainMapPromise = loadTerrainMap(data.gameMap).then(tm => createMiniMap(tm))
terrainMapPromise = loadTerrainMap(data.gameMap)
.then(async terrainMap => {
const miniMap = await createMiniMap(terrainMap);
return {
terrainMap: terrainMap,
miniMap: miniMap
};
});
self.postMessage({ type: 'initialized' });
processingInterval = setInterval(computeSearches, .1) as unknown as number;
}
function findPath(terrainMap: TerrainMap, req: SearchRequest) {
function findPath(terrainMap: TerrainMap, miniTerrainMap: TerrainMap, req: SearchRequest) {
console.log(`terrain map height: ${terrainMap.height()}`)
const aStar = new SerialAStar(
terrainMap.terrain(new Cell(Math.floor(req.start.x / 2), Math.floor(req.start.y / 2))),
terrainMap.terrain(new Cell(Math.floor(req.end.x / 2), Math.floor(req.end.y / 2))),
const aStar = new MiniAStar(
terrainMap,
miniTerrainMap,
terrainMap.terrain(req.start),
terrainMap.terrain(req.end),
(sn: SearchNode) => (sn as TerrainTile).terrainType() == TerrainType.Ocean,
10_000,
req.duration,
@@ -81,12 +89,10 @@ function computeSearches() {
const search = searches.dequeue()
switch (search.aStar.compute()) {
case PathFindResultType.Completed:
const path = upscalePath(search.aStar.reconstructPath().map(sn => ({ x: sn.cell().x, y: sn.cell().y })))
path.push(search.end)
self.postMessage({
type: 'pathFound',
requestId: search.requestId,
path: path
path: search.aStar.reconstructPath()
});
break;
@@ -106,44 +112,3 @@ function computeSearches() {
isProcessingSearch = false
}
}
function upscalePath(path: Point[], scaleFactor: number = 2): Point[] {
// Scale up each point
const scaledPath = path.map(point => ({
x: point.x * scaleFactor,
y: point.y * scaleFactor
}));
const smoothPath: Point[] = [];
for (let i = 0; i < scaledPath.length - 1; i++) {
const current = scaledPath[i];
const next = scaledPath[i + 1];
// Add the current point
smoothPath.push(current);
// Always interpolate between scaled points
const dx = next.x - current.x;
const dy = next.y - current.y;
// Calculate number of steps needed
const distance = Math.max(Math.abs(dx), Math.abs(dy));
const steps = distance;
// Add intermediate points
for (let step = 1; step < steps; step++) {
smoothPath.push({
x: Math.round(current.x + (dx * step) / steps),
y: Math.round(current.y + (dy * step) / steps)
});
}
}
// Add the last point
if (scaledPath.length > 0) {
smoothPath.push(scaledPath[scaledPath.length - 1]);
}
return smoothPath;
}
+42 -14
View File
@@ -1,5 +1,6 @@
import { Cell, Game, GameMap, Tile } from "../game/Game";
import { Cell, Game, GameMap, TerrainTile, TerrainType, Tile } from "../game/Game";
import { AStar, PathFindResultType } from "../pathfinding/AStar";
import { MiniAStar } from "../pathfinding/MiniAStar";
export class WorkerClient {
@@ -34,19 +35,20 @@ export class WorkerClient {
});
}
createParallelAStar(src: Tile, dst: Tile, numTicks: number): ParallelAStar {
createParallelAStar(src: Tile, dst: Tile, numTicks: number, types: TerrainType[]): ParallelAStar {
if (!this.isInitialized) {
throw new Error('PathFinder not initialized');
}
return new ParallelAStar(this.game, this.worker, src, dst, numTicks);
return new ParallelAStar(this.game, this.worker, src, dst, numTicks, types);
}
cleanup() {
this.worker.terminate();
}
}
export class ParallelAStar implements AStar {
private path: Tile[] | 'NOT_FOUND' | null = null;
private path: Cell[] | 'NOT_FOUND' | null = null;
private promise: Promise<void>;
constructor(
@@ -54,25 +56,22 @@ export class ParallelAStar implements AStar {
private worker: Worker,
private src: Tile,
private dst: Tile,
private numTicks: number
private numTicks: number,
private terrainTypes: TerrainType[]
) { }
findPath(): Promise<void> {
const requestId = crypto.randomUUID();
this.promise = new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject("Path timeout");
}, 100000);
const handler = (e: MessageEvent) => {
if (e.data.requestId != requestId) {
return;
}
clearTimeout(timeout);
this.worker.removeEventListener('message', handler);
if (e.data.type === 'pathFound') {
this.path = e.data.path.map(pos => this.game.tile(new Cell(pos.x, pos.y)));
this.path = e.data.path
resolve();
} else if (e.data.type === 'pathNotFound') {
this.path = 'NOT_FOUND';
@@ -85,6 +84,7 @@ export class ParallelAStar implements AStar {
this.worker.postMessage({
type: 'findPath',
requestId: requestId,
terrainTypes: this.terrainTypes,
currentTick: this.game.ticks(),
duration: this.numTicks,
start: { x: this.src.cell().x, y: this.src.cell().y },
@@ -108,17 +108,45 @@ export class ParallelAStar implements AStar {
if (this.path != null) {
return PathFindResultType.Completed;
}
throw new Error(`path not completed in time`);
// Path was not found in worker thread in time, so now we need
// to recompute it in main thread. This will lock up game.
console.warn(`path not completed in worker thread, recomputing`)
const local = new MiniAStar(
this.game.terrainMap(),
this.game.terrainMiniMap(),
this.src, this.dst,
(t: TerrainTile) => t.terrainType() == TerrainType.Ocean,
100_000_000,
20
)
const result = local.compute()
switch (result) {
case PathFindResultType.Completed:
console.log('recomputed path in worker client')
this.path = local.reconstructPath()
break
case PathFindResultType.PathNotFound:
this.path = "NOT_FOUND"
break
case PathFindResultType.Pending:
// TODO: make sure same number of tries as worker thread.
console.warn("path not found after many tries")
this.path = "NOT_FOUND"
break
}
if (result == PathFindResultType.Completed) {
this.path = local.reconstructPath()
}
return result
}
return PathFindResultType.Pending;
}
reconstructPath(): Tile[] {
reconstructPath(): Cell[] {
if (this.path == "NOT_FOUND" || this.path == null) {
throw Error(`cannot reconstruct path: ${this.path}`);
}
return this.path as Tile[];
return this.path
}
}