Decompiling Interactive Fiction

#zmachine #interactivefiction #zork

A couple of weeks ago a friend of mine pointed me to the web-based interactive fiction piece called Aisle. As I played it, I immediately started wondering how I could get a text blob of all the possible paths instead of trying to figure out all the actions required to input into the interpreter.

Aisle Introduction

Finding the Source

I took a look at the source and discovered Aisle was using a tool called Parchment, which is designed for interactive web fiction. Looking at the javascript source of Aisle, it is basically a in a javascript function passed to Parchment.

Since Linux has a base64(1) utility, I figured I’d strip out the javascript and decode the text to see if I could read the text blob and thus read every possible scenario in the story.

% sed -e "s/processBase64Zcode('//g" Aisle.z5.js \
| sed -e "s/');//g" \
| base64 --decode > Aisle.z5

At first I did this without output redirection, and it flooded my terminal with binary control characters, essentially frying it. I killed the tmux window, and started again, this time looking to see if the file type was known.

% file Aisle.z5
Aisle.z5: Infocom (Z-machine 5, Release 1 / Serial 990528)

A quick search on Infocom and Z-Machine, shows that Z-Machine was developed for interactive fiction in 1979 for Infocom text adventure games, of which Zork was the first and where the Z comes from. There’s a whole lot of interesting history online about Z-Machine if anyone wants to know more (spoiler: it’s related to UCSD Pascal P-Machine).

Decoding Z-Machine

Now that the raw source was available, the next step was actually finding useful text. A search found an open source tool called Frotz that can play these files, and the next thing I know I have Aisle running in a terminal and it works just like it does in the browser.

Some addition searches didn’t really uncover much on how to de-compile a Z-Machine file. Nothing in the Debian repos, and some additional man pages for Frotz didn’t lead to much.

Finally I found Z-Code Tools. There was no compiled binaries for Linux, and if there were, they probably don’t work anymore on a newer version of Linux. I downloaded the source and a make later made the binaries magically appear. The freshly compiled txd tool finally yielded the results I wanted:

% ./txd /var/tmp/Aisle.z5 | grep gnocchi | head -5
S139: "You pick up a bag of gnocchi and turn it over. The doughy balls weigh your eyes; gnocchi, women, a woman, statues, a slow motion crash of flesh on show her some gnocchi and then you eat it and live happily ever after." intelligent. Leaving the gnocchi you walk over and drop to a knee. "Will you

I put the full text dump of Aisle into a gist if anyone is interested in taking a look.

There are many other tools included in ztools that can provide additional information, such as listing a dictionary of available actions and commands.

Final Thoughts

Interactive fiction is just more than a big blob of text. Since the early days of Z-Machine, these stories have not only included fascinating stories, but also puzzles and other methods of fully immersing a reader. Fully exploring and creating Z-Machine and Interactive fiction is best left to to the experts if you’d like to learn more.

It also appears the Z-code Tools are not packaged in Debian, and this may be a fun project to do sometime.

Written on August 21, 2015