diff --git a/PKHeX.Core/PKM/Editing/CommonEdits.cs b/PKHeX.Core/PKM/Editing/CommonEdits.cs index b0ca9cd8c..78a769a81 100644 --- a/PKHeX.Core/PKM/Editing/CommonEdits.cs +++ b/PKHeX.Core/PKM/Editing/CommonEdits.cs @@ -60,6 +60,8 @@ public static void SetAltForm(this PKM pk, int form) /// Desired to set. public static void SetAbility(this PKM pk, int abil) { + if (abil < 0) + return; var abilities = pk.PersonalInfo.Abilities; int abilIndex = Array.IndexOf(abilities, abil); abilIndex = Math.Max(0, abilIndex); @@ -309,7 +311,7 @@ public static void ApplySetDetails(this PKM pk, ShowdownSet Set) { pk.Species = Set.Species; pk.Moves = Set.Moves; - pk.HeldItem = Set.HeldItem < 0 ? 0 : Set.HeldItem; + pk.ApplyHeldItem(Set.HeldItem, Set.Format); pk.CurrentLevel = Set.Level; pk.CurrentFriendship = Set.Friendship; pk.IVs = Set.IVs; @@ -334,6 +336,54 @@ public static void ApplySetDetails(this PKM pk, ShowdownSet Set) pk.RefreshChecksum(); } + /// + /// Sets the value depending on the current format and the provided item index & format. + /// + /// Pokémon to modify. + /// Held Item to apply + /// Format required for importing + public static void ApplyHeldItem(this PKM pk, int item, int format) + { + if (item <= 0) + { + pk.HeldItem = 0; + return; + } + + if (format <= 3 && pk.Format != format) + { + if (pk.Format > 3) // try remapping + { + item = format == 2 ? ItemConverter.GetG4Item((byte) item) : ItemConverter.GetG4Item((ushort) item); + pk.ApplyHeldItem(item, pk.Format); + return; + } + + if (pk.Format > format) // can't set past gen items + { + pk.HeldItem = 0; + return; + } + + // ShowdownSet checks gen3 then gen2. For gen2 collisions (if any?) remap 3->4->2. + item = ItemConverter.GetG4Item((ushort) item); + item = ItemConverter.GetG2Item((ushort) item); + if (item == 0 || item < 0) + { + pk.HeldItem = 0; + return; + } + } + + switch (pk.Format) + { + case 3: pk.HeldItem = ItemConverter.GetG3Item((ushort)item); break; + case 2: pk.HeldItem = ItemConverter.GetG2Item((ushort)item); break; + case 1: pk.HeldItem = 0; break; + default: pk.HeldItem = item; break; + } + } + /// /// Sets all Memory related data to the default value (zero). /// diff --git a/PKHeX.Core/PKM/ItemConverter.cs b/PKHeX.Core/PKM/ItemConverter.cs index e9c1b7fae..dafb415c0 100644 --- a/PKHeX.Core/PKM/ItemConverter.cs +++ b/PKHeX.Core/PKM/ItemConverter.cs @@ -1,4 +1,6 @@ -namespace PKHeX.Core +using System; + +namespace PKHeX.Core { /// /// Logic for converting Item IDs between the generation specific value sets. @@ -29,6 +31,32 @@ internal static class ItemConverter /// Generation 4+ Item ID. public static ushort GetG4Item(byte g2val) => g2val > arr2.Length ? NaN : arr2[g2val]; + /// + /// Converts a Generation 4+ Item ID to Generation 3 Item ID. + /// + /// Generation 4+ Item ID. + /// Generation 3 Item ID. + public static ushort GetG3Item(ushort g4val) + { + if (g4val == NaN) + return 0; + int index = Array.IndexOf(arr3, g4val); + return (ushort)Math.Max(0, index); + } + + /// + /// Converts a Generation 4+ Item ID to Generation 2 Item ID. + /// + /// Generation 4+ Item ID. + /// Generation 2 Item ID. + public static byte GetG2Item(ushort g4val) + { + if (g4val == NaN) + return 0; + int index = Array.IndexOf(arr2, g4val); + return (byte)Math.Max(0, index); + } + #region Item Mapping Tables /// Gen2 items (index) and their corresponding Gen4 item ID (value) private static readonly ushort[] arr2 = diff --git a/PKHeX.Core/PKM/PK1.cs b/PKHeX.Core/PKM/PK1.cs index c1c099e3e..7762ce1d8 100644 --- a/PKHeX.Core/PKM/PK1.cs +++ b/PKHeX.Core/PKM/PK1.cs @@ -273,7 +273,7 @@ public override ushort[] GetStats(PersonalInfo p) public override int Characteristic => -1; public override int MarkValue { get => 0; protected set { } } public override int CurrentFriendship { get => 0; set { } } - public override int Ability { get => 0; set { } } + public override int Ability { get => -1; set { } } public override int CurrentHandler { get => 0; set { } } public override int Met_Location { get => 0; set { } } public override int Egg_Location { get => 0; set { } } diff --git a/PKHeX.Core/PKM/PK2.cs b/PKHeX.Core/PKM/PK2.cs index 50fe7fbe6..fc205e07b 100644 --- a/PKHeX.Core/PKM/PK2.cs +++ b/PKHeX.Core/PKM/PK2.cs @@ -346,7 +346,7 @@ public override int HPType public override int PSV => 0xFFFF; public override int Characteristic => -1; public override int MarkValue { get => 0; protected set { } } - public override int Ability { get => 0; set { } } + public override int Ability { get => -1; set { } } public override int CurrentHandler { get => 0; set { } } public override int Egg_Location { get => 0; set { } } public override int OT_Friendship { get => 0; set { } } diff --git a/PKHeX.Core/PKM/ShowdownSet.cs b/PKHeX.Core/PKM/ShowdownSet.cs index addb38021..d12b968dc 100644 --- a/PKHeX.Core/PKM/ShowdownSet.cs +++ b/PKHeX.Core/PKM/ShowdownSet.cs @@ -14,10 +14,13 @@ public class ShowdownSet private static readonly string[] genders = {"M", "F", ""}; private static readonly string[] genderForms = {"", "F", ""}; private const string Language = "en"; + private const int LanguageID = 2; private static readonly string[] types = Util.GetTypesList(Language); private static readonly string[] forms = Util.GetFormsList(Language); private static readonly string[] species = Util.GetSpeciesList(Language); private static readonly string[] items = Util.GetItemsList(Language); + private static readonly string[] g2items = Util.GetStringList("ItemsG2", Language); + private static readonly string[] g3items = Util.GetStringList("ItemsG3", Language); private static readonly string[] natures = Util.GetNaturesList(Language); private static readonly string[] moves = Util.GetMovesList(Language); private static readonly string[] abilities = Util.GetAbilitiesList(Language); @@ -27,10 +30,11 @@ public class ShowdownSet // Default Set Data public string Nickname { get; set; } public int Species { get; private set; } = -1; + public int Format { get; private set; } = PKX.Generation; public string Form { get; private set; } public string Gender { get; private set; } public int HeldItem { get; private set; } - public int Ability { get; private set; } + public int Ability { get; private set; } = -1; public int Level { get; private set; } = 100; public bool Shiny { get; private set; } public int Friendship { get; private set; } = 255; @@ -116,12 +120,8 @@ private void ParseLines(IEnumerable lines) { string[] pieces = line.Split(new[] {" @ "}, StringSplitOptions.None); string itemstr = pieces.Last().Trim(); - int item = Array.IndexOf(items, itemstr); - if (item < 0) - InvalidLines.Add($"Unknown Item: {itemstr}"); - else - HeldItem = item; + ParseItemStr(itemstr); ParseFirstLine(pieces[0]); } else if (brokenline[0].Contains("Nature")) @@ -143,6 +143,28 @@ private void ParseLines(IEnumerable lines) } } + private void ParseItemStr(string itemstr) + { + int item = Array.IndexOf(items, itemstr); + if (item >= 0) + { + HeldItem = item; + return; + } + if ((item = Array.IndexOf(g3items, itemstr)) >= 0) + { + HeldItem = item; + Format = 3; + } + if ((item = Array.IndexOf(g2items, itemstr)) >= 0) + { + HeldItem = item; + Format = 2; + } + else + InvalidLines.Add($"Unknown Item: {itemstr}"); + } + public string Text => GetText(); private string GetText() { @@ -184,11 +206,24 @@ private string GetStringFirstLine(string form) if (!string.IsNullOrWhiteSpace(form)) specForm += $"-{form.Replace("Mega ", "Mega-")}"; - string result = Nickname != null && species[Species] != Nickname ? $"{Nickname} ({specForm})" : $"{specForm}"; + string result = Nickname != null && PKX.GetSpeciesNameGeneration(Species, LanguageID, Format) != Nickname ? $"{Nickname} ({specForm})" : $"{specForm}"; if (!string.IsNullOrEmpty(Gender)) result += $" ({Gender})"; - if (HeldItem > 0 && HeldItem < items.Length) - result += $" @ {items[HeldItem]}"; + if (HeldItem > 0) + { + switch (Format) + { + case 2: if (HeldItem < g2items.Length) + result += $" @ {g2items[HeldItem]}"; + break; + case 3: if (HeldItem < g3items.Length) + result += $" @ {g3items[HeldItem]}"; + break; + default: if (HeldItem < items.Length) + result += $" @ {items[HeldItem]}"; + break; + } + } return result; } private static bool GetStringStats(out IEnumerable result, int[] stats, int ignore) @@ -240,6 +275,7 @@ public static string GetShowdownText(PKM pkm) Shiny = pkm.IsShiny, FormIndex = pkm.AltForm, Form = pkm.AltForm > 0 && pkm.AltForm < Forms.Length ? Forms[pkm.AltForm] : string.Empty, + Format = pkm.Format, }; if (Set.Form == "F")