Slay Tutorial four published

Debug ModeTutorial four of my reimagining of the game Slay continues.

This was an interesting bit of code to write, particularly setting the player hexagons. Then I tried to do too much in one go, adding the forts at the same time. I split this into separate functions.

It makes extensive use of recursion and that handy trick when you work with hexagons, the IsValidHex() function which tells you which six of the surrounding eight locations are valid. This is needed because the map is stored in a 2d array so in a 3×3 block, the centre location is surrounded by eight locations.

When you use hexagons, it’s more like a brick wall with each alternate row moved over a bit.  Now every location is surrounded by six adjacent locations just like in a hexagon grid. It turns out that a very simple function can tell you which of the 8 surrounding locations are valid.

In the grid I’m using, which runs in rows horizontally either 1,3 or 5,7 cells are invalid depending on whether the row is odd or even.

Here is a normal block of 3 x 3 locations.

7 0 1
6 x 2
5 4 3

But if I slide it over by 2 characters you get this.

  7 0 1     or     7 0 1
 6 x 2               6 x 2
  5 4            5 4 3

So by ignoring the bold locations, you can simulate a hexagon grid.

I’ve also added Debug mode which toggles with the tab key and introduced a blockId to each hex so you can see which hexes are lumped together. In Debug mode, the BlockId is printed out over the hex as in the screenshot. I’ve used the SDL_ttf library to draw the text. It slows down the screen update by a factor of 40x but is just fast enough to do 60 frames per second.

It’s interesting that the Ubuntu code running under Hyper-V is actually a lot faster than the Windows code.  As with previous tutorials, the code includes conditional compilation so it will compile and run under Windows or Linux.

Note, this code is not quite perfect. It now has three bugs:

  1. The map generator occasionally produces a map with a 2nd smaller continent separated by one hex from the main continent.
  2. Toggling Debug mode a lot makes it behave oddly. I thought I’d fixed that but I’m not so sure.
  3. Some blocks of contiguous hexes (all the same colour) lack forts.
(Visited 2 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *