Discover more from AI and Games
How Insomniac Designed Marvel's Spider-Man
A (not so) friendly neighbourhood...
AI and Games is made possible thanks to my crowdfunding community 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.
In Marvel's Spider-Man, players seek to defend the citizens of New York City as the beloved web-swinging crime fighter. Not just fighting off everyday thugs, but gun-totting gangsters, technologically enhanced mercenaries, not to mention the myriad of supervillains that threaten the city's very existence. Manhattan is a superhero's playground, and in this episode of AI and Games, we discover how developers Insomniac Games, brought the people of New York to life.
As Peter Parker in the 2018 original and Miles Morales in the 2020 spin-off, players protect New York city, from the machinations of The Kingpin, Mr Negative, Oscorp, Roxxon and more all in a way that stays true to their comic book roots.
For developers Insomniac Games this was a monumental task: a game that took 4 years to create, on a scale and complexity completely beyond that which the studio had built to-date. There are a myriad of AI systems that run throughout both Spider-Man titles, all of which designed to make New York feel alive. From the henchman of Mr Negative and Wilson Fisk, to the traffic that ebbs and flows through Manhattan's concrete jungle, and the everyday civilians that breathe life into the world.
Today we're going to focus on how the AI systems work for all of these characters, and the design considerations required in order for them to work. But keep an eye out for the upcoming episode of my artifacts series, in which we will explore how Insomniac built an entire procedural generation pipeline that not only helped decorate and build the entire open world map, but is responsible for the placement of crimes, NPC activities, civilians, and much more.
WARNING: This article contains spoilers for Marvel’s Spider-Man. With references to specific characters, plot elements and story missions.
For Insomniac Games, the big challenge in crafting enemy AI for Spider-Man, was trying to constrain the scope. When we consider the variety of enemies that Spider-Man faces, this immediately becomes apparent.
There are 5 major factions in the 2018 Spider-Man game:
Random street thugs
The Kingpin's mercenaries
Mr Negative's Demon crew.
The escaped prisoners of the raft.
And the Sable Security teams brought in to maintain order in Manhattan.
Plus most factions typically had enemies that adhere to one of 8 archetypes: melee, shield, baton, pistol, rifle, sniper, rocket and heavy. Not to mention enemies unique to specific factions, such as the sword wielders of the inner demons, or the jetpack soldiers of Sable Security, that help mix things up even further.
Insomniac estimated they required 64 unique AI configurations in order to capture all design requirements, which was significantly more than the 19 classes of enemy that appeared in their previous game ‘Sunset Overdrive’.
Add on top of all of this, once you add on the minor factions - such as Tombstone's thugs, Taskmaster's henchman, and even the likes of the NYPD - and then all of the supervillain boss fights, you begin to see the problem. Insomniac estimated they required 64 unique AI configurations in order to capture all of these requirements, which was significantly more than the 19 classes of enemies that appeared in their previous game - the Xbox One title Sunset Overdrive.
Insomniac's proprietary game engine runs using Behaviour Trees, with the leaf nodes at the very bottom of the tree executed using Finite State Machines. It's a rather popular way to solve AI decision making, given you sort out the high-level problem of what to do using the behaviour tree and then the FSM is responsible for handling the practical needs of executing a particular behaviour. But while this had worked well to-date, it presented a big problem: Sunset Overdrive required 17 behaviour trees to handle the 19 enemy archetypes. It was simply implausible for Insomniac to build enough unique systems to handle all 64 enemy classes during development and maintain them as gameplay evolved. So their approach needed a rethink.
The solution was to change how the finite state machines operated. The behaviour trees were consolidated, to a point that there was only 13 of them: 8 for specific boss battles, and then 5 more enemy archetypes, with four of them all deriving from a main enemy tree. These behaviour trees were smaller, and handle the larger behaviour logic. But now specific behaviours were largely encoded within hierarchical finite state machines. But critically, much of these behaviour was entirely data-driven. What does that mean? Well, we'll dig into that in a second.
But in the meantime, if you're new to AI and Games and none of that technical stuff made sense, be sure to check out my AI 101 episodes on both Behaviour Trees and Finite State Machines, given that will help you get an idea of what all of this technology is doing in the context of the game.
So yeah, like I said, enemy AI, powered by a data-driven finite state machines. Well, what on earth does that mean? It means that rather than making a particular state handle all of the parameters and logic for a given behaviour be hard coded, a lot of that data is being passed in at runtime, and that makes it a lot more flexible and can be used in a myriad of circumstances.
A really good example of this isn't from Spider-Man, but another game I’ve discussed at length on AI and Games: in my videos detailing the AI of F.E.A.R., the finite state machine used for that game's Goal Oriented Action Planner only had three states: move, animate, and use smart object, which was just a fancy way of saying 'run an animation that is tied to an object on the map' such as a table or ladder. Each of those states is incredibly vague, given the planner would create the parameters. The planner dictated where the soldier would move towards, or what smart object it should interact with. Meanwhile actions like shooting at the player or throwing grenades, were technically just considered as animations.
Spider-Man takes the exact same approach, but this time it's the behaviour trees combined with the game designers, that's helping feed the parameters to the system. You know what, let's take a look at a specific example, given that will no doubt help make this clearer.
If we consider the enemy bases that Spider-Man will attempt to clear out, such as Kingpin's construction sites, or Mr Negative's hideouts, many of the AI need to be doing specific patrols or other activities prior to Spider-Man interfering and combat kicking off.
This is achieved using a behaviour called BehaviorScripted which builds a queue of actions that the behaviour tree processes into a sequence of calls to state machines to execute. Whenever an NPC has been told to use BehaviorScripted it would run through the queue of actions and execute them in sequence until they're finished. While this could have been achieved using a sequence in the behaviour tree, by making it entirely data-driven, it means that the sequence is being generated dynamically by the data, rather than being hardcoded in the tree.
While this would be evident at the beginning of this combat missions, the part of the game where this is most evident, is the stealth sequences with Mary Jane and Miles Morales, given it allows for AI characters to do very specific sequences of actions.
But while this was a really useful tool for designers, the one place that this system was used more than anywhere else, was in combat.
BehaviourUseCombo was a data-driven behaviour used in combat, and enabled for all of the data required for an enemy to attack Spider-Man to be put into practice. BehaviourUseCombo would use a special system of what was known as Combo Moves, individual attacks that an AI could trigger. In fact there are 39 combo move types in the original 2018 game. Multiples combo moves are stored in a ComboEntry, with all of this stored in what was known as a Combo Config. Each Combo Move would trigger the AI to move between specific states:
Turning to face a given direction
Transitioning to Idle
Running the Combo Move action itself
By storing multiple combo sequences in a ComboConfig, designers could dictate all of the attack combos available to an enemy, what animations they play, the wind up time, the cooldown time between attacks, other critical information such as what conditions would allow for a specific combo entry to be valid, and what distance is required for that attack to become valid.
These are but two of the data-driven behaviours that were available to designers, but critically for the developers, it meant adding new attacks and other actions became very easy to do. Though as Adam Noonchester explained in the GDC AI Post-Mortem of the game, it did lead to it being a little difficult to edit at times, given it's a lot of fields needing configured inside the game engine's editor, and designers needed trained up on how to use it.
Now having explained the underlying AI architecture of the enemies in Spider-Man, we still need to talk about how combat itself is managed. The goal for combat was to have this fast, fluid, dynamic yet cinematic vibe. Enemies should feel dangerous to go up against, while also allowing players opportunities to improvise, changing tactics, or just get into the flow and smack everyone down with the proportional strength of a spider. So let's talk not just about how enemies decide to attack, but when they attack, how they attack and critically how it all feels smooth in the moment.
To make combat feel smooth, there's a secret animation system running in the game referred to as 'synced animations'. Enemies need to run up and take a crack at Spider-Man, while Spidey himself needs to do the same. But his moveset is far more dynamic, he leaps across the combat area, web zips and kicks a guy in the face. That all needs to be synchronised such that it looks smooth and fluid. The synced animation system achieves this by having an invisible sync joint, that is attached to Spider-Man at all times. So when a particular combat move is being executed by the player, the game works to ensure that both Spidey and the thug are lined up corectly, so that when the punch or kick happens, it connects like it should and looks super smooth.
There is in fact, multiple ways that this system can be adopted such that both characters are in the right place at the right time: sometimes Spidey does all of the movement and leaps across to make the hit (known as a Guest-as-Anchor sync joint) but there's also the external anchor, which is actually used when Spider-Man throws an enemy given the player's input is influencing how the animations should line-up. Then there is the Host-as-Anchor configuration, where Spidey doesn't move, but the thug lines up for the shot. Perhaps the most obvious example of this is a web stealth takedown. The character is moved up to a fixed point in the animation, at which point the takedown actually occurs. This often has to accommodate different height differentials, given this move can be used in a variety of circumstances. So while sometimes the enemy travels farther to reach Spidey, the final takedown part of the animation always happens in the same place.
Okay, so we've talked about why combat can look so smooth, but let's get into how combat is managed. The game actually runs two separate combat managers during any given encounter: the melee manager which is responsible for any and all attacks using melee weapons, and also the ranged manager, which is for enemies that throw projectiles or use firearms. Each of these groups of enemies are managed separately, but these two system do actually talk to each other. The two things they're focussed on achieving is that combat, even in its most intense, feels fair, and that enemies are attacking in a way that looks somewhat intelligent.
Each enemy, be they melee or ranged has a cooldown before they're allowed to attack Spider-Man. But they can't attack without permission. Enemies request to be picked for an attack job, but only if there are available tokens for them to hold onto.
So how do enemies decide when to attack? Each enemy, be they melee or ranged has a cooldown before they're allowed to attack Spider-Man. But they can't attack without permission. The game uses a token system, whereby enemies can request that their combat manager pick them for an attack job, but only if there are available job tokens for them to hold onto. The number of tokens available in a given fight, impacts how many enemies can attack the player at once. The melee manager by default only allows for one attack job at any given time, while the ranged manager can have multiple jobs active at any one time. This is a common idea in modern action games, and in fact we have looked at how this is used previously, back in my analysise of the 2016 reboot of DOOM. Spider-Man adopts many of the same ideas as that game but its implemented differently in both the melee and ranged managers.
So for example, a melee enemy can dictate their priority for an attack job. If they've been waiting a long time to attack, and they're in an ideal position they get priority over other enemies. Priority also takes into consideration certain enemy archetypes, most notably bosses. Hence if a boss has henchman and the boss wants to attack, it will get priority over everyone else.
In addition, much like DOOM, melee enemies can steal the active job token from one another if it makes sense. A good example of this is if an enemy was moving into position to attack, and then the player web zipped across the combat area, then another thug that is closer to where Spider-Man landed can steal the token and trigger their own attack. But also, the manager can override the enemies and create proximity jobs, when it recognises that a nearby enemy should take priority. Over time, the melee manager also has a hidden intensity meter that slowly builds up over time, and earns bonuses based on how well Spider-Man is performing. This then allows for special events to be triggered, like allowing simultaneous attacks by temporarily increasing the token limit, or special sequenced attacks like when a heavy attempts to do a punch combo.
There is also one other way in which the token limit can increase, and that's if the player recently dodged. But I'll come back to that in a second, given dodging has a huge impact on how both combat managers operate.
When an enemy plans to shoot you, your spider-sense is projected on screen to help communicate that the shot is about to occur so you can time your dodge. If the character is off screen, the time taken to fire is doubled.
In the meantime, the ranged manager is also balancing how often ranged enemies can attack, with cooldowns on firing weapons much longer than melee attacks. But it has a variety of unique conditions for selecting enemies and how they subsequently act. By default, the ranged manager will prioritise an enemy that's on screen, give if multiple enemies are armed with guns, it makes sense to have the one facing the player take a shot. But it still permits enemies who are offscreen to attack the player, but with a delay. You'll notice that when an enemy plans to shoot you, your spider-sense is projected on screen to help communicate that the shot is about to occur so you can time your dodge. If the character is on screen, that delay is only 0.75 seconds, but if the character is off screen, it's doubled to 1.5 seconds. But while the attack cooldowns are much longer for ranged enemies, there is one condition that changes it, and that's if Spider-Man starts attacking from the air. The ranged manager maintains an air aggression meter, that runs a probability to end a characters attack cooldown prematurely based on how frequently you're attacking in the air. That's adds a bit more risk to aerial combat and forces you to prioritise ranged enemies more if you're in the air, given every attack committed in the air, increases the chance of enemy cooldown ending early, by 50%.
But even with all of this combat management in place, during development, internal playtesting found that quite often the combat felt very cheap. Players felt it was too easy to be ganged up on, or for enemies to successfully hit Spider-Man when they're both attacking at the same time. This led, to two final adjustments.
First of all, the developers added a new rule, that specific actions by the player could cancel attacks in either the melee or ranged manager that had not been started yet, given it would imply that the enemies had trouble keeping tabs on Spider-Man’s movement. Originally, the plan was for this to focus on dodging, but it actually has a long list of possible outcomes:
Using a finisher
Jumping off an enemy
Landing from a jump
And lastly, taking a hit from an enemy.
This system does however have a failsafe, in that if you spam certainly actions repeatedly, it will ignore them. So you can't just jump around like an idiot and nobody bothers to attack you. But, it does allow for multiple successful dodges, given that's very in keeping with Spider-Man's fighting style. However, if it's a boss battle, then it will ignore all of those dodges and the boss will just attack.
The second change, which is super critical to why Spider-Man's combat feels so smooth is a system called 'Beat to the Punch'. If the player and an enemy are going to try and attack one another, there's a system running under the hood that analyses each characters animation to predict which would hit the other first. In the event the difference is small enough, it will hard override the result, and ensure that Spider-Man's attack connects first. This doesn't mean you win every one-to-one moment, but if does mean that enemies getting away with cheap hits is reduced drastically, and places much more emphasis on the players ability rather than dumb luck.
So far all we've talked about is the AI of the enemy characters, but we've missed out something rather significant: the civilians who wander around New York and frequently get caught up in Spider-Man's adventures.
Originally, civilian AI was going to be implemented as dumb characters that moved around slowly and were limited in number. But this idea was nixed pretty quickly, given that it worked fine when Spider-Man was swinging through the streets up high, but as soon as you're down on the ground, the city would look barren and empty - which is most certainly not how New York is in real life. The original pedestrian NPC system had them act as fully processing non-player characters. But this created a different problem: in that the game could only handle around 30 full NPC AI processes at once, while ensuring it maintained the 33ms frame processing rate that the tech team was enforcing.
This immediately becomes a problem as soon as you're in a densely populated area of the city, or if a crime or story event takes place in the middle of the city, as civilians run around in a panic as Spidey faces off against a gang of armed thugs. Hence the game uses a highly aggressive level of detail, or LOD system, that ensures only the characters that need to be acting intelligently will do so, while everyone else is doing just enough to maintain the fiction. This meant that when the game was shipped, 2018's Spider-Man could have up to 2000 pedestrians wandering the city at the same time.
The trick with any level of detail system, is to prioritise based on distance and interaction. A topic I explored not that long ago with The Plague Tale games, which figured out how to get thousands of rats on screen at once, while each running simple AI behaviours.
In any given scenario, there are only anywhere between 200 and 400 'real' pedestrians acting in the city at once. The game can only render around 200 of them at once, and even then only around 100 of them will be animated, with the game prioritising those closest to the player. In fact, the games uses highly aggressive culling, meaning that as soon as a civilian is no longer on screen, it will stop animating to save resources. These front-facing civilians operate in one of two ways depending on their configuration: there are low-level civilians that run away from conflict, but also wave at the player, or take pictures if you're walking past. And then there are high-intensity civilians, these are low-level AI that, for a short time, are promoted to fully-active AI non-player characters. These are typically used for things like high-fives (which are another synced animation much like combat), or they'll report a nearby crime or point of interest, or in even rarer cases, trigger an ambush and spawn in enemies.
But what about the rest of the city? Well, outside this limit of 400 pedestrians, the game will have anywhere from 1000 to 2000 fake pedestrians nicknamed puppets. These puppets aren't even fully rendered or animated, they're low-bake character models that shimmy around a little on the spot. Just enough that from a distance you think they're moving around, and all in an effort to help bring the city to life.
All of these civilians are loaded in and out of the city based on the secret tiling system that runs in the game engine, prioritising high quality assets near Spider-Man and unloading everything else when it's not needed. To find out more about how that works, check out the aforementioned artifacts episode dedicated to Spider-Man and its procedural world generation workflow.
To prepare this episode, I went through over a dozen sources and hours of content. But to keep this fun and engaging, naturally some stuff gets left on the cutting room floor, unless of course, I tell you them all very quickly. Time for a speed round!
The team that builds the AI for Marvel's Spider-Man is technically the gameplay team. They also handle all of the combat and relevant mechanics for interacting with NPCs. In fact, this is markedly similar to how it was done in that other critically acclaimed superhero series: the Batman Arkham franchise by Rocksteady Games.
Plus, while this was to quote Insomniac's Elan Ruskin "the biggest responsibility they had ever faced", the gameplay programming team was smaller than that on Sunset Overdrive.
Returning to enemy AI, thugs will often deliberately stand underneath perch points, or have patrols that wander past them, enabling for easier takedowns.
Plus enemies will also frequently stand near interactables, to once again make it easier for you to get that cool takedown moment.
On the subject of taking down enemies, in earlier builds of the game, Spider-Man quipped much more, and often made jokes as he took down regular goons - just like in the comics. While much of this is kept in for the bigger boss battles, most of it was toned down or stripped out for regular combat given the developers began to find it incredibly annoying as the same lines of dialogue were repeated again, and again and again...
There's an entire procedural webbing system that was built to ensure that when you web up an enemy, notably with the likes of impact webs, that the web covers and sticks to the surface as naturally as possible.
All of the components in the game engine actually run concurrently, meaning all animation is processed, then all AI is processed, and it actually made it difficult for developers to dictate the order in which enemy AI's act in a given frame.
The positioning of enemies during combat is reliant on a gradient-descent hill climber algorithm with placement occurring on both inner and outer rings with ranged enemies often sticking to the outside. Enemies prioritise being inside the nav mesh, not overlapping with each other, but also not being too close to Spider-Man himself.
As the difficulty of the game increases, the AI largely stays the same. It's just the timing of attacks and the cooldown between attacks that gets adjusted. This isn't just customised per difficulty level, each faction has customised difficulty settings, and the game itself has difficulty adjustments for the AI that kick in as you move between story acts.
Ranged attacks in particular have increased accuracy the higher the difficulty you've selected. But, regardless of difficulty level, the first shot fired from a ranged attack will always miss.
Earlier I said there are 39 combo move types in the game. Around 50% of them are built solely for the boss battles. There's only two bosses that don't use them: the Shocker and Mr Negative, but only for his boss fight in the subway.
Given you fight some bosses more than once, they technically have multiple AI configurations and codebases to address how they behave in each interaction.
Earlier I stated that the game can only handle around 30 active AI's at the same time, this doesn't just factor in the civilian AI, it also includes the traffic.
You'll sometimes notice civilians hanging out and completing some sort of activity together, like shooting hoops, or having a drink. These are technically called vignettes.
Some enemies fly, which is a pain in the ass because navigation meshes don't support flight. So while Electro and Vulture fly around everywhere, their navigation paths are entirely hand authored by designers.
Meanwhile the other flying enemies: the jetpack soldiers from Sable International technically follow the navmesh with a vertical offset, even when they really don't need to.
This also means the entire combat sequence when Mr Negative attacks the armoured Sable truck had to rely on a custom solution given none of the AI was built to support it.
Last but not least, player's will have noticed when you wander around near civilians, you can't punch them. You simply wave and do finger guns. The gameplay logic hasn't changed, you're still trying to trigger a punch. The devs simply swap the punch animation out for when there's no enemies nearby and you're surrounded by civilians.
While Sony's first-party offerings are often highly regarded, Insomniac's Spider-Man series not only set a new standard for superhero video games, but elevated the studios status as one of the most critically acclaimed teams in the industry. But as we've seen today, the level of detail and effort to achieve their creative vision was no small feat, and even now I'm only giving a small insight into the level of creativity and technical ingenuity required to bring it all to life. Insomniac understood the assignment, that with great power, there must also come great responsibility.
The following talks were used as part of my Spider-Man episodes, you might enjoy digging into these to find out more information:
"Marvel's Spider-Man: AI Postmortem",
Adam Noonchester, GDC 2019
"Building Traversal in Marvel's Spider-Man",
Doug Sheahan, GDC 2019
"Conquering the Creative Challenges in Marvel's Spider-Man",
Bryan Intihar, GDC 2019
"Marvel's Spider-Man: A Technical Postmortem",
Elan Ruskin, GDC 2019
"Marvel's Spider-Man, meet Houdini",
David Santiago, GDC 2019
"Procedurally Crafting Manhattan for Marvel's Spider-Man",
David Santiago, GDC 2019
"Building New York in Marvel's Spider-Man: It's Still Just Level Design",
Josue Benavidez, GDC 2019
"Marvel's Spider-Man: Procedural Lighting Tools" Xray Halperin, GDC 2019
"Marvel's Spider-Man: A Deep Dive Into the Look Creation of Manhattan", Matt McAuliffe, Brian Mullen and Ryan Benn, GDC 2019