Game State

Read current game state (read-only)

The game state API provides read-only access to current game data for analysis, UI mods, or debugging.

Stations#

getStations#

Get all stations in the current game.

javascript
const stations = window.SubwayBuilderAPI.gameState.getStations();
console.log('Total stations:', stations.length);

// Station data includes: id, name, coords, stationGroup, etc.
stations.forEach((station) => {
    console.log(`${station.name} at [${station.coords}]`);
});

Routes#

getRoutes#

Get all routes (subway lines).

javascript
const routes = window.SubwayBuilderAPI.gameState.getRoutes();
routes.forEach((route) => {
    console.log(`Route ${route.name}: ${route.stations.length} stations`);
});

Tracks#

getTracks#

Get all tracks with elevation/depth data.

javascript
const tracks = window.SubwayBuilderAPI.gameState.getTracks();

// Each track has startElevation and endElevation
// Negative values = underground, positive = elevated
tracks.forEach((track) => {
    console.log(`Track ${track.id}: ${track.startElevation}m to ${track.endElevation}m`);
});

// Find deepest track
const deepestTrack = tracks.reduce((deepest, track) => {
    const minElev = Math.min(track.startElevation, track.endElevation);
    const deepestElev = Math.min(deepest.startElevation, deepest.endElevation);
    return minElev < deepestElev ? track : deepest;
}, tracks[0]);
console.log('Deepest track:', deepestTrack?.startElevation, 'm');

Trains#

getTrains#

Get all active trains.

javascript
const trains = window.SubwayBuilderAPI.gameState.getTrains();
console.log('Active trains:', trains.length);

Time & Money#

getCurrentDay#

Get the current in-game day.

javascript
const day = window.SubwayBuilderAPI.gameState.getCurrentDay();

getCurrentHour#

Get the current in-game hour (0-23).

javascript
const hour = window.SubwayBuilderAPI.gameState.getCurrentHour();

getElapsedSeconds#

Get total elapsed in-game time in seconds.

javascript
const elapsedSeconds = window.SubwayBuilderAPI.gameState.getElapsedSeconds();

getBudget#

Get current money balance.

javascript
const budget = window.SubwayBuilderAPI.gameState.getBudget();
console.log(`$${budget.toLocaleString()} remaining`);

getTicketPrice#

Get current ticket price.

javascript
const ticketPrice = window.SubwayBuilderAPI.gameState.getTicketPrice();

Game Controls#

getGameSpeed#

Get current game speed.

javascript
const speed = window.SubwayBuilderAPI.gameState.getGameSpeed();
// Returns: 'slow', 'normal', 'fast', or 'ultrafast'

isPaused#

Check if game is paused.

javascript
const paused = window.SubwayBuilderAPI.gameState.isPaused();

Demand Data#

getDemandData#

Get population/commuter demand data.

javascript
const demandData = window.SubwayBuilderAPI.gameState.getDemandData();
if (demandData) {
    console.log('Demand points:', demandData.points.size);
    console.log('Population groups:', demandData.popsMap.size);

    // Iterate over demand points
    demandData.points.forEach((point, id) => {
        console.log(`Point ${id}: ${point.jobs} jobs, ${point.residents} residents`);
    });
}

Financial Data#

getBonds#

Get all active bonds.

javascript
const bonds = window.SubwayBuilderAPI.gameState.getBonds();
bonds.forEach((bond) => {
    console.log(`Bond: $${bond.principal} at ${bond.interestRate}%`);
});

getBondTypes#

Get available bond type definitions.

javascript
const bondTypes = window.SubwayBuilderAPI.gameState.getBondTypes();
console.log('Available bond types:', Object.keys(bondTypes));

Performance Metrics#

getRidershipStats#

Get ridership statistics.

javascript
const ridership = window.SubwayBuilderAPI.gameState.getRidershipStats();
console.log('Riders per hour:', ridership.totalRidersPerHour);
console.log('Total riders all time:', ridership.totalRiders);

getLineMetrics#

Get per-line performance metrics.

javascript
const lineMetrics = window.SubwayBuilderAPI.gameState.getLineMetrics();
lineMetrics.forEach((line) => {
    console.log(`${line.name}: ${line.ridersPerHour} riders/hr`);
});

getModeChoiceStats#

Get commuter mode choice breakdown.

javascript
const modes = window.SubwayBuilderAPI.gameState.getModeChoiceStats();
// { walking: 1200, driving: 8500, transit: 3200, unknown: 100 }

const total = modes.walking + modes.driving + modes.transit;
const transitShare = ((modes.transit / total) * 100).toFixed(1);
console.log(`Transit mode share: ${transitShare}%`);

getStationRidership#

Get ridership data for stations.

javascript
// All stations
const allStats = window.SubwayBuilderAPI.gameState.getStationRidership();
console.log('Total system ridership:', allStats.total);

// Specific station
const stationStats = window.SubwayBuilderAPI.gameState.getStationRidership('station-123');
console.log(`Riders: ${stationStats.total}, transfers: ${stationStats.transfers}`);
stationStats.byRoute.forEach((r) => {
    console.log(`  Route ${r.routeId}: ${r.popCount} riders`);
});

getRouteRidership#

Get ridership data for a route.

javascript
const routeStats = window.SubwayBuilderAPI.gameState.getRouteRidership('route-456');
routeStats.byStation.forEach((s) => {
    console.log(`  ${s.stationId}: ${s.popCount} riders`);
});

Blueprint Cost Calculation#

calculateBlueprintCost#

Calculate cost of blueprint tracks before construction.

javascript
const tracks = window.SubwayBuilderAPI.gameState.getTracks();
const blueprintTracks = tracks.filter((t) => t.displayType === 'blueprint');
const cost = window.SubwayBuilderAPI.gameState.calculateBlueprintCost(blueprintTracks);

console.log('Total cost:', cost.totalCost.toLocaleString());
console.log('Breakdown:');
console.log('  Track cost:', cost.breakdown.trackCost.toLocaleString());
console.log('  Station cost:', cost.breakdown.stationCost.toLocaleString());
console.log('  Scissors crossover:', cost.breakdown.scissorsCrossoverCost.toLocaleString());
console.log('  Demolition:', cost.breakdown.buildingDemolitionCost.toLocaleString());

State Summary#

MethodReturnsDescription
getStations()Station[]All stations
getRoutes()Route[]All routes
getTracks()Track[]All tracks
getTrains()Train[]All trains
getCurrentDay()numberCurrent day
getCurrentHour()numberCurrent hour (0-23)
getElapsedSeconds()numberTotal elapsed seconds
getBudget()numberCurrent money
getTicketPrice()numberTicket price
getGameSpeed()stringGame speed
isPaused()booleanPause state
getDemandData()DemandDataDemand/population data
getBonds()Bond[]Active bonds
getBondTypes()objectBond type definitions
getRidershipStats()objectRidership statistics
getLineMetrics()object[]Per-line metrics
getModeChoiceStats()objectMode choice breakdown
getStationRidership(id?)objectStation ridership
getRouteRidership(id)objectRoute ridership
calculateBlueprintCost(tracks)objectCost breakdown