From 118721c436beddbafb385bbd0510f7a8587e35d0 Mon Sep 17 00:00:00 2001 From: Chomp Date: Sun, 20 Apr 2025 17:04:46 +0100 Subject: [PATCH] Rewrote `LocateSlot` --- .../Helpers/ContainerHelper.cs | 74 ++++++++----------- 1 file changed, 31 insertions(+), 43 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Helpers/ContainerHelper.cs b/Libraries/SPTarkov.Server.Core/Helpers/ContainerHelper.cs index a3460ced..9e7cd1ba 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/ContainerHelper.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/ContainerHelper.cs @@ -42,7 +42,7 @@ public class ContainerHelper // Go left to right across x-axis looking for free position for (var x = 0; x < limitX; x++) { - if (LocateSlot(container2D, containerX, containerY, x, y, itemWidth, itemHeight)) + if (CanItemBePlacedInContainerAtPosition(container2D, containerX, containerY, x, y, itemWidth, itemHeight)) { // Success, return result return new FindSlotResult(true, x, y, rotation); @@ -55,7 +55,7 @@ public class ContainerHelper } // Bigger than 1x1, try rotating by swapping x and y values - if (!LocateSlot(container2D, containerX, containerY, x, y, itemHeight, itemWidth)) + if (!CanItemBePlacedInContainerAtPosition(container2D, containerX, containerY, x, y, itemHeight, itemWidth)) { continue; } @@ -77,58 +77,46 @@ public class ContainerHelper } /// - /// Find a slot inside a container an item can be placed in + /// Can an item of specified size be placed inside a 2d container at a specific position /// - /// Container to find space in - /// Container x size - /// Container y size - /// ??? - /// ??? - /// Items width - /// Items height + /// Container to find space in + /// Container x size + /// Container y size + /// Starting x position for item + /// Starting y position for item + /// Items width + /// Items height /// True - slot found - protected bool LocateSlot( - int[][] container2D, - int containerX, - int containerY, - int x, - int y, - int itemW, - int itemH) + protected bool CanItemBePlacedInContainerAtPosition( + int[][] container, + int containerWidth, + int containerHeight, + int startXPos, + int startYPos, + int itemWidth, + int itemHeight) { - var foundSlot = true; - - for (var itemY = 0; itemY < itemH; itemY++) + // Check item isn't bigger than container when at position + if (startXPos + itemWidth > containerWidth || startYPos + itemHeight > containerHeight) { - if (foundSlot && y + itemH - 1 > containerY - 1) - { - foundSlot = false; - break; - } + return false; + } - // Does item fit x-ways across - for (var itemX = 0; itemX < itemW; itemX++) + // Check each position item will take up in container, go across and then down + for (var itemY = startYPos; itemY < startYPos + itemHeight; itemY++) + { + for (var itemX = startXPos; itemX < startXPos + itemWidth; itemX++) { - if (foundSlot && x + itemW - 1 > containerX - 1) + // e,g for a 2x2 item; [0,0] then [0,1] then [1,0] then [1,1] + if (container[itemY][itemX] != 0) { - foundSlot = false; - break; + // x,y Position blocked, can't place + return false; } - - if (container2D[y + itemY][x + itemX] != 0) - { - foundSlot = false; - break; - } - } - - if (!foundSlot) - { - break; } } - return foundSlot; + return true; } ///