mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 06:20:16 +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>
133 lines
3.7 KiB
TypeScript
133 lines
3.7 KiB
TypeScript
import { TrainExecution } from "../../../src/core/execution/TrainExecution";
|
|
import { Game, Unit, UnitType } from "../../../src/core/game/Game";
|
|
import { Cluster, TrainStation } from "../../../src/core/game/TrainStation";
|
|
|
|
jest.mock("../../../src/core/game/Game");
|
|
jest.mock("../../../src/core/execution/TrainExecution");
|
|
jest.mock("../../../src/core/PseudoRandom");
|
|
|
|
describe("TrainStation", () => {
|
|
let game: jest.Mocked<Game>;
|
|
let unit: jest.Mocked<Unit>;
|
|
let trainExecution: jest.Mocked<TrainExecution>;
|
|
|
|
beforeEach(() => {
|
|
game = {
|
|
ticks: jest.fn().mockReturnValue(123),
|
|
config: jest.fn().mockReturnValue({
|
|
trainGold: () => 10,
|
|
}),
|
|
addUpdate: jest.fn(),
|
|
addExecution: jest.fn(),
|
|
} as any;
|
|
|
|
unit = {
|
|
owner: jest.fn().mockReturnValue({
|
|
addGold: jest.fn(),
|
|
id: 1,
|
|
canTrade: jest.fn().mockReturnValue(true),
|
|
tradingPorts: jest.fn().mockReturnValue([{ name: "Port1" }]),
|
|
}),
|
|
tile: jest.fn().mockReturnValue({ x: 0, y: 0 }),
|
|
type: jest.fn(),
|
|
isActive: jest.fn().mockReturnValue(true),
|
|
} as any;
|
|
|
|
trainExecution = {
|
|
loadCargo: jest.fn(),
|
|
} as any;
|
|
});
|
|
|
|
it("handles City stop", () => {
|
|
unit.type.mockReturnValue(UnitType.City);
|
|
const station = new TrainStation(game, unit);
|
|
|
|
station.onTrainStop(trainExecution);
|
|
|
|
expect(unit.owner().addGold).toHaveBeenCalledWith(10);
|
|
expect(game.addUpdate).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
type: expect.any(Number),
|
|
gold: 10,
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("handles Port stop", () => {
|
|
unit.type.mockReturnValue(UnitType.Port);
|
|
|
|
const station = new TrainStation(game, unit);
|
|
station.onTrainStop(trainExecution);
|
|
|
|
expect(game.addExecution).toHaveBeenCalled();
|
|
});
|
|
|
|
it("handles Factory stop", () => {
|
|
unit.type.mockReturnValue(UnitType.Factory);
|
|
|
|
const station = new TrainStation(game, unit);
|
|
station.onTrainStop(trainExecution);
|
|
|
|
expect(trainExecution.loadCargo).toHaveBeenCalled();
|
|
});
|
|
|
|
it("checks trade availability (same owner)", () => {
|
|
const otherUnit = {
|
|
owner: jest.fn().mockReturnValue(unit.owner()),
|
|
} as any;
|
|
|
|
const station = new TrainStation(game, unit);
|
|
const otherStation = new TrainStation(game, otherUnit);
|
|
|
|
expect(station.tradeAvailable(otherStation.unit.owner())).toBe(true);
|
|
});
|
|
|
|
it("adds and retrieves neighbors", () => {
|
|
const stationA = new TrainStation(game, unit);
|
|
const stationB = new TrainStation(game, unit);
|
|
const railRoad = { from: stationA, to: stationB, tiles: [] } as any;
|
|
|
|
stationA.addRailroad(railRoad);
|
|
|
|
const neighbors = stationA.neighbors();
|
|
expect(neighbors).toContain(stationB);
|
|
});
|
|
|
|
it("removes neighboring rail", () => {
|
|
const stationA = new TrainStation(game, unit);
|
|
const stationB = new TrainStation(game, unit);
|
|
|
|
const railRoad = {
|
|
from: stationA,
|
|
to: stationB,
|
|
tiles: [{ x: 1, y: 1 }],
|
|
} as any;
|
|
|
|
stationA.addRailroad(railRoad);
|
|
expect(stationA.getRailroads().size).toBe(1);
|
|
|
|
stationA.removeNeighboringRails(stationB);
|
|
|
|
expect(game.addUpdate).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
isActive: false,
|
|
}),
|
|
);
|
|
expect(stationA.getRailroads().size).toBe(0);
|
|
});
|
|
|
|
it("assigns and retrieves cluster", () => {
|
|
const cluster: Cluster = {} as Cluster;
|
|
const station = new TrainStation(game, unit);
|
|
|
|
station.setCluster(cluster);
|
|
expect(station.getCluster()).toBe(cluster);
|
|
});
|
|
|
|
it("returns tile and active status", () => {
|
|
const station = new TrainStation(game, unit);
|
|
expect(station.tile()).toEqual({ x: 0, y: 0 });
|
|
expect(station.isActive()).toBe(true);
|
|
});
|
|
});
|