From e4092e64dfd5ecd095fcbc19d941d50faaf0e94b Mon Sep 17 00:00:00 2001 From: Kurt Date: Sat, 18 Jul 2020 22:51:55 -0500 Subject: [PATCH] Relocate gen3 ereader berry logic --- PKHeX.Core/Game/GameStrings/GameStrings.cs | 4 +- PKHeX.Core/Legality/Core.cs | 11 +-- PKHeX.Core/Legality/Verifiers/ItemVerifier.cs | 37 ++++----- .../Gen3/EReaderBerrySettings.cs | 78 +++++++++++++++++++ PKHeX.WinForms/MainWindow/Main.cs | 5 +- 5 files changed, 97 insertions(+), 38 deletions(-) create mode 100644 PKHeX.Core/Saves/Substructures/Gen3/EReaderBerrySettings.cs diff --git a/PKHeX.Core/Game/GameStrings/GameStrings.cs b/PKHeX.Core/Game/GameStrings/GameStrings.cs index 3748f755e..55eeb77b7 100644 --- a/PKHeX.Core/Game/GameStrings/GameStrings.cs +++ b/PKHeX.Core/Game/GameStrings/GameStrings.cs @@ -390,11 +390,11 @@ namespace PKHeX.Core case GameVersion.XD: return g3xditems; default: - if (Legal.EReaderBerryIsEnigma) + if (EReaderBerrySettings.IsEnigma) return g3items; var g3ItemsWithEBerry = (string[])g3items.Clone(); - g3ItemsWithEBerry[175] = Legal.EReaderBerryDisplayName; + g3ItemsWithEBerry[175] = EReaderBerrySettings.DisplayName; return g3ItemsWithEBerry; } } diff --git a/PKHeX.Core/Legality/Core.cs b/PKHeX.Core/Legality/Core.cs index 6ef4644f0..2f3b69bda 100644 --- a/PKHeX.Core/Legality/Core.cs +++ b/PKHeX.Core/Legality/Core.cs @@ -6,15 +6,6 @@ namespace PKHeX.Core { public static partial class Legal { - /// e-Reader Berry is Enigma or special berry - public static bool EReaderBerryIsEnigma { get; set; } = true; - - /// e-Reader Berry Name - public static string EReaderBerryName { get; set; } = string.Empty; - - /// e-Reader Berry Name formatted in Title Case - public static string EReaderBerryDisplayName => string.Format(LegalityCheckStrings.L_XEnigmaBerry_0, Util.ToTitleCase(EReaderBerryName.ToLower())); - // Gen 1 internal static readonly Learnset[] LevelUpRB = LearnsetReader.GetArray(Util.GetBinaryResource("lvlmove_rb.pkl"), MaxSpeciesID_1); internal static readonly Learnset[] LevelUpY = LearnsetReader.GetArray(Util.GetBinaryResource("lvlmove_y.pkl"), MaxSpeciesID_1); @@ -220,7 +211,7 @@ namespace PKHeX.Core return pkm.AltForm == 1; if (pkm.Species == (int)Species.Lycanroc) return pkm.AltForm < 3; - return pkm.Species == 773; + return pkm.Species == (int)Species.Silvally; } internal static bool IsEvolutionValid(PKM pkm, int minSpecies = -1, int minLevel = -1) diff --git a/PKHeX.Core/Legality/Verifiers/ItemVerifier.cs b/PKHeX.Core/Legality/Verifiers/ItemVerifier.cs index 7b4291ada..e16ff58a6 100644 --- a/PKHeX.Core/Legality/Verifiers/ItemVerifier.cs +++ b/PKHeX.Core/Legality/Verifiers/ItemVerifier.cs @@ -24,29 +24,22 @@ namespace PKHeX.Core private void VerifyEReaderBerry(LegalityAnalysis data) { - if (Legal.EReaderBerryIsEnigma) // no E-Reader berry data provided, can't hold berry. + var status = EReaderBerrySettings.GetStatus(); + var chk = GetEReaderCheckResult(status); + if (chk != null) + data.AddLine(chk); + } + + private CheckResult? GetEReaderCheckResult(EReaderBerryMatch status) + { + return status switch { - data.AddLine(GetInvalid(LItemUnreleased)); - return; - } - - var matchUSA = Legal.EReaderBerriesNames_USA.Contains(Legal.EReaderBerryName); - var matchJP = Legal.EReaderBerriesNames_JP.Contains(Legal.EReaderBerryName); - if (!matchJP && !matchUSA) // Does not match any released E-Reader berry - { - data.AddLine(GetInvalid(LEReaderInvalid)); - return; - } - if (ParseSettings.ActiveTrainer.Language <= 0) - return; - - bool jp = ParseSettings.ActiveTrainer.Language == 1; - if (matchJP == jp) - return; // matches - - // E-Reader is region locked - var msg = matchUSA ? LEReaderAmerica : LEReaderJapan; - data.AddLine(GetInvalid(msg)); + EReaderBerryMatch.NoMatch => GetInvalid(LEReaderInvalid), + EReaderBerryMatch.NoData => GetInvalid(LItemUnreleased), + EReaderBerryMatch.InvalidUSA => GetInvalid(LEReaderAmerica), + EReaderBerryMatch.InvalidJPN => GetInvalid(LEReaderJapan), + _ => null + }; } } } diff --git a/PKHeX.Core/Saves/Substructures/Gen3/EReaderBerrySettings.cs b/PKHeX.Core/Saves/Substructures/Gen3/EReaderBerrySettings.cs new file mode 100644 index 000000000..070b7fb4d --- /dev/null +++ b/PKHeX.Core/Saves/Substructures/Gen3/EReaderBerrySettings.cs @@ -0,0 +1,78 @@ +using static PKHeX.Core.EReaderBerryMatch; + +namespace PKHeX.Core +{ + /// + /// Stores details about the Gen3 e-Reader Berry values. + /// + public static class EReaderBerrySettings + { + /// e-Reader Berry is Enigma or special berry (from e-Reader data) + public static bool IsEnigma { get; set; } = true; + + /// e-Reader Berry Name + private static string Name { get; set; } = string.Empty; + + /// e-Reader Berry Name formatted in Title Case + public static string DisplayName => string.Format(LegalityCheckStrings.L_XEnigmaBerry_0, Util.ToTitleCase(Name)); + + private static int Language { get; set; } + + public static EReaderBerryMatch GetStatus() + { + if (IsEnigma) // no e-Reader Berry data provided, can't hold berry. + return NoData; + + var matchUSA = Legal.EReaderBerriesNames_USA.Contains(Name); + if (matchUSA) + { + if (Language <= 0) + return ValidAny; + if (Language != 1) + return ValidUSA; + return InvalidUSA; + } + + var matchJP = Legal.EReaderBerriesNames_JP.Contains(Name); + if (matchJP) + { + if (Language <= 0) + return ValidAny; + if (Language == 1) + return ValidJPN; + return InvalidJPN; + } + + return NoMatch; + } + + public static void LoadFrom(SAV3 sav3) + { + IsEnigma = sav3.IsEBerryIsEnigma; + Name = sav3.EBerryName; + Language = sav3.Japanese ? (int)LanguageID.Japanese : (int)LanguageID.English; + } + } + + /// + /// For use in validating the current e-Reader Berry settings. + /// + public enum EReaderBerryMatch : byte + { + /// Invalid: Doesn't match either language + NoMatch, + /// Invalid: No data provided from a save file + NoData, + /// Invalid: USA berry name on JPN save file + InvalidUSA, + /// Invalid: JPN berry name on USA save file + InvalidJPN, + + /// Valid: Berry name matches either USA/JPN (Ambiguous Save File Language) + ValidAny, + /// Valid: Berry name matches a USA berry name + ValidUSA, + /// Valid: Berry name matches a JPN berry name + ValidJPN, + } +} diff --git a/PKHeX.WinForms/MainWindow/Main.cs b/PKHeX.WinForms/MainWindow/Main.cs index 7debad580..52009999e 100644 --- a/PKHeX.WinForms/MainWindow/Main.cs +++ b/PKHeX.WinForms/MainWindow/Main.cs @@ -707,10 +707,7 @@ namespace PKHeX.WinForms private static void StoreLegalSaveGameData(SaveFile sav) { if (sav is SAV3 sav3) - { - Legal.EReaderBerryIsEnigma = sav3.IsEBerryIsEnigma; - Legal.EReaderBerryName = sav3.EBerryName; - } + EReaderBerrySettings.LoadFrom(sav3); } private bool OpenSAV(SaveFile sav, string path)