Tweaks and fixes

This commit is contained in:
Deukhoofd 2025-07-05 18:27:42 +02:00
parent d57076374f
commit 83f6a183e3
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
4 changed files with 33 additions and 4 deletions

View File

@ -19,14 +19,15 @@ public static class TestCommandRunner
var library = DynamicLibraryImpl.Create([ var library = DynamicLibraryImpl.Create([
new Gen7Plugin(), new Gen7Plugin(),
]); ]);
const int maxTasks = 10;
Log.Information("Running {Battles} battles between {AI1} and {AI2}", battles, ai1.Name, ai2.Name); Log.Information("Running {Battles} battles between {AI1} and {AI2}", battles, ai1.Name, ai2.Name);
var averageTimePerTurnPerBattle = new List<double>(battles); var averageTimePerTurnPerBattle = new List<double>(battles);
var turnsPerBattle = new ConcurrentBag<uint>(); var turnsPerBattle = new ConcurrentBag<uint>();
var results = new ConcurrentBag<BattleResult>(); var results = new ConcurrentBag<BattleResult>();
var timePerBatch = new List<double>(battles / maxTasks);
var rootRandom = new RandomImpl(); var rootRandom = new RandomImpl();
const int maxTasks = 10;
var battleTasks = new Task[maxTasks]; var battleTasks = new Task[maxTasks];
var randoms = new IRandom[maxTasks]; var randoms = new IRandom[maxTasks];
for (var i = 0; i < maxTasks; i++) for (var i = 0; i < maxTasks; i++)
@ -36,7 +37,8 @@ public static class TestCommandRunner
} }
// Show a progress bar if debug logging is not enabled. // Show a progress bar if debug logging is not enabled.
// This is to avoid weird console output where the progress bar is drawn in the middle of debug logs. // We disable this if debug logging is on, to prevent annoying console output where the progress bar is drawn in
// the middle of debug logs.
ProgressBar? pb = null; ProgressBar? pb = null;
if (!Log.IsEnabled(LogEventLevel.Debug)) if (!Log.IsEnabled(LogEventLevel.Debug))
{ {
@ -47,6 +49,7 @@ public static class TestCommandRunner
}); });
pb.EstimatedDuration = TimeSpan.FromMilliseconds(battles); pb.EstimatedDuration = TimeSpan.FromMilliseconds(battles);
} }
var batchStartTime = DateTime.UtcNow;
for (var i = 0; i < battles; i++) for (var i = 0; i < battles; i++)
{ {
var taskIndex = i % maxTasks; var taskIndex = i % maxTasks;
@ -87,6 +90,14 @@ public static class TestCommandRunner
await Task.WhenAll(battleTasks); await Task.WhenAll(battleTasks);
Log.Debug("Batch of {TaskCount} tasks completed", maxTasks); Log.Debug("Batch of {TaskCount} tasks completed", maxTasks);
Array.Fill(battleTasks, Task.CompletedTask); // Reset tasks for the next batch Array.Fill(battleTasks, Task.CompletedTask); // Reset tasks for the next batch
var batchTime = (DateTime.UtcNow - batchStartTime).TotalMilliseconds;
if (pb != null)
{
timePerBatch.Add(batchTime);
pb.EstimatedDuration = TimeSpan.FromMilliseconds(timePerBatch.Average() * battles / maxTasks);
}
batchStartTime = DateTime.UtcNow; // Reset batch start time after each batch
} }
} }
pb?.Dispose(); pb?.Dispose();

View File

@ -10,7 +10,7 @@ namespace PkmnLib.Dynamic.AI;
/// </summary> /// </summary>
public class RandomAI : PokemonAI public class RandomAI : PokemonAI
{ {
private IRandom _random; private readonly IRandom _random;
/// <inheritdoc /> /// <inheritdoc />
public RandomAI() : base("Random") public RandomAI() : base("Random")

View File

@ -47,3 +47,21 @@ public enum Statistic : byte
/// </summary> /// </summary>
Accuracy, Accuracy,
} }
/// <summary>
/// Helper class for <see cref="Statistic"/>.
/// </summary>
public static class StatisticHelper
{
/// <summary>
/// Gets all statistics that are defined in the game, including Evasion and Accuracy.
/// </summary>
public static IEnumerable<Statistic> GetAllStatistics() => Enum.GetValues(typeof(Statistic)).Cast<Statistic>();
/// <summary>
/// Gets the standard statistics that are visible in the Pokémon's base stats. Excludes Evasion and Accuracy.s
/// </summary>
public static IEnumerable<Statistic> GetStandardStatistics() =>
Enum.GetValues(typeof(Statistic)).Cast<Statistic>()
.Where(stat => stat != Statistic.Evasion && stat != Statistic.Accuracy);
}

View File

@ -8,7 +8,7 @@ public class PsychUp : Script, IScriptOnSecondaryEffect
{ {
var targetStats = target.StatBoost; var targetStats = target.StatBoost;
var userStats = move.User.StatBoost; var userStats = move.User.StatBoost;
foreach (Statistic stat in Enum.GetValues(typeof(Statistic))) foreach (var stat in StatisticHelper.GetStandardStatistics())
{ {
var targetStat = targetStats.GetStatistic(stat); var targetStat = targetStats.GetStatistic(stat);
var userStat = userStats.GetStatistic(stat); var userStat = userStats.GetStatistic(stat);