Enemy Gun Selection
Headrock's "How does it work?" Part 11: Enemy Gun Selection explains how enemy soldiers are assigned their guns by the game..
There have been no less than two occasions in the past year where this question has come up: "How do enemies pick which gun to carry?" This chapter does not explore the entire item generation system. It is focused primarily on the enemy decisions about which GUNS to carry. However, the majority of the item choice system works the same way.
Also note that the same formula is used for militia equipment. However, it works slightly differently for them, they generally get worse equipment than Soldiers of similar level.
Basic process overview
When do enemies get randomized equipment?
Enemies come in two major flavours - Pre-generated and Randomly-Generated.
Pre-Generated enemies are the ones that are explicitly placed on maps with the JA2 Map Editor. Modders normally give these enemies a specific gun, but sometimes allow the game to make its own, random choice about which guns to give them. Randomly-Generated enemies generally come from enemy units that move around the Strategy Map. Sometimes these may wander into city sectors and become stationary. Regardless of whether they are moving on the map or not, they come with no pre-made data, and so whatever they are carrying had to has to be randomly generated.
Note that in both cases, if randomization is involved, it happens when the sector is LOADED. If an enemy manages to survive a battle (esp. by running away), the next time you meet the SAME enemy, he will have new randomized equipment. For instance, an enemy that drops its weapon and runs will have a new weapon when you meet him again. He'll have a new weapon even if he didn't drop his last one.
Only pre-made, hand-placed enemies will carry the same weapon every time. Examples include a sniper in Alma carrying a Dragunov. He'll carry that same weapon every game, because he was hand-placed by the SirTech level designers when they made JA2.
What influences weapon choice?
There are several misconceptions about this, which this article is supposed to clear up. Let's see a list of all factors that influence weapon choice:
- The soldier's class (ADMIN / TROOP / ELITE).
- The Difficulty Level of the game you're playing.
- The highest [i]Progress[/i] you've achieved so far in the game (even if it has dropped since).
- The customizable file (DATAFolder)\TableData\EnemyGunChoices.XML.
- Distance from the queen's palace (Sector P3).
- Several randomized variables.
- The JA2_Options.INI setting called "SLOW_PROGRESS_FOR_ENEMY_ITEMS_CHOICE".
The weapons your men are carrying have absolutely no effect on this formula. Only the above-listed values have any effect. In addition, a gun's COOLNESS has no bearing on when it will appear in game. What DOES matter is where this gun is listed in EnemyGunChoices.XML, as you will see shortly.
Let's get a quick overview of how the formula goes. This section explains it briefly. The next section will explain it with greater detail (including pseudo-code).
- Step 1. Calculate the distance from the palace. This is called a "Distance Modifier".
- Step 2. Use the Distance Modifier to calculate a simple "Equipment Modifier".
- Step 3. Calculate the difficulty of the game compared to your highest progress. This is called a "Difficulty Modifier"
- Step 4. Compare the Difficulty Modifier and Equipment Modifier, coming up with a "Basic Weapon Level".
- Step 5. Add a modifier based on Soldier's Class to the Basic Weapon Level.
- Step 6. A random chance to add or subtract a point from the Basic Weapon Level, giving us a Final Weapon Level.
- Step 7. Choose a random weapon from EnemyGunChoices.XML, from a category corresponding to the Final Weapon Level.
Confusing? It is. Let's get down and dirty.
We'll now go step by step, following the exact formula used by the game. Try to stay familiar with the different modifiers, because they can get pretty confusing. Ready? Go.
STEP 1: CALCULATE DISTANCE MODIFIER
Distance is calculated using the Pythagorean Theorem and is rounded down.
Note that the Current Sector is the sector where the soldier is at the time you meet him. If you meet the same soldier in different sectors, the changed distance from the palace may have a serious influence on his weapon choice, assuming he's moved far enough between encounters.
Also note that in Alma and Orta, the distance is artificially reduced to make these randomly-generated enemies more dangerous than they would normally be.
This produces a range of 0 to 30. The result is the Distance Modifier which will be used twice, later in this formula. Remember it!
STEP 2: CALCULATE EQUIPMENT MODIFIER
We're now going to use the Distance Modifier to get an Equipment Modifier.
Given that the Distance Modifier's range is between 0 and 30 (see previous step), we now have a value of between -5 and 5.
We applied a random modifier, and then limited the result to between 0 and 9.
For Autoresolve battles, enemies are actually given worse weapons and equipment. Militia, on the other hand, get a +1 bonus. This is supposed to make battles between them more fair, since Militia get crappier weapons on the whole. But who cares about Autoresolve anyway.
Now, let's find out the Equipment Modifier based on the Temporary Value we have.
Ah, the Equipment Modifier is what we were aiming for, really.
We now dump the Temporary Value, it's of no use to us any longer.
We keep the Equipment Modifier for later use though! It is very important!
STEP 3: CALCULATE DIFFICULTY MODIFIER
Now let's find out what the difficulty modifier is. It's based on the game's difficulty level, but also on our highest Progress level.
Very simple, this is our starting point for the Difficulty Modifier calculation.
Note that the Highest Progress Level is the highest we've achieved in this campaign. It's possible for progress to go down if we mess up, but this formula only cares about our "best" progress so far, not about the "current" progress.
The game makes life a little easier on us if we don't control any mines.
Remember the Distance Modifier from Step 1? We now add it to the Difficulty Modifier.
Simple enough, capping the Difficulty Modifier to correspond to a range of 0 to 100.
And we're through with this. We have to remember the Difficulty Modifier of course, because we're going to use it in the next step.
STEP 4: CALCULATE BASIC WEAPON LEVEL
In this step, we calculate the important modifiers we got from the previous steps together.
Depending on the SLOW_PROGRESS_FOR_ENEMY_ITEMS_CHOICE setting from JA2_Options.INI, we may get completely different results here.
As you can see, SLOW_PROGRESS_FOR_ENEMY_ITEMS_CHOICE=TRUE ignores the Equipment Modifier completely.
At this point, SLOW_PROGRESS_FOR_ENEMY_ITEMS_CHOICE=TRUE actually produces a HIGHER Basic Weapon Level, but as you'll soon see, the end result will be quite different.
STEP 5: ADD SOLDIER CLASS MODIFIER
We now add yet ANOTHER modifier, which is based on the soldier's "rank" - ADMIN (YellowShirt), TROOP (RedShirt) or ELITE (BlackShirt).
Obviously, soldier class is a MAJOR modifier, and Blackshirts get a massive advantage here. Their weapon level is boosted up by a whopping 5 points.
Note, of course, that if SLOW_PROGRESS_FOR_ENEMY_ITEMS_CHOICE is set to TRUE, this modifier is completely ignored!
STEP 6: ADD RANDOM MODIFIER
Now, depending on the soldier's class, we have some chance to get either a +1 or -1 to our weapon level. Most often, however, we get nothing.
This roll is actually much more important than what's shown above, because various roll results can alter the level of all equipment this soldier might be carrying, including armor and grenades etc. However, we are only interested in guns right now.
Aha! This is the Final Weapon Level!
At this point, the Final Weapon Level is anywhere between 0 and 10. Excellent, this represents the number of categories in EnemyGunChoices.XML.
Note that I'm skipping a serious part about soldiers randomly rolling up RPGs and other launcher-type equipment. That's a bit beyond the scope of this article. I might add that bit in, sometime in the future. Or not.
STEP 7: CHOOSE A RANDOM GUN
Now that we have a Final Weapon Level, we go and have a look at EnemyGunChoices.XML.
The file is divided into 11 sections, numbered 0 to 10. Each section holds a list of index number, each representing a single gun from Items.XML.
All we have to do now is to find the section corresponding to our Final Weapon Level, and randomly choose one of the guns listed.
Again, the gun's Coolness Level is completely irrelevant. If a gun with coolness 10 is listed in section 0, it will probably be available at the start of the game from ADMIN-level enemies, on NOVICE difficulty.