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
This commit is contained in:
Dev 2023-07-27 13:14:09 +01:00
parent 2d57bdb0ef
commit 8a26d3f807
4 changed files with 42 additions and 15 deletions

View File

@ -1517,5 +1517,6 @@
"tarkovstreets": 18,
"default": 15
},
"botGenerationBatchSizePerType": 15
"botGenerationBatchSizePerType": 15,
"forceHealingItemsIntoSecure": true
}

View File

@ -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<string, number> = {};
const itemSpawnLimits: Record<string,Record<string, number>> = {};
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)

View File

@ -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;
}
/**

View File

@ -0,0 +1,5 @@
export enum ItemAddedResult
{
SUCCESS = 1,
NO_SPACE = 2
}