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.
http://s6.postimg.org/in3spyr0h/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.
http://s6.postimg.org/dm1glppvl/Generation_animation2.gif
Then I select few random square areas and set their values to the average:
http://s6.postimg.org/tybibg475/Generation_animation3.gif
Finally, I take care of the location next to the gate:
http://s6.postimg.org/a4zeiqqtd/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
http://s6.postimg.org/gl8d8tzcx/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.
http://s6.postimg.org/bb3ehjf41/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.
http://s6.postimg.org/4m1gf9j5t/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.
http://s6.postimg.org/vxwpglnwh/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.