2020-11-14 20:51:24 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-11-11 06:10:53 +00:00
|
|
|
|
using System.Linq;
|
2022-01-03 05:35:59 +00:00
|
|
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
2020-11-11 06:10:53 +00:00
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
|
{
|
2020-12-11 04:42:30 +00:00
|
|
|
|
/// <inheritdoc cref="EncounterArea" />
|
2020-11-11 06:10:53 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <see cref="GameVersion.GO"/> encounter area for direct-to-HOME transfers.
|
|
|
|
|
/// </summary>
|
2021-01-01 18:55:33 +00:00
|
|
|
|
public sealed record EncounterArea8g : EncounterArea, ISpeciesForm
|
2020-11-11 06:10:53 +00:00
|
|
|
|
{
|
2020-11-17 05:17:45 +00:00
|
|
|
|
/// <summary> Species for the area </summary>
|
|
|
|
|
/// <remarks> Due to how the encounter data is packaged by PKHeX, each species-form is grouped together. </remarks>
|
2020-11-14 20:51:24 +00:00
|
|
|
|
public int Species { get; }
|
2020-11-17 05:17:45 +00:00
|
|
|
|
/// <summary> Form of the Species </summary>
|
2020-11-14 20:51:24 +00:00
|
|
|
|
public int Form { get; }
|
2021-06-30 03:58:06 +00:00
|
|
|
|
public readonly EncounterSlot8GO[] Slots;
|
2020-11-11 06:10:53 +00:00
|
|
|
|
|
2021-06-30 03:58:06 +00:00
|
|
|
|
protected override IReadOnlyList<EncounterSlot> Raw => Slots;
|
|
|
|
|
|
2022-03-07 07:25:47 +00:00
|
|
|
|
private EncounterArea8g(ushort species, byte form, EncounterSlot8GO[] slots) : base(GameVersion.GO)
|
2020-11-11 06:10:53 +00:00
|
|
|
|
{
|
2020-11-14 20:51:24 +00:00
|
|
|
|
Species = species;
|
|
|
|
|
Form = form;
|
|
|
|
|
Location = Locations.GO8;
|
2021-06-30 03:58:06 +00:00
|
|
|
|
Slots = slots;
|
2020-11-11 06:10:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-05 01:20:56 +00:00
|
|
|
|
internal static EncounterArea8g[] GetArea(BinLinkerAccessor data)
|
2020-11-11 06:10:53 +00:00
|
|
|
|
{
|
2020-11-14 20:55:30 +00:00
|
|
|
|
var areas = new EncounterArea8g[data.Length];
|
2020-11-14 20:51:24 +00:00
|
|
|
|
for (int i = 0; i < areas.Length; i++)
|
2020-11-14 20:55:30 +00:00
|
|
|
|
areas[i] = GetArea(data[i]);
|
2020-11-14 20:51:24 +00:00
|
|
|
|
return areas;
|
2020-11-11 06:10:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-14 20:51:24 +00:00
|
|
|
|
private const int entrySize = (2 * sizeof(int)) + 2;
|
|
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
|
private static EncounterArea8g GetArea(ReadOnlySpan<byte> data)
|
2020-11-11 06:10:53 +00:00
|
|
|
|
{
|
2022-03-07 07:25:47 +00:00
|
|
|
|
var species = ReadUInt16LittleEndian(data);
|
|
|
|
|
byte form = (byte)(species >> 11);
|
|
|
|
|
species &= 0x3FF;
|
2020-11-11 06:10:53 +00:00
|
|
|
|
|
2020-11-14 20:51:24 +00:00
|
|
|
|
var group = GetGroup(species, form);
|
2020-11-11 06:10:53 +00:00
|
|
|
|
|
2020-11-14 20:51:24 +00:00
|
|
|
|
var result = new EncounterSlot8GO[(data.Length - 2) / entrySize];
|
2021-06-30 03:58:06 +00:00
|
|
|
|
var area = new EncounterArea8g(species, form, result);
|
2020-11-14 20:51:24 +00:00
|
|
|
|
for (int i = 0; i < result.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
var offset = (i * entrySize) + 2;
|
2022-01-03 05:35:59 +00:00
|
|
|
|
var entry = data.Slice(offset, entrySize);
|
|
|
|
|
result[i] = ReadSlot(entry, area, species, form, group);
|
2020-11-11 06:10:53 +00:00
|
|
|
|
}
|
2020-11-14 20:51:24 +00:00
|
|
|
|
|
|
|
|
|
return area;
|
2020-11-11 06:10:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 07:25:47 +00:00
|
|
|
|
private static EncounterSlot8GO ReadSlot(ReadOnlySpan<byte> entry, EncounterArea8g area, ushort species, byte form, GameVersion group)
|
2020-11-11 06:10:53 +00:00
|
|
|
|
{
|
2022-01-03 05:35:59 +00:00
|
|
|
|
int start = ReadInt32LittleEndian(entry);
|
|
|
|
|
int end = ReadInt32LittleEndian(entry[4..]);
|
|
|
|
|
var sg = entry[8];
|
2021-01-26 18:35:57 +00:00
|
|
|
|
var shiny = (Shiny)(sg & 0x3F);
|
2021-01-17 02:29:27 +00:00
|
|
|
|
var gender = (Gender)(sg >> 6);
|
2022-01-03 05:35:59 +00:00
|
|
|
|
var type = (PogoType)entry[9];
|
2021-01-17 02:29:27 +00:00
|
|
|
|
return new EncounterSlot8GO(area, species, form, start, end, shiny, gender, type, group);
|
2020-11-14 20:51:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static GameVersion GetGroup(int species, int form)
|
|
|
|
|
{
|
2020-11-17 05:17:45 +00:00
|
|
|
|
// Transfer Rules:
|
|
|
|
|
// If it can exist in LGP/E, it uses LGP/E's move data for the initial moves.
|
|
|
|
|
// Else, if it can exist in SW/SH, it uses SW/SH's move data for the initial moves.
|
|
|
|
|
// Else, it must exist in US/UM, thus it uses US/UM's moves.
|
|
|
|
|
|
2020-11-14 20:51:24 +00:00
|
|
|
|
var pt8 = PersonalTable.SWSH;
|
|
|
|
|
var ptGG = PersonalTable.GG;
|
|
|
|
|
|
|
|
|
|
var pi8 = (PersonalInfoSWSH)pt8[species];
|
|
|
|
|
if (pi8.IsPresentInGame)
|
|
|
|
|
{
|
2021-04-20 09:17:28 +00:00
|
|
|
|
bool lgpe = (species is (<= 151 or 808 or 809)) && (form == 0 || ptGG[species].HasForm(form));
|
2020-11-14 20:51:24 +00:00
|
|
|
|
return lgpe ? GameVersion.GG : GameVersion.SWSH;
|
|
|
|
|
}
|
|
|
|
|
if (species <= Legal.MaxSpeciesID_7_USUM)
|
|
|
|
|
{
|
2021-05-29 22:31:47 +00:00
|
|
|
|
bool lgpe = species <= 151 && (form == 0 || ptGG[species].HasForm(form));
|
2020-11-14 20:51:24 +00:00
|
|
|
|
return lgpe ? GameVersion.GG : GameVersion.USUM;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(species));
|
2020-11-11 06:10:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-08 01:29:36 +00:00
|
|
|
|
public override IEnumerable<EncounterSlot> GetMatchingSlots(PKM pkm, EvoCriteria[] chain)
|
2020-11-11 06:10:53 +00:00
|
|
|
|
{
|
|
|
|
|
if (pkm.TSV == 0) // HOME doesn't assign TSV=0 to accounts.
|
|
|
|
|
yield break;
|
|
|
|
|
|
2021-02-05 22:20:43 +00:00
|
|
|
|
// 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!
|
2020-12-11 04:42:30 +00:00
|
|
|
|
var sf = chain.FirstOrDefault(z => z.Species == Species && (z.Form == Form || FormInfo.IsFormChangeable(Species, Form, z.Form, pkm.Format)));
|
2022-04-24 04:33:17 +00:00
|
|
|
|
if (sf == default)
|
2020-11-14 20:51:24 +00:00
|
|
|
|
yield break;
|
2020-11-11 06:10:53 +00:00
|
|
|
|
|
2022-05-04 03:17:28 +00:00
|
|
|
|
var species = pkm.Species;
|
2020-11-14 20:51:24 +00:00
|
|
|
|
var ball = (Ball)pkm.Ball;
|
2022-04-24 04:33:17 +00:00
|
|
|
|
var met = Math.Max(sf.LevelMin, pkm.Met_Level);
|
2021-06-30 03:58:06 +00:00
|
|
|
|
EncounterSlot8GO? deferredIV = null;
|
2021-02-05 22:20:43 +00:00
|
|
|
|
|
2021-06-30 03:58:06 +00:00
|
|
|
|
foreach (var slot in Slots)
|
2020-11-14 20:51:24 +00:00
|
|
|
|
{
|
2020-11-16 23:32:22 +00:00
|
|
|
|
if (!slot.IsLevelWithinRange(met))
|
2020-11-14 20:51:24 +00:00
|
|
|
|
continue;
|
2022-05-04 03:17:28 +00:00
|
|
|
|
if (!slot.IsBallValid(ball, species))
|
2020-11-14 20:51:24 +00:00
|
|
|
|
continue;
|
|
|
|
|
if (!slot.Shiny.IsValid(pkm))
|
|
|
|
|
continue;
|
2021-01-17 02:29:27 +00:00
|
|
|
|
if (slot.Gender != Gender.Random && (int)slot.Gender != pkm.Gender)
|
|
|
|
|
continue;
|
2020-11-28 05:04:06 +00:00
|
|
|
|
|
2021-02-06 01:25:38 +00:00
|
|
|
|
if (!slot.GetIVsValid(pkm))
|
2021-02-05 22:20:43 +00:00
|
|
|
|
{
|
|
|
|
|
deferredIV ??= slot;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-14 20:51:24 +00:00
|
|
|
|
yield return slot;
|
2020-11-11 06:10:53 +00:00
|
|
|
|
}
|
2021-02-05 22:20:43 +00:00
|
|
|
|
|
|
|
|
|
if (deferredIV != null)
|
|
|
|
|
yield return deferredIV;
|
2020-11-11 06:10:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|