pyttd 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of pyttd might be problematic. Click here for more details.
- pyttd-1.0.0/.github/workflows/ci.yml +55 -0
- pyttd-1.0.0/.github/workflows/docs.yml +41 -0
- pyttd-1.0.0/.github/workflows/publish.yml +33 -0
- pyttd-1.0.0/LICENSE +21 -0
- pyttd-1.0.0/MANIFEST.in +22 -0
- pyttd-1.0.0/PKG-INFO +253 -0
- pyttd-1.0.0/README.md +213 -0
- pyttd-1.0.0/examples/README.md +31 -0
- pyttd-1.0.0/examples/chat_bot.py +149 -0
- pyttd-1.0.0/examples/data_display.py +421 -0
- pyttd-1.0.0/examples/finance_bot.py +234 -0
- pyttd-1.0.0/examples/manager_bot.py +342 -0
- pyttd-1.0.0/pyproject.toml +97 -0
- pyttd-1.0.0/pyttd/__init__.py +44 -0
- pyttd-1.0.0/pyttd/client.py +1356 -0
- pyttd-1.0.0/pyttd/commands.py +415 -0
- pyttd-1.0.0/pyttd/game_state.py +344 -0
- pyttd-1.0.0/pyttd/protocol.py +341 -0
- pyttd-1.0.0/pyttd/py.typed +0 -0
- pyttd-1.0.0/pyttd.egg-info/PKG-INFO +253 -0
- pyttd-1.0.0/pyttd.egg-info/SOURCES.txt +26 -0
- pyttd-1.0.0/pyttd.egg-info/dependency_links.txt +1 -0
- pyttd-1.0.0/pyttd.egg-info/entry_points.txt +2 -0
- pyttd-1.0.0/pyttd.egg-info/not-zip-safe +1 -0
- pyttd-1.0.0/pyttd.egg-info/requires.txt +10 -0
- pyttd-1.0.0/pyttd.egg-info/top_level.txt +1 -0
- pyttd-1.0.0/setup.cfg +4 -0
- pyttd-1.0.0/setup.py +89 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [ main, develop ]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [ main ]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
runs-on: ${{ matrix.os }}
|
|
12
|
+
strategy:
|
|
13
|
+
matrix:
|
|
14
|
+
os: [ubuntu-latest, windows-latest, macos-latest]
|
|
15
|
+
python-version: ["3.11", "3.12"]
|
|
16
|
+
|
|
17
|
+
steps:
|
|
18
|
+
- uses: actions/checkout@v4
|
|
19
|
+
|
|
20
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
21
|
+
uses: actions/setup-python@v4
|
|
22
|
+
with:
|
|
23
|
+
python-version: ${{ matrix.python-version }}
|
|
24
|
+
|
|
25
|
+
- name: Install dependencies
|
|
26
|
+
run: |
|
|
27
|
+
python -m pip install --upgrade pip
|
|
28
|
+
pip install -e ".[dev]"
|
|
29
|
+
|
|
30
|
+
- name: Lint with flake8
|
|
31
|
+
run: |
|
|
32
|
+
# stop the build if there are Python syntax errors or undefined names
|
|
33
|
+
flake8 pyttd --count --select=E9,F63,F7,F82 --show-source --statistics
|
|
34
|
+
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
|
|
35
|
+
flake8 pyttd --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
|
|
36
|
+
|
|
37
|
+
- name: Check formatting with black
|
|
38
|
+
run: |
|
|
39
|
+
black --check pyttd/
|
|
40
|
+
|
|
41
|
+
- name: Type check with mypy
|
|
42
|
+
run: |
|
|
43
|
+
mypy pyttd/
|
|
44
|
+
|
|
45
|
+
- name: Test with pytest
|
|
46
|
+
run: |
|
|
47
|
+
pytest --cov=pyttd --cov-report=xml
|
|
48
|
+
|
|
49
|
+
- name: Upload coverage to Codecov
|
|
50
|
+
uses: codecov/codecov-action@v3
|
|
51
|
+
with:
|
|
52
|
+
file: ./coverage.xml
|
|
53
|
+
flags: unittests
|
|
54
|
+
name: codecov-umbrella
|
|
55
|
+
fail_ci_if_error: false
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
name: Documentation
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [ main ]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [ main ]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
docs:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v4
|
|
15
|
+
|
|
16
|
+
- name: Set up Python
|
|
17
|
+
uses: actions/setup-python@v4
|
|
18
|
+
with:
|
|
19
|
+
python-version: "3.11"
|
|
20
|
+
|
|
21
|
+
- name: Install dependencies
|
|
22
|
+
run: |
|
|
23
|
+
python -m pip install --upgrade pip
|
|
24
|
+
pip install -e ".[dev]"
|
|
25
|
+
|
|
26
|
+
- name: Test examples
|
|
27
|
+
run: |
|
|
28
|
+
# Test that examples can be imported without errors
|
|
29
|
+
python -c "import sys; sys.path.append('examples'); import data_display"
|
|
30
|
+
python -c "import sys; sys.path.append('examples'); import chat_bot"
|
|
31
|
+
|
|
32
|
+
- name: Check README links
|
|
33
|
+
run: |
|
|
34
|
+
# Simple check that README doesn't have broken markdown
|
|
35
|
+
python -c "
|
|
36
|
+
with open('README.md', 'r') as f:
|
|
37
|
+
content = f.read()
|
|
38
|
+
assert '# PyTTD' in content
|
|
39
|
+
assert 'pip install pyttd' in content
|
|
40
|
+
print('README.md looks good')
|
|
41
|
+
"
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
build:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
|
|
11
|
+
steps:
|
|
12
|
+
- uses: actions/checkout@v4
|
|
13
|
+
|
|
14
|
+
- name: Set up Python
|
|
15
|
+
uses: actions/setup-python@v4
|
|
16
|
+
with:
|
|
17
|
+
python-version: "3.11"
|
|
18
|
+
|
|
19
|
+
- name: Install build dependencies
|
|
20
|
+
run: |
|
|
21
|
+
python -m pip install --upgrade pip
|
|
22
|
+
pip install build twine
|
|
23
|
+
|
|
24
|
+
- name: Build package
|
|
25
|
+
run: python -m build
|
|
26
|
+
|
|
27
|
+
- name: Check package
|
|
28
|
+
run: twine check dist/*
|
|
29
|
+
|
|
30
|
+
- name: Publish to PyPI
|
|
31
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
32
|
+
with:
|
|
33
|
+
password: ${{ secrets.PYPI_API_TOKEN }}
|
pyttd-1.0.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 PyTTD Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
pyttd-1.0.0/MANIFEST.in
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
include README.md
|
|
2
|
+
include LICENSE
|
|
3
|
+
include MANIFEST.in
|
|
4
|
+
include pyproject.toml
|
|
5
|
+
|
|
6
|
+
# Include examples
|
|
7
|
+
recursive-include examples *.py
|
|
8
|
+
include examples/README.md
|
|
9
|
+
|
|
10
|
+
# Exclude development files
|
|
11
|
+
exclude *.pyc
|
|
12
|
+
exclude .gitignore
|
|
13
|
+
exclude .github/*
|
|
14
|
+
recursive-exclude * __pycache__
|
|
15
|
+
recursive-exclude * *.py[co]
|
|
16
|
+
|
|
17
|
+
# Exclude tests from distribution
|
|
18
|
+
prune tests
|
|
19
|
+
|
|
20
|
+
# Include package data
|
|
21
|
+
recursive-include pyttd *.py
|
|
22
|
+
include pyttd/py.typed
|
pyttd-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pyttd
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: OpenTTD Client Library
|
|
5
|
+
Home-page: https://github.com/mssc89/pyttd
|
|
6
|
+
Author: mssc89
|
|
7
|
+
Author-email: mssc89 <pyttd@example.com>
|
|
8
|
+
License: MIT
|
|
9
|
+
Project-URL: Homepage, https://github.com/mssc89/pyttd
|
|
10
|
+
Project-URL: Bug Tracker, https://github.com/mssc89/pyttd/issues
|
|
11
|
+
Project-URL: Documentation, https://github.com/mssc89/pyttd#readme
|
|
12
|
+
Project-URL: Source Code, https://github.com/mssc89/pyttd
|
|
13
|
+
Keywords: openttd,transport,tycoon,simulation,game,ai,bot,client,network,protocol,multiplayer,automation
|
|
14
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Topic :: Games/Entertainment :: Simulation
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
23
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
|
|
24
|
+
Classifier: Topic :: System :: Networking
|
|
25
|
+
Requires-Python: >=3.11
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Provides-Extra: dev
|
|
29
|
+
Requires-Dist: pytest>=6.0; extra == "dev"
|
|
30
|
+
Requires-Dist: pytest-cov>=2.0; extra == "dev"
|
|
31
|
+
Requires-Dist: black>=21.0; extra == "dev"
|
|
32
|
+
Requires-Dist: flake8>=3.8; extra == "dev"
|
|
33
|
+
Requires-Dist: mypy>=0.900; extra == "dev"
|
|
34
|
+
Provides-Extra: examples
|
|
35
|
+
Requires-Dist: colorama>=0.4.4; extra == "examples"
|
|
36
|
+
Dynamic: author
|
|
37
|
+
Dynamic: home-page
|
|
38
|
+
Dynamic: license-file
|
|
39
|
+
Dynamic: requires-python
|
|
40
|
+
|
|
41
|
+
# PyTTD - Python OpenTTD Client Library
|
|
42
|
+
|
|
43
|
+
[](https://badge.fury.io/py/pyttd)
|
|
44
|
+
[](https://pypi.org/project/pyttd/)
|
|
45
|
+
[](https://opensource.org/licenses/MIT)
|
|
46
|
+
|
|
47
|
+
A Python client library for connecting to [OpenTTD](https://www.openttd.org/) servers. Create AI bots, manage companies, and interact with OpenTTD games programmatically with **real-time data** and **without admin port**.
|
|
48
|
+
|
|
49
|
+
## Features
|
|
50
|
+
|
|
51
|
+
todo
|
|
52
|
+
|
|
53
|
+
## Installation
|
|
54
|
+
|
|
55
|
+
### From PyPI (Recommended)
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
pip install pyttd
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### From Source
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
git clone https://github.com/mssc89/pyttd.git
|
|
65
|
+
cd pyttd
|
|
66
|
+
pip install -e .
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Quick Start
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
from pyttd import OpenTTDClient
|
|
73
|
+
|
|
74
|
+
# Connect to OpenTTD server
|
|
75
|
+
client = OpenTTDClient("127.0.0.1", 3979, player_name="MyBot")
|
|
76
|
+
client.connect()
|
|
77
|
+
|
|
78
|
+
# Get real-time game information
|
|
79
|
+
game_info = client.get_game_info()
|
|
80
|
+
print(f"Game Year: {game_info['current_year']}")
|
|
81
|
+
print(f"Companies: {game_info['companies']}/{game_info['companies_max']}")
|
|
82
|
+
print(f"Clients: {game_info['clients']}/{game_info['clients_max']}")
|
|
83
|
+
|
|
84
|
+
# Company management
|
|
85
|
+
if client.get_our_company():
|
|
86
|
+
finances = client.get_company_finances()
|
|
87
|
+
print(f"Money: £{finances['money']:,}")
|
|
88
|
+
print(f"Loan: £{finances['loan']:,}")
|
|
89
|
+
|
|
90
|
+
# Take a loan and send a status message
|
|
91
|
+
client.increase_loan(50000)
|
|
92
|
+
client.send_chat("Bot taking loan for expansion!")
|
|
93
|
+
|
|
94
|
+
# Clean disconnect
|
|
95
|
+
client.disconnect()
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Real-Time Data Features
|
|
99
|
+
|
|
100
|
+
PyTTD provides real-time data that matches what other players see in the OpenTTD GUI:
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
from pyttd import OpenTTDClient
|
|
104
|
+
|
|
105
|
+
client = OpenTTDClient()
|
|
106
|
+
client.connect()
|
|
107
|
+
|
|
108
|
+
# Real-time game state
|
|
109
|
+
game_info = client.get_game_info()
|
|
110
|
+
print(f"Current Game Year: {game_info['current_year']}") # e.g., 1964
|
|
111
|
+
print(f"Game Started: {game_info['start_year']}") # e.g., 1950
|
|
112
|
+
print(f"Companies Active: {game_info['companies']}") # e.g., 8/15
|
|
113
|
+
print(f"Players Online: {game_info['clients']}") # e.g., 12/25
|
|
114
|
+
|
|
115
|
+
# Company information
|
|
116
|
+
companies = client.get_companies()
|
|
117
|
+
for company_id, company in companies.items():
|
|
118
|
+
print(f"Company {company_id}: {company['name']}")
|
|
119
|
+
|
|
120
|
+
# Financial analysis
|
|
121
|
+
finances = client.get_company_finances()
|
|
122
|
+
performance = client.get_company_performance()
|
|
123
|
+
print(f"Net Worth: £{finances['net_worth']:,}")
|
|
124
|
+
print(f"Company Value: £{performance['company_value']:,}")
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Examples
|
|
128
|
+
|
|
129
|
+
### Data Monitor
|
|
130
|
+
```bash
|
|
131
|
+
python examples/data_display.py
|
|
132
|
+
```
|
|
133
|
+
Displays all available real-time game state information in a clean, organized format.
|
|
134
|
+
|
|
135
|
+
### Chat Bot
|
|
136
|
+
```bash
|
|
137
|
+
python examples/chat_bot.py
|
|
138
|
+
```
|
|
139
|
+
Basic example showing connection, company creation, and chat interaction.
|
|
140
|
+
|
|
141
|
+
### Company Manager
|
|
142
|
+
```bash
|
|
143
|
+
python examples/manager_bot.py
|
|
144
|
+
```
|
|
145
|
+
Demonstrates company management features and financial tracking.
|
|
146
|
+
|
|
147
|
+
### Financial Manager
|
|
148
|
+
```bash
|
|
149
|
+
python examples/finance_bot.py
|
|
150
|
+
```
|
|
151
|
+
Interactive financial management with chat-based commands.
|
|
152
|
+
|
|
153
|
+
## API Reference
|
|
154
|
+
|
|
155
|
+
### OpenTTDClient
|
|
156
|
+
|
|
157
|
+
The main client class for connecting to OpenTTD servers.
|
|
158
|
+
|
|
159
|
+
```python
|
|
160
|
+
client = OpenTTDClient(
|
|
161
|
+
server="127.0.0.1", # Server IP address
|
|
162
|
+
port=3979, # Server port
|
|
163
|
+
player_name="MyBot", # Your bot's name
|
|
164
|
+
company_name="MyCompany" # Company name (auto-created)
|
|
165
|
+
)
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
#### Connection Methods
|
|
169
|
+
- `client.connect()` - Connect to server and join game
|
|
170
|
+
- `client.disconnect()` - Clean disconnect from server
|
|
171
|
+
- `client.is_connected()` - Check connection status
|
|
172
|
+
|
|
173
|
+
#### Game Information
|
|
174
|
+
- `client.get_game_info()` - Complete game state information
|
|
175
|
+
- `client.get_map_info()` - Map size and terrain data
|
|
176
|
+
- `client.get_economic_status()` - Economic indicators
|
|
177
|
+
|
|
178
|
+
#### Company Management
|
|
179
|
+
- `client.get_companies()` - List all companies
|
|
180
|
+
- `client.get_our_company()` - Our company information
|
|
181
|
+
- `client.get_company_finances()` - Financial data
|
|
182
|
+
- `client.get_company_performance()` - Performance metrics
|
|
183
|
+
|
|
184
|
+
#### Financial Operations
|
|
185
|
+
- `client.increase_loan(amount)` - Increase company loan
|
|
186
|
+
- `client.decrease_loan(amount)` - Decrease company loan
|
|
187
|
+
- `client.give_money(amount, company)` - Transfer money
|
|
188
|
+
- `client.can_afford(amount)` - Check affordability
|
|
189
|
+
|
|
190
|
+
#### Company Customization
|
|
191
|
+
- `client.rename_company(name)` - Change company name
|
|
192
|
+
- `client.rename_president(name)` - Change manager name
|
|
193
|
+
- `client.set_company_colour(scheme, primary, colour)` - Change colors
|
|
194
|
+
|
|
195
|
+
#### Communication
|
|
196
|
+
- `client.send_chat(message)` - Send public chat message
|
|
197
|
+
- `client.send_chat_to_company(message, company_id)` - Company chat
|
|
198
|
+
- `client.broadcast_status()` - Broadcast bot status
|
|
199
|
+
|
|
200
|
+
#### Vehicle Management
|
|
201
|
+
- `client.get_vehicles()` - List all vehicles
|
|
202
|
+
- `client.get_our_vehicles()` - Our company's vehicles
|
|
203
|
+
- `client.get_vehicle_statistics()` - Vehicle performance data
|
|
204
|
+
|
|
205
|
+
## Requirements
|
|
206
|
+
|
|
207
|
+
- **Python**: 3.11 or higher
|
|
208
|
+
- **OpenTTD Server**: Tested with 14.1
|
|
209
|
+
|
|
210
|
+
## Development
|
|
211
|
+
|
|
212
|
+
### Setting up Development Environment
|
|
213
|
+
|
|
214
|
+
```bash
|
|
215
|
+
git clone https://github.com/mssc89/pyttd.git
|
|
216
|
+
cd pyttd
|
|
217
|
+
|
|
218
|
+
# Install development dependencies
|
|
219
|
+
pip install -e ".[dev]"
|
|
220
|
+
|
|
221
|
+
# Run tests
|
|
222
|
+
pytest
|
|
223
|
+
|
|
224
|
+
# Code formatting
|
|
225
|
+
black pyttd/
|
|
226
|
+
flake8 pyttd/
|
|
227
|
+
|
|
228
|
+
# Type checking
|
|
229
|
+
mypy pyttd/
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## Protocol Implementation
|
|
233
|
+
|
|
234
|
+
PyTTD implements OpenTTD's complete network protocol including:
|
|
235
|
+
|
|
236
|
+
- **CLIENT_JOIN** and **CLIENT_GAME_INFO** packets for connection
|
|
237
|
+
- **SERVER_GAME_INFO** parsing for real-time data synchronization
|
|
238
|
+
- **CLIENT_COMMAND** for all game operations (construction, management, etc.)
|
|
239
|
+
- **SERVER_FRAME** handling for game synchronization
|
|
240
|
+
- **CLIENT_CHAT** and **SERVER_CHAT** for communication
|
|
241
|
+
|
|
242
|
+
The library automatically handles:
|
|
243
|
+
- Map downloading and decompression
|
|
244
|
+
- Game state synchronization
|
|
245
|
+
- Command validation and encoding
|
|
246
|
+
- Network packet parsing and generation
|
|
247
|
+
- Connection management and error handling
|
|
248
|
+
|
|
249
|
+
## Support
|
|
250
|
+
|
|
251
|
+
- **GitHub Issues**: [Report bugs or request features](https://github.com/mssc89/pyttd/issues)
|
|
252
|
+
- **Documentation**: [Full API documentation](https://github.com/mssc89/pyttd#readme)
|
|
253
|
+
- **Examples**: [Comprehensive examples](https://github.com/mssc89/pyttd/tree/main/examples)
|
pyttd-1.0.0/README.md
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
# PyTTD - Python OpenTTD Client Library
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/py/pyttd)
|
|
4
|
+
[](https://pypi.org/project/pyttd/)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
7
|
+
A Python client library for connecting to [OpenTTD](https://www.openttd.org/) servers. Create AI bots, manage companies, and interact with OpenTTD games programmatically with **real-time data** and **without admin port**.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
todo
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
### From PyPI (Recommended)
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip install pyttd
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### From Source
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
git clone https://github.com/mssc89/pyttd.git
|
|
25
|
+
cd pyttd
|
|
26
|
+
pip install -e .
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Quick Start
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
from pyttd import OpenTTDClient
|
|
33
|
+
|
|
34
|
+
# Connect to OpenTTD server
|
|
35
|
+
client = OpenTTDClient("127.0.0.1", 3979, player_name="MyBot")
|
|
36
|
+
client.connect()
|
|
37
|
+
|
|
38
|
+
# Get real-time game information
|
|
39
|
+
game_info = client.get_game_info()
|
|
40
|
+
print(f"Game Year: {game_info['current_year']}")
|
|
41
|
+
print(f"Companies: {game_info['companies']}/{game_info['companies_max']}")
|
|
42
|
+
print(f"Clients: {game_info['clients']}/{game_info['clients_max']}")
|
|
43
|
+
|
|
44
|
+
# Company management
|
|
45
|
+
if client.get_our_company():
|
|
46
|
+
finances = client.get_company_finances()
|
|
47
|
+
print(f"Money: £{finances['money']:,}")
|
|
48
|
+
print(f"Loan: £{finances['loan']:,}")
|
|
49
|
+
|
|
50
|
+
# Take a loan and send a status message
|
|
51
|
+
client.increase_loan(50000)
|
|
52
|
+
client.send_chat("Bot taking loan for expansion!")
|
|
53
|
+
|
|
54
|
+
# Clean disconnect
|
|
55
|
+
client.disconnect()
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Real-Time Data Features
|
|
59
|
+
|
|
60
|
+
PyTTD provides real-time data that matches what other players see in the OpenTTD GUI:
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
from pyttd import OpenTTDClient
|
|
64
|
+
|
|
65
|
+
client = OpenTTDClient()
|
|
66
|
+
client.connect()
|
|
67
|
+
|
|
68
|
+
# Real-time game state
|
|
69
|
+
game_info = client.get_game_info()
|
|
70
|
+
print(f"Current Game Year: {game_info['current_year']}") # e.g., 1964
|
|
71
|
+
print(f"Game Started: {game_info['start_year']}") # e.g., 1950
|
|
72
|
+
print(f"Companies Active: {game_info['companies']}") # e.g., 8/15
|
|
73
|
+
print(f"Players Online: {game_info['clients']}") # e.g., 12/25
|
|
74
|
+
|
|
75
|
+
# Company information
|
|
76
|
+
companies = client.get_companies()
|
|
77
|
+
for company_id, company in companies.items():
|
|
78
|
+
print(f"Company {company_id}: {company['name']}")
|
|
79
|
+
|
|
80
|
+
# Financial analysis
|
|
81
|
+
finances = client.get_company_finances()
|
|
82
|
+
performance = client.get_company_performance()
|
|
83
|
+
print(f"Net Worth: £{finances['net_worth']:,}")
|
|
84
|
+
print(f"Company Value: £{performance['company_value']:,}")
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Examples
|
|
88
|
+
|
|
89
|
+
### Data Monitor
|
|
90
|
+
```bash
|
|
91
|
+
python examples/data_display.py
|
|
92
|
+
```
|
|
93
|
+
Displays all available real-time game state information in a clean, organized format.
|
|
94
|
+
|
|
95
|
+
### Chat Bot
|
|
96
|
+
```bash
|
|
97
|
+
python examples/chat_bot.py
|
|
98
|
+
```
|
|
99
|
+
Basic example showing connection, company creation, and chat interaction.
|
|
100
|
+
|
|
101
|
+
### Company Manager
|
|
102
|
+
```bash
|
|
103
|
+
python examples/manager_bot.py
|
|
104
|
+
```
|
|
105
|
+
Demonstrates company management features and financial tracking.
|
|
106
|
+
|
|
107
|
+
### Financial Manager
|
|
108
|
+
```bash
|
|
109
|
+
python examples/finance_bot.py
|
|
110
|
+
```
|
|
111
|
+
Interactive financial management with chat-based commands.
|
|
112
|
+
|
|
113
|
+
## API Reference
|
|
114
|
+
|
|
115
|
+
### OpenTTDClient
|
|
116
|
+
|
|
117
|
+
The main client class for connecting to OpenTTD servers.
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
client = OpenTTDClient(
|
|
121
|
+
server="127.0.0.1", # Server IP address
|
|
122
|
+
port=3979, # Server port
|
|
123
|
+
player_name="MyBot", # Your bot's name
|
|
124
|
+
company_name="MyCompany" # Company name (auto-created)
|
|
125
|
+
)
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
#### Connection Methods
|
|
129
|
+
- `client.connect()` - Connect to server and join game
|
|
130
|
+
- `client.disconnect()` - Clean disconnect from server
|
|
131
|
+
- `client.is_connected()` - Check connection status
|
|
132
|
+
|
|
133
|
+
#### Game Information
|
|
134
|
+
- `client.get_game_info()` - Complete game state information
|
|
135
|
+
- `client.get_map_info()` - Map size and terrain data
|
|
136
|
+
- `client.get_economic_status()` - Economic indicators
|
|
137
|
+
|
|
138
|
+
#### Company Management
|
|
139
|
+
- `client.get_companies()` - List all companies
|
|
140
|
+
- `client.get_our_company()` - Our company information
|
|
141
|
+
- `client.get_company_finances()` - Financial data
|
|
142
|
+
- `client.get_company_performance()` - Performance metrics
|
|
143
|
+
|
|
144
|
+
#### Financial Operations
|
|
145
|
+
- `client.increase_loan(amount)` - Increase company loan
|
|
146
|
+
- `client.decrease_loan(amount)` - Decrease company loan
|
|
147
|
+
- `client.give_money(amount, company)` - Transfer money
|
|
148
|
+
- `client.can_afford(amount)` - Check affordability
|
|
149
|
+
|
|
150
|
+
#### Company Customization
|
|
151
|
+
- `client.rename_company(name)` - Change company name
|
|
152
|
+
- `client.rename_president(name)` - Change manager name
|
|
153
|
+
- `client.set_company_colour(scheme, primary, colour)` - Change colors
|
|
154
|
+
|
|
155
|
+
#### Communication
|
|
156
|
+
- `client.send_chat(message)` - Send public chat message
|
|
157
|
+
- `client.send_chat_to_company(message, company_id)` - Company chat
|
|
158
|
+
- `client.broadcast_status()` - Broadcast bot status
|
|
159
|
+
|
|
160
|
+
#### Vehicle Management
|
|
161
|
+
- `client.get_vehicles()` - List all vehicles
|
|
162
|
+
- `client.get_our_vehicles()` - Our company's vehicles
|
|
163
|
+
- `client.get_vehicle_statistics()` - Vehicle performance data
|
|
164
|
+
|
|
165
|
+
## Requirements
|
|
166
|
+
|
|
167
|
+
- **Python**: 3.11 or higher
|
|
168
|
+
- **OpenTTD Server**: Tested with 14.1
|
|
169
|
+
|
|
170
|
+
## Development
|
|
171
|
+
|
|
172
|
+
### Setting up Development Environment
|
|
173
|
+
|
|
174
|
+
```bash
|
|
175
|
+
git clone https://github.com/mssc89/pyttd.git
|
|
176
|
+
cd pyttd
|
|
177
|
+
|
|
178
|
+
# Install development dependencies
|
|
179
|
+
pip install -e ".[dev]"
|
|
180
|
+
|
|
181
|
+
# Run tests
|
|
182
|
+
pytest
|
|
183
|
+
|
|
184
|
+
# Code formatting
|
|
185
|
+
black pyttd/
|
|
186
|
+
flake8 pyttd/
|
|
187
|
+
|
|
188
|
+
# Type checking
|
|
189
|
+
mypy pyttd/
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## Protocol Implementation
|
|
193
|
+
|
|
194
|
+
PyTTD implements OpenTTD's complete network protocol including:
|
|
195
|
+
|
|
196
|
+
- **CLIENT_JOIN** and **CLIENT_GAME_INFO** packets for connection
|
|
197
|
+
- **SERVER_GAME_INFO** parsing for real-time data synchronization
|
|
198
|
+
- **CLIENT_COMMAND** for all game operations (construction, management, etc.)
|
|
199
|
+
- **SERVER_FRAME** handling for game synchronization
|
|
200
|
+
- **CLIENT_CHAT** and **SERVER_CHAT** for communication
|
|
201
|
+
|
|
202
|
+
The library automatically handles:
|
|
203
|
+
- Map downloading and decompression
|
|
204
|
+
- Game state synchronization
|
|
205
|
+
- Command validation and encoding
|
|
206
|
+
- Network packet parsing and generation
|
|
207
|
+
- Connection management and error handling
|
|
208
|
+
|
|
209
|
+
## Support
|
|
210
|
+
|
|
211
|
+
- **GitHub Issues**: [Report bugs or request features](https://github.com/mssc89/pyttd/issues)
|
|
212
|
+
- **Documentation**: [Full API documentation](https://github.com/mssc89/pyttd#readme)
|
|
213
|
+
- **Examples**: [Comprehensive examples](https://github.com/mssc89/pyttd/tree/main/examples)
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# PyTTD Examples
|
|
2
|
+
|
|
3
|
+
This directory contains example scripts demonstrating the capabilities of the PyTTD OpenTTD client library.
|
|
4
|
+
|
|
5
|
+
## Available Examples
|
|
6
|
+
|
|
7
|
+
### `data_display.py` - **Complete Data Monitor**
|
|
8
|
+
**Recommended starting example** - Displays all available real-time game state data from an OpenTTD server.
|
|
9
|
+
|
|
10
|
+
**Usage:**
|
|
11
|
+
```bash
|
|
12
|
+
python examples/data_display.py
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### `chat_bot.py` - **Basic Chat Bot**
|
|
16
|
+
Simple example showing basic connection and chat functionality.
|
|
17
|
+
|
|
18
|
+
### `manager_bot.py` - **Company Management Demo**
|
|
19
|
+
Demonstrates advanced company management features.
|
|
20
|
+
|
|
21
|
+
### `finance_bot.py` - **Financial Management Demo**
|
|
22
|
+
Interactive financial management with chat commands.
|
|
23
|
+
|
|
24
|
+
## Getting Started
|
|
25
|
+
|
|
26
|
+
1. **Install PyTTD**: `pip install pyttd`
|
|
27
|
+
2. **Start an OpenTTD server** (default: `127.0.0.1:3979`)
|
|
28
|
+
3. **Run the data display example** to see all available data:
|
|
29
|
+
```bash
|
|
30
|
+
python examples/data_display.py
|
|
31
|
+
```
|