diff --git a/src/core/execution/FakeHumanExecution.ts b/src/core/execution/FakeHumanExecution.ts index 8c03a4d49..90d129b96 100644 --- a/src/core/execution/FakeHumanExecution.ts +++ b/src/core/execution/FakeHumanExecution.ts @@ -433,44 +433,44 @@ export class FakeHumanExecution implements Execution { private handleUnits() { const player = this.player; if (player === null) return; - const ports = player.units(UnitType.Port); - if (ports.length === 0 && player.gold() > this.cost(UnitType.Port)) { - const oceanTiles = Array.from(player.borderTiles()).filter((t) => - this.mg.isOceanShore(t), - ); - if (oceanTiles.length > 0) { - const buildTile = this.random.randElement(oceanTiles); - this.mg.addExecution( - new ConstructionExecution(player, buildTile, UnitType.Port), - ); - } - return; - } - this.maybeSpawnStructure(UnitType.City, 2); - if (this.maybeSpawnWarship()) { - return; - } - this.maybeSpawnStructure(UnitType.MissileSilo, 1); + return ( + this.maybeSpawnStructure(UnitType.Port, 1) || + this.maybeSpawnStructure(UnitType.City, 2) || + this.maybeSpawnWarship() || + this.maybeSpawnStructure(UnitType.MissileSilo, 1) + ); } - private maybeSpawnStructure(type: UnitType, maxNum: number) { + private maybeSpawnStructure(type: UnitType, maxNum: number): boolean { if (this.player === null) throw new Error("not initialized"); const units = this.player.units(type); if (units.length >= maxNum) { - return; + return false; } if (this.player.gold() < this.cost(type)) { - return; + return false; } - const tile = this.randTerritoryTile(this.player); + const tile = this.structureSpawnTile(type); if (tile === null) { - return; + return false; } const canBuild = this.player.canBuild(type, tile); if (canBuild === false) { - return; + return false; } this.mg.addExecution(new ConstructionExecution(this.player, tile, type)); + return true; + } + + private structureSpawnTile(type: UnitType): TileRef | null { + if (this.player === null) throw new Error("not initialized"); + const tiles = + type === UnitType.Port + ? Array.from(this.player.borderTiles()).filter((t) => + this.mg.isOceanShore(t), + ) + : Array.from(this.player.tiles()); + return this.random.randElement(tiles); } private maybeSpawnWarship(): boolean {