From d40ec2f723e9f0705c6103a5e242a8f031c8f852 Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Sat, 7 Jun 2025 23:46:43 -0400 Subject: [PATCH] Nations can spawn cities without a port (#1072) ## Description: Allow nations to spawn cities even if they do not have a port. ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory - [ ] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced - [x] I understand that submitting code with bugs that could have been caught through manual testing blocks releases and new features for all contributors Co-authored-by: Scott Anderson <662325+scottanderson@users.noreply.github.com> Co-authored-by: evanpelle --- src/core/execution/FakeHumanExecution.ts | 48 ++++++++++++------------ 1 file changed, 24 insertions(+), 24 deletions(-) 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 {