mirror of
https://github.com/kwsch/PKHeX
synced 2024-11-27 06:20:25 +00:00
f632aedd15
We implement simple state machine iterators to iterate through every split type encounter array, and more finely control the path we iterate through. And, by using generics, we can have the compiler generate optimized code to avoid virtual calls. In addition to this, we shift away from the big-5 encounter types and not inherit from an abstract class. This allows for creating a PK* of a specific type and directly writing properties (no virtual calls). Plus we can now fine-tune each encounter type to call specific code, and not have to worry about future game encounter types bothering the generation routines.
57 lines
2 KiB
C#
57 lines
2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace PKHeX.Core;
|
|
|
|
/// <summary>
|
|
/// Miscellaneous setup utility for legality checking <see cref="IEncounterTemplate"/> data sources.
|
|
/// </summary>
|
|
internal static class EncounterUtil
|
|
{
|
|
internal static ReadOnlySpan<byte> Get(string resource) => Util.GetBinaryResource($"encounter_{resource}.pkl");
|
|
internal static BinLinkerAccessor Get(string resource, string ident) => BinLinkerAccessor.Get(Get(resource), ident);
|
|
|
|
internal static T? GetMinByLevel<T>(ReadOnlySpan<EvoCriteria> chain, IEnumerable<T> possible) where T : class, IEncounterTemplate
|
|
{
|
|
// MinBy grading: prefer species-form match, select lowest min level encounter.
|
|
// Minimum allocation :)
|
|
T? result = null;
|
|
int min = int.MaxValue;
|
|
|
|
foreach (var enc in possible)
|
|
{
|
|
int m = int.MaxValue;
|
|
foreach (var evo in chain)
|
|
{
|
|
bool specDiff = enc.Species != evo.Species || enc.Form != evo.Form;
|
|
var val = (Convert.ToInt32(specDiff) << 16) | enc.LevelMin;
|
|
if (val < m)
|
|
m = val;
|
|
}
|
|
|
|
if (m >= min)
|
|
continue;
|
|
min = m;
|
|
result = enc;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Grabs the localized names for individual templates for all languages from the specified <see cref="index"/> of the <see cref="names"/> list.
|
|
/// </summary>
|
|
/// <param name="names">Arrays of strings grouped by language</param>
|
|
/// <param name="index">Index to grab from the language arrays</param>
|
|
/// <returns>Row of localized strings for the template.</returns>
|
|
public static string[] GetNamesForLanguage(ReadOnlySpan<string[]> names, uint index)
|
|
{
|
|
var result = new string[names.Length];
|
|
for (int i = 0; i < result.Length; i++)
|
|
{
|
|
var arr = names[i];
|
|
result[i] = index < arr.Length ? arr[index] : string.Empty;
|
|
}
|
|
return result;
|
|
}
|
|
}
|