From aed65ce1cb1574599fdf5060be48600e330a41be Mon Sep 17 00:00:00 2001 From: Dev Date: Sat, 18 Nov 2023 18:46:03 +0000 Subject: [PATCH] Rework `getWeightedCompatibleAmmo()` to loop over cartridges until it finds one compatible instead of giving up after the first failure --- project/src/generators/BotWeaponGenerator.ts | 51 ++++++++++++++------ 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/project/src/generators/BotWeaponGenerator.ts b/project/src/generators/BotWeaponGenerator.ts index bda5b435..04a8b4bf 100644 --- a/project/src/generators/BotWeaponGenerator.ts +++ b/project/src/generators/BotWeaponGenerator.ts @@ -593,7 +593,7 @@ export class BotWeaponGenerator { const desiredCaliber = this.getWeaponCaliber(weaponTemplate); - const compatibleCartridges = ammo[desiredCaliber]; + const compatibleCartridges = this.jsonUtil.clone(ammo[desiredCaliber]); if (!compatibleCartridges || compatibleCartridges?.length === 0) { this.logger.debug( @@ -608,23 +608,42 @@ export class BotWeaponGenerator return weaponTemplate._props.defAmmo; } - const chosenAmmoTpl = this.weightedRandomHelper.getWeightedValue(compatibleCartridges); - if ( - weaponTemplate._props.Chambers[0] - && !weaponTemplate._props.Chambers[0]._props.filters[0].Filter.includes(chosenAmmoTpl) - ) + let chosenAmmoTpl: string; + while (!chosenAmmoTpl) { - 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, - }), - ); + const possibleAmmo = this.weightedRandomHelper.getWeightedValue(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.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) - return weaponTemplate._props.defAmmo; + // Set ammo to default and exit + 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;