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.
Python utilities in this repository use the psycopg driver to
connect to PostgreSQL. Install the dependencies with:
pip install -r requirements.txtUtilities 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.
Individual table definitions live in sql/tables/. Run the generator to
combine them into sql/schema.sql before applying the schema:
make generate-schemaEdit the per-table files rather than schema.sql to avoid divergence.
Documentation for the SQL procedures lives in docs/procs.md. Notable
entries include economy_tick() for economic updates
and move_vehicles() which advances vehicles along
their routes.
A tiny curses-based renderer is included to visualise the map stored in PostgreSQL and advance the simulation.
-
Ensure the database is populated with the required schema.
-
Provide connection parameters using the standard
PGHOST,PGPORT,PGDATABASE,PGUSERandPGPASSWORDenvironment variables or create a JSON configuration file and reference it withPGTTD_CONFIG. A PostgreSQL DSN may also be supplied via the--dsnoption (overriding any environment variables). -
Run the viewer:
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 whentis 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.