Saturday, July 3, 2010

Setting up a public NetHack Server

NetHack is an old role playing game which you play in a terminal (with ASCII characters). A typical NetHack screen could look like this:

The game is very challenging, and really fun to play once you got used to its style. Most players play on public servers; the most widely known one is nethack.alt.org. There's several reasons for why you want to play on a server:
  • The default distribution of NetHack is very, very basic. There's many patches that enhance the user interface (like you can see in the screenshot above, having colored menus and hit points), and applying them all yourself requires a bit of technical experience, and much patience because many of them won't apply cleanly.
  • If the server isn't yours, you can't cheat by copying save files or similar. Thus, if you ascend (win) on a public server, everyone knows you didn't cheat.
  • Other users can watch your game while you're playing and give advice.
  • You can play the same game from everywhere, and the risk of losing it due to a crash is much smaller than on your local machine (because desktop computers usually crash much more frequently than servers).
The most commonly used servers like nethack.alt.org (NAO), are pretty minimalistic with the patches they apply in order to match the needs of as many users as possible. That's just fine, but if you like playing around with things and optimizing this and improving that as much as I do, you might want to set up your own server. I'll give a short explanation on how to do this best.

Step 1: Create your own NetHack build

A. The hard way

  1. Download the NetHack source code from http://nethack.org/v343/download-src.html.
  2. Follow the excellent instructions from the NetHack wiki in order to compile the unmodified "vanilla" version.
  3. Get patches you like, for example from the excellent NetHack patch database, apply them to the source code (cat whatever.diff |patch -p0 under linux)
This can get rather annoying quickly, because the first patch will apply nicely, but maybe the second one won't. For example, the dumplog patch which dumps information about how a game ended into a text file for online viewing, always interferes with the statuscolors patch, which colors your hitpoints and mana. Thus, if you don't like applying patches by hand, you might like the easy way.

B. The easy way

For my server at nethack.feorar.org, I created a source base with a large number of patches. You can download it here. Assuming that 95% of servers run linux, this source is preconfigured for linux; you can just unpack it and "make install", then "/usr/games/nethack" should already start the game. I modified all patches (well, those which didn't already provide it) to offer a flag in include/config.h which you can just comment out to disable the patch. That way, this build is pretty flexible and should suit most needs.
  • statuscolors: Allows coloring the status lines (hitpoints, mana, and stuff like "Hungry"). Disable this by undefining STATUS_COLORS (prefix the line with //).
  • menucolors: Allows coloring of menus (inventory etc.) with regular expressions. Disable by undefining MENU_COLOR.
  • dungeon_overview: Allows you to see a summary of what the dungeon looks like via the #overview command (where are the gnomish mines, etc.). You can also #annotate to make annotations to levels yourself. Disable by undefining DUNGEON_OVERVIEW (added by me)
  • use_darkgray: Makes NetHack display some characters in gray instead of blue (so there's just one more color). You currently cannot remove this from the build, but you can just add OPTIONS=!use_darkgray to your options file.
  • darkroom: Shows a gray dot instead of nothing for dark places in rooms which you already explored. Disable by undefining DARKROOM (added by me). Note that this option won't do anything with DEC / IBMgraphics enabled and use_darkgray disabled.
  • heck2: Gehennom, a branch deep in the dungeon, contains many very similar maze levels in the original NetHack. This gets boring soon. This patch adds some cool new maze parts which make the levels much more interesting, and also adds some new special levels. Because this is a level file modification, it cannot be disabled by a config file option (because they run through NetHack's internal level compiler, not a C compiler); if you want to get rid of it, download the patch, and revert it (cat whatever.diff |patch -p0 -R).
  • Additional conducts: No reason to get rid of this, is there; just ignore them
  • Colored dungeon branches: Gehennom is red now, and the mines are brown, etc. This also makes special room walls colored (beehives are yellow, for example). Disable this by undefining DUNGEON_COLORS (added by me)
  • An improvement to the random number generator, so it cannot be easily predicted by scripts; no reason to get rid of this, either.
  • A river generation patch which sometimes generates rivers, and Islands. Disable this by undefining RIVERS (added by me). You can also change the probability of a river being created by changing RIVER_CHANCE, the original value was 4, I changed it to 6.
  • A patch which adds a Held status flag if you're grabbed by a monster. I added another one, Lev, which is displayed when levitating. You can disable those by undefining HELD_STATUS_FLAG resp. LEV_STATUS_FLAG (added by me)
  • The pilesize patch, which allows you to configure in what case NetHack will show that "Things that are here:" window. Default is if there's 2 to 5 items; so if you set the in-game option to 5, there's no change from vanilla behaviour. However, because I have set this option to 1 in order to completely disable the "Things that are here:" stuff, I added some more indicators ("There are a few objects here" etc.). I didn't add an option for that because I couldn't imagine why anybody would want to disable this. I also modified the patch to not change gameplay with footrice corpses (petrification when blind).
  • Additional level variants for Medusa, the Big Room, and Fort Ludios. As I already mentioned, level changes aren't so easy to make configurable, so you can disable them by downloading the appropriate patch and applying it with the -R option. Sorry.
  • The adjsplit patch, which allows you to split stacks (n - 5 potions of water) by using adjust and a number (#adjust 3 n o -> n - 2 potions of water, o - 3 potions of water). Disable by undefining ADJSPLIT.
  • A patch that allows to toggle display of traps above or below objects with the % key. There's not really a point in disabling this, it's just a more comfortable ^ command.
  • The paranoid patch, which adds some paranoid_ options (for example, if you enable paranoid_quit, you need to type "yes" instead of "y" when asked "Really quit?". Especially useful is "paranoid_hit", no more watchmen-deaths when moving southwest with the y key :]). Disable by undefining PARANOID. 
  • The auto_open patch, which automatically invokes the "open" (o) command when you walk into a door. Modified not to change gameplay. Disable by undefining AUTO_OPEN. You need to enable the auto_open option in game if you want this.
  • Fixes for the longworm split crash bug, the astral call exploit, the deallocated wielded objects crash bug, the acid+water crash bug, and the breakable objects + iron bars crash bug.
  • I added the possibility to display the dungeon name instead of just Dlvl:. Thus, this version shows, for example, Gnomish Mines:5 instead of Dlvl:5. You can color those with statuscolors, like STATUSCOLORS=Gnomish Mines:brown,Dungeons of Doom:white etc. You can disable this by undefining DUNGEON_NAME.
  • Because there's much more free space in the first status line than in the second one, I moved the turn counter to the first line. Move it back to the second one by undefining MOVES_ON_FIRST_LINE.
  • I added the possibilty to color alignments with statuscolors (STATUSCOLORS=Lawful:cyan,Neutral:white,Chaotic:brown). Disable this by undefining COLORABLE_ALIGNMENTS.
  • I added a hack fix for problems with statuscolors and IBMgraphics (IBMgraphics don't work if they aren't enabled after statuscolors in the options file). You can disable this fix by undefining STATUSCOLOR_HACK_FIX.
  • The dumplog patch from nethack.alt.org. Disable it by undefining DUMP_LOG. Set DUMP_FN to the path where you want to store dump logs.
  • A Hitpoint status bar. It changes the background color of your class name ("Foo the bar") and makes a bar out of that representing your current hit points. Enable the "hitpointbar" option to activate this. Configuration options for the colors could be something like this:

    STATUSCOLOR=hpbar_full:green&inverse,hpbar_high:cyan&inverse
    STATUSCOLOR=hpbar_medium:yellow&inverse,hpbar_low:magenta&inverse
    STATUSCOLOR=hpbar_verylow:red&inverse,hpbar_critical:red&inverse,hpbar_black:none  
  • The possibility to colorize the messages ("You hit the foo. The foo misses you.") like you do with menucolors using a new option called MSGCOLOR. For example:
    MSGCOLOR="You hit"=red&bold 
    The message string uses regular expressions. This patch is very useful for seperating relevant messages from the usual battle spam (for example you could color spellcasting / wand zapping / cockatrice stuff with another color).
Of course, you have to "make install" again after you changed anything.

    Step 2: Set up dgamelaunch

    Dgamelaunch is a program that handles user registration and login, watch mode, and some more stuff when users log in via telnet to your server. It's kind of the only program to use this, and it's difficult to set up. You best follow some sophisticated tutorial like this one. I just want to give a few tips:
    • Use dgamelaunch 1.5.x, it's better than 1.4.x
    • If you try to run dgl-create-chroot but get a permission denied for the mknod command, you're probably on a virtual server. You don't need the node, anyway, so just edit the script, search for the mknod line, and comment it out or delete it.
    • Your NETHACKDIR is /usr/games/lib/nethackdir. Everything from there needs to be on the same place in the chroot.
    • Before you try to run dgamelaunch, try running nethack from the chroot: cd into the chroot directory, and then strace -f chroot . /bin/nethack 2> log. If you open the "log" file and see which files NetHack does not find (probably at the end of the file), it'll be easy to copy them to their right location (if you did "make install" your NetHack build, you just need to copy those files from your real root / to your fake chroot to the same location. For example, if your chroot dir is /opt/nethack/myserver, and NetHack doesn't find /usr/games/lib/nethackdir/dungeon, you want to copy /usr/games/lib/nethackdir/dungeon to /opt/nethack/myserver/usr/games/lib/nethackdir/dungeon and create any directories that don't exist in the chroot). That way, you should get NetHack up and running easily.
    • You can do the same for dgamelaunch. Just strace -f dgamelaunch 2> log. Important: for me, dgamelaunch-1.5.x crashed when attempting to play NetHack (p key) when I was running strace in the background. So you may want to try if it works without strace (it will, if the path to NetHack is correct in /etc/dgamelaunch.conf, and NetHack works with the chroot command).
    • Always be sure your permissions are set correctly; it's never a bad idea to chown -R games yourchroot; chgrp -R games yourchroot
    I wish you good luck, and feel free to ask if you still experience problems.

    4 comments:

    1. How get colored interface in telnet? I created recomended ~/.nethackrc file, but either in linux and mac os x I have gloomy white-black interface ;( I say about your's feorar.org.

      ReplyDelete
    2. Hi,

      telnet cannot read your local config file, this hint was meant for local installations. For remote, copy the text from the config file and paste it into the "Edit options" menu option from the telnet login screen (press i, paste text, press Esc, type :wq and press Enter -- the editor is called "vi", if you want to look the commands up).

      Bye,
      Sven

      ReplyDelete
    3. Always be sure your permissions are set correctly; it's never a bad idea to chown -R games yourchroot; chgrp -R games yourchroot...

      fyi, you can shorten that to

      chown -R games:games yourchroot

      ReplyDelete
      Replies
      1. This sounds like you have a reason to say that, but to me it looks like all is set up correctly ;)
        In case there's indeed a security gap somewhere, could you please be more specific so I can close it?

        Thanks,
        Sven

        Delete