PKHeX/PKHeX.Core/Items/ItemStorage7USUM.cs
Kurt fa9a809751
Encapsulate item pouch arrays/etc for finer control (#3860)
* Extract/encapsulate inventory legal arrays to interface+class

Hiding them behind methods allows these to be left as ReadOnlySpan<ushort> and thus never allocate on the heap.
Also refactors out some logic for checking if an item is legal.

End result feels more maintainable and is less bloaty (no more passing in a nullable func)

Batch editing
* Add HasType filter

```
=HasType=11
.HeldItem=Meadow Plate
```

slaps a meadow plate on any pkm with grass type
Use `=PersonalType1=11` for only primary grass types; only-secondary-type grass will not match it.
2023-04-16 12:58:07 -07:00

89 lines
3 KiB
C#

using System;
namespace PKHeX.Core;
public sealed class ItemStorage7USUM : IItemStorage
{
public static readonly ItemStorage7USUM Instance = new();
private static ReadOnlySpan<ushort> Pouch_Key_USUM => new ushort[]
{
216,
440, // US/UM
465, 466, 628, 629, 631, 632, 638,
705, 706, 765, 773, 797,
841, 842, 843, 845, 847, 850, 857, 858, 860,
// US/UM
933, 934, 935, 936, 937, 938, 939, 940, 941, 942, 943, 944, 945, 946, 947, 948,
};
private static ReadOnlySpan<ushort> Pouch_Roto_USUM => new ushort[]
{
949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959,
};
internal static ReadOnlySpan<ushort> Pouch_ZCrystal_USUM => new ushort[]
{
// SM
807, 808, 809, 810, 811, 812, 813, 814, 815, 816, 817, 818, 819, 820, 821, 822, 823, 824, 825, 826, 827, 828, 829, 830, 831, 832, 833, 834, 835,
// US/UM
927, 928, 929, 930, 931, 932,
};
private static ReadOnlySpan<ushort> Pouch_ZCrystalHeld_USUM => new ushort[]
{
// SM
776, 777, 778, 779, 780, 781, 782, 783, 784, 785, 786, 787, 788, 789, 790, 791, 792, 793, 794, 798, 799, 800, 801, 802, 803, 804, 805, 806, 836,
// US/UM Additions
921, 922, 923, 924, 925, 926,
};
public static ushort[] GetAllHeld() => ArrayUtil.ConcatAll(ItemStorage7SM.Pouch_Regular_SM, ItemStorage7SM.Pouch_Berries_SM, ItemStorage7SM.Pouch_Medicine_SM, Pouch_ZCrystalHeld_USUM, Pouch_Roto_USUM);
public static bool GetCrystalHeld(ushort itemKey, out ushort itemHeld)
{
var index = Pouch_ZCrystal_USUM.IndexOf(itemKey);
if (index < 0)
{
itemHeld = 0;
return false;
}
itemHeld = Pouch_ZCrystalHeld_USUM[index];
return true;
}
public static bool GetCrystalKey(ushort itemHeld, out ushort itemKey)
{
var index = Pouch_ZCrystalHeld_USUM.IndexOf(itemHeld);
if (index < 0)
{
itemKey = 0;
return false;
}
itemKey = Pouch_ZCrystal_USUM[index];
return true;
}
public bool IsLegal(InventoryType type, int itemIndex, int itemCount)
{
if (type is InventoryType.KeyItems or InventoryType.ZCrystals)
return true;
var items = GetItems(type);
if (items.BinarySearch((ushort)itemIndex) < 0)
return false;
return ItemStorage7SM.Unreleased.BinarySearch((ushort)itemIndex) < 0;
}
public ReadOnlySpan<ushort> GetItems(InventoryType type) => type switch
{
InventoryType.Medicine => ItemStorage7SM.Pouch_Medicine_SM,
InventoryType.Items => ItemStorage7SM.Pouch_Regular_SM,
InventoryType.TMHMs => ItemStorage7SM.Pouch_TMHM_SM,
InventoryType.Berries => ItemStorage7SM.Pouch_Berries_SM,
InventoryType.KeyItems => Pouch_Key_USUM,
InventoryType.ZCrystals => Pouch_ZCrystal_USUM,
InventoryType.BattleItems => Pouch_Roto_USUM,
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null),
};
}