You may already be aware that Spelunky 2 is one of the best games of the year. You may also be aware that it is very, very difficult. One wrong step or a misplaced whip swing can easily send you back to the very beginning. Part of what makes the Spelunky games so challenging is their randomness: levels are generated on the fly with some preset rules, but the overall layouts can vary dramatically. Playtesting every possible level by hand would take centuries. Which is why the developers made an AI to handle the heavy lifting.
While Spelunky 2’s levels might seem fully random, there is a logic to how they’re designed. One of the key rules is that you should be able to make it through just about any level in the game without having to use a bomb or a rope. The path forward may not always be safe, perhaps littered with moles and rolling lizards, but it’s not a path you’d technically need to bomb through. That’s Spelunky 2’s level generator working as intended.
For Spelunky 2, the game’s designer, Derek Yu, enlisted the help of Blitworks as a co-developer. Blitworks is known as one of the preeminent porting houses in the gaming industry (the impressive Switch port of Divinity: Original Sin 2 was Blitworks’ doing, for example), but Spelunky 2 is the first time the studio has worked as a co-developer. Its experience as a porting house means that testing is a massive focus, and the studio has integrated that mentality directly into the development of Spelunky 2 and its complex level generator.
Javier Moya Nájera, the gameplay and AI programmer on Spelunky 2, spoke with Polygon over email about how the team came up with the AI that would ensure that all of the game’s levels passed muster.
“I think that all started with [lead programmer] Micky [Pascual’s] testing system,” said Moya Nájera. The testing system was “creating levels over and over, with the frame speed unleashed, just to try to catch any potential level generation issue.”
If you were to look at the testing system doing its thing (see above), all you’d see are Spelunky 2 levels flickering across the screen at lightning speed as the system checks for errors. It’s an interesting solve for the problem of testing infinite level generation, but it doesn’t exactly recreate the experience of someone playing through those levels.
That’s where the AI comes in. Blitworks programmed an AI to help control Spelunky 2’s Hired Hands — chaotic helpers you can rescue on your quest, usually to the detriment of said quest. These Hired Hands aren’t meant to be geniuses, but they do need to have some understanding of how to make it through a level without being carried.
So Moya Nájera and the team wondered: “What if we add the AI to the main [character in the level tester], just to keep the level moving a bit more and create some interactions? And, after a few changes, we had a primitive level testing where the AI was just trying to reach the exit door without taking into account anything else.”
Unfortunately — and predictably — the AI wasn’t exactly a wiz at the game when starting out.
“I remember that [the AI character] was trying to go through all monsters and traps carelessly, so he got killed most of the time without reaching the exit. As the AI was being developed (adding combat AI, more complex pathfinding, traps handling…), the level testing was improving more and more, and finally it was able to attack monsters, do very complex jumps, and reach the exit door.”
It’s worth considering just how challenging it is to program an AI that has to account for all of the different variables that Spelunky 2 has to offer.
“The game has a lot of items, powerups, monsters, etc. to handle,” said Moya Nájera, “and each one has its own behavior and peculiarities, and the AI has to know all of them to decide how to proceed. It is not the same to [fight] a flying bat, which can be stomped or whipped, [versus] a yeti, which cannot be whipped but [can be] shot, for example.”
Given all of this, the AI in Spelunky 2 has come a long way, but it still makes plenty of boneheaded mistakes… though, don’t we all? While it’s not quite on the level of an actual player, Moya Nájera said it’s still surprisingly entertaining to watch the computer try to beat the game.
“It is completely hypnotic!” said Nájera. “I’ve discovered myself a lot of times just watching the screen to see if the AI could reach the exit, and watching some incredible movements and impossible situations of the AI killing a lot of monsters that were surrounding it easily.” He describes it as “almost like watching a stream.”
Sadly there’s no way to watch the AI do its thing on its own in-game, but if you ever want a sneak peek, try following one of the Hired Hands around and see if it’s able to pull off any slick moves. Remember, it’s probably played way more levels of Spelunky 2 than you have.