Skip to content

Commit bf1a3a8

Browse files
committed
Build order improvements
1 parent 9512e48 commit bf1a3a8

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

src/core/execution/NationExecution.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
Difficulty,
33
Execution,
44
Game,
5+
GameMode,
56
Gold,
67
Nation,
78
Player,
@@ -252,17 +253,31 @@ export class NationExecution implements Execution {
252253
}
253254

254255
private handleUnits() {
256+
const hasCoastalTiles = this.hasCoastalTiles();
257+
const isTeamGame = this.mg.config().gameConfig().gameMode === GameMode.Team;
255258
return (
256259
this.maybeSpawnStructure(UnitType.City, (num) => num) ||
257260
this.maybeSpawnStructure(UnitType.Port, (num) => num) ||
258261
this.maybeSpawnWarship() ||
259-
this.maybeSpawnStructure(UnitType.Factory, (num) => num) ||
262+
this.maybeSpawnStructure(UnitType.Factory, (num) =>
263+
hasCoastalTiles ? num * 3 : num,
264+
) ||
260265
this.maybeSpawnStructure(UnitType.DefensePost, (num) => (num + 2) ** 2) ||
261-
this.maybeSpawnStructure(UnitType.SAMLauncher, (num) => num ** 2) ||
266+
this.maybeSpawnStructure(UnitType.SAMLauncher, (num) =>
267+
isTeamGame ? num : num ** 2,
268+
) ||
262269
this.maybeSpawnStructure(UnitType.MissileSilo, (num) => num ** 2)
263270
);
264271
}
265272

273+
private hasCoastalTiles(): boolean {
274+
if (this.player === null) return false;
275+
for (const tile of this.player.borderTiles()) {
276+
if (this.mg.isOceanShore(tile)) return true;
277+
}
278+
return false;
279+
}
280+
266281
private maybeSpawnStructure(
267282
type: UnitType,
268283
multiplier: (num: number) => number,
@@ -296,6 +311,7 @@ export class NationExecution implements Execution {
296311
: randTerritoryTileArray(this.random, this.mg, this.player, 25);
297312
if (tiles.length === 0) return null;
298313
const valueFunction = structureSpawnTileValue(this.mg, this.player, type);
314+
if (valueFunction === null) return null;
299315
let bestTile: TileRef | null = null;
300316
let bestValue = 0;
301317
for (const t of tiles) {

src/core/execution/nation/structureSpawnTileValue.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { Game, Player, Relation, UnitType } from "../../game/Game";
1+
import { Game, Player, PlayerType, Relation, UnitType } from "../../game/Game";
22
import { TileRef } from "../../game/GameMap";
33
import { closestTile, closestTwoTiles } from "../Util";
44

55
export function structureSpawnTileValue(
66
mg: Game,
77
player: Player,
88
type: UnitType,
9-
): (tile: TileRef) => number {
9+
): ((tile: TileRef) => number) | null {
1010
const borderTiles = player.borderTiles();
1111
const otherUnits = player.units(type);
1212
// Prefer spacing structures out of atom bomb range
@@ -57,6 +57,22 @@ export function structureSpawnTileValue(
5757
};
5858
}
5959
case UnitType.DefensePost: {
60+
// Check if we have any non-friendly non-bot neighbors
61+
const hasHostileNeighbor =
62+
player
63+
.neighbors()
64+
.filter(
65+
(n): n is Player =>
66+
n.isPlayer() &&
67+
player.isFriendly(n) === false &&
68+
n.type() !== PlayerType.Bot,
69+
).length > 0;
70+
71+
// Don't build defense posts if there is no danger
72+
if (!hasHostileNeighbor) {
73+
return null;
74+
}
75+
6076
return (tile) => {
6177
let w = 0;
6278

@@ -79,6 +95,7 @@ export function structureSpawnTileValue(
7995
if (id === player.smallID()) continue;
8096
const neighbor = mg.playerBySmallID(id);
8197
if (!neighbor.isPlayer()) continue;
98+
if (neighbor.type() === PlayerType.Bot) continue;
8299
neighbors.add(neighbor);
83100
}
84101
for (const neighbor of neighbors) {

0 commit comments

Comments
 (0)