From 18e875102f88970dec79a6cbf9e4b44e4520adad Mon Sep 17 00:00:00 2001 From: Chomp Date: Sun, 20 Apr 2025 16:33:53 +0100 Subject: [PATCH] Cleaned up `FindSlotForItem` in an attempt to make it less vile --- .../Helpers/ContainerHelper.cs | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Helpers/ContainerHelper.cs b/Libraries/SPTarkov.Server.Core/Helpers/ContainerHelper.cs index eb521d22..a3460ced 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/ContainerHelper.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/ContainerHelper.cs @@ -9,13 +9,15 @@ public class ContainerHelper /// /// Finds a slot for an item in a given 2D container map /// - /// List of container with slots filled/free + /// List of container with positions filled/free /// Width of item /// Height of item /// Location to place item in container public FindSlotResult FindSlotForItem(int[][] container2D, int itemWidth, int itemHeight) { + // Assume not rotated var rotation = false; + var minVolume = (itemWidth < itemHeight ? itemWidth : itemHeight) - 1; var containerY = container2D.Length; var containerX = container2D[0].Length; @@ -37,38 +39,41 @@ public class ContainerHelper continue; } - // Try each slot on the row (across = x) + // Go left to right across x-axis looking for free position for (var x = 0; x < limitX; x++) { - var foundSlot = LocateSlot(container2D, containerX, containerY, x, y, itemWidth, itemHeight); - if (foundSlot) + if (LocateSlot(container2D, containerX, containerY, x, y, itemWidth, itemHeight)) { + // Success, return result return new FindSlotResult(true, x, y, rotation); } - // Failed to find slot, rotate item and try again - if (!foundSlot && ItemBiggerThan1X1(itemWidth, itemHeight)) + if (ItemBiggerThan1X1(itemWidth, itemHeight)) { - // Bigger than 1x1, try rotating - foundSlot = LocateSlot(container2D, containerX, containerY, x, y, itemHeight, itemWidth); // Height/Width swapped - if (foundSlot) - { - // Found a slot for it when rotated - rotation = true; - - return new FindSlotResult(true, x, y, rotation); - } + // Pointless rotating a 1x1, try next position across + continue; } + + // Bigger than 1x1, try rotating by swapping x and y values + if (!LocateSlot(container2D, containerX, containerY, x, y, itemHeight, itemWidth)) + { + continue; + } + + // Found a position for item when rotated + rotation = true; + + return new FindSlotResult(true, x, y, rotation); } } - // Tried all possible holes, nothing big enough for the item + // Tried all possible positions, nothing big enough for item return new FindSlotResult(false); } protected static bool ItemBiggerThan1X1(int itemWidth, int itemHeight) { - return itemWidth * itemHeight > 1; + return itemWidth + itemHeight > 2; } ///