How to Record and Play NetHack Games with ttyrec and ttyplay Live

ttyrec is a great program for recording a terminal. I use it for recording nethack games. Any command is easily recorded with the “-e” argument of ttyrec, which allows you to choose a single command to record instead of the whole shell session. For example, I could type

$ ttyrec -e nethack

In order to start nethack, recording the whole game. Or rather, if I want to save the recorded game to a file other than the default “ttyrec”:

$ ttyrec game00 -e nethack

However, I don’t want to nethack. My name is Tim, and I want to nethack -u Tim. Simply adding -u Tim to the previous code does not work, since “-u” is interpreted as another switch for ttyrec. Quotation marks solve the problem:

$ ttyrec game00 -e "nethack -u Tim"

You can easily make a bash script out of this. The following is my playnethack, which is run instead of the regular “nethack”.

#!/bin/bash
echo -n "Name?"
read pname
ttyrec ~/nethack/rec/$pname-`date +%Y%m%d%H%M%S`.tty -e "nethack -u $pname"

Note that the filename here is dynamic. The text within backticks (”`”) is executed as a command, and the command is then substituted with its actual output. date +%Y%m%d%H%M%S returns the current date and time in a format such as 20070613233807. Thus, if I ran the “playnethack” command and set my name to “Tim”, my complete game would be recorded into the file “~/nethack/rec/Tim-20070613233807.tty”, probiding the ~/nethack/rec/ directory existed.

The sister command of ttyrec is ttyplay, which plays the recorded .tty files. Simply do:

$ clear
$ ttyplay ~/nethack/rec/Tim-20070613233807.tty

And you will watch my game, from the beginning to the end. The time is recorded, too–you will know exactly for how long I contemplated each and every action. Using the ttytime utility, you can find out how many seconds long a recorded game is before playing it, e.g:

$ ttytime ~/nethack/rec/Tim-20070613233807.tty
  71946 ~/nethack/rec/Tim-20070613233807.tty

However, if you don’t want to wait for 71946 seconds while watching the replay, you can always press any button in order to forward to the next movement. If you hold a key, you will fast-forward through the entire game.

It is even possible to watch a live game as it is being played, using “-p”:

$ ttyplay -p ~/nethack/rec/Tim-20070613233807.tty

This will let you watch my NetHack game as it progresses in real-time, and thus lets you tell me as soon as I make a stupid mistake.

FireStats iconAnvänder FireStats