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,23 +608,42 @@ 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)
)
{ {
this.logger.debug( const possibleAmmo = this.weightedRandomHelper.getWeightedValue<string>(compatibleCartridges);
this.localisationService.getText("bot-incompatible_ammo_for_weapon_falling_back_to_default", {
chosenAmmo: chosenAmmoTpl, // Check compatibility
weaponId: weaponTemplate._id, if (weaponTemplate._props.Chambers[0]
weaponName: weaponTemplate._name, && !weaponTemplate._props.Chambers[0]._props.filters[0].Filter.includes(possibleAmmo)
defaultAmmo: weaponTemplate._props.defAmmo, )
}), {
); // Ran out of possible choices, use default ammo
if (Object.keys(compatibleCartridges).length === 0)
{
this.logger.debug(
this.localisationService.getText("bot-incompatible_ammo_for_weapon_falling_back_to_default", {
chosenAmmo: chosenAmmoTpl,
weaponId: weaponTemplate._id,
weaponName: weaponTemplate._name,
defaultAmmo: weaponTemplate._props.defAmmo,
}),
);
// 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;