diff --git a/AI/AIRunner/TestCommandRunner.cs b/AI/AIRunner/TestCommandRunner.cs index 125379b..9d2d3d2 100644 --- a/AI/AIRunner/TestCommandRunner.cs +++ b/AI/AIRunner/TestCommandRunner.cs @@ -19,14 +19,15 @@ public static class TestCommandRunner var library = DynamicLibraryImpl.Create([ new Gen7Plugin(), ]); + const int maxTasks = 10; Log.Information("Running {Battles} battles between {AI1} and {AI2}", battles, ai1.Name, ai2.Name); var averageTimePerTurnPerBattle = new List(battles); var turnsPerBattle = new ConcurrentBag(); var results = new ConcurrentBag(); + var timePerBatch = new List(battles / maxTasks); var rootRandom = new RandomImpl(); - const int maxTasks = 10; var battleTasks = new Task[maxTasks]; var randoms = new IRandom[maxTasks]; 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. - // 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; if (!Log.IsEnabled(LogEventLevel.Debug)) { @@ -47,6 +49,7 @@ public static class TestCommandRunner }); pb.EstimatedDuration = TimeSpan.FromMilliseconds(battles); } + var batchStartTime = DateTime.UtcNow; for (var i = 0; i < battles; i++) { var taskIndex = i % maxTasks; @@ -87,6 +90,14 @@ public static class TestCommandRunner await Task.WhenAll(battleTasks); Log.Debug("Batch of {TaskCount} tasks completed", maxTasks); 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(); diff --git a/PkmnLib.Dynamic/AI/RandomAI.cs b/PkmnLib.Dynamic/AI/RandomAI.cs index 1d9fac5..defeb98 100644 --- a/PkmnLib.Dynamic/AI/RandomAI.cs +++ b/PkmnLib.Dynamic/AI/RandomAI.cs @@ -10,7 +10,7 @@ namespace PkmnLib.Dynamic.AI; /// public class RandomAI : PokemonAI { - private IRandom _random; + private readonly IRandom _random; /// public RandomAI() : base("Random") diff --git a/PkmnLib.Static/Statistic.cs b/PkmnLib.Static/Statistic.cs index 98916d6..689ee7f 100644 --- a/PkmnLib.Static/Statistic.cs +++ b/PkmnLib.Static/Statistic.cs @@ -46,4 +46,22 @@ public enum Statistic : byte /// This is not part of base stats, but is a temporary stat boost. /// Accuracy, +} + +/// +/// Helper class for . +/// +public static class StatisticHelper +{ + /// + /// Gets all statistics that are defined in the game, including Evasion and Accuracy. + /// + public static IEnumerable GetAllStatistics() => Enum.GetValues(typeof(Statistic)).Cast(); + + /// + /// Gets the standard statistics that are visible in the Pokémon's base stats. Excludes Evasion and Accuracy.s + /// + public static IEnumerable GetStandardStatistics() => + Enum.GetValues(typeof(Statistic)).Cast() + .Where(stat => stat != Statistic.Evasion && stat != Statistic.Accuracy); } \ No newline at end of file diff --git a/Plugins/PkmnLib.Plugin.Gen7/Scripts/Moves/PsychUp.cs b/Plugins/PkmnLib.Plugin.Gen7/Scripts/Moves/PsychUp.cs index 91058b8..cdeae0a 100644 --- a/Plugins/PkmnLib.Plugin.Gen7/Scripts/Moves/PsychUp.cs +++ b/Plugins/PkmnLib.Plugin.Gen7/Scripts/Moves/PsychUp.cs @@ -8,7 +8,7 @@ public class PsychUp : Script, IScriptOnSecondaryEffect { var targetStats = target.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 userStat = userStats.GetStatistic(stat);