Feat: Display ghost railways when building cities and ports (#3202)

## Description:

Based on [this suggestion on
Discord](https://discord.com/channels/1284581928254701718/1447110257196138577)
and feedback gathered in [this
thread](https://discord.com/channels/1359946986937258015/1469598906173227184).

Supersedes #3143 

This PR introduces "ghost railways": when you are going to place a city
or port, previews railway connections that will be made when actually
building the structure.

Ghost railways are skipped if the structure is going to be snapped to
existing railways (as in railway snapping functionality introduced in
#3156 ).

### Video


https://github.com/user-attachments/assets/ff8cf325-6501-4df8-801d-c8ae3ced3d0e


### Ghost rails color revisited

black with 40% opacity

<img width="695" height="430" alt="image"
src="https://github.com/user-attachments/assets/272efbcc-4185-426a-921c-7fae61f6c462"
/>


## 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

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

deshack_82603
This commit is contained in:
Mattia Migliorini
2026-02-18 22:44:08 +01:00
committed by GitHub
parent f7ebde1c7f
commit ba2a947061
10 changed files with 309 additions and 18 deletions
+1
View File
@@ -852,6 +852,7 @@ export interface BuildableUnit {
type: UnitType;
cost: Gold;
overlappingRailroads: number[];
ghostRailPaths: TileRef[][];
}
export interface PlayerProfile {
+4
View File
@@ -993,6 +993,10 @@ export class PlayerImpl implements Player {
canBuild !== false
? this.mg.railNetwork().overlappingRailroads(canBuild)
: [],
ghostRailPaths:
canBuild !== false
? this.mg.railNetwork().computeGhostRailPaths(u, canBuild)
: [],
} as BuildableUnit;
});
}
+2 -1
View File
@@ -1,4 +1,4 @@
import { Unit } from "./Game";
import { Unit, UnitType } from "./Game";
import { TileRef } from "./GameMap";
import { StationManager } from "./RailNetworkImpl";
import { TrainStation } from "./TrainStation";
@@ -9,5 +9,6 @@ export interface RailNetwork {
findStationsPath(from: TrainStation, to: TrainStation): TrainStation[];
stationManager(): StationManager;
overlappingRailroads(tile: TileRef): number[];
computeGhostRailPaths(unitType: UnitType, tile: TileRef): TileRef[][];
recomputeClusters(): void;
}
+61
View File
@@ -228,6 +228,67 @@ export class RailNetworkImpl implements RailNetwork {
);
}
private canSnapToExistingRailway(tile: TileRef): boolean {
return this.railGrid.query(tile, this.stationRadius).size > 0;
}
computeGhostRailPaths(unitType: UnitType, tile: TileRef): TileRef[][] {
// Factories already show their radius, so we'll exclude from ghost rails
// in order not to clutter the interface too much.
if (![UnitType.City, UnitType.Port].includes(unitType)) {
return [];
}
if (this.canSnapToExistingRailway(tile)) {
return [];
}
const maxRange = this.game.config().trainStationMaxRange();
const minRangeSquared = this.game.config().trainStationMinRange() ** 2;
const maxPathSize = this.game.config().railroadMaxSize();
// Cannot connect if outside the max range of a factory
if (!this.game.hasUnitNearby(tile, maxRange, UnitType.Factory)) {
return [];
}
const neighbors = this.game.nearbyUnits(tile, maxRange, [
UnitType.City,
UnitType.Factory,
UnitType.Port,
]);
neighbors.sort((a, b) => a.distSquared - b.distSquared);
const paths: TileRef[][] = [];
const connectedStations: TrainStation[] = [];
for (const neighbor of neighbors) {
// Limit to the closest 5 stations to avoid running too many pathfinding calls.
if (paths.length >= 5) break;
if (neighbor.distSquared <= minRangeSquared) continue;
const neighborStation = this._stationManager.findStation(neighbor.unit);
if (!neighborStation) continue;
const alreadyReachable = connectedStations.some(
(s) =>
this.distanceFrom(
neighborStation,
s,
this.maxConnectionDistance - 1,
) !== -1,
);
if (alreadyReachable) continue;
const path = this.pathService.findTilePath(tile, neighborStation.tile());
if (path.length > 0 && path.length < maxPathSize) {
paths.push(path);
connectedStations.push(neighborStation);
}
}
return paths;
}
private connectToNearbyStations(station: TrainStation) {
const neighbors = this.game.nearbyUnits(
station.tile(),