Refactored getContainerMap()

This commit is contained in:
Dev 2024-08-31 15:17:24 +01:00
parent 184c81b912
commit 97bcce9819

View File

@ -114,6 +114,11 @@ export class InventoryHelper {
// Get stash layouts ready for use // Get stash layouts ready for use
const stashFS2D = this.getStashSlotMap(pmcData, sessionId); const stashFS2D = this.getStashSlotMap(pmcData, sessionId);
if (!stashFS2D) {
this.logger.error(`Unable to get stash map for players: ${sessionId} stash`);
return;
}
const sortingTableFS2D = this.getSortingTableSlotMap(pmcData); const sortingTableFS2D = this.getSortingTableSlotMap(pmcData);
// Find empty slot in stash for item being added - adds 'location' + parentid + slotId properties to root item // Find empty slot in stash for item being added - adds 'location' + parentid + slotId properties to root item
@ -208,6 +213,11 @@ export class InventoryHelper {
const pmcData = this.profileHelper.getPmcProfile(sessionId); const pmcData = this.profileHelper.getPmcProfile(sessionId);
const stashFS2D = this.cloner.clone(this.getStashSlotMap(pmcData, sessionId)); const stashFS2D = this.cloner.clone(this.getStashSlotMap(pmcData, sessionId));
if (!stashFS2D) {
this.logger.error(`Unable to get stash map for players: ${sessionId} stash`);
return false;
}
for (const itemWithChildren of itemsWithChildren) { for (const itemWithChildren of itemsWithChildren) {
if (!this.canPlaceItemInContainer(stashFS2D, itemWithChildren)) { if (!this.canPlaceItemInContainer(stashFS2D, itemWithChildren)) {
return false; return false;
@ -717,47 +727,52 @@ export class InventoryHelper {
} }
/** /**
* Get a 2d mapping of a container with what grid slots are filled
* @param containerH Horizontal size of container * @param containerH Horizontal size of container
* @param containerV Vertical size of container * @param containerV Vertical size of container
* @param itemList * @param itemList Players inventory items
* @param containerId Id of the container * @param containerId Id of the container
* @returns Two-dimensional representation of container * @returns Two-dimensional representation of container
*/ */
public getContainerMap(containerH: number, containerV: number, itemList: Item[], containerId: string): number[][] { public getContainerMap(containerH: number, containerV: number, itemList: Item[], containerId: string): number[][] {
const container2D: number[][] = this.getBlankContainerMap(containerH, containerV); // Create blank 2d map of container
const container2D = this.getBlankContainerMap(containerH, containerV);
// Get all items in players inventory keyed by their parentId and by ItemId
const inventoryItemHash = this.getInventoryItemHash(itemList); const inventoryItemHash = this.getInventoryItemHash(itemList);
// Get subset of items that belong to the desired container
const containerItemHash = inventoryItemHash.byParentId[containerId]; const containerItemHash = inventoryItemHash.byParentId[containerId];
if (!containerItemHash) { if (!containerItemHash) {
// No items in the container // No items in container, exit early
return container2D; return container2D;
} }
// Check each item in container
for (const item of containerItemHash) { for (const item of containerItemHash) {
if (!("location" in item)) { const itemLocation = item?.location as Location;
if (!itemLocation) {
// item has no location property
this.logger.error(`Unable to find 'location' property on item with id: ${item._id}, skipping`);
continue; continue;
} }
// Get x/y size of item
const tmpSize = this.getSizeByInventoryItemHash(item._tpl, item._id, inventoryItemHash); const tmpSize = this.getSizeByInventoryItemHash(item._tpl, item._id, inventoryItemHash);
const iW = tmpSize[0]; // x const iW = tmpSize[0]; // x
const iH = tmpSize[1]; // y const iH = tmpSize[1]; // y
const fH = const fH = this.isVertical(itemLocation) ? iW : iH;
(item.location as Location).r === 1 || const fW = this.isVertical(itemLocation) ? iH : iW;
(item.location as Location).r === "Vertical" ||
(item.location as Location).rotation === "Vertical" // Find the ending x coord of container
? iW const fillTo = itemLocation.x + fW;
: iH;
const fW =
(item.location as Location).r === 1 ||
(item.location as Location).r === "Vertical" ||
(item.location as Location).rotation === "Vertical"
? iH
: iW;
const fillTo = (item.location as Location).x + fW;
for (let y = 0; y < fH; y++) { for (let y = 0; y < fH; y++) {
try { try {
container2D[(item.location as Location).y + y].fill(1, (item.location as Location).x, fillTo); // Fill the corresponding cells in the container map to show the slot is taken
container2D[itemLocation.y + y].fill(1, itemLocation.x, fillTo);
} catch (e) { } catch (e) {
this.logger.error( this.logger.error(
this.localisationService.getText("inventory-unable_to_fill_container", { this.localisationService.getText("inventory-unable_to_fill_container", {
@ -772,6 +787,10 @@ export class InventoryHelper {
return container2D; return container2D;
} }
protected isVertical(itemLocation: Location): boolean {
return itemLocation.r === 1 || itemLocation.r === "Vertical" || itemLocation.rotation === "Vertical";
}
protected getInventoryItemHash(inventoryItem: Item[]): InventoryHelper.InventoryItemHash { protected getInventoryItemHash(inventoryItem: Item[]): InventoryHelper.InventoryItemHash {
const inventoryItemHash: InventoryHelper.InventoryItemHash = { byItemId: {}, byParentId: {} }; const inventoryItemHash: InventoryHelper.InventoryItemHash = { byItemId: {}, byParentId: {} };
for (const item of inventoryItem) { for (const item of inventoryItem) {