mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-30 21:43:29 +00:00
create game runner
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import { getConfig } from "./configuration/Config";
|
||||
import { EventBus } from "./EventBus";
|
||||
import { Executor } from "./execution/ExecutionManager";
|
||||
import { Game } from "./game/Game";
|
||||
import { createGame } from "./game/GameImpl";
|
||||
import { loadTerrainMap } from "./game/TerrainMapLoader";
|
||||
import { GameConfig, Turn } from "./Schemas";
|
||||
|
||||
export interface GameUpdate {
|
||||
players: PlayerUpdate[]
|
||||
units: UnitUpdate[]
|
||||
|
||||
}
|
||||
|
||||
export interface PlayerUpdate {
|
||||
|
||||
}
|
||||
|
||||
export interface UnitUpdate {
|
||||
|
||||
}
|
||||
|
||||
export interface TileUpdate {
|
||||
x: number
|
||||
y: number
|
||||
isBorder: boolean
|
||||
|
||||
}
|
||||
|
||||
export async function createGameRunner(gameID: string, gameConfig: GameConfig): Promise<GameRunner> {
|
||||
const config = getConfig(gameConfig)
|
||||
const terrainMap = await loadTerrainMap(gameConfig.gameMap);
|
||||
const eventBus = new EventBus()
|
||||
const game = createGame(terrainMap.map, terrainMap.miniMap, eventBus, config)
|
||||
return new GameRunner(game, eventBus, new Executor(game, gameID))
|
||||
}
|
||||
|
||||
export class GameRunner {
|
||||
|
||||
constructor(private game: Game, private eventBus: EventBus, private execManager: Executor) {
|
||||
|
||||
}
|
||||
|
||||
public executeNextTick(turn: Turn): GameUpdate {
|
||||
|
||||
this.game.executeNextTick()
|
||||
return null
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { Cell, PlayerID } from "./game/Game";
|
||||
import { GameUpdate } from "./GameRunner";
|
||||
|
||||
export class TileView {
|
||||
|
||||
}
|
||||
|
||||
export class PlayerView {
|
||||
|
||||
}
|
||||
|
||||
export class GameView {
|
||||
|
||||
public update(gu: GameUpdate) {
|
||||
|
||||
}
|
||||
|
||||
tile(cell: Cell): TileView {
|
||||
return null
|
||||
}
|
||||
|
||||
player(id: PlayerID): PlayerView {
|
||||
return null
|
||||
}
|
||||
|
||||
}
|
||||
@@ -32,7 +32,7 @@ export class Executor {
|
||||
// private random = new PseudoRandom(999)
|
||||
private random: PseudoRandom = null
|
||||
|
||||
constructor(private gs: Game, private gameID: GameID, private workerClient: WorkerClient) {
|
||||
constructor(private gs: Game, private gameID: GameID) {
|
||||
// Add one to avoid id collisions with bots.
|
||||
this.random = new PseudoRandom(simpleHash(gameID) + 1)
|
||||
}
|
||||
@@ -94,7 +94,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), this.workerClient)
|
||||
return new PortExecution(intent.player, new Cell(intent.x, intent.y))
|
||||
case UnitType.MissileSilo:
|
||||
return new MissileSiloExecution(intent.player, new Cell(intent.x, intent.y))
|
||||
case UnitType.DefensePost:
|
||||
@@ -118,7 +118,6 @@ export class Executor {
|
||||
for (const nation of this.gs.nations()) {
|
||||
execs.push(new FakeHumanExecution(
|
||||
this.gameID,
|
||||
this.workerClient,
|
||||
new PlayerInfo(
|
||||
nation.name,
|
||||
PlayerType.FakeHuman,
|
||||
|
||||
@@ -33,7 +33,7 @@ export class FakeHumanExecution implements Execution {
|
||||
private lastEmojiSent = new Map<Player, Tick>()
|
||||
|
||||
|
||||
constructor(gameID: GameID, private worker: WorkerClient, private playerInfo: PlayerInfo, private cell: Cell, private strength: number) {
|
||||
constructor(gameID: GameID, private playerInfo: PlayerInfo, private cell: Cell, private strength: number) {
|
||||
this.random = new PseudoRandom(simpleHash(playerInfo.id) + simpleHash(gameID))
|
||||
}
|
||||
|
||||
@@ -265,7 +265,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.worker))
|
||||
this.mg.addExecution(new PortExecution(this.player.id(), buildTile.cell()))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import { bfs, dist, manhattanDist } from "../Util";
|
||||
import { TradeShipExecution } from "./TradeShipExecution";
|
||||
import { ParallelAStar, WorkerClient } from "../worker/WorkerClient";
|
||||
import { consolex } from "../Consolex";
|
||||
import { MiniAStar } from "../pathfinding/MiniAStar";
|
||||
|
||||
export class PortExecution implements Execution {
|
||||
|
||||
@@ -15,12 +16,11 @@ export class PortExecution implements Execution {
|
||||
private port: MutableUnit
|
||||
private random: PseudoRandom
|
||||
private portPaths = new Map<MutableUnit, Tile[]>()
|
||||
private computingPaths = new Map<MutableUnit, ParallelAStar>()
|
||||
private computingPaths = new Map<MutableUnit, MiniAStar>()
|
||||
|
||||
constructor(
|
||||
private _owner: PlayerID,
|
||||
private cell: Cell,
|
||||
private worker: WorkerClient
|
||||
) { }
|
||||
|
||||
|
||||
@@ -84,9 +84,16 @@ export class PortExecution implements Execution {
|
||||
}
|
||||
continue
|
||||
}
|
||||
const asyncPF = this.worker.createParallelAStar(this.port.tile(), port.tile(), 25, [TerrainType.Ocean])
|
||||
// consolex.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)
|
||||
|
||||
const pf = new MiniAStar(
|
||||
this.mg.terrainMap(),
|
||||
this.mg.terrainMiniMap(),
|
||||
this.port.tile(), port.tile(),
|
||||
sn => sn.terrainType() == TerrainType.Ocean,
|
||||
10_000,
|
||||
25
|
||||
)
|
||||
this.computingPaths.set(port, pf)
|
||||
}
|
||||
|
||||
for (const port of this.portPaths.keys()) {
|
||||
@@ -102,7 +109,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.mg, this.worker, 10)
|
||||
const pf = PathFinder.Mini(this.mg, 10, (sn) => sn.terrainType() == TerrainType.Ocean)
|
||||
this.mg.addExecution(new TradeShipExecution(this.player().id(), this.port, port, pf, path))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,18 +52,6 @@ export class 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, types)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
nextTile(curr: Tile, dst: Tile, dist: number = 1): TileResult {
|
||||
if (curr == null) {
|
||||
consolex.error('curr is null')
|
||||
|
||||
Reference in New Issue
Block a user