2022-11-24 17:42:17 -08:00
|
|
|
using System;
|
2021-08-05 20:47:32 -07:00
|
|
|
using System.Collections.Generic;
|
2017-07-25 00:28:43 -07:00
|
|
|
|
2022-06-18 11:04:24 -07:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Miscellaneous setup utility for legality checking <see cref="IEncounterTemplate"/> data sources.
|
|
|
|
/// </summary>
|
|
|
|
internal static class EncounterUtil
|
2017-07-25 00:28:43 -07:00
|
|
|
{
|
2022-11-24 17:42:17 -08:00
|
|
|
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);
|
2022-06-18 11:04:24 -07:00
|
|
|
|
2023-07-05 21:14:09 -07:00
|
|
|
internal static T? GetMinByLevel<T>(ReadOnlySpan<EvoCriteria> chain, IEnumerable<T> possible) where T : class, IEncounterTemplate
|
2022-06-18 11:04:24 -07:00
|
|
|
{
|
|
|
|
// MinBy grading: prefer species-form match, select lowest min level encounter.
|
|
|
|
// Minimum allocation :)
|
|
|
|
T? result = null;
|
|
|
|
int min = int.MaxValue;
|
2017-07-30 12:31:17 -07:00
|
|
|
|
2022-06-18 11:04:24 -07:00
|
|
|
foreach (var enc in possible)
|
2022-05-07 21:25:26 -07:00
|
|
|
{
|
2022-06-18 11:04:24 -07:00
|
|
|
int m = int.MaxValue;
|
|
|
|
foreach (var evo in chain)
|
2022-05-07 21:25:26 -07:00
|
|
|
{
|
2022-06-18 11:04:24 -07:00
|
|
|
bool specDiff = enc.Species != evo.Species || enc.Form != evo.Form;
|
|
|
|
var val = (Convert.ToInt32(specDiff) << 16) | enc.LevelMin;
|
|
|
|
if (val < m)
|
|
|
|
m = val;
|
2022-05-07 21:25:26 -07:00
|
|
|
}
|
|
|
|
|
2022-06-18 11:04:24 -07:00
|
|
|
if (m >= min)
|
|
|
|
continue;
|
|
|
|
min = m;
|
|
|
|
result = enc;
|
2022-05-07 21:25:26 -07:00
|
|
|
}
|
|
|
|
|
2022-06-18 11:04:24 -07:00
|
|
|
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>
|
2023-08-12 16:01:16 -07:00
|
|
|
public static string[] GetNamesForLanguage(ReadOnlySpan<string[]> names, uint index)
|
2022-06-18 11:04:24 -07:00
|
|
|
{
|
|
|
|
var result = new string[names.Length];
|
|
|
|
for (int i = 0; i < result.Length; i++)
|
2021-08-05 20:47:32 -07:00
|
|
|
{
|
2022-06-18 11:04:24 -07:00
|
|
|
var arr = names[i];
|
|
|
|
result[i] = index < arr.Length ? arr[index] : string.Empty;
|
2018-03-26 22:23:11 -07:00
|
|
|
}
|
2022-06-18 11:04:24 -07:00
|
|
|
return result;
|
2017-07-25 00:28:43 -07:00
|
|
|
}
|
|
|
|
}
|