Add short circuit optimisation to isItemIncompatibleWithCurrentItems()

This commit is contained in:
Dev 2024-01-19 09:56:18 +00:00
parent f3f008e0ec
commit 63ca393372
3 changed files with 25 additions and 13 deletions

View File

@ -287,19 +287,25 @@ export class BotInventoryGenerator
continue; continue;
} }
if ( const compatabilityResult = this.botGeneratorHelper.isItemIncompatibleWithCurrentItems(
this.botGeneratorHelper.isItemIncompatibleWithCurrentItems(
settings.inventory.items, settings.inventory.items,
chosenItemTpl, chosenItemTpl,
settings.rootEquipmentSlot, settings.rootEquipmentSlot);
).incompatible) 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) if (attempts >= 8)
{ {
return; return;
} }
// remove picked item // Remove picked item
delete settings.rootEquipmentPool[chosenItemTpl]; delete settings.rootEquipmentPool[chosenItemTpl];
attempts++; attempts++;
@ -335,7 +341,7 @@ export class BotInventoryGenerator
); );
} }
// Item has slots // Item has slots, fill them
if ( pickedItemDb._props.Slots?.length > 0 ) if ( pickedItemDb._props.Slots?.length > 0 )
{ {
const items = this.botEquipmentModGenerator.generateModsForEquipment( const items = this.botEquipmentModGenerator.generateModsForEquipment(

View File

@ -286,7 +286,7 @@ export class BotGeneratorHelper
itemsEquipped: Item[], itemsEquipped: Item[],
tplToCheck: string, tplToCheck: string,
equipmentSlot: string, equipmentSlot: string,
): { incompatible: boolean; reason: string; } ): { incompatible: boolean; reason: string; slotBlocked?: boolean; }
{ {
// Skip slots that have no incompatibilities // Skip slots that have no incompatibilities
if (["Scabbard", "Backpack", "SecureContainer", "Holster", "ArmBand"].includes(equipmentSlot)) if (["Scabbard", "Backpack", "SecureContainer", "Holster", "ArmBand"].includes(equipmentSlot))
@ -333,6 +333,7 @@ export class BotGeneratorHelper
incompatible: true, incompatible: true,
reason: reason:
`${tplToCheck} ${itemToEquip._name} in slot: ${equipmentSlot} blocked by: ${blockingItem._id} ${blockingItem._name}`, `${tplToCheck} ${itemToEquip._name} in slot: ${equipmentSlot} blocked by: ${blockingItem._id} ${blockingItem._name}`,
slotBlocked: true,
}; };
} }
@ -345,6 +346,7 @@ export class BotGeneratorHelper
incompatible: true, incompatible: true,
reason: reason:
`${tplToCheck} ${itemToEquip._name} in slot: ${equipmentSlot} blocked by: ${blockingItem._id} ${blockingItem._name}`, `${tplToCheck} ${itemToEquip._name} in slot: ${equipmentSlot} blocked by: ${blockingItem._id} ${blockingItem._name}`,
slotBlocked: true,
}; };
} }
@ -358,6 +360,7 @@ export class BotGeneratorHelper
incompatible: true, incompatible: true,
reason: reason:
`${tplToCheck} ${itemToEquip._name} is blocked by: ${existingHeadwear._tpl} in slot: ${existingHeadwear.slotId}`, `${tplToCheck} ${itemToEquip._name} is blocked by: ${existingHeadwear._tpl} in slot: ${existingHeadwear.slotId}`,
slotBlocked: true,
}; };
} }
} }
@ -372,6 +375,7 @@ export class BotGeneratorHelper
incompatible: true, incompatible: true,
reason: reason:
`${tplToCheck} ${itemToEquip._name} is blocked by: ${existingFaceCover._tpl} in slot: ${existingFaceCover.slotId}`, `${tplToCheck} ${itemToEquip._name} is blocked by: ${existingFaceCover._tpl} in slot: ${existingFaceCover.slotId}`,
slotBlocked: true,
}; };
} }
} }
@ -386,6 +390,7 @@ export class BotGeneratorHelper
incompatible: true, incompatible: true,
reason: reason:
`${tplToCheck} ${itemToEquip._name} is blocked by: ${existingEarpiece._tpl} in slot: ${existingEarpiece.slotId}`, `${tplToCheck} ${itemToEquip._name} is blocked by: ${existingEarpiece._tpl} in slot: ${existingEarpiece.slotId}`,
slotBlocked: true,
}; };
} }
} }
@ -400,6 +405,7 @@ export class BotGeneratorHelper
incompatible: true, incompatible: true,
reason: reason:
`${tplToCheck} ${itemToEquip._name} is blocked by: ${existingArmorVest._tpl} in slot: ${existingArmorVest.slotId}`, `${tplToCheck} ${itemToEquip._name} is blocked by: ${existingArmorVest._tpl} in slot: ${existingArmorVest.slotId}`,
slotBlocked: true,
}; };
} }
} }

View File

@ -177,7 +177,7 @@ export class BotWeaponGeneratorHelper
for (const slot of equipmentSlots) for (const slot of equipmentSlots)
{ {
// Get container to put item into // 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) if (!container)
{ {
missingContainerCount++; missingContainerCount++;
@ -240,9 +240,9 @@ export class BotWeaponGeneratorHelper
break; break;
} }
// Get all root items in backpack // Get all root items in found container
const existingContainerItems = inventory.items.filter((i) => const existingContainerItems = inventory.items.filter((item) =>
i.parentId === container._id && i.slotId === slotGrid._name item.parentId === container._id && item.slotId === slotGrid._name
); );
// Get root items in container we can iterate over to find out what space is free // Get root items in container we can iterate over to find out what space is free