From ed40cdae644015a1927fcc1caaf0b35f49373303 Mon Sep 17 00:00:00 2001 From: Kurt Date: Sat, 25 Mar 2017 13:53:33 -0700 Subject: [PATCH] Misc tweaks int[][] to List to reduce ToArray() overhead as adding range is quicker remove multiple use of ienumerable by calling tolist() --- PKHeX/Legality/Analysis.cs | 18 ++--- PKHeX/Legality/Checks.cs | 79 +++++++------------ PKHeX/Legality/Core.cs | 56 +++++++------ PKHeX/Legality/LegalityCheckStrings.cs | 2 +- .../text/en/LegalityCheckStrings_en.txt | 2 +- 5 files changed, 70 insertions(+), 87 deletions(-) diff --git a/PKHeX/Legality/Analysis.cs b/PKHeX/Legality/Analysis.cs index 951038255..30a508cdc 100644 --- a/PKHeX/Legality/Analysis.cs +++ b/PKHeX/Legality/Analysis.cs @@ -417,19 +417,17 @@ private static int[] getGenMovesCheckOrder(PKM pkm) { if (pkm.Format == 1) return new [] { 1, 2 }; - else if (pkm.Format == 2) + if (pkm.Format == 2) return new [] { 2, 1 }; - else if (pkm.Format == 7 && pkm.VC1) + if (pkm.Format == 7 && pkm.VC1) return new [] { 7, 1 }; - else if (pkm.Format == 7 && pkm.VC2) + if (pkm.Format == 7 && pkm.VC2) return new [] { 7, 2, 1 }; - else - { - var order = new int[pkm.Format - pkm.GenNumber + 1]; - for (int i = 0; i < order.Length; i++) - order[i] = pkm.Format - i; - return order; - } + + var order = new int[pkm.Format - pkm.GenNumber + 1]; + for (int i = 0; i < order.Length; i++) + order[i] = pkm.Format - i; + return order; } } } diff --git a/PKHeX/Legality/Checks.cs b/PKHeX/Legality/Checks.cs index cf2a3e392..d73dbebaf 100644 --- a/PKHeX/Legality/Checks.cs +++ b/PKHeX/Legality/Checks.cs @@ -2086,7 +2086,7 @@ private CheckResult[] verifyMoves(GameVersion game = GameVersion.Any) return res; } - private CheckResult[] verifyMovesEggPreRelearn(int[] Moves, int[][] validLevelMoves, int[][] validTMHM, int[][] validTutor) + private CheckResult[] verifyMovesEggPreRelearn(int[] Moves, List[] validLevelMoves, List[] validTMHM, List[] validTutor) { CheckResult[] res = new CheckResult[4]; @@ -2127,7 +2127,7 @@ private CheckResult[] parseMovesSketch(int[] Moves) : new CheckResult(CheckIdentifier.Move); return res; } - private CheckResult[] parseMovesGetGift(int[] Moves, int[][] validLevelMoves, int[][] validTMHM, int[][] validTutor) + private CheckResult[] parseMovesGetGift(int[] Moves, List[] validLevelMoves, List[] validTMHM, List[] validTutor) { int[] RelearnMoves = pkm.RelearnMoves; foreach (MysteryGift mg in EventGiftMatch) @@ -2146,7 +2146,7 @@ private CheckResult[] parseMovesGetGift(int[] Moves, int[][] validLevelMoves, in // no Mystery Gifts matched return parseMoves(Moves, validLevelMoves, RelearnMoves, validTMHM, validTutor, new int[0], new int[0], new int[0], new int[0]); } - private CheckResult[] parseMovesRegular(int[] Moves, int[][] validLevelMoves, int[][] validTMHM, int[][] validTutor, int[] baseEggMoves, GameVersion game) + private CheckResult[] parseMovesRegular(int[] Moves, List[] validLevelMoves, List[] validTMHM, List[] validTutor, int[] baseEggMoves, GameVersion game) { int[] EggMoves = pkm.WasEgg ? Legal.getEggMoves(pkm, game).ToArray() : new int[0]; int[] EventEggMoves = pkm.WasEgg ? Legal.getSpecialEggMoves(pkm, game).ToArray() : new int[0]; @@ -2167,7 +2167,7 @@ private CheckResult[] parseMovesRegular(int[] Moves, int[][] validLevelMoves, in return res; } - private CheckResult[] parseMoves(int[] moves, int[][] learn, int[] relearn, int[][] tmhm, int[][] tutor, int[] special, int[] baseegg, int[] egg, int[] eventegg) + private CheckResult[] parseMoves(int[] moves, List[] learn, int[] relearn, List[] tmhm, List[] tutor, int[] special, int[] baseegg, int[] egg, int[] eventegg) { CheckResult[] res = new CheckResult[4]; var Gen1MovesLearned = new List(); @@ -2186,12 +2186,11 @@ private CheckResult[] parseMoves(int[] moves, int[][] learn, int[] relearn, int[ if (res.All(r => r != null)) return res; - bool MixedGen1NonTradebackGen2= false; + bool MixedGen1NonTradebackGen2 = false; // Check moves going backwards, marking the move valid in the most current generation when it can be learned int[] generations = getGenMovesCheckOrder(pkm); - for (int g = 0; g < generations.Length; g++) + foreach (var gen in generations) { - var gen = generations[g]; if (!pkm.InhabitedGeneration(gen)) continue; @@ -2208,36 +2207,27 @@ private CheckResult[] parseMoves(int[] moves, int[][] learn, int[] relearn, int[ else if (gen == 3 && pkm.Format > 3) HMLearned = moves.Select((m, i) => i).Where(i => !(res[i]?.Valid ?? false) && Legal.HM_3.Any(l => l == moves[i])).ToArray(); + bool native = gen == pkm.Format; for (int m = 0; m < 4; m++) { if (res[m]?.Valid ?? false) continue; if (learn[gen].Contains(moves[m])) - { - res[m] = new CheckResult(Severity.Valid, (gen == pkm.Format)? V177 : String.Format(V330, gen), CheckIdentifier.Move); - if (gen == 1) Gen1MovesLearned.Add(m); - } + res[m] = new CheckResult(Severity.Valid, native ? V177 : string.Format(V330, gen), CheckIdentifier.Move); else if (tmhm[gen].Contains(moves[m])) - { - res[m] = new CheckResult(Severity.Valid, (gen == pkm.Format) ? V173 : String.Format(V331, gen), CheckIdentifier.Move); - if (gen == 1) Gen1MovesLearned.Add(m); - } + res[m] = new CheckResult(Severity.Valid, native ? V173 : string.Format(V331, gen), CheckIdentifier.Move); else if (tutor[gen].Contains(moves[m])) - { - res[m] = new CheckResult(Severity.Valid, (gen == pkm.Format) ? V173 : String.Format(V332, gen), CheckIdentifier.Move); - if (gen == 1) Gen1MovesLearned.Add(m); - } + res[m] = new CheckResult(Severity.Valid, native ? V173 : string.Format(V332, gen), CheckIdentifier.Move); else if (gen == pkm.GenNumber && special.Contains(moves[m])) - { res[m] = new CheckResult(Severity.Valid, V175, CheckIdentifier.Move); - if (gen == 1) Gen1MovesLearned.Add(m); - } else if (gen == pkm.GenNumber && baseegg.Contains(moves[m])) - { res[m] = new CheckResult(Severity.Valid, V177, CheckIdentifier.Move); - if (gen == 1) Gen1MovesLearned.Add(m); - } + + if (res[m] == null) + continue; + if (res[m].Valid && gen == 1) + Gen1MovesLearned.Add(m); } if (gen == generations.Last()) @@ -2261,20 +2251,20 @@ private CheckResult[] parseMoves(int[] moves, int[][] learn, int[] relearn, int[ res[m] = new CheckResult(Severity.Valid, V171, CheckIdentifier.Move) { Flag = true }; EggMovesLearned.Add(m); } - if (eventegg.Contains(moves[m])) + if (!eventegg.Contains(moves[m])) + continue; + + if (!egg.Contains(moves[m])) { - if(!egg.Contains(moves[m])) + if (IsGen2Pkm && Gen1MovesLearned.Any() && moves[m] > Legal.MaxMoveID_1) { - if (IsGen2Pkm && Gen1MovesLearned.Any() && moves[m] > Legal.MaxMoveID_1) - { - res[m] = new CheckResult(Severity.Invalid, V334, CheckIdentifier.Move) { Flag = true }; - MixedGen1NonTradebackGen2 = true; - } - else - res[m] = new CheckResult(Severity.Valid, V33, CheckIdentifier.Move) { Flag = true }; + res[m] = new CheckResult(Severity.Invalid, V334, CheckIdentifier.Move) { Flag = true }; + MixedGen1NonTradebackGen2 = true; } - EventEggMovesLearned.Add(m); + else + res[m] = new CheckResult(Severity.Valid, V33, CheckIdentifier.Move) { Flag = true }; } + EventEggMovesLearned.Add(m); } // A pokemon could have normal egg moves and regular egg moves @@ -2282,7 +2272,7 @@ private CheckResult[] parseMoves(int[] moves, int[][] learn, int[] relearn, int[ if (EggMovesLearned.Any() && EventEggMovesLearned.Any()) { // Moves that are egg moves or event egg moves but not both - var IncompatibleEggMoves = EggMovesLearned.Except(EventEggMovesLearned).Union(EventEggMovesLearned.Except(EggMovesLearned)); + var IncompatibleEggMoves = EggMovesLearned.Except(EventEggMovesLearned).Union(EventEggMovesLearned.Except(EggMovesLearned)).ToList(); if (IncompatibleEggMoves.Any()) { foreach(int m in IncompatibleEggMoves) @@ -2306,35 +2296,22 @@ private CheckResult[] parseMoves(int[] moves, int[][] learn, int[] relearn, int[ KnowDefogWhirlpool = moves.Where((m,i) => (m == 250 || m == 432) && (res[i]?.Valid ?? false)).Count() == 2; if (KnowDefogWhirlpool) - { - foreach (int index in moves.Select((m, i) => i).Where(i => (moves[i] == 250 || moves[i] == 432))) - { + foreach (int index in moves.Select((m, i) => i).Where(i => moves[i] == 250 || moves[i] == 432)) res[index] = new CheckResult(Severity.Invalid, V338, CheckIdentifier.Move); - } - } foreach (int hm in HMLearned.Where(l => res[l]?.Valid ?? false)) - { - res[hm] = new CheckResult(Severity.Invalid, string.Format(V339,gen,gen+1), CheckIdentifier.Move); - } + res[hm] = new CheckResult(Severity.Invalid, string.Format(V339, gen, gen + 1), CheckIdentifier.Move); } // Mark the gen 1 exclusive moves as illegal because the pokemon also have Non tradeback egg moves. if (MixedGen1NonTradebackGen2) - { foreach (int m in Gen1MovesLearned) - { res[m] = new CheckResult(Severity.Invalid, V335, CheckIdentifier.Move); - } - } if (res.All(r => r != null)) return res; } - if (res.All(r => r != null)) - return res; - for (int m = 0; m < 4; m++) { if (res[m] == null) diff --git a/PKHeX/Legality/Core.cs b/PKHeX/Legality/Core.cs index 5e12e13ef..f807dd8a4 100644 --- a/PKHeX/Legality/Core.cs +++ b/PKHeX/Legality/Core.cs @@ -569,54 +569,62 @@ private static EncounterArea[] getTables2(GameVersion Version) } // Moves - internal static void RemoveFutureMoves(PKM pkm, DexLevel[][] evoChains, ref int[][] validLevelMoves, ref int[][] validTMHM, ref int[][] validTutor) + internal static void RemoveFutureMoves(PKM pkm, DexLevel[][] evoChains, ref List[] validLevelMoves, ref List[] validTMHM, ref List[] validTutor) { if (pkm.Format >= 3) { - var FutureMoves = validLevelMoves[pkm.Format].Concat(validTMHM[pkm.Format]).Concat(validTutor[pkm.Format]); - if(pkm.VC1) + var FutureMoves = new List(); + FutureMoves.AddRange(validLevelMoves[pkm.Format]); + FutureMoves.AddRange(validTMHM[pkm.Format]); + FutureMoves.AddRange(validTutor[pkm.Format]); + if (pkm.VC1) { - validLevelMoves[1] = validLevelMoves[1].Except(FutureMoves).ToArray(); - validTMHM[1] = validTMHM[1].Except(FutureMoves).ToArray(); - validTutor[1] = validTutor[1].Except(FutureMoves).ToArray(); + validLevelMoves[1]?.RemoveAll(x => FutureMoves.Contains(x)); + validTMHM[1]?.RemoveAll(x => FutureMoves.Contains(x)); + validTutor[1]?.RemoveAll(x => FutureMoves.Contains(x)); } else if (pkm.VC2) { for (int i = 2; i >= 1; i--) { - validLevelMoves[i] = validLevelMoves[i]?.Except(FutureMoves).ToArray(); - validTMHM[i] = validTMHM[i]?.Except(FutureMoves).ToArray(); - validTutor[i] = validTutor[i]?.Except(FutureMoves).ToArray(); - if(validLevelMoves[i]!=null && validTMHM[i]!= null && validTutor[i]!= null) - FutureMoves = FutureMoves.Concat(validLevelMoves[i]).Concat(validTMHM[i]).Concat(validTutor[i]); + validLevelMoves[i]?.RemoveAll(x => FutureMoves.Contains(x)); + validTMHM[i]?.RemoveAll(x => FutureMoves.Contains(x)); + validTutor[i]?.RemoveAll(x => FutureMoves.Contains(x)); + + if (validLevelMoves[i] == null || validTMHM[i] == null || validTutor[i] == null) + continue; + FutureMoves.AddRange(validLevelMoves[i].Concat(validTMHM[i]).Concat(validTutor[i])); } } else { for (int i = pkm.Format - 1; i >= pkm.GenNumber; i--) { - validLevelMoves[i] = validLevelMoves[i].Except(FutureMoves).ToArray(); - validTMHM[i] = validTMHM[i].Except(FutureMoves).ToArray(); - validTutor[i] = validTutor[i].Except(FutureMoves).ToArray(); - FutureMoves = FutureMoves.Concat(validLevelMoves[i]).Concat(validTMHM[i]).Concat(validTutor[i]); + validLevelMoves[i]?.RemoveAll(x => FutureMoves.Contains(x)); + validTMHM[i]?.RemoveAll(x => FutureMoves.Contains(x)); + validTutor[i]?.RemoveAll(x => FutureMoves.Contains(x)); + + if (validLevelMoves[i] == null || validTMHM[i] == null || validTutor[i] == null) + continue; + FutureMoves.AddRange(validLevelMoves[i].Concat(validTMHM[i]).Concat(validTutor[i])); } } } else { - int tradeback = (pkm.Format == 2) ? 1 : 2; - var formatmoves = validLevelMoves[pkm.Format].Concat(validTMHM[pkm.Format]).Concat(validTutor[pkm.Format]); - validLevelMoves[tradeback] = validLevelMoves[tradeback]?.Except(formatmoves).ToArray(); - validTMHM[tradeback] = validTMHM[tradeback]?.Except(formatmoves).ToArray(); - validTutor[tradeback] = validTutor[tradeback]?.Except(formatmoves).ToArray(); + int tradeback = pkm.Format == 2 ? 1 : 2; + var formatmoves = validLevelMoves[pkm.Format].Concat(validTMHM[pkm.Format]).Concat(validTutor[pkm.Format]).ToList(); + validLevelMoves[tradeback]?.RemoveAll(x => formatmoves.Contains(x)); + validTMHM[tradeback]?.RemoveAll(x => formatmoves.Contains(x)); + validTutor[tradeback]?.RemoveAll(x => formatmoves.Contains(x)); } } - internal static int[][] getValidMovesAllGens(PKM pkm, DexLevel[][] evoChains, bool LVL = true, bool Tutor = true, bool Machine = true, bool MoveReminder = true, bool RemoveTransferHM = true) + internal static List[] getValidMovesAllGens(PKM pkm, DexLevel[][] evoChains, bool LVL = true, bool Tutor = true, bool Machine = true, bool MoveReminder = true, bool RemoveTransferHM = true) { - int[][] Moves = new int[evoChains.Length][]; + List[] Moves = new List[evoChains.Length]; for (int i = 1; i < evoChains.Length; i++) if (evoChains[i].Any()) - Moves[i] = getValidMoves(pkm, evoChains[i], i, LVL, Tutor, Machine, MoveReminder, RemoveTransferHM).ToArray(); + Moves[i] = getValidMoves(pkm, evoChains[i], i, LVL, Tutor, Machine, MoveReminder, RemoveTransferHM).ToList(); return Moves; } internal static IEnumerable getValidMoves(PKM pkm, DexLevel[][] evoChains, bool LVL = true, bool Tutor = true, bool Machine = true, bool MoveReminder = true, bool RemoveTransferHM = true) @@ -2432,7 +2440,7 @@ private static IEnumerable getSpecialEggMoves(PKM pkm, int species, int alf { case 3: { - var boxencounter = Encounter_Box.Where(e => e.Species == species).FirstOrDefault(); + var boxencounter = Encounter_Box.FirstOrDefault(e => e.Species == species); if (boxencounter != null) return boxencounter.Moves; break; diff --git a/PKHeX/Legality/LegalityCheckStrings.cs b/PKHeX/Legality/LegalityCheckStrings.cs index 4f95d77e7..432d9ca0c 100644 --- a/PKHeX/Legality/LegalityCheckStrings.cs +++ b/PKHeX/Legality/LegalityCheckStrings.cs @@ -100,7 +100,7 @@ public static class LegalityCheckStrings public static string V31 {get; set;} = "All IVs are 0."; // Fishy public static string V32 {get; set;} = "All IVs are equal."; // Fishy - public static string V28 {get; set;} = "Should have at least {0} IVs {get; set;} = 31."; // Invalid + public static string V28 {get; set;} = "Should have at least {0} IVs = 31."; // Invalid public static string V29 {get; set;} = "Friend Safari captures should have at least 2 IVs = 31."; // Invalid public static string V30 {get; set;} = "IVs do not match Mystery Gift Data."; // Invalid diff --git a/PKHeX/Resources/text/en/LegalityCheckStrings_en.txt b/PKHeX/Resources/text/en/LegalityCheckStrings_en.txt index d931a0c9c..7636968c4 100644 --- a/PKHeX/Resources/text/en/LegalityCheckStrings_en.txt +++ b/PKHeX/Resources/text/en/LegalityCheckStrings_en.txt @@ -62,7 +62,7 @@ V26 = EVs cannot go above 252. V27 = EVs are all equal. V31 = All IVs are 0. V32 = All IVs are equal. -V28 = Should have at least {0} IVs {get; set;} = 31. +V28 = Should have at least {0} IVs = 31. V29 = Friend Safari captures should have at least 2 IVs = 31. V30 = IVs do not match Mystery Gift Data. V38 = OT Name too long.