PKHeX/PKHeX.Core/Legality/Areas/EncounterArea7g.cs
Kurt 88830e0d00
Update from .NET Framework 4.6 to .NET 7 (#3729)
Updates from net46->net7, dropping support for mono in favor of using the latest runtime (along with the performance/API improvements). Releases will be posted as 64bit only for now.

Refactors a good amount of internal API methods to be more performant and more customizable for future updates & fixes.

Adds functionality for Batch Editor commands to `>`, `<` and <=/>=

TID/SID properties renamed to TID16/SID16 for clarity; other properties exposed for Gen7 / display variants.

Main window has a new layout to account for DPI scaling (8 point grid)

Fixed: Tatsugiri and Paldean Tauros now output Showdown form names as Showdown expects
Changed: Gen9 species now interact based on the confirmed National Dex IDs (closes #3724)
Fixed: Pokedex set all no longer clears species with unavailable non-base forms (closes #3720)
Changed: Hyper Training suggestions now apply for level 50 in SV. (closes #3714)
Fixed: B2/W2 hatched egg met locations exclusive to specific versions are now explicitly checked (closes #3691)
Added: Properties for ribbon/mark count (closes #3659)
Fixed: Traded SV eggs are now checked correctly (closes #3692)
2023-01-21 20:02:33 -08:00

114 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using static System.Buffers.Binary.BinaryPrimitives;
namespace PKHeX.Core;
/// <inheritdoc cref="EncounterArea" />
/// <summary>
/// <see cref="GameVersion.GO"/> encounter area for <see cref="GameVersion.GG"/>
/// </summary>
public sealed record EncounterArea7g : EncounterArea, ISpeciesForm
{
/// <summary> Species for the area </summary>
/// <remarks> Due to how the encounter data is packaged by PKHeX, each species-form is grouped together. </remarks>
public ushort Species { get; }
/// <summary> Form of the Species </summary>
public byte Form { get; }
public readonly EncounterSlot7GO[] Slots;
protected override IReadOnlyList<EncounterSlot7GO> Raw => Slots;
private EncounterArea7g(ushort species, byte form, EncounterSlot7GO[] slots) : base(GameVersion.GO)
{
Species = species;
Form = form;
Location = Locations.GO7;
Slots = slots;
}
internal static EncounterArea7g[] GetArea(BinLinkerAccessor data)
{
var areas = new EncounterArea7g[data.Length];
for (int i = 0; i < areas.Length; i++)
areas[i] = GetArea(data[i]);
return areas;
}
private const int meta = 4;
private const int entrySize = (2 * sizeof(int)) + 2;
private static EncounterArea7g GetArea(ReadOnlySpan<byte> data)
{
var species = ReadUInt16LittleEndian(data);
var form = data[2];
//var import = (EntityFormatDetected)data[3];
data = data[meta..];
var result = new EncounterSlot7GO[data.Length / entrySize];
var area = new EncounterArea7g(species, form, result);
for (int i = 0; i < result.Length; i++)
{
var offset = i * entrySize;
var entry = data.Slice(offset, entrySize);
result[i] = ReadSlot(entry, area, species, form);
}
return area;
}
private static EncounterSlot7GO ReadSlot(ReadOnlySpan<byte> entry, EncounterArea7g area, ushort species, byte form)
{
int start = ReadInt32LittleEndian(entry);
int end = ReadInt32LittleEndian(entry[4..]);
var sg = entry[8];
var shiny = (Shiny)(sg & 0x3F);
var gender = (Gender)(sg >> 6);
var type = (PogoType)entry[9];
return new EncounterSlot7GO(area, species, form, start, end, shiny, gender, type);
}
public override IEnumerable<EncounterSlot7GO> GetMatchingSlots(PKM pk, EvoCriteria[] chain)
{
// Find the first chain that has slots defined.
// Since it is possible to evolve before transferring, we only need the highest evolution species possible.
// PoGoEncTool has already extrapolated the evolutions to separate encounters!
var sf = Array.Find(chain, z => z.Species == Species && z.Form == Form);
if (sf == default)
return Array.Empty<EncounterSlot7GO>();
return GetMatchingSlots(pk, sf);
}
public IEnumerable<EncounterSlot7GO> GetMatchingSlots(PKM pk, EvoCriteria evo)
{
var stamp = EncounterSlotGO.GetTimeStamp(pk.Met_Year + 2000, pk.Met_Month, pk.Met_Day);
var met = Math.Max(evo.LevelMin, pk.Met_Level);
EncounterSlot7GO? deferredIV = null;
foreach (var slot in Slots)
{
if (!slot.IsLevelWithinRange(met))
continue;
//if (!slot.IsBallValid(ball)) -- can have any of the in-game balls due to re-capture
// continue;
if (!slot.Shiny.IsValid(pk))
continue;
//if (slot.Gender != Gender.Random && (int) slot.Gender != pk.Gender)
// continue;
if (!slot.IsWithinStartEnd(stamp))
continue;
if (!slot.GetIVsValid(pk))
{
deferredIV ??= slot;
continue;
}
yield return slot;
}
if (deferredIV != null)
yield return deferredIV;
}
}