PKHeX/PKHeX.Core/Saves/Substructures/PokeDex/Zukan4.cs

538 lines
17 KiB
C#
Raw Normal View History

using System;
using static System.Buffers.Binary.BinaryPrimitives;
2021-02-21 17:59:10 +00:00
namespace PKHeX.Core;
/// <summary>
/// Pokédex structure used by <see cref="SAV4"/> games.
/// </summary>
public sealed class Zukan4 : ZukanBase<SAV4>
2021-02-21 17:59:10 +00:00
{
private readonly byte[] Data;
private readonly int Offset;
// General structure: u32 magic, 4*bitflags, u32 spinda, form flags, language flags, more form flags, upgrade flags
/* 4 BitRegions with 0x40*8 bits
* Region 0: Caught (Captured/Owned) flags
* Region 1: Seen flags
* Region 2: First Seen Gender
* Region 3: Second Seen Gender
* When setting a newly seen species (first time), we set the gender bit to both First and Second regions.
* When setting an already-seen species, we set the Second region bit if the now-seen gender-bit is not equal to the first-seen bit.
* 4 possible states: 00, 01, 10, 11
* 00 - 1Seen: Male Only
* 01 - 2Seen: Male First, Female Second
* 10 - 2Seen: Female First, Male Second
* 11 - 1Seen: Female Only
* assuming the species is seen, (bit1 ^ bit2) + 1 = genders in dex
*/
public const string GENDERLESS = "Genderless";
public const string MALE = "Male";
public const string FEMALE = "Female";
private const int SIZE_REGION = 0x40;
private const int COUNT_REGION = 4;
private const int OFS_SPINDA = sizeof(uint) + (COUNT_REGION * SIZE_REGION);
private const int OFS_FORM1 = OFS_SPINDA + sizeof(uint);
private bool HGSS => SAV is SAV4HGSS;
private bool DP => SAV is SAV4DP;
public Zukan4(SAV4 sav, int offset) : base(sav, offset)
{
Data = sav.General;
Offset = offset;
}
2021-02-21 17:59:10 +00:00
public uint Magic { get => ReadUInt32LittleEndian(Data.AsSpan(Offset)); set => WriteUInt32LittleEndian(Data.AsSpan(Offset), value); }
2021-02-21 17:59:10 +00:00
public override bool GetCaught(ushort species) => GetRegionFlag(0, species - 1);
public override bool GetSeen(ushort species) => GetRegionFlag(1, species - 1);
public int GetSeenGenderFirst(ushort species) => GetRegionFlag(2, species - 1) ? 1 : 0;
public int GetSeenGenderSecond(ushort species) => GetRegionFlag(3, species - 1) ? 1 : 0;
public bool GetSeenSingleGender(ushort species) => GetSeenGenderFirst(species) == GetSeenGenderSecond(species);
2021-02-21 17:59:10 +00:00
private bool GetRegionFlag(int region, int index)
{
var ofs = Offset + 4 + (region * SIZE_REGION) + (index >> 3);
return FlagUtil.GetFlag(Data, ofs, index);
}
2021-02-21 17:59:10 +00:00
public void SetCaught(ushort species, bool value = true) => SetRegionFlag(0, species - 1, value);
public void SetSeen(ushort species, bool value = true) => SetRegionFlag(1, species - 1, value);
public void SetSeenGenderFirst(ushort species, int value = 0) => SetRegionFlag(2, species - 1, value == 1);
public void SetSeenGenderSecond(ushort species, int value = 0) => SetRegionFlag(3, species - 1, value == 1);
2021-02-21 17:59:10 +00:00
private void SetRegionFlag(int region, int index, bool value)
{
var ofs = Offset + 4 + (region * SIZE_REGION) + (index >> 3);
FlagUtil.SetFlag(Data, ofs, index, value);
}
2021-02-21 17:59:10 +00:00
public uint SpindaPID { get => ReadUInt32LittleEndian(Data.AsSpan(Offset + OFS_SPINDA)); set => WriteUInt32LittleEndian(Data.AsSpan(Offset), value); }
2021-02-21 17:59:10 +00:00
public static string[] GetFormNames4Dex(ushort species)
{
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
string[] formNames = FormConverter.GetFormList(species, GameInfo.Strings.types, GameInfo.Strings.forms, Array.Empty<string>(), EntityContext.Gen4);
if (species == (int)Species.Pichu)
formNames = new[] { MALE, FEMALE, formNames[1] }; // Spiky
return formNames;
}
2021-02-21 17:59:10 +00:00
2022-09-05 05:23:35 +00:00
public const byte FORM_NONE = byte.MaxValue;
public byte[] GetForms(ushort species)
{
const int brSize = 0x40;
if (species == (int)Species.Deoxys)
{
uint val = (uint)(Data[Offset + 0x4 + (1 * brSize) - 1] | (Data[Offset + 0x4 + (2 * brSize) - 1] << 8));
return GetDexFormValues(val, 4, 4);
}
int FormOffset1 = Offset + 4 + (4 * brSize) + 4;
switch (species)
{
case (int)Species.Shellos: // Shellos
return GetDexFormValues(Data[FormOffset1 + 0], 1, 2);
case (int)Species.Gastrodon: // Gastrodon
return GetDexFormValues(Data[FormOffset1 + 1], 1, 2);
case (int)Species.Burmy: // Burmy
return GetDexFormValues(Data[FormOffset1 + 2], 2, 3);
case (int)Species.Wormadam: // Wormadam
return GetDexFormValues(Data[FormOffset1 + 3], 2, 3);
case (int)Species.Unown: // Unown
2022-09-05 05:23:35 +00:00
return Data.AsSpan(FormOffset1 + 4, 0x1C).ToArray();
}
if (DP)
2022-09-05 05:23:35 +00:00
return Array.Empty<byte>();
int PokeDexLanguageFlags = FormOffset1 + (HGSS ? 0x3C : 0x20);
int FormOffset2 = PokeDexLanguageFlags + 0x1F4;
return species switch
{
(int)Species.Rotom => GetDexFormValues(ReadUInt32LittleEndian(Data.AsSpan(FormOffset2)), 3, 6),
(int)Species.Shaymin => GetDexFormValues(Data[FormOffset2 + 4], 1, 2),
(int)Species.Giratina => GetDexFormValues(Data[FormOffset2 + 5], 1, 2),
(int)Species.Pichu when HGSS => GetDexFormValues(Data[FormOffset2 + 6], 2, 3),
2022-09-05 05:23:35 +00:00
_ => Array.Empty<byte>(),
};
}
2022-09-05 05:23:35 +00:00
public void SetForms(ushort species, ReadOnlySpan<byte> forms)
{
const int brSize = 0x40;
switch (species)
2021-02-21 17:59:10 +00:00
{
case (int)Species.Deoxys: // Deoxys
uint newval = SetDexFormValues(forms, 4, 4);
Data[Offset + 0x4 + (1 * brSize) - 1] = (byte)(newval & 0xFF);
Data[Offset + 0x4 + (2 * brSize) - 1] = (byte)((newval >> 8) & 0xFF);
break;
2021-02-21 17:59:10 +00:00
}
int FormOffset1 = Offset + OFS_FORM1;
switch (species)
2021-02-21 17:59:10 +00:00
{
case (int)Species.Shellos: // Shellos
Data[FormOffset1 + 0] = (byte)SetDexFormValues(forms, 1, 2);
return;
case (int)Species.Gastrodon: // Gastrodon
Data[FormOffset1 + 1] = (byte)SetDexFormValues(forms, 1, 2);
return;
case (int)Species.Burmy: // Burmy
Data[FormOffset1 + 2] = (byte)SetDexFormValues(forms, 2, 3);
return;
case (int)Species.Wormadam: // Wormadam
Data[FormOffset1 + 3] = (byte)SetDexFormValues(forms, 2, 3);
return;
case (int)Species.Unown: // Unown
2022-09-05 05:23:35 +00:00
var unown = Data.AsSpan(FormOffset1 + 4, 0x1C);
forms.CopyTo(unown);
if (forms.Length != unown.Length)
unown[forms.Length..].Fill(FORM_NONE);
2021-02-21 17:59:10 +00:00
return;
}
if (DP)
return;
2021-02-21 17:59:10 +00:00
int PokeDexLanguageFlags = FormOffset1 + (HGSS ? 0x3C : 0x20);
int FormOffset2 = PokeDexLanguageFlags + 0x1F4;
switch (species)
2021-02-21 17:59:10 +00:00
{
case (int)Species.Rotom: // Rotom
var value = SetDexFormValues(forms, 3, 6);
WriteUInt32LittleEndian(Data.AsSpan(FormOffset2), value);
return;
case (int)Species.Shaymin: // Shaymin
Data[FormOffset2 + 4] = (byte)SetDexFormValues(forms, 1, 2);
return;
case (int)Species.Giratina: // Giratina
Data[FormOffset2 + 5] = (byte)SetDexFormValues(forms, 1, 2);
return;
case (int)Species.Pichu when HGSS: // Pichu
Data[FormOffset2 + 6] = (byte)SetDexFormValues(forms, 2, 3);
return;
2021-02-21 17:59:10 +00:00
}
}
2021-02-21 17:59:10 +00:00
2022-09-05 05:23:35 +00:00
private static byte[] GetDexFormValues(uint Value, int BitsPerForm, int readCt)
{
2022-09-05 05:23:35 +00:00
byte[] Forms = new byte[readCt];
int n1 = 0xFF >> (8 - BitsPerForm);
for (int i = 0; i < Forms.Length; i++)
2021-02-21 17:59:10 +00:00
{
int val = (int)(Value >> (i * BitsPerForm)) & n1;
if (n1 == val && BitsPerForm > 1)
2022-09-05 05:23:35 +00:00
Forms[i] = byte.MaxValue;
else
2022-09-05 05:23:35 +00:00
Forms[i] = (byte)val;
}
2021-02-21 17:59:10 +00:00
// (BitsPerForm > 1) was already handled, handle (BitsPerForm == 1)
if (BitsPerForm == 1 && Forms[0] == Forms[1] && Forms[0] == 1)
2022-09-05 05:23:35 +00:00
Forms[0] = Forms[1] = byte.MaxValue;
2021-02-21 17:59:10 +00:00
return Forms;
}
2021-02-21 17:59:10 +00:00
2022-09-05 05:23:35 +00:00
private static uint SetDexFormValues(ReadOnlySpan<byte> Forms, int BitsPerForm, int readCt)
{
int n1 = 0xFF >> (8 - BitsPerForm);
uint Value = 0xFFFFFFFF << (readCt * BitsPerForm);
for (int i = 0; i < Forms.Length; i++)
2021-02-21 17:59:10 +00:00
{
int val = Forms[i];
if (val == -1)
val = n1;
Value |= (uint)(val << (BitsPerForm * i));
if (i >= readCt)
throw new ArgumentOutOfRangeException(nameof(readCt), "Array count should be less than bitfield count");
2021-02-21 17:59:10 +00:00
}
return Value;
}
2022-09-05 05:23:35 +00:00
private static bool TryInsertForm(Span<byte> forms, byte form)
{
if (forms.IndexOf(form) >= 0)
return false; // already in list
2021-02-21 17:59:10 +00:00
// insert at first empty
2022-09-05 05:23:35 +00:00
var index = forms.IndexOf(FORM_NONE);
if (index < 0)
return false; // no free slots?
forms[index] = form;
return true;
}
private const byte UnownEmpty = byte.MaxValue;
public int GetUnownFormIndex(byte form)
{
var ofs = Offset + OFS_FORM1 + 4;
for (byte i = 0; i < 0x1C; i++)
2021-02-21 17:59:10 +00:00
{
byte val = Data[ofs + i];
if (val == form)
return i;
2022-09-05 05:23:35 +00:00
if (val == FORM_NONE) // end of populated indexes
return UnownEmpty;
2021-02-21 17:59:10 +00:00
}
return UnownEmpty;
}
2021-02-21 17:59:10 +00:00
public int GetUnownFormIndexNext(byte form)
{
var ofs = Offset + OFS_FORM1 + 4;
for (int i = 0; i < 0x1C; i++)
2021-02-21 17:59:10 +00:00
{
byte val = Data[ofs + i];
if (val == form)
return i;
2022-09-05 05:23:35 +00:00
if (val == FORM_NONE)
return i;
2021-02-21 17:59:10 +00:00
}
return UnownEmpty;
}
2021-02-21 17:59:10 +00:00
public void ClearUnownForms()
{
var ofs = Offset + OFS_FORM1 + 4;
for (int i = 0; i < 0x1C; i++)
2022-09-05 05:23:35 +00:00
Data[ofs + i] = FORM_NONE;
}
2021-02-21 17:59:10 +00:00
public bool GetUnownForm(byte form) => GetUnownFormIndex(form) != UnownEmpty;
2021-02-21 17:59:10 +00:00
public void AddUnownForm(byte form)
{
var index = GetUnownFormIndexNext(form);
if (index == UnownEmpty)
return;
2021-02-21 17:59:10 +00:00
var ofs = Offset + OFS_FORM1 + 4;
Data[ofs + index] = form;
}
2021-02-21 17:59:10 +00:00
public override void SetDex(PKM pk)
{
var species = pk.Species;
if (species is 0 or > Legal.MaxSpeciesID_4)
return;
if (pk.IsEgg) // do not add
return;
var gender = pk.Gender;
var form = pk.Form;
var language = pk.Language;
SetDex(species, gender, form, language);
}
2021-02-21 17:59:10 +00:00
private void SetDex(ushort species, int gender, byte form, int language)
{
SetCaught(species);
SetSeenGender(species, gender);
SetSeen(species);
SetForms(species, form, gender);
SetLanguage(species, language);
}
2021-02-21 17:59:10 +00:00
public void SetSeenGender(ushort species, int gender)
{
if (!GetSeen(species))
SetSeenGenderNewFlag(species, gender);
else if (GetSeenSingleGender(species))
2021-02-21 17:59:10 +00:00
SetSeenGenderSecond(species, gender);
}
2021-02-21 17:59:10 +00:00
public void SetSeenGenderNewFlag(ushort species, int gender)
{
SetSeenGenderFirst(species, gender);
SetSeenGenderSecond(species, gender);
}
2021-02-21 17:59:10 +00:00
public void SetSeenGenderNeither(ushort species)
{
SetSeenGenderFirst(species, 0);
SetSeenGenderSecond(species, 0);
}
2021-02-21 17:59:10 +00:00
private void SetForms(ushort species, byte form, int gender)
{
if (species == (int)Species.Unown) // Unown
{
AddUnownForm(form);
return;
2021-02-21 17:59:10 +00:00
}
2022-09-05 05:23:35 +00:00
var forms = GetForms(species);
if (forms.Length == 0)
return;
if (species == (int)Species.Pichu && HGSS) // Pichu (HGSS Only)
2021-02-21 17:59:10 +00:00
{
var formID = form == 1 ? (byte)2 : (byte)gender;
if (TryInsertForm(forms, formID))
SetForms(species, forms);
2021-02-21 17:59:10 +00:00
}
else
2021-02-21 17:59:10 +00:00
{
if (TryInsertForm(forms, form))
SetForms(species, forms);
2021-02-21 17:59:10 +00:00
}
}
2021-02-21 17:59:10 +00:00
public void SetLanguage(ushort species, int language, bool value = true)
{
int lang = GetGen4LanguageBitIndex(language);
SetLanguageBitIndex(species, lang, value);
}
2021-02-21 17:59:10 +00:00
public bool GetLanguageBitIndex(ushort species, int lang)
{
int dpl = 1 + Array.IndexOf(DPLangSpecies, species);
if (DP && dpl < 0)
return false;
int FormOffset1 = Offset + OFS_FORM1;
int PokeDexLanguageFlags = FormOffset1 + (HGSS ? 0x3C : 0x20);
var ofs = PokeDexLanguageFlags + (DP ? dpl : species);
return FlagUtil.GetFlag(Data, ofs, lang & 7);
}
2021-02-21 17:59:10 +00:00
public void SetLanguageBitIndex(ushort species, int lang, bool value)
{
int dpl = 1 + Array.IndexOf(DPLangSpecies, species);
if (DP && dpl <= 0)
return;
int FormOffset1 = Offset + OFS_FORM1;
int PokeDexLanguageFlags = FormOffset1 + (HGSS ? 0x3C : 0x20);
var ofs = PokeDexLanguageFlags + (DP ? dpl : species);
FlagUtil.SetFlag(Data, ofs, lang & 7, value);
}
2021-02-21 17:59:10 +00:00
public bool HasLanguage(ushort species) => GetSpeciesLanguageByteIndex(species) >= 0;
2021-02-21 17:59:10 +00:00
private int GetSpeciesLanguageByteIndex(ushort species)
{
if (DP)
return Array.IndexOf(DPLangSpecies, species);
return species;
}
2021-02-21 17:59:10 +00:00
private static readonly int[] DPLangSpecies = { 23, 25, 54, 77, 120, 129, 202, 214, 215, 216, 228, 278, 287, 315 };
2021-02-21 17:59:10 +00:00
public static int GetGen4LanguageBitIndex(int lang) => --lang switch
{
3 => 4, // invert ITA/GER
4 => 3, // invert ITA/GER
> 5 => 0, // Japanese
< 0 => 1, // English
_ => lang,
};
[Flags]
public enum SetDexArgs
{
None,
SeenAll = 1 << 0,
2021-02-21 17:59:10 +00:00
CaughtNone = 1 << 1,
CaughtAll = 1 << 2,
2021-02-21 17:59:10 +00:00
SetNoLanguages = 1 << 3,
SetAllLanguages = 1 << 4,
SetSingleLanguage = 1 << 5,
2021-02-21 17:59:10 +00:00
SetAllForms = 1 << 6,
2021-02-21 17:59:10 +00:00
Complete = SeenAll | CaughtAll | SetAllLanguages | SetAllForms,
}
2021-02-21 17:59:10 +00:00
public void ModifyAll(ushort species, SetDexArgs args, int lang = 0)
{
if (args == SetDexArgs.None)
2021-02-21 17:59:10 +00:00
{
ClearSeen(species);
return;
2021-02-21 17:59:10 +00:00
}
if ((args & SetDexArgs.SeenAll) != 0)
CompleteSeen(species);
2021-02-21 17:59:10 +00:00
if ((args & SetDexArgs.CaughtNone) != 0)
2021-02-21 17:59:10 +00:00
{
SetCaught(species, false);
ClearLanguages(species);
2021-02-21 17:59:10 +00:00
}
else if ((args & SetDexArgs.CaughtAll) != 0)
2021-02-21 17:59:10 +00:00
{
SetCaught(species);
2021-02-21 17:59:10 +00:00
}
if ((args & SetDexArgs.SetNoLanguages) != 0)
2021-02-21 17:59:10 +00:00
{
ClearLanguages(species);
}
if ((args & SetDexArgs.SetAllLanguages) != 0)
2021-02-21 17:59:10 +00:00
{
SetLanguages(species);
2021-02-21 17:59:10 +00:00
}
else if ((args & SetDexArgs.SetSingleLanguage) != 0)
2021-02-21 17:59:10 +00:00
{
SetLanguage(species, lang);
2021-02-21 17:59:10 +00:00
}
if ((args & SetDexArgs.SetAllForms) != 0)
{
CompleteForms(species);
}
}
2021-02-21 17:59:10 +00:00
private void CompleteForms(ushort species)
{
var forms = GetFormNames4Dex(species);
if (forms.Length <= 1)
return;
2022-09-05 05:23:35 +00:00
Span<byte> values = stackalloc byte[forms.Length];
for (byte i = 1; i < values.Length; i++)
values[i] = i;
SetForms(species, values);
}
2021-02-21 17:59:10 +00:00
private void CompleteSeen(ushort species)
{
SetSeen(species);
var pi = PersonalTable.HGSS[species];
if (pi.IsDualGender)
2021-02-21 17:59:10 +00:00
{
SetSeenGenderFirst(species, 0);
SetSeenGenderSecond(species, 1);
2021-02-21 17:59:10 +00:00
}
else
2021-02-21 17:59:10 +00:00
{
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
SetSeenGender(species, pi.FixedGender() & 1);
2021-02-21 17:59:10 +00:00
}
}
2021-02-21 17:59:10 +00:00
public void ClearSeen(ushort species)
{
SetCaught(species, false);
SetSeen(species, false);
SetSeenGenderNeither(species);
2022-09-05 05:23:35 +00:00
SetForms(species, ReadOnlySpan<byte>.Empty);
ClearLanguages(species);
}
private const int LangCount = 6;
private void ClearLanguages(ushort species)
{
for (int i = 0; i < 8; i++)
SetLanguageBitIndex(species, i, false);
}
private void SetLanguages(ushort species, bool value = true)
{
for (int i = 0; i < LangCount; i++)
SetLanguageBitIndex(species, i, value);
}
// Bulk Manipulation
public override void CompleteDex(bool shinyToo = false) => IterateAll(z => ModifyAll(z, SetDexArgs.Complete));
public override void SeenNone() => IterateAll(ClearSeen);
public override void CaughtNone() => IterateAll(z => SetCaught(z, false));
public override void SeenAll(bool shinyToo = false) => IterateAll(CompleteSeen);
public override void SetDexEntryAll(ushort species, bool shinyToo = false) => ModifyAll(species, SetDexArgs.Complete);
public override void ClearDexEntryAll(ushort species) => ModifyAll(species, SetDexArgs.None);
private static void IterateAll(Action<ushort> a)
{
for (ushort i = 1; i <= Legal.MaxSpeciesID_4; i++)
a(i);
}
public override void SetAllSeen(bool value = true, bool shinyToo = false)
{
if (!value)
2021-02-21 17:59:10 +00:00
{
SeenNone();
return;
2021-02-21 17:59:10 +00:00
}
IterateAll(CompleteSeen);
}
public override void CaughtAll(bool shinyToo = false)
{
SeenAll();
IterateAll(z => SetCaught(z));
2021-02-21 17:59:10 +00:00
}
}