From 8a26d3f8074965fe8d862da17ddf9e11898e9837 Mon Sep 17 00:00:00 2001 From: Dev Date: Thu, 27 Jul 2023 13:14:09 +0100 Subject: [PATCH] Rework items added to container code, now stops after 4 failed attempts Don't increment cost of loot container total value if item was not placed in container --- project/assets/configs/bot.json | 3 ++- project/src/generators/BotLootGenerator.ts | 23 +++++++++++++--- .../src/helpers/BotWeaponGeneratorHelper.ts | 26 +++++++++++-------- project/src/models/enums/ItemAddedResult.ts | 5 ++++ 4 files changed, 42 insertions(+), 15 deletions(-) create mode 100644 project/src/models/enums/ItemAddedResult.ts diff --git a/project/assets/configs/bot.json b/project/assets/configs/bot.json index d41088dc..ef9374c2 100644 --- a/project/assets/configs/bot.json +++ b/project/assets/configs/bot.json @@ -1517,5 +1517,6 @@ "tarkovstreets": 18, "default": 15 }, - "botGenerationBatchSizePerType": 15 + "botGenerationBatchSizePerType": 15, + "forceHealingItemsIntoSecure": true } diff --git a/project/src/generators/BotLootGenerator.ts b/project/src/generators/BotLootGenerator.ts index a6f933eb..c14fbfd0 100644 --- a/project/src/generators/BotLootGenerator.ts +++ b/project/src/generators/BotLootGenerator.ts @@ -11,6 +11,7 @@ import { ITemplateItem } from "../models/eft/common/tables/ITemplateItem"; import { BaseClasses } from "../models/enums/BaseClasses"; import { ConfigTypes } from "../models/enums/ConfigTypes"; import { EquipmentSlots } from "../models/enums/EquipmentSlots"; +import { ItemAddedResult } from "../models/enums/ItemAddedResult"; import { LootCacheType } from "../models/spt/bots/IBotLootCache"; import { IBotConfig } from "../models/spt/config/IBotConfig"; import { ILogger } from "../models/spt/utils/ILogger"; @@ -74,7 +75,7 @@ export class BotLootGenerator const grenadeCount = this.getRandomisedCount(itemCounts.grenades.min, itemCounts.grenades.max, 4); // Forced pmc healing loot - if (isPmc) + if (isPmc && this.botConfig.pmc.forceHealingItemsIntoSecure) { this.addForcedMedicalItemsToPmcSecure(botInventory, botRole); } @@ -249,6 +250,7 @@ export class BotLootGenerator let currentTotalRub = 0; const itemLimits: Record = {}; const itemSpawnLimits: Record> = {}; + let fitItemIntoContainerAttempts = 0; for (let i = 0; i < totalItemCount; i++) { const itemToAddTemplate = this.getRandomItemFromPoolByRole(pool, botRole); @@ -294,10 +296,25 @@ export class BotLootGenerator this.randomiseAmmoStackSize(isPmc, itemToAddTemplate, itemsToAdd[0]); } - this.botWeaponGeneratorHelper.addItemWithChildrenToEquipmentSlot(equipmentSlots, id, itemToAddTemplate._id, itemsToAdd, inventoryToAddItemsTo); + // Attempt to add item to container(s) + const itemAddedResult = this.botWeaponGeneratorHelper.addItemWithChildrenToEquipmentSlot(equipmentSlots, id, itemToAddTemplate._id, itemsToAdd, inventoryToAddItemsTo); + if (itemAddedResult === ItemAddedResult.NO_SPACE) + { + fitItemIntoContainerAttempts++; + if (fitItemIntoContainerAttempts >= 4) + { + this.logger.debug(`Failed to place item ${i} of ${totalItemCount} item into ${botRole} container: ${equipmentSlots}, ${fitItemIntoContainerAttempts} times, skipping`); + + break; + } + } + else + { + fitItemIntoContainerAttempts = 0; + } // Stop adding items to bots pool if rolling total is over total limit - if (totalValueLimitRub > 0) + if (totalValueLimitRub > 0 && itemAddedResult === ItemAddedResult.SUCCESS) { currentTotalRub += this.handbookHelper.getTemplatePrice(itemToAddTemplate._id); if (currentTotalRub > totalValueLimitRub) diff --git a/project/src/helpers/BotWeaponGeneratorHelper.ts b/project/src/helpers/BotWeaponGeneratorHelper.ts index 28c121b1..ad6798f1 100644 --- a/project/src/helpers/BotWeaponGeneratorHelper.ts +++ b/project/src/helpers/BotWeaponGeneratorHelper.ts @@ -6,6 +6,7 @@ import { Item } from "../models/eft/common/tables/IItem"; import { Grid, ITemplateItem } from "../models/eft/common/tables/ITemplateItem"; import { BaseClasses } from "../models/enums/BaseClasses"; import { EquipmentSlots } from "../models/enums/EquipmentSlots"; +import { ItemAddedResult } from "../models/enums/ItemAddedResult"; import { ILogger } from "../models/spt/utils/ILogger"; import { DatabaseServer } from "../servers/DatabaseServer"; import { LocalisationService } from "../services/LocalisationService"; @@ -153,7 +154,7 @@ export class BotWeaponGeneratorHelper * @param inventory Inventory to add item+children into * @returns a `boolean` indicating item was added */ - public addItemWithChildrenToEquipmentSlot(equipmentSlots: string[], parentId: string, parentTpl: string, itemWithChildren: Item[], inventory: Inventory): boolean + public addItemWithChildrenToEquipmentSlot(equipmentSlots: string[], parentId: string, parentTpl: string, itemWithChildren: Item[], inventory: Inventory): ItemAddedResult { for (const slot of equipmentSlots) { @@ -201,8 +202,8 @@ export class BotWeaponGeneratorHelper const containerItems = inventory.items.filter(i => i.parentId === container._id && i.slotId === slotGrid._name); // Get a copy of base level items we can iterate over - const itemsToCheck = containerItems.filter(x => x.slotId === slotGrid._name); - for (const item of itemsToCheck) + const containerItemsToCheck = containerItems.filter(x => x.slotId === slotGrid._name); + for (const item of containerItemsToCheck) { // Look for children on items, insert into array if found // (used later when figuring out how much space weapon takes up) @@ -213,13 +214,17 @@ export class BotWeaponGeneratorHelper } } - const slotMap = this.inventoryHelper.getContainerMap(slotGrid._props.cellsH, slotGrid._props.cellsV, containerItems, container._id); - const findSlotResult = this.containerHelper.findSlotForItem(slotMap, itemSize[0], itemSize[1]); + // Get rid of items free/used spots in current grid + const slotGridMap = this.inventoryHelper.getContainerMap(slotGrid._props.cellsH, slotGrid._props.cellsV, containerItems, container._id); + // Try to fit item into grid + const findSlotResult = this.containerHelper.findSlotForItem(slotGridMap, itemSize[0], itemSize[1]); + // Open slot found, add item to inventory if (findSlotResult.success) { const parentItem = itemWithChildren.find(i => i._id === parentId); + // Set items parent to container id parentItem.parentId = container._id; parentItem.slotId = slotGrid._name; parentItem.location = { @@ -229,16 +234,15 @@ export class BotWeaponGeneratorHelper }; inventory.items.push(...itemWithChildren); - return true; - } - else - { - this.logger.warning(`Unable to find space in container: ${container.slotId} item: ${itemWithChildren[0]._tpl}`); + + return ItemAddedResult.SUCCESS; } + + // Start loop again in next grid of container } } - return false; + return ItemAddedResult.NO_SPACE; } /** diff --git a/project/src/models/enums/ItemAddedResult.ts b/project/src/models/enums/ItemAddedResult.ts new file mode 100644 index 00000000..2dca874a --- /dev/null +++ b/project/src/models/enums/ItemAddedResult.ts @@ -0,0 +1,5 @@ +export enum ItemAddedResult + { + SUCCESS = 1, + NO_SPACE = 2 +} \ No newline at end of file