Navigation

Home

Downfall

Hex

Blog

Devlog

R1 backup

Contact me

News

I made a port of the game Brogue to Linux.

I have a blog.

I made some changes to this site's layout.

Downfall 0.3 released!

Hexagonal Dungeons in Roguelikes

Nathan Stoddard

Most roguelike games use rectangular dungeons. This is natural, because most roguelikes use text for output, and text is usually arranged in rows and columns. But there's another way to represent dungeons, and that is to use hexagons.

Hexagons are useful partly because they touch six neighboring hexagons. This is different than rectangles, which touch four rectangles along a side, and four other rectangles on a corner. Hexagons touch all six neighbors on a side, not a corner. This is important because it allows hexagons to represent distances more accurately. Rogue multiplies diagonal travel times by 1.4, to compensate for the added distance. Most roguelikes just ignore the problem. Hexagonal roguelikes don't need to do this, since the centers of all neighboring hexagons are all the same distance from the center hexagon. Also, line of sight algorithms can be more accurate with hexagons. However, it's also harder to implement.

By now you're probably thinking, "how do I implement hexagonal dungeons?". This can be divided into two parts, "how do I display hexagons?", and "how do I represent hexagonal dungeons in code?".

The first question is easier to answer. You can do two things. First, you can leave a space between tiles, like this:

. . . . .
 r . . . 
. . @ . #
 . . . # 
. . . # .

This works, but it doesn't look very good, and it wastes lots of space. A better alternative is to offset some of the tiles by half, without wasting any space. This method requires you to have a special library that allows you to do that. The only library I know of that can do that is RLLib, available at http://nate879.org/ (my website).

The second question is a bit trickier. It basically means picking a coordinate system. There are a few different methods to choose from. You can read about some of them at http://sc.tri-bit.com/Hex_Grids.

The one I use is different than any of the ones there. It uses two coordinates, x and y, but they are different than a cartesian coordinate system. X increases as we go down and to the right, and y increases as we go down and to the left. If x and y are always positive, and the maximum x and the maximum y are equal, it will form a diamond. It's easy to change to a hexagon; you can see that in the image below. This method has the advantage that it's relatively easy to adjust existing algorithms to use it. For example, in the image, you can see the dungeon generator, which was designed for rectangular dungeons. To display this, use code like this:

void display_hexagonal_coordinates(int x, int y) {
   int x2 = x - y + map_x_size - 1;
   int y2 = (x + y) / 2;
   if ((x + y) % 2)
      //insert code to display a character at the point (x2, y2 + .5)
   else
      //insert code to display a character at the point (x2, y2)
}

The code will probably be a lot more complicated than the code shown here.

Here is a screenshot of Downfall, which uses a hexagonal dungeon and uses RLLib:

Screenshot of Downfall

If you need more information about using hexagons, look at Amit's page. The information here is also useful. I don't like its coordinate system, but the section "The mathematical structure of hexagons" is very useful, and I used it to create the display code for Downfall.