PKHeX/PKHeX.Core/Legality/BulkGenerator.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

73 lines
2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace PKHeX.Core;
/// <summary>
/// Logic for generating a large amount of <see cref="PKM"/> data.
/// </summary>
public static class BulkGenerator
{
public static List<PKM> GetLivingDex(this SaveFile sav)
{
var speciesToGenerate = GetAll(1, sav.MaxSpeciesID);
return GetLivingDex(sav, speciesToGenerate);
}
private static IEnumerable<ushort> GetAll(ushort min, ushort max)
{
for (ushort i = min; i <= max; i++)
yield return i;
}
private static List<PKM> GetLivingDex(SaveFile sav, IEnumerable<ushort> speciesToGenerate)
{
return sav.GetLivingDex(speciesToGenerate, sav.BlankPKM);
}
public static List<PKM> GetLivingDex(this ITrainerInfo tr, IEnumerable<ushort> speciesToGenerate, PKM blank)
{
var result = new List<PKM>();
var destType = blank.GetType();
foreach (var s in speciesToGenerate)
{
var pk = blank.Clone();
pk.Species = s;
pk.Gender = pk.GetSaneGender();
var pi = pk.PersonalInfo;
for (byte f = 0; f < pi.FormCount; f++)
{
var entry = tr.GetLivingEntry(pk, s, f, destType);
if (entry == null)
continue;
result.Add(entry);
}
}
return result;
}
public static PKM? GetLivingEntry(this ITrainerInfo tr, PKM template, ushort species, byte form, Type destType)
{
template.Species = species;
template.Form = form;
template.Gender = template.GetSaneGender();
var f = EncounterMovesetGenerator.GeneratePKMs(template, tr, template.Moves).FirstOrDefault();
if (f == null)
return null;
var result = EntityConverter.ConvertToType(f, destType, out _);
if (result == null)
return null;
result.Species = species;
result.Form = form;
result.CurrentLevel = 100;
result.Heal();
return result;
}
}