mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-03 23:40:34 +00:00
43397779fa
## Description: Add a rail network to handle train stations/railroad between structures. Changes: - `RailNetwork` is responsible for the train station graph. Use it to connect new `TrainStations` - A `RailRoad` connects two `TrainStation` - No loop possible in the rail network - Train stations handles its railroads - Added a layer to draw the railroads under the structures #### Clusters - To speed up computations, each `TrainStation` references its own cluster - A cluster is a list of `TrainStation` connected with each other, created by the `RailNetwork` when connecting the station - Train stations spawn trains randomly depending on its current cluster size - A `TrainStation` decides randomly of the train destination by picking one from the cluster #### Production building: - Added a factory which has no gameplay impact currently. _To be discussed._ #### Train stops: - When a train reaches a factory, it's filled with a "cargo". The loaded trains has no impact currently. _To be discussed._ - When a train reaches a city, the player earn 10k gold - When a train reaches a port, it sends a new tradeship if possible - If a destination/source is destroyed, the train & railroad are deleted too https://github.com/user-attachments/assets/42375c17-9e04-4a42-98d0-708c81ffd609 https://github.com/user-attachments/assets/fbecdb53-a516-4df8-87fb-1f9a62c4efa0 ## 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 --------- Co-authored-by: Scott Anderson <scottanderson@users.noreply.github.com>
228 lines
5.2 KiB
TypeScript
228 lines
5.2 KiB
TypeScript
import { TradeShipExecution } from "../execution/TradeShipExecution";
|
|
import { TrainExecution } from "../execution/TrainExecution";
|
|
import { GraphAdapter } from "../pathfinding/SerialAStar";
|
|
import { PseudoRandom } from "../PseudoRandom";
|
|
import { Game, Player, Unit, UnitType } from "./Game";
|
|
import { TileRef } from "./GameMap";
|
|
import { GameUpdateType, RailTile, RailType } from "./GameUpdates";
|
|
import { Railroad } from "./Railroad";
|
|
|
|
/**
|
|
* Handle train stops at various station types
|
|
*/
|
|
interface TrainStopHandler {
|
|
onStop(mg: Game, station: TrainStation, trainExecution: TrainExecution): void;
|
|
}
|
|
|
|
class CityStopHandler implements TrainStopHandler {
|
|
onStop(
|
|
mg: Game,
|
|
station: TrainStation,
|
|
trainExecution: TrainExecution,
|
|
): void {
|
|
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,
|
|
});
|
|
}
|
|
}
|
|
|
|
class PortStopHandler implements TrainStopHandler {
|
|
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));
|
|
}
|
|
}
|
|
|
|
class FactoryStopHandler implements TrainStopHandler {
|
|
onStop(
|
|
mg: Game,
|
|
station: TrainStation,
|
|
trainExecution: TrainExecution,
|
|
): void {
|
|
trainExecution.loadCargo();
|
|
}
|
|
}
|
|
|
|
export function createTrainStopHandlers(
|
|
random: PseudoRandom,
|
|
): Partial<Record<UnitType, TrainStopHandler>> {
|
|
return {
|
|
[UnitType.City]: new CityStopHandler(),
|
|
[UnitType.Port]: new PortStopHandler(random),
|
|
[UnitType.Factory]: new FactoryStopHandler(),
|
|
};
|
|
}
|
|
|
|
export class TrainStation {
|
|
private readonly stopHandlers: Partial<Record<UnitType, TrainStopHandler>> =
|
|
{};
|
|
private cluster: Cluster | null;
|
|
private railroads: Set<Railroad> = new Set();
|
|
|
|
constructor(
|
|
private mg: Game,
|
|
public unit: Unit,
|
|
) {
|
|
this.stopHandlers = createTrainStopHandlers(new PseudoRandom(mg.ticks()));
|
|
}
|
|
|
|
tradeAvailable(otherPlayer: Player): boolean {
|
|
const player = this.unit.owner();
|
|
return otherPlayer === player || player.canTrade(otherPlayer);
|
|
}
|
|
|
|
clearRailroads() {
|
|
this.railroads.clear();
|
|
}
|
|
|
|
addRailroad(railRoad: Railroad) {
|
|
this.railroads.add(railRoad);
|
|
}
|
|
|
|
removeNeighboringRails(station: TrainStation) {
|
|
const toRemove = [...this.railroads].find(
|
|
(r) => r.from === station || r.to === station,
|
|
);
|
|
if (toRemove) {
|
|
const railTiles: RailTile[] = toRemove.tiles.map((tile) => ({
|
|
tile,
|
|
railType: RailType.VERTICAL,
|
|
}));
|
|
this.mg.addUpdate({
|
|
type: GameUpdateType.RailroadEvent,
|
|
isActive: false,
|
|
railTiles,
|
|
});
|
|
this.railroads.delete(toRemove);
|
|
}
|
|
}
|
|
|
|
neighbors(): TrainStation[] {
|
|
const neighbors: TrainStation[] = [];
|
|
for (const r of this.railroads) {
|
|
if (r.from !== this) {
|
|
neighbors.push(r.from);
|
|
} else {
|
|
neighbors.push(r.to);
|
|
}
|
|
}
|
|
return neighbors;
|
|
}
|
|
|
|
tile(): TileRef {
|
|
return this.unit.tile();
|
|
}
|
|
|
|
isActive(): boolean {
|
|
return this.unit.isActive();
|
|
}
|
|
|
|
getRailroads(): Set<Railroad> {
|
|
return this.railroads;
|
|
}
|
|
|
|
setCluster(cluster: Cluster | null) {
|
|
this.cluster = cluster;
|
|
}
|
|
|
|
getCluster(): Cluster | null {
|
|
return this.cluster;
|
|
}
|
|
|
|
onTrainStop(trainExecution: TrainExecution) {
|
|
const type = this.unit.type();
|
|
const handler = this.stopHandlers[type];
|
|
if (handler) {
|
|
handler.onStop(this.mg, this, trainExecution);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Make the trainstation usable with A*
|
|
*/
|
|
export class TrainStationMapAdapter implements GraphAdapter<TrainStation> {
|
|
constructor(private game: Game) {}
|
|
|
|
neighbors(node: TrainStation): TrainStation[] {
|
|
return node.neighbors();
|
|
}
|
|
|
|
cost(node: TrainStation): number {
|
|
return 1;
|
|
}
|
|
|
|
position(node: TrainStation): { x: number; y: number } {
|
|
return { x: this.game.x(node.tile()), y: this.game.y(node.tile()) };
|
|
}
|
|
|
|
isTraversable(from: TrainStation, to: TrainStation): boolean {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Cluster of connected stations
|
|
*/
|
|
export class Cluster {
|
|
public stations: Set<TrainStation> = new Set();
|
|
|
|
has(station: TrainStation) {
|
|
return this.stations.has(station);
|
|
}
|
|
|
|
addStation(station: TrainStation) {
|
|
this.stations.add(station);
|
|
station.setCluster(this);
|
|
}
|
|
|
|
removeStation(station: TrainStation) {
|
|
this.stations.delete(station);
|
|
}
|
|
|
|
addStations(stations: Set<TrainStation>) {
|
|
for (const station of stations) {
|
|
this.addStation(station);
|
|
}
|
|
}
|
|
|
|
merge(other: Cluster) {
|
|
for (const s of other.stations) {
|
|
this.addStation(s);
|
|
}
|
|
}
|
|
|
|
availableForTrade(player: Player): Set<TrainStation> {
|
|
const tradingStations = new Set<TrainStation>();
|
|
for (const station of this.stations) {
|
|
if (station.tradeAvailable(player)) {
|
|
tradingStations.add(station);
|
|
}
|
|
}
|
|
return tradingStations;
|
|
}
|
|
|
|
size() {
|
|
return this.stations.size;
|
|
}
|
|
|
|
clear() {
|
|
this.stations.clear();
|
|
}
|
|
}
|