PKHeX/PKHeX.Core/Editing/PKM/QR/QRPKM.cs
Kurt 9166d0eb64
Refactoring: Move Source (Legality) (#3560)
Rewrites a good amount of legality APIs pertaining to:
* Legal moves that can be learned
* Evolution chains & cross-generation paths
* Memory validation with forgotten moves

In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data.

The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space.

The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation.

* `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game.
* `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`).
* Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
2022-08-03 16:15:27 -07:00

70 lines
2.1 KiB
C#

using System.Collections.Generic;
using System.Text;
namespace PKHeX.Core;
public static class QRPKM
{
/// <summary>
/// Summarizes the details of a <see cref="PKM"/> into multiple lines for display in an image.
/// </summary>
/// <param name="pk">Pokémon to generate details for.</param>
/// <returns>Lines representing a Header, Moves, and IVs/EVs</returns>
public static string[] GetQRLines(this PKM pk)
{
var s = GameInfo.Strings;
var header = GetHeader(pk, s);
var sb = new StringBuilder(48);
for (int i = 0; i < 4; i++)
{
var move = pk.GetMove(i);
if (move == 0)
continue;
if (sb.Length != 0)
sb.Append(" / ");
var moveName = move < s.movelist.Length ? s.movelist[move] : "ERROR";
sb.Append(moveName);
}
string IVs = $"IVs: {pk.IV_HP:00}/{pk.IV_ATK:00}/{pk.IV_DEF:00}/{pk.IV_SPA:00}/{pk.IV_SPD:00}/{pk.IV_SPE:00}";
string EVs = $"EVs: {pk.EV_HP:00}/{pk.EV_ATK:00}/{pk.EV_DEF:00}/{pk.EV_SPA:00}/{pk.EV_SPD:00}/{pk.EV_SPE:00}";
return new[]
{
string.Join(" ", header),
sb.ToString(),
IVs + " " + EVs,
};
}
private static IEnumerable<string> GetHeader(PKM pk, GameStrings s)
{
string filename = pk.Nickname;
if ((uint) pk.Species < s.Species.Count)
{
var name = s.Species[pk.Species];
if (pk.Nickname != name)
filename += $" ({name})";
}
yield return filename;
if (pk.Format >= 3 && (uint)pk.Ability < s.Ability.Count)
yield return $"[{s.Ability[pk.Ability]}]";
var level = pk.Stat_Level;
if (level == 0)
level = pk.CurrentLevel;
yield return $"lv{level}";
if (pk.HeldItem > 0)
{
var items = s.GetItemStrings(pk.Context);
if ((uint)pk.HeldItem < items.Length)
yield return $" @ {items[pk.HeldItem]}";
}
if (pk.Format >= 3 && (uint)pk.Nature < s.Natures.Count)
yield return s.natures[pk.Nature];
}
}