Convert GetLevelLearnMove to use lazy dictionary

faster move lookup
This commit is contained in:
Kurt
2018-06-07 19:05:28 -07:00
parent 17553e6b6a
commit b1cd68e3f3

View File

@@ -66,13 +66,23 @@ public int GetMinMoveLevel(int level)
return Math.Max(end - 4, 1);
}
private Dictionary<int, int> Learn;
private Dictionary<int, int> GetDictionary()
{
var dict = new Dictionary<int, int>();
for (int i = 0; i < Moves.Length; i++)
if (!dict.ContainsKey(Moves[i]))
dict.Add(Moves[i], Levels[i]);
return dict;
}
/// <summary>Returns the level that a Pokémon can learn the specified move.</summary>
/// <param name="move">Move ID</param>
/// <returns>Level the move is learned at. If the result is below 0, it cannot be learned by levelup.</returns>
public int GetLevelLearnMove(int move)
{
int index = Array.IndexOf(Moves, move);
return index < 0 ? index : Levels[index];
return (Learn ?? (Learn = GetDictionary())).TryGetValue(move, out var level) ? level : -1;
}
}
}