Procedural Level Generation – Block Building part 2

A room made of pillars

A room filled with pillars, generated randomly as a combination of pathways

In part 1 I have defined the building blocks of the Level Genaration Algorithm that we will be using for Wizards’ Duel. Today I will describe in detail the algorithm, but first we need to talk about the level properties.

The Level in itself, in fact, has some properties to fine tune the generation process:

  1. Valid Blocks. A list of references to what Blocks are allowed (Blocks are not a property of the level, so that they can be reused).

  2. Guaranteed Blocks. A list of Blocks that must be generated for the level to be valid.

  3. Starting Block. A reference to what Block used as the first iteration of the algorithm, it can be empty to use a random one.

  4. Ending Block. A reference to what Block used as the first iteration of the algorithm, it can be empty to use a random one.

  5. Minimum Area. The minimum used area for a level to be considered valid.

  6. Maximum Width and Maximum Height. These give a sort of shape to a level, but are used mostly to instantiate the empty level.

  7. Default Tile. At creation, the level is filled with this Tile. Usually the Default Tiles are walls, normally meant to be impassable, for this reason many level generation algorithms also use the term Digger to define themselves or part of the procedure.

Other than that some post process operations may be defined. These can be a number of runs from a Cellular Automaton or some specific algorithms like:

  1. Dead Ends Removal. Removes all the cells that can be accessed from a single side, with respect of the 4 cardinal directions.

  2. Gaps Removal. Change a cell from a wall to open if it could be reached from two sides, with respect of the 4 cardinal directions.

Level Building

First all the level assets (Blocks, Tile Blueprints and Level Properties) are loaded. Then a blank level of the specified size is created, empty.

Blocks list (without variations)

Blocks list (without variations)

The algorithm starts by placing the Starting Room as defined in the level properties. The placement can follow some special rules, like always place it in a certain position on the map, but is otherwise guaranteed to succeed and no special checks is necessary.

Generation Steps

Generation Steps

With the Starting Room also its exits are added to a list of Potential Exits. This list keeps track of what exits can be currently used to connect to another room.

Now the core of the algorithm begins. One of the Potential Exits is randomly selected and a random block, with a matching access, is selected (see the yellow dots in the picture), prioritizing Blocks in the “Guaranteed” category.

The algorithm then checks if the selected room has enough space to be correctly placed.

If there is enough space, the room is drawn on the map and its exits, if any, are added to the Potential Exits lists, while the original exit is removed (see for example the yellow and green rooms). During this step the Block is parsed for special situations, like Conditional Tiles (tiles that can randomly have different properties, like being a wall half of the time) or unique Objects.

If there is not enough space a new room is random selected and the check is performed again. If no rooms are found that can be placed then the exit is closed and the cycle restarts, selecting a new Potential Exit.

Rooms are added until a target number of used cells is reached, thus guaranteeing a level of a specific size.

It can happen, depending on the list of Blocks to be used, that certain combinations may empty the list of Potential Exits before the level has reached the threshold size. In this case the algorithm can simply be restarted.

Finally the Ending Block is placed, attached to one of the remaining exits.

After the level is complete some post-processing may occur to remove spurious elements like dead ends or non connected corridors.

For dead ends it is possible to iteratively remove all cells that can be accessed from only one side until no dead end cells remains.

For gaps between corridors it is possible to convert to open cells all the walls that can be accessed from exactly two opposing sides.

Bear in mind that the order of these post process may affect the end result, see for example the following figure center and right images (with dead ends removal and gaps removal executed first, respectively).

Final Level steps

Final level (left), with dead ends removed (center) and with gaps between corridors closed (right)

Summary

The algorithm can be summarized in the following steps:

  1. Place the starting block, according to the level parameters,

  2. Add its exits to the list of Potential Exits,

  3. While the level has too few open cells:

    1. Randomly select a Potential Exit,

    2. Find a random block that can be connected to the exit and place it,

    3. Add the new block exits to the list of Potential Exits while removing the one that was used,

  4. Select an unused exit and place the end block, accordinf to the level parameters,
  5. Run the post process algorithm (close gaps and dead ends).

 

That wraps up the level generation algorithm. Just by changing the Blocks you can have a lot of variations and generate different kind of level, the following is an example using the same blocks defined above minus the room.

Maze generation

Maze generation

Of course there are a ton of algorithms for Procedural Level Generation, and I will briefly describe them in my next post. What do you think so far? Have you got any suggestions to improve the algorithm? You can always share them in the comments or on twitter.

Thanks for reading.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s