private List <Item> ConvertToStashItems(IList <PlayerItem> playerItems, int itemsRemaining, StashTab tab) { var result = new List <Item>(); var packer = new Packer(tab.Height, tab.Width); _itemSizeService.CacheItemSizes(playerItems); // Position existing items _itemSizeService.MapItemSizes(tab.Items); foreach (var item in tab.Items) { packer.Insert(new Shape { Height = item.Height, Width = item.Width }); } // Add and position any items that fits foreach (var playerItem in playerItems) { if (playerItem.StackCount == 0) { playerItem.StackCount = 1; } while (playerItem.StackCount > 0 && itemsRemaining > 0) { var stashItem = GetItem(playerItem); // Map item size and create a shape map if (!PositionItem(packer, stashItem)) { Logger.Info("Could not fit all items in stash, stopping early"); playerItem.StackCount += stashItem.StackCount; return(result); } result.Add(stashItem); itemsRemaining -= (int)stashItem.StackCount; } } Logger.Debug("All items fit into stash"); return(result); }
private static bool PositionItem(Packer packer, Item item) { var shape = new Shape { Height = item.Height, Width = item.Width }; var position = packer.Insert(shape); if (position == null) { return(false); } item.XOffset = position.UX; item.YOffset = position.UY; return(true); }
public bool Insert(AtlasImage image, out Rect rect) { int width = image.Width + Spacing.Left + Spacing.Right; int height = image.Height + Spacing.Top + Spacing.Bottom; if (Packer.Insert(width, height, Heuristic, out rect)) { // we don't want to change the size of the image, // so we remove the offsets after packing rect.X += Spacing.Left; rect.Y += Spacing.Top; rect.W -= Spacing.Left + Spacing.Right; rect.H -= Spacing.Top + Spacing.Bottom; return(true); } return(false); }
public override List <Container> GetContainers(InputData data) { Vector3 size = data.ContainerSize; List <Block> blocks = data.Blocks; var stopWatch = new Stopwatch(); stopWatch.Start(); var initialMemory = GC.GetTotalMemory(false); blocks.Sort((Block a, Block b) => { if (a.Volume < b.Volume) { return(1); } else if (a.Volume == b.Volume) { return(0); } else { return(-1); } }); Packer packer = new Packer(size); // Algorithm execution List <Container> containers = packer.Insert(blocks); var finalMemory = GC.GetTotalMemory(false); stopWatch.Stop(); TimeSpan timeElapsedSpan = stopWatch.Elapsed; TimeElapsed = timeElapsedSpan.TotalSeconds.ToString(); MemoryUsed = ((finalMemory - initialMemory) / 1_024).ToString(); return(containers); }