Skip to main content

OpenTTD Client Library

Project description

PyTTD - Python OpenTTD Client Library

PyPI version Python versions License: MIT

Finally, a Python client library for connecting to OpenTTD servers as a player and parsing maps! Create AI bots, manage companies, and interact with OpenTTD games programmatically with real-time data and without admin port access.

Features

Feature Status Remarks
Multiplayer protocol Done -
Commands Done Might have missed some
Game state Done As far as possible without save parsing
Save file parsing In Progress Game data and companies work
High level functions In Progress 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 game
  • client.disconnect() - Clean disconnect from server
  • client.is_connected() - Check connection status

Game Information

  • client.get_game_info() - Game state information
  • client.get_map_info() - Map size and terrain data
  • client.get_economic_status() - Economic indicators

Company Management

  • client.get_companies() - List all companies
  • client.get_our_company() - Our company information
  • client.get_company_finances() - Financial data
  • client.get_company_performance() - Performance metrics

Financial Operations

  • client.increase_loan(amount) - Increase company loan
  • client.decrease_loan(amount) - Decrease company loan
  • client.give_money(amount, company) - Transfer money
  • client.can_afford(amount) - Check affordability

Company Customization

  • client.rename_company(name) - Change company name
  • client.rename_president(name) - Change manager name
  • client.set_company_colour(scheme, primary, colour) - Change colors

Communication

  • client.send_chat(message) - Send public chat message
  • client.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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyttd-1.1.0.tar.gz (48.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyttd-1.1.0-py3-none-any.whl (38.6 kB view details)

Uploaded Python 3

File details

Details for the file pyttd-1.1.0.tar.gz.

File metadata

  • Download URL: pyttd-1.1.0.tar.gz
  • Upload date:
  • Size: 48.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pyttd-1.1.0.tar.gz
Algorithm Hash digest
SHA256 3b88adb369c8f316909b8a148b3a669573b56c62d5ff8c93277fdcb058d3f091
MD5 16ea52f2cc02458af2b44f24e359cedb
BLAKE2b-256 3c98a41f38638e99602bbbd392f81c2274943a768f7692d5a4af21f050137b6c

See more details on using hashes here.

File details

Details for the file pyttd-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyttd-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 38.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pyttd-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 89edcfbfdbb6372ecf717a3693eaa9b67962205fa20c9796fea7ecdbfdc6093b
MD5 33c809e94b07a08826cd904ad04e3807
BLAKE2b-256 f0eeadb836cd5d34527b654bda2355698255f34e9cebf071acf2a0b449bf8ad0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page