Saturday, March 12, 2011

Design Decisions: Grid type.

First of all, this blog is not dead.  There has been a lot of work going on the implementation of Lorem Ipsum I (as the roguelike is now going to be called).

 One of the decisions most roguelike developers never have to face is how to lay out their tiles, and what shape they will be.  Given the constraint of working with curses, the choice of a rectangular grid with square -- well, rectangular of 2x1 dimensions -- tiles is an obvious one.  Rectangular grids pose many advantages:
  • Rectangular grids lend themselves naturally to rectangular rooms in a dungeon map, for an easy correspondence to the real world;
  • Similarly they promote 90 degree angles in corridors, which is also quite natural;
  • They are easy to visualize and describe -- for that matter they may be effectively stored in a text file, either for a saved game or for a post-mortem character dump;
  • As has been mentioned before, they are the only sensible choice when working with curses;
  • Most computations get easier, for instance distance between tiles is trivial to compute, as the square root of x^2+y^2.
They also have certain disadvantages of varying gravity:
  • Using curses and monospace fonts, visually the distance between horizontal and vertical tiles is off by a factor of two.  This is a result of most monospace fonts being 2x1;
  • Worse yet, diagonal distances are off by a square root of two, and the developer is left with an uncomfortable task of reconciling what that will mean in game terms.  Common approaches involve assuming that diagonal movement is as expensive as that along the axes (which distorts distance favoring diagonal movement), or treating it to be 1.5 times as expensive (a crude approximation of the square root of two, but generally functional), which also entails tailoring the game to support 'fractional' movement;
  • On the same note, rectangular grids mean a player character can be surrounded from 8 sides... with four of the sides being a different distance 'off', potentially;
  • Lastly, rectangular grids make line of sight and field of view algorithms harder.  For instance, if two 'wall' tiles are touching on a diagonal, do they block line of sight?  Why or why not?
Hence, after a bit of deliberation I have decided to go with hexagonal tiles on a hexagonal grid.  To be exact, in my case the tiles themselves are mathematically circles inscribed inside the hexagons, but the principle is the same.  While certain math gets more difficult as a result, there is the trade-off of not distorting distance or treating half of the adjacent tiles as somehow special, while presenting a more organic look to the user.  Furthermore, while room layouts get a bit stranger, these grids are quite a charm for caves.
The layout also happens to be surprisingly easy -- working with monospace fonts of 2x1, successive columns can be staggered by half a tile's height presenting a pseudo hexagonal grid which looks pretty damn good.  For greater effect, I have included an option to offset the columns away from each other by a factor of 1.75, which approximates the distance between actual hexagonal tiles.  While this wastes some screen real estate, the outcome looks particularly even as a result.
Here is an image showcasing a hexagonal grid, as well as the new FOV algorithm (to be discussed later):