Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

Creation of Zergification tech demo: Town layout generation

Sign in to follow this  


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


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.
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.

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

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

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

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.

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.

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.

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.
Sign in to follow this  

1 Comment

Recommended Comments

Very cool write-up. It's always neat to see how someone else solves a cool problem.

Share this comment

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!