2019-09-13 06:20:52 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2022-01-03 05:35:59 +00:00
|
|
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
2019-09-13 06:20:52 +00:00
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
|
{
|
2021-01-01 18:55:33 +00:00
|
|
|
|
/// <inheritdoc cref="EncounterArea" />
|
2019-09-13 06:20:52 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <see cref="GameVersion.Gen4"/> encounter area
|
|
|
|
|
/// </summary>
|
2021-01-01 18:55:33 +00:00
|
|
|
|
public sealed record EncounterArea4 : EncounterArea
|
2019-09-13 06:20:52 +00:00
|
|
|
|
{
|
2020-08-30 17:23:22 +00:00
|
|
|
|
public readonly int Rate;
|
2021-06-30 03:58:06 +00:00
|
|
|
|
public readonly GroundTilePermission GroundTile;
|
|
|
|
|
public readonly EncounterSlot4[] Slots;
|
|
|
|
|
|
|
|
|
|
protected override IReadOnlyList<EncounterSlot> Raw => Slots;
|
2019-09-13 06:20:52 +00:00
|
|
|
|
|
2022-02-05 01:20:56 +00:00
|
|
|
|
public static EncounterArea4[] GetAreas(BinLinkerAccessor input, GameVersion game)
|
2020-08-30 17:23:22 +00:00
|
|
|
|
{
|
|
|
|
|
var result = new EncounterArea4[input.Length];
|
2022-02-05 01:20:56 +00:00
|
|
|
|
for (int i = 0; i < result.Length; i++)
|
2020-08-30 17:23:22 +00:00
|
|
|
|
result[i] = new EncounterArea4(input[i], game);
|
|
|
|
|
return result;
|
2019-09-13 06:20:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
|
private EncounterArea4(ReadOnlySpan<byte> data, GameVersion game) : base(game)
|
2019-09-13 06:20:52 +00:00
|
|
|
|
{
|
2022-01-03 05:35:59 +00:00
|
|
|
|
Location = ReadUInt16LittleEndian(data);
|
2020-08-30 17:23:22 +00:00
|
|
|
|
Type = (SlotType)data[2];
|
|
|
|
|
Rate = data[3];
|
2021-06-30 03:58:06 +00:00
|
|
|
|
// although GroundTilePermission flags are 32bit, none have values > 16bit.
|
2022-01-03 05:35:59 +00:00
|
|
|
|
GroundTile = (GroundTilePermission)ReadUInt16LittleEndian(data[4..]);
|
2020-08-30 17:23:22 +00:00
|
|
|
|
|
2020-08-30 18:08:21 +00:00
|
|
|
|
Slots = ReadRegularSlots(data);
|
2019-09-13 06:20:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
|
private EncounterSlot4[] ReadRegularSlots(ReadOnlySpan<byte> data)
|
2019-09-13 06:20:52 +00:00
|
|
|
|
{
|
2020-08-30 17:23:22 +00:00
|
|
|
|
const int size = 10;
|
|
|
|
|
int count = (data.Length - 6) / size;
|
|
|
|
|
var slots = new EncounterSlot4[count];
|
|
|
|
|
for (int i = 0; i < slots.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
int offset = 6 + (size * i);
|
2022-01-03 05:35:59 +00:00
|
|
|
|
var entry = data.Slice(offset, size);
|
|
|
|
|
slots[i] = ReadRegularSlot(entry);
|
2020-08-30 17:23:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return slots;
|
2019-09-13 06:20:52 +00:00
|
|
|
|
}
|
2019-09-19 23:19:07 +00:00
|
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
|
private EncounterSlot4 ReadRegularSlot(ReadOnlySpan<byte> entry)
|
|
|
|
|
{
|
|
|
|
|
int species = ReadUInt16LittleEndian(entry);
|
|
|
|
|
int form = entry[2];
|
|
|
|
|
int slotNum = entry[3];
|
|
|
|
|
int min = entry[4];
|
|
|
|
|
int max = entry[5];
|
|
|
|
|
int mpi = entry[6];
|
|
|
|
|
int mpc = entry[7];
|
|
|
|
|
int sti = entry[8];
|
|
|
|
|
int stc = entry[9];
|
|
|
|
|
return new EncounterSlot4(this, species, form, min, max, slotNum, mpi, mpc, sti, stc);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-21 23:35:49 +00:00
|
|
|
|
public override IEnumerable<EncounterSlot> GetMatchingSlots(PKM pkm, IReadOnlyList<EvoCriteria> chain)
|
2019-09-19 23:19:07 +00:00
|
|
|
|
{
|
2020-08-21 23:35:49 +00:00
|
|
|
|
if (pkm.Format != 4) // Met Location and Met Level are changed on PK4->PK5
|
|
|
|
|
return GetSlotsFuzzy(chain);
|
|
|
|
|
if (pkm.Met_Location != Location)
|
2021-06-30 03:58:06 +00:00
|
|
|
|
return Array.Empty<EncounterSlot4>();
|
2021-12-25 21:48:28 +00:00
|
|
|
|
return GetSlotsMatching(chain, pkm.Met_Level, pkm);
|
2020-08-21 23:35:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-25 21:48:28 +00:00
|
|
|
|
private IEnumerable<EncounterSlot4> GetSlotsMatching(IReadOnlyList<EvoCriteria> chain, int lvl, PKM pk)
|
2020-08-21 23:35:49 +00:00
|
|
|
|
{
|
|
|
|
|
foreach (var slot in Slots)
|
|
|
|
|
{
|
|
|
|
|
foreach (var evo in chain)
|
|
|
|
|
{
|
|
|
|
|
if (slot.Species != evo.Species)
|
|
|
|
|
continue;
|
2019-09-19 23:19:07 +00:00
|
|
|
|
|
2021-09-07 22:35:36 +00:00
|
|
|
|
if (slot.Form != evo.Form && slot.Species is not (int)Species.Burmy)
|
|
|
|
|
{
|
|
|
|
|
// Unown forms are random, not specific form IDs
|
|
|
|
|
if (!slot.IsRandomUnspecificForm)
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-08-21 23:35:49 +00:00
|
|
|
|
if (!slot.IsLevelWithinRange(lvl))
|
|
|
|
|
break;
|
|
|
|
|
|
2021-12-25 21:48:28 +00:00
|
|
|
|
if (Type is SlotType.HoneyTree && IsInaccessibleHoneySlotLocation(slot, pk))
|
|
|
|
|
break;
|
|
|
|
|
|
2020-08-21 23:35:49 +00:00
|
|
|
|
yield return slot;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-25 21:48:28 +00:00
|
|
|
|
private static bool IsInaccessibleHoneySlotLocation(EncounterSlot4 slot, PKM pk)
|
|
|
|
|
{
|
|
|
|
|
// A/B/C tables, only Munchlax is a 'C' encounter, and A/B are accessible from any tree.
|
|
|
|
|
// C table encounters are only available from 4 trees, which are determined by TID/SID of the save file.
|
|
|
|
|
if (slot.Species is not (int)Species.Munchlax)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// We didn't encode the honey tree index to the encounter slot resource.
|
|
|
|
|
// Check if any of the slot's location doesn't match any of the groupC trees' area location ID.
|
|
|
|
|
var location = pk.Met_Location;
|
|
|
|
|
var trees = SAV4Sinnoh.CalculateMunchlaxTrees(pk.TID, pk.SID);
|
|
|
|
|
return LocationID_HoneyTree[trees.Tree1] != location
|
|
|
|
|
&& LocationID_HoneyTree[trees.Tree2] != location
|
|
|
|
|
&& LocationID_HoneyTree[trees.Tree3] != location
|
|
|
|
|
&& LocationID_HoneyTree[trees.Tree4] != location;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static readonly byte[] LocationID_HoneyTree =
|
|
|
|
|
{
|
|
|
|
|
20, // 00 Route 205 Floaroma
|
|
|
|
|
20, // 01 Route 205 Eterna
|
|
|
|
|
21, // 02 Route 206
|
|
|
|
|
22, // 03 Route 207
|
|
|
|
|
23, // 04 Route 208
|
|
|
|
|
24, // 05 Route 209
|
|
|
|
|
25, // 06 Route 210 Solaceon
|
|
|
|
|
25, // 07 Route 210 Celestic
|
|
|
|
|
26, // 08 Route 211
|
|
|
|
|
27, // 09 Route 212 Hearthome
|
|
|
|
|
27, // 10 Route 212 Pastoria
|
|
|
|
|
28, // 11 Route 213
|
|
|
|
|
29, // 12 Route 214
|
|
|
|
|
30, // 13 Route 215
|
|
|
|
|
33, // 14 Route 218
|
|
|
|
|
36, // 15 Route 221
|
|
|
|
|
37, // 16 Route 222
|
|
|
|
|
47, // 17 Valley Windworks
|
|
|
|
|
48, // 18 Eterna Forest
|
|
|
|
|
49, // 19 Fuego Ironworks
|
|
|
|
|
58, // 20 Floaroma Meadow
|
|
|
|
|
};
|
|
|
|
|
|
2021-09-07 22:35:36 +00:00
|
|
|
|
// original met level cannot be inferred
|
2021-06-30 03:58:06 +00:00
|
|
|
|
private IEnumerable<EncounterSlot4> GetSlotsFuzzy(IReadOnlyList<EvoCriteria> chain)
|
2020-08-21 23:35:49 +00:00
|
|
|
|
{
|
|
|
|
|
foreach (var slot in Slots)
|
|
|
|
|
{
|
|
|
|
|
foreach (var evo in chain)
|
|
|
|
|
{
|
|
|
|
|
if (slot.Species != evo.Species)
|
|
|
|
|
continue;
|
|
|
|
|
|
2021-09-07 22:35:36 +00:00
|
|
|
|
if (slot.Form != evo.Form && slot.Species is not (int)Species.Burmy)
|
|
|
|
|
{
|
|
|
|
|
// Unown forms are random, not specific form IDs
|
|
|
|
|
if (!slot.IsRandomUnspecificForm)
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-08-30 17:23:22 +00:00
|
|
|
|
if (slot.LevelMin > evo.Level)
|
2020-08-21 23:35:49 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
yield return slot;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-19 23:19:07 +00:00
|
|
|
|
}
|
2019-09-13 06:20:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|