PKHeX/PKHeX.Core/Legality/Encounters/EncounterStatic/EncounterStatic4Pokewalker.cs

126 lines
3.8 KiB
C#
Raw Normal View History

using System;
using static System.Buffers.Binary.BinaryPrimitives;
Refactoring: Move Source (Legality) (#3560) Rewrites a good amount of legality APIs pertaining to: * Legal moves that can be learned * Evolution chains & cross-generation paths * Memory validation with forgotten moves In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data. The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space. The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation. * `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game. * `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`). * Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
2022-08-03 23:15:27 +00:00
namespace PKHeX.Core;
/// <summary>
/// Generation 4 Pokéwalker Encounter
/// </summary>
/// <inheritdoc cref="EncounterStatic"/>
public sealed record EncounterStatic4Pokewalker : EncounterStatic
{
public override int Generation => 4;
Refactoring: Move Source (Legality) (#3560) Rewrites a good amount of legality APIs pertaining to: * Legal moves that can be learned * Evolution chains & cross-generation paths * Memory validation with forgotten moves In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data. The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space. The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation. * `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game. * `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`). * Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
2022-08-03 23:15:27 +00:00
public override EntityContext Context => EntityContext.Gen4;
public PokewalkerCourse4 Course { get; }
public EncounterStatic4Pokewalker(ReadOnlySpan<byte> data, PokewalkerCourse4 course) : base(GameVersion.HGSS)
{
Species = ReadUInt16LittleEndian(data);
Level = data[2];
Gender = (sbyte)data[3];
Course = course;
var move1 = ReadUInt16LittleEndian(data[0x4..]);
var move2 = ReadUInt16LittleEndian(data[0x6..]);
var move3 = ReadUInt16LittleEndian(data[0x8..]);
var move4 = ReadUInt16LittleEndian(data[0xA..]);
Moves = new(move1, move2, move3, move4);
// All obtained entities are in Poke Ball and have a met location of "PokeWalker"
Gift = true;
Location = Locations.PokeWalker4;
}
protected override bool IsMatchLocation(PKM pk)
{
if (pk.Format == 4)
return Location == pk.Met_Location;
return true; // transfer location verified later
}
protected override bool IsMatchLevel(PKM pk, EvoCriteria evo)
{
if (pk.Format != 4) // Met Level lost on PK4=>PK5
return Level <= evo.LevelMax;
return pk.Met_Level == Level;
}
protected override bool IsMatchPartial(PKM pk)
{
if (Gift && pk.Ball != Ball)
return true;
if (!IsCourseAvailable(pk.Language))
return true;
return base.IsMatchPartial(pk);
}
public bool IsCourseAvailable(int language) => Course switch
{
PokewalkerCourse4.Rally => language is (int)LanguageID.Japanese,
PokewalkerCourse4.Sightseeing => language is (int)LanguageID.Japanese or (int)LanguageID.Korean,
PokewalkerCourse4.AmityMeadow => language is (int)LanguageID.Japanese,
_ => true,
};
protected override void SetPINGA(PKM pk, EncounterCriteria criteria)
{
var pi = pk.PersonalInfo;
int gender = criteria.GetGender(Gender, pi);
int nature = (int)criteria.GetNature(Nature.Random);
// Cannot force an ability; nature-gender-trainerID only yield fixed PIDs.
// int ability = criteria.GetAbilityFromNumber(Ability, pi);
PIDGenerator.SetRandomPIDPokewalker(pk, nature, gender);
criteria.SetRandomIVs(pk);
}
public static EncounterStatic4Pokewalker[] GetAll(ReadOnlySpan<byte> data)
{
const int size = 0xC;
var count = data.Length / size;
System.Diagnostics.Debug.Assert(count == 6 * (int)PokewalkerCourse4.MAX_COUNT);
var result = new EncounterStatic4Pokewalker[count];
for (int i = 0; i < result.Length; i++)
{
var offset = i * size;
var slice = data[offset..];
var course = (PokewalkerCourse4)(i / 6);
result[i] = new(slice, course);
}
return result;
}
}
public enum PokewalkerCourse4 : byte
{
RefreshingField = 0,
NoisyForest = 1,
RuggedRoad = 2,
BeautifulBeach = 3,
SuburbanArea = 4,
DimCave = 5,
BlueLake = 6,
TownOutskirts = 7,
HoennField = 8,
WarmBeach = 9,
VolcanoPath = 10,
Treehouse = 11,
ScaryCave = 12,
SinnohField = 13,
IcyMountainRoad = 14,
BigForest = 15,
WhiteLake = 16,
StormyBeach = 17,
Resort = 18,
QuietCave = 19,
BeyondTheSea = 20,
NightSkysEdge = 21,
YellowForest = 22,
Rally = 23, // JPN Exclusive
Sightseeing = 24, // JPN/KOR Exclusive
WinnersPath = 25,
AmityMeadow = 26, // JPN Exclusive
MAX_COUNT,
}