From 6faa69964d3b1d585cdf7575c374845c2f55d2cf Mon Sep 17 00:00:00 2001 From: Chomp Date: Sun, 20 Apr 2025 19:00:52 +0100 Subject: [PATCH] Improved comments inside `GetSizeByInventoryItemHash` and string comparisons --- .../Generators/LocationLootGenerator.cs | 5 ++++ .../Helpers/InventoryHelper.cs | 26 +++++++++++-------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Generators/LocationLootGenerator.cs b/Libraries/SPTarkov.Server.Core/Generators/LocationLootGenerator.cs index 09e7a56c..bf3eab46 100644 --- a/Libraries/SPTarkov.Server.Core/Generators/LocationLootGenerator.cs +++ b/Libraries/SPTarkov.Server.Core/Generators/LocationLootGenerator.cs @@ -518,6 +518,11 @@ public class LocationLootGenerator( return containerClone; } + /// + /// Get the height/width of an item including its children + /// + /// + /// protected ItemSize? GetItemSize(List? items) { var rootItem = items[0]; diff --git a/Libraries/SPTarkov.Server.Core/Helpers/InventoryHelper.cs b/Libraries/SPTarkov.Server.Core/Helpers/InventoryHelper.cs index 359ea8da..565c67d3 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/InventoryHelper.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/InventoryHelper.cs @@ -17,6 +17,7 @@ using SPTarkov.Common.Annotations; using SPTarkov.Common.Extensions; using LogLevel = SPTarkov.Server.Core.Models.Spt.Logging.LogLevel; using System.Collections.Frozen; +using System; namespace SPTarkov.Server.Core.Helpers; @@ -652,10 +653,10 @@ public class InventoryHelper( /// takes into account if item is folded /// /// Items template id - /// Items id + /// Items id /// Hashmap of inventory items /// An array representing the [width, height] of the item - protected List GetSizeByInventoryItemHash(string itemTpl, string itemID, InventoryItemHash inventoryItemHash) + protected List GetSizeByInventoryItemHash(string itemTpl, string itemId, InventoryItemHash inventoryItemHash) { // Invalid item var (isValidItem, itemTemplate) = _itemHelper.GetItem(itemTpl); @@ -686,7 +687,7 @@ public class InventoryHelper( return [1, 1]; // Invalid input data, return defaults } - var rootItem = inventoryItemHash.ByItemId[itemID]; + var rootItem = inventoryItemHash.ByItemId[itemId]; // Does root item support being folded var rootIsFoldable = itemTemplate.Properties.Foldable.GetValueOrDefault(false); @@ -709,11 +710,12 @@ public class InventoryHelper( outX -= itemTemplate.Properties.SizeReduceRight.Value; } - // Calculate size contribution from child items/attachments + // Item can have child items that adjust its size if (_itemHelper.IsOfBaseclasses(itemTpl, _variableSizeItemTypes)) { // Storage for root item and its children, store root item id for now - var toDo = new Queue([itemID]); + // Will store child items that may have sub-children to process + var toDo = new Queue([itemId]); while (toDo.Count > 0) { // Lookup parent in `todo` and get all of its children, then loop over them @@ -721,16 +723,16 @@ public class InventoryHelper( { foreach (var childItem in children) { - // Filtering child items outside of mod slots, such as those inside containers, without counting their ExtraSize attribute - if (childItem.SlotId.IndexOf("mod_", StringComparison.Ordinal) < 0) + // Skip mods that don't increase size. e.g. cartridges + if (!childItem.SlotId.StartsWith("mod_", StringComparison.OrdinalIgnoreCase)) { continue; } - // Add child to queue + // Add child to processing queue to be checked for sub-children later toDo.Enqueue(childItem.Id); - // If the barrel is folded the space in the barrel is not counted + // Get child item from db var (isValid, template) = _itemHelper.GetItem(childItem.Template); if (!isValid) { @@ -750,13 +752,14 @@ public class InventoryHelper( continue; } + // Child mod can and is folded, don't include it in size calc if (childIsFoldable && rootIsFolded && childIsFolded) { continue; } // Calculating child ExtraSize - if (template.Properties.ExtraSizeForceAdd == true) + if (template.Properties.ExtraSizeForceAdd.GetValueOrDefault(false)) { forcedUp += template.Properties.ExtraSizeUp.Value; forcedDown += template.Properties.ExtraSizeDown.Value; @@ -781,6 +784,7 @@ public class InventoryHelper( } } + // Item has been processed, remove from queue toDo.Dequeue(); } } @@ -903,7 +907,7 @@ public class InventoryHelper( protected bool IsVertical(ItemLocation itemLocation) { var castValue = itemLocation.R.ToString(); - return castValue == "1" || castValue == "Vertical" || itemLocation.Rotation?.ToString() == "Vertical"; + return castValue == "1" || string.Equals(castValue, "vertical", StringComparison.OrdinalIgnoreCase) || string.Equals(itemLocation.Rotation?.ToString(), "vertical", StringComparison.OrdinalIgnoreCase); } protected InventoryItemHash GetInventoryItemHash(List inventoryItems)