Fix forced compound looseloot throwing exceptions (!424)

Fixes error on Factory when halloween pumpkins attempt to spawn

Co-authored-by: DrakiaXYZ <565558+TheDgtl@users.noreply.github.com>
Reviewed-on: https://dev.sp-tarkov.com/SPT/Server/pulls/424
Co-authored-by: DrakiaXYZ <drakiaxyz@noreply.dev.sp-tarkov.com>
Co-committed-by: DrakiaXYZ <drakiaxyz@noreply.dev.sp-tarkov.com>
This commit is contained in:
DrakiaXYZ 2024-11-05 09:08:38 +00:00 committed by chomp
parent 5ed4802d9e
commit 7b35a71884

View File

@ -584,7 +584,7 @@ export class LocationLootGenerator {
dynamicForcedSpawnPoints.push(...dynamicLootDist.spawnpoints.filter((point) => point.template.IsAlwaysSpawn)); dynamicForcedSpawnPoints.push(...dynamicLootDist.spawnpoints.filter((point) => point.template.IsAlwaysSpawn));
// Add forced loot // Add forced loot
this.addForcedLoot(loot, dynamicForcedSpawnPoints, locationName); this.addForcedLoot(loot, dynamicForcedSpawnPoints, locationName, staticAmmoDist);
const allDynamicSpawnpoints = dynamicLootDist.spawnpoints; const allDynamicSpawnpoints = dynamicLootDist.spawnpoints;
@ -711,7 +711,7 @@ export class LocationLootGenerator {
// Draw a random item from spawn points possible items // Draw a random item from spawn points possible items
const chosenComposedKey = itemArray.draw(1)[0]; const chosenComposedKey = itemArray.draw(1)[0];
const createItemResult = this.createDynamicLootItem(chosenComposedKey, spawnPoint, staticAmmoDist); const createItemResult = this.createDynamicLootItem(chosenComposedKey, spawnPoint.template.Items, staticAmmoDist);
// Root id can change when generating a weapon, ensure ids match // Root id can change when generating a weapon, ensure ids match
spawnPoint.template.Root = createItemResult.items[0]._id; spawnPoint.template.Root = createItemResult.items[0]._id;
@ -735,6 +735,7 @@ export class LocationLootGenerator {
lootLocationTemplates: ISpawnpointTemplate[], lootLocationTemplates: ISpawnpointTemplate[],
forcedSpawnPoints: ISpawnpointsForced[], forcedSpawnPoints: ISpawnpointsForced[],
locationName: string, locationName: string,
staticAmmoDist: Record<string, IStaticAmmoDetails[]>,
): void { ): void {
const lootToForceSingleAmountOnMap = this.locationConfig.forcedLootSingleSpawnById[locationName]; const lootToForceSingleAmountOnMap = this.locationConfig.forcedLootSingleSpawnById[locationName];
if (lootToForceSingleAmountOnMap) { if (lootToForceSingleAmountOnMap) {
@ -771,8 +772,16 @@ export class LocationLootGenerator {
); );
continue; continue;
} }
lootItem.Root = this.objectId.generate();
lootItem.Items[0]._id = lootItem.Root; const createItemResult = this.createDynamicLootItem(
lootItem.Items[0]._id,
lootItem.Items,
staticAmmoDist
);
// Update root ID with the dynamically generated ID
lootItem.Root = createItemResult.items[0]._id;
lootItem.Items = createItemResult.items;
lootLocationTemplates.push(lootItem); lootLocationTemplates.push(lootItem);
} }
} }
@ -796,10 +805,15 @@ export class LocationLootGenerator {
} }
const locationTemplateToAdd = forcedLootLocation.template; const locationTemplateToAdd = forcedLootLocation.template;
const createItemResult = this.createDynamicLootItem(
locationTemplateToAdd.Items[0]._id,
forcedLootLocation.template.Items,
staticAmmoDist
);
// Ensure root id matches the first items id // Update root ID with the dynamically generated ID
locationTemplateToAdd.Root = this.objectId.generate(); forcedLootLocation.template.Root = createItemResult.items[0]._id;
locationTemplateToAdd.Items[0]._id = locationTemplateToAdd.Root; forcedLootLocation.template.Items = createItemResult.items;
// Push forced location into array as long as it doesnt exist already // Push forced location into array as long as it doesnt exist already
const existingLocation = lootLocationTemplates.some( const existingLocation = lootLocationTemplates.some(
@ -824,10 +838,10 @@ export class LocationLootGenerator {
*/ */
protected createDynamicLootItem( protected createDynamicLootItem(
chosenComposedKey: string, chosenComposedKey: string,
spawnPoint: ISpawnpoint, items: IItem[],
staticAmmoDist: Record<string, IStaticAmmoDetails[]>, staticAmmoDist: Record<string, IStaticAmmoDetails[]>,
): IContainerItem { ): IContainerItem {
const chosenItem = spawnPoint.template.Items.find((item) => item._id === chosenComposedKey); const chosenItem = items.find((item) => item._id === chosenComposedKey);
const chosenTpl = chosenItem?._tpl; const chosenTpl = chosenItem?._tpl;
if (!chosenTpl) { if (!chosenTpl) {
throw new Error(`Item for tpl ${chosenComposedKey} was not found in the spawn point`); throw new Error(`Item for tpl ${chosenComposedKey} was not found in the spawn point`);
@ -874,7 +888,7 @@ export class LocationLootGenerator {
// Also used by armors to get child mods // Also used by armors to get child mods
// Get item + children and add into array we return // Get item + children and add into array we return
let itemWithChildren = this.itemHelper.findAndReturnChildrenAsItems( let itemWithChildren = this.itemHelper.findAndReturnChildrenAsItems(
spawnPoint.template.Items, items,
chosenItem._id, chosenItem._id,
); );