Metadata-Version: 2.5
Name: pyttd
Version: 2.0.0
Summary: Play OpenTTD from Python: a real OpenTTD engine, driven through the AI Script API
Project-URL: Source, https://github.com/mssc89/pyttd
Project-URL: AI API, https://docs.openttd.org/ai-api/
License-Expression: MIT
License-File: LICENSE
Classifier: Operating System :: MacOS
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Games/Entertainment :: Simulation
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.12
Requires-Dist: numpy>=1.26
Requires-Dist: packaging>=23
Requires-Dist: pyttd-engine>=15.3
Description-Content-Type: text/markdown

# pyttd

Finally, a way to play [OpenTTD](https://www.openttd.org) from Python. A real OpenTTD engine runs inside your Python process, and your code controls companies through OpenTTD own [AI Script API](https://docs.openttd.org/ai-api/) - the same functions, rules, costs and command rate as any Squirrel AI. Test host-paced local games, or join multiplayer servers as an ordinary player.

```
pip install pyttd
```

Linux (x86_64, aarch64), macOS (Apple silicon, Intel) and Windows (x64); Python 3.12+.

```python
import pyttd

game = pyttd.Game(seed=42)                 # a local game, advancing only when you step it
agent = game.create_agent()                # a new company, controlled from Python
api = agent.api                            # the AI Script API, acting for this company

town = max(api.AITownList(), key=api.AITown.GetPopulation)
print(api.AITown.GetName(town), api.AITown.GetPopulation(town))

api.AIRoad.SetCurrentRoadType(api.AIRoad.ROADTYPE_ROAD)
with api.AITestMode(), api.AIAccounting() as cost:
    api.AIRoad.BuildRoad(a, b)             # estimate only: cost.GetCosts()
api.AIRoad.BuildRoad(a, b)                 # build; waits for the command slot like an AI
game.step(30 * pyttd.TICKS_PER_DAY)        # let a month pass
```

## How it works

![One command, from Python through OpenTTD to a multiplayer server and back](assets/command-flow.svg)

The engine is OpenTTD compiled as a Python extension module. Calls go through its own Script API in the agent's script context, so a command behaves exactly as it would for a Squirrel AI. For multiplayer servers, if a command is issued too early, it waits (or raises `BlockingIOError` with `create_agent(auto_wait=False)`).

## Multiplayer

```python
game = pyttd.Game.join("example.org:3979", name="bot")   # create a company
me = game.create_agent()
```

The client is an ordinary player - no admin port access, or server-side AI slot are needed, and the game runs at the server's pace (`step()` waits). `pyttd.server.query(address)` reads a server's name, version, map and player counts without joining.

## OpenTTD versions

A server only accepts clients of its exact OpenTTD version, and pyttd handles that for you. `pip install pyttd` brings the engine of the newest OpenTTD release. `Game.join` asks the server for its version first and, if it differs, downloads the matching engine once into pyttd's data directory. To run a given version locally, pass `Game(version="15.3")`, call `pyttd.engines.use("15.3")`, or set `$PYTTD_ENGINE`. One process runs one engine - you can start several processes for several games or versions.

## More

- `pyttd.observe`: the map as numpy arrays, and tables of towns, industries, vehicles, stations and finances.
- `pyttd.roads`, `pyttd.rails`: A\* pathfinders with bridges and tunnels, and builders for their paths.
- `pyttd.record`: every command scripts execute, with decoded arguments and results; `Agent.do_command` issues them again.
- `pyttd.content`: download Squirrel AIs from BaNaNaS and start them as opponents.
- `Game`: `new_game`, `load`, `save`, `console`, and `observe_company` (a read-only view of any company).

See [`examples/`](examples).

## Building

```
python engine/tools/build.py [--version 15.3] [--server]   # wheel -> engine/dist/
```

Supported OpenTTD versions are listed in `engine/versions.toml`.

## Licence

pyttd is MIT licensed. The engine, `pyttd-engine`, contains OpenTTD and OpenGFX and follows them with GPL-2.0 licensing.
