aliens

Author Topic: Smart Auto-Equip Algorithm  (Read 3196 times)

Offline The Reaver of Darkness

  • Commander
  • *****
  • Posts: 1512
    • View Profile
Smart Auto-Equip Algorithm
« on: December 02, 2017, 06:06:44 am »
I came up with an auto-equip algorithm that could equip soldiers in a more intelligent fashion than the default. It'll have a tendency to equip soldiers of different strengths with different weapons, and will put the heavier weapons on the stronger soldiers. You'll get both a variety of weapons and a better fit to the soldiers' strength. Since I don't do programming, I'll post the algorithm here.



Everything defaults to alphabetical order of the STR_ tag if the algorithm doesn't have a higher priority for a different operation.
Initially, the algorithm will create weapon groupings. Two-hand ranged weapons get equipped with their ammo inside, each unit of weapon+ammo is a weapon grouping. One-hand ranged weapons attempt to group with one ammo clip inside plus an off-hand non-weapon (defined as items which aren't weapons but which have at least one of the following functions: medi-kit, motion scanner, mind probe, psi-amp) or a melee weapon. The one-hand ranged weapons are lined up from heaviest to lightest and paired up with the categories of off-hand items, first category heaviest to lightest, second category from heaviest to lightest, etc. until all one-hand ranged weapons are grouped with an off-hand item or there are no off-hand items left to group. Any remaining one-hand ranged weapons group by themselves (with just the one ammo clip inside). Off-hand groups are as follows, ordered from highest to lowest priority:
1.) Off-hand non-weapons, defined as anything that isn't a weapon (not ranged weapon, melee weapon, or grenade) but has at least one of the following functions: medi-kit, mind-probe, motion scanner, psi-amp.
2.) Grenades, defined as anything that can be thrown and then exploded (standard grenade, proximity grenade, thrown: explode on impact).
3.) Melee weapons, defined as anything which deals damage either as melee attack or with ranged attack but with only adjacent range (adjacent-only ranged weapons do not check one vs. two handed status).
4.) Misc. category, includes electro-flares as well as anything from mods that cannot be identified to match one of the other categories (including the primary weapon category described above). This does not include anything that cannot be used due to technology restrictions; those items will never be equipped by the algorithm. It does include items with no function that the algorithm can detect.
5.) Off-hand weapons, defined as any one-hand ranged weapon that isn't the same weapon as the weapon it is being grouped with.

If the ammo is 1 or 2 shots per item, the weapon will group with a second unit of ammo as long as there are any left after all weapons are equipped with ammo. The algorithm does not check if different ammo types for the same weapon have different amounts of ammo, and instead only equips them in alphabetical order of STR_ label, and only checks how much ammo is in the unit that gets equipped.

.

Once all one-hand ranged weapons are paired into weapon groups, all weapon groups are lined up from heaviest to lightest. The soldiers are all lined up from strongest to weakest, with any tie being resolved by choosing from soldier list order. The algorithm begins round zero of equipping soldiers. Each soldier is given an ideal weight for their weapon group, which is 2/3rds of their strength, rounded down. The algorithm attempts to pair the first (strongest) soldier with a weapon group closest to his/her ideal weight, considering 2 points under to be equivalent to 1 point over but preferring underweight over overweight. The algorithm will completely disallow equipping a group with a weight that goes over the soldier's strength. If there are no weapon groups that can be carried by the soldier, all one-hand ranged weapons will attempt to be paired without an off-hand, and if this check fails to equip the soldier with a primary weapon, it will go through all melee weapons, then all grenades. If nothing can be equipped to the soldier without going over their strength, it will give up and refuse to equip this soldier with a primary weapon.

Once the soldier is paired up with a primary weapon (or the check fails), that weapon group is removed from the list and the algorithm moves to the next soldier. Once all soldiers have been passed over for a primary weapon (equipped or not), the algorithm will move onto round one:

Each round from here on out will attempt to equip soldiers from strongest to weakest with one item from one of the following categories, prioritizing category order first and alphabetical STR_ label order second. All weapons that use ammo count the weapon plus one unit of ammo (in the weapon) as one item. They will not count an extra unit of ammo for 1 or 2 shot ammo; that is only done for primary weapons.
1.) backup ammo, one unit of ammo matching the weapon, preferring a type that isn't already in the soldier's inventory
2.) off-hand non-weapon
3.) grenade
4.) melee weapon
5.) misc. category (electro-flare)
6.) off-hand ranged weapon

The algorithm will start at the strongest soldier and attempt to put on an item from category 1. If there are no category 1 items left or none fit within the remaining strength (or if the primary weapon does not use ammo), it will attempt category 2, and so on until a single item is equipped or the algorithm finishes going through all categories. Then it will go to the next strongest soldier, and so on. Once all soldiers have been passed through round one, it will begin round two. This round will do the same, attempting to put on one item from each category in order until one item is equipped.

Once an item from category 1, 4, or 6 is equipped, those will be removed from the list for that soldier. The one exception is for single shot ammo, in which it can equip up to three times from category 1 (five total units of ammunition). For other categories, it will first attempt to equip the soldier with an item from categories for which the number the soldier has been equipped with so far is fewest. For example, if the soldier has already been equipped with a medi-kit but not anything else, it will attempt category 3 first and put category 2 at the end. Also, the algorithm will not equip a soldier with more than 2 of the same grenade, or more than one of the same item of any other kind.

Once round 2 finishes, the algorithm begins round 3, and keeps doing more rounds until a round does not equip a single item to any soldier. Once that happens, the algorithm finishes. It is worth noting that if you include a wide variety of small items with zero weight, or have many small items and very strong soldiers, this algorithm may fill their inventory with a wide variety of stuff, leaving few or no spaces empty.

.



I ran the algorithm through a group of 8 soldiers (by hand) with an assortment of conventional weapons and early tech (including a bit from my mod) to see what they would wind up with. Here are the results, showing soldiers in deployment list order:

Benjamin Kammerer (40 strength, 39 weight)
Primary weapon: autocannon with AP ammo
1.) AC-HE ammo
2.) proximity grenade
3.) motion scanner

Oluwa Ogechi (18 strength, 18 weight)
Primary weapon: laser pistol and stun rod
1.) grenade
2.) motion Scanner

Slavomir Kolesar (35 strength, 33 weight)
Primary weapon: heavy auto-laser
1.) first-aid kit
2.) grenade

Ngozi Anakobe (42 strength, 42 weight)
Primary weapon: rocket launcher with incendiary rocket, backup explosive rocket (STR_LARGE_ROCKET)
1.) smoke rocket
2.) high explosive

Yordan Dragomirov (43 strength, 42 weight)
Primary weapon: rocket launcher with incendiary rocket, backup explosive rocket
1.) smoke rocket
2.) grenade
3.) medi-kit

Ciara O'Brien (21 strength, 21 weight)
Primary weapon: pistol with clip and stun rod
1.) pistol clip
2.) proximity grenade
3.) electro-flare

Marcin Jankowski (30 strength, 29 weight)
Primary weapon: heavy cannon with AP ammo
1.) HC-HE ammo
2.) proximity grenade

Dulcine Busasa (31 strength, 29 weight)
Primary weapon: heavy laser
1.) first-aid kit
2.) grenade


Unequipped items:
Pistol x1 (with clip)
Pistol clip x1
Auto-pistol x2 (with clip)
Auto-pistol clip x2
Rifle clip x2
Rifle x2 (with clip)
Machinegun x2 (with clip)
Machinegun clip x2
Heavy cannon x1 (AP ammo)
HC-HE ammo x3
HC-I ammo x2
Autocannon x1 (AP ammo)
AC-AP ammo x2
AP-HE ammo x3
AC-I ammo x2
Explosive rocket x2
Laser auto-pistol x1
Laser rifle x1
Laser pulse rifle x1
High explosive x3
Proximity grenade x1
Smoke grenade x4
Medi-kit x1
Psi-amp x2
Electro-flare x3



Some final notes:
The equipment loadout isn't pretty, but it's more utilitarian. If you don't know what you're up against, it's not a bad loadout. It maintains a lot of flexibility. You may see some flaws in the loadout, but that's more due to alphabetical order or the equipment selection of the commander. If we assume all of the weapons and other devices are reasonably balanced (and more importantly that the commander only included items which (s)he wishes to use on the mission), then there is no reason to expect the soldier with a pistol and stun rod to be at a disadvantage to someone with only a rifle. They might be at a disadvantage, if they are equipped with a melee weapon but have low time units, or are equipped with a low-accuracy weapon and themselves have low accuracy, but such things are up to the commander to sort out and should not be done by an auto-equip algorithm. You may notice above that the rocket launchers are equipped with incendiary rockets first. This is due to choosing equipment in alphabetical order and this particular case happened in the original game. I don't know if it was done on purpose, but most of the ammo and grenade types are well sorted by alphabetical order. Incendiary rockets are an outlier. AP ammo on heavy/auto cannons being unpopular is due to underpowered AP ammo, and not any problem with that ammo type being first in the list. I'm considering changing my Explosive Rocket from STR_LARGE_ROCKET to STR_EXPLOSIVE_ROCKET to put it ahead of STR_INCENDIARY_ROCKET, but that would cause explosive rockets in older saves to become a defunct item, and I'd have to change it to still be usable without appearing in the purchase list or UFOPedia.

Anyway, what are your thoughts on my algorithm? I'm happy to hear from you and you're welcome to suggest changes though I have no intention of editing the OP to reflect them. I think it would be nice if this or something like it were made into a mod for OpenXcom, and even nicer if it were included as a UFOExtender option in the base game.

Offline Warboy1982

  • Administrator
  • Commander
  • *****
  • Posts: 2333
  • Developer
    • View Profile
Re: Smart Auto-Equip Algorithm
« Reply #1 on: December 02, 2017, 10:31:02 am »
take it from me, one man's smart algorithm is another man's annoying insanity.
not to discredit the idea at all, just that this is one of those areas where everyone has their own idea about the best way to do it and no two people agree.

Offline The Reaver of Darkness

  • Commander
  • *****
  • Posts: 1512
    • View Profile
Re: Smart Auto-Equip Algorithm
« Reply #2 on: December 02, 2017, 02:08:18 pm »
Sure, that's why I want it to be an option. If there were more than one auto-equip algorithm to choose from, people could try them out and choose the one they like best, if it's important to them.
« Last Edit: December 02, 2017, 02:21:47 pm by The Reaver of Darkness »

Offline Meridian

  • Global Moderator
  • Commander
  • *****
  • Posts: 9094
    • View Profile
Re: Smart Auto-Equip Algorithm
« Reply #3 on: December 21, 2017, 11:57:44 am »
Todolisted with low prio.