created path finding web worker

This commit is contained in:
Evan
2024-11-28 12:25:34 -08:00
parent 2216c34c41
commit 3e4f4e42cf
24 changed files with 643 additions and 306 deletions
+2 -1
View File
@@ -1,5 +1,6 @@
import { Cell, Execution, MutableGame, MutablePlayer, MutableUnit, PlayerID, Tile, UnitType } from "../game/Game";
import { AStar, PathFinder, PathFindResultType } from "../PathFinding";
import { PathFinder, PathFindResultType } from "../pathfinding/PathFinding";
import { AStar } from "../pathfinding/AStar";
import { PseudoRandom } from "../PseudoRandom";
import { distSort, distSortUnit, manhattanDist } from "../Util";
import { ShellExecution } from "./ShellExecution";
+2 -1
View File
@@ -1,5 +1,6 @@
import { Cell, Execution, MutableGame, MutablePlayer, MutableUnit, PlayerID, Tile, UnitType } from "../game/Game";
import { AStar, PathFinder, PathFindResultType } from "../PathFinding";
import { PathFinder, PathFindResultType } from "../pathfinding/PathFinding";
import { AStar } from "../pathfinding/AStar";
import { PseudoRandom } from "../PseudoRandom";
import { distSort, distSortUnit, manhattanDist } from "../Util";
+4 -2
View File
@@ -20,6 +20,7 @@ import { DestroyerExecution } from "./DestroyerExecution";
import { PortExecution } from "./PortExecution";
import { MissileSiloExecution } from "./MissileSiloExecution";
import { BattleshipExecution } from "./BattleshipExecution";
import { AsyncPathFinderCreator } from "../pathfinding/AsyncPathFinding";
@@ -30,7 +31,7 @@ export class Executor {
// private random = new PseudoRandom(999)
private random: PseudoRandom = null
constructor(private gs: Game, private difficulty: Difficulty, private gameID: GameID) {
constructor(private gs: Game, private difficulty: Difficulty, private gameID: GameID, private asyncPathFinder: AsyncPathFinderCreator) {
// Add one to avoid id collisions with bots.
this.random = new PseudoRandom(simpleHash(gameID) + 1)
}
@@ -92,7 +93,7 @@ export class Executor {
case UnitType.Battleship:
return new BattleshipExecution(intent.player, new Cell(intent.x, intent.y))
case UnitType.Port:
return new PortExecution(intent.player, new Cell(intent.x, intent.y))
return new PortExecution(intent.player, new Cell(intent.x, intent.y), this.asyncPathFinder)
case UnitType.MissileSilo:
return new MissileSiloExecution(intent.player, new Cell(intent.x, intent.y))
default:
@@ -111,6 +112,7 @@ export class Executor {
const execs = []
for (const nation of this.gs.nations()) {
execs.push(new FakeHumanExecution(
this.asyncPathFinder,
new PlayerInfo(
nation.name,
PlayerType.FakeHuman,
+3 -2
View File
@@ -5,6 +5,7 @@ import { AttackExecution } from "./AttackExecution";
import { TransportShipExecution } from "./TransportShipExecution";
import { SpawnExecution } from "./SpawnExecution";
import { PortExecution } from "./PortExecution";
import { AsyncPathFinder, AsyncPathFinderCreator } from "../pathfinding/AsyncPathFinding";
export class FakeHumanExecution implements Execution {
@@ -21,7 +22,7 @@ export class FakeHumanExecution implements Execution {
private relations = new Map<Player, number>()
constructor(private playerInfo: PlayerInfo, private cell: Cell, private strength: number) {
constructor(private asyncPathFinder: AsyncPathFinderCreator, private playerInfo: PlayerInfo, private cell: Cell, private strength: number) {
this.random = new PseudoRandom(simpleHash(playerInfo.id))
}
@@ -151,7 +152,7 @@ export class FakeHumanExecution implements Execution {
const oceanTiles = Array.from(this.player.borderTiles()).filter(t => t.isOceanShore())
if (oceanTiles.length > 0) {
const buildTile = this.random.randElement(oceanTiles)
this.mg.addExecution(new PortExecution(this.player.id(), buildTile.cell()))
this.mg.addExecution(new PortExecution(this.player.id(), buildTile.cell(), this.asyncPathFinder))
}
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
import { nextTick } from "process";
import { Cell, Execution, MutableGame, MutablePlayer, PlayerID, Tile, MutableUnit, UnitType } from "../game/Game";
import { PathFinder, PathFindResultType } from "../PathFinding";
import { PathFinder, PathFindResultType } from "../pathfinding/PathFinding";
import { PseudoRandom } from "../PseudoRandom";
import { bfs, dist, distSortUnit, euclideanDist, manhattanDist } from "../Util";
+10 -5
View File
@@ -1,8 +1,10 @@
import { AllPlayers, Cell, Execution, MutableGame, MutablePlayer, MutableUnit, Player, PlayerID, Tile, Unit, UnitType } from "../game/Game";
import { AStar, PathFinder, PathFindResultType } from "../PathFinding";
import { PathFinder, PathFindResultType } from "../pathfinding/PathFinding";
import { AStar } from "../pathfinding/AStar";
import { PseudoRandom } from "../PseudoRandom";
import { bfs, dist, manhattanDist } from "../Util";
import { TradeShipExecution } from "./TradeShipExecution";
import { AsyncPathFinder, AsyncPathFinderCreator } from "../pathfinding/AsyncPathFinding";
export class PortExecution implements Execution {
@@ -11,11 +13,12 @@ export class PortExecution implements Execution {
private port: MutableUnit
private random: PseudoRandom
private portPaths = new Map<MutableUnit, Tile[]>()
private computingPaths = new Map<MutableUnit, AStar>()
private computingPaths = new Map<MutableUnit, AsyncPathFinder>()
constructor(
private _owner: PlayerID,
private cell: Cell
private cell: Cell,
private asyncPathFinderCreator: AsyncPathFinderCreator
) { }
@@ -25,6 +28,7 @@ export class PortExecution implements Execution {
}
tick(ticks: number): void {
if (this.port == null) {
const tile = this.mg.tile(this.cell)
const player = this.mg.player(this._owner)
@@ -62,7 +66,7 @@ export class PortExecution implements Execution {
const aStar = this.computingPaths.get(port)
switch (aStar.compute()) {
case PathFindResultType.Completed:
this.portPaths.set(port, aStar.reconstructPath())
this.portPaths.set(port, aStar.reconstructPath().map(sn => sn as Tile))
this.computingPaths.delete(port)
break
case PathFindResultType.Pending:
@@ -73,8 +77,9 @@ export class PortExecution implements Execution {
}
continue
}
const asyncPF = this.asyncPathFinderCreator.createPathFinder(this.port.tile(), port.tile(), 100)
// 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, new AStar(this.port.tile(), port.tile(), t => t.isWater(), 4000, 100))
this.computingPaths.set(port, asyncPF)
}
for (const port of this.portPaths.keys()) {
+1 -1
View File
@@ -1,5 +1,5 @@
import { Execution, MutableGame, MutablePlayer, MutableUnit, Tile, Unit, UnitType } from "../game/Game";
import { PathFinder, PathFindResultType } from "../PathFinding";
import { PathFinder, PathFindResultType } from "../pathfinding/PathFinding";
export class ShellExecution implements Execution {
+2 -1
View File
@@ -1,7 +1,8 @@
import { MessageType } from "../../client/graphics/layers/EventsDisplay";
import { renderNumber } from "../../client/graphics/Utils";
import { AllPlayers, Cell, Execution, MutableGame, MutablePlayer, MutableUnit, Player, PlayerID, Tile, Unit, UnitType } from "../game/Game";
import { AStar, PathFinder, PathFindResultType } from "../PathFinding";
import { PathFinder, PathFindResultType } from "../pathfinding/PathFinding";
import { AStar } from "../pathfinding/AStar";
import { PseudoRandom } from "../PseudoRandom";
import { bfs, dist, distSortUnit, manhattanDist } from "../Util";
+2 -1
View File
@@ -2,7 +2,8 @@ import { Unit, Cell, Execution, MutableUnit, MutableGame, MutablePlayer, Player,
import { and, bfs, manhattanDistWrapped, sourceDstOceanShore, targetTransportTile } from "../Util";
import { AttackExecution } from "./AttackExecution";
import { DisplayMessageEvent, MessageType } from "../../client/graphics/layers/EventsDisplay";
import { AStar, PathFinder, PathFindResultType } from "../PathFinding";
import { PathFinder, PathFindResultType } from "../pathfinding/PathFinding";
import { AStar } from "../pathfinding/AStar";
export class TransportShipExecution implements Execution {