Split apart headbutt tree logic

precompute Index & make readonly when initializing as we use Index at
least once when initializing the treesarea
This commit is contained in:
Kurt 2018-08-28 15:10:04 -07:00
parent b5089658f7
commit f89d9ca323
3 changed files with 51 additions and 34 deletions

View File

@ -0,0 +1,19 @@
namespace PKHeX.Core
{
/// <summary>
/// Coordinate / Index Relationship for a Generation 2 Headbutt Tree
/// </summary>
internal sealed class TreeCoordinates
{
public readonly int X;
public readonly int Y;
public readonly int Index;
public TreeCoordinates(int x, int y)
{
X = x;
Y = y;
Index = ((X * Y) + X + Y) / 5 % 10;
}
}
}

View File

@ -0,0 +1,23 @@
namespace PKHeX.Core
{
/// <summary>
/// Indicates the Availability of the Generation 2 Headbutt Tree
/// </summary>
public enum TreeEncounterAvailable
{
/// <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
}
}

View File

@ -5,34 +5,9 @@ namespace PKHeX.Core
// Pokemon Crystal Headbutt tree encounters by trainer id, based on mechanics described in
// https://bulbapedia.bulbagarden.net/wiki/Headbutt_tree#Mechanics
/// <summary> Indicates the Availability of the Headbutt Tree </summary>
public enum TreeEncounterAvailable
{
/// <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
}
/// <summary> Coordinate / Index Relationship for a Headbutt Tree </summary>
internal sealed class TreeCoordinates
{
internal int X { get; }
internal int Y { get; }
internal int Index => ((X*Y) + X+Y) / 5 % 10;
public TreeCoordinates(int x, int y)
{
X = x;
Y = y;
}
}
/// <summary> Trees on a given map </summary>
/// <summary>
/// Generation 2 Headbutt Trees on a given map
/// </summary>
public sealed class TreesArea
{
private const int PivotCount = 10;
@ -40,13 +15,13 @@ public sealed class TreesArea
private static int[][] GenerateTrainersTreeIndex()
{
// A tree have a low encounter or moderate encounter base on the TID Pivot Index (TID % 10)
// Calculate for every Trainer Pivot Index the 5 tree index for low encounters
// A tree has a low encounter or moderate encounter base on the TID Pivot Index (TID % 10)
// For every Trainer Pivot Index, calculate the low encounter trees (total of 5)
int[][] TrainersIndex = new int[PivotCount][];
for (int i = 0; i < PivotCount; i++)
{
int[] ModerateEncounterTreeIndex = new int[5];
for (int j = 0; j <= 4; j++)
for (int j = 0; j < ModerateEncounterTreeIndex.Length; j++)
ModerateEncounterTreeIndex[j] = (i + j) % PivotCount;
TrainersIndex[i] = ModerateEncounterTreeIndex.OrderBy(x => x).ToArray();
}
@ -76,7 +51,7 @@ private TreesArea(byte[] entry)
private void ReadAreaRawData(byte[] entry)
{
// Coordinates of trees for every are obtained with the program G2Map
// Coordinates of trees were obtained with the program G2Map
// ValidTrees are those accessible by the player
Location = entry[0];
ValidTrees = new TreeCoordinates[entry[1]];
@ -131,9 +106,9 @@ private TreeEncounterAvailable GetAvailableLow(int[] moderate)
}
#if DEBUG
private void DumpLocation()
public void DumpLocation(string[] locationNames)
{
string loc = GameInfo.GetStrings("en").metGSC_00000[Location];
string loc = locationNames[Location];
System.Console.WriteLine($"Location: {loc}");
System.Console.WriteLine("Valid:");
foreach (var tree in ValidTrees)