doomscript

The Doom engine, written entirely in PostScript.

Ghostscript runs it. The Silkie terminal shows it. Your keyboard drives it. There is no C helper and no display device, just a PostScript program: the same kind of file your printer and your PDF tools open every day.

Source code on GitHub
The title screen: a white Silkie chicken in a red scarf stands on a hill under a full moon, surrounded by cartoon ghosts, beneath the words SILKIE VS THE GHOSTS.
The title screen. Every pixel is drawn by PostScript at startup, and the lettering is set in Ghostscript's own fonts.

A PostScript file is a program.

PostScript looks like a page description format, but it is a complete programming language, with loops, recursion, dictionaries, strings, file I/O and a clock. Opening a PostScript document means running whatever code is inside it.

A document that plays a game

doomscript is a translation of id Software's Doom engine into PostScript. It includes the BSP renderer, the game simulation and monster AI, the menus, the status bar, a keyboard decoder and a terminal graphics encoder. It reads keystrokes from /dev/stdin and draws by writing graphics escape sequences to standard output.

Why build it?

doomscript runs under Ghostscript's -dSAFER sandbox. It may read its own files, the game data and the keyboard, and nothing else. That is still enough for a whole game: 3D rendering, physics, enemies that hunt you, a resizable graphical display and live keyboard input.

Anything a sandbox leaves open, a program can use. Every system that opens PostScript or PDF files (printers, previewers, converters, mail scanners) is running an interpreter for a general-purpose language. That interpreter deserves the same suspicion as any other code runner: keep it patched, keep it sandboxed, and give it as little as possible.

The cast

The hero is a Silkie chicken, and the monsters are ghosts. None of them come from an image file. Each figure is described with PostScript path operators (ellipses, curves, fluffy clouds of feathers) and painted into the game's palette by a scanline rasteriser that is itself written in PostScript. Sprites are drawn the first time they are needed, in every pose and from every angle, with an outline so the white bird reads against any wall.

In the game: a corridor with a sheet ghost ahead, the hero's white wing holding a pistol, and the Silkie's face on the status bar.
In the game: a ghost down the corridor, the pistol held in a white wing, and the Silkie's face on the status bar.

Resize whenever you like

Everyone resizes their terminal all the time, so doomscript follows the window. The renderer works at the window's shape, so a wider window gets a wider field of view, and it holds the pixel count steady to keep the frame rate. The title screen is repainted at the new size, with more sky, more hills and more stars.

The title screen in a very wide window: the starry sky and hills stretch out to both sides.
The title screen in a tall, narrow window: more sky above the cast.

How it works

Input

A tiny shell launcher puts the terminal in raw mode and starts gs. Ghostscript treats an empty read from standard input as end of file forever, so the game reopens /dev/stdin every frame, without blocking, and parses the kitty keyboard protocol for key presses and releases. The terminal also reports window resizes in the same input stream.

Output

The renderer draws 8-bit palette indices into a PostScript string. That string is base64-encoded in PostScript and sent to the terminal as kitty graphics protocol escapes, in 4096-byte chunks. Silkie accepts compact palette frames; any kitty-compatible terminal gets 24-bit RGB.

The engine

The game logic and renderer follow id's linuxdoom-1.10 source closely: BSP traversal, visplanes, sprite clipping, sector physics and thinkers. Doom's 16.16 fixed-point arithmetic maps neatly onto Ghostscript's 64-bit integers. PostScript has no return, so early exits become loop … exit.

% FixedMul: 16.16 x 16.16 -> 16.16
/FixedMul { mul -16 bitshift } bind def

Play it

You need Ghostscript 9.5 or later and a terminal that speaks the kitty graphics protocol: Silkie or kitty. The levels and textures come from Freedoom, downloaded separately.

git clone https://github.com/adamdeprince/doomscript.git
cd doomscript
mkdir -p wads && cd wads
curl -LO https://github.com/freedoom/freedoom/releases/download/v0.13.0/freedoom-0.13.0.zip
unzip freedoom-0.13.0.zip && cd ..
./doomscript                 # title screen and menu
./doomscript -sWARP=E1M1     # straight into a level
KeyAction
W / S or ↑ / ↓move
A / Dstrafe
← / →turn
Ctrl or Ffire
Space or Euse (doors, switches)
1–7weapons
F5trade sharpness for speed
Escmenu

Credits and licence

doomscript is a translation of id Software's Doom source code (linuxdoom-1.10), which id released under the GNU General Public License version 2. doomscript is distributed under the same licence (GPL-2.0-only). The Silkie, the ghosts and the title screen are original art, drawn in PostScript, under the same licence.

Levels and textures come from Freedoom (BSD licence) and are not included. Ghostscript is the interpreter that runs the program.

DOOM is a trademark of id Software. PostScript is a registered trademark of Adobe Inc. Ghostscript is a registered trademark of Artifex Software, Inc. doomscript is a personal project and is not affiliated with or endorsed by id Software, Adobe or Artifex. It uses only the ordinary features of the PostScript language and does not exploit any flaw in Ghostscript.