PKHeX/PKHeX.Core/Legality/Evolutions/EvolutionSets/EvolutionSet3.cs
Kurt 3c232505e5
Refactoring: Narrow some value types (Species, Move, Form) (#3575)
In this pull request I've changed a ton of method signatures to reflect the more-narrow types of Species, Move# and Form; additionally, I've narrowed other large collections that stored lists of species / permitted values, and reworked them to be more performant with the latest API spaghetti that PKHeX provides. Roamer met locations, usually in a range of [max-min]<64, can be quickly checked using a bitflag operation on a UInt64. Other collections (like "Is this from Colosseum or XD") were eliminated -- shadow state is not transferred COLO<->XD, so having a Shadow ID or matching the met location from a gift/wild encounter is a sufficient check for "originated in XD".
2022-08-26 23:43:36 -07:00

81 lines
3 KiB
C#

using System;
using System.Collections.Generic;
using static System.Buffers.Binary.BinaryPrimitives;
namespace PKHeX.Core;
/// <summary>
/// Generation 3 Evolution Branch Entries
/// </summary>
public static class EvolutionSet3
{
private static EvolutionMethod GetMethod(ReadOnlySpan<byte> data)
{
var method = data[0];
var arg = ReadUInt16LittleEndian(data[2..]);
var species = SpeciesConverter.GetG4Species(ReadUInt16LittleEndian(data[4..]));
//2 bytes padding
switch (method)
{
case 1: /* Friendship*/
case 2: /* Friendship day*/
case 3: /* Friendship night*/
return new EvolutionMethod((EvolutionType)method, species, Argument: arg, LevelUp: 1);
case 5: /* Trade */
case 6: /* Trade while holding */
return new EvolutionMethod((EvolutionType)method, species, Argument: arg);
case 4: /* Level Up */
return new EvolutionMethod(EvolutionType.LevelUp, species, Argument: arg, Level: (byte)arg, LevelUp: 1);
case 7: /* Use item */
return new EvolutionMethod((EvolutionType)(method + 1), species, Argument: arg);
case 15: /* Beauty evolution*/
return new EvolutionMethod((EvolutionType)(method + 1), species, Argument: arg, LevelUp: 1);
case 8: /* Tyrogue -> Hitmonchan */
case 9: /* Tyrogue -> Hitmonlee */
case 10: /* Tyrogue -> Hitmontop*/
case 11: /* Wurmple -> Silcoon evolution */
case 12: /* Wurmple -> Cascoon evolution */
case 13: /* Nincada -> Ninjask evolution */
case 14: /* Shedinja spawn in Nincada -> Ninjask evolution */
return new EvolutionMethod((EvolutionType)(method + 1), species, Argument: arg, Level: (byte)arg, LevelUp: 1);
default:
throw new ArgumentOutOfRangeException(nameof(method));
}
}
public static IReadOnlyList<EvolutionMethod[]> GetArray(ReadOnlySpan<byte> data)
{
var evos = new EvolutionMethod[Legal.MaxSpeciesID_3 + 1][];
evos[0] = Array.Empty<EvolutionMethod>();
for (ushort i = 1; i <= Legal.MaxSpeciesIndex_3; i++)
{
int g4species = SpeciesConverter.GetG4Species(i);
if (g4species == 0)
continue;
const int maxCount = 5;
const int size = 8;
int offset = i * (maxCount * size);
int count = 0;
for (; count < maxCount; count++)
{
if (data[offset + (count * size)] == 0)
break;
}
if (count == 0)
{
evos[g4species] = Array.Empty<EvolutionMethod>();
continue;
}
var set = new EvolutionMethod[count];
for (int j = 0; j < set.Length; j++)
set[j] = GetMethod(data.Slice(offset + (j * size), size));
evos[g4species] = set;
}
return evos;
}
}