2021-03-16 06:51:58 +00:00
using System ;
2021-08-21 03:33:53 +00:00
using System.Collections.Generic ;
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 3 <see cref="SaveFile"/> object for <see cref="GameVersion.RS"/>.
/// </summary>
/// <inheritdoc cref="SAV3" />
public sealed class SAV3RS : SAV3 , IGen3Hoenn
2021-03-16 06:51:58 +00:00
{
2022-06-18 18:04:24 +00:00
// Configuration
protected override SaveFile CloneInternal ( ) = > new SAV3RS ( Write ( ) ) ;
public override GameVersion Version { get = > GameVersion . RS ; protected 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 23:15:27 +00:00
public override IPersonalTable Personal = > PersonalTable . RS ;
2021-03-16 06:51:58 +00:00
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 ;
public override int DaycareSeedSize = > 4 ; // 16bit
protected override int EggEventFlag = > 0x86 ;
protected override int BadgeFlagStart = > 0x807 ;
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
public SAV3RS ( byte [ ] data ) : base ( data ) = > Initialize ( ) ;
public SAV3RS ( bool japanese = false ) : base ( japanese ) = > Initialize ( ) ;
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
protected override int EventFlag = > 0x1220 ;
protected override int EventWork = > 0x1340 ;
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 = 0x2F9C ;
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
#region Small
public override bool NationalDex
{
get = > PokedexNationalMagicRSE = = PokedexNationalUnlockRSE ;
set
2021-03-16 06:51:58 +00:00
{
2022-06-18 18:04:24 +00:00
PokedexMode = value ? ( byte ) 1 : ( byte ) 0 ; // mode
PokedexNationalMagicRSE = value ? PokedexNationalUnlockRSE : ( byte ) 0 ; // magic
SetEventFlag ( 0x836 , value ) ;
SetWork ( 0x46 , PokedexNationalUnlockWorkRSE ) ;
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 override uint SecurityKey { get = > 0 ; set { } }
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
public RTC3 ClockInitial
{
get = > new ( GetData ( Small , 0x98 , RTC3 . Size ) ) ;
set = > SetData ( Small , value . Data , 0x98 ) ;
}
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
public RTC3 ClockElapsed
{
get = > new ( GetData ( Small , 0xA0 , RTC3 . Size ) ) ;
set = > SetData ( Small , value . Data , 0xA0 ) ;
}
#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 [ 0x234 ] ; protected set = > Large [ 0x234 ] = ( byte ) value ; }
public override int GetPartyOffset ( int slot ) = > 0x238 + ( 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 ( 0x0490 ) ) ;
set = > WriteUInt32LittleEndian ( Large . AsSpan ( 0x0490 ) , value ) ;
}
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
public override uint Coin
{
get = > ReadUInt16LittleEndian ( Large . AsSpan ( 0x0494 ) ) ;
set = > WriteUInt16LittleEndian ( Large . AsSpan ( 0x0494 ) , ( ushort ) ( value ) ) ;
}
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
private const int OFS_PCItem = 0x0498 ;
private const int OFS_PouchHeldItem = 0x0560 ;
private const int OFS_PouchKeyItem = 0x05B0 ;
private const int OFS_PouchBalls = 0x0600 ;
private const int OFS_PouchTMHM = 0x0640 ;
private const int OFS_PouchBerry = 0x0740 ;
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
protected override InventoryPouch3 [ ] GetItems ( )
{
const int max = 99 ;
var PCItems = ArrayUtil . ConcatAll ( Legal . Pouch_Items_RS , Legal . Pouch_Key_RS , Legal . Pouch_Ball_RS , Legal . Pouch_TMHM_RS , Legal . Pouch_Berries_RS ) ;
return new InventoryPouch3 [ ]
2021-03-16 06:51:58 +00:00
{
2022-06-18 18:04:24 +00:00
new ( InventoryType . Items , Legal . Pouch_Items_RS , max , OFS_PouchHeldItem , ( OFS_PouchKeyItem - OFS_PouchHeldItem ) / 4 ) ,
new ( InventoryType . KeyItems , Legal . Pouch_Key_RS , 1 , OFS_PouchKeyItem , ( OFS_PouchBalls - OFS_PouchKeyItem ) / 4 ) ,
new ( InventoryType . Balls , Legal . Pouch_Ball_RS , max , OFS_PouchBalls , ( OFS_PouchTMHM - OFS_PouchBalls ) / 4 ) ,
new ( InventoryType . TMHMs , Legal . Pouch_TMHM_RS , max , OFS_PouchTMHM , ( OFS_PouchBerry - OFS_PouchTMHM ) / 4 ) ,
new ( InventoryType . Berries , Legal . Pouch_Berries_RS , 999 , OFS_PouchBerry , 46 ) ,
new ( InventoryType . PCItems , PCItems , 999 , OFS_PCItem , ( OFS_PouchHeldItem - OFS_PCItem ) / 4 ) ,
} ;
}
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
public PokeBlock3Case PokeBlocks
{
get = > new ( Large , 0x7F8 ) ;
set = > SetData ( Large , value . Write ( ) , 0x7F8 ) ;
}
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
protected override int SeenOffset2 = > 0x938 ;
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
public DecorationInventory3 Decorations = > new ( Large . AsSpan ( 0x26A0 , DecorationInventory3 . SIZE ) ) ;
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
public Swarm3 Swarm
{
get = > new ( Large . Slice ( 0x2AFC , Swarm3 . SIZE ) ) ;
set = > SetData ( Large , value . Data , 0x2AFC ) ;
}
2021-08-21 02:52:31 +00:00
2022-06-18 18:04:24 +00:00
private void ClearSwarm ( ) = > Large . AsSpan ( 0x2AFC , Swarm3 . SIZE ) . Clear ( ) ;
2022-03-26 02:47:23 +00:00
2022-06-18 18:04:24 +00:00
public IReadOnlyList < Swarm3 > DefaultSwarms = > Swarm3Details . Swarms_RS ;
2021-08-21 03:33:53 +00:00
2022-06-18 18:04:24 +00:00
public int SwarmIndex
{
get = > Array . FindIndex ( Swarm3Details . Swarms_RS , z = > z . MapNum = = Swarm . MapNum ) ;
set
2021-08-21 03:33:53 +00:00
{
2022-06-18 18:04:24 +00:00
var arr = DefaultSwarms ;
if ( ( uint ) value > = arr . Count )
ClearSwarm ( ) ;
else
Swarm = arr [ value ] ;
2021-08-21 03:33:53 +00:00
}
2022-06-18 18:04:24 +00:00
}
2021-08-21 03:33:53 +00:00
2022-06-18 18:04:24 +00:00
protected override int MailOffset = > 0x2B4C ;
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
protected override int GetDaycareEXPOffset ( int slot ) = > GetDaycareSlotOffset ( 0 , 2 ) + ( 2 * 0x38 ) + ( 4 * slot ) ; // consecutive vals, after both consecutive slots & 2 mail
public override string GetDaycareRNGSeed ( int loc ) = > ReadUInt16LittleEndian ( Large . AsSpan ( GetDaycareEXPOffset ( 2 ) ) ) . ToString ( "X4" ) ;
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 = > 0x311B ;
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
#region eBerry
private const int OFFSET_EBERRY = 0x3160 ;
private const int SIZE_EBERRY = 0x530 ;
2021-03-16 06:51:58 +00:00
2022-06-18 18:04:24 +00:00
public byte [ ] GetEReaderBerry ( ) = > Large . Slice ( OFFSET_EBERRY , SIZE_EBERRY ) ;
public 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-28 01:58:51 +00:00
2022-06-18 18:04:24 +00:00
public override MysteryEvent3 MysteryEvent
{
get = > new ( Large . Slice ( 0x3690 , MysteryEvent3 . SIZE ) ) ;
set = > SetData ( Large , value . Data , 0x3690 ) ;
2021-03-16 06:51:58 +00:00
}
2022-06-18 18:04:24 +00:00
protected override int SeenOffset3 = > 0x3A8C ;
#endregion
2021-03-16 06:51:58 +00:00
}