Home » MODDING HQ 1.13 » v1.13 Modding, Customising, Editing » v1.13 Time Capsule (How-to Library) » "How does it work?" Part 11: Enemy Gun Selection
"How does it work?" Part 11: Enemy Gun Selection[message #244880] Sun, 21 February 2010 05:55 Go to next message
Headrock

 
Messages:1795
Registered:March 2006
Location: Jerusalem
"How Does It Work?" series: [/b]</font>Enemy Gun Choices

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?"

Speculations were rife, and both times I had to step in to clear things up. I do this by looking at the game's source code and trying to figure out, well, "how does it work".

Since I'm really not looking to do the research again a third time, I figured I'd just write it all down, once and for all, so that I could point people to it.

Disclaimer: The game is written in C++, and while fairly simple to read, it may confuse many players. Therefore, "How Does It Work" guides are written in Pseudo-Code, which is basically a translation into plain English.
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.
In the future, I may or may not expand this article with those missing details. No promises.

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 Progress 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".


Contrary to... common belief... 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.

Formula Outline

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.

<font size="]Formula


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

If the Current Sector is in Alma, then
   Distance = 10
If the Current Sector is in Orta, then
   Distance = 4
Else, 
   Distance = The distance between the Current Sector and the Queen's Palace (sector P3)
   Distance is capped at 20, although it can't get over 21.213 anyway.


Distance is calculated using the Pythagorean Theorem. If you've studied any maths in school, you should know what this means.
It 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.

Distance Modifier = (( 20 - Distance ) * 30) / 20


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.

Temporary Value = (Distance Modifier / 3) - 5


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.

RandomNum = Roll a random number between 0 and 9.
Temporary Value is increased by RandomNum.
If Temporary Value is smaller than 0, then
   Temporary Value = 0.
If Temporary Value is greater than 9, then
   Temporary Value = 9.


We applied a random modifier, and then limited the result to between 0 and 9.

If we're in Autoresolve mode, then
   Temporary Value is decreased by 1.


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.

If Temporary Value = 0, then
   Equipment Modifier = 0.
If Temporary Value = 1 or 2, then
   Equipment Modifier = +1.
If Temporary Value = 3, 4, 5 or 6, then
   Equipment Modifier = +2.
If Temporary Value = 7 or 8, then
   Equipment Modifier = +3.
If Temporary Value = 9, then
   Equipment Modifier = +4.


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.

If Game Difficulty is NOVICE, then
   Difficulty Modifier = 0.
If Game Difficulty is EXPERIENCED, then
   Difficulty Modifier = 10.
If Game Difficulty is EXPERT, then
   Difficulty Modifier = 20.
If Game Difficulty is INSANE, then
   Difficulty Modifier = 30.


Very simple, this is our starting point for the Difficulty Modifier calculation.

If our Highest Progress Level is more than 0, then
   Difficulty Modifier is increased by 5.


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.

If Daily Mine Income = 0, then
   Difficulty Modifier is decreased by 5.


The game makes life a little easier on us if we don't control any mines.

Difficulty Modifier is increased by Distance Modifier.


Remember the Distance Modifier from Step 1? We now add it to the Difficulty Modifier.

Make sure that the Difficulty Modifier is no less than 0 and no more than 100.


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.

If SLOW_PROGRESS_FOR_ENEMY_ITEMS_CHOICE is set to FALSE, then
   Basic Weapon Level = (Equipment Modifier - 5) + (Difficulty Modifier / 10).
If SLOW_PROGRESS_FOR_ENEMY_ITEMS_CHOICE is set to TRUE, then
   Basic Weapon Level = Difficulty Modifier / 10.


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).

If SLOW_PROGRESS_FOR_ENEMY_ITEMS_CHOICE = FALSE, then
   If Soldier's Class = ADMIN, then
      Basic Weapon Level is increased by 1.
   If Soldier's Class = TROOP, then
      Basic Weapon Level is increased by 3.
   If Soldier's Class = ELITE, then
      Basic Weapon Level is increased by 5.


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.

If Soldier's Class = ADMIN, then
   RandomNum = Roll a random number between 0 and 4.
   If RandomNum = 0, then
      Random Modifier = +1.
   If RandomNum = 1, then
      Random Modifier = -1.
If Soldier's Class = TROOP, then
   RandomNum = Roll a random number between 0 and 6.
   If RandomNum = 3, then
      Random Modifier = +1.
   If RandomNum = 4, then
      Random Modifier = -1.
If Soldier's Class = ELITE, then
   RandomNum = Roll a random number between 0 and 10 (or 0 and 11, if using NIV)
   If RandomNum = 4 or 10, then
      Random Modifier = +1.
   If RandomNum = 5, then
      Random Modifier = -1.


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.

Final Weapon Level = Basic Weapon Level + Random Modifier.


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.

And that's all there is to it. Hope the modifiers haven't blown your head off, and see you next time in "How does it work"!

[Updated on: Mon, 27 April 2015 23:46] by Moderator



Re: "How does it work?" Part 11: Enemy Gun Selection[message #244907] Sun, 21 February 2010 17:26 Go to previous messageGo to next message
Tao

 
Messages:2245
Registered:August 2009
Location: The Known Universe
Headrock....Just for the record......You're still my hero!
Re: "How does it work?" Part 11: Enemy Gun Selection[message #246222] Fri, 05 March 2010 12:58 Go to previous messageGo to next message
aelfewulf

 
Messages:21
Registered:December 2007
Location: Singapore
I've been puzzled and frustrated by this for years...and editing the enemygunchoices xml in vain.
MANY THANKS!
Re: "How does it work?" Part 11: Enemy Gun Selection[message #252740] Tue, 01 June 2010 12:36 Go to previous messageGo to next message
dreamer2008

 
Messages:59
Registered:May 2010
Thank you Headrock for this "How to" series! It's great! Now I know how to create my perfect IMP, how to make really efficient battle tactics, more things to be careful about when hiring mercs, and other useful details that I never imagined that they were used by the game.
Re: "How does it work?" Part 11: Enemy Gun Selection[message #252741] Tue, 01 June 2010 13:02 Go to previous messageGo to next message
Will Gates

 
Messages:1029
Registered:September 2006
Location: Far far away.
The deeper you look you realise the deeper it is. Sirtech were on the edge of sheer GENIUS and the main reason we are all still playing over 10 years later modded or not. You will never get such depth from modern developers... it's not in their financial interest to create it.

Re: "How does it work?" Part 11: Enemy Gun Selection[message #252761] Tue, 01 June 2010 23:19 Go to previous messageGo to next message
P.Val

 
Messages:72
Registered:May 2010
Will Gates
... it's not in their financial interest to create it.

Of course... a million gamers playing a game for a week and afterwards completely forgetting it brings in more money than a few thousands of dedicated fans who will play it even 20 years later. Sad
Re: "How does it work?" Part 11: Enemy Gun Selection[message #253380] Wed, 09 June 2010 05:02 Go to previous messageGo to next message
LootFragg

 
Messages:344
Registered:August 2009
Location: Berlin, Germany
It's our own damn fault indeed. We should have bought this game more often. Like every 3 months or so, just to make sure the developers get more time to work on the sequel. Now Headrock does. G'job, mate. =D

I love the "How does it work?" series. Teach us fools. *worship*
Re: "How does it work?" Part 11: Enemy Gun Selection[message #253507] Thu, 10 June 2010 12:27 Go to previous messageGo to next message
usrbid

 
Messages:1580
Registered:December 2008
How about a subscription fee for a non online game? Very Happy

I don't even know if this is legally possible. However this way you get $5 - $10 every month you want to play the game. This way you would get both (a million for a week and a thousand for 20 years).


Re: "How does it work?" Part 11: Enemy Gun Selection[message #253766] Sat, 12 June 2010 14:24 Go to previous messageGo to next message
BallerDave

 
Messages:74
Registered:April 2008
Location: Ze land of ze sheep, Dubl...

Dieter
How about a subscription fee for a non online game? Very Happy

I don't even know if this is legally possible. However this way you get $5 - $10 every month you want to play the game. This way you would get both (a million for a week and a thousand for 20 years).


Why dont modders and other major contributors send us their PayPal account names so we can buy them a drink or two Wink

Id certainly give you guys a tenner
Re: "How does it work?" Part 11: Enemy Gun Selection[message #253771] Sat, 12 June 2010 14:57 Go to previous messageGo to next message
Headrock

 
Messages:1795
Registered:March 2006
Location: Jerusalem
Please bring $10,000 in small unmarked bills to the corner of 21st and Broadway. Come alone.


Re: "How does it work?" Part 11: Enemy Gun Selection[message #253773] Sat, 12 June 2010 15:09 Go to previous messageGo to next message
BallerDave

 
Messages:74
Registered:April 2008
Location: Ze land of ze sheep, Dubl...

Feck off. I will hire Flo to make the transfer. Very Happy
But if you screw with me I will send Reaper after you.....!

No I'm serious. Any modders who have paypal:

Soon as I get my next paycheck I'll give yous a few Euro. I think others who can afford it and keep mouthing without actually contributing to the game whatsoverer (like me) should do the same.
Re: "How does it work?" Part 11: Enemy Gun Selection[message #253774] Sat, 12 June 2010 15:14 Go to previous messageGo to next message
usrbid

 
Messages:1580
Registered:December 2008
Hi dave17, That is certainly an interesting idea, I will check with Shanga if this is legally an option. If so, we can make a board list and people could decide who they want to support. Might be a cool thing to do.


Re: "How does it work?" Part 11: Enemy Gun Selection[message #253775] Sat, 12 June 2010 15:17 Go to previous messageGo to next message
Headrock

 
Messages:1795
Registered:March 2006
Location: Jerusalem
I don't think any of us can accept money for this work. Aside from the legal implications, it just wouldn't feel right. (I bet some modders would want to kill me now Very Happy )


Re: "How does it work?" Part 11: Enemy Gun Selection[message #253782] Sat, 12 June 2010 15:48 Go to previous messageGo to next message
BallerDave

 
Messages:74
Registered:April 2008
Location: Ze land of ze sheep, Dubl...

Headrock
I don't think any of us can accept money for this work. Aside from the legal implications, it just wouldn't feel right. (I bet some modders would want to kill me now Very Happy )


No legal implications at all. I'm not gonna pay you for the work you're doing. Just buying you a drink as a "thanks" Wink

[Updated on: Sat, 12 June 2010 15:49] by Moderator

Re: "How does it work?" Part 11: Enemy Gun Selection[message #253784] Sat, 12 June 2010 16:23 Go to previous messageGo to next message
Headrock

 
Messages:1795
Registered:March 2006
Location: Jerusalem
Is it cognac?


Re: "How does it work?" Part 11: Enemy Gun Selection[message #253786] Sat, 12 June 2010 16:27 Go to previous messageGo to next message
Logisteric

 
Messages:3500
Registered:December 2008
Location: B
dublin, headrock - it'll be john barleycorn
Re: "How does it work?" Part 11: Enemy Gun Selection[message #253790] Sat, 12 June 2010 16:51 Go to previous messageGo to next message
BallerDave

 
Messages:74
Registered:April 2008
Location: Ze land of ze sheep, Dubl...

Well actually I have a passport that says German but my family are either Polish or Italian and I happen to study computersciences in Dublin sooo.... I can supply you with anything from Vodka and Whiskey to Beer and Wine.

[Updated on: Sat, 12 June 2010 16:56] by Moderator

Re: "How does it work?" Part 11: Enemy Gun Selection[message #253791] Sat, 12 June 2010 17:05 Go to previous messageGo to next message
Logisteric

 
Messages:3500
Registered:December 2008
Location: B
comme on - that's simply not fair Wink send him a can of sauerkraut
Re: "How does it work?" Part 11: Enemy Gun Selection[message #254039] Wed, 16 June 2010 07:14 Go to previous messageGo to next message
usrbid

 
Messages:1580
Registered:December 2008
Hi dave17, I am still working on the donations question, it might take a little time as we are looking at legal questions around this.


Re: "How does it work?" Part 11: Enemy Gun Selection[message #254260] Fri, 18 June 2010 18:43 Go to previous messageGo to next message
BallerDave

 
Messages:74
Registered:April 2008
Location: Ze land of ze sheep, Dubl...

Thanks Dieter

this should provide a way for users who do not contribute to the game directly to at least try and sponsor all of you good people who keep the game alive and in a state of constant evolution.

[Updated on: Fri, 18 June 2010 18:51] by Moderator

Re: "How does it work?" Part 11: Enemy Gun Selection[message #254261] Fri, 18 June 2010 19:09 Go to previous messageGo to next message
Headrock

 
Messages:1795
Registered:March 2006
Location: Jerusalem
. o O ( I wonder how long I could just live off donations before I actually have to add more features to get more money. :vader: )


Re: "How does it work?" Part 11: Enemy Gun Selection[message #254262] Fri, 18 June 2010 19:15 Go to previous messageGo to next message
Khor1255

 
Messages:1834
Registered:August 2003
Location: Pleasantville, NJ
Around here you wouldn't make it passed lunch.
Re: "How does it work?" Part 11: Enemy Gun Selection[message #269285] Mon, 03 January 2011 16:14 Go to previous messageGo to next message
Buns

 
Messages:677
Registered:September 2010
Headrock

[*]The highest Progress you've achieved so far in the game (even if it has dropped since).
(...)
If our Highest Progress Level is more than 0, then
   Difficulty Modifier is increased by 5.


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.

Necroign here because of my own mod.

Do I get this right? This is the only occasion when progress level is checked for in this formular; means progress doesn't matter as long as it is >0, regardless whether it is 1 or 100? This would mean that a troop of enemies always has (about) the same (level of) weapons in a given sector, indepdently of whether you enter this sector in the early or late game. The only factor that would count in the end would be distance to P3; setting aside enemy class when slow_progression is set to "false".

Re: "How does it work?" Part 11: Enemy Gun Selection[message #269510] Fri, 07 January 2011 00:34 Go to previous messageGo to next message
Headrock

 
Messages:1795
Registered:March 2006
Location: Jerusalem
Quote:
Do I get this right? This is the only occasion when progress level is checked for in this formular; means progress doesn't matter as long as it is >0, regardless whether it is 1 or 100? This would mean that a troop of enemies always has (about) the same (level of) weapons in a given sector, indepdently of whether you enter this sector in the early or late game. The only factor that would count in the end would be distance to P3; setting aside enemy class when slow_progression is set to "false".


Unless I've missed something in the code, which is always possible (though unlikely), your statement is correct.

Or should I say, was correct, because then SANDRO went and added a system to take progress into account. I think it's toggled by INI, and only available in the MP branch (until the new 1.13 beta is released...)



Re: "How does it work?" Part 11: Enemy Gun Selection[message #269530] Fri, 07 January 2011 12:34 Go to previous message
Buns

 
Messages:677
Registered:September 2010
Ah yes, thank you. I had been setting up a mod that makes weapon progress by technology levels and was noticing that it didn't work as it should. Such as WWII still being dropped at progress level 70 (what should give 1970s weaponary) in case the battle took place somewhere north of Cambria. Now I know the reason: progress doesn't play a role.

OK, I'll wait for the new version and meanwhile make a different approach to Arulcan Army outfit.
Previous Topic: editing IMP's in 1.13
Next Topic: "How does it work?" Part 2: Character Skills
Goto Forum:
  


Current Time: Sun Mar 26 17:53:18 EEST 2017

Total time taken to generate the page: 0.00799 seconds