This is a system for generating the layout of a dungeon for a rogue-like game. The system uses graph grammars in order to expand the dungeon.

The dungeon starts with two linked rooms, a start room and a boss room, like so:

Then by applying rules to it the layout is expanded. The player can define a rule by specifying what structure of rooms must be found and what to then replace or add to those rooms.

For example, this rule:

Says if a start and boss room are found and they are linked: then add an empty room between them. The colour of the circle indicates what side of the rule the set of nodes are on, blue meaning what it is looking for and green what it will be replaced with. Plus, the colouring of the letters show which node this one is equivalent to on the other side of the rule, in order to ensure that edges are correctly reconnected.

When this rule is applied to the original layout the outputted dungeon is:

By building up just a few rules complex dungeons can be created, in the case of the following clip, which shows the full process of creating a rule then generating a dungeon, the dungeon contains: Start (S), Boss (B), Item (I), Empty (Em) and Enemy (Em) rooms.

The system works by loading all of the defined rules and seeing if it can find the required pattern in the dungeon, if so it stores where that pattern is. Once all rules have been tested it then randomly chooses one to apply, the designer can make a rule more likely to be picked by allocating it more ‘tickets’ when it is defined. This means that when the rule is chosen at random it has more chances to win, just like buying lots of lottery tickets.

In order to find where the rule could be applied I developed an algorithm which finds all instances of a given sub graph in a main graph, where in this case the rule is considered the sub graph and the dungeon the main graph.

1. Firstly I compile a list of each node in the sub graph and what nodes it is equivalent to in the main graph. In order for nodes to be considered equivalent they must be of the same room type and the node in the main graph must link to, at least, all the same nodes in the sub graph, but the main graph may also link to extra ones.

2. Next I generate a set of lists, (which contain matchings between nodes in the main and sub graph). Each of the lists in the set contain one matching for each node in the sub graph to its equivalent in the main graph, and the final list contains every possible permutation of these orderings.

Using these matchings I choose one to assign to its rule so that when it comes to choosing which rule to apply it already has a matching to use. The rule is then randomly chosen, using the ticketing system that I mentioned earlier, and then applied. To apply the rule I use a standard node replacement system: by removing links between the matched nodes in the main graph; adding the new nodes then reconnecting all of them.

By applying this several times over with just a couple of simple rules you can easily build complex and unique dungeons.

In the future I hope to implement this into a rogue-like game which will be a mix between Nuclear Throne and The Binding of Isaac.

Here is a link to the code, in case anyone is interested in how it works: https://github.com/DomGeoT/DungeonGraphGrammarGame!