2020-11-28 22:55:09 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2017-07-06 23:03:41 +00:00
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
|
{
|
2018-08-28 22:10:04 +00:00
|
|
|
|
/// <summary>
|
2020-11-28 22:55:09 +00:00
|
|
|
|
/// Generation 2 Headbutt Trees on a given <see cref="Location"/> map.
|
2018-08-28 22:10:04 +00:00
|
|
|
|
/// </summary>
|
2020-11-28 22:55:09 +00:00
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Pokemon Crystal Headbutt tree encounters by trainer id, based on mechanics described in
|
|
|
|
|
/// https://bulbapedia.bulbagarden.net/wiki/Headbutt_tree#Mechanics
|
|
|
|
|
/// </remarks>
|
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;
|
2020-11-28 22:55:09 +00:00
|
|
|
|
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
|
2018-08-26 23:29:52 +00:00
|
|
|
|
|
2020-11-28 22:55:09 +00:00
|
|
|
|
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()
|
2017-07-06 23:03:41 +00:00
|
|
|
|
{
|
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)
|
2020-11-28 22:55:09 +00:00
|
|
|
|
// For every Trainer Pivot Index, calculate the moderate encounter trees (total of 5)
|
|
|
|
|
byte[][] result = new byte[PivotCount][];
|
2017-08-01 00:09:16 +00:00
|
|
|
|
for (int i = 0; i < PivotCount; i++)
|
2017-07-06 23:03:41 +00:00
|
|
|
|
{
|
2020-11-28 22:55:09 +00:00
|
|
|
|
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;
|
2017-07-06 23:03:41 +00:00
|
|
|
|
}
|
2020-11-28 22:55:09 +00:00
|
|
|
|
return result;
|
2017-07-06 23:03:41 +00:00
|
|
|
|
}
|
2018-08-26 23:29:52 +00:00
|
|
|
|
|
2017-08-01 00:09:16 +00:00
|
|
|
|
private TreesArea(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];
|
2020-11-28 22:55:09 +00:00
|
|
|
|
|
|
|
|
|
var valid = new TreeCoordinates[entry[1]];
|
2017-07-06 23:03:41 +00:00
|
|
|
|
var ofs = 2;
|
2020-11-28 22:55:09 +00:00
|
|
|
|
for (int i = 0; i < valid.Length; i++, ofs += 2)
|
|
|
|
|
valid[i] = new TreeCoordinates(entry[ofs], entry[ofs + 1]);
|
2017-08-01 00:09:16 +00:00
|
|
|
|
|
|
|
|
|
// Invalid tress are trees that the player can not reach without cheating devices, like a tree beyond other trees
|
2020-11-28 22:55:09 +00:00
|
|
|
|
var invalid = new TreeCoordinates[entry[ofs]];
|
2018-05-12 19:28:48 +00:00
|
|
|
|
ofs++;
|
2020-11-28 22:55:09 +00:00
|
|
|
|
for (int i = 0; i < invalid.Length; i++, ofs += 2)
|
|
|
|
|
invalid[i] = new TreeCoordinates(entry[ofs], entry[ofs + 1]);
|
2017-07-06 23:03:41 +00:00
|
|
|
|
|
2020-11-28 22:55:09 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
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
|
2020-11-28 22:55:09 +00:00
|
|
|
|
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);
|
2017-07-06 23:03:41 +00:00
|
|
|
|
|
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
|
2020-11-28 22:55:09 +00:00
|
|
|
|
moderate = new TreeEncounterAvailable[PivotCount];
|
|
|
|
|
low = new TreeEncounterAvailable[PivotCount];
|
2017-08-01 00:09:16 +00:00
|
|
|
|
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];
|
2020-11-28 22:55:09 +00:00
|
|
|
|
moderate[i] = GetIsAvailableModerate(TrainerModerateTrees, TreeIndexValid, TreeIndexInvalid);
|
|
|
|
|
low[i] = GetIsAvailableLow(TrainerModerateTrees, TreeIndexValid, TreeIndexInvalid);
|
2017-07-06 23:03:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-01 00:09:16 +00:00
|
|
|
|
|
2020-11-28 22:55:09 +00:00
|
|
|
|
private static TreeEncounterAvailable GetIsAvailableModerate(byte[] moderate, byte[] valid, byte[] invalid)
|
2017-08-01 00:09:16 +00:00
|
|
|
|
{
|
2020-11-28 22:55:09 +00:00
|
|
|
|
if (valid.Any(moderate.Contains))
|
2017-08-01 00:09:16 +00:00
|
|
|
|
return TreeEncounterAvailable.ValidTree;
|
2020-11-28 22:55:09 +00:00
|
|
|
|
if (invalid.Any(moderate.Contains))
|
2017-08-01 00:09:16 +00:00
|
|
|
|
return TreeEncounterAvailable.InvalidTree;
|
|
|
|
|
return TreeEncounterAvailable.Impossible;
|
|
|
|
|
}
|
2018-08-26 23:29:52 +00:00
|
|
|
|
|
2020-11-28 22:55:09 +00:00
|
|
|
|
private static TreeEncounterAvailable GetIsAvailableLow(byte[] moderate, byte[] valid, byte[] invalid)
|
2017-08-01 00:09:16 +00:00
|
|
|
|
{
|
2020-11-28 22:55:09 +00:00
|
|
|
|
if (valid.Except(moderate).Any())
|
2017-08-01 00:09:16 +00:00
|
|
|
|
return TreeEncounterAvailable.ValidTree;
|
2020-11-28 22:55:09 +00:00
|
|
|
|
if (invalid.Except(moderate).Any())
|
2017-08-01 00:09:16 +00:00
|
|
|
|
return TreeEncounterAvailable.InvalidTree;
|
|
|
|
|
return TreeEncounterAvailable.Impossible;
|
|
|
|
|
}
|
2018-05-19 04:35:57 +00:00
|
|
|
|
|
2020-11-28 22:55:09 +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];
|
2020-11-28 22:55:09 +00:00
|
|
|
|
Console.WriteLine($"Location: {loc}");
|
|
|
|
|
Console.WriteLine("Valid:");
|
2018-05-19 04:35:57 +00:00
|
|
|
|
foreach (var tree in ValidTrees)
|
2020-11-28 22:55:09 +00:00
|
|
|
|
Console.WriteLine(tree);
|
|
|
|
|
Console.WriteLine("Invalid:");
|
2018-05-19 04:35:57 +00:00
|
|
|
|
foreach (var tree in InvalidTrees)
|
2020-11-28 22:55:09 +00:00
|
|
|
|
Console.WriteLine(tree);
|
|
|
|
|
Console.WriteLine("===");
|
2018-05-19 04:35:57 +00:00
|
|
|
|
}
|
2020-11-28 22:55:09 +00:00
|
|
|
|
#endif
|
2017-07-06 23:03:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|