2020-08-30 17:23:22 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2022-01-03 05:35:59 +00:00
|
|
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
2020-08-30 17:23:22 +00:00
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
2019-09-13 06:20:52 +00:00
|
|
|
|
{
|
2021-01-01 18:55:33 +00:00
|
|
|
|
/// <inheritdoc cref="EncounterArea" />
|
2019-09-13 06:20:52 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <see cref="GameVersion.Gen5"/> encounter area
|
|
|
|
|
/// </summary>
|
2021-01-01 18:55:33 +00:00
|
|
|
|
public sealed record EncounterArea5 : EncounterArea
|
2019-09-13 06:20:52 +00:00
|
|
|
|
{
|
2021-06-30 03:58:06 +00:00
|
|
|
|
public readonly EncounterSlot5[] Slots;
|
|
|
|
|
|
|
|
|
|
protected override IReadOnlyList<EncounterSlot> Raw => Slots;
|
|
|
|
|
|
2020-08-30 17:23:22 +00:00
|
|
|
|
public static EncounterArea5[] GetAreas(byte[][] input, GameVersion game)
|
|
|
|
|
{
|
|
|
|
|
var result = new EncounterArea5[input.Length];
|
|
|
|
|
for (int i = 0; i < input.Length; i++)
|
|
|
|
|
result[i] = new EncounterArea5(input[i], game);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
|
private EncounterArea5(ReadOnlySpan<byte> data, GameVersion game) : base(game)
|
2020-08-30 17:23:22 +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];
|
|
|
|
|
|
2020-08-30 18:08:21 +00:00
|
|
|
|
Slots = ReadSlots(data);
|
2020-08-30 17:23:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
|
private EncounterSlot5[] ReadSlots(ReadOnlySpan<byte> data)
|
2020-08-30 17:23:22 +00:00
|
|
|
|
{
|
|
|
|
|
const int size = 4;
|
|
|
|
|
int count = (data.Length - 4) / size;
|
|
|
|
|
var slots = new EncounterSlot5[count];
|
|
|
|
|
for (int i = 0; i < slots.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
int offset = 4 + (size * i);
|
2022-01-03 05:35:59 +00:00
|
|
|
|
var entry = data.Slice(offset, size);
|
|
|
|
|
slots[i] = ReadSlot(entry);
|
2020-08-30 17:23:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return slots;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
|
private EncounterSlot5 ReadSlot(ReadOnlySpan<byte> entry)
|
|
|
|
|
{
|
|
|
|
|
ushort SpecForm = ReadUInt16LittleEndian(entry);
|
|
|
|
|
int species = SpecForm & 0x3FF;
|
|
|
|
|
int form = SpecForm >> 11;
|
|
|
|
|
int min = entry[2];
|
|
|
|
|
int max = entry[3];
|
|
|
|
|
return new EncounterSlot5(this, species, form, min, max);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 17:23:22 +00:00
|
|
|
|
public override IEnumerable<EncounterSlot> GetMatchingSlots(PKM pkm, IReadOnlyList<EvoCriteria> chain)
|
|
|
|
|
{
|
|
|
|
|
foreach (var slot in Slots)
|
|
|
|
|
{
|
|
|
|
|
foreach (var evo in chain)
|
|
|
|
|
{
|
|
|
|
|
if (slot.Species != evo.Species)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (!slot.IsLevelWithinRange(pkm.Met_Level))
|
|
|
|
|
break;
|
|
|
|
|
|
2021-12-26 08:00:38 +00:00
|
|
|
|
// Deerling and Sawsbuck can change forms when seasons change, thus can be any of the [0,3] form values.
|
|
|
|
|
// no other wild forms can change
|
|
|
|
|
if (slot.Form != evo.Form && slot.Species is not ((int)Species.Deerling or (int)Species.Sawsbuck))
|
2020-08-30 17:23:22 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
yield return slot;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-13 06:20:52 +00:00
|
|
|
|
}
|
2022-01-03 05:35:59 +00:00
|
|
|
|
}
|