Endure and Survive: the AI of The Last of Us

June 17, 2020
protect

<iframe title="Endure and Survive: the AI of The Last of Us | AI and Games #52" src="//www.youtube.com/embed/7MVzKuQ5SfY?enablejsapi=1&amp;origin=https%3A%2F%2Fwww.gamedeveloper.com" height="360px" width="100%" data-testid="iframe" loading="lazy" scrolling="auto" class="optanon-category-C0004 ot-vscat-C0004 " data-gtm-yt-inspected-91172384_163="true" id="294589486" data-gtm-yt-inspected-91172384_165="true" data-gtm-yt-inspected-113="true"></iframe>

'AI and Games' is a crowdfunded YouTube series that explores research and applications of artificial intelligence in video games.  You can support this work by visiting my Patreon page.

The Last of Us is one of the Playstation's most revered titles. Players must guide Joel and Ellie through a world left in ruin, as rogue factions of humanity war with one another all the while the fungal brain-altering virus that has swept the globe transforms the infected into violent mindless creatures. It's a game driven by cinematic spectacle, of human stories set against an inhuman reality. And while players take charge of cynical and embittered Joel, artificial intelligence helps put together the rest of the dramatic performance, be it ally, enemy or infected. In this case study, we're going to explore the inner workings of The Last of Us: the design philosophies that drove its development, the AI technologies adopted and how developers Naughty Dog crafted an experience where players were made to feel the emotional weight of every enemy kill.

About the Game

The Last of Us is a third-person action-adventure game with a large focus on cover shooting and stealth. As players make their way across post-apocalyptic America, players come into contact with two types of opposing forces: Hunters - the humans who patrol and control regions of territory around the country and the Infected, the mindless crazed creatures that are all that remain of those infected by the fungal plague.

As the game began development in 2009, one of the earliest design principles was ensuring that players recognised the dramatic impact of taking another life in a world built atop those that had fallen.

"When we started prototyping the human enemy AI, we began with this question: How do we make the player believe that their enemies are real enough that they feel bad about killing them? Answering that one question drove the entire design of the enemy AI. Answering that question required more than just hiring the best voice actors, the best modelers, and the best animators, although it did require all of those things. It also required solving an AI problem. Because if we couldn’t make the player believe that these roving bands of survivors were thinking and acting together like real people, then no amount of perfectly presented mocap was going to prevent the player from being pulled out of the game whenever an NPC took cover on the wrong side of a doorway or walked in front of his friend’s line of fire."

Travis McIntosh, "Human Enemy AI in The Last of Us", Game AI Pro, Volume 2, Chapter 34, 2015.

Hence there's a need not just for the hunters, to appear intelligent, coordinated and ruthless, but also for the Infected to feel just as if not more threatening thanks to their more chaotic and aggressive behaviour. On top of all of this, there is, of course, Ellie: the young woman who Joel is tasked with providing safe passage across the country to the Fireflies. Ellie is in many respects the player's avatar within the story. Unlike Joel who is all too aware of the horrors of the outside world, Ellie has no idea what awaits beyond the quarantine walls of Boston. She reacts to the world and the drama that unfolds as and when it happens and it was critical that players developed a relationship with her in much the same way that Joel does.

AI Architecture

Before I can explain how the different AI characters behave in The Last of Us, I need to take a moment to explain the underlying architecture that they're built upon. But more critically, I need to explain why Naughty Dog built it the way that they did.

The Last of Us AI is built using Finite State Machines (FSMs), a long-established approach to crafting AI behaviours. FSMs were popularised by Half Life back in 1998 as means through which to structure individual intelligent behaviours as individual states. This means a character could be attacking a target or searching a location until an event triggers in the game that forces the character to transition from one state of another. If you want to know more of the inner workings of FSM, go check out that video.

A simple FSM with two states.

The AI of The Last of Us is built around the idea of Skills and Behaviours: skills are high-level ideas of what a character might be doing. For a Hunter, this could be investigating a disturbance, hiding behind cover or flanking the player. Meanwhile for the Infected this could be wandering around the map or giving chase to an opponent. In each of these cases, they use smaller more specific actions in the world, be it moving to locations, interacting with or reacting to objects in the world in order to make that skill look intelligent. And that's where behaviours kick in.

Behaviours are specific concrete actions that all characters might execute, but how they do it will differ from one another. Hence if a character is moving from A to B or attacking the player with a melee attack, how they complete those actions and the animations performed will differ if they're a human hunter or an infected clicker. The idea is that behaviours are reusable and it's only when they're executed by different characters you see how the exact same action is performed differently.

Each skill - which acts as a state within the finite state machine - contains its own state machine comprised of behaviours. This hierarchical FSM is a well-worn technique for behaviour management and I discussed how this is still used in games as recent as the 2016 reboot of DOOM back in episode 30. But the critical part of all this, is that each skill and behaviour is modular and contained in and of itself.

By building a more modular and decoupled system, it allows for a lot of iteration by designers without overwhelming the programmers with feature requests or the need for bespoke tweaks in certain parts of the code. This allowed for a lot more development energy and time to be focussed on playtesting each character type, ensuring it works as intended, refining features, rapidly prototyping new ideas or just outright scrapping ones that weren't working. All the while striving to achieve their design goals I mentioned earlier.

"The best way to achieve these goals is to make our characters not stupid before making them smart. Characters give the illusion of intelligence when they are placed in well thought-out setups, are responsive to the player, play convincing animations and sounds,and behave in interesting ways. Yet all of this is easily undermined when they mindlessly run into walls or do any of the endless variety of things that plague AI characters. Not only does eliminating these glitches provide a more polished experience, but it is amazing how much intelligence is attributed to characters that simply don't do stupid things."

"As a general rule, characters don’t need complex high-level decision-making logic in order to be believable and compelling and to give the illusion of intelligence. What they need is to appear grounded by reacting to and interacting with the world around them in believable ways."

Mark Botta, "Infected AI in The Last of Us", Game AI Pro, Volume 2, Chapter 33, 2015.

But even with a good suite of tools available, it doesn't mean that the game will come together without continued experimentation. As Naughty Dog sought to achieve their design vision, numerous elements of the friendly and enemy AI were drastically altered or revised during production. With some of the enemy archetypes and even Ellie's final behaviour system only coming together in the closing five months before the game was launched on Playstation 3 back in 2013.

So now that we know how the core architecture of the game's AI works, let's take a look at how each of the AI characters was designed in The Last of Us.

Hunters

So first let's take a look at the human enemies, known as the Hunters. The Hunters are designed such that each and every one of them should be a credible threat. That without consideration and care, they could easily kill you but also that they put up a fight in return and are not mere cannon fodder. The Hunters are designed to appear coordinated, to systematically hunt down and eliminate the player all while caring for their own personal safety, but also crafted such that they communicate their behaviour, allowing the player to respond in kind.

One of the first things to talk about is how the Hunters and other AI can detect the player in the world. The Hunters have both vision and audio sensors to detect disturbances in the world, they run on what is arguably the default audio and visual sense levels, and as we'll see later in this video, the sensitivity of sensors is changed quite drastically for each of the four Infected archetypes.

Uncharted View Cones

  • The Last of Us View Cones

First up let's talk vision, NPCs use a variety of view cones - a point discussed heavily in the most blog posts - for detecting the player within the space. The Last of Us originally used the same view cones adopted by Naughty Dog for the Uncharted series, but this didn't really work. Players were spotted too quickly at distance but also were largely on noticed at close range. They didn't fit the pace of the game, and hence the viewcones used in The Last of Us, much like Splinter Cell: Blackist are not cone-shaped. While in Splinter Cell they look more like a coffin, in The Last of Us it's sort of like a keyhole, but in each case it's the same concept: both provide greater peripheral vision while distant vision is much narrower. In addition, like other stealth games, the player isn't spotted immediately upon standing in the view cone, you have to stay there for a period of time before the Hunter will see you, typically 1-2 seconds, with it typically being shorter in combat as the Hunter to reflect the higher state of awareness. In addition, much like Splinter Cell any NPC that has the player in their view cone runs an additional site test for the detection timer to increase, each NPC runs a raycast from them to a position on Joel's body to see if anything blocks their vision. Originally a character would run raycasts to each joint in Joel's body, but it was a rather inefficient. Eventually the team created two conditions, one where the raycast is aimed at the centre of Joel's chest if the player has not been detected yet, or the top of his head when in combat. It's simple, but it was found to work really well. In addition to this Hunters can hear noises at different levels of severity and priority, but I'm going to come back to

JikGuard.com, a high-tech security service provider focusing on game protection and anti-cheat, is committed to helping game companies solve the problem of cheats and hacks, and providing deeply integrated encryption protection solutions for games.

Read More>>