From 59b4b8f755d203aec6ef3b42e3b129ab84634961 Mon Sep 17 00:00:00 2001 From: Kurt Date: Mon, 4 Jun 2018 21:28:14 -0700 Subject: [PATCH] Simplify pkm conversion just loop instead of goto, let the jit handle optimization src looks quite pretty now! some tradeoffs made but much more maintainable now --- PKHeX.Core/PKM/PK2.cs | 7 +- PKHeX.Core/PKM/Util/PKMConverter.cs | 124 +++++++++++----------------- 2 files changed, 47 insertions(+), 84 deletions(-) diff --git a/PKHeX.Core/PKM/PK2.cs b/PKHeX.Core/PKM/PK2.cs index d1dcffdd4..3f84b6587 100644 --- a/PKHeX.Core/PKM/PK2.cs +++ b/PKHeX.Core/PKM/PK2.cs @@ -378,12 +378,7 @@ public PK1 ConvertToPK1() Array.Copy(otname, 0, pk1.otname, 0, otname.Length); Array.Copy(nick, 0, pk1.nick, 0, nick.Length); - int[] newMoves = pk1.Moves; - for (int i = 0; i < 4; i++) - if (newMoves[i] > 165) // not present in Gen 1 - newMoves[i] = 0; - pk1.Moves = newMoves; - pk1.FixMoves(); + pk1.ClearInvalidMoves(); return pk1; } diff --git a/PKHeX.Core/PKM/Util/PKMConverter.cs b/PKHeX.Core/PKM/Util/PKMConverter.cs index 69ebb6cc1..eb4ac7023 100644 --- a/PKHeX.Core/PKM/Util/PKMConverter.cs +++ b/PKHeX.Core/PKM/Util/PKMConverter.cs @@ -215,8 +215,10 @@ public static PKM ConvertToType(PKM pk, Type PKMType, out string comment) } var pkm = ConvertPKM(pk, PKMType, fromType, out comment); - if (!AllowIncompatibleConversion || pkm != null) + if (pkm?.GetType() == PKMType) return pkm; + if (!AllowIncompatibleConversion) + return null; // Try Incompatible Conversion pkm = GetBlank(PKMType); @@ -242,7 +244,7 @@ private static PKM ConvertPKM(PKM pk, Type PKMType, Type fromType, out string co return null; } - var pkm = ConvertPKM(pk, PKMType, fromType, toFormat, ref comment); + var pkm = ConvertPKM(pk, PKMType, toFormat, ref comment); comment = pkm == null ? $"Cannot convert a {fromType.Name} to a {PKMType.Name}." @@ -250,88 +252,54 @@ private static PKM ConvertPKM(PKM pk, Type PKMType, Type fromType, out string co return pkm; } - private static PKM ConvertPKM(PKM pk, Type PKMType, Type fromType, int toFormat, ref string comment) + private static PKM ConvertPKM(PKM pk, Type PKMType, int toFormat, ref string comment) { PKM pkm = pk.Clone(); if (pkm.IsEgg) pkm.ForceHatchPKM(); - - switch (fromType.Name) + while (true) { - case nameof(PK1): - if (toFormat == 7) // VC->Bank - pkm = ((PK1)pk).ConvertToPK7(); - else if (toFormat == 2) // GB<->GB - pkm = ((PK1)pk).ConvertToPK2(); - break; - case nameof(PK2): - if (toFormat == 7) // VC->Bank - pkm = ((PK2)pk).ConvertToPK7(); - else if (toFormat == 1) // GB<->GB - { - if (pk.Species > 151) - { - comment = $"Cannot convert a {PKX.GetSpeciesName(pkm.Species, pkm.Japanese ? 1 : 2)} to {PKMType.Name}"; - return pkm; - } - pkm = ((PK2)pk).ConvertToPK1(); - pkm.ClearInvalidMoves(); - } - break; - case nameof(CK3): - pkm = ((CK3)pkm).ConvertToPK3(); - goto case nameof(PK3); // fall through - case nameof(XK3): - pkm = ((XK3)pkm).ConvertToPK3(); - goto case nameof(PK3); // fall through - case nameof(PK3): - if (toFormat == 3) - { - if (PKMType == typeof(CK3)) - pkm = ((PK3)pkm).ConvertToCK3(); - else if (PKMType == typeof(XK3)) - pkm = ((PK3)pkm).ConvertToXK3(); - break; - } - - pkm = ((PK3) pkm).ConvertToPK4(); - if (toFormat == 4) - break; - goto case nameof(PK4); - case nameof(BK4): - pkm = ((BK4) pkm).ConvertToPK4(); - if (toFormat == 4) - break; - goto case nameof(PK4); - case nameof(PK4): - if (PKMType == typeof(BK4)) - { - pkm = ((PK4) pkm).ConvertToBK4(); - break; - } - pkm = ((PK4) pkm).ConvertToPK5(); - if (toFormat == 5) - break; - goto case nameof(PK5); - case nameof(PK5): - pkm = ((PK5) pkm).ConvertToPK6(); - if (toFormat == 6) - break; - goto case nameof(PK6); - case nameof(PK6): - if (pkm.Species == 25 && pkm.AltForm != 0) // cosplay pikachu - { - comment = "Cannot transfer Cosplay Pikachu forward."; - return pkm; - } - pkm = ((PK6) pkm).ConvertToPK7(); - if (toFormat == 7) - break; - goto case nameof(PK7); - case nameof(PK7): - break; + pkm = IntermediaryConvert(pkm, PKMType, toFormat, ref comment); + if (pkm == null) // fail convert + return null; + if (pkm.GetType() == PKMType) // finish convert + return pkm; + } + } + private static PKM IntermediaryConvert(PKM pk, Type PKMType, int toFormat, ref string comment) + { + switch (pk) + { + // Non-sequential + case PK1 pk1 when toFormat >= 7: return pk1.ConvertToPK7(); + case PK2 pk2 when toFormat >= 7: return pk2.ConvertToPK7(); + case PK3 pk3 when PKMType == typeof(CK3): return pk3.ConvertToCK3(); + case PK3 pk3 when PKMType == typeof(XK3): return pk3.ConvertToXK3(); + case PK4 pk4 when PKMType == typeof(BK4): return pk4.ConvertToBK4(); + + // Invalid + case PK2 pk2 when pk.Species > Legal.MaxSpeciesID_1: + var lang = pk2.Japanese ? (int)LanguageID.Japanese : (int)LanguageID.English; + var name = PKX.GetSpeciesName(pk2.Species, lang); + comment = $"Cannot convert a {name} to {PKMType.Name}"; + return null; + + // Sequential + case PK1 pk1: return pk1.ConvertToPK2(); + case PK2 pk2: return pk2.ConvertToPK1(); + case CK3 ck3: return ck3.ConvertToPK3(); + case XK3 xk3: return xk3.ConvertToPK3(); + case PK3 pk3: return pk3.ConvertToPK4(); + case BK4 bk4: return bk4.ConvertToPK4(); + case PK4 pk4: return pk4.ConvertToPK5(); + case PK5 pk5: return pk5.ConvertToPK6(); + case PK6 pk6: return pk6.ConvertToPK7(); + + // None + default: + comment = "Cannot transfer this format to the requested format."; + return null; } - return pkm; } ///