How to Randomly Generate Loot: Part 1
Part 1: What Makes a Gun?
Ever wonder how Borderlands randomly generates its guns?
You might think it’s just a matter of rolling the dice for each of a gun’s many attributes. Damage? Roll the dice, 10-300. Clip size? Roll the dice again, 16-92. But it’s not quite that simple; such an approach doesn’t say anything about the gun’s rareness, or suitableness for the player’s current level. Without algorithms that establish relationships between things like fire rate, damage, and clip size, there’s nothing to stop the game from eventually producing a single stupid-powerful gun with all its attributes pinned as high as the dice will allow. That would be a fun gun for about 10 minutes, but then it would just be overcompensating.
This is the first in a series of posts where I’ll walk through one imagining of how such a system might work. Because I think guns are more fun than swords, I’m going to use the Borderlands weapon system as a reference. But the same technique could be applied to swords so long as they shoot bullets. Normal swords won’t work.
I want to make it clear that I don’t work for Gearbox Software and don’t claim to know the “secret sauce” behind their gun tech. I’m just a programmer that’s fascinated by procedurally-generated content and can offer a simple blueprint to others who might want to build their own.
What Makes a Gun?
Let’s start by identifying a gun’s intrinsic characteristics. These are the properties we’ll eventually have to randomize. At their simplest, guns might be defined by:
- Damage
- Fire rate
- Clip size
- Reload speed
But if you’re building a big game in which the players will spend 99% of their time playing with guns, there are other attributes that those gun aficionados are likely to be interested in in the heat of combat:
- Accuracy: the likelihood that a bullet will hit the exact pixel under reticle.
- Recoil: related to accuracy; this is the degree to which pulling the trigger will throw off your aim.
- Weight: this could affect the player’s movement speed or factor into inventory management.
- Elemental damage: e.g. fire, lightning, etc.
- Full-auto or semi-auto?
There’s, more, though. Pay close attention to games like Borderlands and you’ll realize that there are other things the game engine must conjure whenever it’s asked to produce a gun:
- Class of gun: is it a rifle? a rocket launcher? a rare alien weapon?
- Class of projectile: does that shotgun fire buckshot or rockets? (Bravo by the way, Gearbox. What were you smoking?)
- Player class restrictions: Borderlands only applies these to mods, but other games are big on restricting weapons to specific classes.
- Multipliers: most frequently added to damage, they can be added to other things, too, like the number of bullets fired per shot (still a damage multiplier, but cleverly realized in Borderlands)
- Worth: how much can you sell it for? ”Bah!” I hear you say. Who cares? Not important, right? After all, most players find an RPG’s currency unimportant after a certain level. They’ll sell it just to get it out of their inventory. But not so fast! As it turns out, in the approach I’ll show you, it actually provides a neat jumping-off point for a mental model about randomization.
- Rarity: very important! This can have a fundamental impact on how the randomizer calculates all other attributes. Interestingly, rarity can be an input to the overall randomizer or an output of it. That is, you can either pick a certain rarity first and randomize the gun accordingly, or you can randomize all the attributes first and then label the gun with a rarity according to the probability that such a randomization could occur. I’ll be taking the former approach; it’s way easier.
Fortunately, regardless of how many characteristics you can think of that make up a gun, having more of them doesn’t make it harder to randomize them. So go nuts.
In the next post, I’ll talk about pie. Procedurally-generated loot is always better with pie.