1. Every wall may intersect with some other wall at most once. (this includes the outer wall)
With the above rule in mind, you can create mazes with any type of random proccess (without the need for backtracking or bookkeeping). You don't have to check wether places are still accessible -- they just always are and will be. Back in those days (qbasic on a 268) just drawing randomly from some point in random directions was about ten times faster than the common path walking (backtracking) style algorithms. Taking two seconds instead of a full minute.
Don't take my word for it. Grab a pencil. Start drawing random lines. They may only cross some other line at most once. You'll end up with a fully accessible maze.
The maze may be complicated -- the walls in a proper maze are not. Its a great example of how understanding the invariants and rules that govern your problem domain will help you write faster and simpler code.
If you'd like a more academic look into procedural 2d game generation, there's a nice research paper here, that describes a method and talks about Spelunky a lot (the king of procedural 2d level generation, in my book): http://games.soe.ucsc.edu/sites/default/files/cig10_043CP2_1...
Additionally, Derek Yu open sourced the code to the original Spelunky, and Darius Kazemi created a great breakdown of its level generation techniques here, also with interactive examples: http://tinysubversions.com/spelunkyGen/index.html
The action roguelike genre, particularly the roguelike FPS, is a vital new area being explored by indie game developers. It reminds me of the way 2D platformers were mined, explored, and iterated upon starting around 7 or 8 years ago.
Right now, it doesn't take much to stand out from the herd, as many of the most popular games in the genre don't do much beyond generating a bunch of square rooms and connecting them with doors and short, straight corridors. In my opinion, developers in the genre should take more cues from Doom, and less from original Zelda dungeons moving forward.
And, from a more holistic perspective, nobody really cares about mazes, room placement on a grid, and connective corridors when playing a game, beyond a brute mechanical level. A more useful framework for thinking of generating levels might be to go one level of abstraction higher. Think about a level as a story for your player, and generate setpieces or 'acts' that you want the player to experience as they play. Keep in mind the basics of a good story: an escalation in tension and difficulty, spaced with breathers for rhythm and flow. Place those sets on a path in a map, then figure out a way to connect them together at the lower level with rooms, objects, enemies, and corridors.
> Keep in mind the basics of a good story: an escalation in tension and difficulty, spaced with breathers for rhythm and flow.
It's out of scope for this article, but my little game tries to do some of that. Once the dungeon is generated, it chooses a random starting place for the player. Then it calculates the distance to that tile for every other tile.
When monsters, treasure, and quest goals are placed, those distances are taken into account. Harder monsters and better stuff are placed (roughly) farther from the player, so it feels like there's a general progression in difficulty and reward as you go farther through the dungeon.
There's definitely a lot more I could there.
Now that I think about it, one simple refinement would be:
1. Place all of the monsters first.
2. Recalculate distances and treat tiles near monsters as being "high distance" based on the monster's strength so that paths that go through monsters become "longer".
3. Now place treasure according to that.
That would give you dungeons where monsters "guard" better treasure, and stronger monsters protect better loot.
(Draft in part because some of the figures still need work. I'm a coauthor of the book, but not of this particular chapter.)
I was really excited when I first stumbled onto this book. As you can imagine, it's perfectly in line with my interests. But, when I tried to read it, the prose just killed me. I think there's good ideas in there, but I can't bear to wade through the heavy-handed academic style get there.
I understand writing for a certain venue requires certain stylistic choices, but I really hope you guys can tone it down a bit. Here's a sentence plucked randomly from the linked chapter:
> "Generative grammars were originally devised as a method to formally describe sets of linguistic phrases."
Let me just go through that:
"originally devised" -> "Devise" implies originality.
"as a method" -> This adds nothing to the sentence.
"formally" -> I suppose this matters in some cases but given that the chapter isn't a precise introduction to generative grammars, whether or not it's a formal system doesn't seem very critical to me.
"sets of" -> This adds nothing.
"linquistic" -> What other kinds of phrases are there?
I would edit this sentence down to:
> Generative grammars were invented to describe written text.
I understand academic writing isn't designed to be read for pleasure, but we're all human. If you make the prose more approachable, you'll reach a much wider audience. You have some fantastic material in here, great algorithms, diagrams, and structure. I just feel that the writing style gets in the way of it.
If I could suggest anything, it's that the authors go through the prose and for every phrase ask themselves: "Does this add information? Is there a way to convey the same concept in plainer language?"
One area I've been experimenting in is constraint-based solvers in my generators. It requires keeping track of rooms or regions but allows the algorithm to express ideas like: this room has a health potion but the boss cannot be within 2 rooms of it. You can of course get more creative with your constraints. I'd like to build up the ability to have sophisticated (for some value of sophistication) puzzles built into the dungeons I generate.
I think an often-discounted aspect of JavaScript's appeal is its ability to breathe life into a bunch of fascinating but otherwise dry concepts. Another terrific example is this page by the prolific Mike Bostock: http://bost.ocks.org/mike/algorithms/
I'd like to do more posts like this, but I can see why they aren't common. This one took me forever to finish. Easily eight times as much effort as a regular text + code samples blog post.
I think I'd get faster as I did more in this style, but it's really hard to for me to stay energized on a single concept long enough to finish posts like this.
http://www.gridsagegames.com/blog/2014/06/procedural-map-gen...
http://weblog.jamisbuck.org/2011/2/7/maze-generation-algorit...
One possibility is to straight some corridors in a new final step, but I don't know how well this work in the actual mazes. For example, transform:
-+ +- ==> -----
+-+
and | |
| +- ==> +---
+-+To cop a quote from the OP itself:
"Fundamentally, games are about making decisions from a set of alternatives."
In a hallway, your alternatives are reduced to moving forward and moving backward. When confronted with enemies, you can choose from at most two to attack (ignoring AOE for a moment, since it's usually also strictly less interesting in narrow corridors, as the article mentions as well).
Instead of passageways, I prefer a technique I've seen in Dungeon Crawl Stone Soup: start with an enormous open level (often in a more interesting shape than a square, such as a randomly-generated blob), and generate enclosed rooms within that level. At the limit, you can recursively partition the entire level until it's a series of small interconnected rooms, doing away with passageways altogether.
An honest question, though, is maze generation generally useful? Other than for generating mazes and dungeons, obviously, not that isn't a worthy goal in and of it self.
Here is an approach in Python albeit not as interactive as the mentioned article: http://pixelenvy.ca/wa/ca_cave.html
In case people didn't recognize, the author of the articel is also the author of this book which is for free (kudos!) on the interwebs and can be bought as a eBook. http://gameprogrammingpatterns.com/
One of the techniques I've heard works well is to step up the abstraction level one click, and create interesting environments and set pieces, then combine those to create a compelling environment for your players. One approach to do this uses herringbone Wang tiles to place various precreated environment tiles in a random, pathable way.
http://nothings.org/gamedev/herringbone/
The results are similar, but allow the developer to inject a little bit of human direction along the way.
Another option is to generate non-overlapping bounding boxes for rooms, but then don't fill in that entire box. Instead, you can draw whatever shape you want in there.
The maze generator will then fill in the cracks you leave.
good work