mirror of
https://github.com/kwsch/PKHeX.git
synced 2026-08-25 23:54:33 -05:00
Simplify headbutt tree pivot check
See the EncounterSlotDumper with the memoization logic & json tree listing. Simplifies things a lot, and improves checking speed. Unreachable trees are now treated the same as no-trees maps. ez 1.5KB reduction in file size :P
This commit is contained in:
@@ -1,27 +0,0 @@
|
||||
namespace PKHeX.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Coordinate / Index Relationship for a Generation 2 Headbutt Tree
|
||||
/// </summary>
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
namespace PKHeX.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Indicates the Availability of the Generation 2 Headbutt Tree
|
||||
/// </summary>
|
||||
public enum TreeEncounterAvailable : byte
|
||||
{
|
||||
/// <summary>
|
||||
/// Encounter is possible a reachable tree
|
||||
/// </summary>
|
||||
ValidTree,
|
||||
|
||||
/// <summary>
|
||||
/// Encounter is only possible a tree reachable only with walk-through walls cheats
|
||||
/// </summary>
|
||||
InvalidTree,
|
||||
|
||||
/// <summary>
|
||||
/// Encounter is not possible in any tree
|
||||
/// </summary>
|
||||
Impossible
|
||||
}
|
||||
}
|
||||
@@ -1,130 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace PKHeX.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Generation 2 Headbutt Trees on a given <see cref="Location"/> map.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Pokemon Crystal Headbutt tree encounters by trainer id, based on mechanics described in
|
||||
/// https://bulbapedia.bulbagarden.net/wiki/Headbutt_tree#Mechanics
|
||||
/// </remarks>
|
||||
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<TreeEncounterAvailable> 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
|
||||
}
|
||||
}
|
||||
@@ -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<int, int> 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;
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -114,7 +114,6 @@
|
||||
<None Remove="Resources\byte\personal_xy" />
|
||||
<None Remove="Resources\byte\personal_y" />
|
||||
<None Remove="Resources\byte\pgf.pkl" />
|
||||
<None Remove="Resources\byte\trees_h_c.pkl" />
|
||||
<None Remove="Resources\byte\tutors_g3.pkl" />
|
||||
<None Remove="Resources\byte\tutors_g4.pkl" />
|
||||
<None Remove="Resources\byte\wc6.pkl" />
|
||||
@@ -965,7 +964,6 @@
|
||||
<EmbeddedResource Include="Resources\byte\personal_xy" />
|
||||
<EmbeddedResource Include="Resources\byte\personal_y" />
|
||||
<EmbeddedResource Include="Resources\byte\pgf.pkl" />
|
||||
<EmbeddedResource Include="Resources\byte\trees_h_c.pkl" />
|
||||
<EmbeddedResource Include="Resources\byte\tutors_g3.pkl" />
|
||||
<EmbeddedResource Include="Resources\byte\tutors_g4.pkl" />
|
||||
<EmbeddedResource Include="Resources\byte\wc6.pkl" />
|
||||
|
||||
Reference in New Issue
Block a user