mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 06:10:42 +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>
164 lines
5.2 KiB
TypeScript
164 lines
5.2 KiB
TypeScript
import { Unit } from "../../../src/core/game/Game";
|
|
import {
|
|
RailNetworkImpl,
|
|
StationManagerImpl,
|
|
} from "../../../src/core/game/RailNetworkImpl";
|
|
import { Railroad } from "../../../src/core/game/Railroad";
|
|
import { Cluster } from "../../../src/core/game/TrainStation";
|
|
|
|
// Mock types
|
|
const createMockStation = (unitId: number): any => {
|
|
const cluster = new Cluster();
|
|
const railroads = new Set<Railroad>();
|
|
return {
|
|
unit: { id: unitId },
|
|
tile: jest.fn(),
|
|
neighbors: jest.fn(() => []),
|
|
getCluster: jest.fn(() => cluster),
|
|
setCluster: jest.fn(),
|
|
addRailroad: jest.fn(),
|
|
getRailroads: jest.fn(() => railroads),
|
|
clearRailroads: jest.fn(),
|
|
};
|
|
};
|
|
|
|
describe("StationManagerImpl", () => {
|
|
let manager: StationManagerImpl;
|
|
|
|
beforeEach(() => {
|
|
manager = new StationManagerImpl();
|
|
});
|
|
|
|
test("adds and retrieves station", () => {
|
|
const station = createMockStation(1);
|
|
manager.addStation(station);
|
|
expect(manager.findStation(station.unit)).toBe(station);
|
|
});
|
|
|
|
test("removes station", () => {
|
|
const station = createMockStation(1);
|
|
manager.addStation(station);
|
|
manager.removeStation(station);
|
|
expect(manager.findStation(station.unit)).toBe(null);
|
|
});
|
|
});
|
|
|
|
describe("RailNetworkImpl", () => {
|
|
let network: RailNetworkImpl;
|
|
let stationManager: any;
|
|
let pathService: any;
|
|
let game: any;
|
|
|
|
beforeEach(() => {
|
|
stationManager = {
|
|
addStation: jest.fn(),
|
|
removeStation: jest.fn(),
|
|
findStation: jest.fn(),
|
|
getAll: jest.fn(() => new Set()),
|
|
};
|
|
pathService = {
|
|
findTilePath: jest.fn(() => [0]),
|
|
findStationsPath: jest.fn(() => [0]),
|
|
};
|
|
game = {
|
|
nearbyUnits: jest.fn(() => []),
|
|
addExecution: jest.fn(),
|
|
config: () => ({
|
|
trainStationMaxRange: () => 80,
|
|
trainStationMinRange: () => 10,
|
|
railroadMaxSize: () => 100,
|
|
}),
|
|
};
|
|
|
|
network = new RailNetworkImpl(game, stationManager, pathService);
|
|
});
|
|
|
|
test("does not connect if path is empty or too long", () => {
|
|
const stationA = createMockStation(1);
|
|
const stationB = createMockStation(2);
|
|
|
|
game.nearbyUnits.mockReturnValue([stationB]);
|
|
|
|
pathService.findTilePath.mockReturnValue([]);
|
|
network.connectStation(stationA);
|
|
|
|
const cluster = stationB.getCluster();
|
|
cluster.addStation = jest.fn();
|
|
expect(cluster.addStation).not.toHaveBeenCalled();
|
|
|
|
pathService.findTilePath.mockReturnValue(new Array(200));
|
|
network.connectStation(stationA);
|
|
expect(cluster.addStation).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("removeStation removes all neighbor links", () => {
|
|
const neighbor = { removeNeighboringRails: jest.fn() };
|
|
const station = createMockStation(1);
|
|
station.neighbors = jest.fn(() => [neighbor]);
|
|
stationManager.findStation.mockReturnValue(station);
|
|
network.removeStation(station);
|
|
expect(station.clearRailroads).toHaveBeenCalled();
|
|
});
|
|
|
|
test("connectStation calls addStation and connects to nearby", () => {
|
|
const station = createMockStation(1);
|
|
network.connectStation(station);
|
|
expect(stationManager.addStation).toHaveBeenCalledWith(station);
|
|
});
|
|
|
|
test("removeStation does nothing if station not found", () => {
|
|
stationManager.findStation.mockReturnValue(null);
|
|
network.removeStation({ id: 1 } as unknown as Unit);
|
|
expect(stationManager.removeStation).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("removeStation disconnects and removes from cluster if one neighbor", () => {
|
|
const cluster = new Cluster();
|
|
const neighbor = createMockStation(1);
|
|
const station = createMockStation(2);
|
|
station.getCluster = jest.fn(() => cluster);
|
|
station.neighbors = jest.fn(() => [neighbor]);
|
|
cluster.removeStation = jest.fn();
|
|
|
|
stationManager.findStation.mockReturnValue(station);
|
|
|
|
network.removeStation(station.unit);
|
|
expect(cluster.removeStation).toHaveBeenCalledWith(station);
|
|
expect(stationManager.removeStation).toHaveBeenCalledWith(station);
|
|
});
|
|
|
|
test("findStationsPath", () => {
|
|
const stationA = createMockStation(1);
|
|
const stationB = createMockStation(2);
|
|
const result = network.findStationsPath(stationA, stationB);
|
|
expect(result).toEqual([0]);
|
|
});
|
|
|
|
test("connectToNearbyStations creates new cluster when no neighbors", () => {
|
|
const station = createMockStation(1);
|
|
game.nearbyUnits.mockReturnValue([]);
|
|
network.connectStation(station);
|
|
expect(stationManager.addStation).toHaveBeenCalledWith(station);
|
|
expect(station.setCluster).toHaveBeenCalled();
|
|
});
|
|
|
|
test("connectToNearbyStations connects and merges clusters", () => {
|
|
const station = createMockStation(1);
|
|
const neighborStation = createMockStation(2);
|
|
const cluster = new Cluster();
|
|
cluster.addStation(neighborStation);
|
|
neighborStation.getCluster = jest.fn(() => cluster);
|
|
cluster.has = jest.fn(() => false);
|
|
|
|
const neighborUnit = { unit: neighborStation.unit, distSquared: 20 };
|
|
|
|
game.nearbyUnits.mockReturnValue([neighborUnit]);
|
|
stationManager.findStation.mockReturnValue(neighborStation);
|
|
|
|
network.connectStation(station);
|
|
// Both station should have their cluster reset to the merged one
|
|
expect(station.setCluster).toHaveBeenCalled();
|
|
expect(neighborStation.setCluster).toHaveBeenCalled();
|
|
});
|
|
});
|