Prevented drawAmmoTpl() failures from killing loot generation when cartridge data cannot be found inside staticAmmoDist

This commit is contained in:
Dev 2024-04-30 23:23:00 +01:00
parent 06b35ddde8
commit bddf87a4e2
2 changed files with 25 additions and 6 deletions

View File

@ -1061,6 +1061,7 @@ export class LocationGenerator
const weaponTemplate = this.itemHelper.getItem(chosenTpl)[1]; const weaponTemplate = this.itemHelper.getItem(chosenTpl)[1];
// Create array with just magazine // Create array with just magazine
const defaultWeapon = this.itemHelper.getItem(rootItem._tpl)[1];
const magazineWithCartridges = [magazine]; const magazineWithCartridges = [magazine];
this.itemHelper.fillMagazineWithRandomCartridge( this.itemHelper.fillMagazineWithRandomCartridge(
magazineWithCartridges, magazineWithCartridges,
@ -1068,7 +1069,8 @@ export class LocationGenerator
staticAmmoDist, staticAmmoDist,
weaponTemplate._props.ammoCaliber, weaponTemplate._props.ammoCaliber,
0.25, 0.25,
this.itemHelper.getItem(rootItem._tpl)[1], defaultWeapon._props.defAmmo,
defaultWeapon,
); );
// Replace existing magazine with above array // Replace existing magazine with above array

View File

@ -1181,6 +1181,7 @@ export class ItemHelper
* @param staticAmmoDist Cartridge distribution * @param staticAmmoDist Cartridge distribution
* @param caliber Caliber of cartridge to add to magazine * @param caliber Caliber of cartridge to add to magazine
* @param minSizePercent % the magazine must be filled to * @param minSizePercent % the magazine must be filled to
* @param defaultCartridgeTpl Cartridge to use when none found
* @param weapon Weapon the magazine will be used for (if passed in uses Chamber as whitelist) * @param weapon Weapon the magazine will be used for (if passed in uses Chamber as whitelist)
*/ */
public fillMagazineWithRandomCartridge( public fillMagazineWithRandomCartridge(
@ -1189,7 +1190,8 @@ export class ItemHelper
staticAmmoDist: Record<string, IStaticAmmoDetails[]>, staticAmmoDist: Record<string, IStaticAmmoDetails[]>,
caliber: string = undefined, caliber: string = undefined,
minSizePercent = 0.25, minSizePercent = 0.25,
weapon: ITemplateItem = null, defaultCartridgeTpl?: string,
weapon?: ITemplateItem,
): void ): void
{ {
let chosenCaliber = caliber || this.getRandomValidCaliber(magTemplate); let chosenCaliber = caliber || this.getRandomValidCaliber(magTemplate);
@ -1204,9 +1206,15 @@ export class ItemHelper
const cartridgeTpl = this.drawAmmoTpl( const cartridgeTpl = this.drawAmmoTpl(
chosenCaliber, chosenCaliber,
staticAmmoDist, staticAmmoDist,
weapon?._props.defAmmo, defaultCartridgeTpl,
weapon?._props?.Chambers[0]?._props?.filters[0]?.Filter, weapon?._props?.Chambers[0]?._props?.filters[0]?.Filter,
); );
if (!cartridgeTpl)
{
return;
}
this.fillMagazineWithCartridge(magazine, magTemplate, cartridgeTpl, minSizePercent); this.fillMagazineWithCartridge(magazine, magTemplate, cartridgeTpl, minSizePercent);
} }
@ -1324,9 +1332,8 @@ export class ItemHelper
cartridgeWhitelist: string[] = null, cartridgeWhitelist: string[] = null,
): string ): string
{ {
const ammoArray = new ProbabilityObjectArray<string>(this.mathUtil, this.jsonUtil);
const ammos = staticAmmoDist[caliber]; const ammos = staticAmmoDist[caliber];
if (!ammos) if (!ammos && fallbackCartridgeTpl)
{ {
this.logger.error( this.logger.error(
`Unable to pick a cartridge for caliber: ${caliber} as staticAmmoDist has no data. using fallback value of ${fallbackCartridgeTpl}`, `Unable to pick a cartridge for caliber: ${caliber} as staticAmmoDist has no data. using fallback value of ${fallbackCartridgeTpl}`,
@ -1335,7 +1342,7 @@ export class ItemHelper
return fallbackCartridgeTpl; return fallbackCartridgeTpl;
} }
if (!Array.isArray(ammos)) if (!Array.isArray(ammos) && fallbackCartridgeTpl)
{ {
this.logger.error( this.logger.error(
`Unable to pick a cartridge for caliber: ${caliber}, the chosen staticAmmoDist data is not an array. Using fallback value of ${fallbackCartridgeTpl}`, `Unable to pick a cartridge for caliber: ${caliber}, the chosen staticAmmoDist data is not an array. Using fallback value of ${fallbackCartridgeTpl}`,
@ -1344,6 +1351,16 @@ export class ItemHelper
return fallbackCartridgeTpl; return fallbackCartridgeTpl;
} }
if (!ammos && !fallbackCartridgeTpl)
{
this.logger.error(
`Unable to pick a cartridge for caliber: ${caliber} as staticAmmoDist has no data. No fallback value provided`,
);
return;
}
const ammoArray = new ProbabilityObjectArray<string>(this.mathUtil, this.jsonUtil);
for (const icd of ammos) for (const icd of ammos)
{ {
// Whitelist exists and tpl not inside it, skip // Whitelist exists and tpl not inside it, skip