Discover more from AI and Games
How the Rats Work in 'A Plague Tale'
Pathfinding AI at Scale in Contemporary Gaming
AI and Games is a YouTube series made possible thanks to crowdfunding on Patreon as well as right here with paid subscriptions on Substack.
Support the show to have your name in video credits, contribute to future episode topics, watch content in early access and receive exclusive supporters-only content.
AI and Games is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.
Rats, rats, rats...
There are many highlights of playing A Plague Tale by Asobo Studios, be it the emotional story of Amicia and Hugo as they seek to escape the Inquisition, witnessing the horrors that befell 14th century France in this fictional telling of the black plague, or the emphasis on puzzle solving and evasion rather an outright combat.
But yeah, at the end of the day, people are gonna be talking about the rats.
I mean they're an impressive technical achievement, often with hundreds of the wee critters appearing on-screen at once during 2019's A Plague Tale: Innocence. Meanwhile, the 2022 sequel A Plague Tale: Requiem sometimes has thousands flooding the screen at once.
For this issue of AI and Games, I'm going to explain how the rats actually work, but also this is a 2-for-1 special, because the secret to A Plague Tale, is that Asobo Studio's rat tech shares an idea that was adopted by the Micro Machines racing games released almost 30 years earlier.
About the Games
The Plague Tale games follow the journey of Amicia and Hugo de Rune, two young survivors of the massacre of their hometown by the French Inquisition, all while France is at war with England during the Hundred Years War. Their scramble for survival leads them into contact with hordes of rats that are responsible for spreading the black death, the bubonic plague pandemic that killed millions of people in the mid-1300s. The Plague Tale series is part survival horror, part puzzle game, as is inspired by the likes of Naughty Dog's The Last of Us and Starbreeze Studios Brothers: A Tale of Two Sons. As Amicia, you guide Hugo through treacherous environments as you seek to avoid the soldiers looking to kill you, while utilising fire and other environmental features to keep the swarms of rats at bay, or even use enemies as bait, all in an effort to navigate the terrain they've infested.
The rats implemented in The Plague Tale games are an impressive technical feat, in that at first, the developers weren't sure it was even plausible, and it led to a lot of experimentation to figure out what was attainable. But the end result in A Plague Tale: Innocence, resulted in up to 5000 of the wee critters at its absolute limit. Meanwhile in the 2022 sequel, A Plague Tale: Requiem, they managed to boost the upper bound and optimise the rats such that they have could around 300,000 rats on screen at most. A significant increase. And it's important to state that at all times, the rats are powered by an AI system. It's not using particle systems to enable their behaviour, like say the fish in Sea of Thieves, or animated textures often used for crowds in sports games like FIFA.
But okay, enough of the platitudes, how does it work? And what does it have to do with a NES game from the 1990s? Okay, stay with me, the rat AI works, by making them incredibly dumb, and moving the entire decision-making process out of the rat’s brains, but instead, onto the ground itself.
How the Rat AI Works
The rats in A Plague's Tale are only making one key decision at any point in time: which direction do you move towards? But that decision is being influenced by the environment around them: they want to kill any and all human characters they encounter, but they're scared of light sources. Given if they're caught in the light, they will die. This isn't a particularly complicated process, but given we're running it at scale. This is naturally going to be a computational nightmare if we try to path for each individual rat during every second of gameplay.
In previous episodes, we've discussed the most common way to navigate in a 3D video game, and that's through the use of a navigation mesh. The core concept is that there's an underlying grid or mesh structure that sits on the ground, and it tells a given character how to move through that space. For a more comprehensive overview of navigation meshes, please consult my AI 101 video (above), or catch the episode on the AI of Death Stranding, given the bulk of that episode is figuring out navigation in a large open-world environment.
Navmeshes work fine in the majority of cases, but when we consider trying to pathfind for hundreds of rats at once, it's actually detrimental to the needs of the game. First of all, it's quite expensive, maybe not for one character, but for hundreds at once it certainly will be. Plus, the resulting paths on navmeshes are typically built to compensate for local obstacles, other characters or even things like shallow water and other terrain. This isn't really a requirement for the rats, given they're well... rats. We just need a path that gets us as close to the human non-player characters as possible, while following the flow of the environment and avoiding the light sources.
The solution is to invert the pathfinding approach such that all the calculations and decision-making are taken out of the rats themselves, and instead are embedded in the floor of the game world. In every area of A Plague Tale where the rats are in proximity, the game creates an invisible grid over the floor, and for each square in that grid, it calculates the direction that a rat should move towards if they're on that square based on the position of local human NPCs and light sources.
This is a concept known as Flow Field Pathfinding, an algorithm that can be applied to any 2D grid you can find in a game. As you can see from the diagram, when we calculate the optimal directions towards a particular goal in the grid, all of the arrows imply the flow of a river, right up to the destination. Hence the name of the algorithm, though it's worth stating you'll also find it referred to as Dijkstra Maps, given it's something of a variant of Dijsktra's pathfinding algorithm. Flow Fields allow you to compute navigation in a fixed space that incorporates information about the local environment, and the goals of the AI character. In fact, it's more commonly used in strategy games, such as Supreme Commander 2, and Planetary Annihilation to speed up pathfinding for a myriad of different unit types. And the reason games like those, and A Plague Tale, opt to use them is that once it's calculated the flow field for the current scenario, it can be reused by any and all AI characters that are currently standing inside the grid.
This makes it a perfect fit for A Plague Tale, given once it's been calculated for the current area, all each rat has to do is start moving in the direction it's told to. Plus, it's much faster to compute the flow field once every couple of frames, than it is to calculate movement for each of the rats individually in the same amount of time. Given this relatively short computation time, it also means it can be re-calculated quickly, should a light source in the game be moved or destroyed.
The calculation of the Flow Field is pretty straightforward: the grids are all pre-determined in the game world by level designers given they know that's where the rats are going to be. After which, the game finds every human NPC in the map that's a valid target, and then runs the algorithm to compute the direction to the nearest valid target for the rats.
There is, however, a little more detail added to the process. As discussed already, the rats are told to avoid lights. Hence if a cell of the flow fields is within the range of a light source, the rats are told to move away from it. This isn't just within the active range within which the light will kill the rats, but a little farther afield. It helps showcase the rats actively avoiding the light, particularly when you as Amicia walk towards them holding a torch. Plus, permanent structures in the map that block navigation are given a repulsive force. This means that on the grid squares nearest them, the rats are told to move away from them. Hence this helps maintain the readability of the map and the rats can't inadvertently hop over pieces of geometry.
In the event that the human targets move, or the light sources move, as is possible with the likes of torches and carts, then the grid is recomputed to allow the rats to move once again as intended.
With this flow field pathfinding in place, there were still a lot of smaller adjustments needed in order for the rats to work effectively. The designers came up with a set of rules on how things should work, such that the game felt balanced. This set of rules was referred to as the Dictionary of 'Things to Do with Rats, Humans and Light'.
For example, the design rules dictated how many rats should attack any particular human at once. Rather than allowing the entire swarm to descend on how. Hence once the limit is reached, another target can be selected or they simply wander around in their local area. But also this helped enforce the speed limits of the rats. How fast a rat could travel should allow for a bit of breathing room for the player, but also not enough breathing room for enemy NPCs.
This was in fact a problem at first, given that human enemies could often run away from rats when they're exposed. Which can be done by destroying their lamps. The trick there was to actually slow the guard’s reaction times, rather than their speed. A simple adjustment in making them slower to turn to run, given they're terrified by the swarm of rats headed towards them, was enough to ensure they get devoured the majority of the time.
But this still required final behavioural tweaks, as well as some balancing for when the number of rats exploded to crazy numbers, with the limit in A Plague Tale: Innocence being 5000. This is all part of the process for level of detailing or LODding, which helps balance the performance of the rats. In fact, when you play the Innoncene only the frontmost rats are fully animated, complete with a tail, and red eyes. This only accounts for perhaps the first 400 rats. Meanwhile, the next level of rats share just one animation but are still individual rat models, just without tails and eyes. The final level of detail, only for the most distant of rats, is a simple static mesh. They're essentially little black potatoes. With some mesh blending going on so that groups of them from afar look more like a uniform mass.
There was one final level of detail addition made, but only to the frontmost rats. During playtesting, it was determined that the rats closest to the player appeared too passive, and didn't seem as threatening as they should. One of the last additions made to the rats prior to launch was to have rats that are close to light sources rush in and scuttle away from the light more aggressively. Plus in the event they spot a human, they use a separate animation where they sit upright on their hind legs, before rushing them. This event, when triggered, is also passed onto nearby rats if they're not year aware of the player. Hence you see what looks like collective intelligence, as the rats all react to the player’s presence and try to eat your face.
Now you might be thinking, "This rat tech is super cool, but what on earth does any of this have to do with Micro Machines?" Well, Flow Field Pathfinding is an established concept in and of itself, and it's often used in strategy games. But whenever I think about flow fields, it really gets me thinking about is the AI of Micro Machines.
For anyone under the age of 40, Micro Machines was a 1991 racing game developed by Codemasters inspired by the Galoob toy line. A top-down racer for up to two players, that was originally released on the Nintendo Entertainment System, and was later ported to the Sega Mega Drive, Super Nintendo and Game Boy in 1993, 1994 and 1995 respectively. This first entry in the Micro Machines series had the challenge of figuring out how to ensure AI-controlled race cars stayed on track, but it had to do so knowing that the overall CPU and memory resources were heavily constrained. So how do you ensure each car knows how to drive the track? But also gets back on it in the event it all goes wrong, which in Micro Machines happens rather frequently.
As detailed in the Read Only Memory article by Keith Stuart, the team came up with a rather economic solution that, on the surface is markedly similar to what Asobo did, for A Plague Tale.
‘Essentially you cover the entire map in an invisible layer of arrows which tell each car in which direction to drive whenever it’s in that part of the map,’ says [Andrew] Graham (designer and programmer). ‘So there are arrows all along the track itself, and there are arrows off the track which point back to it. That’s about as far as the AI goes. What I’ve found over the years is that people read a lot more into it!’
It's worth saying that in Micro Machines the arrows are pre-built and fixed on the track, effectively written by hand by the developers, whereas the flow field pathfinding algorithm is run automatically based on the local data. It's essentially the same concept, but flow fields are computed at runtime, and in the case of A Plague Tale, it has to do it at runtime given the fact the light sources and other objects can move. So yes, these are quite different from one another in practice, but conceptually, it's the same thing and resolves a complex decision-making process into something that is far simpler to execute, and players are ultimately none the wiser.
The Plague Tale games are a fun and fairly unique mix of survival horror, puzzle solving, and stealth gameplay that is all reliant on the core tech of the not-so-intelligent rats. The same system as described throughout this video was adopted in both A Plague Tale Innocence and Requiem, with the rats in the sequel being more deadly. The rats at better at reacting to light, more likely to attack on the fringes of light sources and can even climb up walls or operate on multiple floors. But as stated, the most impressive element is how they're optimised, now with 300,000 rats being able to pathfind at once. And it's through one simple yet effective piece of AI tech, that the game’s design goals are realised.
How A Plague Tale: Innocence's rat hordes were made,
Rock Paper Shotgun, 2019
Making the rat-tastic sequel A Plague Tale: Requiem, Game Developer, 2022