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;
|
|
|
|
|
public readonly int Rate;
|
|
|
|
|
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;
|
2020-08-30 17:23:22 +00:00
|
|
|
|
var rate = data[3];
|
2019-09-13 06:20:52 +00:00
|
|
|
|
|
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;
|
|
|
|
|
int count = (data.Length - 4) / size;
|
2019-09-13 06:20:52 +00:00
|
|
|
|
|
2020-08-30 17:23:22 +00:00
|
|
|
|
var rates = new byte[count];
|
|
|
|
|
for (int i = 0; i < rates.Length; i++)
|
|
|
|
|
rates[i] = data[4 + i];
|
2019-09-13 06:20:52 +00:00
|
|
|
|
|
2020-08-30 17:23:22 +00:00
|
|
|
|
Rates = rates;
|
2020-08-30 18:08:21 +00:00
|
|
|
|
Slots = ReadSlots(data, count, 4 + 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
|
|
|
|
Rate = rate;
|
2019-09-13 06:20:52 +00:00
|
|
|
|
|
2020-08-30 17:23:22 +00:00
|
|
|
|
const int size = 4;
|
|
|
|
|
int count = (data.Length - 4) / 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
|
|
|
|
};
|
2020-08-30 18:08:21 +00:00
|
|
|
|
Slots = ReadSlots(data, count, 4);
|
2019-12-06 07:04:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
|
private EncounterSlot2[] ReadSlots(ReadOnlySpan<byte> data, int count, int start)
|
2019-09-13 06:20:52 +00:00
|
|
|
|
{
|
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
|
|
|
|
{
|
2020-08-30 17:23:22 +00:00
|
|
|
|
int offset = start + (4 * i);
|
2022-01-03 05:35:59 +00:00
|
|
|
|
var entry = data.Slice(offset, 4);
|
|
|
|
|
slots[i] = ReadSlot(entry);
|
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
|
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
|
private EncounterSlot2 ReadSlot(ReadOnlySpan<byte> entry)
|
|
|
|
|
{
|
|
|
|
|
int species = entry[0];
|
|
|
|
|
int slotNum = entry[1];
|
|
|
|
|
int min = entry[2];
|
|
|
|
|
int max = entry[3];
|
|
|
|
|
return new EncounterSlot2(this, species, min, max, slotNum);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
{
|
2020-12-22 01:12:39 +00:00
|
|
|
|
if (pkm is not ICaughtData2 pk2 || pk2.CaughtData == 0)
|
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
|
|
|
|
}
|