Automatic wall generation


Most of my games use some form of random level generation. I love the process of creating the code to make that work, and I love that it allows me to generate much more playable content by saving time on designing levels.  For Heroes of Loot and now my new game Space Grunts, I used some code that also generates the looks of a level, so besides the layout which is mostly saying “hey, this is a wall, and this is not”, the game also has to show correct graphics for each wall.

A couple of people drilled me about how that actually works, so now I can just point them to this article – hi!

First thing to do is create your level-generation code, there are many ways of doing that and this article is not going to tell you how, you can find a few of my posts on that here.

The code this blog is all about is assigned the right “texture” to each 1 and 0 in your tilemap. Obviously 0’s are empty spots so they are normally just ground textures and easy to do, the 1’s are walls and are a bit trickier.  To keep this a bit cleaner, in my code I use a tilemap for the 1’s and 0’s, and a rendermap for the texture indices.

Simple theory behind it: check every tile in your tilemap, see if it’s solid or empty. If it’s solid, let’s get to work and check it’s surrounding tiles.  This really comes down to a lot of “IF then, Else” checks.  So what we do for EACH tile in the tilemap:

1) are we a solid tile, or an empty tile?  if empty? place the floor texture, and continue with the next tile
2) we are solid! so make us a full wall texture (usually a full-square) and continue with step 3
3) check our surrounding neighbors in all 8 directions. If we have NO solid neighbors, we are a single solid tile in the middle of open floor.. so we can turn into a pillar, or a cube, or statue, etc.
4) this is where your logic brain has to start working, cause we need to do a lot of IF checks:
– IF the tile on the left+right of us are also solid, but the tiles top+bottom are not.. we are part of a horizontal wall
– IF the tile on the top+down of us are also solid, but the tiles left+right are not.. we are part of a vertical wall

That’s the basic test, and you’ll have to just do this test for all possible cases. These cases are pretty simple to figure out, cause your tile-set will actually show you which ones you have to think about. So for every wall tile in your tile-set, check what the IF statement should be, and add that code.

What you’ll end up is something in the direction of this:


The Checkcorners function simply checks in the 8 directions (top,right,down,left, and 4 diagonals) and based on that outcome we set the right tile to the rendermap.

Extra tip: start with the “easy” walls, so the simple horizontal+vertical ones I explained above, then the outer-corner tiles, then the inner corner tiles, and after that do the “weird” ones like T caps, etc.

If you got questions about this, drop me a comment below!


Bookmark the permalink.