Procedurally generating enemies, places, and loot in State of Decay 2
One of the trickiest challenges of building an open world game is populating it.
Defining the boundaries and laying out terrain is the easy bit — the real work begins when you need to fill all that vast, empty terrain with life and loot and interesting things to do and see.
It’s an issue that Undead Labs found itself grappling with on a large scale with their upcoming open world zombie survival game, State of Decay 2, and fellow devs may appreciate hearing about how the studio tackled it.
With that in mind, Gamasutra reached out to the studio to see how they handled placing gear, materials, and enemies in a way that felt interesting and intentional without having to do all of the grunt work by hand.
The procedures of generation
“Loot in State of Decay 2 is distributed through a few different procedural systems, each of which serves its own purpose,” says Brian Giaime, State of Decay 2’s systems designer.
“Containers where loot can be procedurally populated are placed by hand, but no loot drop itself on any location is ‘hand placed’. Every time we generate a map, a variety of items and resources and loot and vehicles are distributed according to this process."
"Rather than use pure random odds, we use a ‘deck of cards’ setup, to ensure that, say, out of ten restaurant sites, exactly seven will have one rucksack of food...which helps enormously with long term economy balance."
"Some elements will be the same across multiple instances of the same map; every map guarantees that every Gas Station site has at least one gas can," he continues. "Some elements will be very different. Say for example that across the whole map, we want to drop, say 25 normal melee weapons. These aren’t guaranteed to show up in certain sites, but are instead distributed procedurally across all sites, into containers whitelisted to make sense dropping items of certain implied physical sizes which make sense for them.”
Giaime and his team ensure that while there’s a random element to the procedural population systems, the loot itself is distributed in intelligent ways according to a number of different filters and constraints. One of these filters is determined by the site type that’s being populated.
“Site type guarantees are used to ensure we match player expectations when they go to loot sites that strongly telegraph certain kinds of loot. Construction sites will always have at least one Rucksack of Materials, and Gas Stations will always have at least one gas can item that can be used to refuel a car. This system allows us to precisely dictate what percentage of sites of a given user-facing type drop items of a certain type. Rather than use pure random odds, we use a ‘deck of cards’ setup, to ensure that, say, out of ten restaurant sites, exactly seven will have one rucksack of food. Which seven will be different every time the map is instantiated, but we can rely on seven rucksacks of food being available to the player on each map, which helps enormously with long term economy balance.”
A secondary system influencing drops involves quotas. Every location may have a quota of weapons, for instance, and then each container with a loot table that contains weapons has a random chance of fulfilling that quota, after which no further weapons will drop at that site. Some of this data is then fed back to players as they explore an environment.
“Between quotas and site type guarantees, we fill in data on the map the player sees for any site they’ve scouted, telling them if there exists any potential to find certain resources there,” Giaime told us. “This lets players self-select what resources to pursue with a reasonable amount of confidence that they’ll find what they seek. Rarely 100%, but high enough that the cost in time, health, items, and danger should feel worthwhile.”
In multiplayer, additional loot is distributed in containers that aren’t openable in single player. In an effort to encourage cooperation over competition, the game only allows the player a lootable object belongs to to open it (after which they can share out its contents if they feel so inclined). The team also focused on trying to equally distribute containers throughout larger structures.
“We try to massage loot distribution into multi-room structures by ensuring the total amount of containers we want to activate are spread somewhat evenly through its rooms, and will try and find the next container we need in another room before checking a room with an active container again.”
The clockwork population systems also play a role in the game’s narrative progression.
“Missions in State of Decay 2 try to tell heavily contextualized stories, and the loot system is called upon to help deliver on this,” Giaime explains. “To do so, when missions need to put an item in a site, we use the same whitelists we’d use to decide if the containers in that site would be able to drop that item - so a baseball bat comes out of large storage and not an end table, for example. On the off chance nothing is appropriate to find the loot you seek, all sites have what’s called a ‘fallback’ container - often a large cardboard box, which we can use to supply an item called for by a specific narrative into any site that lacks a more appropriate container.”
Giving your game teeth
Jason Hail, one of State of Decay 2’s gameplay engineers, chimes in to talk about how enemies were placed in relation to structures and loot.
“We have several systems that contribute to the on-going, ambient zombie population in the world of State of Decay 2,” Hail says. “The largest one of these is the Ambient Population Manager. Not surprisingly for an open world zombie game, this was one of our earliest prototypes when we started working in Unreal Engine 4. It has evolved continuously over the entire development process, keeping flexibility and rapid experimentation as our highest priorities. As such we built it in (or at least exposed to) Unreal’s Blueprint system so we could tweak pretty much any aspect of it within a single editor session. We also leveraged another very powerful tool built into Unreal called the Environment Query System, or EQS.”
EQS is a visual tool that enables designers to built spatial queries employed by the game’s AI.
“For example, if you want to create an NPC’s cover-using behavior, you might build the query as ‘find all spots on the navigation mesh, within 20 meters of my current position, and score the ones the highest that cannot be seen/aimed at from any of my enemy’s point-of-view’," Hall continues.
"You can then ask EQS to obtain either the best result, a random result from the top percentile, or just an aggregate list of every location that satisfies the query. The system is very performant since out of the box it is capped on how much processing time it uses per tick, so EQS will never tank your frame rate. The key feature of EQS, though, and the one that enables us to iterate quickly is that it all previews from within editor. You can add, remove, or adjust new queries, filters, or the parameters on either and preview the results on-the-fly.”
The Ambient Population Manager is a workhorse that does a tremendous amount of the labor involved in placing zombies and determining where and when special infected will appear to challenge players.
“Like most open world games, State of Decay 2 maintains an active ‘simulation bubble’ around each player,” Hail adds. “Full-fledged zombies don’t exist everywhere in the world at all times, since that would be a waste of resources. Instead, we only spawn zombies as you move into a new unpopulated area, or kill existing zombies in your current location. As you leave the area, we reduce most zombies to a “probability cloud”, for respawning them when you return to the area. Special infected Freaks and hordes of zombies are an exception to this: we remember exactly where they were last seen, so you can come back to that challenging Juggernaut once you’ve found a grenade launcher and are ready to deal with him. To pull this all together, the system needs to evaluate what you’re doing (walking, driving, etc), evaluate a huge number of locations in the world to find the best spawn points for zombies based on that context, and finally sunset zombies that are no longer within your simulation bubble. All of this work is orchestrated by the Ambient Population Manager.”
"Full-fledged zombies don’t exist everywhere in the world at all times, since that would be a waste of resources. Instead, we only spawn zombies as you move into a new unpopulated area, or kill existing zombies in your current location. As you leave the area, we reduce most zombies to a 'probability cloud.'"
The population manager relies on EQS queries to do its work. Early in development, Hail and the team used two primary queries, one when the player was on foot and one when the player was in a vehicle. Further queries were then layered in for more specific scenarios, like when a player was exploring a dangerous building on foot. Queries were customized to suit the context: in buildings, the priority was spawning zombies outside of the player’s view, for instance.
“It also is responsible for load-balancing the spawns, so that all players in multiplayer have an equitable number of zombies placed near them. One nice side effect of calculating spawn locations on the fly is that we have no hand-placed spawn locations for zombies: you’ll never notice a zombie always spawns in one particular doorway or behind a specific dumpster. We procedurally evaluate thousands of locations per second, in order to find the most appropriate places to spawn threats. Like most of our systems, the exact experience that each player has will be unique for them.”
This allows for a large degree of customization for individual queries and lets designers alter them according to their needs.
“One of the easiest to understand tools for this is by marking off areas of the map as safe in the world,” Hail says. “Of course, designers can hand place safe zones in their maps (which would be appropriate for things like a tutorial) however most of the time they ask the system to declare a room, building, base, or even a neighborhood ‘safe’ or ‘cleared’ at runtime. Safe zones remain safe for an unlimited amount of time, while clear sites are safe only for a short duration. Most of our EQS queries heed these volumes, and simply filter out locations that are safe. Mission-spawning EQS is the exception to this - we allow it to ignore safe zones, if the designer wants to create a tense moment in the story by springing something nasty on you in a place you were led to believe was safe.”
The types and volume of zombies that spawn are also regulated by the Ambient Population Manager according to “recipes” that indicate the percentage chance of a zombie spawning, an optional cap number for that zombie type, and a cooldown that forces the spawner to wait until producing more. These recipes can be altered by everything from mission outcomes, time of day, to incidents like a player breaking down a door.
“Player choice needs to affect this spawning model, or else you risk getting a never-ending flow of zombies from every corner. To prevent this ‘clown car’ spawning effect, we modulate zombie population on a cell-by-cell basis in the map based on recent kills in an area or the adjacent areas. We remember how many zombies you’ve killed, or if you’ve cleared a house nearby, and debit those kills from the available zombie population cap. Over time, this effect fades so that if you kill a horde in a neighborhood you’ll see a lot less zombies there for a while, but eventually new ones trickle in and the area becomes as dangerous as before. Again, determining if a cell of the map should feel cleared is just another filter in EQS: we have a criteria that filters locations out if they fall within a cell that has had a certain number of zombies killed within it recently. That filter also has a criteria for your overall game progress, so we can make areas feel safe for shorter and shorter periods of time as the game creeps towards its conclusion.”
While enemy AI isn’t individually aware of how resources are distributed, Hail points out that “zombies certainly congregate in denser areas of the map like towns and malls — places where the player is likely to search for better resources, with a corresponding risk of finding nasty zombies lurking there. They are drawn to things player does, like making noise — firing guns, knocking into things, or using noisy facilities in your base."
"You’ll also find that unlucky well-armored humans, like police and soldiers, become well-armored zombies when they meet their demise," he adds. "As great as the ammo cache in the army barracks may be, there’s also the very real probability that you’ll find a lot of tough-skinned zombies lingering in that location. As with most of State of Decay 2, a lot of this behavior is emergent, because we model so many different parts of the post-apocalyptic world and allow the systems to cross-talk in order to create believable results.”
Risk versus reward
When it comes to rare and unique items and crafting materials, the risk involved is often determined less by the quality of the item than other environmental factors. Giaime says that while special items are often tucked away in the far corners of a map, the monsters guarding them are determined mostly by the Ambient Population Manager’s pre-determined constraints.
“There are times where we use special ‘override volumes’ placed in the world to artificially juice up the ambient spawn system Jason talked about earlier to steer the simulation towards more challenging encounters, but more typically we use such override volumes to simulate the natural density of humanity in dense areas versus sparse ones. Downtown areas rife with loot are going to have more and stronger zeds to fight through, while empty rural fields will tend to be less challenging to navigate. Finding special loot is often less important in State of Decay 2 than finding enough of the core resources you need to survive, and by making loot-rich environments more dangerous, we can match risk to reward.”
Josiah Colborn, one of the teams world builders, says it was important to encourage players to explore their vast environments without feeling punished for doing so.
"We were inspired by other open-world games like Legend of Zelda: Breath of the Wild, where you could explore for even a short distance and find something worth your while. It might just be a nice view, or a little environmental story, or it could be something super useful."
“If you decide to go for a hike just for the fun of it, you may find yourself stumbling upon some goodies, and occasionally some rare goodies,” Colborn reports. “We were inspired by other open-world games like Legend of Zelda: Breath of the Wild, where you could explore for even a short distance and find something worth your while. It might just be a nice view, or a little environmental story, or it could be something super useful. What we hope is that players feel like you didn’t waste your time by doing that exploration.”
It was also important for game logic to match real world logic, so a player who intelligently planned a raid or supply run would be rewarded in a reasonable way that matched their planning.
“For example, town centers are clusters of establishments that are meant to service society’s general needs, and that’s knowledge players bring with them into the game. So we’ll cluster a variety of resource sites into a town center, and ‘going into town’ for a resource run is pretty reliable logic when thinking about how they’re going to survive. Clusters of sites are inherently more dangerous. For the player, logic would dictate that high-population areas pre-apocalypse would tend to be more threatening in the post-apocalypse. But under-the-hood, something the player doesn’t realize is that we only want to spawn zombies nearby when the player can’t see them pop in. When making maps, we’ve also taken advantage of this by deliberately placing large line-of-sight blockers to make sure there’s more areas zombies can pop in, and keep the excitement up in the moment-to-moment gameplay.”
Retaining the sense of unpredictability on every run, something that procedural generation in large part makes possible, means that once a player is on a run, their plans can change dramatically based on events.
“Once they get into town, players’ minute-to-minute thinking can take over for their long-term planning. They might find they came into town for meds, but perhaps there’s a hardware store nearby the clinic they just raided. They want to grab some materials or parts while they’re here, but maybe they’re also running running low of ammo for their equipped weapon, or maybe they’re already injured from that clinic raid. Maybe their trunk is too full, and the amount they’re carrying is already starting to weigh them down. It’s really up to the player, at that point, how much risk they decide to engage with. Ultimately, sticking around looting for longer may be more efficient for your long-term fuel-conservation plans, but it’s probably also a riskier prospect in the short term.”
The systems are designed to also interlock in ways that affect how and where players will choose to settle, and how sustainable those safe houses will be.
“Players who settle a homesite right in the middle of town will find that they have some nice scavenging opportunities within walking distance of home,” Colborn says. “But that may put their home closer to a broader array of potential threats, and eventually they may deplete their local scavenging opportunities. For the hermits, we have bases way off in the boonies. These bases will take some extra driving to get to and back from scavenging runs, but you also may feel more secure knowing other humans and zombie infestations are less likely to show up close to home."
"The result is that the resources you have nearby are variable to your own preferences and their resultant choices as a player," he continues.
"At some point, you may find you’ve picked a town clean, and it’s time to move to another base, or even another region. How nomadic or stationary you choose to be is part of the State of Decay 2 ‘How will you survive?’ question, and the maps and the resource systems that service them reinforce that.”