diff --git a/PKHeX.Core/Legality/Encounters/Generator/MysteryGiftGenerator.cs b/PKHeX.Core/Legality/Encounters/Generator/MysteryGiftGenerator.cs index 6a8284532..66501d2b4 100644 --- a/PKHeX.Core/Legality/Encounters/Generator/MysteryGiftGenerator.cs +++ b/PKHeX.Core/Legality/Encounters/Generator/MysteryGiftGenerator.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using static PKHeX.Core.EncounterEvent; @@ -30,15 +31,15 @@ public static IEnumerable GetValidGifts(PKM pkm) { int gen = pkm.GenNumber; if (pkm.IsEgg && pkm.Format != gen) // transferred - return Enumerable.Empty(); + return Array.Empty(); - if (gen == 4) // check for manaphy gift + if (gen == 4) // check for Manaphy gift return GetMatchingPCD(pkm, MGDB_G4); var table = GetTable(gen, pkm); return GetMatchingGifts(pkm, table); } - private static IEnumerable GetTable(int generation, PKM pkm) + private static IReadOnlyList GetTable(int generation, PKM pkm) { return generation switch { @@ -46,13 +47,13 @@ private static IEnumerable GetTable(int generation, PKM pkm) 4 => MGDB_G4, 5 => MGDB_G5, 6 => MGDB_G6, - 7 => (pkm.LGPE ? (IEnumerable)MGDB_G7GG : MGDB_G7), + 7 => pkm.LGPE ? (IReadOnlyList)MGDB_G7GG : MGDB_G7, 8 => MGDB_G8, - _ => Enumerable.Empty() + _ => Array.Empty() }; } - private static IEnumerable GetMatchingPCD(PKM pkm, IEnumerable DB) + private static IEnumerable GetMatchingPCD(PKM pkm, IReadOnlyList DB) { if (PGT.IsRangerManaphy(pkm)) { @@ -64,25 +65,28 @@ private static IEnumerable GetMatchingPCD(PKM pkm, IEnumerable yield return g; } - private static IEnumerable GetMatchingGifts(PKM pkm, IEnumerable DB) + private static IEnumerable GetMatchingGifts(PKM pkm, IReadOnlyList DB) { var chain = EvolutionChain.GetOriginChain(pkm); return GetMatchingGifts(pkm, DB, chain); } - private static IEnumerable GetMatchingGifts(PKM pkm, IEnumerable DB, IReadOnlyList chain) + private static IEnumerable GetMatchingGifts(PKM pkm, IReadOnlyList DB, IReadOnlyList chain) { var deferred = new List(); - var gifts = DB.Where(wc => chain.Any(dl => dl.Species == wc.Species)); - foreach (var mg in gifts) + foreach (var mg in DB) { - var result = mg.IsMatch(pkm); - if (result == EncounterMatchRating.None) - continue; - if (result == EncounterMatchRating.Match) - yield return mg; - else if (result == EncounterMatchRating.Deferred) - deferred.Add(mg); + foreach (var dl in chain) + { + if (dl.Species != mg.Species) + continue; + var result = mg.IsMatch(pkm, dl); + if (result == EncounterMatchRating.Match) + yield return mg; + else if (result == EncounterMatchRating.Deferred) + deferred.Add(mg); + break; + } } foreach (var z in deferred) yield return z; diff --git a/PKHeX.Core/MysteryGifts/MysteryGift.cs b/PKHeX.Core/MysteryGifts/MysteryGift.cs index e6a74360b..dfab27633 100644 --- a/PKHeX.Core/MysteryGifts/MysteryGift.cs +++ b/PKHeX.Core/MysteryGifts/MysteryGift.cs @@ -132,12 +132,12 @@ public abstract class MysteryGift : IEncounterable, IMoveset, IRelearn, ILocatio public PKM ConvertToPKM(ITrainerInfo sav) => ConvertToPKM(sav, EncounterCriteria.Unrestricted); public abstract PKM ConvertToPKM(ITrainerInfo sav, EncounterCriteria criteria); - protected abstract bool IsMatchExact(PKM pkm); + protected abstract bool IsMatchExact(PKM pkm, DexLevel evo); protected abstract bool IsMatchDeferred(PKM pkm); - public EncounterMatchRating IsMatch(PKM pkm) + public EncounterMatchRating IsMatch(PKM pkm, DexLevel evo) { - if (!IsMatchExact(pkm)) + if (!IsMatchExact(pkm, evo)) return EncounterMatchRating.None; if (IsMatchDeferred(pkm)) return EncounterMatchRating.Deferred; diff --git a/PKHeX.Core/MysteryGifts/PCD.cs b/PKHeX.Core/MysteryGifts/PCD.cs index 87ff505da..864d9b9f5 100644 --- a/PKHeX.Core/MysteryGifts/PCD.cs +++ b/PKHeX.Core/MysteryGifts/PCD.cs @@ -129,7 +129,7 @@ public override PKM ConvertToPKM(ITrainerInfo sav, EncounterCriteria criteria) public bool CanBeReceivedBy(int pkmVersion) => (CardCompatibility >> pkmVersion & 1) == 1; - protected override bool IsMatchExact(PKM pkm) + protected override bool IsMatchExact(PKM pkm, DexLevel evo) { var wc = Gift.PK; if (!wc.IsEgg) @@ -161,7 +161,7 @@ protected override bool IsMatchExact(PKM pkm) return false; } - if (wc.AltForm != pkm.AltForm && !Legal.IsFormChangeable(pkm, Species, wc.AltForm)) + if (wc.AltForm != evo.Form && !Legal.IsFormChangeable(pkm, Species, wc.AltForm)) return false; if (wc.Ball != pkm.Ball) return false; diff --git a/PKHeX.Core/MysteryGifts/PGF.cs b/PKHeX.Core/MysteryGifts/PGF.cs index 15e1636d6..925e6f207 100644 --- a/PKHeX.Core/MysteryGifts/PGF.cs +++ b/PKHeX.Core/MysteryGifts/PGF.cs @@ -336,7 +336,7 @@ private void SetIVs(PKM pk) pk.IVs = finalIVs; } - protected override bool IsMatchExact(PKM pkm) + protected override bool IsMatchExact(PKM pkm, DexLevel evo) { if (!IsEgg) { @@ -369,7 +369,7 @@ protected override bool IsMatchExact(PKM pkm) return false; } - if (Form != pkm.AltForm && !Legal.IsFormChangeable(pkm, Species, Form)) + if (Form != evo.Form && !Legal.IsFormChangeable(pkm, Species, Form)) return false; if (Level != pkm.Met_Level) return false; diff --git a/PKHeX.Core/MysteryGifts/PGT.cs b/PKHeX.Core/MysteryGifts/PGT.cs index 5ac04b1e2..4c2ca1dfc 100644 --- a/PKHeX.Core/MysteryGifts/PGT.cs +++ b/PKHeX.Core/MysteryGifts/PGT.cs @@ -254,7 +254,7 @@ public static bool IsRangerManaphy(PKM pkm) return met == Locations.LinkTrade4 || met == 0; } - protected override bool IsMatchExact(PKM pkm) => false; + protected override bool IsMatchExact(PKM pkm, DexLevel evo) => false; protected override bool IsMatchDeferred(PKM pkm) => false; } } diff --git a/PKHeX.Core/MysteryGifts/WB7.cs b/PKHeX.Core/MysteryGifts/WB7.cs index e2168107a..605ed2384 100644 --- a/PKHeX.Core/MysteryGifts/WB7.cs +++ b/PKHeX.Core/MysteryGifts/WB7.cs @@ -458,7 +458,7 @@ private void SetIVs(PKM pk) pk.IVs = finalIVs; } - protected override bool IsMatchExact(PKM pkm) + protected override bool IsMatchExact(PKM pkm, DexLevel evo) { if (pkm.Egg_Location == 0) // Not Egg { @@ -474,7 +474,7 @@ protected override bool IsMatchExact(PKM pkm) if (EncryptionConstant != 0 && EncryptionConstant != pkm.EncryptionConstant) return false; } - if (Form != pkm.AltForm && !Legal.IsFormChangeable(pkm, Species, Form)) + if (Form != evo.Form && !Legal.IsFormChangeable(pkm, Species, Form)) return false; if (IsEgg) diff --git a/PKHeX.Core/MysteryGifts/WC3.cs b/PKHeX.Core/MysteryGifts/WC3.cs index f0031b3d0..0069e4b40 100644 --- a/PKHeX.Core/MysteryGifts/WC3.cs +++ b/PKHeX.Core/MysteryGifts/WC3.cs @@ -211,7 +211,7 @@ private static GameVersion GetRandomVersion(GameVersion version) } } - protected override bool IsMatchExact(PKM pkm) + protected override bool IsMatchExact(PKM pkm, DexLevel evo) { // Gen3 Version MUST match. if (Version != 0 && !Version.Contains((GameVersion)pkm.Version)) @@ -238,6 +238,9 @@ protected override bool IsMatchExact(PKM pkm) } } + if (Form != evo.Form && !Legal.IsFormChangeable(pkm, Species, Form)) + return false; + if (Language != -1 && Language != pkm.Language) return false; if (Ball != pkm.Ball) return false; if (Fateful != pkm.FatefulEncounter) diff --git a/PKHeX.Core/MysteryGifts/WC6.cs b/PKHeX.Core/MysteryGifts/WC6.cs index ca6d86004..6535a801a 100644 --- a/PKHeX.Core/MysteryGifts/WC6.cs +++ b/PKHeX.Core/MysteryGifts/WC6.cs @@ -457,7 +457,7 @@ private void SetIVs(PKM pk) pk.IVs = finalIVs; } - protected override bool IsMatchExact(PKM pkm) + protected override bool IsMatchExact(PKM pkm, DexLevel evo) { if (pkm.Egg_Location == 0) // Not Egg { @@ -478,7 +478,7 @@ protected override bool IsMatchExact(PKM pkm) if (EncryptionConstant != 0 && EncryptionConstant != pkm.EncryptionConstant) return false; if (Language != 0 && Language != pkm.Language) return false; } - if (Form != pkm.AltForm && !Legal.IsFormChangeable(pkm, Species, Form)) + if (Form != evo.Form && !Legal.IsFormChangeable(pkm, Species, Form)) return false; if (IsEgg) diff --git a/PKHeX.Core/MysteryGifts/WC7.cs b/PKHeX.Core/MysteryGifts/WC7.cs index aa4894f02..9a57c0d71 100644 --- a/PKHeX.Core/MysteryGifts/WC7.cs +++ b/PKHeX.Core/MysteryGifts/WC7.cs @@ -493,7 +493,7 @@ public bool IsAshGreninjaWC7(PKM pkm) return CardID == 2046 && (pkm.SID << 16 | pkm.TID) == 0x79F57B49; } - protected override bool IsMatchExact(PKM pkm) + protected override bool IsMatchExact(PKM pkm, DexLevel evo) { if (pkm.Egg_Location == 0) // Not Egg { @@ -509,17 +509,8 @@ protected override bool IsMatchExact(PKM pkm) if (Language != 0 && Language != pkm.Language) return false; } - if (Form != pkm.AltForm && !Legal.IsFormChangeable(pkm, Species, Form)) - { - if (Species == (int)Core.Species.Rockruff && Form == 1 && pkm.Species == 745 && pkm.AltForm == 2) - { - // Rockruff gift edge case; has altform 1 then evolves to altform 2 - } - else - { - return false; - } - } + if (Form != evo.Form && !Legal.IsFormChangeable(pkm, Species, Form)) + return false; if (IsEgg) { diff --git a/PKHeX.Core/MysteryGifts/WC8.cs b/PKHeX.Core/MysteryGifts/WC8.cs index ed91a1f2a..1c3ad4067 100644 --- a/PKHeX.Core/MysteryGifts/WC8.cs +++ b/PKHeX.Core/MysteryGifts/WC8.cs @@ -517,7 +517,7 @@ private void SetIVs(PKM pk) pk.IVs = finalIVs; } - protected override bool IsMatchExact(PKM pkm) + protected override bool IsMatchExact(PKM pkm, DexLevel evo) { if (pkm.Egg_Location == 0) // Not Egg { diff --git a/PKHeX.Core/MysteryGifts/WR7.cs b/PKHeX.Core/MysteryGifts/WR7.cs index 1c4b0276f..f61bd818a 100644 --- a/PKHeX.Core/MysteryGifts/WR7.cs +++ b/PKHeX.Core/MysteryGifts/WR7.cs @@ -97,7 +97,7 @@ public LanguageID LanguageReceived // Mystery Gift implementation public override int Format => 7; - protected override bool IsMatchExact(PKM pkm) => false; + protected override bool IsMatchExact(PKM pkm, DexLevel evo) => false; protected override bool IsMatchDeferred(PKM pkm) => false; public override int Location { get; set; } public override int EggLocation { get; set; }