An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# pg_ttd
Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo

[![CI](https://github.com/seanwevans/pg_ttd/actions/workflows/tests.yml/badge.svg)](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.