PKHeX/PKHeX.Core/Legality/Encounters/EncounterStatic/EncounterStatic8.cs

121 lines
4.1 KiB
C#
Raw Normal View History

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;
using System.Collections.Generic;
using static PKHeX.Core.OverworldCorrelation8Requirement;
namespace PKHeX.Core;
/// <summary>
/// Generation 8 Static Encounter
/// </summary>
/// <inheritdoc cref="EncounterStatic"/>
public record EncounterStatic8(GameVersion Version) : EncounterStatic(Version), IDynamaxLevel, IGigantamax, IRelearn, IOverworldCorrelation8
{
public sealed 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.Gen8;
public bool ScriptedNoMarks { get; init; }
public bool CanGigantamax { get; set; }
public byte DynamaxLevel { get; set; }
public IReadOnlyList<int> Relearn { get; init; } = Array.Empty<int>();
public AreaWeather8 Weather {get; init; } = AreaWeather8.Normal;
protected override bool IsMatchLevel(PKM pk, EvoCriteria evo)
{
var met = pk.Met_Level;
var lvl = Level;
if (met == lvl)
return true;
if (lvl < EncounterArea8.BoostLevel && EncounterArea8.IsBoostedArea60(Location))
return met == EncounterArea8.BoostLevel;
return false;
}
public override bool IsMatchExact(PKM pk, EvoCriteria evo)
{
if (pk is PK8 d && d.DynamaxLevel < DynamaxLevel)
return false;
if (pk.Met_Level < EncounterArea8.BoostLevel && Weather is AreaWeather8.Heavy_Fog && EncounterArea8.IsBoostedArea60Fog(Location))
return false;
return base.IsMatchExact(pk, evo);
}
protected override void ApplyDetails(ITrainerInfo tr, EncounterCriteria criteria, PKM pk)
{
base.ApplyDetails(tr, criteria, pk);
if (Weather is AreaWeather8.Heavy_Fog && EncounterArea8.IsBoostedArea60Fog(Location))
pk.CurrentLevel = pk.Met_Level = EncounterArea8.BoostLevel;
var req = GetRequirement(pk);
if (req != MustHave)
{
pk.SetRandomEC();
return;
}
var shiny = Shiny == Shiny.Random ? Shiny.FixedValue : Shiny;
Overworld8RNG.ApplyDetails(pk, criteria, shiny, FlawlessIVCount);
}
public bool IsOverworldCorrelation
{
get
{
if (Gift)
return false; // gifts can have any 128bit seed from overworld
if (ScriptedNoMarks)
return false; // scripted encounters don't act as saved spawned overworld encounters
return true;
}
}
public OverworldCorrelation8Requirement GetRequirement(PKM pk) => IsOverworldCorrelation
? MustHave
: MustNotHave;
2021-02-14 23:14:45 +00:00
public bool IsOverworldCorrelationCorrect(PKM pk)
{
return Overworld8RNG.ValidateOverworldEncounter(pk, Shiny == Shiny.Random ? Shiny.FixedValue : Shiny, FlawlessIVCount);
}
2021-02-14 23:14:45 +00:00
public override EncounterMatchRating GetMatchRating(PKM pk)
{
var rating = base.GetMatchRating(pk);
if (rating != EncounterMatchRating.Match)
return rating;
var req = GetRequirement(pk);
bool correlation = IsOverworldCorrelationCorrect(pk);
if ((req == MustHave) != correlation)
return EncounterMatchRating.DeferredErrors;
// Only encounter slots can have these marks; defer for collisions.
if (pk.Species == (int) Core.Species.Shedinja)
{
// Loses Mark on evolution to Shedinja, but not affixed ribbon value.
return pk switch
{
IRibbonSetMark8 {RibbonMarkCurry: true} => EncounterMatchRating.DeferredErrors,
PK8 {AffixedRibbon: (int) RibbonIndex.MarkCurry} => EncounterMatchRating.Deferred,
_ => EncounterMatchRating.Match,
};
2021-02-14 23:14:45 +00:00
}
if (pk is IRibbonSetMark8 m && (m.RibbonMarkCurry || m.RibbonMarkFishing || m.HasWeatherMark()))
return EncounterMatchRating.DeferredErrors;
return EncounterMatchRating.Match;
}
}
public interface IOverworldCorrelation8
{
OverworldCorrelation8Requirement GetRequirement(PKM pk);
bool IsOverworldCorrelationCorrect(PKM pk);
}
public enum OverworldCorrelation8Requirement
{
CanBeEither,
MustHave,
MustNotHave,
}