Customizing AI Route Logic
Relevant source files
The following files were used as context for generating this wiki page:
Customizing AI Route Logic in Trans AI involves configuring how the agent selects transport modes, establishes connections between towns and industries, and manages specific vehicle types. The AI is designed for OpenTTD and implements a strategic approach to cargo transport using a "Cargo Concept" that prioritizes many-to-one destination mappings.
Sources: README.md:3-8, ROADMAP.md:32-34
The logic is modularized across different transport domains, including Air, Road, Marine, and Rail routes. Developers can influence the AI's behavior by modifying its handling of infrastructure maintenance, industry-to-industry chains, and specific vehicle operational constraints such as double-track requirements for rail.
Sources: ROADMAP.md:4-30
Transport Mode Strategy
Trans AI employs distinct logic for each transport medium. The high-level strategy focuses on transporting passengers and freight (mail and cargo) across different network types.
Route Classification
| Route Type | Primary Function | Key Components | Status |
|---|---|---|---|
| Air | Town-to-town passenger transport | Airports, Airplanes, Choppers | Implemented |
| Road | Town-to-town and Industry-to-industry | Buses, Trucks | Implemented |
| Tram | Town-to-town and Industry-to-industry | Tram-buses, Tram-trucks | Implemented |
| Marine | Passenger and Industry freight | Ships, Buoys | Implemented |
| Rail | Industry raw cargo transport | Trains, Double/Single tracks | Partially Implemented |
Sources: ROADMAP.md:4-30
Logic Flow for Route Initialization
The AI follows a specific sequence when determining how to build and maintain routes based on game settings and available assets.
The logic for rail specifically checks for buildability of double tracks before falling back to single tracks with a two-train limit.
Sources: ROADMAP.md:27-30, ROADMAP.md:46-48
Cargo Concept and Destination Logic
A core feature of the AI route logic is the Cargo Concept (Many-to-one). This logic dictates that the AI shall attempt to use only one specific industry type as the destination for transporting a particular cargo type.
Implementation Status
- Current State: The system is being reworked for Road and Marine routes.
- Goal: Streamline logistics to reduce network complexity and maximize efficiency at single hubs.
Sources: ROADMAP.md:32-39
Destination Selection Flow
Sources: README.md:3-8, ROADMAP.md:32-34
Specialized Vehicle Logic
Different vehicles require unique pathfinding or operational logic within the AI script.
Marine and Air Operations
- Marine Routes: The AI utilizes buoys to establish proper routing for ships, ensuring they can navigate between ports effectively.
- Chopper Service: Logic is restricted to town-to-oil-rig transport for passengers, distinct from standard airplane town-to-town logic.
- Air Routes: Infrastructure maintenance awareness is critical; the AI detects if this setting is enabled and adjusts its airport/aircraft deployment accordingly.
Sources: ROADMAP.md:7-9, ROADMAP.md:18-21, ROADMAP.md:46-48
Rail Logic Constraints
The rail module handles raw cargo between industries. It includes a specific fallback mechanism for track types:
- Attempt to build double tracks for high capacity.
- If buildable area is restricted, use single tracks.
- Enforce a hard limit of two operating trains on single-track sections to prevent deadlocks.
Sources: ROADMAP.md:23-30
External Dependencies and APIs
The AI route logic relies on standard OpenTTD AI libraries. These must be present in the ../ai/library directory for the custom logic to execute correctly:
- AILibCommon: General AI utilities.
- AILibList: Data structure management for industries/towns.
- AILibString: String manipulation for naming and identification.
- CargoDist API: Future support is planned to allow the AI to integrate with the native OpenTTD CargoDist game mode.
Sources: README.md:37-43, ROADMAP.md:50-51
The AI's versioning (rolling release) ensures that the most recent logic updates are prioritized by the game engine when using a local development clone by setting the version date far into the future in info.nut.
Sources: README.md:45-53