2021-11-20 02:23:49 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2022-01-03 05:35:59 +00:00
|
|
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
2021-11-20 02:23:49 +00:00
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
|
{
|
|
|
|
|
/// <inheritdoc cref="EncounterArea" />
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <see cref="GameVersion.BDSP"/> encounter area
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed record EncounterArea8b : EncounterArea
|
|
|
|
|
{
|
|
|
|
|
public readonly EncounterSlot8b[] Slots;
|
|
|
|
|
|
|
|
|
|
protected override IReadOnlyList<EncounterSlot> Raw => Slots;
|
|
|
|
|
|
|
|
|
|
public static EncounterArea8b[] GetAreas(byte[][] input, GameVersion game)
|
|
|
|
|
{
|
|
|
|
|
var result = new EncounterArea8b[input.Length];
|
|
|
|
|
for (int i = 0; i < input.Length; i++)
|
|
|
|
|
result[i] = new EncounterArea8b(input[i], game);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
|
private EncounterArea8b(ReadOnlySpan<byte> data, GameVersion game) : base(game)
|
2021-11-20 02:23:49 +00:00
|
|
|
|
{
|
|
|
|
|
Location = data[0] | (data[1] << 8);
|
|
|
|
|
Type = (SlotType)data[2];
|
|
|
|
|
|
|
|
|
|
Slots = ReadSlots(data);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
|
private EncounterSlot8b[] ReadSlots(ReadOnlySpan<byte> data)
|
2021-11-20 02:23:49 +00:00
|
|
|
|
{
|
|
|
|
|
const int size = 4;
|
|
|
|
|
int count = (data.Length - 4) / size;
|
|
|
|
|
var slots = new EncounterSlot8b[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);
|
2021-11-20 02:23:49 +00:00
|
|
|
|
}
|
|
|
|
|
return slots;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
|
private EncounterSlot8b ReadSlot(ReadOnlySpan<byte> data)
|
|
|
|
|
{
|
|
|
|
|
ushort SpecForm = ReadUInt16LittleEndian(data);
|
|
|
|
|
int species = SpecForm & 0x3FF;
|
|
|
|
|
int form = SpecForm >> 11;
|
|
|
|
|
int min = data[2];
|
|
|
|
|
int max = data[3];
|
|
|
|
|
return new EncounterSlot8b(this, species, form, min, max);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-12 05:31:32 +00:00
|
|
|
|
public override bool IsMatchLocation(int location)
|
|
|
|
|
{
|
|
|
|
|
if (base.IsMatchLocation(location))
|
|
|
|
|
return true;
|
|
|
|
|
return CanCrossoverTo(location);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool CanCrossoverTo(int location)
|
|
|
|
|
{
|
|
|
|
|
if (Type is SlotType.Surf)
|
|
|
|
|
{
|
|
|
|
|
return Location switch
|
|
|
|
|
{
|
|
|
|
|
486 => location is 167, // Route 223 -> Pokémon League
|
|
|
|
|
167 => location is 486, // Pokémon League -> Route 223
|
2021-12-12 14:18:57 +00:00
|
|
|
|
420 => location is 489, // Route 229 -> Route 230
|
|
|
|
|
489 => location is 420, // Route 230 -> Route 229
|
2021-12-12 05:31:32 +00:00
|
|
|
|
|
|
|
|
|
// All other crossover surf locations are identical slot lists.
|
|
|
|
|
_ => false,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// No crossover
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-20 02:23:49 +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-11-20 17:59:14 +00:00
|
|
|
|
if (slot.Form != evo.Form && slot.Species is not (int)Species.Burmy)
|
|
|
|
|
break;
|
2021-11-20 02:23:49 +00:00
|
|
|
|
|
2021-12-25 21:48:28 +00:00
|
|
|
|
if (Type is SlotType.HoneyTree && IsInaccessibleHoneySlotLocation(slot, pkm))
|
|
|
|
|
break;
|
|
|
|
|
|
2021-11-20 02:23:49 +00:00
|
|
|
|
yield return slot;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-25 21:48:28 +00:00
|
|
|
|
private static bool IsInaccessibleHoneySlotLocation(EncounterSlot8b 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 ushort[] LocationID_HoneyTree =
|
|
|
|
|
{
|
|
|
|
|
359, // 00 Route 205 Floaroma
|
|
|
|
|
361, // 01 Route 205 Eterna
|
|
|
|
|
362, // 02 Route 206
|
|
|
|
|
364, // 03 Route 207
|
|
|
|
|
365, // 04 Route 208
|
|
|
|
|
367, // 05 Route 209
|
|
|
|
|
373, // 06 Route 210 Solaceon
|
|
|
|
|
375, // 07 Route 210 Celestic
|
|
|
|
|
378, // 08 Route 211
|
|
|
|
|
379, // 09 Route 212 Hearthome
|
|
|
|
|
383, // 10 Route 212 Pastoria
|
|
|
|
|
385, // 11 Route 213
|
|
|
|
|
392, // 12 Route 214
|
|
|
|
|
394, // 13 Route 215
|
|
|
|
|
400, // 14 Route 218
|
|
|
|
|
404, // 15 Route 221
|
|
|
|
|
407, // 16 Route 222
|
|
|
|
|
197, // 17 Valley Windworks
|
|
|
|
|
199, // 18 Eterna Forest
|
|
|
|
|
201, // 19 Fuego Ironworks
|
|
|
|
|
253, // 20 Floaroma Meadow
|
|
|
|
|
};
|
2021-11-20 02:23:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|