From 9b55c015b6d7b71cf91e7e2b4356d318a74eee3d Mon Sep 17 00:00:00 2001 From: Kaphotics Date: Thu, 3 Mar 2016 22:12:22 -0800 Subject: [PATCH] Refactoring Fix getValidPreEvolutions not decrementing level (the evo level is the actual value, not 1 for levelup. Did some performance analysis and optimized a little; the Valid Moves are now only obtained once instead of 4 times in a loop; used a list to hold the moves instead of constantly ToArray() which is a little computation intensive. --- Legality/Analysis.cs | 7 ++- Legality/Core.cs | 124 +++++++++++++++++++------------------------ 2 files changed, 57 insertions(+), 74 deletions(-) diff --git a/Legality/Analysis.cs b/Legality/Analysis.cs index 4f932f3ab..daf24e0ed 100644 --- a/Legality/Analysis.cs +++ b/Legality/Analysis.cs @@ -27,8 +27,6 @@ public class LegalityAnalysis { public bool Valid = false; public LegalityCheck EC, Nickname, PID, IDs, IVs, EVs; - public int[] ValidMoves => Legal.getValidMoves(pk6.Species, pk6.CurrentLevel); - public bool DexNav => Legal.getDexNavValid(pk6); public string Report => getLegalityReport(); private readonly PK6 pk6; @@ -46,6 +44,7 @@ public bool[] getMoveValidity(int[] Moves, int[] RelearnMoves) if (!pk6.Gen6) return res; + int[] validMoves = Legal.getValidMoves(pk6); if (pk6.Species == 235) { for (int i = 0; i < 4; i++) @@ -54,7 +53,7 @@ public bool[] getMoveValidity(int[] Moves, int[] RelearnMoves) else { for (int i = 0; i < 4; i++) - res[i] = Moves[i] != Legal.Struggle && ValidMoves.Concat(RelearnMoves).Contains(Moves[i]); + res[i] = Moves[i] != Legal.Struggle && validMoves.Concat(RelearnMoves).Contains(Moves[i]); } if (Moves[0] == 0) res[0] = false; @@ -123,7 +122,7 @@ public bool[] getRelearnValidity(int[] Moves) // Check DexNav for (int i = 0; i < 4; i++) res[i] &= Moves[i] == 0; - if (DexNav) + if (Legal.getDexNavValid(pk6)) res[0] = relearnMoves.Contains(Moves[0]); return res; diff --git a/Legality/Core.cs b/Legality/Core.cs index a68cca144..798fffac6 100644 --- a/Legality/Core.cs +++ b/Legality/Core.cs @@ -37,60 +37,36 @@ internal static bool getDexNavValid(PK6 pk6) bool alpha = pk6.Version == 26; if (!alpha && pk6.Version != 27) return false; - EncounterArea[] locs = (alpha ? DexNavA : DexNavO).Where(l => l.Location == pk6.Met_Location).ToArray(); - return locs.Select(loc => getValidEncounterSlots(pk6, loc)).Any(slots => slots.Length > 0); + IEnumerable locs = (alpha ? DexNavA : DexNavO).Where(l => l.Location == pk6.Met_Location); + return locs.Select(loc => getValidEncounterSlots(pk6, loc)).Any(slots => slots.Any()); } - - internal static EncounterSlot[] getValidEncounterSlots(PK6 pk6, EncounterArea loc) + internal static int[] getValidMoves(PK6 pk6) { - // Get Valid levels - DexLevel[] vs = getValidPreEvolutions(pk6); - // Get slots where pokemon can exist - EncounterSlot[] slots = loc.Slots.Where(slot => vs.Any(evo => evo.Species == slot.Species && evo.Level >= slot.LevelMin)).ToArray(); - - // Filter for Form Specific - if (WildForms.Contains(pk6.Species)) - slots = slots.Where(slot => slot.Form == pk6.AltForm).ToArray(); - return slots; - } - internal static DexLevel[] getValidPreEvolutions(PK6 pk6) - { - var evos = Evolves[pk6.Species].Evos; - int dec = 0; - List dl = new List {new DexLevel {Species = pk6.Species, Level = pk6.CurrentLevel}}; - foreach (DexLevel evo in evos) + List r = new List {0}; + + r.AddRange(getLVLMoves(pk6.Species, pk6.CurrentLevel)); + r.AddRange(getTutorMoves(pk6.Species)); + r.AddRange(getMachineMoves(pk6.Species)); + IEnumerable vs = getValidPreEvolutions(pk6); + foreach (DexLevel evo in vs) { - if (evo.Level == 1) // Level Up (from previous level) - dec++; - int lvl = pk6.CurrentLevel - dec; - if (lvl >= pk6.Met_Level && lvl > evo.Level) - dl.Add(new DexLevel {Species = evo.Species, Level = lvl}); - } - return dl.ToArray(); - } - - internal static int[] getValidMoves(int species, int level) - { - int[] r = new int[1]; - - // r = r.Concat(getEggMoves(species)).ToArray(); - r = r.Concat(getLVLMoves(species, level)).ToArray(); - r = r.Concat(getTutorMoves(species)).ToArray(); - r = r.Concat(getMachineMoves(species)).ToArray(); - var e = Evolves[species].Evos; - int dec = 0; - foreach (DexLevel evo in e) - { - if (evo.Level == 1) // In order to level up evolve, the list of available moves is for one level previous. - dec++; - r = r.Concat(getLVLMoves(evo.Species, level - dec)).ToArray(); - r = r.Concat(getTutorMoves(evo.Species)).ToArray(); - r = r.Concat(getMachineMoves(evo.Species)).ToArray(); + r.AddRange(getLVLMoves(evo.Species, evo.Level)); + r.AddRange(getTutorMoves(evo.Species)); + r.AddRange(getMachineMoves(evo.Species)); } return r.Distinct().ToArray(); } + internal static int[] getValidRelearn(PK6 pk6, int skipOption) + { + List r = new List { 0 }; + int species = getBaseSpecies(pk6, skipOption); + r.AddRange(getLVLMoves(species, 1)); + r.AddRange(getEggMoves(species)); + r.AddRange(getLVLMoves(species, 100)); + return r.Distinct().ToArray(); + } - internal static int getBaseSpecies(PK6 pk6, int skipOption) + private static int getBaseSpecies(PK6 pk6, int skipOption) { DexLevel[] evos = Evolves[pk6.Species].Evos; switch (skipOption) @@ -100,34 +76,42 @@ internal static int getBaseSpecies(PK6 pk6, int skipOption) default: return evos.Length <= 0 ? pk6.Species : evos.Last().Species; } } + private static IEnumerable getValidEncounterSlots(PK6 pk6, EncounterArea loc) + { + // Get Valid levels + IEnumerable vs = getValidPreEvolutions(pk6); + // Get slots where pokemon can exist + IEnumerable slots = loc.Slots.Where(slot => vs.Any(evo => evo.Species == slot.Species && evo.Level >= slot.LevelMin)); - internal static int[] getDexNavRelearn(PK6 pk6, int skipOption) - { - int species = getBaseSpecies(pk6, skipOption); - - var moves = new int[1]; - moves = moves.Concat(getEggMoves(species)).ToArray(); - return moves.Distinct().ToArray(); + // Filter for Form Specific + if (WildForms.Contains(pk6.Species)) + slots = slots.Where(slot => slot.Form == pk6.AltForm); + return slots; } - internal static int[] getValidRelearn(PK6 pk6, int skipOption) + private static IEnumerable getValidPreEvolutions(PK6 pk6) { - int[] moves = new int[1]; - int species = getBaseSpecies(pk6, skipOption); - moves = moves.Concat(getLVLMoves(species, 1)).ToArray(); - moves = moves.Concat(getEggMoves(species)).ToArray(); - moves = moves.Concat(getLVLMoves(species, 100)).ToArray(); - return moves.Distinct().ToArray(); + var evos = Evolves[pk6.Species].Evos; + int dec = 0; + List dl = new List { new DexLevel { Species = pk6.Species, Level = pk6.CurrentLevel } }; + foreach (DexLevel evo in evos) + { + if (evo.Level > 0) // Level Up (from previous level) + dec++; + int lvl = pk6.CurrentLevel - dec; + if (lvl >= pk6.Met_Level && lvl > evo.Level) + dl.Add(new DexLevel { Species = evo.Species, Level = lvl }); + } + return dl; } - - private static int[] getEggMoves(int species) + private static IEnumerable getEggMoves(int species) { - return EggMoveAO[species].Moves.Concat(EggMoveXY[species].Moves).ToArray(); + return EggMoveAO[species].Moves.Concat(EggMoveXY[species].Moves); } - private static int[] getLVLMoves(int species, int lvl) + private static IEnumerable getLVLMoves(int species, int lvl) { - return LevelUpXY[species].getMoves(lvl).Concat(LevelUpAO[species].getMoves(lvl)).ToArray(); + return LevelUpXY[species].getMoves(lvl).Concat(LevelUpAO[species].getMoves(lvl)); } - private static int[] getTutorMoves(int species) + private static IEnumerable getTutorMoves(int species) { PersonalInfo pkAO = PersonalAO[species]; @@ -140,16 +124,16 @@ private static int[] getTutorMoves(int species) if (pkAO.ORASTutors[i][b]) moves.Add(Tutors_AO[i][b]); - return moves.ToArray(); + return moves; } - private static int[] getMachineMoves(int species) + private static IEnumerable getMachineMoves(int species) { PersonalInfo pkXY = PersonalXY[species]; PersonalInfo pkAO = PersonalAO[species]; List moves = new List(); moves.AddRange(TMHM_XY.Where((t, i) => pkXY.TMHM[i])); moves.AddRange(TMHM_AO.Where((t, i) => pkAO.TMHM[i])); - return moves.ToArray(); + return moves; } } }