This is my first blog post here, and in this and a couple more following I'm going to focus on the hexes of the game Divine Right. If you haven't played Divine Right yet, it is a great game.
For a game like this, the hexes are very important. One of the things we'll need to understand in the coding is which hexes surround a particular hex. I refer to these as Hex Neighbors. We need to know this for calculating movement.
If you examine the board, you find that there are 31 rows and 34 columns. In the coding we will start at 0, instead of 1, so we will have boundaries setup like this:
( (ROW >= 0) && (ROW<=30) )
( (COL >= 0) && (COL<=33) )
We've come up with a hex identity which is 2 digits row and 2 digits column. I placed several hex ids on hexes on the map above to help in following this post. So for instance, Khuzdul is at row 7, column 18. This translates to 0718.
The shape of these hexes, with a point at the north and south, make the hex sides NW, NE, W, E, SW, and SE.
What if I am at Khuzdul (0718) and I want to know the hex ids of the 6 surrounding hexes?
We could do this by brute force and eyeball every hex, and put all of their ids into a 6 element structure. One element for the NW, NE, W, E, SW, SE.
It would be easier to do this in coding.
We can use these calculations below to identify the surrounding hexes. Great! That wasn't so bad.
NE=row++, col++
NW=row++, col
E =row, col++
W =row, col--
SE=row--, col++
SW=row--, col
Row = 07; Col = 18;
E : Row = 07, Col= 18+1 = 07,19
W : Row = 07, Col= 18-1 = 07,17
NE: Row = 07+1, Col=18+1 = 08,19
NW: Row = 07+1, Col=18 = 08,18
SE: Row = 07-1, Col=18+1 = 06,19
NW: Row = 07-1, Col=18 = 06,18
Yes, this works fine.
Lets try it on Zefnar by the Sea.(08,10)
Row = 08, Col = 10
E : Row = 08, Col=10+1 = 08,11
W : Row = 08, Col=10-1 = 08,09
NE: Row = 08+1, Col = 10+1 = 09,11
NW: Row = 08+1, Col = 10 = 09,10
SE: Row = 08-1, Col = 10+1 = 07,11
SW: Row = 08-1, Col = 10 = 07,10
Uh oh, the NE, NW, SE, and SW are not correct. It turns out that we need a seperate set of calculations for Odd and Even rows. We include the 0 row as even for these purposes.
Even (and 0) columns
NE=Row++, col
NW=row++, col--
E = row, col++
W = row, col--
SE = row--, col
SW = row--, col--
Row = 08, Col = 10
E : Row = 08, Col=10+1 = 08,11
W : Row = 08, Col=10-1 = 08,09
NE: Row = 08+1, Col = 10 = 09,10
NW: Row = 08+1, Col = 10-1 = 09,09
SE: Row = 08-1, Col = 10 = 07,10
SW: Row = 08-1, Col = 10-1 = 07,09
Here we have all the elements needed to determine a hexes neighbors.