# matthewxbird/openttd_ai architecture

> Architecture diagram of the GitHub repository matthewxbird/openttd_ai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/matthewxbird/openttd_ai
- Repository: https://github.com/matthewxbird/openttd_ai

## Overview

MvB AI is a competitive OpenTTD NoAI script. Its principal workflow scans map opportunities, compares rail, air, and road routes on a shared economic value surface, then builds the winning opportunity and manages its finances and route lifecycle. Major documented branches include rail backhaul and competition-aware pathfinding, road feeders, aircraft shuttles, and diagnostics. Relationships here are based on README documentation; source excerpts were not provided, so implementation-level call ownership cannot be verified.

Main entry points: [`main.nut`](https://github.com/matthewxbird/openttd_ai/blob/main/main.nut) (AI entry point), [`src/rail2_route.nut`](https://github.com/matthewxbird/openttd_ai/blob/main/src/rail2_route.nut) (Rail routing), [`src/strategy.nut`](https://github.com/matthewxbird/openttd_ai/blob/main/src/strategy.nut) (Mode strategy), [`src/candidates.nut`](https://github.com/matthewxbird/openttd_ai/blob/main/src/candidates.nut) (Route candidates)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_economics["Scanning and economics"]
  node_main["AI entry point<br/>[main.nut]"]
  node_cargo["Cargo scan<br/>[cargo_scan.nut]"]
  node_candidates["Route candidates<br/>[candidates.nut]"]
  node_estimator["Value estimator<br/>[estimator.nut]"]
  node_scoring["Route scoring<br/>[scoring.nut]"]
  node_strategy["Mode strategy<br/>[strategy.nut]"]
end

subgraph group_modes["Transport modes"]
  node_air["Aircraft shuttles<br/>[air.nut]"]
  node_road["Road routes<br/>[road.nut]"]
end

subgraph group_rail["Rail construction"]
  node_railroute["Rail routing<br/>[rail2_route.nut]"]
  node_backhaul["Rail backhaul<br/>[backhaul.nut]"]
  node_railpf["Rail pathfinding<br/>[rail_pf.nut]"]
  node_aystar["A* search<br/>[aystar.nut]"]
  node_stations["Station building"]
  node_tracks["Track building<br/>[track_builder.nut]"]
  node_signals["Rail signals<br/>[signals.nut]"]
  node_trains["Train building<br/>[trains.nut]"]
end

subgraph group_operations["Operations"]
  node_money["Money discipline<br/>[money.nut]"]
  node_state[("Route lifecycle<br/>[state.nut]")]
  node_maintenance["Route maintenance<br/>[maintenance.nut]"]
  node_autoreplace["Yearly autoreplace<br/>[autoreplace.nut]"]
  node_authority["Town authority<br/>[town_authority.nut]"]
end

subgraph group_diagnostics["Diagnostics"]
  node_diagnostics["Build diagnostics<br/>[build_diag.nut]"]
  node_mapdump["Annotated map dump<br/>[map_dump.nut]"]
end

node_openttd(("OpenTTD"))

node_openttd -->|"runs AI"| node_main
node_main -->|"scans cargo"| node_cargo
node_main -->|"scans routes"| node_candidates
node_candidates -->|"estimates value"| node_estimator
node_estimator -->|"ranks routes"| node_scoring
node_scoring -->|"selects mode"| node_strategy
node_strategy -->|"builds shuttle"| node_air
node_strategy -->|"builds route"| node_road
node_strategy -->|"builds route"| node_railroute
node_railroute -->|"adds return legs"| node_backhaul
node_railroute -->|"finds rail paths"| node_railpf
node_railpf -->|"uses search"| node_aystar
node_railroute -->|"builds stations"| node_stations
node_railroute -->|"builds track"| node_tracks
node_railroute -->|"places signals"| node_signals
node_railroute -->|"builds trains"| node_trains
node_main -->|"manages borrowing"| node_money
node_main -->|"tracks lifecycle"| node_state
node_main -->|"manages routes"| node_maintenance
node_main -->|"replaces vehicles"| node_autoreplace
node_main -->|"manages authority"| node_authority
node_main -->|"reports failures"| node_diagnostics
node_diagnostics -->|"dumps map"| node_mapdump

click node_main "https://github.com/matthewxbird/openttd_ai/blob/main/main.nut"
click node_cargo "https://github.com/matthewxbird/openttd_ai/blob/main/src/cargo_scan.nut"
click node_candidates "https://github.com/matthewxbird/openttd_ai/blob/main/src/candidates.nut"
click node_estimator "https://github.com/matthewxbird/openttd_ai/blob/main/src/estimator.nut"
click node_scoring "https://github.com/matthewxbird/openttd_ai/blob/main/src/scoring.nut"
click node_strategy "https://github.com/matthewxbird/openttd_ai/blob/main/src/strategy.nut"
click node_air "https://github.com/matthewxbird/openttd_ai/blob/main/src/air.nut"
click node_road "https://github.com/matthewxbird/openttd_ai/blob/main/src/road.nut"
click node_railroute "https://github.com/matthewxbird/openttd_ai/blob/main/src/rail2_route.nut"
click node_backhaul "https://github.com/matthewxbird/openttd_ai/blob/main/src/backhaul.nut"
click node_railpf "https://github.com/matthewxbird/openttd_ai/blob/main/src/rail_pf.nut"
click node_aystar "https://github.com/matthewxbird/openttd_ai/blob/main/src/aystar.nut"
click node_stations "https://github.com/matthewxbird/openttd_ai/blob/main/src/station_builder.nut"
click node_tracks "https://github.com/matthewxbird/openttd_ai/blob/main/src/track_builder.nut"
click node_signals "https://github.com/matthewxbird/openttd_ai/blob/main/src/signals.nut"
click node_trains "https://github.com/matthewxbird/openttd_ai/blob/main/src/trains.nut"
click node_money "https://github.com/matthewxbird/openttd_ai/blob/main/src/money.nut"
click node_state "https://github.com/matthewxbird/openttd_ai/blob/main/src/state.nut"
click node_maintenance "https://github.com/matthewxbird/openttd_ai/blob/main/src/maintenance.nut"
click node_autoreplace "https://github.com/matthewxbird/openttd_ai/blob/main/src/autoreplace.nut"
click node_authority "https://github.com/matthewxbird/openttd_ai/blob/main/src/town_authority.nut"
click node_diagnostics "https://github.com/matthewxbird/openttd_ai/blob/main/src/build_diag.nut"
click node_mapdump "https://github.com/matthewxbird/openttd_ai/blob/main/src/map_dump.nut"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_cargo,node_candidates,node_estimator,node_scoring,node_strategy toneBlue
class node_air,node_road toneAmber
class node_railroute,node_backhaul,node_railpf,node_aystar,node_stations,node_tracks,node_signals,node_trains toneMint
class node_money,node_state,node_maintenance,node_autoreplace,node_authority toneRose
class node_diagnostics,node_mapdump,node_openttd toneIndigo
```

## Components

### Scanning and economics

- [AI entry point](https://github.com/matthewxbird/openttd_ai/blob/main/main.nut): `main.nut`
- [Cargo scan](https://github.com/matthewxbird/openttd_ai/blob/main/src/cargo_scan.nut): `src/cargo_scan.nut`
- [Route candidates](https://github.com/matthewxbird/openttd_ai/blob/main/src/candidates.nut): `src/candidates.nut`
- [Value estimator](https://github.com/matthewxbird/openttd_ai/blob/main/src/estimator.nut): `src/estimator.nut`
- [Route scoring](https://github.com/matthewxbird/openttd_ai/blob/main/src/scoring.nut): `src/scoring.nut`
- [Mode strategy](https://github.com/matthewxbird/openttd_ai/blob/main/src/strategy.nut): `src/strategy.nut`

### Transport modes

- [Aircraft shuttles](https://github.com/matthewxbird/openttd_ai/blob/main/src/air.nut): `src/air.nut`
- [Road routes](https://github.com/matthewxbird/openttd_ai/blob/main/src/road.nut): `src/road.nut`

### Rail construction

- [Rail routing](https://github.com/matthewxbird/openttd_ai/blob/main/src/rail2_route.nut): `src/rail2_route.nut`
- [Rail backhaul](https://github.com/matthewxbird/openttd_ai/blob/main/src/backhaul.nut): `src/backhaul.nut`
- [Rail pathfinding](https://github.com/matthewxbird/openttd_ai/blob/main/src/rail_pf.nut): `src/rail_pf.nut`
- [A* search](https://github.com/matthewxbird/openttd_ai/blob/main/src/aystar.nut): `src/aystar.nut`
- [Station building](https://github.com/matthewxbird/openttd_ai/blob/main/src/station_builder.nut): `src/station_builder.nut`
- [Track building](https://github.com/matthewxbird/openttd_ai/blob/main/src/track_builder.nut): `src/track_builder.nut`
- [Rail signals](https://github.com/matthewxbird/openttd_ai/blob/main/src/signals.nut): `src/signals.nut`
- [Train building](https://github.com/matthewxbird/openttd_ai/blob/main/src/trains.nut): `src/trains.nut`

### Operations

- [Money discipline](https://github.com/matthewxbird/openttd_ai/blob/main/src/money.nut): `src/money.nut`
- [Route lifecycle](https://github.com/matthewxbird/openttd_ai/blob/main/src/state.nut): `src/state.nut`
- [Route maintenance](https://github.com/matthewxbird/openttd_ai/blob/main/src/maintenance.nut): `src/maintenance.nut`
- [Yearly autoreplace](https://github.com/matthewxbird/openttd_ai/blob/main/src/autoreplace.nut): `src/autoreplace.nut`
- [Town authority](https://github.com/matthewxbird/openttd_ai/blob/main/src/town_authority.nut): `src/town_authority.nut`

### Diagnostics

- [Build diagnostics](https://github.com/matthewxbird/openttd_ai/blob/main/src/build_diag.nut): `src/build_diag.nut`
- [Annotated map dump](https://github.com/matthewxbird/openttd_ai/blob/main/src/map_dump.nut): `src/map_dump.nut`

### Other components

- **OpenTTD**

## Connections

- OpenTTD → AI entry point: runs AI (evidence: `README.md`)
- AI entry point → Cargo scan: scans cargo (evidence: `README.md`)
- AI entry point → Route candidates: scans routes (evidence: `README.md`)
- Route candidates → Value estimator: estimates value (evidence: `README.md`)
- Value estimator → Route scoring: ranks routes (evidence: `README.md`)
- Route scoring → Mode strategy: selects mode (evidence: `README.md`)
- Mode strategy → Aircraft shuttles: builds shuttle (evidence: `README.md`)
- Mode strategy → Road routes: builds route (evidence: `README.md`)
- Mode strategy → Rail routing: builds route (evidence: `README.md`)
- Rail routing → Rail backhaul: adds return legs (evidence: `README.md`)
- Rail routing → Rail pathfinding: finds rail paths (evidence: `README.md`)
- Rail pathfinding → A* search: uses search (evidence: `README.md`)
- Rail routing → Station building: builds stations (evidence: `README.md`)
- Rail routing → Track building: builds track (evidence: `README.md`)
- Rail routing → Rail signals: places signals (evidence: `README.md`)
- Rail routing → Train building: builds trains (evidence: `README.md`)
- AI entry point → Money discipline: manages borrowing (evidence: `README.md`)
- AI entry point → Route lifecycle: tracks lifecycle (evidence: `README.md`)
- AI entry point → Route maintenance: manages routes (evidence: `README.md`)
- AI entry point → Yearly autoreplace: replaces vehicles (evidence: `README.md`)
- AI entry point → Town authority: manages authority (evidence: `README.md`)
- AI entry point → Build diagnostics: reports failures (evidence: `README.md`)
- Build diagnostics → Annotated map dump: dumps map (evidence: `README.md`)
