2018-11-01 22:38:09 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
2022-01-03 05:35:59 +00:00
|
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Pokédex structure used for Generation 7 games.
|
|
|
|
/// </summary>>
|
2023-01-22 04:02:33 +00:00
|
|
|
public class Zukan7 : Zukan<SaveFile>
|
2018-11-01 22:38:09 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
private const int MAGIC = 0x2F120F17;
|
|
|
|
private const int SIZE_MAGIC = 4;
|
|
|
|
private const int SIZE_FLAGS = 4;
|
|
|
|
private const int SIZE_MISC = 0x80; // Misc Data (1024 bits)
|
|
|
|
private const int SIZE_CAUGHT = 0x68; // 832 bits
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected sealed override int OFS_CAUGHT => SIZE_MAGIC + SIZE_FLAGS + SIZE_MISC;
|
|
|
|
protected sealed override int OFS_SEEN => OFS_CAUGHT + SIZE_CAUGHT;
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected sealed override int BitSeenSize => 0x8C; // 1120 bits
|
|
|
|
protected sealed override int DexLangFlagByteCount => 920; // 0x398 = 817*9, top off the savedata block.
|
|
|
|
protected sealed override int DexLangIDCount => 9; // CHT, skipping langID 6 (unused)
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
private readonly IList<ushort> FormBaseSpecies;
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public Zukan7(SAV7SM sav, int dex, int langflag) : this(sav, dex, langflag, DexFormUtil.GetDexFormIndexSM) { }
|
|
|
|
public Zukan7(SAV7USUM sav, int dex, int langflag) : this(sav, dex, langflag, DexFormUtil.GetDexFormIndexUSUM) { }
|
|
|
|
protected Zukan7(SAV7b sav, int dex, int langflag) : this(sav, dex, langflag, DexFormUtil.GetDexFormIndexGG) { }
|
PKHeX.Core Nullable cleanup (#2401)
* Handle some nullable cases
Refactor MysteryGift into a second abstract class (backed by a byte array, or fake data)
Make some classes have explicit constructors instead of { } initialization
* Handle bits more obviously without null
* Make SaveFile.BAK explicitly readonly again
* merge constructor methods to have readonly fields
* Inline some properties
* More nullable handling
* Rearrange box actions
define straightforward classes to not have any null properties
* Make extrabyte reference array immutable
* Move tooltip creation to designer
* Rearrange some logic to reduce nesting
* Cache generated fonts
* Split mystery gift album purpose
* Handle more tooltips
* Disallow null setters
* Don't capture RNG object, only type enum
* Unify learnset objects
Now have readonly properties which are never null
don't new() empty learnsets (>800 Learnset objects no longer created,
total of 2400 objects since we also new() a move & level array)
optimize g1/2 reader for early abort case
* Access rewrite
Initialize blocks in a separate object, and get via that object
removes a couple hundred "might be null" warnings since blocks are now readonly getters
some block references have been relocated, but interfaces should expose all that's needed
put HoF6 controls in a groupbox, and disable
* Readonly personal data
* IVs non nullable for mystery gift
* Explicitly initialize forced encounter moves
* Make shadow objects readonly & non-null
Put murkrow fix in binary data resource, instead of on startup
* Assign dex form fetch on constructor
Fixes legality parsing edge cases
also handle cxd parse for valid; exit before exception is thrown in FrameGenerator
* Remove unnecessary null checks
* Keep empty value until init
SetPouch sets the value to an actual one during load, but whatever
* Readonly team lock data
* Readonly locks
Put locked encounters at bottom (favor unlocked)
* Mail readonly data / offset
Rearrange some call flow and pass defaults
Add fake classes for SaveDataEditor mocking
Always party size, no need to check twice in stat editor
use a fake save file as initial data for savedata editor, and for
gamedata (wow i found a usage)
constrain eventwork editor to struct variable types (uint, int, etc),
thus preventing null assignment errors
2019-10-17 01:47:31 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
private Zukan7(SaveFile sav, int dex, int langflag, Func<ushort, byte, int, int> form) : base(sav, dex, langflag)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
DexFormIndexFetcher = form;
|
|
|
|
FormBaseSpecies = GetFormIndexBaseSpeciesList();
|
|
|
|
Debug.Assert(!SAV.State.Exportable || ReadUInt32LittleEndian(SAV.Data.AsSpan(PokeDex)) == MAGIC);
|
|
|
|
}
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
public Func<ushort, byte, int, int> DexFormIndexFetcher { get; }
|
2018-11-14 03:14:23 +00:00
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
protected sealed override void SetAllDexSeenFlags(int baseBit, byte form, int gender, bool isShiny, bool value = true)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2022-08-27 06:43:36 +00:00
|
|
|
var species = (ushort)(baseBit + 1);
|
2022-06-18 18:04:24 +00:00
|
|
|
if (species == (int)Species.Castform)
|
|
|
|
isShiny = false;
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Starting with Gen7, form bits are stored in the same region as the species flags.
|
|
|
|
int formstart = form;
|
|
|
|
int formend = form;
|
|
|
|
bool reset = GetSaneFormsToIterate(species, out int fs, out int fe, formstart);
|
|
|
|
if (reset)
|
|
|
|
{
|
|
|
|
formstart = fs;
|
|
|
|
formend = fe;
|
|
|
|
}
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
int shiny = isShiny ? 1 : 0;
|
|
|
|
for (int f = formstart; f <= formend; f++)
|
|
|
|
{
|
|
|
|
int formBit = baseBit;
|
|
|
|
if (f > 0) // Override the bit to overwrite
|
2018-11-01 22:38:09 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
var fc = SAV.Personal[species].FormCount;
|
2022-06-18 18:04:24 +00:00
|
|
|
if (fc > 1) // actually has forms
|
2018-11-01 22:38:09 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
int index = DexFormIndexFetcher(species, fc, SAV.MaxSpeciesID - 1);
|
|
|
|
if (index >= 0) // bit index valid
|
|
|
|
formBit = index + f;
|
2018-11-01 22:38:09 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
SetDexFlags(baseBit, formBit, gender, shiny, value);
|
2018-11-01 22:38:09 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
protected override bool GetSaneFormsToIterate(ushort species, out int formStart, out int formEnd, int formIn)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
return SanitizeFormsToIterate(species, out formStart, out formEnd, formIn, SAV is SAV7USUM);
|
|
|
|
}
|
2019-06-09 02:56:11 +00:00
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
public static bool SanitizeFormsToIterate(ushort species, out int formStart, out int formEnd, int formIn, bool USUM)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
// 004AA370 in Moon
|
|
|
|
// Simplified in terms of usage -- only overrides to give all the battle forms for a pk
|
|
|
|
switch (species)
|
2018-11-01 22:38:09 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
case 351: // Castform
|
|
|
|
formStart = 0;
|
|
|
|
formEnd = 3;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case 421: // Cherrim
|
|
|
|
case 555: // Darmanitan
|
|
|
|
case 648: // Meloetta
|
|
|
|
case 746: // Wishiwashi
|
|
|
|
case 778: // Mimikyu
|
|
|
|
// Alolans
|
|
|
|
case 020: // Raticate
|
|
|
|
case 105: // Marowak
|
|
|
|
formStart = 0;
|
|
|
|
formEnd = 1;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case 735: // Gumshoos
|
|
|
|
case 758: // Salazzle
|
|
|
|
case 754: // Lurantis
|
|
|
|
case 738: // Vikavolt
|
|
|
|
case 784: // Kommo-o
|
|
|
|
case 752: // Araquanid
|
|
|
|
case 777: // Togedemaru
|
|
|
|
case 743: // Ribombee
|
|
|
|
case 744: // Rockruff
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 774 when formIn <= 6: // Minior
|
|
|
|
break; // don't give meteor forms except the first
|
|
|
|
|
|
|
|
case 718 when formIn > 1:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
int count = USUM ? DexFormUtil.GetDexFormCountUSUM(species) : DexFormUtil.GetDexFormCountSM(species);
|
|
|
|
formStart = formEnd = 0;
|
|
|
|
return count < formIn;
|
2018-11-01 22:38:09 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
formStart = 0;
|
|
|
|
formEnd = 0;
|
|
|
|
return true;
|
|
|
|
}
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected sealed override int GetDexLangFlag(int lang) => lang switch
|
|
|
|
{
|
|
|
|
> 10 or 6 or <= 0 => -1, // invalid language
|
|
|
|
// skip over langID 0 (unused) => [0-8]
|
|
|
|
// skip over langID 6 (unused)
|
|
|
|
>= 7 => lang - 2,
|
|
|
|
_ => lang - 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
protected sealed override void SetSpindaDexData(PKM pk, bool alreadySeen)
|
|
|
|
{
|
|
|
|
int shift = (pk.Gender & 1) | (pk.IsShiny ? 2 : 0);
|
|
|
|
if (alreadySeen) // update?
|
2018-11-01 22:38:09 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var flag1 = (1 << (shift + 4));
|
|
|
|
if ((SAV.Data[PokeDex + 0x84] & flag1) != 0) // Already showing this one
|
|
|
|
return;
|
2022-01-03 05:35:59 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var span = SAV.Data.AsSpan(PokeDex + 0x8E8 + (shift * 4));
|
|
|
|
WriteUInt32LittleEndian(span, pk.EncryptionConstant);
|
|
|
|
SAV.Data[PokeDex + 0x84] |= (byte)(flag1 | (1 << shift));
|
2018-11-01 22:38:09 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
else if ((SAV.Data[PokeDex + 0x84] & (1 << shift)) == 0)
|
2018-11-01 22:38:09 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var span = SAV.Data.AsSpan(PokeDex + 0x8E8 + (shift * 4));
|
|
|
|
WriteUInt32LittleEndian(span, pk.EncryptionConstant);
|
|
|
|
SAV.Data[PokeDex + 0x84] |= (byte)(1 << shift);
|
2018-11-01 22:38:09 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dex Flags
|
|
|
|
public bool NationalDex
|
|
|
|
{
|
|
|
|
get => (SAV.Data[PokeDex + 4] & 1) == 1;
|
|
|
|
set => SAV.Data[PokeDex + 4] = (byte)((SAV.Data[PokeDex + 4] & 0xFE) | (value ? 1 : 0));
|
|
|
|
}
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
2023-12-04 04:13:20 +00:00
|
|
|
/// Gets the last viewed dex entry in the Pokédex (by National Dex ID), internally called DefaultMons
|
2022-06-18 18:04:24 +00:00
|
|
|
/// </summary>
|
|
|
|
public uint CurrentViewedDex => (ReadUInt32LittleEndian(SAV.Data.AsSpan(PokeDex + 4)) >> 9) & 0x3FF;
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
public IEnumerable<int> GetAllFormEntries(ushort species)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
var fc = SAV.Personal[species].FormCount;
|
|
|
|
for (int j = 1; j < fc; j++)
|
2018-11-01 22:38:09 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
int start = j;
|
|
|
|
int end = j;
|
|
|
|
if (GetSaneFormsToIterate(species, out int s, out int n, j))
|
2018-11-01 22:38:09 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
start = s;
|
|
|
|
end = n;
|
|
|
|
}
|
|
|
|
start = Math.Max(1, start);
|
|
|
|
for (int f = start; f <= end; f++)
|
|
|
|
{
|
|
|
|
int x = GetDexFormIndex(species, fc, f);
|
|
|
|
if (x >= 0)
|
|
|
|
yield return x;
|
2018-11-01 22:38:09 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
public int GetDexFormIndex(ushort species, byte formCount, int form)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
var index = DexFormIndexFetcher(species, formCount, form);
|
2022-06-18 18:04:24 +00:00
|
|
|
if (index < 0)
|
|
|
|
return index;
|
|
|
|
return index + SAV.MaxSpeciesID - 1;
|
|
|
|
}
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public IList<string> GetEntryNames(IReadOnlyList<string> speciesNames)
|
|
|
|
{
|
|
|
|
var names = new List<string>();
|
|
|
|
var max = SAV.MaxSpeciesID;
|
|
|
|
for (int i = 1; i <= max; i++)
|
|
|
|
names.Add($"{i:000} - {speciesNames[i]}");
|
|
|
|
|
|
|
|
// Add forms
|
|
|
|
int ctr = max + 1;
|
2022-08-27 06:43:36 +00:00
|
|
|
for (ushort species = 1; species <= max; species++)
|
2018-11-01 22:38:09 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
var c = SAV.Personal[species].FormCount;
|
2022-06-18 18:04:24 +00:00
|
|
|
for (int f = 1; f < c; f++)
|
2018-11-01 22:38:09 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
int x = GetDexFormIndex(species, c, f);
|
|
|
|
if (x >= 0)
|
|
|
|
names.Add($"{ctr++:000} - {speciesNames[species]}-{f}");
|
2018-11-01 22:38:09 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return names;
|
|
|
|
}
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets a list of Species IDs that a given dex-form index corresponds to.
|
|
|
|
/// </summary>
|
2022-08-27 06:43:36 +00:00
|
|
|
private List<ushort> GetFormIndexBaseSpeciesList()
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2022-08-27 06:43:36 +00:00
|
|
|
var baseSpecies = new List<ushort>();
|
|
|
|
for (ushort species = 1; species <= SAV.MaxSpeciesID; species++)
|
2018-11-01 22:38:09 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
var c = SAV.Personal[species].FormCount;
|
2022-06-18 18:04:24 +00:00
|
|
|
for (int f = 1; f < c; f++)
|
2018-11-01 22:38:09 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
int x = GetDexFormIndex(species, c, f);
|
|
|
|
if (x >= 0)
|
|
|
|
baseSpecies.Add(species);
|
2018-11-01 22:38:09 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return baseSpecies;
|
|
|
|
}
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
public byte GetBaseSpeciesGenderValue(int index)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
// meowstic special handling
|
|
|
|
const int meow = 678;
|
|
|
|
if (index == meow - 1 || (index >= SAV.MaxSpeciesID && FormBaseSpecies[index - SAV.MaxSpeciesID] == meow))
|
|
|
|
return index < SAV.MaxSpeciesID ? PersonalInfo.RatioMagicMale : PersonalInfo.RatioMagicFemale; // M : F
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (index < SAV.MaxSpeciesID)
|
|
|
|
return SAV.Personal[index + 1].Gender;
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
index -= SAV.MaxSpeciesID;
|
2022-08-27 06:43:36 +00:00
|
|
|
ushort species = FormBaseSpecies[index];
|
2022-06-18 18:04:24 +00:00
|
|
|
return SAV.Personal[species].Gender;
|
|
|
|
}
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
public ushort GetBaseSpecies(ushort index)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
if (index <= SAV.MaxSpeciesID)
|
|
|
|
return index;
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return FormBaseSpecies[index - SAV.MaxSpeciesID - 1];
|
|
|
|
}
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected sealed override void SetAllDexFlagsLanguage(int bit, int lang, bool value = true)
|
|
|
|
{
|
|
|
|
lang = GetDexLangFlag(lang);
|
|
|
|
if (lang < 0)
|
|
|
|
return;
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
int lbit = (bit * DexLangIDCount) + lang;
|
|
|
|
if (lbit < DexLangFlagByteCount << 3) // Sanity check for max length of region
|
|
|
|
SetFlag(PokeDexLanguageFlags, lbit, value);
|
|
|
|
}
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
public bool[] GetLanguageBitflags(ushort species)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
var result = new bool[DexLangIDCount];
|
|
|
|
int bit = species - 1;
|
|
|
|
for (int i = 0; i < DexLangIDCount; i++)
|
2018-11-01 22:38:09 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
int lbit = (bit * DexLangIDCount) + i;
|
|
|
|
result[i] = GetFlag(PokeDexLanguageFlags, lbit);
|
2018-11-01 22:38:09 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return result;
|
|
|
|
}
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
public void SetLanguageBitflags(ushort species, ReadOnlySpan<bool> value)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
int bit = species - 1;
|
|
|
|
for (int i = 0; i < DexLangIDCount; i++)
|
2018-11-01 22:38:09 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
int lbit = (bit * DexLangIDCount) + i;
|
|
|
|
SetFlag(PokeDexLanguageFlags, lbit, value[i]);
|
2018-11-01 22:38:09 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public void ToggleLanguageFlagsAll(bool value)
|
|
|
|
{
|
|
|
|
var arr = GetBlankLanguageBits(value);
|
2022-08-27 06:43:36 +00:00
|
|
|
for (ushort i = 1; i <= SAV.MaxSpeciesID; i++)
|
2022-06-18 18:04:24 +00:00
|
|
|
SetLanguageBitflags(i, arr);
|
|
|
|
}
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
public void ToggleLanguageFlagsSingle(ushort species, bool value)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
var arr = GetBlankLanguageBits(value);
|
|
|
|
SetLanguageBitflags(species, arr);
|
|
|
|
}
|
2018-11-01 22:38:09 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private bool[] GetBlankLanguageBits(bool value)
|
|
|
|
{
|
|
|
|
var result = new bool[DexLangIDCount];
|
|
|
|
for (int i = 0; i < DexLangIDCount; i++)
|
|
|
|
result[i] = value;
|
|
|
|
return result;
|
2018-11-01 22:38:09 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|