Fixed location loot generator not creating unique IDs for weapons/armor child items

This commit is contained in:
Dev 2024-09-13 20:51:22 +01:00
parent eef071724f
commit 1d55075f99
3 changed files with 26 additions and 24 deletions

View File

@ -873,13 +873,13 @@ export class LocationLootGenerator {
} else {
// Also used by armors to get child mods
// Get item + children and add into array we return
const itemWithChildren = this.itemHelper.findAndReturnChildrenAsItems(
let itemWithChildren = this.itemHelper.findAndReturnChildrenAsItems(
spawnPoint.template.Items,
chosenItem._id,
);
// We need to reparent to ensure ids are unique
this.reparentItemAndChildren(itemWithChildren);
// Ensure all IDs are unique
itemWithChildren = this.itemHelper.replaceIDs(itemWithChildren);
itemWithMods.push(...itemWithChildren);
}
@ -890,26 +890,6 @@ export class LocationLootGenerator {
return { items: itemWithMods, width: size.width, height: size.height };
}
/**
* Replace the _id value for base item + all children items parentid value
* @param itemWithChildren Item with mods to update
* @param newId new id to add on chidren of base item
*/
protected reparentItemAndChildren(itemWithChildren: Item[], newId = this.objectId.generate()): void {
// original id on base item
const oldId = itemWithChildren[0]._id;
// Update base item to use new id
itemWithChildren[0]._id = newId;
// Update all parentIds of items attached to base item to use new id
for (const item of itemWithChildren) {
if (item.parentId === oldId) {
item.parentId = newId;
}
}
}
/**
* Find an item in array by its _tpl, handle differently if chosenTpl is a weapon
* @param items Items array to search
@ -977,7 +957,7 @@ export class LocationLootGenerator {
}
} else {
// RSP30 (62178be9d0050232da3485d9/624c0b3340357b5f566e8766/6217726288ed9f0845317459) doesnt have any default presets and kills this code below as it has no chidren to reparent
this.logger.debug(`createItem() No preset found for weapon: ${chosenTpl}`);
this.logger.debug(`createStaticLootItem() No preset found for weapon: ${chosenTpl}`);
}
const rootItem = items[0];

View File

@ -787,6 +787,27 @@ export class ItemHelper {
return matchingItems;
}
/**
* Replace the _id value for base item + all children that are children of it
* REPARENTS ROOT ITEM ID, NOTHING ELSE
* @param itemWithChildren Item with mods to update
* @param newId new id to add on chidren of base item
*/
public replaceRootItemID(itemWithChildren: Item[], newId = this.objectId.generate()): void {
// original id on base item
const oldId = itemWithChildren[0]._id;
// Update base item to use new id
itemWithChildren[0]._id = newId;
// Update all parentIds of items attached to base item to use new id
for (const item of itemWithChildren) {
if (item.parentId === oldId) {
item.parentId = newId;
}
}
}
/**
* Regenerate all GUIDs with new IDs, for the exception of special item types (e.g. quest, sorting table, etc.) This
* function will not mutate the original items array, but will return a new array with new GUIDs.

View File

@ -85,6 +85,7 @@ export class LocationLifecycleService {
this.locationConfig = this.configServer.getConfig(ConfigTypes.LOCATION);
}
/** Handle client/match/local/start */
public startLocalRaid(sessionId: string, request: IStartLocalRaidRequestData): IStartLocalRaidResponseData {
const playerProfile = this.profileHelper.getPmcProfile(sessionId);