PKHeX/PKHeX.Core/Legality/BulkGenerator.cs

78 lines
2.3 KiB
C#
Raw Normal View History

using System;
2020-03-20 20:33:15 +00:00
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
{
2023-03-31 20:00:34 +00:00
/// <summary>
/// Gets a list of <see cref="PKM"/> data that are valid for the provided <see cref="sav"/>.
/// </summary>
public static List<PKM> GetLivingDex(this SaveFile sav)
{
var speciesToGenerate = GetAll(1, sav.MaxSpeciesID);
2023-03-31 20:00:34 +00:00
speciesToGenerate = speciesToGenerate.Where(sav.Personal.IsSpeciesInGame);
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);
}
2020-03-20 20:33:15 +00:00
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);
}
}
2020-03-20 20:33:15 +00:00
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();
2020-03-20 20:33:15 +00:00
var f = EncounterMovesetGenerator.GeneratePKMs(template, tr, template.Moves).FirstOrDefault();
if (f == null)
return null;
2020-03-20 20:33:15 +00:00
var result = EntityConverter.ConvertToType(f, destType, out _);
if (result == null)
return null;
2020-03-20 20:33:15 +00:00
result.Species = species;
result.Form = form;
result.CurrentLevel = 100;
2020-03-20 20:33:15 +00:00
result.Heal();
return result;
}
}