Procedural Generation Pattern: Puzzle Pieces
I have previously talked about level generation that can produce infinite terrain, a simple method for generating levels that satisfy puzzle solution constraints, and levels composed of pre-designed, self-contained sections.
The Pattern
The puzzle pieces approach to level generation relies on pre-designed modules that are combined according to local constraints: Modules boundaries are labelled with a “connection type”, and like
puzzle pieces, only modules with compatible connection labels can fit
together. For example, a module with an road exiting to the right can be placed to the left of a module with an road to the right, so the roads will connect.
I made these literal “puzzle pieces” (based on the platformer tileset “Candy Land” by MadByte from opengameart.org) to illustrate the concept further. There are three kinds of connectors I will call “cave wall“, “water“ and “open land“. Only puzzle pieces with fitting connectors can be out together.
The puzzle piece/module pattern looks similar to the “deck of cards” approach mentioned above, but instead of self-contained “rooms”, the generated level consists of open, interconnected puzzle pieces that fit together.
Boundaries between modules do not have special significance to gameplay (in the deck of cards pattern, each room must be solved individually before the player can move on), and module boundaries are not emphasised by the game. There are different kinds of connections between modules (in the deck of cards pattern, there is one kind of doorway only).
Ideally, the juxtapositions of different modules lead to different gameplay dynamics: If the main challenge of the game lies within individual modules, and the module boundaries are clearly visible, you lose the main advantage of this method, and turn the level into a sequence of a small number of disconnected sections, instead of a large space of possible combined levels. In the generated platformer level above, you can see how the two cave levels look very self-contained, while the second level, shown below here, looks much more organic.
Maze Generation
You can combine two-dimensional maze generation algorithms, on a coarser scale, with puzzle pieces. This ensures that every module in your level is reachable.
You can also pre-generate parts of the level based on a more purposeful algorithm and then fill the rest with random modules that fit into the existing connections.
Complete Tiling
If you want to make sure that you can easily tile a 2D or 3D level without complex constraint solving techniques or backtracking, you must design modules for all possible combinations of connection labels. Otherwise you can “paint yourself into a corner“ and generate “gaps“ that cannot be filled by any existing module. In the example above, there is no module that connects a “cave wall“ to a “water“ edge, but since the modules exist in only one dimension, this is not a problem.
Complete tilesets can also be combined with gradient noise to produce biomes or terrain shapes which serve as module connection types: http://www.cr31.co.uk/stagecast/wang/blob_g.html
With complete tile sets, you can start at any point and greedily, incrementally fill in tiles at any position.
Roads
If you restrict certain tile types, you can enforce global constraints by satisfying local connection constraints. For example, on a 2D rectangular tile grid, if you only allow tiles with one road entry and one road exit (i.e. no crossings, T-junctions or dead ends), then every consistent tiling consists of only loops. If you start with only two dead ends and fill the edges of the map with non-road tiles, and only use the tiles mentioned above, the two dead ends are guaranteed to be connected in every solution.
Games
Spelunky lays out a path for the player to follow, similar to the solve-then-generate method, and populates that path with connected modules that form a corridor to the exit. The other modules may or may not be connected to the rest of the level, and can sometimes only be accessed by spending bombs or ropes. Modules are further customised by adding traps, loot and monsters. (http://makegames.tumblr.com/post/4061040007/the-full-spelunky-on-spelunky)
Torchlight I&II (https://www.runicgames.com/blog/2011/05/20/making-the-world-of-torchlight-ii/)
Eldritch starts by generating a maze in 3D. Then the generator removes some maze walls, and populates the resulting grid with predesigned modules. (http://www.gdcvault.com/play/1022110/Level-Design-in-a-Day)
Aesthetics
Map generation based on puzzle piece modules allows the game designer to exert a high amount of control over the generated maps. Maps can be varied, challenging, and unpredictable, while requiring only a moderate amount of pre-designed content.
Puzzle-piece-based levels can guide the player along a path, but still feel open, organic and non-linear. By obscuring module boundaries, e.g. by not always putting walls at module boundaries, or loot dead in the center, we can draw attention away from the generation algorithm and toward the scale at which gameplay is happening.