2019-09-13 06:20:52 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
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.GSC"/> encounter area
|
|
|
|
|
/// </summary>
|
2021-01-01 18:55:33 +00:00
|
|
|
|
public sealed record EncounterArea2 : EncounterArea
|
2019-09-13 06:20:52 +00:00
|
|
|
|
{
|
2020-08-30 17:23:22 +00:00
|
|
|
|
private static readonly byte[] BCC_SlotRates = { 20, 20, 10, 10, 05, 05, 10, 10, 05, 05 };
|
|
|
|
|
private static readonly byte[] RatesGrass = { 30, 30, 20, 10, 5, 4, 1 };
|
|
|
|
|
private static readonly byte[] RatesSurf = { 60, 30, 10 };
|
2019-09-13 06:20:52 +00:00
|
|
|
|
|
2020-08-30 17:23:22 +00:00
|
|
|
|
internal readonly EncounterTime Time;
|
2022-03-07 06:47:03 +00:00
|
|
|
|
public readonly byte Rate;
|
2020-08-30 17:23:22 +00:00
|
|
|
|
public readonly IReadOnlyList<byte> Rates;
|
2021-06-30 03:58:06 +00:00
|
|
|
|
public readonly EncounterSlot2[] 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 EncounterArea2[] GetAreas(BinLinkerAccessor input, GameVersion game)
|
2019-09-13 06:20:52 +00:00
|
|
|
|
{
|
2020-08-30 17:23:22 +00:00
|
|
|
|
var result = new EncounterArea2[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 EncounterArea2(input[i], game);
|
|
|
|
|
return result;
|
2019-09-13 06:20:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
|
private EncounterArea2(ReadOnlySpan<byte> data, GameVersion game) : base(game)
|
2019-09-13 06:20:52 +00:00
|
|
|
|
{
|
2020-08-30 17:23:22 +00:00
|
|
|
|
Location = data[0];
|
|
|
|
|
Time = (EncounterTime)data[1];
|
2021-02-08 05:40:31 +00:00
|
|
|
|
var type = (Type = (SlotType)data[2]) & (SlotType)0xF;
|
2022-03-07 06:47:03 +00:00
|
|
|
|
Rate = data[3];
|
2019-09-13 06:20:52 +00:00
|
|
|
|
|
2022-03-07 06:47:03 +00:00
|
|
|
|
var next = data[4..];
|
2021-07-03 18:35:28 +00:00
|
|
|
|
if (type is > SlotType.Surf and not SlotType.BugContest) // Not Grass/Surf
|
2019-09-13 06:20:52 +00:00
|
|
|
|
{
|
2020-08-30 17:23:22 +00:00
|
|
|
|
const int size = 5;
|
2022-03-07 06:47:03 +00:00
|
|
|
|
int count = next.Length / size;
|
|
|
|
|
Rates = next[..count].ToArray();
|
|
|
|
|
Slots = ReadSlots(next[count..], count);
|
2019-09-13 06:20:52 +00:00
|
|
|
|
}
|
2020-08-30 17:23:22 +00:00
|
|
|
|
else
|
2019-09-13 06:20:52 +00:00
|
|
|
|
{
|
2020-08-30 17:23:22 +00:00
|
|
|
|
const int size = 4;
|
2022-03-07 06:47:03 +00:00
|
|
|
|
int count = next.Length / size;
|
2021-03-14 23:16:55 +00:00
|
|
|
|
Rates = type switch
|
|
|
|
|
{
|
|
|
|
|
SlotType.BugContest => BCC_SlotRates,
|
|
|
|
|
SlotType.Grass => RatesGrass,
|
2021-08-20 20:49:20 +00:00
|
|
|
|
_ => RatesSurf,
|
2021-03-14 23:16:55 +00:00
|
|
|
|
};
|
2022-03-07 06:47:03 +00:00
|
|
|
|
Slots = ReadSlots(next, count);
|
2019-12-06 07:04:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 06:47:03 +00:00
|
|
|
|
private EncounterSlot2[] ReadSlots(ReadOnlySpan<byte> data, int count)
|
2019-09-13 06:20:52 +00:00
|
|
|
|
{
|
2022-03-07 06:47:03 +00:00
|
|
|
|
const int size = 4;
|
2020-08-30 17:23:22 +00:00
|
|
|
|
var slots = new EncounterSlot2[count];
|
|
|
|
|
for (int i = 0; i < slots.Length; i++)
|
2019-09-13 06:20:52 +00:00
|
|
|
|
{
|
2022-03-07 06:47:03 +00:00
|
|
|
|
var entry = data.Slice(i * size, size);
|
2022-03-07 07:25:47 +00:00
|
|
|
|
byte max = entry[3];
|
|
|
|
|
byte min = entry[2];
|
2022-03-07 06:47:03 +00:00
|
|
|
|
byte slotNum = entry[1];
|
2022-03-07 07:25:47 +00:00
|
|
|
|
byte species = entry[0];
|
2022-03-07 06:47:03 +00:00
|
|
|
|
slots[i] = new EncounterSlot2(this, species, min, max, slotNum);
|
2019-09-13 06:20:52 +00:00
|
|
|
|
}
|
2020-08-30 17:23:22 +00:00
|
|
|
|
return slots;
|
2019-09-13 06:20:52 +00:00
|
|
|
|
}
|
2020-07-19 18:32:40 +00:00
|
|
|
|
|
2020-08-21 23:35:49 +00:00
|
|
|
|
public override IEnumerable<EncounterSlot> GetMatchingSlots(PKM pkm, IReadOnlyList<EvoCriteria> chain)
|
2020-07-19 18:32:40 +00:00
|
|
|
|
{
|
2022-03-07 06:47:03 +00:00
|
|
|
|
if (pkm is not ICaughtData2 {CaughtData: not 0} pk2)
|
2020-08-21 23:35:49 +00:00
|
|
|
|
return GetSlotsFuzzy(chain);
|
2020-07-19 18:32:40 +00:00
|
|
|
|
|
2020-08-21 23:35:49 +00:00
|
|
|
|
if (pk2.Met_Location != Location)
|
|
|
|
|
return Array.Empty<EncounterSlot>();
|
|
|
|
|
return GetSlotsSpecificLevelTime(chain, pk2.Met_TimeOfDay, pk2.Met_Level);
|
2020-07-19 18:32:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-30 03:58:06 +00:00
|
|
|
|
private IEnumerable<EncounterSlot2> GetSlotsSpecificLevelTime(IReadOnlyList<EvoCriteria> chain, int time, int lvl)
|
2020-07-19 18:32:40 +00:00
|
|
|
|
{
|
2020-08-21 23:35:49 +00:00
|
|
|
|
foreach (var slot in Slots)
|
|
|
|
|
{
|
|
|
|
|
foreach (var evo in chain)
|
|
|
|
|
{
|
|
|
|
|
if (slot.Species != evo.Species)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (slot.Form != evo.Form)
|
|
|
|
|
{
|
|
|
|
|
if (slot.Species != (int)Species.Unown || evo.Form >= 26) // Don't yield !? forms
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!slot.IsLevelWithinRange(lvl))
|
|
|
|
|
break;
|
|
|
|
|
|
2020-08-30 17:23:22 +00:00
|
|
|
|
if (!Time.Contains(time))
|
2020-08-21 23:35:49 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
yield return slot;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-19 18:32:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-30 03:58:06 +00:00
|
|
|
|
private IEnumerable<EncounterSlot2> GetSlotsFuzzy(IReadOnlyList<EvoCriteria> chain)
|
2020-07-19 18:32:40 +00:00
|
|
|
|
{
|
2020-08-21 23:35:49 +00:00
|
|
|
|
foreach (var slot in Slots)
|
|
|
|
|
{
|
|
|
|
|
foreach (var evo in chain)
|
|
|
|
|
{
|
|
|
|
|
if (slot.Species != evo.Species)
|
|
|
|
|
continue;
|
2020-07-19 18:32:40 +00:00
|
|
|
|
|
2020-08-21 23:35:49 +00:00
|
|
|
|
if (slot.Form != evo.Form)
|
|
|
|
|
{
|
|
|
|
|
if (slot.Species != (int) Species.Unown || evo.Form >= 26) // Don't yield !? forms
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-19 18:32:40 +00:00
|
|
|
|
}
|
2019-09-13 06:20:52 +00:00
|
|
|
|
}
|
2020-07-19 18:32:40 +00:00
|
|
|
|
}
|