Procedural Room Generation Explained

There are lots of ways to do procedural dungeon generation. I came up with a way that will allow for both really chaotic room designs and fully formed, hand crafted rooms. It’s based on joining smaller patterns together to form the room’s interior. In this post, I’ll do my best to explain what’s going on.

Setup: Defining Patterns

This is a collection of four small primitive patterns, bound in separate colored boxes.
This is a collection of four small primitive patterns, bound in separate colored boxes.

I use the editor to make this room, which contains four basic structures, the patterns. Everything is composed of simple tiles like walls, columns, floor, and water.  Metadata in the room file marks specific tiles to define the pattern area (colored box enclosing the pattern) and two special types of tiles: the inlink and outlink positions. Each pattern has one inlink, shown with an E (for entrance), and one or more outlinks (empty colored squares). These are used to join patterns logically, which I’ll explain later.

For now, let’s walk through each step of the layout algorithm.

1. Create an Empty, Enclosed Room

The blue tiles are empty spaces--I know, it reminds me of Super Mario World too.
The blue tiles are empty spaces–I know, it reminds me of Super Mario World too.

Rooms can be any size–I’m using 31×17 as the default size for now. Each room is enclosed by the basic wall tile. (Walls are automatically blended to their neighbors in a separate algorithm.)

2. Place doorways at the door positions.

The top doorway has a cute little doorstep marker to prevent anything from blocking it off during construction.
The top doorway has a cute little doorstep marker to prevent anything from blocking it off during construction.

Each room has one entrance, and optionally has exits on the three other walls. In this room, we have the entrance at the bottom center, and a single exit at the top center. Some decorative side pillars are added for some ambience.

The arrows are special. An arrow on a tile means that tile is a boundary tile and can be joined to other patterns. The engine knows to start at the bottom arrow, because that’s the entrance door, and that’s where layout begins. The bottom arrow is actually treated as a pattern outlink (explained next).

3. Find a Pattern from the Collection

A pool of pattern candidates is created from the four patterns we defined in the editor earlier. It automatically makes variants that are flipped and rotated in various ways for the ultimate in variety and versatility.

To select a pattern, it goes through each one and tests it against the room’s outlink positions (currently just one, at the entrance door). It checks if the pattern will have all of its floor tiles contained within the room and not intersecting with existing walls or water. Once it has the list of candidates, it randomly selects one, and the layout engine applies it to the room.

It picked the blue pattern from our collection! Excellent choice.
It picked the blue pattern from our collection! Excellent choice.

After the pattern is applied, it clears the outlink marker that was used, so it doesn’t get tested in the next iteration. Instead, we have two exciting new outlinks, provided by the pattern: up and to the right.

4. Repeat step 3

…over and over, until nothing else fits.

5. Ensure a path to the exits

We only have one exit to worry about in this room, so it’s only done once. It essentially does a Photoshop-style “flood fill” of floor tiles starting at the exit’s doorstep position:

We can reach the exit now
We can reach the exit now

This isn’t always necessary, because sometimes the layout engine conveniently places a pattern that enables passage to the exit. The engine will never place a wall blocking the exit, because of the “reserved” doorstep position we added at the beginning.

6. Trim Dead Ends

Anyone who’s played Soulcaster knows how deadly nooks and crannies can be. If you get chased in there, you’re going to have a hard time getting out. So in an act of charity, I turn the unused outlinks (the ones not pointing to floor tiles) into walls.

Much safer
Much safer

Notice it doesn’t get rid of all the dead ends, just the ones created by the dangling pattern links. If I wanted to be truly kind I could have it fully zap the dead ends and ensure circular passages everywhere. But that’s a task for another day.

7. The Finishing Touch

No more empties
No more empties

Leftover tiles are transformed into pretty water. Aesthetics.

So that’s basically it! I’m aware that while this type of chaotic level layout may be interesting, it’s not as cool as having a more pre-planned layout like the old school Soulcasters had. I liked to put concentric circles like walls and moats, have islands with bridges, or an enclosure surrounded by trees that you needed to infiltrate. That stuff’s all coming: the cool thing about this pattern system is that I can define patterns of any size, tag them for special purposes, and link them together however. A pattern could be an entire castle 40×40 tiles in size. It could even leave interior rooms vacant and link into them, populating them as needed.

Spawner placement is the next challenge, something I’ll be tackling this week.

Thanks for reading–and comment if you liked this post and want more of this sort of thing (or if you want to hear about something in particular).

Excitement

I’m constantly coming up with ideas for whatever game I’m working on. I never have time to try them all, so I tend to bank the riskier ones for use in a sequel. But we’re still in the prototype stage in Soulcaster 3, so it’s the perfect opportunity to test a new combat idea I had last week. Once I saw this in action, I felt an excitement I haven’t felt yet in this project.

Sorry for the tiny file, WordPress is acting up and not allowing files larger than 3MB.
Sorry for the tiny file, WordPress is acting up and not allowing files larger than 3MB.

OG Soulcaster players will immediately see what the difference is here. Instead of recalling summons one at a time and placing them only when you have an orb available, you can now queue up “future summons” with markers. As the existing summons are recalled or destroyed, the orbs powering them will look for a marker to inhabit and spawn a new summon. If there aren’t any, the orbs zoom back to the player.

I haven’t exhaustively tested this yet, but in basic test arenas, it allows for much more strategic play. In the original games, if your defensive line collapsed and you had to reform your summons in a good formation, you were struggling to place them while kiting enemies, and since they appeared one at a time, they were way less effective. This way, you can have a whole new formation set up ahead of time to get back on your feet quickly.

Even better, it gives the player something to do while the guardians attack the enemies. You can choose where to take the battle next based on which monsters are appearing.

One new challenge with this system is what to do if the monsters walk over the markers before the orbs get to them. Should they get destroyed? Or do they only break if the orb reaches them and a monster is still walking over them? There are only a few options for what to do, so I will just try out a few things and see what feels best.

Why Soulcaster 3 Will Not Have Persistence Between Runs

I’m a big fan of generosity in game design. My past games have all had infinite lives and quick respawns. So get ready for a break from tradition:

When you die in Soulcaster 3, you lose everything.

There are no unlockables or upgrades you can take with you to make the next run easier or different.

But why, Ian, WHY…?

I realize how polarizing this is. AAA companies spend a lot of money on market research and focus testing, and they are pretty much all in agreement on game presentation: tell the player exactly what they do next, and if they make a mistake or die in the process, start them about five seconds back.  I have no doubt this is how most people like their game experience. It especially makes sense to cater to this if you’re spending $50 million making the game.

It seems like indies are given a bit more license to be brutally difficult. I’m not sure why, but maybe it’s just that those of us who don’t much like the “Disneyland ride” game experience are happy to get a bone thrown our way. So I’m taking this license and running with it, perhaps to an abusive level. (Generosity is not completely dead, either… more on that at the end.)

Scoring in Roguelikes

My new favorite YouTube channel is Matthewmatosis, a collection of in-depth game reviews and commentary. In-depth means really in-depth–30+ minutes in most of the videos. One of the most recent is on Pac-Man and scoring, which explores the phenomenon of the “perfect score” in Pac-Man: something made possible by the fact that a) the game is deterministic (the ghosts’ behavior is predictable and reliable), and b) the game has a finite number of a levels (not by design–but an integer overflow enforces this).

So for Pac-Man, there really is just a perfect score to attain, and the leaderboard shifted to who could do it the fastest. But there is actually a fastest-possible time as well, if you execute everything perfectly. So now it’s just an account of people who can attain the fastest time to the highest score.

Matthewmatosis offers a solution in the form of two missing ingredients: randomness, and unlimited time. They are both required, however, because random game elements (such as Tetris or a roguelike) combined with limited time means that top scorers are mostly at the mercy of the random number generator for a good score. And unlimited time without randomness turns the game into an endurance test, rather than a skill test.