Automatic train stations (#1353)

## Description:

Train stations are now built automatically when a factory is
constructed.

Changes:
- When a factory is built, nearby structures are connected to the rail
network
- When a city is built near a factory, it is connected to the rail
network
    - All structures behave the same when a train stops: to be defined
    - Removed station badge
    - Gold income is now related to the structure's level

## 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
- [x] 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

## Please put your Discord username so you can be contacted if a bug or
regression is found:

IngloriousTom
This commit is contained in:
DevelopingTom
2025-07-06 22:15:49 +02:00
committed by GitHub
parent 3108921637
commit 6353a5d6f7
23 changed files with 105 additions and 185 deletions
+16
View File
@@ -1,5 +1,6 @@
import { Execution, Game, Player, Unit, UnitType } from "../game/Game";
import { TileRef } from "../game/GameMap";
import { TrainStationExecution } from "./TrainStationExecution";
export class CityExecution implements Execution {
private mg: Game;
@@ -24,6 +25,7 @@ export class CityExecution implements Execution {
return;
}
this.city = this.player.buildUnit(UnitType.City, spawnTile, {});
this.createStation();
}
if (!this.city.isActive()) {
this.active = false;
@@ -42,4 +44,18 @@ export class CityExecution implements Execution {
activeDuringSpawnPhase(): boolean {
return false;
}
createStation(): void {
if (this.city !== null) {
const nearbyFactory = this.mg.hasUnitNearby(
this.city.tile()!,
this.mg.config().trainStationMaxRange(),
UnitType.Factory,
this.player.id(),
);
if (nearbyFactory) {
this.mg.addExecution(new TrainStationExecution(this.city));
}
}
}
}
-3
View File
@@ -23,7 +23,6 @@ import { RetreatExecution } from "./RetreatExecution";
import { SetTargetTroopRatioExecution } from "./SetTargetTroopRatioExecution";
import { SpawnExecution } from "./SpawnExecution";
import { TargetPlayerExecution } from "./TargetPlayerExecution";
import { TrainStationExecution } from "./TrainStationExecution";
import { TransportShipExecution } from "./TransportShipExecution";
import { UpgradeStructureExecution } from "./UpgradeStructureExecution";
@@ -118,8 +117,6 @@ export class Executor {
case "upgrade_structure":
return new UpgradeStructureExecution(player, intent.unitId);
case "create_station":
return new TrainStationExecution(player, intent.unitId);
case "quick_chat":
return new QuickChatExecution(
player,
+29 -10
View File
@@ -1,28 +1,30 @@
import { Execution, Game, Player, Unit, UnitType } from "../game/Game";
import { TileRef } from "../game/GameMap";
import { TrainStationExecution } from "./TrainStationExecution";
export class FactoryExecution implements Execution {
private factory: Unit | null = null;
private active: boolean = true;
private game: Game;
constructor(
private player: Player,
private tile: TileRef,
) {}
init(mg: Game, ticks: number): void {
const spawnTile = this.player.canBuild(UnitType.Factory, this.tile);
if (spawnTile === false) {
console.warn("cannot build factory");
this.active = false;
return;
}
this.factory = this.player.buildUnit(UnitType.Factory, spawnTile, {});
this.game = mg;
}
tick(ticks: number): void {
if (this.factory === null) {
throw new Error("Not initialized");
if (!this.factory) {
const spawnTile = this.player.canBuild(UnitType.Factory, this.tile);
if (spawnTile === false) {
console.warn("cannot build factory");
this.active = false;
return;
}
this.factory = this.player.buildUnit(UnitType.Factory, spawnTile, {});
this.createStation();
}
if (!this.factory.isActive()) {
this.active = false;
@@ -41,4 +43,21 @@ export class FactoryExecution implements Execution {
activeDuringSpawnPhase(): boolean {
return false;
}
createStation(): void {
if (this.factory !== null) {
const structures = this.game.nearbyUnits(
this.factory.tile()!,
this.game.config().trainStationMaxRange(),
[UnitType.City, UnitType.Port, UnitType.Factory],
);
// Use different seeds or trains will spawn simultaneously
let seed = 0;
for (const { unit } of structures) {
if (!unit.hasTrainStation()) {
this.game.addExecution(new TrainStationExecution(unit, ++seed));
}
}
}
}
}
+1 -26
View File
@@ -22,7 +22,6 @@ import { ConstructionExecution } from "./ConstructionExecution";
import { EmojiExecution } from "./EmojiExecution";
import { NukeExecution } from "./NukeExecution";
import { SpawnExecution } from "./SpawnExecution";
import { TrainStationExecution } from "./TrainStationExecution";
import { TransportShipExecution } from "./TransportShipExecution";
import { closestTwoTiles } from "./Util";
import { BotBehavior } from "./utils/BotBehavior";
@@ -438,35 +437,11 @@ export class FakeHumanExecution implements Execution {
this.maybeSpawnStructure(UnitType.Port, 1) ||
this.maybeSpawnStructure(UnitType.City, 2) ||
this.maybeSpawnWarship() ||
this.maybeSpawnTrainStation() ||
this.maybeSpawnStructure(UnitType.Factory, 1) ||
this.maybeSpawnStructure(UnitType.MissileSilo, 1)
);
}
private maybeSpawnTrainStation(): boolean {
if (this.mg.config().isUnitDisabled(UnitType.Train)) {
return false;
}
if (this.player === null) throw new Error("not initialized");
const citiesWithoutStations = this.player.units().filter((unit) => {
switch (unit.type()) {
case UnitType.City:
case UnitType.Port:
case UnitType.Factory:
return !unit.hasTrainStation();
default:
return false;
}
});
if (citiesWithoutStations.length === 0) {
return false;
}
this.mg.addExecution(
new TrainStationExecution(this.player, citiesWithoutStations[0].id()),
);
return true;
}
private maybeSpawnStructure(type: UnitType, maxNum: number): boolean {
if (this.player === null) throw new Error("not initialized");
if (this.player.unitsOwned(type) >= maxNum) {
+2 -4
View File
@@ -22,7 +22,7 @@ export class TrainExecution implements Execution {
private usedTiles: TileRef[] = []; // used for cars behind
private stations: TrainStation[] = [];
private currentRailroad: OrientedRailroad | null = null;
private speed: number = 3;
private speed: number = 2;
constructor(
private railNetwork: RailNetwork,
@@ -30,9 +30,7 @@ export class TrainExecution implements Execution {
private source: TrainStation,
private destination: TrainStation,
private numCars: number,
) {
this.hasCargo = source.unit.type() === UnitType.Factory;
}
) {}
init(mg: Game, ticks: number): void {
this.mg = mg;
+4 -19
View File
@@ -1,4 +1,4 @@
import { Execution, Game, Player, Unit, UnitType } from "../game/Game";
import { Execution, Game, Unit } from "../game/Game";
import { TrainStation } from "../game/TrainStation";
import { PseudoRandom } from "../PseudoRandom";
import { TrainExecution } from "./TrainExecution";
@@ -8,11 +8,10 @@ export class TrainStationExecution implements Execution {
private active: boolean = true;
private random: PseudoRandom | null = null;
private station: TrainStation | null = null;
private unit: Unit | undefined = undefined;
private numCars: number = 5;
constructor(
private player: Player,
private unitId: number,
private unit: Unit,
private randomSeed?: number,
) {}
isActive(): boolean {
@@ -21,21 +20,7 @@ export class TrainStationExecution implements Execution {
init(mg: Game, ticks: number): void {
this.mg = mg;
if (this.mg.config().isUnitDisabled(UnitType.Train)) {
this.active = false;
console.warn(`train station is disabled`);
return;
}
this.random = new PseudoRandom(mg.ticks());
this.unit = this.player.units().find((unit) => unit.id() === this.unitId);
if (this.unit === undefined) {
console.warn(`station unit is undefined`);
this.active = false;
return;
}
this.random = new PseudoRandom(mg.ticks() + (this.randomSeed ?? 0));
this.unit.setTrainStation(true);
}