Introduction

Last time we made a Roguelike Dungeon Generator. This time we’ll make something a lot simpler.. a Zelda Dungeon Generator.

If you remember Zelda for the NES or the SNES you will know what type of dungeons I’m talking about.

The Dungeon

Zelda dungeons are mainly a grid of fixed size rooms connected:

There are some things to take into account when procedurally designing this type of dungeons:

Each room must be reachable from anywhere on the map. A room could be “locked” by a key. That key must be in a place the “Hero” can reach. Each room should be aware of its neighbors and position inside the dungeon, so we can put some semantics in there (increase the “danger level of the room”, etc..).

Algorithm

We need a tree structure. For this demo we’ll use a Binary Tree. That means that each room may have up to 3 connected rooms (the parent and the 2 children).

Our algorithm does the following:

Create the initial room in a random position. Check if there’s an empty neighbor. Build first child room. Check if there’s another empty neighbor. Build second child room. Repeat step 2 for every child.

Mainly, our algorithm won’t stop until every room has the opportunity to “reproduce”. We could put a condition for randomly stopping the recursion if we don’t want to fill the dungeon. We could also modify the script to build more (or less) children.

As you see, the idea is quite simple… but what’s really interesting about a Binary (or any) Tree structure? Check the next image from the “300 Mechanics” web:

Imagine that this is our dungeon. We have also a Tree Structure, so when we lock a room, we lock all its children. That means that we can lock a room (a “subtree”) and place the key at any room that is not inside that subtree. You should maintain a list of locked subtrees while you do this.

You build the “puzzles” backwards, from end nodes to the first node:

Lock subtree.

Add subtree to list of locked subtrees.

Place key in any subtree not “locked”.

Repeat for any “open” subtree.

If we wanted to close all “B” subtree, we just need to lock the “B” door and place the key in the “A” room or the “C” subtree.

Unity Package

As always, here’s the Unity Package with a demo to test the algorithm. DOWNLOAD



Also, I’ll publish this demo to the Asset Store for free to get more people to try it.

Greener = Older generations

If you have any questions please just ask :)

Follow my tumblr or twitter for more gamedev related posts.

Regards,

David León.