2022-08-27 06:43:36 +00:00
|
|
|
using System;
|
2023-09-11 04:17:47 +00:00
|
|
|
using System.Buffers;
|
2020-03-20 20:33:15 +00:00
|
|
|
using System.Collections.Generic;
|
2019-10-26 19:58:55 +00:00
|
|
|
using System.Linq;
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logic for generating a large amount of <see cref="PKM"/> data.
|
|
|
|
/// </summary>
|
|
|
|
public static class BulkGenerator
|
2019-10-26 19:58:55 +00:00
|
|
|
{
|
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>
|
2022-06-18 18:04:24 +00:00
|
|
|
public static List<PKM> GetLivingDex(this SaveFile sav)
|
2019-10-26 19:58:55 +00:00
|
|
|
{
|
2022-08-27 06:43:36 +00:00
|
|
|
var speciesToGenerate = GetAll(1, sav.MaxSpeciesID);
|
2023-03-31 20:00:34 +00:00
|
|
|
speciesToGenerate = speciesToGenerate.Where(sav.Personal.IsSpeciesInGame);
|
2022-06-18 18:04:24 +00:00
|
|
|
return GetLivingDex(sav, speciesToGenerate);
|
|
|
|
}
|
2020-01-26 06:54:25 +00:00
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
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)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
return sav.GetLivingDex(speciesToGenerate, sav.BlankPKM);
|
|
|
|
}
|
2020-03-20 20:33:15 +00:00
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
public static List<PKM> GetLivingDex(this ITrainerInfo tr, IEnumerable<ushort> speciesToGenerate, PKM blank)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
var result = new List<PKM>();
|
|
|
|
var destType = blank.GetType();
|
|
|
|
foreach (var s in speciesToGenerate)
|
2020-01-26 06:54:25 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var pk = blank.Clone();
|
|
|
|
pk.Species = s;
|
|
|
|
pk.Gender = pk.GetSaneGender();
|
2019-10-26 19:58:55 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var pi = pk.PersonalInfo;
|
2022-08-27 06:43:36 +00:00
|
|
|
for (byte f = 0; f < pi.FormCount; f++)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
var entry = tr.GetLivingEntry(pk, s, f, destType);
|
|
|
|
if (entry == null)
|
|
|
|
continue;
|
|
|
|
result.Add(entry);
|
2019-10-26 19:58:55 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-20 20:33:15 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
public static PKM? GetLivingEntry(this ITrainerInfo tr, PKM template, ushort species, byte form, Type destType)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
template.Species = species;
|
|
|
|
template.Form = form;
|
|
|
|
template.Gender = template.GetSaneGender();
|
2020-03-20 20:33:15 +00:00
|
|
|
|
2023-09-11 04:17:47 +00:00
|
|
|
var moves = ArrayPool<ushort>.Shared.Rent(4);
|
|
|
|
var memory = moves.AsMemory(0, 4);
|
|
|
|
var span = memory.Span;
|
|
|
|
template.GetMoves(span);
|
2023-09-12 01:20:22 +00:00
|
|
|
var first = EncounterMovesetGenerator.GenerateEncounters(template, tr, memory).FirstOrDefault();
|
2023-09-11 04:17:47 +00:00
|
|
|
span.Clear();
|
|
|
|
ArrayPool<ushort>.Shared.Return(moves);
|
2023-09-12 01:20:22 +00:00
|
|
|
if (first == null)
|
2022-06-18 18:04:24 +00:00
|
|
|
return null;
|
2020-03-20 20:33:15 +00:00
|
|
|
|
2023-09-12 01:20:22 +00:00
|
|
|
var pk = first.ConvertToPKM(tr);
|
|
|
|
var result = EntityConverter.ConvertToType(pk, destType, out _);
|
2022-06-18 18:04:24 +00:00
|
|
|
if (result == null)
|
|
|
|
return null;
|
2020-03-20 20:33:15 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
result.Species = species;
|
|
|
|
result.Form = form;
|
|
|
|
result.CurrentLevel = 100;
|
2020-03-20 20:33:15 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
result.Heal();
|
|
|
|
return result;
|
2019-10-26 19:58:55 +00:00
|
|
|
}
|
|
|
|
}
|