Skip to content

Commit

Permalink
add inv funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaksuhn committed Jul 24, 2024
1 parent 0bca48f commit 4302f13
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
6 changes: 6 additions & 0 deletions SomethingNeedDoing/Misc/Changelog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ static void DisplayChangelog(string date, string changes, bool separator = true)
ImGui.Separator();
}
using var font = ImRaii.PushFont(UiBuilder.MonoFont);

DisplayChangelog(
"2024-07-24",
"- Added MoveItemToContainer()\n" +
"- Added GetItemCountInContainer()\n");

DisplayChangelog(
"2024-07-23",
"- Added TerritorySupportsMounting()\n" +
Expand Down
37 changes: 27 additions & 10 deletions SomethingNeedDoing/Misc/Commands/InventoryCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,23 @@ public List<string> ListAllFunctions()
}
return list;
}

public unsafe int GetItemCount(int itemID, bool includeHQ = true)
=> includeHQ ? InventoryManager.Instance()->GetInventoryItemCount((uint)itemID, true) + InventoryManager.Instance()->GetInventoryItemCount((uint)itemID) + InventoryManager.Instance()->GetInventoryItemCount((uint)itemID + 500_000)
: InventoryManager.Instance()->GetInventoryItemCount((uint)itemID) + InventoryManager.Instance()->GetInventoryItemCount((uint)itemID + 500_000);

public unsafe uint GetItemCountInContainer(uint itemID, uint container) => GetItemInInventory(itemID, (InventoryType)container)->Quantity;

public unsafe int GetInventoryFreeSlotCount()
{
InventoryType[] types = [InventoryType.Inventory1, InventoryType.Inventory2, InventoryType.Inventory3, InventoryType.Inventory4];
var c = InventoryManager.Instance();
var slots = 0;
foreach (var x in types)
{
var inv = c->GetInventoryContainer(x);
for (var i = 0; i < inv->Size; i++)
{
if (inv->Items[i].ItemId == 0)
{
var cont = InventoryManager.Instance()->GetInventoryContainer(x);
for (var i = 0; i < cont->Size; i++)
if (cont->Items[i].ItemId == 0)
slots++;
}
}
}
return slots;
}
Expand All @@ -48,10 +46,29 @@ public unsafe int GetFreeSlotsInContainer(uint container)
var cont = inv->GetInventoryContainer((InventoryType)container);
var slots = 0;
for (var i = 0; i < cont->Size; i++)
{
if (cont->Items[i].ItemId == 0)
slots++;
}
return slots;
}

public unsafe void MoveItemToContainer(uint itemID, uint srcContainer, uint dstContainer)
=> InventoryManager.Instance()->MoveItemSlot((InventoryType)srcContainer, (ushort)GetItemInInventory(itemID, (InventoryType)srcContainer)->Slot, (InventoryType)dstContainer, GetFirstAvailableSlot((InventoryType)dstContainer));

private static unsafe InventoryItem* GetItemInInventory(uint itemId, InventoryType inv, bool mustBeHQ = false)
{
var cont = InventoryManager.Instance()->GetInventoryContainer(inv);
for (var i = 0; i < cont->Size; ++i)
if (cont->GetInventorySlot(i)->ItemId == itemId && (!mustBeHQ || cont->GetInventorySlot(i)->Flags == InventoryItem.ItemFlags.HighQuality))
return cont->GetInventorySlot(i);
return null;
}

private static unsafe ushort GetFirstAvailableSlot(InventoryType container)
{
var cont = InventoryManager.Instance()->GetInventoryContainer(container);
for (var i = 0; i < cont->Size; i++)
if (cont->Items[i].ItemId == 0)
return (ushort)i;
return 0;
}
}

0 comments on commit 4302f13

Please sign in to comment.