diff --git a/PKHeX.Core/Legality/Encounters/EncounterGenerator.cs b/PKHeX.Core/Legality/Encounters/EncounterGenerator.cs index 5d9a1d8e6..7c12a9e99 100644 --- a/PKHeX.Core/Legality/Encounters/EncounterGenerator.cs +++ b/PKHeX.Core/Legality/Encounters/EncounterGenerator.cs @@ -295,7 +295,8 @@ private static IEnumerable GenerateRawEncounters4(PKM pkm, Legal var slots = FrameFinder.GetFrames(info.PIDIV, pkm).ToList(); foreach (var z in GetValidWildEncounters(pkm)) { - if (slots.Any(s => s.EncounterSlot(z.Type, z) == z.SlotNumber)) + var frame = slots.FirstOrDefault(s => s.IsSlotCompatibile(z, pkm)); + if (frame != null) yield return z; else deferred.Add(z); @@ -329,7 +330,8 @@ private static IEnumerable GenerateRawEncounters3(PKM pkm, Legal var slots = FrameFinder.GetFrames(info.PIDIV, pkm).ToList(); foreach (var z in GetValidWildEncounters(pkm)) { - if (slots.Any(s => s.EncounterSlot(z.Type, z) == z.SlotNumber)) + var frame = slots.FirstOrDefault(s => s.IsSlotCompatibile(z, pkm)); + if (frame != null) yield return z; else deferred.Add(z); diff --git a/PKHeX.Core/Legality/RNG/Frame/Frame.cs b/PKHeX.Core/Legality/RNG/Frame/Frame.cs index 3cafecada..697fbb709 100644 --- a/PKHeX.Core/Legality/RNG/Frame/Frame.cs +++ b/PKHeX.Core/Legality/RNG/Frame/Frame.cs @@ -8,11 +8,10 @@ public class Frame private readonly FrameType FrameType; private readonly RNG RNG; + public uint RandLevel { get; set; } public uint ESV { get; set; } - public void SetOriginSeed(int Offset) => OriginSeed = RNG.Reverse(Seed, Offset); - public bool LevelSlotModified => Lead > LeadRequired.SynchronizeFail; - public uint OriginSeed; + public bool LevelSlotModified => Lead > LeadRequired.SynchronizeFail; public Frame(uint seed, FrameType type, RNG rng, LeadRequired lead) { @@ -22,30 +21,78 @@ public Frame(uint seed, FrameType type, RNG rng, LeadRequired lead) RNG = rng; } - public int EncounterSlot(SlotType t, EncounterSlot slot) + /// + /// Checks the Encounter Slot for RNG calls before the Nature loop. + /// + /// Slot Data + /// Ancillary pkm data for determining how to check level. + /// Slot number for this frame & lead value. + public bool IsSlotCompatibile(EncounterSlot slot, PKM pkm) { - if (Lead == LeadRequired.StaticMagnet) - { - if (slot.Permissions.MagnetPullIndex >= 0) - { - var index = ESV % slot.Permissions.MagnetPullCount; - return index == slot.Permissions.MagnetPullIndex ? slot.SlotNumber : -1; - } + // Level is before Nature, but usually isn't varied. Check ESV calc first. + int s = GetSlot(slot); + if (s != slot.SlotNumber) + return false; - if (slot.Permissions.StaticIndex >= 0) - { - var index = ESV % slot.Permissions.StaticCount; - return index == slot.Permissions.StaticIndex ? slot.SlotNumber : -1; - } - return -1; + // Check Level Now + int lvl = SlotRange.GetLevel(slot, FrameType, Lead, RandLevel); + if (lvl < 0) { } // todo + else if (pkm.HasOriginalMetLocation) + { + if (lvl != pkm.Met_Level) + return false; } - return EncounterSlot(t); + else + { + if (lvl < pkm.Met_Level) + return false; + } + + // Check if the slot is actually encounterable (considering Sweet Scent) + bool encounterable = SlotRange.GetIsEncounterable(slot, FrameType); + return encounterable; } + + /// + /// Gets the slot value for the input slot. + /// + /// Slot Data + /// Slot number for this frame & lead value. + private int GetSlot(EncounterSlot slot) + { + // Static and Magnet Pull do a slot search rather than slot mapping 0-99. + return Lead != LeadRequired.StaticMagnet + ? GetSlot(slot.Type) + : GetSlotStaticMagnet(slot); + } + + /// + /// Checks both Static and Magnet Pull ability type selection encounters to see if the encounter can be selected. + /// + /// Slot Data + /// Slot number from the slot data if the slot is selected on this frame, else an invalid slot value. + private int GetSlotStaticMagnet(EncounterSlot slot) + { + if (slot.Permissions.StaticIndex >= 0) + { + var index = ESV % slot.Permissions.StaticCount; + if (index == slot.Permissions.StaticIndex) + return slot.SlotNumber; + } + if (slot.Permissions.MagnetPullIndex >= 0) + { + var index = ESV % slot.Permissions.MagnetPullCount; + if (index == slot.Permissions.MagnetPullIndex) + return slot.SlotNumber; + } + return -1; + } + /// /// Only use this for test methods. /// /// /// - public int EncounterSlot(SlotType t) => SlotRange.GetSlot(t, ESV, FrameType, Seed); + public int GetSlot(SlotType t) => SlotRange.GetSlot(t, ESV, FrameType, Seed); } } diff --git a/PKHeX.Core/Legality/RNG/Frame/FrameFinder.cs b/PKHeX.Core/Legality/RNG/Frame/FrameFinder.cs index 90687440d..a4f96c898 100644 --- a/PKHeX.Core/Legality/RNG/Frame/FrameFinder.cs +++ b/PKHeX.Core/Legality/RNG/Frame/FrameFinder.cs @@ -96,11 +96,11 @@ private static IEnumerable GenerateLeadSpecificFrames3(Frame f, FrameGene if (f.Lead == LeadRequired.CuteCharm) // 100% required for frame base { if (cc) - yield return info.GetFrame(prev2, LeadRequired.CuteCharm, p2); + yield return info.GetFrame(prev2, LeadRequired.CuteCharm, p2, p1); yield break; } lead = cc ? LeadRequired.CuteCharm : LeadRequired.CuteCharmFail; - yield return info.GetFrame(prev2, lead, p2); + yield return info.GetFrame(prev2, lead, p2, p1); } // Pressure, Hustle, Vital Spirit = Force Maximum Level from slot @@ -110,7 +110,7 @@ private static IEnumerable GenerateLeadSpecificFrames3(Frame f, FrameGene // 1 Nature bool max = p0 % 2 == 1; lead = max ? LeadRequired.PressureHustleSpirit : LeadRequired.PressureHustleSpiritFail; - yield return info.GetFrame(prev2, lead, p2); + yield return info.GetFrame(prev2, lead, p2, p1); // Keen Eye, Intimidate // -2 ESV @@ -121,7 +121,7 @@ private static IEnumerable GenerateLeadSpecificFrames3(Frame f, FrameGene if (max) // same result as above, no need to recalculate { lead = LeadRequired.IntimidateKeenEye; - yield return info.GetFrame(prev2, lead, p2); + yield return info.GetFrame(prev2, lead, p2, p1); } // Static or Magnet Pull @@ -134,7 +134,7 @@ private static IEnumerable GenerateLeadSpecificFrames3(Frame f, FrameGene { // Since a failed proc is indistinguishable from the default frame calls, only generate if it succeeds. lead = LeadRequired.StaticMagnet; - yield return info.GetFrame(prev2, lead, p1); + yield return info.GetFrame(prev2, lead, p1, p0); } } @@ -162,8 +162,8 @@ private static IEnumerable RefineFrames4(IEnumerable frames, Frame var prev = info.RNG.Prev(f.Seed); var p16 = prev >> 16; - yield return info.GetFrame(prev, LeadRequired.IntimidateKeenEye, p16); - yield return info.GetFrame(prev, LeadRequired.PressureHustleSpirit, p16); + yield return info.GetFrame(prev, LeadRequired.IntimidateKeenEye, p16, p16); + yield return info.GetFrame(prev, LeadRequired.PressureHustleSpirit, p16, p16); // Slot Modifiers before ESV var force = (info.DPPt ? p16 >> 15 : p16 & 1) == 1; @@ -171,7 +171,7 @@ private static IEnumerable RefineFrames4(IEnumerable frames, Frame continue; var rand = f.Seed >> 16; - yield return info.GetFrame(prev, LeadRequired.StaticMagnet, rand); + yield return info.GetFrame(prev, LeadRequired.StaticMagnet, rand, p16); } } diff --git a/PKHeX.Core/Legality/RNG/Frame/FrameGenerator.cs b/PKHeX.Core/Legality/RNG/Frame/FrameGenerator.cs index dea752e31..e8b6fa316 100644 --- a/PKHeX.Core/Legality/RNG/Frame/FrameGenerator.cs +++ b/PKHeX.Core/Legality/RNG/Frame/FrameGenerator.cs @@ -13,7 +13,7 @@ public class FrameGenerator public readonly bool Safari3; public Frame GetFrame(uint seed, LeadRequired lead) => new Frame(seed, FrameType, RNG, lead); - public Frame GetFrame(uint seed, LeadRequired lead, uint esv) => new Frame(seed, FrameType, RNG, lead) {ESV = esv}; + public Frame GetFrame(uint seed, LeadRequired lead, uint esv, uint lvl) => new Frame(seed, FrameType, RNG, lead) {ESV = esv, RandLevel = lvl}; /// /// Gets the Search Criteria parameters necessary for generating and objects. diff --git a/PKHeX.Core/Legality/RNG/Frame/SlotRange.cs b/PKHeX.Core/Legality/RNG/Frame/SlotRange.cs index 88299442c..2d3742bf4 100644 --- a/PKHeX.Core/Legality/RNG/Frame/SlotRange.cs +++ b/PKHeX.Core/Legality/RNG/Frame/SlotRange.cs @@ -129,12 +129,21 @@ private static int CalcSlot(uint esv, Range[] ranges) return -1; } - private static Range[] Reverse(Range[] r) + public static int GetLevel(EncounterSlot slot, FrameType frameType, LeadRequired seed, uint lvlrand) { - var arr = new Range[r.Length]; - for (int i = 0; i < arr.Length; ++i) - arr[i] = r[r.Length - 1 - i]; - return arr; + if (seed == LeadRequired.PressureHustleSpirit) + return slot.LevelMax; + if (slot.LevelMin == slot.LevelMax) + return slot.LevelMin; + int delta = slot.LevelMax - slot.LevelMin + 1; + var adjust = (int)(lvlrand % delta); + + var lvl = slot.LevelMin + adjust; + return -1; // lvl; todo + } + public static bool GetIsEncounterable(EncounterSlot slot, FrameType frameType) + { + return true; // todo } } } diff --git a/Tests/PKHeX.Tests/PKM/PIDIVTests.cs b/Tests/PKHeX.Tests/PKM/PIDIVTests.cs index 51db6a480..3f7dd708d 100644 --- a/Tests/PKHeX.Tests/PKM/PIDIVTests.cs +++ b/Tests/PKHeX.Tests/PKM/PIDIVTests.cs @@ -192,8 +192,8 @@ public void PIDIVEncounterSlotTest() var type = SlotType.Grass; var slots = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 9 }; - Assert.IsTrue(slots.All(s => r2.Any(z => z.EncounterSlot(type) == s)), "Required slots not present."); - var slotsForType = r2.Where(z => !z.LevelSlotModified).Select(z => z.EncounterSlot(type)).Distinct().OrderBy(z => z); + Assert.IsTrue(slots.All(s => r2.Any(z => z.GetSlot(type) == s)), "Required slots not present."); + var slotsForType = r2.Where(z => !z.LevelSlotModified).Select(z => z.GetSlot(type)).Distinct().OrderBy(z => z); Assert.IsTrue(slotsForType.SequenceEqual(slots), "Unexpected slots present."); } // Test for Method H and K