From 63ca3933727c5c5beaff89702476e54c484ed731 Mon Sep 17 00:00:00 2001 From: Dev Date: Fri, 19 Jan 2024 09:56:18 +0000 Subject: [PATCH] Add short circuit optimisation to `isItemIncompatibleWithCurrentItems()` --- .../src/generators/BotInventoryGenerator.ts | 22 ++++++++++++------- project/src/helpers/BotGeneratorHelper.ts | 8 ++++++- .../src/helpers/BotWeaponGeneratorHelper.ts | 8 +++---- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/project/src/generators/BotInventoryGenerator.ts b/project/src/generators/BotInventoryGenerator.ts index db4d3b95..f373c8fa 100644 --- a/project/src/generators/BotInventoryGenerator.ts +++ b/project/src/generators/BotInventoryGenerator.ts @@ -287,19 +287,25 @@ export class BotInventoryGenerator continue; } - if ( - this.botGeneratorHelper.isItemIncompatibleWithCurrentItems( - settings.inventory.items, - chosenItemTpl, - settings.rootEquipmentSlot, - ).incompatible) + const compatabilityResult = this.botGeneratorHelper.isItemIncompatibleWithCurrentItems( + settings.inventory.items, + chosenItemTpl, + settings.rootEquipmentSlot); + if (compatabilityResult.incompatible) { + // Entire slot is blocked by another item, no point in checking other items + if (compatabilityResult.slotBlocked) + { + return; + } + + // Tried 8 different items that failed, stop if (attempts >= 8) { return; } - // remove picked item + // Remove picked item delete settings.rootEquipmentPool[chosenItemTpl]; attempts++; @@ -335,7 +341,7 @@ export class BotInventoryGenerator ); } - // Item has slots + // Item has slots, fill them if ( pickedItemDb._props.Slots?.length > 0 ) { const items = this.botEquipmentModGenerator.generateModsForEquipment( diff --git a/project/src/helpers/BotGeneratorHelper.ts b/project/src/helpers/BotGeneratorHelper.ts index bd586fbf..4882f93b 100644 --- a/project/src/helpers/BotGeneratorHelper.ts +++ b/project/src/helpers/BotGeneratorHelper.ts @@ -286,7 +286,7 @@ export class BotGeneratorHelper itemsEquipped: Item[], tplToCheck: string, equipmentSlot: string, - ): { incompatible: boolean; reason: string; } + ): { incompatible: boolean; reason: string; slotBlocked?: boolean; } { // Skip slots that have no incompatibilities if (["Scabbard", "Backpack", "SecureContainer", "Holster", "ArmBand"].includes(equipmentSlot)) @@ -333,6 +333,7 @@ export class BotGeneratorHelper incompatible: true, reason: `${tplToCheck} ${itemToEquip._name} in slot: ${equipmentSlot} blocked by: ${blockingItem._id} ${blockingItem._name}`, + slotBlocked: true, }; } @@ -345,6 +346,7 @@ export class BotGeneratorHelper incompatible: true, reason: `${tplToCheck} ${itemToEquip._name} in slot: ${equipmentSlot} blocked by: ${blockingItem._id} ${blockingItem._name}`, + slotBlocked: true, }; } @@ -358,6 +360,7 @@ export class BotGeneratorHelper incompatible: true, reason: `${tplToCheck} ${itemToEquip._name} is blocked by: ${existingHeadwear._tpl} in slot: ${existingHeadwear.slotId}`, + slotBlocked: true, }; } } @@ -372,6 +375,7 @@ export class BotGeneratorHelper incompatible: true, reason: `${tplToCheck} ${itemToEquip._name} is blocked by: ${existingFaceCover._tpl} in slot: ${existingFaceCover.slotId}`, + slotBlocked: true, }; } } @@ -386,6 +390,7 @@ export class BotGeneratorHelper incompatible: true, reason: `${tplToCheck} ${itemToEquip._name} is blocked by: ${existingEarpiece._tpl} in slot: ${existingEarpiece.slotId}`, + slotBlocked: true, }; } } @@ -400,6 +405,7 @@ export class BotGeneratorHelper incompatible: true, reason: `${tplToCheck} ${itemToEquip._name} is blocked by: ${existingArmorVest._tpl} in slot: ${existingArmorVest.slotId}`, + slotBlocked: true, }; } } diff --git a/project/src/helpers/BotWeaponGeneratorHelper.ts b/project/src/helpers/BotWeaponGeneratorHelper.ts index 41b008b8..0683870c 100644 --- a/project/src/helpers/BotWeaponGeneratorHelper.ts +++ b/project/src/helpers/BotWeaponGeneratorHelper.ts @@ -177,7 +177,7 @@ export class BotWeaponGeneratorHelper for (const slot of equipmentSlots) { // Get container to put item into - const container = inventory.items.find((i) => i.slotId === slot); + const container = inventory.items.find((item) => item.slotId === slot); if (!container) { missingContainerCount++; @@ -240,9 +240,9 @@ export class BotWeaponGeneratorHelper break; } - // Get all root items in backpack - const existingContainerItems = inventory.items.filter((i) => - i.parentId === container._id && i.slotId === slotGrid._name + // Get all root items in found container + const existingContainerItems = inventory.items.filter((item) => + item.parentId === container._id && item.slotId === slotGrid._name ); // Get root items in container we can iterate over to find out what space is free