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;
|
2019-09-13 06:20:52 +00:00
|
|
|
|
|
2020-08-30 17:23:22 +00:00
|
|
|
|
public static EncounterArea2[] GetAreas(byte[][] 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];
|
|
|
|
|
for (int i = 0; i < input.Length; i++)
|
|
|
|
|
result[i] = new EncounterArea2(input[i], game);
|
|
|
|
|
return result;
|
2019-09-13 06:20:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 18:08:21 +00:00
|
|
|
|
private EncounterArea2(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];
|
|
|
|
|
var type = Type = (SlotType)data[2];
|
|
|
|
|
var rate = data[3];
|
2019-09-13 06:20:52 +00:00
|
|
|
|
|
2020-08-30 17:23:22 +00:00
|
|
|
|
if (type > SlotType.Surf) // 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;
|
|
|
|
|
Rates = type == SlotType.BugContest ? BCC_SlotRates : (type == SlotType.Grass) ? RatesGrass : RatesSurf;
|
2020-08-30 18:08:21 +00:00
|
|
|
|
Slots = ReadSlots(data, count, 4);
|
2019-12-06 07:04:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 18:08:21 +00:00
|
|
|
|
private EncounterSlot2[] ReadSlots(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);
|
|
|
|
|
int species = data[offset + 0];
|
|
|
|
|
int slotNum = data[offset + 1];
|
|
|
|
|
int min = data[offset + 2];
|
|
|
|
|
int max = data[offset + 3];
|
2020-08-30 18:08:21 +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
|
|
|
|
{
|
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
|
|
|
|
}
|
|
|
|
|
|
2020-08-21 23:35:49 +00:00
|
|
|
|
private IEnumerable<EncounterSlot> 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
|
|
|
|
}
|
|
|
|
|
|
2020-08-21 23:35:49 +00:00
|
|
|
|
private IEnumerable<EncounterSlot> 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
|
|
|
|
}
|