0
0
mirror of https://github.com/sp-tarkov/server.git synced 2025-02-13 09:50:43 -05:00

Improved about 5-10% performance on botgen and fixed bug that would skip containers when generating items that wouldnt fit in pockets (!314)

Co-authored-by: clodan <clodan@clodan.com>
Reviewed-on: SPT-AKI/Server#314
This commit is contained in:
chomp 2024-04-29 13:27:07 +00:00
parent 619137417b
commit b6926c1bf9
2 changed files with 46 additions and 11 deletions

View File

@ -119,6 +119,10 @@ export class BotLootGenerator
const containersBotHasAvailable = this.getAvailableContainersBotCanStoreItemsIn(botInventory); const containersBotHasAvailable = this.getAvailableContainersBotCanStoreItemsIn(botInventory);
// This set is passed as a reference to fill up the containers that are already full, this aliviates
// generation of the bots by avoiding checking the slots of containers we already know are full
const containersIdFull = new Set<string>();
// Special items // Special items
this.addLootFromPool( this.addLootFromPool(
this.botLootCacheService.getLootFromCache(botRole, isPmc, LootCacheType.SPECIAL, botJsonTemplate), this.botLootCacheService.getLootFromCache(botRole, isPmc, LootCacheType.SPECIAL, botJsonTemplate),
@ -127,6 +131,9 @@ export class BotLootGenerator
botInventory, botInventory,
botRole, botRole,
botItemLimits, botItemLimits,
undefined,
undefined,
containersIdFull,
); );
// Healing items / Meds // Healing items / Meds
@ -139,6 +146,7 @@ export class BotLootGenerator
null, null,
0, 0,
isPmc, isPmc,
containersIdFull,
); );
// Drugs // Drugs
@ -151,6 +159,7 @@ export class BotLootGenerator
null, null,
0, 0,
isPmc, isPmc,
containersIdFull,
); );
// Food // Food
@ -163,6 +172,7 @@ export class BotLootGenerator
null, null,
0, 0,
isPmc, isPmc,
containersIdFull,
); );
// Drink // Drink
@ -175,6 +185,7 @@ export class BotLootGenerator
null, null,
0, 0,
isPmc, isPmc,
containersIdFull,
); );
// Currency // Currency
@ -187,6 +198,7 @@ export class BotLootGenerator
null, null,
0, 0,
isPmc, isPmc,
containersIdFull,
); );
// Stims // Stims
@ -199,6 +211,7 @@ export class BotLootGenerator
botItemLimits, botItemLimits,
0, 0,
isPmc, isPmc,
containersIdFull,
); );
// Grenades // Grenades
@ -211,6 +224,7 @@ export class BotLootGenerator
null, null,
0, 0,
isPmc, isPmc,
containersIdFull,
); );
// Backpack - generate loot if they have one // Backpack - generate loot if they have one
@ -228,6 +242,7 @@ export class BotLootGenerator
botRole, botRole,
isPmc, isPmc,
botLevel, botLevel,
containersIdFull,
); );
} }
@ -240,6 +255,7 @@ export class BotLootGenerator
botItemLimits, botItemLimits,
this.pmcConfig.maxBackpackLootTotalRub, this.pmcConfig.maxBackpackLootTotalRub,
isPmc, isPmc,
containersIdFull,
); );
} }
@ -256,6 +272,7 @@ export class BotLootGenerator
botItemLimits, botItemLimits,
this.pmcConfig.maxVestLootTotalRub, this.pmcConfig.maxVestLootTotalRub,
isPmc, isPmc,
containersIdFull,
); );
} }
@ -269,6 +286,7 @@ export class BotLootGenerator
botItemLimits, botItemLimits,
this.pmcConfig.maxPocketLootTotalRub, this.pmcConfig.maxPocketLootTotalRub,
isPmc, isPmc,
containersIdFull,
); );
// Secure // Secure
@ -281,6 +299,7 @@ export class BotLootGenerator
null, null,
-1, -1,
isPmc, isPmc,
containersIdFull,
); );
} }
@ -373,6 +392,7 @@ export class BotLootGenerator
itemSpawnLimits: IItemSpawnLimitSettings = null, itemSpawnLimits: IItemSpawnLimitSettings = null,
totalValueLimitRub = 0, totalValueLimitRub = 0,
isPmc = false, isPmc = false,
containersIdFull = new Set<string>(),
): void ): void
{ {
// Loot pool has items // Loot pool has items
@ -465,6 +485,7 @@ export class BotLootGenerator
itemToAddTemplate._id, itemToAddTemplate._id,
itemWithChildrenToAdd, itemWithChildrenToAdd,
inventoryToAddItemsTo, inventoryToAddItemsTo,
containersIdFull,
); );
// Handle when item cannot be added // Handle when item cannot be added
@ -593,6 +614,7 @@ export class BotLootGenerator
botRole: string, botRole: string,
isPmc: boolean, isPmc: boolean,
botLevel: number, botLevel: number,
containersIdFull?: Set<string>,
): void ): void
{ {
const chosenWeaponType = this.randomUtil.getArrayValue([ const chosenWeaponType = this.randomUtil.getArrayValue([
@ -625,6 +647,7 @@ export class BotLootGenerator
generatedWeapon.weapon[0]._tpl, generatedWeapon.weapon[0]._tpl,
[...generatedWeapon.weapon], [...generatedWeapon.weapon],
botInventory, botInventory,
containersIdFull,
); );
if (result !== ItemAddedResult.SUCCESS) if (result !== ItemAddedResult.SUCCESS)

View File

@ -532,12 +532,17 @@ export class BotGeneratorHelper
rootItemTplId: string, rootItemTplId: string,
itemWithChildren: Item[], itemWithChildren: Item[],
inventory: Inventory, inventory: Inventory,
containersIdFull?: Set<string>,
): ItemAddedResult ): ItemAddedResult
{ {
/** Track how many containers are unable to be found */ /** Track how many containers are unable to be found */
let missingContainerCount = 0; let missingContainerCount = 0;
for (const equipmentSlotId of equipmentSlots) for (const equipmentSlotId of equipmentSlots)
{ {
if (containersIdFull?.has(equipmentSlotId))
{
continue;
}
// Get container to put item into // Get container to put item into
const container = inventory.items.find((item) => item.slotId === equipmentSlotId); const container = inventory.items.find((item) => item.slotId === equipmentSlotId);
if (!container) if (!container)
@ -583,8 +588,11 @@ export class BotGeneratorHelper
const totalSlotGridCount = containerTemplate[1]._props.Grids.length; const totalSlotGridCount = containerTemplate[1]._props.Grids.length;
for (const slotGrid of containerTemplate[1]._props.Grids) for (const slotGrid of containerTemplate[1]._props.Grids)
{ {
// Grid is empty, skip // Grid is empty, skip or item size is bigger than grid
if (slotGrid._props.cellsH === 0 || slotGrid._props.cellsV === 0) if (
(slotGrid._props.cellsH === 0 || slotGrid._props.cellsV === 0)
|| (itemSize[0] * itemSize[1] > slotGrid._props.cellsV * slotGrid._props.cellsH)
)
{ {
continue; continue;
} }
@ -592,12 +600,6 @@ export class BotGeneratorHelper
// Can't put item type in grid, skip all grids as we're assuming they have the same rules // Can't put item type in grid, skip all grids as we're assuming they have the same rules
if (!this.itemAllowedInContainer(slotGrid, rootItemTplId)) if (!this.itemAllowedInContainer(slotGrid, rootItemTplId))
{ {
// Only one possible slot and item is incompatible, exit function and inform caller
if (equipmentSlots.length === 1)
{
return ItemAddedResult.INCOMPATIBLE_ITEM;
}
// Multiple containers, maybe next one allows item, only break out of loop for this containers grids // Multiple containers, maybe next one allows item, only break out of loop for this containers grids
break; break;
} }
@ -653,15 +655,25 @@ export class BotGeneratorHelper
// If we've checked all grids in container and reached this point, there's no space for item // If we've checked all grids in container and reached this point, there's no space for item
if (currentGridCount >= totalSlotGridCount) if (currentGridCount >= totalSlotGridCount)
{ {
return ItemAddedResult.NO_SPACE; break;
} }
currentGridCount++;
currentGridCount++;
// No space in this grid, move to next container grid and try again // No space in this grid, move to next container grid and try again
} }
// if we got to this point, the item couldnt be placed on the container
if (containersIdFull)
{
// if the item was a one by one, we know it must be full. Or if the maps cant find a slot for a one by one
if ((itemSize[0] === 1 && itemSize[1] === 1))
{
containersIdFull.add(equipmentSlotId);
}
}
} }
return ItemAddedResult.UNKNOWN; return ItemAddedResult.NO_SPACE;
} }
/** /**