PyTTD - Python OpenTTD Client Library
Finally, a Python client library for connecting to OpenTTD servers as a player, issuing commands, observing game state and parsing maps! Create bots, manage companies, and interact with OpenTTD games programmatically with real-time data and without admin port access.
Features
| Feature | Status | Remarks |
|---|---|---|
| Multiplayer protocol | - | |
| Commands | Might have missed something | |
| Game state | Might have missed something | |
| Save file parsing | Game data and companies work | |
| High level functions | Helpers for common high-level tasks |
Requirements
- Python: 3.11 or higher
- OpenTTD Server: Tested with 14.1
Installation
From PyPI (Recommended)
pip install pyttd
From Source
git clone https://github.com/mssc89/pyttd.git
cd pyttd
pip install -e .
Quick Start
from pyttd import OpenTTDClient
# Connect to OpenTTD server
client = OpenTTDClient("127.0.0.1", 3979, player_name="MyBot")
client.connect()
# Get real-time game information
game_info = client.get_game_info()
print(f"Game Year: {game_info['current_year']}")
print(f"Companies: {game_info['companies']}/{game_info['companies_max']}")
print(f"Clients: {game_info['clients']}/{game_info['clients_max']}")
# Company management
if client.get_our_company():
finances = client.get_company_finances()
print(f"Money: £{finances['money']:,}")
print(f"Loan: £{finances['loan']:,}")
# Take a loan and send a status message
client.increase_loan(50000)
client.send_chat("Bot taking loan for expansion!")
# Clean disconnect
client.disconnect()
Save File Parsing
PyTTD includes a save file parser module that can extract detailed game data from OpenTTD save files.
from pyttd import load_save_file
# Parse a save file
game_data = load_save_file("path/to/savefile.sav")
# Access parsed data
print(f"Save version: {game_data['meta']['save_version']}")
print(f"Map size: {game_data['statistics']['map_size']}")
print(f"Companies: {game_data['statistics']['companies_count']}")
# Company information with financial data
for company in game_data['companies']:
print(f"{company['name']}: £{company['money']:,} (AI: {company['is_ai']})")
# Game date and economy
date = game_data['game']['date']['calendar_date']
print(f"Game date: {date['year']}-{date['month']}-{date['day']}")
economy = game_data['game']['economy']
print(f"Interest rate: {economy['interest_rate']}%")
Examples
Data Monitor
python examples/data_display.py
Displays all available real-time game state information.
Chat Bot
python examples/chat_bot.py
Basic example showing connection, company creation, and chat interaction.
Company Manager
python examples/manager_bot.py
Demonstrates company management features and financial tracking.
Financial Manager
python examples/finance_bot.py
Interactive financial management with chat-based commands.
Save File Parser
python examples/save_file_parser.py path/to/savefile.sav
Parse OpenTTD save files to extract game data including companies, map information, and economic data. All in a clean JSON file!
API Reference
OpenTTDClient
The main client class for connecting to OpenTTD servers.
client = OpenTTDClient(
server="127.0.0.1", # Server IP address
port=3979, # Server port
player_name="MyBot", # Your bot's name
company_name="MyCompany" # Company name (auto-created)
)
Connection Methods
client.connect()- Connect to server and join gameclient.disconnect()- Clean disconnect from serverclient.is_connected()- Check connection status
Game Information
client.get_game_info()- Game state informationclient.get_map_info()- Map size and terrain dataclient.get_economic_status()- Economic indicators
Company Management
client.get_companies()- List all companiesclient.get_our_company()- Our company informationclient.get_company_finances()- Financial dataclient.get_company_performance()- Performance metrics
Financial Operations
client.increase_loan(amount)- Increase company loanclient.decrease_loan(amount)- Decrease company loanclient.give_money(amount, company)- Transfer moneyclient.can_afford(amount)- Check affordability
Company Customization
client.rename_company(name)- Change company nameclient.rename_president(name)- Change manager nameclient.set_company_colour(scheme, primary, colour)- Change colors
Communication
client.send_chat(message)- Send public chat messageclient.send_chat_to_company(message, company_id)- Company chat
Maps
TODO: describe it here
Development
Setting up Development Environment
git clone https://github.com/mssc89/pyttd.git
cd pyttd
# Install development dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Code formatting
black pyttd/
flake8 pyttd/
# Type checking
mypy pyttd/
Support
- GitHub Issues: Report bugs or request features
- Documentation: Full API documentation
- Examples: Comprehensive examples
Metadata
Release files for pyttd 1.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pyttd-1.3.0.tar.gz | 73.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pyttd-1.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 131.1 kB
Release files / pyttd-1.3.0.tar.gz
| Download URL | pyttd-1.3.0.tar.gz |
|---|---|
| Size | 73.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
986aea8ae2853df9ad20526e671bd4550a25097fb385e47732cc25ded3234f80
|
|
BLAKE2b-256 checksum How to use checksums |
510037c18f8481629bfe2ba5becfe8187ca0f0c9ad2172e4bb110c53fc401b93
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Release files / pyttd-1.3.0-py3-none-any.whl
| Download URL | pyttd-1.3.0-py3-none-any.whl |
|---|---|
| Size | 57.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
61ad048c477a64180b8298a6fdc4b68ce162110f83746b84c591e70f1c01c318
|
|
BLAKE2b-256 checksum How to use checksums |
4012e8ccfae8ff6ce23350b20d0a52712bb35ed677c579c77e0ea2218910fe3a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|