From 47bc45d854be64ca8d03f8fba7613dc114e6bd77 Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 18 May 2025 01:36:05 -0500 Subject: [PATCH] GetLevelLearnMove->TryGetLevelLearnMove As alluded to in https://github.com/kwsch/PKHeX/commit/7442e86d6573ec6f9c5e5a398300d3a24dfd36cb --- .../Applicators/MoveShopRecordApplicator.cs | 8 +++---- .../Encounters/Generator/EncounterCriteria.cs | 24 +++++++++---------- .../Evolutions/Methods/EvoCriteria.cs | 2 +- .../Legality/LearnSource/Group/LearnGroup3.cs | 7 +++--- .../Legality/LearnSource/Group/LearnGroup4.cs | 7 +++--- .../LearnSource/Sources/LearnSource1RB.cs | 5 ++-- .../LearnSource/Sources/LearnSource1YW.cs | 5 ++-- .../LearnSource/Sources/LearnSource2C.cs | 5 ++-- .../LearnSource/Sources/LearnSource2GS.cs | 7 +++--- .../LearnSource/Sources/LearnSource3E.cs | 5 ++-- .../LearnSource/Sources/LearnSource3FR.cs | 5 ++-- .../LearnSource/Sources/LearnSource3LG.cs | 5 ++-- .../LearnSource/Sources/LearnSource3RS.cs | 5 ++-- .../LearnSource/Sources/LearnSource4DP.cs | 5 ++-- .../LearnSource/Sources/LearnSource4HGSS.cs | 5 ++-- .../LearnSource/Sources/LearnSource4Pt.cs | 5 ++-- .../LearnSource/Sources/LearnSource5B2W2.cs | 5 ++-- .../LearnSource/Sources/LearnSource5BW.cs | 5 ++-- .../LearnSource/Sources/LearnSource6AO.cs | 5 ++-- .../LearnSource/Sources/LearnSource6XY.cs | 5 ++-- .../LearnSource/Sources/LearnSource7GG.cs | 5 ++-- .../LearnSource/Sources/LearnSource7SM.cs | 5 ++-- .../LearnSource/Sources/LearnSource7USUM.cs | 5 ++-- .../LearnSource/Sources/LearnSource8BDSP.cs | 10 ++++---- .../LearnSource/Sources/LearnSource8PLA.cs | 10 ++++---- .../LearnSource/Sources/LearnSource8SWSH.cs | 10 ++++---- .../LearnSource/Sources/LearnSource9SV.cs | 10 ++++---- PKHeX.Core/Legality/Learnset/Learnset.cs | 17 +++++++++---- .../Verifiers/LegendsArceusVerifier.cs | 8 +++---- PKHeX.Core/PKM/Interfaces/IMoveShop8.cs | 10 ++++---- 30 files changed, 97 insertions(+), 118 deletions(-) diff --git a/PKHeX.Core/Editing/Applicators/MoveShopRecordApplicator.cs b/PKHeX.Core/Editing/Applicators/MoveShopRecordApplicator.cs index b4a63299d..64cffb863 100644 --- a/PKHeX.Core/Editing/Applicators/MoveShopRecordApplicator.cs +++ b/PKHeX.Core/Editing/Applicators/MoveShopRecordApplicator.cs @@ -102,14 +102,14 @@ public static void SetMasteredFlag(this IMoveShop8Mastery shop, Learnset learn, if (shop.GetMasteredRecordFlag(index)) return; - if (level < (uint)learn.GetLevelLearnMove(move)) // Can't learn it yet; must purchase. + if (learn.TryGetLevelLearnMove(move, out var learnLevel) && level < learnLevel) // Can't learn it yet; must purchase. { shop.SetPurchasedRecordFlag(index, true); shop.SetMasteredRecordFlag(index, true); return; } - if (level < (uint)mastery.GetLevelLearnMove(move)) // Can't master it yet; must Seed of Mastery + if (mastery.TryGetLevelLearnMove(move, out var masterLevel) && level < masterLevel) // Can't master it yet; must Seed of Mastery shop.SetMasteredRecordFlag(index, true); } @@ -131,8 +131,8 @@ public static void SetEncounterMasteryFlags(this IMoveShop8Mastery shop, ReadOnl // If the Pokémon is caught with any move shop move in its learnset, // and it is high enough level to master it, the game will automatically // give it the "Mastered" flag but not the "Purchased" flag - // For moves that are not in the learnset, it returns -1 which is true, thus set as mastered. - if (level >= mastery.GetLevelLearnMove(move)) + // For moves that are not in the learnset, set as mastered. + if (!mastery.TryGetLevelLearnMove(move, out var masteryLevel) || level >= masteryLevel) shop.SetMasteredRecordFlag(index, true); } } diff --git a/PKHeX.Core/Legality/Encounters/Generator/EncounterCriteria.cs b/PKHeX.Core/Legality/Encounters/Generator/EncounterCriteria.cs index 29dcda7b6..4faae48f8 100644 --- a/PKHeX.Core/Legality/Encounters/Generator/EncounterCriteria.cs +++ b/PKHeX.Core/Legality/Encounters/Generator/EncounterCriteria.cs @@ -256,34 +256,32 @@ public byte GetGender(Gender gender, IGenderDetail pkPersonalInfo) /// /// Gets the gender to generate, random if unspecified. /// - public byte GetGender(IGenderDetail pkPersonalInfo) + public byte GetGender(IGenderDetail info) { - if (!pkPersonalInfo.IsDualGender) - return pkPersonalInfo.FixedGender(); - if (pkPersonalInfo.Genderless) + if (!info.IsDualGender) + return info.FixedGender(); + if (info.Genderless) return 2; if (Gender is not Gender.Random) return (byte)Gender; - return pkPersonalInfo.RandomGender(); + return info.RandomGender(); } /// - /// Gets a random ability index (0/1/2) to generate, based off an encounter's . + /// Gets a random ability index (0/1/2) to generate, based off an encounter's . /// - public int GetAbilityFromNumber(AbilityPermission num) + public int GetAbilityFromNumber(AbilityPermission encounter) { - if (num.IsSingleValue(out int index)) // fixed number + if (encounter.IsSingleValue(out int index)) // fixed number return index; - - bool canBeHidden = num.CanBeHidden(); - return GetAbilityIndexPreference(canBeHidden); + return GetAbilityIndexPreference(encounter); } - private int GetAbilityIndexPreference(bool canBeHidden = false) => Ability switch + private int GetAbilityIndexPreference(AbilityPermission perm) => Ability switch { OnlyFirst => 0, OnlySecond => 1, - OnlyHidden or Any12H when canBeHidden => 2, // hidden allowed + OnlyHidden or Any12H when perm.CanBeHidden() => 2, // hidden allowed _ => Util.Rand.Next(2), }; diff --git a/PKHeX.Core/Legality/Evolutions/Methods/EvoCriteria.cs b/PKHeX.Core/Legality/Evolutions/Methods/EvoCriteria.cs index 5dc113143..829c5055c 100644 --- a/PKHeX.Core/Legality/Evolutions/Methods/EvoCriteria.cs +++ b/PKHeX.Core/Legality/Evolutions/Methods/EvoCriteria.cs @@ -16,7 +16,7 @@ namespace PKHeX.Core; public bool RequiresLvlUp => LevelUpRequired != 0; - public bool InsideLevelRange(int level) => LevelMin <= level && level <= LevelMax; + public bool InsideLevelRange(byte level) => LevelMin <= level && level <= LevelMax; public override string ToString() => $"{(Species) Species}{(Form != 0 ? $"-{Form}" : "")}}} [{LevelMin},{LevelMax}] via {Method}"; diff --git a/PKHeX.Core/Legality/LearnSource/Group/LearnGroup3.cs b/PKHeX.Core/Legality/LearnSource/Group/LearnGroup3.cs index ad71e64ef..17c81d692 100644 --- a/PKHeX.Core/Legality/LearnSource/Group/LearnGroup3.cs +++ b/PKHeX.Core/Legality/LearnSource/Group/LearnGroup3.cs @@ -55,11 +55,12 @@ private static void CheckNincadaMoves(Span result, ReadOnlySpan result, ReadOnlySpan moves, ReadOnlySpan ignore, i /// Move ID public bool GetIsLearn(ushort move) => Moves.AsSpan().Contains(move); - /// Returns the level that a Pokémon can learn the specified move. + /// + /// Checks if the specified move is learned by level up. + /// /// Move ID - /// Level the move is learned at. If the result is below 0, the move cannot be learned by leveling up. - public int GetLevelLearnMove(ushort move) + /// Level at which the move is learned + /// True if the move is learned by level up, false otherwise. + public bool TryGetLevelLearnMove(ushort move, out byte level) { var index = Array.IndexOf(Moves, move); if (index == -1) - return -1; - return Levels[index]; + { + level = 0; + return false; + } + level = Levels[index]; + return true; } public ReadOnlySpan GetBaseEggMoves(byte level) diff --git a/PKHeX.Core/Legality/Verifiers/LegendsArceusVerifier.cs b/PKHeX.Core/Legality/Verifiers/LegendsArceusVerifier.cs index 2a35f6335..04b3bd31c 100644 --- a/PKHeX.Core/Legality/Verifiers/LegendsArceusVerifier.cs +++ b/PKHeX.Core/Legality/Verifiers/LegendsArceusVerifier.cs @@ -154,9 +154,8 @@ private static int AddMasteredMissing(PA8 pa, Span current, int ctr, Lea // Check if we can swap it into the moveset after it evolves. var move = purchased[i]; - var baseLevel = baseLearn.GetLevelLearnMove(move); - var mustKnow = baseLevel is not -1 && baseLevel <= pa.MetLevel; - if (!mustKnow && currentLearn.GetLevelLearnMove(move) != level) + var mustKnow = baseLearn.TryGetLevelLearnMove(move, out var baseLevel) && baseLevel <= pa.MetLevel; + if (!mustKnow && currentLearn.TryGetLevelLearnMove(move, out var c2) && c2 != level) continue; if (!current.Contains(move)) @@ -229,8 +228,7 @@ private static bool CanLearnMoveByLevelUp(LegalityAnalysis data, PA8 pa, int i, foreach (var evo in data.Info.EvoChainsAllGens.Gen8a) { var moveset = LearnSource8LA.Instance.GetLearnset(evo.Species, evo.Form); - var lvl = moveset.GetLevelLearnMove(moves[i]); - if (lvl == -1) + if (!moveset.TryGetLevelLearnMove(moves[i], out var lvl)) continue; // cannot learn via level up level = Math.Min(lvl, level); } diff --git a/PKHeX.Core/PKM/Interfaces/IMoveShop8.cs b/PKHeX.Core/PKM/Interfaces/IMoveShop8.cs index 17b99bc31..bbd4fffe9 100644 --- a/PKHeX.Core/PKM/Interfaces/IMoveShop8.cs +++ b/PKHeX.Core/PKM/Interfaces/IMoveShop8.cs @@ -76,8 +76,9 @@ public static bool IsValidPurchasedEncounter(this IMoveShop8 shop, Learnset lear var move = permit[i]; // Can only purchase a move if it is not already in the available learnset. - var learnLevel = learn.GetLevelLearnMove(move); - if ((uint)learnLevel <= level) + if (!learn.TryGetLevelLearnMove(move, out var learnLevel)) + return false; + if (learnLevel <= level) return false; // Can only purchase an Alpha Move if it was pre-1.1 patch. @@ -101,15 +102,14 @@ public static bool IsValidMasteredEncounter(this IMoveShop8Mastery shop, Span metLevel && move != alphaMove) // no master flag set + if (mastery.TryGetLevelLearnMove(move, out var masteryLevel) && masteryLevel > metLevel && move != alphaMove) { if (!mastered) continue; if (purchased) continue; // Check for seed of mastery usage - if (learn.GetLevelLearnMove(move) > metLevel) + if (learn.TryGetLevelLearnMove(move, out var learnLevel) && learnLevel > metLevel) return false; } else