From e75df9dfb3b5773272bd9b5fe89cd7d9503e24e2 Mon Sep 17 00:00:00 2001 From: mischa Date: Mon, 24 Jul 2023 17:57:36 +0800 Subject: [PATCH] Insert: naive implementation --- .../Mirror/Core/Prediction/HistoryBounds.cs | 21 ++++++++-- .../Editor/Prediction/HistoryBoundsTests.cs | 39 +++++++++++++++++-- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/Assets/Mirror/Core/Prediction/HistoryBounds.cs b/Assets/Mirror/Core/Prediction/HistoryBounds.cs index 684df7300..3b69d6f66 100644 --- a/Assets/Mirror/Core/Prediction/HistoryBounds.cs +++ b/Assets/Mirror/Core/Prediction/HistoryBounds.cs @@ -11,12 +11,25 @@ namespace Mirror public static class HistoryBounds { // insert current bounds into history. returns new total bounds. - public static Bounds Insert(Queue history, Bounds bounds) + // Queue.Dequeue() always has the oldest bounds. + public static Bounds Insert(Queue history, int limit, Bounds bounds) { - // TODO insert new - // TODO remove old based on history limit + // remove oldest if limit reached + if (history.Count >= limit) + history.Dequeue(); - return bounds; + // insert the new bounds + history.Enqueue(bounds); + + // summarize total bounds. + // starting at latest bounds, not at 'new Bounds' because that would + // encapsulate (0,0) too. + // TODO make this not be O(N) + Bounds total = bounds; + foreach (Bounds b in history) + total.Encapsulate(b); + + return total; } diff --git a/Assets/Mirror/Tests/Editor/Prediction/HistoryBoundsTests.cs b/Assets/Mirror/Tests/Editor/Prediction/HistoryBoundsTests.cs index b200b5a24..8c28be114 100644 --- a/Assets/Mirror/Tests/Editor/Prediction/HistoryBoundsTests.cs +++ b/Assets/Mirror/Tests/Editor/Prediction/HistoryBoundsTests.cs @@ -14,13 +14,44 @@ public void SetUp() history = new Queue(); } + // helper function to construct (min, max) bounds + public static Bounds MinMax(Vector3 min, Vector3 max) + { + Bounds bounds = new Bounds(); + bounds.SetMinMax(min, max); + return bounds; + } + [Test] public void Insert() { - // insert initial. new bounds should be initial. - Bounds initial = new Bounds(Vector3.zero, Vector3.one); - Bounds total = HistoryBounds.Insert(history, initial); - Assert.That(total, Is.EqualTo(initial)); + const int limit = 3; + + // insert initial [-1, 1]. + // should calculate new bounds == initial. + Bounds total = HistoryBounds.Insert(history, limit, MinMax(-Vector3.one, Vector3.one)); + Assert.That(history.Count, Is.EqualTo(1)); + Assert.That(total, Is.EqualTo(MinMax(-Vector3.one, Vector3.one))); + + // insert [0, 2] + // should calculate new bounds == [-1, 2]. + total = HistoryBounds.Insert(history, limit, MinMax(Vector3.zero, Vector3.one * 2)); + Assert.That(history.Count, Is.EqualTo(2)); + Assert.That(total, Is.EqualTo(MinMax(-Vector3.one, Vector3.one * 2))); + + // insert one that's smaller than current bounds [-.5, 0] + // history needs to contain it even if smaller, because once the oldest + // largest one gets removed, this one matters too. + total = HistoryBounds.Insert(history, limit, MinMax(-Vector3.one / 2, Vector3.zero)); + Assert.That(history.Count, Is.EqualTo(3)); + Assert.That(total, Is.EqualTo(MinMax(-Vector3.one, Vector3.one * 2))); + + // insert more than 'limit': [0, 0] + // the oldest one [-1, 1] should be discarded. + // new bounds should be [-0.5, 2] + total = HistoryBounds.Insert(history, limit, MinMax(Vector3.zero, Vector3.zero)); + Assert.That(history.Count, Is.EqualTo(3)); + Assert.That(total, Is.EqualTo(MinMax(-Vector3.one / 2, Vector3.one * 2))); } } }