Random City Generator

UPDATE: I’ve released the source for this HERE

I’ve recently developed a random city generator and thought my method might be helpful to someone else in the future.

The Problem
The plan is to randomly create a portion of an unlimited sized city. This will work by having a set sized grid with multiple exit points and the buildings & road structure will be randomly generated. Each portion of the city will be created upon entry and the exit points should match up to appear that the player is moving seamlessly between sections of the city. We therefore need to seed the sections so if an area is revisited, it’s the same as before.

The solution
To start with, we’re going to create a multidimensional array of grid city segments to give a 2D map of the whole city. The whole map will be empty to start with but will end with each tile being assigned to a road or a building. We need to determine if there should be any existing exit points on each side of the section based on neighbouring sections. These exit points are essentially roads so the cells between each exit point is a building. The starting point buildings will have to be generated between the roads but they can be of any length. The exits should have the option to be predetermined to allow matching up with neighbouring blocks or if there are no predetermined neighbours the exits will be dynamically generated.

With all of the exits in position we can start filling in our buildings. The buildings are generated in a spiral manner, starting from the top and working clockwise inwards towards the centre. As an example, the first building starts at point 1,1. We start looking right until we find road and this gives us our width, we then look down in the same manner to get our height. We check each square during this process to make sure each square is empty, if it is, we can create a building here. I set a maximum length for the buildings so if we hit this limit without hitting a road, we can randomly pick a length to use.Spiralling  clockwise toward the centre allows us to make sure that all of the area is covered and should reduce an exact grid type look.

Here’s what the final generator is currently producing. It’s not perfect but it makes a good starting point & will work for what I”ve got planned:

Randomly generated City example