PKHeX/PKHeX.Core/PersonalInfo/PersonalInfo.cs

118 lines
4 KiB
C#
Raw Normal View History

using System;
namespace PKHeX.Core;
/// <summary>
/// Stat/misc data for individual species or their associated alternate form data.
/// </summary>
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 16:15:27 -07:00
public abstract class PersonalInfo : IPersonalInfo
{
public abstract byte[] Write();
public abstract int HP { get; set; }
public abstract int ATK { get; set; }
public abstract int DEF { get; set; }
public abstract int SPE { get; set; }
public abstract int SPA { get; set; }
public abstract int SPD { get; set; }
public abstract int EV_HP { get; set; }
public abstract int EV_ATK { get; set; }
public abstract int EV_DEF { get; set; }
public abstract int EV_SPE { get; set; }
public abstract int EV_SPA { get; set; }
public abstract int EV_SPD { get; set; }
public abstract byte Type1 { get; set; }
public abstract byte Type2 { get; set; }
public abstract int EggGroup1 { get; set; }
public abstract int EggGroup2 { get; set; }
public abstract int CatchRate { get; set; }
public virtual int EvoStage { get; set; }
public abstract int Gender { get; set; }
public abstract int HatchCycles { get; set; }
public abstract int BaseFriendship { get; set; }
public abstract byte EXPGrowth { get; set; }
public abstract int GetIndexOfAbility(int abilityID);
public abstract int GetAbilityAtIndex(int abilityIndex);
public abstract int AbilityCount { get; }
public abstract int EscapeRate { get; set; }
public virtual byte FormCount { get; set; } = 1;
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 16:15:27 -07:00
public virtual int FormStatsIndex { get; set; }
public abstract int BaseEXP { get; set; }
public abstract int Color { get; set; }
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 16:15:27 -07:00
public virtual int Height { get; set; }
public virtual int Weight { get; set; }
/// <summary>
/// TM/HM learn compatibility flags for individual moves.
/// </summary>
public bool[] TMHM = Array.Empty<bool>();
/// <summary>
/// Grass-Fire-Water-Etc typed learn compatibility flags for individual moves.
/// </summary>
public bool[] TypeTutors = Array.Empty<bool>();
/// <summary>
/// Special tutor learn compatibility flags for individual moves.
/// </summary>
public bool[][] SpecialTutors = Array.Empty<bool[]>();
protected static bool[] GetBits(ReadOnlySpan<byte> data)
{
bool[] result = new bool[data.Length << 3];
for (int i = result.Length - 1; i >= 0; i--)
result[i] = ((data[i >> 3] >> (i & 7)) & 0x1) == 1;
return result;
}
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 16:15:27 -07:00
protected static void SetBits(ReadOnlySpan<bool> bits, Span<byte> data)
{
for (int i = bits.Length - 1; i >= 0; i--)
data[i>>3] |= (byte)(bits[i] ? 1 << (i&0x7) : 0);
}
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 16:15:27 -07:00
public void AddTMHM(ReadOnlySpan<byte> data) => TMHM = GetBits(data);
public void AddTypeTutors(ReadOnlySpan<byte> data) => TypeTutors = GetBits(data);
public int FormIndex(ushort species, byte form)
{
if (!HasForm(form))
return species;
return FormStatsIndex + form - 1;
}
public bool HasForm(byte form)
{
2022-08-30 15:00:45 -07:00
if (form == 0) // no form requested
return false;
if (FormStatsIndex <= 0) // no forms present
return false;
if (form >= FormCount) // beyond range of species' forms
return false;
return true;
}
public const int RatioMagicGenderless = 255;
public const int RatioMagicFemale = 254;
public const int RatioMagicMale = 0;
public static bool IsSingleGender(int gt) => (uint)(gt - 1) >= 253;
public bool IsDualGender => (uint)(Gender - 1) < 253;
public bool Genderless => Gender == RatioMagicGenderless;
public bool OnlyFemale => Gender == RatioMagicFemale;
public bool OnlyMale => Gender == RatioMagicMale;
public bool HasForms => FormCount > 1;
/// <summary>
/// Checks to see if the <see cref="PKM.Form"/> is valid within the <see cref="FormCount"/>
/// </summary>
/// <param name="form"></param>
public bool IsFormWithinRange(byte form)
{
if (form == 0)
return true;
return form < FormCount;
}
}