diff --git a/src/AcDream.App/Rendering/GameWindow.cs b/src/AcDream.App/Rendering/GameWindow.cs index f34fb77..bdf88d6 100644 --- a/src/AcDream.App/Rendering/GameWindow.cs +++ b/src/AcDream.App/Rendering/GameWindow.cs @@ -8796,7 +8796,12 @@ public sealed class GameWindow : IDisposable int nz = 0; foreach (var v in copy) if (v > 0) nz++; if (nz == 0) return 0; - return copy[copy.Length - nz / 2]; + // Sorted ascending: zero-padding at the front, samples at the back. + // Median of nz samples is the middle of the last nz entries; using + // (nz - 1) / 2 from the end keeps the offset >= 0 for all nz >= 1 + // (the original nz / 2 form underflowed to copy.Length on first + // diag-flush when only 1 sample had been recorded). + return copy[copy.Length - 1 - (nz - 1) / 2]; } private static long TerrainDiagPercentile95Micros(long[] samples) @@ -8806,8 +8811,10 @@ public sealed class GameWindow : IDisposable int nz = 0; foreach (var v in copy) if (v > 0) nz++; if (nz == 0) return 0; - int idx = copy.Length - 1 - (int)(nz * 0.05); - return copy[idx]; + // 95th percentile = upper end of the sorted samples; clamp the + // offset to stay inside the populated tail when nz < 20. + int offset = (int)((nz - 1) * 0.05); + return copy[copy.Length - 1 - offset]; } private void OnClosing()