diff --git a/EOLib.IO/Map/Matrix.cs b/EOLib.IO/Map/Matrix.cs index 553b51a9c..bfb05f807 100644 --- a/EOLib.IO/Map/Matrix.cs +++ b/EOLib.IO/Map/Matrix.cs @@ -30,7 +30,7 @@ public Matrix(int rows, int cols, T defaultValue) Fill(defaultValue); } - public Matrix(Matrix other) + public Matrix(IReadOnlyMatrix other) : this(new T[other.Rows, other.Cols]) { for (int row = 0; row < other.Rows; ++row) diff --git a/EndlessClient/HUD/Inventory/InventorySlotRepository.cs b/EndlessClient/HUD/Inventory/InventorySlotRepository.cs index 1f1b6811a..b3da1b47a 100644 --- a/EndlessClient/HUD/Inventory/InventorySlotRepository.cs +++ b/EndlessClient/HUD/Inventory/InventorySlotRepository.cs @@ -2,17 +2,22 @@ using EndlessClient.HUD.Panels; using EOLib; using EOLib.IO.Map; +using System.Collections.Generic; namespace EndlessClient.HUD.Inventory { public interface IInventorySlotRepository { Matrix FilledSlots { get; set; } + + Dictionary SlotMap { get; set; } } public interface IInventorySlotProvider { IReadOnlyMatrix FilledSlots { get; } + + IReadOnlyDictionary SlotMap { get; } } [AutoMappedType(IsSingleton = true)] @@ -20,8 +25,12 @@ public class InventorySlotRepository : IInventorySlotProvider, IInventorySlotRep { public Matrix FilledSlots { get; set; } + public Dictionary SlotMap { get; set; } + IReadOnlyMatrix IInventorySlotProvider.FilledSlots => FilledSlots; + IReadOnlyDictionary IInventorySlotProvider.SlotMap => SlotMap; + public InventorySlotRepository() { ResetState(); @@ -30,6 +39,7 @@ public InventorySlotRepository() public void ResetState() { FilledSlots = new Matrix(InventoryPanel.InventoryRows, InventoryPanel.InventoryRowSlots, false); + SlotMap = new Dictionary(); } } } diff --git a/EndlessClient/HUD/Inventory/InventorySpaceValidator.cs b/EndlessClient/HUD/Inventory/InventorySpaceValidator.cs index 54766b72c..a69af1f8a 100644 --- a/EndlessClient/HUD/Inventory/InventorySpaceValidator.cs +++ b/EndlessClient/HUD/Inventory/InventorySpaceValidator.cs @@ -5,6 +5,7 @@ using EOLib.IO.Map; using EOLib.IO.Repositories; using Optional; +using Optional.Collections; using System.Collections.Generic; using System.Linq; using System.Windows.Documents; @@ -42,6 +43,31 @@ public bool ItemFits(int itemId) public bool ItemsFit(IReadOnlyList outItems, IReadOnlyList inItems) { + var slotsCopy = new Matrix(_inventorySlotProvider.FilledSlots); + + foreach (var item in outItems) + { + var itemData = _eifFileProvider.EIFFile[item.ItemID]; + _inventorySlotProvider.SlotMap.SingleOrNone(x => x.Value == item.ItemID) + .Map(x => x.Key) + .MatchSome(x => _inventoryService.ClearSlots(slotsCopy, x, itemData.Size)); + } + + foreach (var item in inItems) + { + var itemData = _eifFileProvider.EIFFile[item.ItemID]; + var itemFits = _inventoryService + .GetNextOpenSlot(slotsCopy, itemData.Size, Option.None()) + .Match(some: slot => + { + _inventoryService.SetSlots(slotsCopy, slot, itemData.Size); + return true; + }, + none: () => false); + if (!itemFits) + return false; + } + return true; } diff --git a/EndlessClient/HUD/Panels/InventoryPanel.cs b/EndlessClient/HUD/Panels/InventoryPanel.cs index 7c89b0b5b..c87fd8238 100644 --- a/EndlessClient/HUD/Panels/InventoryPanel.cs +++ b/EndlessClient/HUD/Panels/InventoryPanel.cs @@ -50,7 +50,6 @@ public class InventoryPanel : XNAPanel, IHudPanel private readonly IActiveDialogProvider _activeDialogProvider; private readonly ISfxPlayer _sfxPlayer; - private readonly Dictionary _itemSlotMap; private readonly List _childItems = new List(); private readonly IXNALabel _weightLabel; @@ -101,7 +100,7 @@ public InventoryPanel(INativeGraphicsManager nativeGraphicsManager, AutoSize = false }; - _itemSlotMap = GetItemSlotMap(_playerInfoProvider.LoggedInAccountName, _characterProvider.MainCharacter.Name); + _inventorySlotRepository.SlotMap = GetItemSlotMap(_playerInfoProvider.LoggedInAccountName, _characterProvider.MainCharacter.Name); var weirdOffsetSheet = NativeGraphicsManager.TextureFromResource(GFXTypes.PostLoginUI, 27); @@ -206,7 +205,7 @@ protected override void OnUpdateControl(GameTime gameTime) { var itemData = _pubFileProvider.EIFFile[item.ItemID]; - var preferredSlot = _itemSlotMap.SingleOrNone(x => x.Value == item.ItemID).Map(x => x.Key); + var preferredSlot = _inventorySlotRepository.SlotMap.SingleOrNone(x => x.Value == item.ItemID).Map(x => x.Key); var actualSlot = _inventoryService.GetNextOpenSlot(_inventorySlotRepository.FilledSlots, itemData.Size, preferredSlot); actualSlot.MatchSome(slot => @@ -484,38 +483,6 @@ private void HandleItemDoneDragging(object sender, InventoryPanelItem.ItemDragCo { _sfxPlayer.PlaySfx(SoundEffectID.InventoryPlace); } - - #region Unimplemented drag action - /* - if (TradeDialog.Instance != null && TradeDialog.Instance.MouseOver && TradeDialog.Instance.MouseOverPreviously - && !TradeDialog.Instance.MainPlayerAgrees) - { - if (m_itemData.Special == ItemSpecial.Lore) - { - EOMessageBox.Show(DialogResourceID.ITEM_IS_LORE_ITEM); - } - else if (m_inventory.Amount > 1) - { - ItemTransferDialog dlg = new ItemTransferDialog(m_itemData.Name, ItemTransferDialog.TransferType.TradeItems, - m_inventory.Amount, EOResourceID.DIALOG_TRANSFER_OFFER); - dlg.DialogClosing += (o, e) => - { - if (e.Result != XNADialogResult.OK) return; - - if (!m_api.TradeAddItem(m_inventory.ItemID, dlg.SelectedAmount)) - { - TradeDialog.Instance.Close(XNADialogResult.NO_BUTTON_PRESSED); - ((EOGame)Game).DoShowLostConnectionDialogAndReturnToMainMenu(); - } - }; - } - else if (!m_api.TradeAddItem(m_inventory.ItemID, 1)) - { - TradeDialog.Instance.Close(XNADialogResult.NO_BUTTON_PRESSED); - ((EOGame)Game).DoShowLostConnectionDialogAndReturnToMainMenu(); - } - }*/ - #endregion } private static IEnumerable GetOverlappingTakenSlots(int newSlot, ItemSize size, IEnumerable<(int Slot, ItemSize Size)> items)