Rework getWeightedCompatibleAmmo() to loop over cartridges until it finds one compatible instead of giving up after the first failure

This commit is contained in:
Dev 2023-11-18 18:46:03 +00:00
parent eb77589606
commit aed65ce1cb

View File

@ -593,7 +593,7 @@ export class BotWeaponGenerator
{ {
const desiredCaliber = this.getWeaponCaliber(weaponTemplate); const desiredCaliber = this.getWeaponCaliber(weaponTemplate);
const compatibleCartridges = ammo[desiredCaliber]; const compatibleCartridges = this.jsonUtil.clone(ammo[desiredCaliber]);
if (!compatibleCartridges || compatibleCartridges?.length === 0) if (!compatibleCartridges || compatibleCartridges?.length === 0)
{ {
this.logger.debug( this.logger.debug(
@ -608,11 +608,18 @@ export class BotWeaponGenerator
return weaponTemplate._props.defAmmo; return weaponTemplate._props.defAmmo;
} }
const chosenAmmoTpl = this.weightedRandomHelper.getWeightedValue<string>(compatibleCartridges); let chosenAmmoTpl: string;
if ( while (!chosenAmmoTpl)
weaponTemplate._props.Chambers[0] {
&& !weaponTemplate._props.Chambers[0]._props.filters[0].Filter.includes(chosenAmmoTpl) const possibleAmmo = this.weightedRandomHelper.getWeightedValue<string>(compatibleCartridges);
// Check compatibility
if (weaponTemplate._props.Chambers[0]
&& !weaponTemplate._props.Chambers[0]._props.filters[0].Filter.includes(possibleAmmo)
) )
{
// Ran out of possible choices, use default ammo
if (Object.keys(compatibleCartridges).length === 0)
{ {
this.logger.debug( this.logger.debug(
this.localisationService.getText("bot-incompatible_ammo_for_weapon_falling_back_to_default", { this.localisationService.getText("bot-incompatible_ammo_for_weapon_falling_back_to_default", {
@ -623,8 +630,20 @@ export class BotWeaponGenerator
}), }),
); );
// Incompatible ammo found, return default (can happen with .366 and 7.62x39 weapons) // Set ammo to default and exit
return weaponTemplate._props.defAmmo; chosenAmmoTpl = weaponTemplate._props.defAmmo;
break;
}
// Not compatible, remove item from possible list and try again
delete compatibleCartridges[possibleAmmo];
}
else
{
// Compatible ammo found
chosenAmmoTpl = possibleAmmo;
break;
}
} }
return chosenAmmoTpl; return chosenAmmoTpl;