Skip to content

Commit

Permalink
Implement InventorySpaceValidator.ItemsFit
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanmoffat committed Sep 16, 2022
1 parent c0d1145 commit fdd97b2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 36 deletions.
2 changes: 1 addition & 1 deletion EOLib.IO/Map/Matrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Matrix(int rows, int cols, T defaultValue)
Fill(defaultValue);
}

public Matrix(Matrix<T> other)
public Matrix(IReadOnlyMatrix<T> other)
: this(new T[other.Rows, other.Cols])
{
for (int row = 0; row < other.Rows; ++row)
Expand Down
10 changes: 10 additions & 0 deletions EndlessClient/HUD/Inventory/InventorySlotRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,35 @@
using EndlessClient.HUD.Panels;
using EOLib;
using EOLib.IO.Map;
using System.Collections.Generic;

namespace EndlessClient.HUD.Inventory
{
public interface IInventorySlotRepository
{
Matrix<bool> FilledSlots { get; set; }

Dictionary<int, int> SlotMap { get; set; }
}

public interface IInventorySlotProvider
{
IReadOnlyMatrix<bool> FilledSlots { get; }

IReadOnlyDictionary<int, int> SlotMap { get; }
}

[AutoMappedType(IsSingleton = true)]
public class InventorySlotRepository : IInventorySlotProvider, IInventorySlotRepository, IResettable
{
public Matrix<bool> FilledSlots { get; set; }

public Dictionary<int, int> SlotMap { get; set; }

IReadOnlyMatrix<bool> IInventorySlotProvider.FilledSlots => FilledSlots;

IReadOnlyDictionary<int, int> IInventorySlotProvider.SlotMap => SlotMap;

public InventorySlotRepository()
{
ResetState();
Expand All @@ -30,6 +39,7 @@ public InventorySlotRepository()
public void ResetState()
{
FilledSlots = new Matrix<bool>(InventoryPanel.InventoryRows, InventoryPanel.InventoryRowSlots, false);
SlotMap = new Dictionary<int, int>();
}
}
}
26 changes: 26 additions & 0 deletions EndlessClient/HUD/Inventory/InventorySpaceValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,6 +43,31 @@ public bool ItemFits(int itemId)

public bool ItemsFit(IReadOnlyList<InventoryItem> outItems, IReadOnlyList<InventoryItem> inItems)
{
var slotsCopy = new Matrix<bool>(_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<int>())
.Match(some: slot =>
{
_inventoryService.SetSlots(slotsCopy, slot, itemData.Size);
return true;
},
none: () => false);
if (!itemFits)
return false;
}

return true;
}

Expand Down
37 changes: 2 additions & 35 deletions EndlessClient/HUD/Panels/InventoryPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public class InventoryPanel : XNAPanel, IHudPanel
private readonly IActiveDialogProvider _activeDialogProvider;
private readonly ISfxPlayer _sfxPlayer;

private readonly Dictionary<int, int> _itemSlotMap;
private readonly List<InventoryPanelItem> _childItems = new List<InventoryPanelItem>();

private readonly IXNALabel _weightLabel;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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 =>
Expand Down Expand Up @@ -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<int> GetOverlappingTakenSlots(int newSlot, ItemSize size, IEnumerable<(int Slot, ItemSize Size)> items)
Expand Down

0 comments on commit fdd97b2

Please sign in to comment.