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;
|
2021-11-20 02:23:49 +00:00
|
|
|
using static PKHeX.Core.StaticCorrelation8bRequirement;
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Generation 7 Static Encounter
|
|
|
|
/// </summary>
|
|
|
|
/// <inheritdoc cref="EncounterStatic"/>
|
|
|
|
public sealed record EncounterStatic8b : EncounterStatic, IStaticCorrelation8b
|
2021-11-20 02:23:49 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
public override int Generation => 8;
|
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.Gen8b;
|
2021-11-20 02:23:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public bool Roaming { get; init; }
|
|
|
|
public override bool EggEncounter => EggLocation != Locations.Default8bNone;
|
2021-11-20 02:23:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public EncounterStatic8b(GameVersion game) : base(game) => EggLocation = Locations.Default8bNone;
|
2021-11-20 02:23:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override bool IsMatchLocation(PKM pk)
|
|
|
|
{
|
|
|
|
if (pk is PK8)
|
2022-09-27 16:57:42 +00:00
|
|
|
return Locations.IsValidMetBDSP((ushort)pk.Met_Location, pk.Version);
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!Roaming)
|
|
|
|
return base.IsMatchLocation(pk);
|
|
|
|
return IsRoamingLocation(pk);
|
|
|
|
}
|
2022-03-06 21:03:39 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static bool IsRoamingLocation(PKM pk)
|
|
|
|
{
|
|
|
|
var location = pk.Met_Location;
|
|
|
|
foreach (var value in Roaming_MetLocation_BDSP)
|
2022-03-06 21:03:39 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (value == location)
|
|
|
|
return true;
|
2021-11-20 02:23:49 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-11-20 02:23:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public StaticCorrelation8bRequirement GetRequirement(PKM pk) => Roaming
|
|
|
|
? MustHave
|
|
|
|
: MustNotHave;
|
2021-11-20 02:23:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public bool IsStaticCorrelationCorrect(PKM pk)
|
|
|
|
{
|
|
|
|
return Roaming8bRNG.ValidateRoamingEncounter(pk, Shiny == Shiny.Random ? Shiny.FixedValue : Shiny, FlawlessIVCount);
|
|
|
|
}
|
2021-11-20 02:23:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override bool IsMatchEggLocation(PKM pk)
|
|
|
|
{
|
|
|
|
if (pk is not PB8)
|
2021-11-20 02:23:49 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!EggEncounter)
|
|
|
|
return pk.Egg_Location == 0;
|
|
|
|
|
|
|
|
if (pk is PK8)
|
2022-05-31 04:43:52 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (EggLocation > 60000 && pk.Egg_Location == Locations.HOME_SWSHBDSPEgg)
|
|
|
|
return true;
|
|
|
|
// >60000 can be reset to Link Trade (30001), then altered differently.
|
2022-09-27 16:57:42 +00:00
|
|
|
return Locations.IsValidMetBDSP((ushort)pk.Egg_Location, pk.Version) && pk.Egg_Location == pk.Met_Location;
|
2022-05-31 04:43:52 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Hatched
|
|
|
|
return pk.Egg_Location == EggLocation || pk.Egg_Location == Locations.LinkTrade6NPC;
|
|
|
|
}
|
2021-11-20 02:23:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var eggloc = pk.Egg_Location;
|
|
|
|
if (!EggEncounter)
|
|
|
|
return eggloc == EggLocation;
|
2021-11-20 02:23:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!pk.IsEgg) // hatched
|
|
|
|
return eggloc == EggLocation || eggloc == Locations.LinkTrade6NPC;
|
2021-11-20 02:23:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Unhatched:
|
|
|
|
if (eggloc != EggLocation)
|
|
|
|
return false;
|
|
|
|
if (pk.Met_Location is not (Locations.Default8bNone or Locations.LinkTrade6NPC))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2021-11-20 02:23:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override void ApplyDetails(ITrainerInfo tr, EncounterCriteria criteria, PKM pk)
|
|
|
|
{
|
|
|
|
pk.Met_Location = pk.Egg_Location = Locations.Default8bNone;
|
|
|
|
base.ApplyDetails(tr, criteria, pk);
|
|
|
|
var req = GetRequirement(pk);
|
|
|
|
if (req == MustHave) // Roamers
|
2021-11-20 02:23:49 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var shiny = Shiny == Shiny.Random ? Shiny.FixedValue : Shiny;
|
|
|
|
Roaming8bRNG.ApplyDetails(pk, criteria, shiny, FlawlessIVCount);
|
2021-11-20 02:23:49 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
else
|
2021-11-20 02:23:49 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var shiny = Shiny == Shiny.Never ? Shiny.Never : Shiny.Random;
|
|
|
|
Wild8bRNG.ApplyDetails(pk, criteria, shiny, FlawlessIVCount, Ability);
|
|
|
|
}
|
2021-11-20 02:23:49 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override void SetMetData(PKM pk, int level, DateTime today)
|
2021-11-20 02:23:49 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
pk.Met_Level = level;
|
|
|
|
pk.Met_Location = !Roaming ? Location : Roaming_MetLocation_BDSP[0];
|
2023-01-22 04:02:33 +00:00
|
|
|
pk.MetDate = DateOnly.FromDateTime(today);
|
2021-11-20 02:23:49 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// defined by mvpoke in encounter data
|
2023-03-26 00:55:55 +00:00
|
|
|
private static ReadOnlySpan<ushort> Roaming_MetLocation_BDSP => new ushort[]
|
2021-11-20 02:23:49 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
197, 201, 354, 355, 356, 357, 358, 359, 361, 362, 364, 365, 367, 373, 375, 377,
|
|
|
|
378, 379, 383, 385, 392, 394, 395, 397, 400, 403, 404, 407,
|
|
|
|
485,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface IStaticCorrelation8b
|
|
|
|
{
|
|
|
|
StaticCorrelation8bRequirement GetRequirement(PKM pk);
|
|
|
|
bool IsStaticCorrelationCorrect(PKM pk);
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum StaticCorrelation8bRequirement
|
|
|
|
{
|
|
|
|
CanBeEither,
|
|
|
|
MustHave,
|
|
|
|
MustNotHave,
|
2021-11-20 02:23:49 +00:00
|
|
|
}
|