Generative Grammars

Generative grammars are a class of techniques used to create dungeons based on what actions the player needs to perform to win the game level. They are similar to L-systems in that there are production rules and rewriting, however generative grammars work on graphs instead of strings.

Generative Grammar
There are many ways to implement generative grammars in detail, so a general form of the algorithm is shown here. The algorithm works on graphs, which are composed of nodes and edges. It also uses production rules, which are key-value pairs used to find subgraphs in the main graph and expand it.

Generative Grammar Algorithm
create a set of production rules create the start of the main graph for a desired number of iterations: create a temporary graph that matches the main graph for each production rule: find each instance of the production rule's key in the temporary graph replace the instances of that production rule's key with its value set the main graph to match the temporary graph

Generative Grammar Algorithm Explained
First it is important to understand what production rules look like. These are phrases or symbols representing actions the player has to do, such as "finish level," "fight main boss," "pick up key," and so on. The key of the production rule is a more general phrase, such as "finish level," and the value that is put into the main graph are the needed steps: "pick up key, fight an orc, drink potion, unlock door." Branching is allowed: the player may have more than one option: "fight the bridge troll" versus "swim across the river." The more iterations used to generate the graph, the longer and more complex the graph becomes.

The main loop of the algorithm is relatively simple. Go through the production rules and wherever it is found in the graph, replace it with the actions associated with it. Coming up with a good starting graph and good production rules is the most challenging part of generative grammars. This can be done manually or be created using other procedural generation methods.

It is important to note this algorithm says nothing about actual layout of dungeons or their visual properties. It just creates an abstract "to do" list. Dungeons may be rendered as plain square rooms in a line, placed in a spiral, or even made using procedural generation methods like Simplex noise or cellular automata.