OpenTTD: Timelapse from game save

OpenttdThis script allows you to create a timelapse video from your OpenTTD game. It will use your previously saved games. No need to remember to take screenshots during the game, ad since you play it safe and save before and after each big civil engineering work (ooops ?), you will have enough frames to create a nice work to put into your Company heritage cabinet.

The resulting video is zoomed into a defined location and works with large games (tested on a 512×512 map).

After opening an old game by mistake, I realized how much civil engineering my region had undergone. Having a timelapse of all that heavy work sprung to my mind. But I never saved any screenshot of my games and opening each game one by one is not my favorite thing. Better spend more time to create an automated script.

Basics

OpenTTD has a console that allows some basic commands mainly intended for network games), and more importantly allows you to take screenshots. A second sweet feature, is the support for start up scripts, on openttd level and/or on a game level.

What we need

  • OpenTTD (obviously)
  • Python with PIL support
  • ffmpeg or avconv
  • Some saved games

Take the right screenshot

OpenTTD’s console allows you take different types of screenshots:

  • screenshot: just grabs the screen with all the GUI present
  • screenshot no_con: same as above but without the console window
  • screenshot big: a zoomed in version
  • screenshot giant: the whole map

The location and zoom level are stored within the saved game. Meaning that when you open a game your visible area is how you left it. You can go to a specific tile with the console command scrollto, but you can’t set a specific zoom level. The first three screenshot options are dependent on zoom level, you won’t get a consistent area for each saved game.

This leaves us only with the giant option, which creates really huge files: 32000×16000, 40MB for a 512×512 map.

Re-sampling the images

Re-sampling the images to 1920×1080 and using it as could be a choice, but you loose the sight of the detail and you end up with only a generic overview. Don’t you prefer to show the amazing landscaping you made to fit this airport and good’s station into the city?

We will cut out the interesting region from the main file, to simulate a zoom-in and achieve something similar to screenshot big, but without having to worry about the zoom.

The steps

I ran this script on Ubuntu 14.04, it will work on other Linux distro or OSX and should work on Windows too (still someone using Windows, seriously ?) as long as you have all the tools installed. The paths in the sample are for Ubuntu, just replace them with the ones for your installation.

Screenshot and exit

To take a screenshot and exit immediately after starting a game, simply create the file ~/.openttd/scripts/game_start.scr, with this content:

screenshot giant
exit

Start a game

You can start OpenTTD and directly open a game:

openttd -x -g game_filename

Crop and resize images

PIL is surprisingly handling this quite well. My first approach was to use ImageMagick, but the time and CPU taken to crop the huge source file was below expectations.

Display the right area

I was too lazy to write a tile_to_pixel method by using matrix calculation and geographic projections, so I went the easy route by using the position relative to the source image.

The final script

Help

python openttd_timelapse.py --help should give you all the information you need.

usage: openttd_timelapse.py [-h] -c COMPANY [-d OPENTTD_FOLDER] [-dx WIDTH]
[-dy HEIGHT] [-z {1,2,3,4}] [-l LEFT] [-t TOP]
[-s] [-f FONT PATH] [--debug] [--check]

Create a Timelapse from an OpenTTD game

optional arguments:
-h, --help show this help message and exit
-c COMPANY, --company COMPANY
Game Name
-d OPENTTD_FOLDER, --dir OPENTTD_FOLDER
OpenTTD folder (default ~/.openttd)
-dx WIDTH, --width WIDTH
Width of frames (px) (default: 1920)
-dy HEIGHT, --height HEIGHT
Height of frames (px) (default: 1080)
-z {1,2,3,4}, --zoom {1,2,3,4}
Scale factor
-l LEFT, --left LEFT Position from left in percent (default: 50)
-t TOP, --top TOP Position from top in percent (default: 50)
-s, --timestamp Display year on frames
-f FONT PATH, --font FONT PATH
Path to TTF file
--debug Do not clean temp files
--check Output first image only, useful to verify the selected
area

output
Movie is created in current path, with the name company_name.mp4
-z, –zoom {1,2,3,4}
Allows to cut out a smaller or bigger area from the source file which is resized to the movie size.
Zoom 1 crops out the exact size. Zoom 2 the double size. The timelapse will display a bigger regions, similar to a “zoom-out” in game.
–c, -check
Creates only the first file in current folder as timelapse_check.png. This allows you to verify that the settings are alright, before starting the generation of all images.