diff --git a/PKHeX.Core/Legality/Areas/TreeCoordinates.cs b/PKHeX.Core/Legality/Areas/TreeCoordinates.cs deleted file mode 100644 index b6e01ef04..000000000 --- a/PKHeX.Core/Legality/Areas/TreeCoordinates.cs +++ /dev/null @@ -1,27 +0,0 @@ -namespace PKHeX.Core -{ - /// - /// Coordinate / Index Relationship for a Generation 2 Headbutt Tree - /// - internal readonly struct TreeCoordinates - { -#if DEBUG - private readonly int X; - private readonly int Y; -#endif - public readonly byte Index; - - public TreeCoordinates(in int x, in int y) - { -#if DEBUG - X = x; - Y = y; -#endif - Index = (byte)(((x * y) + x + y) / 5 % 10); - } - -#if DEBUG - public override string ToString() => $"{Index} @ ({X:D2},{Y:D2})"; -#endif - } -} diff --git a/PKHeX.Core/Legality/Areas/TreeEncounterAvailable.cs b/PKHeX.Core/Legality/Areas/TreeEncounterAvailable.cs deleted file mode 100644 index a4046ca82..000000000 --- a/PKHeX.Core/Legality/Areas/TreeEncounterAvailable.cs +++ /dev/null @@ -1,23 +0,0 @@ -namespace PKHeX.Core -{ - /// - /// Indicates the Availability of the Generation 2 Headbutt Tree - /// - public enum TreeEncounterAvailable : byte - { - /// - /// Encounter is possible a reachable tree - /// - ValidTree, - - /// - /// Encounter is only possible a tree reachable only with walk-through walls cheats - /// - InvalidTree, - - /// - /// Encounter is not possible in any tree - /// - Impossible - } -} \ No newline at end of file diff --git a/PKHeX.Core/Legality/Areas/TreesArea.cs b/PKHeX.Core/Legality/Areas/TreesArea.cs deleted file mode 100644 index ffdadeea8..000000000 --- a/PKHeX.Core/Legality/Areas/TreesArea.cs +++ /dev/null @@ -1,130 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace PKHeX.Core -{ - /// - /// Generation 2 Headbutt Trees on a given map. - /// - /// - /// Pokemon Crystal Headbutt tree encounters by trainer id, based on mechanics described in - /// https://bulbapedia.bulbagarden.net/wiki/Headbutt_tree#Mechanics - /// - public sealed class TreesArea - { - private const int PivotCount = 10; - private const int ModerateTreeCount = 5; - - public readonly int Location; - private readonly TreeEncounterAvailable[] PivotModerate; - private readonly TreeEncounterAvailable[] PivotLow; - -#if DEBUG - private readonly TreeCoordinates[] ValidTrees; - private readonly TreeCoordinates[] InvalidTrees; -#endif - - public IReadOnlyList GetTrees(SlotType t) => t == SlotType.Headbutt - ? PivotModerate - : PivotLow; - - private static readonly byte[][] TrainerModerateTreeIndex = GenerateModerateTreeIndex(); - internal static TreesArea[] GetArray(byte[][] entries) => entries.Select(z => new TreesArea(z)).ToArray(); - - private static byte[][] GenerateModerateTreeIndex() - { - // A tree has a low encounter or moderate encounter base on the TID Pivot Index (TID % 10) - // For every Trainer Pivot Index, calculate the moderate encounter trees (total of 5) - byte[][] result = new byte[PivotCount][]; - for (int i = 0; i < PivotCount; i++) - { - var moderate = new byte[ModerateTreeCount]; - for (int j = 0; j < moderate.Length; j++) - moderate[j] = (byte)((i + j) % PivotCount); - Array.Sort(moderate); - result[i] = moderate; - } - return result; - } - - private TreesArea(byte[] entry) - { - // Coordinates of trees were obtained with the program G2Map - // ValidTrees are those accessible by the player - Location = entry[0]; - - var valid = new TreeCoordinates[entry[1]]; - var ofs = 2; - for (int i = 0; i < valid.Length; i++, ofs += 2) - valid[i] = new TreeCoordinates(entry[ofs], entry[ofs + 1]); - - // Invalid tress are trees that the player can not reach without cheating devices, like a tree beyond other trees - var invalid = new TreeCoordinates[entry[ofs]]; - ofs++; - for (int i = 0; i < invalid.Length; i++, ofs += 2) - invalid[i] = new TreeCoordinates(entry[ofs], entry[ofs + 1]); - - CreatePivotLists(valid, invalid, out PivotModerate, out PivotLow); - -#if DEBUG - ValidTrees = valid; - InvalidTrees = invalid; -#endif - } - - private static void CreatePivotLists(TreeCoordinates[] valid, TreeCoordinates[] invalid, out TreeEncounterAvailable[] moderate, out TreeEncounterAvailable[] low) - { - // For legality purposes, only the tree index is needed. - // Group the trees data by their index; trees that share indexes are indistinguishable from one another - var TreeIndexValid = valid.Select(t => t.Index).Distinct().ToArray(); - var TreeIndexInvalid = invalid.Select(t => t.Index).Distinct().Except(TreeIndexValid).ToArray(); - - Array.Sort(TreeIndexValid); - Array.Sort(TreeIndexInvalid); - - // Check for every trainer pivot index if there are trees with moderate encounter and low encounter available in the area - moderate = new TreeEncounterAvailable[PivotCount]; - low = new TreeEncounterAvailable[PivotCount]; - for (int i = 0; i < PivotCount; i++) - { - var TrainerModerateTrees = TrainerModerateTreeIndex[i]; - moderate[i] = GetIsAvailableModerate(TrainerModerateTrees, TreeIndexValid, TreeIndexInvalid); - low[i] = GetIsAvailableLow(TrainerModerateTrees, TreeIndexValid, TreeIndexInvalid); - } - } - - private static TreeEncounterAvailable GetIsAvailableModerate(byte[] moderate, byte[] valid, byte[] invalid) - { - if (valid.Any(moderate.Contains)) - return TreeEncounterAvailable.ValidTree; - if (invalid.Any(moderate.Contains)) - return TreeEncounterAvailable.InvalidTree; - return TreeEncounterAvailable.Impossible; - } - - private static TreeEncounterAvailable GetIsAvailableLow(byte[] moderate, byte[] valid, byte[] invalid) - { - if (valid.Except(moderate).Any()) - return TreeEncounterAvailable.ValidTree; - if (invalid.Except(moderate).Any()) - return TreeEncounterAvailable.InvalidTree; - return TreeEncounterAvailable.Impossible; - } - -#if DEBUG - public void DumpLocation(string[] locationNames) - { - string loc = locationNames[Location]; - Console.WriteLine($"Location: {loc}"); - Console.WriteLine("Valid:"); - foreach (var tree in ValidTrees) - Console.WriteLine(tree); - Console.WriteLine("Invalid:"); - foreach (var tree in InvalidTrees) - Console.WriteLine(tree); - Console.WriteLine("==="); - } -#endif - } -} diff --git a/PKHeX.Core/Legality/Encounters/Data/Encounters2.cs b/PKHeX.Core/Legality/Encounters/Data/Encounters2.cs index 2d5394e06..d671a278c 100644 --- a/PKHeX.Core/Legality/Encounters/Data/Encounters2.cs +++ b/PKHeX.Core/Legality/Encounters/Data/Encounters2.cs @@ -1,4 +1,4 @@ -using System; +using System.Collections.Generic; using System.Linq; using static PKHeX.Core.EncounterUtil; using static PKHeX.Core.GameVersion; @@ -17,7 +17,6 @@ internal static class Encounters2 internal static readonly EncounterArea2[] SlotsGS = ArrayUtil.ConcatAll(SlotsG, SlotsS); internal static readonly EncounterArea2[] SlotsGSC = ArrayUtil.ConcatAll(SlotsGS, SlotsC); - private static readonly TreesArea[] HeadbuttTreesC = TreesArea.GetArray(BinLinker.Unpack(Util.GetBinaryResource("trees_h_c.pkl"), "ch")); private static EncounterArea2[] Get(string name, string ident, GameVersion game) => EncounterArea2.GetAreas(BinLinker.Unpack(Util.GetBinaryResource($"encounter_{name}.pkl"), ident), game); @@ -129,17 +128,43 @@ internal static class Encounters2 private const string tradeGSC = "tradegsc"; private static readonly string[][] TradeGift_GSC_OTs = Util.GetLanguageStrings8(tradeGSC); - internal static TreeEncounterAvailable GetGSCHeadbuttAvailability(EncounterSlot encounter, int trainerID) + internal static bool IsTreeAvailable(EncounterSlot encounter, int trainerID) { - var area = Array.Find(HeadbuttTreesC, a => a.Location == encounter.Location); - if (area == null) // Failsafe, every area with headbutt encounters has a tree area - return TreeEncounterAvailable.Impossible; + if (!Trees.TryGetValue(encounter.Location, out var permissions)) + return false; - var table = area.GetTrees(encounter.Area.Type); - var trainerpivot = trainerID % 10; - return table[trainerpivot]; + var pivot = trainerID % 10; + var type = encounter.Area.Type; + return type switch + { + SlotType.Headbutt => (permissions & (1 << pivot)) != 0, + /*special*/_ => (permissions & (1 << (pivot + 12))) != 0, + }; } + private static readonly Dictionary Trees = new() + { + {02, 0x3FF_3FF}, // Route 29 + {04, 0x39D_3FF}, // Route 30 + {05, 0x13D_3FF}, // Route 31 + {08, 0x2FF_3FF}, // Route 32 + {11, 0x009_3FF}, // Route 33 + {12, 0x3DF_3FF}, // Azalea Town + {14, 0x3FF_3FF}, // Ilex Forest + {15, 0x100_2FF}, // Route 34 + {18, 0x099_3FF}, // Route 35 + {20, 0x3FF_3FF}, // Route 36 + {21, 0x2F6_3FF}, // Route 37 + {25, 0x3FF_3FF}, // Route 38 + {26, 0x188_3FF}, // Route 39 + {34, 0x3FE_3FF}, // Route 42 + {37, 0x3B7_3FF}, // Route 43 + {38, 0x3FF_3FF}, // Lake of Rage + {39, 0x2FF_3FF}, // Route 44 + {91, 0x300_3FF}, // Route 26 + {92, 0x1FE_3FF}, // Route 27 + }; + internal static readonly EncounterStatic2[] StaticGSC = Encounter_GSC; internal static readonly EncounterStatic2[] StaticGS = Encounter_GS; internal static readonly EncounterStatic2[] StaticC = Encounter_C; diff --git a/PKHeX.Core/Legality/Encounters/Generator/EncounterMovesetGenerator.cs b/PKHeX.Core/Legality/Encounters/Generator/EncounterMovesetGenerator.cs index a302a095d..132ee3aff 100644 --- a/PKHeX.Core/Legality/Encounters/Generator/EncounterMovesetGenerator.cs +++ b/PKHeX.Core/Legality/Encounters/Generator/EncounterMovesetGenerator.cs @@ -365,8 +365,8 @@ private static bool IsUnobtainable(this EncounterSlot slot, ITrainerID pk) switch (slot.Generation) { case 2: - if (slot.Area.Type == SlotType.Headbutt) // Unreachable Headbutt Trees. - return Encounters2.GetGSCHeadbuttAvailability(slot, pk.TID) != TreeEncounterAvailable.ValidTree; + if ((slot.Area.Type & SlotType.Headbutt) != 0) // Unreachable Headbutt Trees. + return !Encounters2.IsTreeAvailable(slot, pk.TID); break; case 4: if (slot.Location == 193 && slot.Area.Type == SlotType.Surf) // Johto Route 45 surfing encounter. Unreachable Water tiles. diff --git a/PKHeX.Core/Legality/Encounters/Verifiers/EncounterVerifier.cs b/PKHeX.Core/Legality/Encounters/Verifiers/EncounterVerifier.cs index 68a9bb92c..30f9659c5 100644 --- a/PKHeX.Core/Legality/Encounters/Verifiers/EncounterVerifier.cs +++ b/PKHeX.Core/Legality/Encounters/Verifiers/EncounterVerifier.cs @@ -76,13 +76,9 @@ private static CheckResult VerifyWildEncounterCrystal(PKM pkm, EncounterSlot enc private static CheckResult VerifyWildEncounterCrystalHeadbutt(ITrainerID tr, EncounterSlot encounter) { - var tree = Encounters2.GetGSCHeadbuttAvailability(encounter, tr.TID); - return tree switch - { - TreeEncounterAvailable.ValidTree => new CheckResult(Severity.Valid, LG2TreeID, CheckIdentifier.Encounter), - TreeEncounterAvailable.InvalidTree => new CheckResult(Severity.Invalid, LG2InvalidTileTreeID, CheckIdentifier.Encounter), - _ => new CheckResult(Severity.Invalid, LG2InvalidTileTreeNotFound, CheckIdentifier.Encounter) - }; + return Encounters2.IsTreeAvailable(encounter, tr.TID) + ? new CheckResult(Severity.Valid, LG2TreeID, CheckIdentifier.Encounter) + : new CheckResult(Severity.Invalid, LG2InvalidTileTreeNotFound, CheckIdentifier.Encounter); } // Eggs diff --git a/PKHeX.Core/PKHeX.Core.csproj b/PKHeX.Core/PKHeX.Core.csproj index ae196e5df..097f1ced8 100644 --- a/PKHeX.Core/PKHeX.Core.csproj +++ b/PKHeX.Core/PKHeX.Core.csproj @@ -114,7 +114,6 @@ - @@ -965,7 +964,6 @@ -