2D Pac-Man style Maze Generation

procedural maze generation

A simple method for procedurally generating 2D mazes as used in Chompston. The generated maps are “Pac-Man style” in that they are a series of interconnected corridors with no dead ends.

The maze is produced by placing a number of “builders” in an empty map and then carving out paths by moving them simultaneously until none remain. Essentially it’s a simulation of a multiplayer Light Cycles type of game performed by unskilled participants.

maze generation 01

First we place a number of spawners in random locations in our world – a typical 2D array of cells which is initially the map is filled with solid cells.

maze generation 02

At the spawner locations we place between two and four builders, each of which is assigned one of the four available directions. There needs to be at least two builders per spawner to avoid dead-ends in our maze. Each builder is also assigned a random distance it will travel before changing direction.

maze generation 03

All the builders are moved simultaneously in their current direction, clearing cells in the map as they go. With every iteration each builder’s remaining travel distance is decreased.

maze generation 04

When a builder’s remaining travel distance reaches zero it turns 90 degress left or right and we assign a new random distance to it. If they reach the edge of the world they also turn left or right.

maze generation 05

A builder is removed when it encounters an empty cell, so we just need to continue iterating until none remain.

maze generation 06

The finished maze, but we’ve produced a few little rooms so it’s not strictly Pac-Man-like.

maze generation 07

Ensuring our maze comprises single-block wide corridors is simple – constrain the placement of spawners to every other column and row of the 2D grid and always move the builders for an even number of steps before changing direction.

maze_generation_08

In this instance the maze was generated by placing five spawners and moving each builder between two and ten blocks before changing direction. By encapsulating the generation algorithm in a function that accepts a few parameters we gain some control of the flavour of the maps it produces. So with a maze generation function defined as:

BuildMap(Spawners,MinimumDistance,MaximumDistance)

the random mazes produced have characteristics similar to the following:

BuildMap(3,8,12)
BuildMap(3,8,12)
BuildMap(6,2,4)
BuildMap(6,2,4)
BuildMap(12,8,20)
BuildMap(12,8,20)

Implementing this technique for procedural maze generation in Chompston allowed me to progressively increase the complexity of the random maps through successive levels.