Add trains (#1159)

## 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>
This commit is contained in:
DevelopingTom
2025-06-22 08:14:08 -07:00
committed by GitHub
co-authored by Scott Anderson
parent 0f2008a68d
commit 43397779fa
60 changed files with 2427 additions and 104 deletions
+13 -7
View File
@@ -2,7 +2,7 @@ import { Game } from "../game/Game";
import { GameMap, TileRef } from "../game/GameMap";
import { PseudoRandom } from "../PseudoRandom";
import { DistanceBasedBezierCurve } from "../utilities/Line";
import { AStar, PathFindResultType, TileResult } from "./AStar";
import { AStar, AStarResult, PathFindResultType } from "./AStar";
import { MiniAStar } from "./MiniAStar";
const parabolaMinHeight = 50;
@@ -89,15 +89,20 @@ export class PathFinder {
private curr: TileRef | null = null;
private dst: TileRef | null = null;
private path: TileRef[] | null = null;
private aStar: AStar;
private aStar: AStar<TileRef>;
private computeFinished = true;
private constructor(
private game: Game,
private newAStar: (curr: TileRef, dst: TileRef) => AStar,
private newAStar: (curr: TileRef, dst: TileRef) => AStar<TileRef>,
) {}
public static Mini(game: Game, iterations: number, maxTries: number = 20) {
public static Mini(
game: Game,
iterations: number,
waterPath: boolean = true,
maxTries: number = 20,
) {
return new PathFinder(game, (curr: TileRef, dst: TileRef) => {
return new MiniAStar(
game.map(),
@@ -106,6 +111,7 @@ export class PathFinder {
dst,
iterations,
maxTries,
waterPath,
);
});
}
@@ -114,7 +120,7 @@ export class PathFinder {
curr: TileRef | null,
dst: TileRef | null,
dist: number = 1,
): TileResult {
): AStarResult<TileRef> {
if (curr === null) {
console.error("curr is null");
return { type: PathFindResultType.PathNotFound };
@@ -125,7 +131,7 @@ export class PathFinder {
}
if (this.game.manhattanDist(curr, dst) < dist) {
return { type: PathFindResultType.Completed, tile: curr };
return { type: PathFindResultType.Completed, node: curr };
}
if (this.computeFinished) {
@@ -141,7 +147,7 @@ export class PathFinder {
if (tile === undefined) {
throw new Error("missing tile");
}
return { type: PathFindResultType.NextTile, tile };
return { type: PathFindResultType.NextTile, node: tile };
}
}