From 711157f3eaa4b6dd47215bc18c023d1982ef7b60 Mon Sep 17 00:00:00 2001 From: Kurt Date: Wed, 25 Jul 2018 20:40:57 -0700 Subject: [PATCH] Refactoring Extract trainer pkm origin check to interface extension --- PKHeX.Core/Legality/Core.cs | 52 ++++++++----------- .../Legality/Structures/ITrainerInfo.cs | 25 +++++++++ 2 files changed, 48 insertions(+), 29 deletions(-) diff --git a/PKHeX.Core/Legality/Core.cs b/PKHeX.Core/Legality/Core.cs index ed773d572..692c597b1 100644 --- a/PKHeX.Core/Legality/Core.cs +++ b/PKHeX.Core/Legality/Core.cs @@ -33,6 +33,7 @@ public static partial class Legal public static string EReaderBerryDisplayName => string.Format(V372, Util.ToTitleCase(EReaderBerryName.ToLower())); public static ITrainerInfo ActiveTrainer = new SimpleTrainerInfo {OT = string.Empty, Game = (int)GameVersion.Any, Language = -1}; + internal static bool IsNotFromActiveTrainer(PKM pkm) => !ActiveTrainer.IsFromTrainer(pkm); // Gen 1 internal static readonly Learnset[] LevelUpRB = Learnset1.GetArray(Util.GetBinaryResource("lvlmove_rb.pkl"), MaxSpeciesID_1); @@ -1177,27 +1178,33 @@ private static IEnumerable GetValidMoves(PKM pkm, GameVersion Version, IRea } private static IEnumerable GetEvoMoves(PKM pkm, GameVersion Version, IReadOnlyList vs, int Generation, int minLvLG1, int minLvLG2, bool LVL, bool Tutor, bool Machine, bool MoveReminder, bool RemoveTransferHM, bool moveTutor, int i, EvoCriteria evo) { - var minlvlevo1 = 1; - var minlvlevo2 = 1; - if (Generation == 1) - { - // Return moves from minLvLG1 if species if the species encounters - // For evolutions return moves using evolution min level as min level - minlvlevo1 = minLvLG1; - if (evo.MinLevel > 1) - minlvlevo1 = Math.Min(pkm.CurrentLevel, evo.MinLevel); - } - if (Generation == 2 && !AllowGen2MoveReminder(pkm)) - { - minlvlevo2 = minLvLG2; - if (evo.MinLevel > 1) - minlvlevo2 = Math.Min(pkm.CurrentLevel, evo.MinLevel); - } + int minlvlevo1 = GetEvoMoveMinLevel1(pkm, Generation, minLvLG1, evo); + int minlvlevo2 = GetEvoMoveMinLevel2(pkm, Generation, minLvLG2, evo); var maxLevel = evo.Level; if (i != 0 && vs[i - 1].RequiresLvlUp) // evolution ++maxLevel; // allow lvlmoves from the level it evolved to the next species return GetMoves(pkm, evo.Species, minlvlevo1, minlvlevo2, maxLevel, pkm.AltForm, moveTutor, Version, LVL, Tutor, Machine, MoveReminder, RemoveTransferHM, Generation); } + + private static int GetEvoMoveMinLevel1(PKM pkm, int Generation, int minLvLG1, EvoCriteria evo) + { + // Return moves from minLvLG1 if species if the species encounters + // For evolutions return moves using evolution min level as min level + if (Generation != 1) + return 1; + if (evo.MinLevel > 1) + return Math.Min(pkm.CurrentLevel, evo.MinLevel); + return minLvLG1; + } + private static int GetEvoMoveMinLevel2(PKM pkm, int Generation, int minLvLG2, EvoCriteria evo) + { + if (Generation != 2 || AllowGen2MoveReminder(pkm)) + return 1; + if (evo.MinLevel > 1) + return Math.Min(pkm.CurrentLevel, evo.MinLevel); + return minLvLG2; + } + private static IEnumerable GetMoves(PKM pkm, int species, int minlvlG1, int minlvlG2, int lvl, int form, bool moveTutor, GameVersion Version, bool LVL, bool specialTutors, bool Machine, bool MoveReminder, bool RemoveTransferHM, int Generation) { List r = new List(); @@ -1240,19 +1247,6 @@ internal static bool IsTradedKadabraG1(PKM pkm) return false; } - internal static bool IsNotFromActiveTrainer(PKM pkm) - { - if (ActiveTrainer.Game == (int)GameVersion.Any) - return false; - var Outsider = ActiveTrainer.TID != pkm.TID || ActiveTrainer.OT != pkm.OT_Name; - if (pkm.Format <= 2) - return Outsider; - Outsider |= ActiveTrainer.SID != pkm.SID; - if (pkm.Format == 3) // Generation 3 does not check ot gender nor pokemon version - return Outsider; - Outsider |= ActiveTrainer.Gender != pkm.OT_Gender || ActiveTrainer.Game != pkm.Version; - return Outsider; - } internal const GameVersion NONE = GameVersion.Invalid; internal static readonly LearnVersion LearnNONE = new LearnVersion(-1); diff --git a/PKHeX.Core/Legality/Structures/ITrainerInfo.cs b/PKHeX.Core/Legality/Structures/ITrainerInfo.cs index c7b7be0d1..bea21a05f 100644 --- a/PKHeX.Core/Legality/Structures/ITrainerInfo.cs +++ b/PKHeX.Core/Legality/Structures/ITrainerInfo.cs @@ -51,5 +51,30 @@ public static void ApplyHandlingTrainerInfo(this ITrainerInfo SAV, PKM pk, bool ((PK6)pk).TradeMemory(true); } } + + public static bool IsFromTrainer(this ITrainerInfo tr, PKM pk) + { + if (tr.Game == (int)GameVersion.Any) + return true; + + if (tr.TID != pk.TID) + return false; + if (tr.OT != pk.OT_Name) + return false; + if (pk.Format <= 2) + return false; + + if (tr.SID != pk.SID) + return false; + if (pk.Format == 3) + return false; + + if (tr.Gender != pk.OT_Gender) + return false; + if (tr.Game != pk.Version) + return false; + + return true; + } } } \ No newline at end of file