PKHeX/PKHeX.Core/Legality/Areas/EncounterArea8g.cs
Kurt 1e86fdcea8
Fracture the encounter matching checks to allow progressive validation (#3137)
## Issue

We want to discard-but-remember any slots that aren't a perfect fit, on the off chance that a better one exists later in the search space. If there's no better match, then we gotta go with what we got.

## Example:
Wurmple exists in area `X`, and also has a more rare slot for Silcoon, with the same level for both slots. 
* We have a Silcoon that we've leveled up a few times.

Was our Silcoon originally a Wurmple, or was it caught as a Silcoon? 
* To be sure, we have to check the EC/PID if the Wurmple wouldn't evolve into Cascoon instead.
* We don't want to wholly reject that Wurmple slot, as maybe the Met Level isn't within Silcoon's slot range.

---

Existing implementation would store "deferred" matches in a list; we only need to keep 1 of these matches around (less allocation!). We also want to differentiate between a "good" deferral and a "bad" deferral; I don't think this is necessary but it's currently used by Mystery Gift matching (implemented for the Eeveelution mystery gifts which matter for evolution moves).

The existing logic didn't use inheritance, and instead had static methods being reused across generations. Quite kludgy. Also, the existing logic was a pain to modify the master encounter yield methods, as one generation's quirks had to not impact all other generations that used the method.

---

The new implementation splits out the encounter yielding methods to be separate for each generation / subset. Now, things don't have to check `WasLink` for Gen7 origin, because Pokémon Link wasn't a thing in Gen7.

---

## Future
Maybe refactoring yielders into "GameCores" that expose yielding behaviors / properties, rather than the static logic. As more generations and side-gamegroups get added (thanks LGPE/GO/GameCube), all this switch stuff gets annoying to maintain instead of just overriding/inheritance.

## Conclusion

This shouldn't impact any legality results negatively; if you notice any regressions, report them! This should reduce false flags where we didn't defer-discard an encounter when we should have (wild area mons being confused with raids).
2021-01-29 17:55:27 -08:00

118 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace PKHeX.Core
{
/// <inheritdoc cref="EncounterArea" />
/// <summary>
/// <see cref="GameVersion.GO"/> encounter area for direct-to-HOME transfers.
/// </summary>
public sealed record EncounterArea8g : 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 int Species { get; }
/// <summary> Form of the Species </summary>
public int Form { get; }
private EncounterArea8g(int species, int form) : base(GameVersion.GO)
{
Species = species;
Form = form;
Location = Locations.GO8;
}
internal static EncounterArea8g[] GetArea(byte[][] data)
{
var areas = new EncounterArea8g[data.Length];
for (int i = 0; i < areas.Length; i++)
areas[i] = GetArea(data[i]);
return areas;
}
private const int entrySize = (2 * sizeof(int)) + 2;
private static EncounterArea8g GetArea(byte[] data)
{
var sf = BitConverter.ToInt16(data, 0);
int species = sf & 0x7FF;
int form = sf >> 11;
var group = GetGroup(species, form);
var result = new EncounterSlot8GO[(data.Length - 2) / entrySize];
var area = new EncounterArea8g(species, form) {Slots = result};
for (int i = 0; i < result.Length; i++)
{
var offset = (i * entrySize) + 2;
result[i] = ReadSlot(data, offset, area, species, form, group);
}
return area;
}
private static EncounterSlot8GO ReadSlot(byte[] data, int offset, EncounterArea8g area, int species, int form, GameVersion group)
{
int start = BitConverter.ToInt32(data, offset);
int end = BitConverter.ToInt32(data, offset + 4);
var sg = data[offset + 8];
var shiny = (Shiny)(sg & 0x3F);
var gender = (Gender)(sg >> 6);
var type = (PogoType)data[offset + 9];
return new EncounterSlot8GO(area, species, form, start, end, shiny, gender, type, group);
}
private static GameVersion GetGroup(int species, int form)
{
// 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.
var pt8 = PersonalTable.SWSH;
var ptGG = PersonalTable.GG;
var pi8 = (PersonalInfoSWSH)pt8[species];
if (pi8.IsPresentInGame)
{
bool lgpe = (species <= 151 || species is 808 or 809) && (form == 0 || ptGG[species].HasForm(form));
return lgpe ? GameVersion.GG : GameVersion.SWSH;
}
if (species <= Legal.MaxSpeciesID_7_USUM)
{
bool lgpe = species <= 151 && (form == 0 || ptGG[species].HasForm(form));
return lgpe ? GameVersion.GG : GameVersion.USUM;
}
throw new ArgumentOutOfRangeException(nameof(species));
}
public override IEnumerable<EncounterSlot> GetMatchingSlots(PKM pkm, IReadOnlyList<EvoCriteria> chain)
{
if (pkm.TSV == 0) // HOME doesn't assign TSV=0 to accounts.
yield break;
var sf = chain.FirstOrDefault(z => z.Species == Species && (z.Form == Form || FormInfo.IsFormChangeable(Species, Form, z.Form, pkm.Format)));
if (sf == null)
yield break;
var ball = (Ball)pkm.Ball;
var met = Math.Max(sf.MinLevel, pkm.Met_Level);
foreach (var s in Slots)
{
var slot = (EncounterSlot8GO)s;
if (!slot.IsLevelWithinRange(met))
continue;
if (!slot.IsBallValid(ball))
continue;
if (!slot.Shiny.IsValid(pkm))
continue;
if (slot.Gender != Gender.Random && (int)slot.Gender != pkm.Gender)
continue;
yield return slot;
}
}
}
}