From 890ce9ea4e3a35389b01ed6cfce61983d7d648c6 Mon Sep 17 00:00:00 2001 From: Kurt Date: Tue, 12 Jan 2021 22:25:30 -0800 Subject: [PATCH] Simplify some expressions Search array for index rather than loop and check; gives early-break too -- was O(n) now is O(<~n) --- PKHeX.Core/Legality/Moves/MoveTutor.cs | 137 ++++++++++--------------- 1 file changed, 55 insertions(+), 82 deletions(-) diff --git a/PKHeX.Core/Legality/Moves/MoveTutor.cs b/PKHeX.Core/Legality/Moves/MoveTutor.cs index 1bc4258f6..b0b451d0f 100644 --- a/PKHeX.Core/Legality/Moves/MoveTutor.cs +++ b/PKHeX.Core/Legality/Moves/MoveTutor.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using static PKHeX.Core.Legal; @@ -38,12 +39,10 @@ private static GameVersion GetIsTutor2(PKM pkm, int species, int move) if (!ParseSettings.AllowGen2Crystal(pkm)) return NONE; var info = PersonalTable.C[species]; - for (int i = 0; i < Tutors_GSC.Length; i++) - { - if (Tutors_GSC[i] == move) - return info.TMHM[57 + i] ? GameVersion.C : NONE; - } - return GetIsTutor1(pkm, species, move); + var tutor = Array.IndexOf(Tutors_GSC, move); + if (tutor != -1 && info.TMHM[57 + tutor]) + return GameVersion.C; + return NONE; } private static GameVersion GetIsTutor3(int species, int move) @@ -51,26 +50,20 @@ private static GameVersion GetIsTutor3(int species, int move) // E Tutors (Free) // E Tutors (BP) var info = PersonalTable.E[species]; - for (int i = 0; i < Tutor_E.Length; i++) - { - if (Tutor_E[i] == move && info.TypeTutors[i]) - return GameVersion.E; - } + var e = Array.IndexOf(Tutor_E, move); + if (e != -1 && info.TypeTutors[e]) + return GameVersion.E; // FRLG Tutors // Only special tutor moves, normal tutor moves are already included in Emerald data - for (int i = 0; i < SpecialTutors_FRLG.Length; i++) - { - if (Tutor_FRLG[i] == move && species == SpecialTutors_Compatibility_FRLG[i]) - return GameVersion.FRLG; - } + var frlg = Array.IndexOf(SpecialTutors_FRLG, move); + if (frlg != -1 && SpecialTutors_Compatibility_FRLG[frlg] == species) + return GameVersion.FRLG; // XD - for (int i = 0; i < SpecialTutors_XD_Exclusive.Length; i++) - { - if (SpecialTutors_XD_Exclusive[i] == move && SpecialTutors_Compatibility_XD_Exclusive[i].Any(e => e == species)) - return GameVersion.XD; - } + var xd = Array.IndexOf(SpecialTutors_XD_Exclusive, move); + if (xd != -1 && SpecialTutors_Compatibility_XD_Exclusive[xd].Contains(species)) + return GameVersion.XD; // XD (Mew) if (species == (int)Species.Mew && Tutor_3Mew.Contains(move)) @@ -82,17 +75,13 @@ private static GameVersion GetIsTutor3(int species, int move) private static GameVersion GetIsTutor4(int species, int form, int move) { var pi = PersonalTable.HGSS.GetFormEntry(species, form); - for (int i = 0; i < Tutors_4.Length; i++) - { - if (Tutors_4[i] == move && pi.TypeTutors[i]) - return GameVersion.Gen4; - } + var type = Array.IndexOf(Tutors_4, move); + if (type != -1 && pi.TypeTutors[type]) + return GameVersion.Gen4; - for (int i = 0; i < SpecialTutors_4.Length; i++) - { - if (SpecialTutors_4[i] == move && SpecialTutors_Compatibility_4[i].Any(e => e == species)) - return GameVersion.HGSS; - } + var special = Array.IndexOf(SpecialTutors_4, move); + if (special != -1 && SpecialTutors_Compatibility_4[special].Contains(species)) + return GameVersion.HGSS; return NONE; } @@ -100,23 +89,21 @@ private static GameVersion GetIsTutor4(int species, int form, int move) private static GameVersion GetIsTutor5(PKM pkm, int species, int form, bool specialTutors, int move) { var pi = PersonalTable.B2W2.GetFormEntry(species, form); - var arr = TypeTutor6; - for (int i = 0; i < arr.Length; i++) - { - if (arr[i] == move && pi.TypeTutors[i]) + var type = Array.IndexOf(TypeTutor6, move); + if (type != -1 && pi.TypeTutors[type]) return GameVersion.Gen5; - } if (specialTutors && pkm.HasVisitedB2W2(species)) { var tutors = Tutors_B2W2; for (int i = 0; i < tutors.Length; i++) { - for (int j = 0; j < tutors[i].Length; j++) - { - if (tutors[i][j] == move && pi.SpecialTutors[i][j]) - return GameVersion.B2W2; - } + var tutor = Array.IndexOf(tutors[i], move); + if (tutor == -1) + continue; + if (pi.SpecialTutors[i][tutor]) + return GameVersion.B2W2; + break; } } @@ -126,23 +113,21 @@ private static GameVersion GetIsTutor5(PKM pkm, int species, int form, bool spec private static GameVersion GetIsTutor6(PKM pkm, int species, int form, bool specialTutors, int move) { var pi = PersonalTable.AO.GetFormEntry(species, form); - var arr = TypeTutor6; - for (int i = 0; i < arr.Length; i++) - { - if (arr[i] == move && pi.TypeTutors[i]) - return GameVersion.Gen6; - } + var type = Array.IndexOf(TypeTutor6, move); + if (type != -1 && pi.TypeTutors[type]) + return GameVersion.Gen6; if (specialTutors && pkm.HasVisitedORAS(species)) { var tutors = Tutors_AO; for (int i = 0; i < tutors.Length; i++) { - for (int j = 0; j < tutors[i].Length; j++) - { - if (tutors[i][j] == move && pi.SpecialTutors[i][j]) - return GameVersion.ORAS; - } + var tutor = Array.IndexOf(tutors[i], move); + if (tutor == -1) + continue; + if (pi.SpecialTutors[i][tutor]) + return GameVersion.ORAS; + break; } } @@ -152,21 +137,15 @@ private static GameVersion GetIsTutor6(PKM pkm, int species, int form, bool spec private static GameVersion GetIsTutor7(PKM pkm, int species, int form, bool specialTutors, int move) { var pi = PersonalTable.USUM.GetFormEntry(species, form); - var arr = TypeTutor6; - for (int i = 0; i < arr.Length; i++) - { - if (arr[i] == move && pi.TypeTutors[i]) - return GameVersion.Gen7; - } + var type = Array.IndexOf(TypeTutor6, move); + if (type != -1 && pi.TypeTutors[type]) + return GameVersion.Gen7; if (specialTutors && pkm.HasVisitedUSUM(species)) { - var tutors = Tutors_USUM; - for (int i = 0; i < tutors.Length; i++) - { - if (tutors[i] == move && pi.SpecialTutors[0][i]) - return GameVersion.USUM; - } + var tutor = Array.IndexOf(Tutors_USUM, move); + if (tutor != -1 && pi.SpecialTutors[0][tutor]) + return GameVersion.USUM; } return NONE; @@ -175,22 +154,16 @@ private static GameVersion GetIsTutor7(PKM pkm, int species, int form, bool spec private static GameVersion GetIsTutor8(PKM pkm, int species, int form, bool specialTutors, int move) { var pi = (PersonalInfoSWSH)PersonalTable.SWSH.GetFormEntry(species, form); - var arr = TypeTutor8; - for (int i = 0; i < arr.Length; i++) - { - if (arr[i] == move && pi.TypeTutors[i]) - return GameVersion.Gen8; - } + var type = Array.IndexOf(TypeTutor8, move); + if (type != -1 && pi.TypeTutors[type]) + return GameVersion.Gen8; if (!specialTutors) return NONE; - var tutors = Tutors_SWSH_1; - for (int i = 0; i < tutors.Length; i++) - { - if (tutors[i] == move && pi.SpecialTutors[0][i]) - return GameVersion.USUM; - } + var tutor = Array.IndexOf(Tutors_SWSH_1, move); + if (tutor != -1 && pi.SpecialTutors[0][tutor]) + return GameVersion.USUM; return NONE; } @@ -322,25 +295,25 @@ internal static void AddSpecialTutorMoves(List r, PKM pkm, int Generation, break; case (int)Species.Pikachu or (int)Species.Raichu when Generation == 7 && !pkm.GG: - r.Add(344); // Volt Tackle + r.Add((int)Move.VoltTackle); break; } } /// Rotom Moves that correspond to a specific form (form-0 ignored). - private static readonly int[] RotomMoves = { 315, 056, 059, 403, 437 }; + private static readonly int[] RotomMoves = { (int)Move.Overheat, (int)Move.HydroPump, (int)Move.Blizzard, (int)Move.AirSlash, (int)Move.LeafStorm }; - internal static void AddSpecialFormChangeMoves(List r, PKM pkm, int Generation, int species) + internal static void AddSpecialFormChangeMoves(List r, PKM pkm, int generation, int species) { switch (species) { - case (int)Species.Rotom when Generation >= 4: + case (int)Species.Rotom when generation >= 4: var formMoves = RotomMoves; var form = pkm.Form - 1; if ((uint)form < formMoves.Length) r.Add(RotomMoves[form]); break; - case (int)Species.Zygarde when Generation == 7: + case (int)Species.Zygarde when generation == 7: r.AddRange(ZygardeMoves); break; case (int)Species.Necrozma when pkm.Form == 1: // Sun