Skip to content

Quick Hex Systems in Python

March 15, 2009

So, today I was trying to get an old, wonderful game working properly: Missionforce: Cyberstorm, one of the better mecha wargames ever shipped for Windows.

Given that this game doesn’t work very well on Vista, I went along and started setting up a VM specifically to play Cyberstorm in. I know, uber geeky. *grin*

While the VM was setting up, disks were formatting, etc., I started to think about how much fun it would be to develop my own giant-mecha wargame, so I’m not constantly balked by the fact that one of my most favourite games is 13 years old and closed-source.

It turns out that writing a simple hex map really isn’t very hard – about 45 minutes to come up with a workable design that can create arbitrarily shaped layouts. This doesn’t handle terrain or any other feature that might be desired for a wargame, nor movement logic. It’s just the first step, building a place.

The requirements are simple: You have a map of x,y size and you need to be able to pick any point and get neighbors.
The first approach I considered for this was simply, at generation time, set every hex to know exactly what its neighbors are, as it is created. Immediately, I realized that this was incredibly inefficient to do, as the requirements of building a bunch of interdependent blocks would take a lot of CPU time.

So I went and looked at some basic hex maps, and realized that, due to the hex structure, they’re always arranged in a horizontal or vertical alignment. Which means rows.
Which means we can use an array.

So we start doing this:

class hexmap(object):

def __init__(self, x, y):

self.x = x
self.y = y = []
for i in range(0,y):[]) # add an array to hold this row of hexes[i] = [hex_block() for r in range(0,x)]

For each row, create a length of hexes. Simple.

So now we can look everything up, but we still don’t really have a method to find neighbors, which we need. I thought about this some more, and looked at the maps some more, and realized that we can take advantage of array lookups to figure out neighbors.

As an example, in a horizontally-aligned hex map, for y,x of 3, 3, you could say that

  • My left and right neighors will ALWAYS be (y, x-1) and (y, x+1)
  • My top neighbors will ALWAYS be (y+1, x-1+offset) and (y+1, x+offset)
  • My bottom neighbors will ALWAYS be (y-1, x-1+offset) and (y-1, x+offset)

For which we are making the assumption that all odd rows are offset by 1 from all even rows.
This allows us to do a very quick and easy lookup, arbitrarily, of any neighbor that a given hex might have.

(The offset math isn’t quite right yet, I don’t think.)

Coming up, discussion on landmasses and terrain formations.


From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: