mirror of
https://github.com/kwsch/PKHeX.git
synced 2026-05-21 05:09:35 -05:00
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
124 lines
4.4 KiB
C#
124 lines
4.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using static PKHeX.Core.LegalityCheckStrings;
|
|
|
|
namespace PKHeX.Core;
|
|
|
|
/// <summary>
|
|
/// Formatting logic for <see cref="LegalityAnalysis"/> to create a human readable <see cref="T:System.String"/>.
|
|
/// </summary>
|
|
public static class LegalityFormatting
|
|
{
|
|
/// <summary>
|
|
/// Creates a report message with optional verbosity for in-depth analysis.
|
|
/// </summary>
|
|
/// <param name="la">Legality result to format</param>
|
|
/// <param name="verbose">Include all details in the parse, including valid check messages.</param>
|
|
/// <returns>Single line string</returns>
|
|
public static string Report(this LegalityAnalysis la, bool verbose = false) => verbose ? GetVerboseLegalityReport(la) : GetLegalityReport(la);
|
|
|
|
public static ILegalityFormatter Formatter { private get; set; } = new BaseLegalityFormatter();
|
|
|
|
public static string GetLegalityReport(LegalityAnalysis la) => Formatter.GetReport(la);
|
|
public static string GetVerboseLegalityReport(LegalityAnalysis la) => Formatter.GetReportVerbose(la);
|
|
|
|
public static void AddSecondaryChecksValid(IEnumerable<CheckResult> results, List<string> lines)
|
|
{
|
|
var outputLines = results
|
|
.Where(chk => chk.Valid && chk.Comment != L_AValid)
|
|
.OrderBy(chk => chk.Judgement) // Fishy sorted to top
|
|
.Select(chk => chk.Format(L_F0_1));
|
|
lines.AddRange(outputLines);
|
|
}
|
|
|
|
public static void AddSecondaryChecksInvalid(IReadOnlyList<CheckResult> results, List<string> lines)
|
|
{
|
|
foreach (var chk in results)
|
|
{
|
|
if (chk.Valid)
|
|
continue;
|
|
lines.Add(chk.Format(L_F0_1));
|
|
}
|
|
}
|
|
|
|
public static void AddRelearn(ReadOnlySpan<MoveResult> relearn, List<string> lines, bool state, PKM pk, EvolutionHistory history)
|
|
{
|
|
for (int i = 0; i < relearn.Length; i++)
|
|
{
|
|
var move = relearn[i];
|
|
if (move.Valid == state)
|
|
lines.Add(move.Format(L_F0_RM_1_2, i + 1, pk, history));
|
|
}
|
|
}
|
|
|
|
public static void AddMoves(ReadOnlySpan<MoveResult> moves, List<string> lines, in int currentFormat, bool state, PKM pk, EvolutionHistory history)
|
|
{
|
|
for (int i = 0; i < moves.Length; i++)
|
|
{
|
|
var move = moves[i];
|
|
if (move.Valid != state)
|
|
continue;
|
|
var msg = move.Format(L_F0_M_1_2, i + 1, pk, history);
|
|
var gen = move.Generation;
|
|
if (currentFormat != gen && gen != 0)
|
|
msg += $" [Gen{gen}]";
|
|
lines.Add(msg);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds information about the <see cref="LegalityAnalysis.EncounterMatch"/> to the <see cref="lines"/>.
|
|
/// </summary>
|
|
public static void AddEncounterInfo(LegalityAnalysis la, List<string> lines)
|
|
{
|
|
var enc = la.EncounterOriginal;
|
|
|
|
// Name
|
|
lines.Add(string.Format(L_FEncounterType_0, enc.GetEncounterName()));
|
|
if (enc is MysteryGift g)
|
|
lines.Add(g.CardHeader);
|
|
|
|
// Location
|
|
var loc = enc.GetEncounterLocation();
|
|
if (!string.IsNullOrEmpty(loc))
|
|
lines.Add(string.Format(L_F0_1, "Location", loc));
|
|
|
|
// Version
|
|
if (enc.Generation <= 2)
|
|
lines.Add(string.Format(L_F0_1, nameof(GameVersion), enc.Version));
|
|
|
|
// PIDIV
|
|
AddEncounterInfoPIDIV(la, lines);
|
|
}
|
|
|
|
public static void AddEncounterInfoPIDIV(LegalityAnalysis la, List<string> lines)
|
|
{
|
|
var info = la.Info;
|
|
if (!info.PIDParsed)
|
|
info.PIDIV = MethodFinder.Analyze(la.Entity);
|
|
AddEncounterInfoPIDIV(lines, info.PIDIV);
|
|
}
|
|
|
|
private static void AddEncounterInfoPIDIV(List<string> lines, PIDIV pidiv)
|
|
{
|
|
lines.Add(string.Format(L_FPIDType_0, pidiv.Type));
|
|
if (!pidiv.NoSeed)
|
|
lines.Add(string.Format(L_FOriginSeed_0, pidiv.OriginSeed.ToString("X8")));
|
|
}
|
|
|
|
public static string GetEncounterName(this IEncounterable enc)
|
|
{
|
|
var str = ParseSettings.SpeciesStrings;
|
|
var name = (uint) enc.Species < str.Count ? str[enc.Species] : enc.Species.ToString();
|
|
return $"{enc.LongName} ({name})";
|
|
}
|
|
|
|
public static string? GetEncounterLocation(this IEncounterTemplate enc)
|
|
{
|
|
if (enc is not ILocation loc)
|
|
return null;
|
|
return loc.GetEncounterLocation(enc.Generation, (int)enc.Version);
|
|
}
|
|
}
|