diff --git a/Libraries/SPTarkov.Server.Core/Extensions/ProfileExtensions.cs b/Libraries/SPTarkov.Server.Core/Extensions/ProfileExtensions.cs index 5b5525ad..c4ab820d 100644 --- a/Libraries/SPTarkov.Server.Core/Extensions/ProfileExtensions.cs +++ b/Libraries/SPTarkov.Server.Core/Extensions/ProfileExtensions.cs @@ -200,5 +200,32 @@ namespace SPTarkov.Server.Core.Extensions return pmcData.Info.Level; } + + /// + /// Does the provided item have a root item with the provided id + /// + /// Profile with items + /// Item to check + /// Root item id to check for + /// True when item has rootId, false when not + public static bool DoesItemHaveRootId(this PmcData pmcData, Item item, string rootId) + { + var currentItem = item; + while (currentItem is not null) + { + // If we've found the equipment root ID, return true + if (currentItem.Id == rootId) + { + return true; + } + + // Otherwise get the parent item + currentItem = pmcData.Inventory.Items.FirstOrDefault(item => + item.Id == currentItem.ParentId + ); + } + + return false; + } } } diff --git a/Libraries/SPTarkov.Server.Core/Helpers/InRaidHelper.cs b/Libraries/SPTarkov.Server.Core/Helpers/InRaidHelper.cs index 098c0df0..2caf807e 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/InRaidHelper.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/InRaidHelper.cs @@ -243,14 +243,10 @@ public class InRaidHelper( } // Pocket items are lost on death - // Ensure we dont pick up pocket items from manniquins + // Ensure we don't pick up pocket items from mannequins if ( item.SlotId.StartsWith("pocket") - && _inventoryHelper.DoesItemHaveRootId( - pmcProfile, - item, - pmcProfile.Inventory.Equipment - ) + && pmcProfile.DoesItemHaveRootId(item, pmcProfile.Inventory.Equipment) ) { return true; diff --git a/Libraries/SPTarkov.Server.Core/Helpers/InventoryHelper.cs b/Libraries/SPTarkov.Server.Core/Helpers/InventoryHelper.cs index f2172ad8..8aea7703 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/InventoryHelper.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/InventoryHelper.cs @@ -857,15 +857,23 @@ public class InventoryHelper( /// /// Get a 2d mapping of a container with what grid slots are filled /// - /// Horizontal size of container - /// Vertical size of container + /// Horizontal (Column) size of container + /// Vertical (Row) size of container /// Players inventory items /// Id of the container /// Two-dimensional representation of container - public int[,] GetContainerMap(int sizeX, int sizeY, List itemList, string containerId) + public int[,] GetContainerMap( + int containerSizeHorizontalX, + int containerSizeVerticalY, + List itemList, + string containerId + ) { // Create blank 2d map of container - var containerYX = _itemHelper.GetBlankContainerMap(sizeY, sizeX); + var container = _itemHelper.GetBlankContainerMap( + containerSizeHorizontalX, // Column count + containerSizeVerticalY // Row count + ); // Get all items in players inventory keyed by their parentId and by ItemId var inventoryItemHash = GetInventoryItemHash(itemList); @@ -874,7 +882,7 @@ public class InventoryHelper( if (!inventoryItemHash.ByParentId.TryGetValue(containerId, out var rootItemsInContainer)) { // No items in container, exit early and return the blank container map - return containerYX; + return container; } // Add every root items size (with mods attached) found in container @@ -892,26 +900,36 @@ public class InventoryHelper( } // Get x/y size of item - var (xSize, ySize) = GetSizeByInventoryItemHash( + var (itemXWidth, itemYHeight) = GetSizeByInventoryItemHash( rootItem.Template, rootItem.Id, inventoryItemHash ); - var itemHSize = itemLocation.IsVertical() ? xSize : ySize; - var itemWSize = itemLocation.IsVertical() ? ySize : xSize; + // Items horizontal size + var itemHSize = itemLocation.IsVertical() ? itemXWidth : itemYHeight; + // Items vertical size + var itemWSize = itemLocation.IsVertical() ? itemYHeight : itemXWidth; + + // vertical for (var yOffset = 0; yOffset < itemHSize; yOffset++) { + // horizontal for (var xOffset = 0; xOffset < itemWSize; xOffset++) { var currentY = itemLocation.Y.Value + yOffset; var currentX = itemLocation.X.Value + xOffset; // Check still in containers bounds - if (currentY >= 0 && currentY < sizeY && currentX >= 0 && currentX < sizeX) + if ( + currentY >= 0 + && currentY < containerSizeVerticalY + && currentX >= 0 + && currentX < containerSizeHorizontalX + ) { // mark slot used - containerYX[currentY, currentX] = 1; + container[currentY, currentX] = 1; } else { @@ -933,7 +951,7 @@ public class InventoryHelper( } } - return containerYX; + return container; } protected InventoryItemHash GetInventoryItemHash(List inventoryItems) @@ -1060,10 +1078,13 @@ public class InventoryHelper( var containerTemplate = _itemHelper.GetItem(containerTpl).Value; var firstContainerGrid = containerTemplate.Properties.Grids.FirstOrDefault(); - var containerH = firstContainerGrid.Props.CellsH; - var containerV = firstContainerGrid.Props.CellsV; + var containerRowCount = firstContainerGrid.Props.CellsH; + var containerColumnCount = firstContainerGrid.Props.CellsV; - return _itemHelper.GetBlankContainerMap(containerH.Value, containerV.Value); + return _itemHelper.GetBlankContainerMap( + containerColumnCount.Value, + containerRowCount.Value + ); } /// @@ -1153,7 +1174,7 @@ public class InventoryHelper( InventoryMoveRequestData request ) { - HandleCartridges(sourceItems, request); + HandleCartridgeMove(sourceItems, request); // Get all children item has, they need to move with item var idsToMove = sourceItems.FindAndReturnChildrenByItems(request.Item); @@ -1210,7 +1231,7 @@ public class InventoryHelper( ) { errorMessage = string.Empty; - HandleCartridges(inventoryItems, moveRequest); + HandleCartridgeMove(inventoryItems, moveRequest); // Find item we want to 'move' var matchingInventoryItem = inventoryItems.FirstOrDefault(item => @@ -1312,9 +1333,11 @@ public class InventoryHelper( } /// - /// Internal helper function to handle cartridges in inventory if any of them exist. + /// Helper function to handle cartridges in inventory if any of them exist. /// - protected void HandleCartridges(List items, InventoryMoveRequestData request) + /// + /// + protected void HandleCartridgeMove(List items, InventoryMoveRequestData request) { // Not moving item into a cartridge slot, skip if (request.To.Container != "cartridges") @@ -1333,9 +1356,11 @@ public class InventoryHelper( /// /// Container being opened /// Reward details - public RewardDetails GetRandomLootContainerRewardDetails(string itemTpl) + public RewardDetails? GetRandomLootContainerRewardDetails(string itemTpl) { - return _inventoryConfig.RandomLootContainers[itemTpl]; + _inventoryConfig.RandomLootContainers.TryGetValue(itemTpl, out var result); + + return result; } /// @@ -1362,33 +1387,6 @@ public class InventoryHelper( "This profile is not compatible with SPT, See above for a list of incompatible IDs that is not compatible. Loading of SPT has been halted, use another profile or create a new one" ); } - - /// - /// Does the provided item have a root item with the provided id - /// - /// Profile with items - /// Item to check - /// Root item id to check for - /// True when item has rootId, false when not - public bool DoesItemHaveRootId(PmcData pmcData, Item item, string rootId) - { - var currentItem = item; - while (currentItem is not null) - { - // If we've found the equipment root ID, return true - if (currentItem.Id == rootId) - { - return true; - } - - // Otherwise get the parent item - currentItem = pmcData.Inventory.Items.FirstOrDefault(item => - item.Id == currentItem.ParentId - ); - } - - return false; - } } public class InventoryItemHash diff --git a/Libraries/SPTarkov.Server.Core/Helpers/ItemHelper.cs b/Libraries/SPTarkov.Server.Core/Helpers/ItemHelper.cs index f475e053..30a1c9cb 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/ItemHelper.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/ItemHelper.cs @@ -2070,17 +2070,17 @@ public class ItemHelper( var height = containerTemplate.Properties.Grids[0].Props.CellsV; var width = containerTemplate.Properties.Grids[0].Props.CellsH; - return GetBlankContainerMap(height.Value, width.Value); + return GetBlankContainerMap(width.Value, height.Value); } /// /// Get a blank two-dimensional representation of a container /// - /// Horizontal size of container - /// Vertical size of container + /// Width of container (columns) + /// Height of container (rows) /// Two-dimensional representation of container - public int[,] GetBlankContainerMap(int containerY, int containerX) + public int[,] GetBlankContainerMap(int horizontalSizeX, int verticalSizeY) { - return new int[containerX, containerY]; + return new int[horizontalSizeX, verticalSizeY]; } }