mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-10 06:04:36 +00:00
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:
+1
-10
@@ -38,9 +38,7 @@ export type Intent =
|
||||
| QuickChatIntent
|
||||
| MoveWarshipIntent
|
||||
| MarkDisconnectedIntent
|
||||
| UpgradeStructureIntent
|
||||
| CreateStationIntent;
|
||||
|
||||
| UpgradeStructureIntent;
|
||||
export type AttackIntent = z.infer<typeof AttackIntentSchema>;
|
||||
export type CancelAttackIntent = z.infer<typeof CancelAttackIntentSchema>;
|
||||
export type SpawnIntent = z.infer<typeof SpawnIntentSchema>;
|
||||
@@ -63,7 +61,6 @@ export type BuildUnitIntent = z.infer<typeof BuildUnitIntentSchema>;
|
||||
export type UpgradeStructureIntent = z.infer<
|
||||
typeof UpgradeStructureIntentSchema
|
||||
>;
|
||||
export type CreateStationIntent = z.infer<typeof CreateStationIntentSchema>;
|
||||
export type MoveWarshipIntent = z.infer<typeof MoveWarshipIntentSchema>;
|
||||
export type QuickChatIntent = z.infer<typeof QuickChatIntentSchema>;
|
||||
export type MarkDisconnectedIntent = z.infer<
|
||||
@@ -310,11 +307,6 @@ export const UpgradeStructureIntentSchema = BaseIntentSchema.extend({
|
||||
unitId: z.number(),
|
||||
});
|
||||
|
||||
export const CreateStationIntentSchema = BaseIntentSchema.extend({
|
||||
type: z.literal("create_station"),
|
||||
unitId: z.number(),
|
||||
});
|
||||
|
||||
export const CancelAttackIntentSchema = BaseIntentSchema.extend({
|
||||
type: z.literal("cancel_attack"),
|
||||
attackID: z.string(),
|
||||
@@ -360,7 +352,6 @@ const IntentSchema = z.discriminatedUnion("type", [
|
||||
TargetTroopRatioIntentSchema,
|
||||
BuildUnitIntentSchema,
|
||||
UpgradeStructureIntentSchema,
|
||||
CreateStationIntentSchema,
|
||||
EmbargoIntentSchema,
|
||||
MoveWarshipIntentSchema,
|
||||
QuickChatIntentSchema,
|
||||
|
||||
@@ -37,6 +37,7 @@ export const OtherUnitSchema = z.union([
|
||||
z.literal("wshp"),
|
||||
z.literal("silo"),
|
||||
z.literal("saml"),
|
||||
z.literal("fact"),
|
||||
]);
|
||||
export type OtherUnit = z.infer<typeof OtherUnitSchema>;
|
||||
export type OtherUnitType =
|
||||
@@ -45,7 +46,8 @@ export type OtherUnitType =
|
||||
| UnitType.MissileSilo
|
||||
| UnitType.Port
|
||||
| UnitType.SAMLauncher
|
||||
| UnitType.Warship;
|
||||
| UnitType.Warship
|
||||
| UnitType.Factory;
|
||||
|
||||
export const unitTypeToOtherUnit = {
|
||||
[UnitType.City]: "city",
|
||||
@@ -54,6 +56,7 @@ export const unitTypeToOtherUnit = {
|
||||
[UnitType.Port]: "port",
|
||||
[UnitType.SAMLauncher]: "saml",
|
||||
[UnitType.Warship]: "wshp",
|
||||
[UnitType.Factory]: "fact",
|
||||
} as const satisfies Record<OtherUnitType, OtherUnit>;
|
||||
|
||||
// Attacks
|
||||
|
||||
@@ -310,7 +310,7 @@ export class DefaultConfig implements Config {
|
||||
return Math.min(50, Math.round(10 * Math.pow(numberOfPorts, 0.6)));
|
||||
}
|
||||
trainSpawnRate(numberOfStations: number): number {
|
||||
return Math.min(1400, Math.round(60 * Math.pow(numberOfStations, 0.8)));
|
||||
return Math.min(1400, Math.round(70 * Math.pow(numberOfStations, 0.8)));
|
||||
}
|
||||
trainGold(): Gold {
|
||||
return BigInt(10_000);
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -633,6 +633,12 @@ export interface Game extends GameMap {
|
||||
// Units
|
||||
units(...types: UnitType[]): Unit[];
|
||||
unitInfo(type: UnitType): UnitInfo;
|
||||
hasUnitNearby(
|
||||
tile: TileRef,
|
||||
searchRange: number,
|
||||
type: UnitType,
|
||||
playerId: PlayerID,
|
||||
);
|
||||
nearbyUnits(
|
||||
tile: TileRef,
|
||||
searchRange: number,
|
||||
|
||||
@@ -715,6 +715,15 @@ export class GameImpl implements Game {
|
||||
}
|
||||
}
|
||||
|
||||
hasUnitNearby(
|
||||
tile: TileRef,
|
||||
searchRange: number,
|
||||
type: UnitType,
|
||||
playerId: PlayerID,
|
||||
) {
|
||||
return this.unitGrid.hasUnitNearby(tile, searchRange, type, playerId);
|
||||
}
|
||||
|
||||
nearbyUnits(
|
||||
tile: TileRef,
|
||||
searchRange: number,
|
||||
|
||||
@@ -120,6 +120,7 @@ export class RailNetworkImpl implements RailNetwork {
|
||||
newCluster.addStations(stations);
|
||||
}
|
||||
}
|
||||
station.unit.setTrainStation(false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { TradeShipExecution } from "../execution/TradeShipExecution";
|
||||
import { TrainExecution } from "../execution/TrainExecution";
|
||||
import { GraphAdapter } from "../pathfinding/SerialAStar";
|
||||
import { PseudoRandom } from "../PseudoRandom";
|
||||
@@ -14,30 +13,34 @@ interface TrainStopHandler {
|
||||
onStop(mg: Game, station: TrainStation, trainExecution: TrainExecution): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* All stop handlers share the same logic for the time being
|
||||
* Behavior to be defined
|
||||
*/
|
||||
class CityStopHandler implements TrainStopHandler {
|
||||
private factor: bigint = BigInt(2);
|
||||
onStop(
|
||||
mg: Game,
|
||||
station: TrainStation,
|
||||
trainExecution: TrainExecution,
|
||||
): void {
|
||||
const goldBonus = mg.config().trainGold();
|
||||
const level = BigInt(station.unit.level() + 1);
|
||||
const goldBonus = (mg.config().trainGold() * level) / this.factor;
|
||||
station.unit.owner().addGold(goldBonus, station.tile());
|
||||
}
|
||||
}
|
||||
|
||||
class PortStopHandler implements TrainStopHandler {
|
||||
private factor: bigint = BigInt(2);
|
||||
constructor(private random: PseudoRandom) {}
|
||||
onStop(
|
||||
mg: Game,
|
||||
station: TrainStation,
|
||||
trainExecution: TrainExecution,
|
||||
): void {
|
||||
const unit = station.unit;
|
||||
const ports = unit.owner().tradingPorts(unit);
|
||||
if (ports.length === 0) return;
|
||||
|
||||
const port = this.random.randElement(ports);
|
||||
mg.addExecution(new TradeShipExecution(unit.owner(), unit, port));
|
||||
const level = BigInt(station.unit.level() + 1);
|
||||
const goldBonus = (mg.config().trainGold() * level) / this.factor;
|
||||
station.unit.owner().addGold(goldBonus, station.tile());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +50,15 @@ class FactoryStopHandler implements TrainStopHandler {
|
||||
station: TrainStation,
|
||||
trainExecution: TrainExecution,
|
||||
): void {
|
||||
trainExecution.loadCargo();
|
||||
const goldBonus = mg.config().trainGold();
|
||||
station.unit.owner().addGold(goldBonus);
|
||||
mg.addUpdate({
|
||||
type: GameUpdateType.BonusEvent,
|
||||
tile: station.tile(),
|
||||
gold: Number(goldBonus),
|
||||
workers: 0,
|
||||
troops: 0,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -249,6 +249,7 @@ export class UnitImpl implements Unit {
|
||||
case UnitType.Port:
|
||||
case UnitType.SAMLauncher:
|
||||
case UnitType.Warship:
|
||||
case UnitType.Factory:
|
||||
this.mg.stats().unitDestroy(destroyer, this._type);
|
||||
this.mg.stats().unitLose(this.owner(), this._type);
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user