PKHeX/PKHeX.Core/Legality/Encounters/EncounterTrade/EncounterTrade1.cs
Kurt 9166d0eb64
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 16:15:27 -07:00

138 lines
4 KiB
C#

using System.Linq;
namespace PKHeX.Core;
/// <summary>
/// Trade Encounter data with a fixed Catch Rate
/// </summary>
/// <remarks>
/// Generation 1 specific value used in detecting unmodified/un-traded Generation 1 Trade Encounter data.
/// Species &amp; Minimum level (legal) possible to acquire at.
/// </remarks>
public sealed record EncounterTrade1 : EncounterTradeGB
{
public override int Generation => 1;
public override EntityContext Context => EntityContext.Gen1;
public override byte LevelMin => CanObtainMinGSC() ? LevelMinGSC : LevelMinRBY;
private readonly byte LevelMinRBY;
private readonly byte LevelMinGSC;
public override int Location => 0;
public override Shiny Shiny => Shiny.Random;
public EncounterTrade1(int species, GameVersion game, byte rby, byte gsc) : base(species, gsc, game)
{
TrainerNames = StringConverter12.G1TradeOTName;
LevelMinRBY = rby;
LevelMinGSC = gsc;
}
public EncounterTrade1(int species, GameVersion game, byte rby) : this(species, game, rby, rby) { }
public byte GetInitialCatchRate()
{
var pt = Version == GameVersion.YW ? PersonalTable.Y : PersonalTable.RB;
return (byte)pt[Species].CatchRate;
}
protected override void ApplyDetails(ITrainerInfo sav, EncounterCriteria criteria, PKM pk)
{
base.ApplyDetails(sav, criteria, pk);
var pk1 = (PK1)pk;
pk1.Catch_Rate = GetInitialCatchRate();
}
internal bool IsNicknameValid(PKM pk)
{
var nick = pk.Nickname;
if (pk.Format <= 2)
return Nicknames.Contains(nick);
// Converted string 1/2->7 to language specific value
// Nicknames can be from any of the languages it can trade between.
int lang = pk.Language;
if (lang == 1)
{
// Special consideration for Hiragana strings that are transferred
if (Version == GameVersion.YW && Species == (int)Core.Species.Dugtrio)
return nick == "ぐりお";
return nick == Nicknames[1];
}
return GetNicknameIndex(nick) >= 2;
}
internal bool IsTrainerNameValid(PKM pk)
{
string ot = pk.OT_Name;
if (pk.Format <= 2)
return ot == StringConverter12.G1TradeOTStr;
// Converted string 1/2->7 to language specific value
int lang = pk.Language;
var tr = GetOT(lang);
return ot == tr;
}
private int GetNicknameIndex(string nickname)
{
var nn = Nicknames;
for (int i = 0; i < nn.Count; i++)
{
if (nn[i] == nickname)
return i;
}
return -1;
}
private bool CanObtainMinGSC()
{
if (!ParseSettings.AllowGen1Tradeback)
return false;
if (Version == GameVersion.BU && EvolveOnTrade)
return ParseSettings.AllowGBCartEra;
return true;
}
private bool IsMatchLevel(PKM pk, int lvl)
{
if (pk is not PK1)
return lvl >= LevelMinGSC;
return lvl >= LevelMin;
}
public override bool IsMatchExact(PKM pk, EvoCriteria evo)
{
if (!IsMatchLevel(pk, pk.CurrentLevel)) // minimum required level
return false;
if (Version == GameVersion.BU)
{
// Encounters with this version have to originate from the Japanese Blue game.
if (!pk.Japanese)
return false;
// Stadium 2 can transfer from GSC->RBY without a "Trade", thus allowing un-evolved outsiders
if (EvolveOnTrade && !ParseSettings.AllowGBCartEra && pk.CurrentLevel < LevelMinRBY)
return false;
}
return true;
}
protected override bool IsMatchPartial(PKM pk)
{
if (!IsTrainerNameValid(pk))
return true;
if (!IsNicknameValid(pk))
return true;
if (ParseSettings.AllowGen1Tradeback)
return false;
if (pk is not PK1 pk1)
return false;
var req = GetInitialCatchRate();
return req != pk1.Catch_Rate;
}
}