https://github.com/seanwevans/pg_ttd
openTTD clone in postgres
https://github.com/seanwevans/pg_ttd
database-experiment game-clone openttd simulation
Last synced: 2 days ago
JSON representation
openTTD clone in postgres
- Host: GitHub
- URL: https://github.com/seanwevans/pg_ttd
- Owner: seanwevans
- License: mit
- Created: 2025-08-27T19:58:29.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-09-27T19:35:01.000Z (10 months ago)
- Last Synced: 2025-09-27T21:18:11.277Z (10 months ago)
- Topics: database-experiment, game-clone, openttd, simulation
- Language: Python
- Homepage:
- Size: 214 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Roadmap: ROADMAP.md
Awesome Lists containing this project
README
# pg_ttd

[](https://github.com/seanwevans/pg_ttd/actions/workflows/tests.yml)
**pg_ttd** is a prototype of an attempt at *OpenTTD inside PostgreSQL*.
We attempt to reproduce at least part of OpenTTD within a Postgres database.
That is, all simulation, logic—world generation, tile updates, entities, economics, etc
is implemented as stored procedures and tables in PostgreSQL.
## Requirements
Python utilities in this repository use the [`psycopg`](https://www.psycopg.org/psycopg3/) driver to
connect to PostgreSQL. Install the dependencies with:
```bash
pip install -r requirements.txt
```
Utilities such as `python -m pgttd.run_tick` and `python -m pgttd.create_vehicle`
expect a PostgreSQL connection string via the `--dsn` option or the
`DATABASE_URL` environment variable. The `create_vehicle` command defaults to
placing vehicles at coordinates `(1, 1)` when `--x` and `--y` are omitted.
## Schema
Individual table definitions live in `sql/tables/`. Run the generator to
combine them into `sql/schema.sql` before applying the schema:
```bash
make generate-schema
```
Edit the per-table files rather than `schema.sql` to avoid divergence.
## Stored procedures
Documentation for the SQL procedures lives in [docs/procs.md](docs/procs.md). Notable
entries include [`economy_tick()`](docs/procs.md#economy_tick) for economic updates
and [`move_vehicles()`](docs/procs.md#move_vehicles) which advances vehicles along
their routes.
## Renderer
A tiny curses-based renderer is included to visualise the map stored in
PostgreSQL and advance the simulation.
### Launch
1. Ensure the database is populated with the required schema.
2. Provide connection parameters using the standard `PGHOST`, `PGPORT`,
`PGDATABASE`, `PGUSER` and `PGPASSWORD` environment variables **or** create a
JSON configuration file and reference it with `PGTTD_CONFIG`. A PostgreSQL
DSN may also be supplied via the `--dsn` option (overriding any environment
variables).
3. Run the viewer:
```bash
python renderer/cli_viewer.py [--dsn DSN] [--refresh SECONDS] [--step]
```
* `--refresh` – delay between screen updates in seconds (default: 0.5)
* `--step` – advance the simulation only when `t` is pressed
Press `q` to quit. By default each refresh calls `tick()` in the database to
advance the world state. When `--step` is supplied the simulation advances only
when `t` is pressed.