Creation of Zergification tech demo: Town layout generation

Published November 15, 2015
Advertisement

Town generation



Time to explain in depth how I'm actually generating the town. My initial plan was to put all the generation code in the single world_builder class, however, quite soon I realized that it takes a lot of time to compile a class that big and I'll need to recompile it all the time. Because of that I have split the code into five classes:
o Builder_city
o Builder_wall
o Builder_buildings
o Builder_roads
o Builder_edges

Builder_city


The goal of this class is to create a smart looking town layout, save it to multiple 2d arrays and send it to the next builder class.

Step 1: Randomize grid size
I start by randomizing the size of the grids that i'm going to be using. This by changing variables at this stage I can easily change size and
aspect ratio of the town I want to generate.

Step 2: Create terrain shape
Terrain only exist in the town, so by creating shapes of the terrain we are creating town shape. I do this by drawing 5 rectangles. First one is
created in the middle of the grid and do not touch any of the grid the edges. Another 4 start at the middle square and are touching one of the
grid edges.
Generation_animation.gif
When I'm generating bottom square I'm saving its middle point, I'll use it later to create a gate.

Step 3: Generate the height of the terrain
First of all I generate hill shape by cutting terrain horizontally and vertically.
Generation_animation2.gif

Then I select few random square areas and set their values to the average:
Generation_animation3.gif

Finally, I take care of the location next to the gate:
Generation_animation4.gif

Step 4: Create wall array
Creating wall array is really easy. Just copy terrain array and apply this logic for every cell:
If (All surrounding cells empty) -> set cell empty
Else if (None of the surrounding cells are empty) -> set cell empty
Else -> set cell full
Generation_animation5.gif

Step 5: Create road array
I start road creation by adding road seeds. In essence road seed is just a point with direction and length. I add them in the middle of the squares I used to create terrain shape, gate location and a few random areas. Then I create roads out of them, road stops if it reaches a wall or another road.
Generation_animation6.gif

Then it is time to break large empty areas. If the cell is empty and three cells to the left is empty or three cells down is empty set cell to full.
Generation_animation7.gif

Next I check for for narrow areas, where is wall is on one side and cliff on the other. Places like that should always be a road. Finally, I check for cells that have different state than non diagonal surrounding cells and share state with at least one diagonal surrounding cells.
Generation_animation8.gif

Step 6: Create buildings array
Building array is quite simple, it is an inverted road array.

Step 7: Spawn wall builder and send all data to it.

Obviously, no one could live in town with the layout like that, but it does not have to be logical, it just has to look like it. And there are many ways how to make it look logical, even when it is not.

Next time I'm going to write a bit more about wall_builder.
5 likes 1 comments

Comments

Eck
Very cool write-up. It's always neat to see how someone else solves a cool problem.
November 16, 2015 02:57 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement