From 572f6a5668f96b0021ad22124095828b33ce1851 Mon Sep 17 00:00:00 2001 From: Dev Date: Sun, 12 May 2024 10:15:08 +0100 Subject: [PATCH] Improved `getModPoolForSlot()` Comment changes Variable change Only log debug message when default mod not found + multiple choices exist in pool Fixed possible nullref if `getMatchingPreset()` returns null --- .../generators/BotEquipmentModGenerator.ts | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/project/src/generators/BotEquipmentModGenerator.ts b/project/src/generators/BotEquipmentModGenerator.ts index 88c0532c..45406055 100644 --- a/project/src/generators/BotEquipmentModGenerator.ts +++ b/project/src/generators/BotEquipmentModGenerator.ts @@ -950,22 +950,23 @@ export class BotEquipmentModGenerator if (modSpawnResult === ModSpawn.DEFAULT_MOD) { const matchingPreset = this.getMatchingPreset(weaponTemplate, parentTemplate._id); - const matchingMod = matchingPreset._items.find(item => + const matchingModFromPreset = matchingPreset?._items.find(item => item?.slotId?.toLowerCase() === modSlot.toLowerCase(), ); // Only filter mods down to single default item if it already exists in existing itemModPool, OR the default item has no children // Filtering mod pool to item that wasnt already there can have problems; // You'd have a mod being picked without any sub-mods in its chain, possibly resulting in missing required mods not being added - if (matchingMod) + if (matchingModFromPreset) { // Mod is in existing mod pool - if (itemModPool[modSlot].includes(matchingMod._tpl)) + if (itemModPool[modSlot].includes(matchingModFromPreset._tpl)) { // Found mod on preset + it already exists in mod pool - return [matchingMod._tpl]; + return [matchingModFromPreset._tpl]; } + // Get an array of items that are allowed in slot from parent item // Check the filter of the slot to ensure a chosen mod fits const parentSlotCompatibleItems = parentTemplate._props.Slots?.find(slot => slot._name.toLowerCase() === modSlot.toLowerCase(), @@ -973,16 +974,19 @@ export class BotEquipmentModGenerator // Mod isnt in existing pool, only add if it has no children and matches parent filter if ( - this.itemHelper.getItem(matchingMod._tpl)[1]._props.Slots.length === 0 - && parentSlotCompatibleItems.includes(matchingMod._tpl) + parentSlotCompatibleItems.includes(matchingModFromPreset._tpl) + && this.itemHelper.getItem(matchingModFromPreset._tpl)[1]._props.Slots.length === 0 ) { // Mod has no children and matches parent filters, can be used - return [matchingMod._tpl]; + return [matchingModFromPreset._tpl]; } } - this.logger.debug(`No default: ${modSlot} mod found on template: ${weaponTemplate._id}`); + if (itemModPool[modSlot]?.length > 1) + { + this.logger.debug(`No default: ${modSlot} mod found on template: ${weaponTemplate._name} and multiple items found in existing pool`); + } // Couldnt find default in globals, use existing mod pool data return itemModPool[modSlot]; @@ -1001,21 +1005,19 @@ export class BotEquipmentModGenerator * Get default preset for weapon, get specific weapon presets for edge cases (mp5/silenced dvl) * @param weaponTemplate * @param parentItemTpl - * @returns + * @returns Default preset found */ protected getMatchingPreset(weaponTemplate: ITemplateItem, parentItemTpl: string): IPreset { // Edge case - using mp5sd reciever means default mp5 handguard doesnt fit const isMp5sd = parentItemTpl === "5926f2e086f7745aae644231"; - - // Edge case - dvl 500mm is the silenced barrel and has specific muzzle mods - const isDvl500mmSilencedBarrel = parentItemTpl === "5888945a2459774bf43ba385"; - if (isMp5sd) { return this.presetHelper.getPreset("59411abb86f77478f702b5d2"); } + // Edge case - dvl 500mm is the silenced barrel and has specific muzzle mods + const isDvl500mmSilencedBarrel = parentItemTpl === "5888945a2459774bf43ba385"; if (isDvl500mmSilencedBarrel) { return this.presetHelper.getPreset("59e8d2b386f77445830dd299");