Minor optimizations

Only fetch egg moves when using them
Localize KOR gen2 string directly instead of switch expression
Remap FRE gen4 chars directly, hot path instead of dictionary lookup
Uppercase chars in place
This commit is contained in:
Kurt 2023-03-21 17:20:13 -07:00
parent 6247a61bd3
commit 46de8c4b06
7 changed files with 28 additions and 25 deletions

View File

@ -37,7 +37,6 @@ public static bool Validate(ushort species, GameVersion version, ReadOnlySpan<us
var learnset = learn[species];
var pi = table[species];
var egg = Legal.EggMovesRS[species].Moves;
var actual = MemoryMarshal.Cast<byte, EggSource34>(origins);
Span<byte> possible = stackalloc byte[count];
@ -52,6 +51,7 @@ public static bool Validate(ushort species, GameVersion version, ReadOnlySpan<us
}
else
{
var egg = Legal.EggMovesRS[species].Moves;
bool inherit = Breeding.GetCanInheritMoves(species);
MarkMovesForOrigin(value, egg, count, inherit, pi);
valid = RecurseMovesForOrigin(value, count - 1);

View File

@ -33,7 +33,6 @@ public static bool Validate(ushort species, GameVersion version, ReadOnlySpan<us
_ => PersonalTable.Pt,
};
var pi = table[species];
var egg = (version is HG or SS ? Legal.EggMovesHGSS : Legal.EggMovesDPPt)[species].Moves;
var actual = MemoryMarshal.Cast<byte, EggSource34>(origins);
Span<byte> possible = stackalloc byte[count];
@ -49,6 +48,7 @@ public static bool Validate(ushort species, GameVersion version, ReadOnlySpan<us
else
{
bool inherit = Breeding.GetCanInheritMoves(species);
var egg = (version is HG or SS ? Legal.EggMovesHGSS : Legal.EggMovesDPPt)[species].Moves;
MarkMovesForOrigin(value, egg, count, inherit, pi, version);
valid = RecurseMovesForOrigin(value, count - 1);
}

View File

@ -29,7 +29,6 @@ public static bool Validate(ushort species, GameVersion version, ReadOnlySpan<us
GameVersion.B or GameVersion.W => PersonalTable.BW[species],
_ => PersonalTable.B2W2[species],
};
var egg = Legal.EggMovesBW[species].Moves;
var actual = MemoryMarshal.Cast<byte, EggSource5>(origins);
Span<byte> possible = stackalloc byte[count];
@ -45,6 +44,7 @@ public static bool Validate(ushort species, GameVersion version, ReadOnlySpan<us
else
{
bool inherit = Breeding.GetCanInheritMoves(species);
var egg = Legal.EggMovesBW[species].Moves;
MarkMovesForOrigin(value, egg, count, inherit, pi);
valid = RecurseMovesForOrigin(value, count - 1);
}

View File

@ -26,7 +26,6 @@ public static bool Validate(int generation, ushort species, byte form, GameVersi
var table = GameData.GetPersonal(version);
var index = table.GetFormIndex(species, form);
var learnset = learn[index];
var egg = MoveEgg.GetEggMoves(generation, species, form, version);
var actual = MemoryMarshal.Cast<byte, EggSource6>(origins);
Span<byte> possible = stackalloc byte[count];
@ -41,6 +40,7 @@ public static bool Validate(int generation, ushort species, byte form, GameVersi
}
else
{
var egg = MoveEgg.GetEggMoves(generation, species, form, version);
bool inherit = Breeding.GetCanInheritMoves(species);
MarkMovesForOrigin(value, egg, count, inherit);
valid = RecurseMovesForOrigin(value, count - 1);

View File

@ -136,14 +136,16 @@ public static int LoadString(ReadOnlySpan<byte> data, Span<char> result)
/// <summary>
/// Localizes a Gen4+ Korean species name to the localization used in Generation 2 Gold/Silver
/// </summary>
/// <param name="species">Species ID</param>
/// <param name="nick">Generation 4 Species Name</param>
/// <returns>Localized Name for Generation 2</returns>
public static string LocalizeKOR2(string nick) => nick switch
public static void LocalizeKOR2(ushort species, ref string nick)
{
"덩쿠리" => "덩구리", // Tangela
"슈륙챙이" => "수륙챙이", // Poliwhirl
_ => nick,
};
if (species == 61) // Poliwhirl
nick = "수륙챙이"; // "슈륙챙이" in future games
else if (species == 114) // Tangela
nick = "덩구리"; // "덩쿠리" in future games
}
#region Gen 2 Korean Character Tables
private static readonly Dictionary<char, byte> U2GSC_KOR_0 = new(256)

View File

@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
@ -39,21 +39,21 @@ public static ushort ConvertChar2ValueG4(ushort chr)
/// <remarks>Only 4 characters are accented in gen1-4</remarks>
public static void StripDiacriticsFR4(Span<char> input)
{
for (int i = 0; i < input.Length; i++)
foreach (ref var c in input)
{
if (FrDiacritic.TryGetValue(input[i], out var value))
input[i] = value;
// È É Ê Ï
// C8 C9 CA map to 'E'
// CF map to 'I'
var delta = (uint)(c - 'È');
if (delta > 7)
continue;
if (delta < 3)
c = 'E';
else if (delta == 7)
c = 'I';
}
}
private static readonly Dictionary<char, char> FrDiacritic = new(4)
{
{ 'È', 'E' },
{ 'É', 'E' },
{ 'Ê', 'E' },
{ 'Ï', 'I' },
};
#region Conversion Data
/// <summary>

View File

@ -107,9 +107,10 @@ private static string GetSpeciesName1234(ushort species, int language, int gener
var nick = GetSpeciesName(species, language);
switch (language)
{
case (int)LanguageID.Korean when generation == 2:
return StringConverter2KOR.LocalizeKOR2(nick);
case (int)LanguageID.Korean:
if (generation == 2)
StringConverter2KOR.LocalizeKOR2(species, ref nick);
return nick; // No further processing
case (int)LanguageID.Japanese:
return nick; // No further processing
}
@ -118,8 +119,8 @@ private static string GetSpeciesName1234(ushort species, int language, int gener
nick.CopyTo(result);
// All names are uppercase.
for (int i = 0; i < result.Length; i++)
result[i] = char.ToUpperInvariant(result[i]);
foreach (ref var c in result)
c = char.ToUpperInvariant(c);
if (language == (int)LanguageID.French)
StringConverter4Util.StripDiacriticsFR4(result); // strips accents on E and I