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
using System ;
2022-01-03 05:35:59 +00:00
using static System . Buffers . Binary . BinaryPrimitives ;
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
namespace PKHeX.Core ;
/// <summary>
/// Generation 5 <see cref="SaveFile"/> object for <see cref="GameVersion.FRLG"/>.
/// </summary>
/// <inheritdoc cref="SAV3" />
public sealed class SAV3FRLG : SAV3 , IGen3Joyful , IGen3Wonder
2021-03-16 06:51:58 +00:00
{
2022-06-18 18:04:24 +00:00
// Configuration
2023-01-22 04:02:33 +00:00
protected override SAV3FRLG CloneInternal ( ) = > new ( Write ( ) ) ;
2022-06-18 18:04:24 +00:00
public override GameVersion Version { get ; protected set ; } = GameVersion . FR ; // allow mutation
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
private PersonalTable3 _personal = PersonalTable . FR ;
2023-01-22 04:02:33 +00:00
public override PersonalTable3 Personal = > _personal ;
2022-06-18 18:04:24 +00:00
public override int EventFlagCount = > 8 * 288 ;
public override int EventWorkCount = > 0x100 ;
protected override int DaycareSlotSize = > SIZE_STORED + 0x3C ; // 0x38 mail + 4 exp
public override int DaycareSeedSize = > 4 ; // 16bit
protected override int EggEventFlag = > 0x266 ;
protected override int BadgeFlagStart = > 0x820 ;
public SAV3FRLG ( bool japanese = false ) : base ( japanese ) = > Initialize ( ) ;
public SAV3FRLG ( byte [ ] data ) : base ( data )
2021-03-16 06:51:58 +00:00
{
2022-06-18 18:04:24 +00:00
Initialize ( ) ;
2021-04-01 20:37:39 +00:00
2022-06-18 18:04:24 +00:00
// Fix save files that have an overflow corruption with the Pokédex.
// Future loads of this save file will cause it to be recognized as FR/LG correctly.
if ( IsCorruptPokedexFF ( ) )
WriteUInt32LittleEndian ( Small . AsSpan ( 0xAC ) , 1 ) ;
}
2021-04-01 20:37:39 +00:00
2022-06-18 18:04:24 +00:00
protected override int EventFlag = > 0xEE0 ;
protected override int EventWork = > 0x1000 ;
2022-04-10 01:12:57 +00:00
2022-06-18 18:04:24 +00:00
private void Initialize ( )
{
// small
PokeDex = 0x18 ;
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
// large
DaycareOffset = 0x2F80 ;
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
// storage
Box = 0 ;
}
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
public bool ResetPersonal ( GameVersion g )
{
if ( g is not ( GameVersion . FR or GameVersion . LG ) )
return false ;
_personal = g = = GameVersion . FR ? PersonalTable . FR : PersonalTable . LG ;
Version = g ;
return true ;
}
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
#region Small
public override bool NationalDex
{
get = > PokedexNationalMagicFRLG = = PokedexNationalUnlockFRLG ;
set
2021-03-16 06:51:58 +00:00
{
2022-06-18 18:04:24 +00:00
PokedexNationalMagicFRLG = value ? PokedexNationalUnlockFRLG : ( byte ) 0 ; // magic
SetEventFlag ( 0x840 , value ) ;
SetWork ( 0x4E , PokedexNationalUnlockWorkFRLG ) ;
2021-03-16 06:51:58 +00:00
}
2022-06-18 18:04:24 +00:00
}
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
public ushort JoyfulJumpInRow { get = > ReadUInt16LittleEndian ( Small . AsSpan ( 0xB00 ) ) ; set = > WriteUInt16LittleEndian ( Small . AsSpan ( 0xB00 ) , Math . Min ( ( ushort ) 9999 , value ) ) ; }
// u16 field2;
public ushort JoyfulJump5InRow { get = > ReadUInt16LittleEndian ( Small . AsSpan ( 0xB04 ) ) ; set = > WriteUInt16LittleEndian ( Small . AsSpan ( 0xB04 ) , Math . Min ( ( ushort ) 9999 , value ) ) ; }
public ushort JoyfulJumpGamesMaxPlayers { get = > ReadUInt16LittleEndian ( Small . AsSpan ( 0xB06 ) ) ; set = > WriteUInt16LittleEndian ( Small . AsSpan ( 0xB06 ) , Math . Min ( ( ushort ) 9999 , value ) ) ; }
// u32 field8;
public uint JoyfulJumpScore { get = > ReadUInt16LittleEndian ( Small . AsSpan ( 0xB0C ) ) ; set = > WriteUInt32LittleEndian ( Small . AsSpan ( 0xB0C ) , Math . Min ( 9999 , value ) ) ; }
2021-03-17 01:48:33 +00:00
2022-06-18 18:04:24 +00:00
public uint JoyfulBerriesScore { get = > ReadUInt16LittleEndian ( Small . AsSpan ( 0xB10 ) ) ; set = > WriteUInt32LittleEndian ( Small . AsSpan ( 0xB10 ) , Math . Min ( 9999 , value ) ) ; }
public ushort JoyfulBerriesInRow { get = > ReadUInt16LittleEndian ( Small . AsSpan ( 0xB14 ) ) ; set = > WriteUInt16LittleEndian ( Small . AsSpan ( 0xB14 ) , Math . Min ( ( ushort ) 9999 , value ) ) ; }
public ushort JoyfulBerries5InRow { get = > ReadUInt16LittleEndian ( Small . AsSpan ( 0xB16 ) ) ; set = > WriteUInt16LittleEndian ( Small . AsSpan ( 0xB16 ) , Math . Min ( ( ushort ) 9999 , value ) ) ; }
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
public uint BerryPowder
{
get = > ReadUInt32LittleEndian ( Small . AsSpan ( 0xAF8 ) ) ^ SecurityKey ;
set = > WriteUInt32LittleEndian ( Small . AsSpan ( 0xAF8 ) , value ^ SecurityKey ) ;
}
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
public override uint SecurityKey
{
get = > ReadUInt32LittleEndian ( Small . AsSpan ( 0xF20 ) ) ;
set = > WriteUInt32LittleEndian ( Small . AsSpan ( 0xF20 ) , value ) ;
}
#endregion
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
#region Large
public override int PartyCount { get = > Large [ 0x034 ] ; protected set = > Large [ 0x034 ] = ( byte ) value ; }
public override int GetPartyOffset ( int slot ) = > 0x038 + ( SIZE_PARTY * slot ) ;
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
public override uint Money
{
get = > ReadUInt32LittleEndian ( Large . AsSpan ( 0x0290 ) ) ^ SecurityKey ;
set = > WriteUInt32LittleEndian ( Large . AsSpan ( 0x0290 ) , value ^ SecurityKey ) ;
}
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
public override uint Coin
{
get = > ( ushort ) ( ReadUInt16LittleEndian ( Large . AsSpan ( 0x0294 ) ) ^ SecurityKey ) ;
set = > WriteUInt16LittleEndian ( Large . AsSpan ( 0x0294 ) , ( ushort ) ( value ^ SecurityKey ) ) ;
}
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
private const int OFS_PCItem = 0x0298 ;
private const int OFS_PouchHeldItem = 0x0310 ;
private const int OFS_PouchKeyItem = 0x03B8 ;
private const int OFS_PouchBalls = 0x0430 ;
private const int OFS_PouchTMHM = 0x0464 ;
private const int OFS_PouchBerry = 0x054C ;
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
protected override InventoryPouch3 [ ] GetItems ( )
{
const int max = 999 ;
2023-04-16 19:58:07 +00:00
var info = ItemStorage3FRLG . Instance ;
2022-06-18 18:04:24 +00:00
return new InventoryPouch3 [ ]
2021-03-16 06:51:58 +00:00
{
2023-04-16 19:58:07 +00:00
new ( InventoryType . Items , info , max , OFS_PouchHeldItem , ( OFS_PouchKeyItem - OFS_PouchHeldItem ) / 4 ) ,
new ( InventoryType . KeyItems , info , 1 , OFS_PouchKeyItem , ( OFS_PouchBalls - OFS_PouchKeyItem ) / 4 ) ,
new ( InventoryType . Balls , info , max , OFS_PouchBalls , ( OFS_PouchTMHM - OFS_PouchBalls ) / 4 ) ,
new ( InventoryType . TMHMs , info , max , OFS_PouchTMHM , ( OFS_PouchBerry - OFS_PouchTMHM ) / 4 ) ,
new ( InventoryType . Berries , info , 999 , OFS_PouchBerry , 43 ) ,
new ( InventoryType . PCItems , info , 999 , OFS_PCItem , ( OFS_PouchHeldItem - OFS_PCItem ) / 4 ) ,
2022-06-18 18:04:24 +00:00
} ;
}
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
protected override int SeenOffset2 = > 0x5F8 ;
protected override int MailOffset = > 0x2CD0 ;
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
protected override int GetDaycareEXPOffset ( int slot ) = > GetDaycareSlotOffset ( 0 , slot + 1 ) - 4 ; // @ end of each pk slot
public override string GetDaycareRNGSeed ( int loc ) = > ReadUInt16LittleEndian ( Large . AsSpan ( GetDaycareEXPOffset ( 2 ) ) ) . ToString ( "X4" ) ; // after the 2nd slot EXP, before the step counter
public override void SetDaycareRNGSeed ( int loc , string seed ) = > WriteUInt16LittleEndian ( Large . AsSpan ( GetDaycareEXPOffset ( 2 ) ) , ( ushort ) Util . GetHexValue ( seed ) ) ;
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
protected override int ExternalEventData = > 0x30A7 ;
2021-08-23 00:15:08 +00:00
2022-06-18 18:04:24 +00:00
#region eBerry
private const int OFFSET_EBERRY = 0x30EC ;
private const int SIZE_EBERRY = 0x134 ;
2021-03-16 06:51:58 +00:00
2023-03-20 23:26:23 +00:00
public override byte [ ] GetEReaderBerry ( ) = > Large . Slice ( OFFSET_EBERRY , SIZE_EBERRY ) ;
public override void SetEReaderBerry ( ReadOnlySpan < byte > data ) = > data . CopyTo ( Large . AsSpan ( OFFSET_EBERRY ) ) ;
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
public override string EBerryName = > GetString ( Large . AsSpan ( OFFSET_EBERRY , 7 ) ) ;
public override bool IsEBerryEngima = > Large [ OFFSET_EBERRY ] is 0 or 0xFF ;
#endregion
2021-03-16 06:51:58 +00:00
2023-03-20 23:26:23 +00:00
#region eTrainer
public override byte [ ] GetEReaderTrainer ( ) = > Small . Slice ( 0x4A0 , 0xBC ) ;
public override void SetEReaderTrainer ( ReadOnlySpan < byte > data ) = > data . CopyTo ( Small . AsSpan ( 0x4A0 ) ) ;
#endregion
2022-06-18 18:04:24 +00:00
public int WonderOffset = > WonderNewsOffset ;
private const int WonderNewsOffset = 0x3120 ;
2023-03-20 23:26:23 +00:00
private int WonderCardOffset = > WonderNewsOffset + ( Japanese ? WonderNews3 . SIZE_JAP : WonderNews3 . SIZE ) ;
private int WonderCardExtraOffset = > WonderCardOffset + ( Japanese ? WonderCard3 . SIZE_JAP : WonderCard3 . SIZE ) ;
2021-03-28 01:58:51 +00:00
2023-03-26 06:14:50 +00:00
public WonderNews3 WonderNews { get = > new ( Large . Slice ( WonderNewsOffset , Japanese ? WonderNews3 . SIZE_JAP : WonderNews3 . SIZE ) ) ; set = > SetData ( Large . AsSpan ( WonderOffset ) , value . Data ) ; }
public WonderCard3 WonderCard { get = > new ( Large . Slice ( WonderCardOffset , Japanese ? WonderCard3 . SIZE_JAP : WonderCard3 . SIZE ) ) ; set = > SetData ( Large . AsSpan ( WonderCardOffset ) , value . Data ) ; }
public WonderCard3Extra WonderCardExtra { get = > new ( Large . Slice ( WonderCardExtraOffset , WonderCard3Extra . SIZE ) ) ; set = > SetData ( Large . AsSpan ( WonderCardExtraOffset ) , value . Data ) ; }
2022-06-18 18:04:24 +00:00
// 0x338: 4 easy chat words
// 0x340: news MENewsJisanStruct
// 0x344: uint[5], uint[5] tracking?
2021-03-28 01:58:51 +00:00
2023-03-20 23:26:23 +00:00
public override Gen3MysteryData MysteryData
2022-06-18 18:04:24 +00:00
{
2023-03-20 23:26:23 +00:00
get = > new MysteryEvent3 ( Large . Slice ( 0x361C , MysteryEvent3 . SIZE ) ) ;
2023-03-26 06:14:50 +00:00
set = > SetData ( Large . AsSpan ( 0x361C ) , value . Data ) ;
2022-06-18 18:04:24 +00:00
}
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
protected override int SeenOffset3 = > 0x3A18 ;
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
public string RivalName
{
get = > GetString ( Large . AsSpan ( 0x3A4C , 8 ) ) ;
2023-01-22 04:02:33 +00:00
set = > SetString ( Large . AsSpan ( 0x3A4C , 8 ) , value , 7 , StringConverterOption . ClearZero ) ;
2021-03-16 06:51:58 +00:00
}
2022-06-18 18:04:24 +00:00
#endregion
2021-03-16 06:51:58 +00:00
}