GenerationStats.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System.Collections.Generic;
  2. using System.Diagnostics;
  3. using System.Linq;
  4. namespace DunGen
  5. {
  6. public sealed class GenerationStats
  7. {
  8. #region Nested Types
  9. public sealed class TileStatistics
  10. {
  11. public Tile TilePrefab { get; set; } = null;
  12. public int TotalCount { get; set; } = 0;
  13. public int FromPoolCount { get; set; } = 0;
  14. }
  15. #endregion
  16. public int MainPathRoomCount { get; private set; }
  17. public int BranchPathRoomCount { get; private set; }
  18. public int TotalRoomCount { get; private set; }
  19. public int MaxBranchDepth { get; private set; }
  20. public int TotalRetries { get; private set; }
  21. public int PrunedBranchTileCount { get; internal set; }
  22. public Dictionary<GenerationStatus, float> GenerationStepTimes { get; private set; }
  23. public float TotalTime => GenerationStepTimes.Values.Sum();
  24. private Stopwatch stopwatch = new Stopwatch();
  25. private GenerationStatus generationStatus;
  26. private readonly Dictionary<Tile, TileStatistics> tileStatistics = new Dictionary<Tile, TileStatistics>();
  27. public GenerationStats()
  28. {
  29. GenerationStepTimes = new Dictionary<GenerationStatus, float>();
  30. }
  31. public float GetGenerationStepTime(GenerationStatus step)
  32. {
  33. if (GenerationStepTimes.TryGetValue(step, out float time))
  34. return time;
  35. else
  36. return 0f;
  37. }
  38. public IEnumerable<TileStatistics> GetTileStatistics() => tileStatistics.Values;
  39. public void TileAdded(Tile tilePrefab, bool fromPool)
  40. {
  41. if (!tileStatistics.TryGetValue(tilePrefab, out TileStatistics stats))
  42. {
  43. stats = new TileStatistics
  44. {
  45. TilePrefab = tilePrefab
  46. };
  47. tileStatistics.Add(tilePrefab, stats);
  48. }
  49. stats.TotalCount++;
  50. if (fromPool)
  51. stats.FromPoolCount++;
  52. }
  53. internal void Clear()
  54. {
  55. MainPathRoomCount = 0;
  56. BranchPathRoomCount = 0;
  57. TotalRoomCount = 0;
  58. MaxBranchDepth = 0;
  59. TotalRetries = 0;
  60. PrunedBranchTileCount = 0;
  61. GenerationStepTimes.Clear();
  62. tileStatistics.Clear();
  63. }
  64. internal void IncrementRetryCount()
  65. {
  66. TotalRetries++;
  67. }
  68. internal void SetRoomStatistics(int mainPathRoomCount, int branchPathRoomCount, int maxBranchDepth)
  69. {
  70. MainPathRoomCount = mainPathRoomCount;
  71. BranchPathRoomCount = branchPathRoomCount;
  72. MaxBranchDepth = maxBranchDepth;
  73. TotalRoomCount = MainPathRoomCount + BranchPathRoomCount;
  74. }
  75. internal void BeginTime(GenerationStatus status)
  76. {
  77. if (stopwatch.IsRunning)
  78. EndTime();
  79. generationStatus = status;
  80. stopwatch.Reset();
  81. stopwatch.Start();
  82. }
  83. internal void EndTime()
  84. {
  85. stopwatch.Stop();
  86. float elapsedTime = (float)stopwatch.Elapsed.TotalMilliseconds;
  87. GenerationStepTimes.TryGetValue(generationStatus, out float currentTime);
  88. currentTime += elapsedTime;
  89. GenerationStepTimes[generationStatus] = currentTime;
  90. }
  91. public GenerationStats Clone()
  92. {
  93. GenerationStats newStats = new GenerationStats();
  94. newStats.MainPathRoomCount = MainPathRoomCount;
  95. newStats.BranchPathRoomCount = BranchPathRoomCount;
  96. newStats.TotalRoomCount = TotalRoomCount;
  97. newStats.MaxBranchDepth = MaxBranchDepth;
  98. newStats.TotalRetries = TotalRetries;
  99. newStats.PrunedBranchTileCount = PrunedBranchTileCount;
  100. newStats.GenerationStepTimes = new Dictionary<GenerationStatus, float>(GenerationStepTimes);
  101. return newStats;
  102. }
  103. }
  104. }