2017-07-06 23:03:41 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
|
{
|
2017-07-07 11:53:21 +00:00
|
|
|
|
// Pokemon Crystal Headbutt tree encounters by trainer id, based on mechanics described in
|
2017-07-06 23:03:41 +00:00
|
|
|
|
// https://bulbapedia.bulbagarden.net/wiki/Headbutt_tree#Mechanics
|
|
|
|
|
|
2018-08-28 22:10:04 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Generation 2 Headbutt Trees on a given map
|
|
|
|
|
/// </summary>
|
2018-08-26 23:29:52 +00:00
|
|
|
|
public sealed class TreesArea
|
2017-08-01 00:09:16 +00:00
|
|
|
|
{
|
|
|
|
|
private const int PivotCount = 10;
|
2018-08-26 23:29:52 +00:00
|
|
|
|
private static readonly int[][] TrainerModerateTreeIndex = GenerateTrainersTreeIndex();
|
|
|
|
|
|
2017-07-06 23:03:41 +00:00
|
|
|
|
private static int[][] GenerateTrainersTreeIndex()
|
|
|
|
|
{
|
2018-08-28 22:10:04 +00:00
|
|
|
|
// 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)
|
2017-08-01 00:09:16 +00:00
|
|
|
|
int[][] TrainersIndex = new int[PivotCount][];
|
|
|
|
|
for (int i = 0; i < PivotCount; i++)
|
2017-07-06 23:03:41 +00:00
|
|
|
|
{
|
|
|
|
|
int[] ModerateEncounterTreeIndex = new int[5];
|
2018-08-28 22:10:04 +00:00
|
|
|
|
for (int j = 0; j < ModerateEncounterTreeIndex.Length; j++)
|
2017-08-01 00:09:16 +00:00
|
|
|
|
ModerateEncounterTreeIndex[j] = (i + j) % PivotCount;
|
|
|
|
|
TrainersIndex[i] = ModerateEncounterTreeIndex.OrderBy(x => x).ToArray();
|
2017-07-06 23:03:41 +00:00
|
|
|
|
}
|
|
|
|
|
return TrainersIndex;
|
|
|
|
|
}
|
2018-08-26 23:29:52 +00:00
|
|
|
|
|
2017-08-01 00:09:16 +00:00
|
|
|
|
internal static TreesArea[] GetArray(byte[][] entries) => entries.Select(z => new TreesArea(z)).ToArray();
|
2017-07-06 23:03:41 +00:00
|
|
|
|
|
2017-08-01 00:09:16 +00:00
|
|
|
|
public int Location { get; private set; }
|
|
|
|
|
private TreeEncounterAvailable[] TrainerModerateEncounterTree { get; set; }
|
|
|
|
|
private TreeEncounterAvailable[] TrainerLowEncounterTree { get; set; }
|
|
|
|
|
private int[] ValidTreeIndex { get; set; }
|
|
|
|
|
private int[] InvalidTreeIndex { get; set; }
|
|
|
|
|
private TreeCoordinates[] ValidTrees { get; set; }
|
|
|
|
|
private TreeCoordinates[] InvalidTrees { get; set; }
|
|
|
|
|
|
2018-08-26 23:29:52 +00:00
|
|
|
|
public TreeEncounterAvailable[] GetTrees(SlotType t) => t == SlotType.Headbutt
|
|
|
|
|
? TrainerModerateEncounterTree
|
|
|
|
|
: TrainerLowEncounterTree;
|
|
|
|
|
|
2017-08-01 00:09:16 +00:00
|
|
|
|
private TreesArea(byte[] entry)
|
|
|
|
|
{
|
|
|
|
|
ReadAreaRawData(entry);
|
|
|
|
|
GenerateAreaTreeIndex();
|
|
|
|
|
GenerateAreaTrainerEncounters();
|
2017-07-06 23:03:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-01 00:09:16 +00:00
|
|
|
|
private void ReadAreaRawData(byte[] entry)
|
2017-07-06 23:03:41 +00:00
|
|
|
|
{
|
2018-08-28 22:10:04 +00:00
|
|
|
|
// Coordinates of trees were obtained with the program G2Map
|
2017-07-07 11:53:21 +00:00
|
|
|
|
// ValidTrees are those accessible by the player
|
2017-08-01 00:09:16 +00:00
|
|
|
|
Location = entry[0];
|
|
|
|
|
ValidTrees = new TreeCoordinates[entry[1]];
|
2017-07-06 23:03:41 +00:00
|
|
|
|
var ofs = 2;
|
2017-08-01 00:09:16 +00:00
|
|
|
|
for (int i = 0; i < ValidTrees.Length; i++, ofs += 2)
|
|
|
|
|
ValidTrees[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
|
|
|
|
|
InvalidTrees = new TreeCoordinates[entry[ofs]];
|
2018-05-12 19:28:48 +00:00
|
|
|
|
ofs++;
|
2017-08-01 00:09:16 +00:00
|
|
|
|
for (int i = 0; i < InvalidTrees.Length; i++, ofs += 2)
|
|
|
|
|
InvalidTrees[i] = new TreeCoordinates(entry[ofs], entry[ofs + 1]);
|
2017-07-06 23:03:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void GenerateAreaTreeIndex()
|
|
|
|
|
{
|
2017-08-01 00:09:16 +00:00
|
|
|
|
// 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
|
2017-07-06 23:03:41 +00:00
|
|
|
|
ValidTreeIndex = ValidTrees.Select(t => t.Index).Distinct().OrderBy(i => i).ToArray();
|
|
|
|
|
InvalidTreeIndex = InvalidTrees.Select(t => t.Index).Distinct().OrderBy(i => i).Except(ValidTreeIndex).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void GenerateAreaTrainerEncounters()
|
|
|
|
|
{
|
2017-08-01 00:09:16 +00:00
|
|
|
|
// Check for every trainer pivot index if there are trees with moderate encounter and low encounter available in the area
|
|
|
|
|
TrainerModerateEncounterTree = new TreeEncounterAvailable[PivotCount];
|
|
|
|
|
TrainerLowEncounterTree = new TreeEncounterAvailable[PivotCount];
|
|
|
|
|
for (int i = 0; i < PivotCount; i++)
|
2017-07-06 23:03:41 +00:00
|
|
|
|
{
|
2017-08-01 00:09:16 +00:00
|
|
|
|
var TrainerModerateTrees = TrainerModerateTreeIndex[i];
|
|
|
|
|
TrainerModerateEncounterTree[i] = GetAvailableModerate(TrainerModerateTrees);
|
|
|
|
|
TrainerLowEncounterTree[i] = GetAvailableLow(TrainerModerateTrees);
|
2017-07-06 23:03:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-01 00:09:16 +00:00
|
|
|
|
|
|
|
|
|
private TreeEncounterAvailable GetAvailableModerate(int[] moderate)
|
|
|
|
|
{
|
|
|
|
|
if (ValidTreeIndex.Any(moderate.Contains))
|
|
|
|
|
return TreeEncounterAvailable.ValidTree;
|
|
|
|
|
if (InvalidTreeIndex.Any(moderate.Contains))
|
|
|
|
|
return TreeEncounterAvailable.InvalidTree;
|
|
|
|
|
return TreeEncounterAvailable.Impossible;
|
|
|
|
|
}
|
2018-08-26 23:29:52 +00:00
|
|
|
|
|
2017-08-01 00:09:16 +00:00
|
|
|
|
private TreeEncounterAvailable GetAvailableLow(int[] moderate)
|
|
|
|
|
{
|
|
|
|
|
if (ValidTreeIndex.Except(moderate).Any())
|
|
|
|
|
return TreeEncounterAvailable.ValidTree;
|
|
|
|
|
if (InvalidTreeIndex.Except(moderate).Any())
|
|
|
|
|
return TreeEncounterAvailable.InvalidTree;
|
|
|
|
|
return TreeEncounterAvailable.Impossible;
|
|
|
|
|
}
|
2018-05-19 04:35:57 +00:00
|
|
|
|
|
|
|
|
|
#if DEBUG
|
2018-08-28 22:10:04 +00:00
|
|
|
|
public void DumpLocation(string[] locationNames)
|
2018-05-19 04:35:57 +00:00
|
|
|
|
{
|
2018-08-28 22:10:04 +00:00
|
|
|
|
string loc = locationNames[Location];
|
2018-05-19 04:35:57 +00:00
|
|
|
|
System.Console.WriteLine($"Location: {loc}");
|
|
|
|
|
System.Console.WriteLine("Valid:");
|
|
|
|
|
foreach (var tree in ValidTrees)
|
|
|
|
|
System.Console.WriteLine($"{tree.Index} @ ({tree.X:D2},{tree.Y:D2})");
|
|
|
|
|
System.Console.WriteLine("Invalid:");
|
|
|
|
|
foreach (var tree in InvalidTrees)
|
|
|
|
|
System.Console.WriteLine($"{tree.Index} @ ({tree.X:D2},{tree.Y:D2})");
|
|
|
|
|
System.Console.WriteLine("===");
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2017-07-06 23:03:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|