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 ;
2018-12-30 05:24:38 +00:00
2022-06-18 18:04:24 +00:00
namespace PKHeX.Core ;
/// <summary>
/// Record of a received <see cref="WB7"/> file.
/// </summary>
/// <remarks>
/// A full <see cref="WB7"/> is not stored in the <see cref="SAV7b"/> structure, as it is immediately converted to <see cref="PKM"/> upon receiving from server.
/// The save file just stores a summary of the received data for the user to look back at.
/// </remarks>
public sealed class WR7 : DataMysteryGift
2018-12-30 05:24:38 +00:00
{
2022-06-18 18:04:24 +00:00
public const int Size = 0x140 ;
public override int Generation = > 7 ;
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 EntityContext Context = > EntityContext . Gen7 ;
2023-01-22 04:02:33 +00:00
public override bool FatefulEncounter = > true ;
2018-12-30 05:24:38 +00:00
2022-06-18 18:04:24 +00:00
public override GameVersion Version { get = > GameVersion . GG ; set { } }
2021-08-26 22:47:44 +00:00
2022-06-18 18:04:24 +00:00
public WR7 ( ) : this ( new byte [ Size ] ) { }
public WR7 ( byte [ ] data ) : base ( data ) { }
2018-12-30 05:24:38 +00:00
2022-06-18 18:04:24 +00:00
public override AbilityPermission Ability = > AbilityPermission . Any12H ; // undefined
2021-12-09 08:46:59 +00:00
2023-01-22 04:02:33 +00:00
public long Epoch
2022-06-18 18:04:24 +00:00
{
2023-01-22 04:02:33 +00:00
get = > ReadInt64LittleEndian ( Data . AsSpan ( 0x00 ) ) ;
set = > WriteInt64LittleEndian ( Data . AsSpan ( 0x00 ) , value ) ;
2022-06-18 18:04:24 +00:00
}
2018-12-30 05:24:38 +00:00
2023-01-22 04:02:33 +00:00
public DateOnly Date = > new DateOnly ( 1970 , 1 , 1 ) . AddDays ( ( int ) ( Epoch / 86400 ) ) ;
2018-12-30 05:24:38 +00:00
2022-06-18 18:04:24 +00:00
public override int CardID
{
get = > ReadUInt16LittleEndian ( Data . AsSpan ( 0x08 ) ) ;
set = > WriteUInt16LittleEndian ( Data . AsSpan ( 0x08 ) , ( ushort ) value ) ;
}
2018-12-30 05:24:38 +00:00
2022-06-18 18:04:24 +00:00
public ushort CardType
{
get = > ReadUInt16LittleEndian ( Data . AsSpan ( 0x0A ) ) ;
set = > WriteUInt16LittleEndian ( Data . AsSpan ( 0x0A ) , value ) ;
}
2018-12-30 05:24:38 +00:00
2022-06-18 18:04:24 +00:00
public WR7GiftType GiftType
{
get = > ( WR7GiftType ) Data [ 0x0C ] ;
set = > Data [ 0x0C ] = ( byte ) value ;
}
2018-12-30 05:24:38 +00:00
2022-06-18 18:04:24 +00:00
public int ItemCount
{
get = > Data [ 0x0D ] ;
set = > Data [ 0x0D ] = ( byte ) value ;
}
2018-12-30 05:24:38 +00:00
2022-06-18 18:04:24 +00:00
// unknown: region from 0x10 to 0xFF ?
2018-12-30 07:38:59 +00:00
2022-08-30 22:00:45 +00:00
public override ushort Species
2022-06-18 18:04:24 +00:00
{
get = > ReadUInt16LittleEndian ( Data . AsSpan ( 0x10C ) ) ;
2022-08-27 06:43:36 +00:00
set = > WriteUInt16LittleEndian ( Data . AsSpan ( 0x10C ) , value ) ;
2022-06-18 18:04:24 +00:00
}
2018-12-30 05:24:38 +00:00
2022-06-18 18:04:24 +00:00
public override bool GiftUsed { get ; set ; }
2018-12-30 06:09:27 +00:00
2022-06-18 18:04:24 +00:00
public override byte Level // are moves stored? mew has '1' but this could be move
{
get = > Data [ 0x10E ] ;
set = > Data [ 0x10E ] = value ;
}
2018-12-30 05:24:38 +00:00
2022-06-18 18:04:24 +00:00
public override int ItemID { get = > ReadUInt16LittleEndian ( Data . AsSpan ( 0x110 ) ) ; set = > WriteUInt16LittleEndian ( Data . AsSpan ( 0x110 ) , ( ushort ) value ) ; }
public ushort ItemIDCount { get = > ReadUInt16LittleEndian ( Data . AsSpan ( 0x112 ) ) ; set = > WriteUInt16LittleEndian ( Data . AsSpan ( 0x112 ) , value ) ; }
public ushort ItemSet2Item { get = > ReadUInt16LittleEndian ( Data . AsSpan ( 0x114 ) ) ; set = > WriteUInt16LittleEndian ( Data . AsSpan ( 0x114 ) , value ) ; }
public ushort ItemSet2Count { get = > ReadUInt16LittleEndian ( Data . AsSpan ( 0x116 ) ) ; set = > WriteUInt16LittleEndian ( Data . AsSpan ( 0x116 ) , value ) ; }
public ushort ItemSet3Item { get = > ReadUInt16LittleEndian ( Data . AsSpan ( 0x118 ) ) ; set = > WriteUInt16LittleEndian ( Data . AsSpan ( 0x118 ) , value ) ; }
public ushort ItemSet3Count { get = > ReadUInt16LittleEndian ( Data . AsSpan ( 0x11A ) ) ; set = > WriteUInt16LittleEndian ( Data . AsSpan ( 0x11A ) , value ) ; }
public ushort ItemSet4Item { get = > ReadUInt16LittleEndian ( Data . AsSpan ( 0x11C ) ) ; set = > WriteUInt16LittleEndian ( Data . AsSpan ( 0x11C ) , value ) ; }
public ushort ItemSet4Count { get = > ReadUInt16LittleEndian ( Data . AsSpan ( 0x11E ) ) ; set = > WriteUInt16LittleEndian ( Data . AsSpan ( 0x11E ) , value ) ; }
public ushort ItemSet5Item { get = > ReadUInt16LittleEndian ( Data . AsSpan ( 0x120 ) ) ; set = > WriteUInt16LittleEndian ( Data . AsSpan ( 0x120 ) , value ) ; } // struct union overlaps OT Name data, beware!
public ushort ItemSet5Count { get = > ReadUInt16LittleEndian ( Data . AsSpan ( 0x122 ) ) ; set = > WriteUInt16LittleEndian ( Data . AsSpan ( 0x122 ) , value ) ; }
public ushort ItemSet6Item { get = > ReadUInt16LittleEndian ( Data . AsSpan ( 0x124 ) ) ; set = > WriteUInt16LittleEndian ( Data . AsSpan ( 0x124 ) , value ) ; }
public ushort ItemSet6Count { get = > ReadUInt16LittleEndian ( Data . AsSpan ( 0x126 ) ) ; set = > WriteUInt16LittleEndian ( Data . AsSpan ( 0x126 ) , value ) ; }
public override int Gender { get ; set ; }
2022-08-27 06:43:36 +00:00
public override byte Form { get ; set ; }
2023-01-22 04:02:33 +00:00
public override uint ID32 { get ; set ; }
public override ushort TID16 { get ; set ; }
public override ushort SID16 { get ; set ; }
2022-06-18 18:04:24 +00:00
public override string OT_Name
{
get = > StringConverter8 . GetString ( Data . AsSpan ( 0x120 , 0x1A ) ) ;
2023-01-22 04:02:33 +00:00
set = > StringConverter8 . SetString ( Data . AsSpan ( 0x120 , 0x1A ) , value , 12 , StringConverterOption . ClearZero ) ;
2022-06-18 18:04:24 +00:00
}
2018-12-30 05:24:38 +00:00
2022-06-18 18:04:24 +00:00
public LanguageID LanguageReceived
{
get = > ( LanguageID ) Data [ 0x13A ] ;
set = > Data [ 0x13A ] = ( byte ) value ;
}
2018-12-30 05:24:38 +00:00
2022-06-18 18:04:24 +00:00
// Mystery Gift implementation, unused.
public override bool IsMatchExact ( PKM pk , EvoCriteria evo ) = > false ;
protected override bool IsMatchDeferred ( PKM pk ) = > false ;
protected override bool IsMatchPartial ( PKM pk ) = > false ;
public override Shiny Shiny = > Shiny . Never ;
Fracture the encounter matching checks to allow progressive validation (#3137)
## Issue
We want to discard-but-remember any slots that aren't a perfect fit, on the off chance that a better one exists later in the search space. If there's no better match, then we gotta go with what we got.
## Example:
Wurmple exists in area `X`, and also has a more rare slot for Silcoon, with the same level for both slots.
* We have a Silcoon that we've leveled up a few times.
Was our Silcoon originally a Wurmple, or was it caught as a Silcoon?
* To be sure, we have to check the EC/PID if the Wurmple wouldn't evolve into Cascoon instead.
* We don't want to wholly reject that Wurmple slot, as maybe the Met Level isn't within Silcoon's slot range.
---
Existing implementation would store "deferred" matches in a list; we only need to keep 1 of these matches around (less allocation!). We also want to differentiate between a "good" deferral and a "bad" deferral; I don't think this is necessary but it's currently used by Mystery Gift matching (implemented for the Eeveelution mystery gifts which matter for evolution moves).
The existing logic didn't use inheritance, and instead had static methods being reused across generations. Quite kludgy. Also, the existing logic was a pain to modify the master encounter yield methods, as one generation's quirks had to not impact all other generations that used the method.
---
The new implementation splits out the encounter yielding methods to be separate for each generation / subset. Now, things don't have to check `WasLink` for Gen7 origin, because Pokémon Link wasn't a thing in Gen7.
---
## Future
Maybe refactoring yielders into "GameCores" that expose yielding behaviors / properties, rather than the static logic. As more generations and side-gamegroups get added (thanks LGPE/GO/GameCube), all this switch stuff gets annoying to maintain instead of just overriding/inheritance.
## Conclusion
This shouldn't impact any legality results negatively; if you notice any regressions, report them! This should reduce false flags where we didn't defer-discard an encounter when we should have (wild area mons being confused with raids).
2021-01-30 01:55:27 +00:00
2022-06-18 18:04:24 +00:00
public override int Location { get ; set ; }
public override int EggLocation { get ; set ; }
public override int Ball { get ; set ; } = 4 ;
2018-12-30 06:09:27 +00:00
2022-06-18 18:04:24 +00:00
public override string CardTitle { get = > $"{nameof(WB7)} Record ({OT_Name}) [{LanguageReceived}]" ; set { } }
2018-12-30 06:09:27 +00:00
2022-06-18 18:04:24 +00:00
public override bool IsItem
{
get = > GiftType = = WR7GiftType . Item ;
set
2018-12-30 06:09:27 +00:00
{
2022-06-18 18:04:24 +00:00
if ( value )
GiftType = WR7GiftType . Item ;
2018-12-30 06:09:27 +00:00
}
2022-06-18 18:04:24 +00:00
}
2018-12-30 06:09:27 +00:00
2022-06-18 18:04:24 +00:00
public override bool IsEntity
{
get = > GiftType = = WR7GiftType . Pokemon ;
set
2018-12-30 06:09:27 +00:00
{
2022-06-18 18:04:24 +00:00
if ( value )
GiftType = WR7GiftType . Pokemon ;
2018-12-30 06:09:27 +00:00
}
2022-06-18 18:04:24 +00:00
}
2018-12-30 07:33:36 +00:00
2023-01-22 04:02:33 +00:00
public override PB7 ConvertToPKM ( ITrainerInfo tr , EncounterCriteria criteria )
2022-06-18 18:04:24 +00:00
{
// this method shouldn't really be called, use the WB7 data not the WR7 data.
if ( ! IsEntity )
throw new ArgumentException ( nameof ( IsEntity ) ) ;
2018-12-30 07:33:36 +00:00
2022-06-18 18:04:24 +00:00
// we'll just generate something as close as we can, since we must return something!
var pk = new PB7 ( ) ;
tr . ApplyTo ( pk ) ;
if ( ! GameVersion . GG . Contains ( ( GameVersion ) tr . Game ) )
pk . Version = ( int ) GameVersion . GP ;
2018-12-30 07:33:36 +00:00
2022-06-18 18:04:24 +00:00
pk . Species = Species ;
pk . Met_Level = pk . CurrentLevel = Level ;
pk . MetDate = Date ;
2018-12-30 07:33:36 +00:00
2022-06-18 18:04:24 +00:00
return pk ; // can't really do much more, just return the rough data
2018-12-30 05:24:38 +00:00
}
}