diff --git a/Assets/Scripts/ServerShared/TacticalView.cs b/Assets/Scripts/ServerShared/TacticalView.cs index 55b78687e..295f16f83 100644 --- a/Assets/Scripts/ServerShared/TacticalView.cs +++ b/Assets/Scripts/ServerShared/TacticalView.cs @@ -39,7 +39,11 @@ public sealed class TacticalView // Commanded and eased positions and the ease velocity, in ln metres; the grid point the tactical camera looks at. public float Commanded { get; private set; } private float _eased, _velocity; - public float2 Target { get; private set; } + private float2 _ship, _pan; + + // The grid point the camera looks at: the ship in flight, the panned grid point in tactical, eased between by the + // rig's own weight (the blend of the eased distance), never by the commanded flip. + public float2 Target => _ship + (_pan - _ship) * Blend(Distance); // Derived, never stored: the commanded position is past the detent's crest. public bool Tactical => Commanded > _crest; @@ -55,7 +59,7 @@ public sealed class TacticalView _zoneRadius = zoneRadius; Commanded = _eased = _lnBase; _velocity = 0; - Target = shipXZ; + _ship = _pan = shipXZ; } // Each notch (positive zooms out) moves the commanded position by ln ZoomStep. Inside a view it moves freely. A notch @@ -110,19 +114,22 @@ public sealed class TacticalView _velocity = (_velocity - w * carried * realSeconds) * decay; } - // The target rides the ship while flying; only a tactical player moves it. + // The ship's grid position, every frame. The panned point rides the ship while the eased camera is in flight (to within + // float noise of the band's near edge, a weight of about a millionth), so a pan starts from the hull; once the camera + // leaves flight it stays where the player put it, and the eased weight carries Target back to the ship on the way home. public void Track(float2 shipXZ) { - if (!Tactical) Target = shipXZ; + _ship = shipXZ; + if (!Tactical && Distance <= _blendStart * 1.001f) _pan = shipXZ; } // Move the target over the grid by a ground delta, never off the zone. public void Pan(float2 groundDelta) { if (!Tactical) return; - var moved = Target + groundDelta; + var moved = _pan + groundDelta; var reach = length(moved); - Target = reach > _zoneRadius && reach > 0 ? moved * (_zoneRadius / reach) : moved; + _pan = reach > _zoneRadius && reach > 0 ? moved * (_zoneRadius / reach) : moved; } // 0 at or below the band's near edge, 1 at or above its far edge, smooth in the logarithm of distance between. diff --git a/tests/Aetheria.Shared.Tests/TacticalViewTests.cs b/tests/Aetheria.Shared.Tests/TacticalViewTests.cs index f77dfe407..05d4a8f6b 100644 --- a/tests/Aetheria.Shared.Tests/TacticalViewTests.cs +++ b/tests/Aetheria.Shared.Tests/TacticalViewTests.cs @@ -207,6 +207,7 @@ public sealed class TacticalViewTests ToFlightEdge(view); Cross(view); Assert.True(view.Tactical); + Settle(view); view.Track(new float2(-900, 900)); Assert.Equal(new float2(7, 8), view.Target); view.Pan(new float2(10, -4)); @@ -277,4 +278,148 @@ public sealed class TacticalViewTests // Flying never sees it: every distance at or under the near edge runs at full speed. Assert.Equal(1f, view.TimeRate(view.Blend(Start))); } + + // The committed notch count of a held scroll at a rate, ticked between notches; 0 if it never crosses in 200. + private static int HeldCrossing(float perSecond) + { + var view = Make(); + ToFlightEdge(view); + for (var i = 1; i <= 200; i++) + { + view.Tick(1 / perSecond); + view.Scroll(1); + if (view.Tactical) return i; + } + return 0; + } + + [Fact] + public void TenNotchesASecondHoldsAndFifteenCrosses() + { + Assert.Equal(0, HeldCrossing(10)); + Assert.InRange(HeldCrossing(15), 1, 12); + // The ruled claim sits clear of the boundary on both sides, so a softened or stiffened detent is red. + Assert.Equal(0, HeldCrossing(8)); + Assert.InRange(HeldCrossing(20), 1, 10); + } + + [Fact] + public void ACrestAtTheBandsMidpointSplitsATwoNotchPushBothWays() + { + // One notch into the band stays in flight; the commit lands only past the crest, not after a fixed few notches. + var view = Make(); + ToFlightEdge(view); + view.Scroll(1); + Assert.False(view.Tactical); + Assert.True(view.Commanded > MathF.Log(Start)); + Assert.True(view.Commanded < (MathF.Log(Start) + MathF.Log(End)) / 2); + Assert.Equal(MathF.Log(Start) + LnStep * Resistance, view.Commanded, 3); + } + + // Eased from rest, critically damped: ln d(t) = c - offset (1 + wt) e^(-wt), w = 2 / EaseTime. + [Fact] + public void TheEaseIsACriticallyDampedSpringNotASnap() + { + var view = Make(); + var lnBase = MathF.Log(BaseDistance); + view.Scroll(2); + Assert.Equal(BaseDistance, view.Distance, 4); + view.Tick(.05f); + var w = 2 / Ease; + var expected = lnBase + 2 * LnStep * (1 - (1 + w * .05f) * MathF.Exp(-w * .05f)); + Assert.Equal(MathF.Exp(expected), view.Distance, 3); + Assert.True(view.Distance < BaseDistance * 1.25f * 1.25f); + // The same step in two halves lands at the same place: the solution is exact at any dt. + var halves = Make(); + halves.Scroll(2); + halves.Tick(.025f); + halves.Tick(.025f); + Assert.Equal(view.Distance, halves.Distance, 3); + } + + [Fact] + public void ResetClearsTheEasedPositionAndTheVelocity() + { + var view = Make(); + view.Scroll(4); + view.Tick(.05f); + Assert.NotEqual(BaseDistance, view.Distance); + view.Reset(Radius, BaseDistance, new float2(0, 0)); + Assert.Equal(BaseDistance, view.Distance, 4); + Assert.Equal(MathF.Log(BaseDistance), view.Commanded, 5); + view.Tick(.1f); + Assert.Equal(BaseDistance, view.Distance, 4); + } + + [Fact] + public void ANonPositiveTickChangesNothing() + { + var view = Make(); + ToFlightEdge(view); + view.Scroll(1); + view.Tick(.02f); + var commanded = view.Commanded; + var distance = view.Distance; + view.Tick(0); + view.Tick(-1); + Assert.Equal(commanded, view.Commanded, 6); + Assert.Equal(distance, view.Distance, 4); + } + + // The ruling: everything follows the eased distance. Flicking back from a panned tactical view must not move the + // camera's target by the pan offset in one frame; it returns to the ship with the rig's weight. + [Fact] + public void TargetReturnsToTheShipWithTheEasedCameraNotTheCommandedFlip() + { + var view = Make(); + var ship = new float2(100, -50); + ToFlightEdge(view); + Cross(view); + Settle(view); + view.Pan(new float2(12000, 0)); + view.Track(ship); + var panned = view.Target; + Assert.Equal(12100f, panned.x, 2); + view.Track(ship); + Assert.Equal(panned, view.Target); + + var last = view.Target; + var worst = 0f; + // A fast flick back at 20 notches a second, stepped at 5 ms frames, then settled: no frame moves it far. + for (var i = 0; i < 40 && view.Tactical; i++) + { + for (var frame = 0; frame < 10; frame++) + { + view.Tick(.005f); + view.Track(ship); + worst = MathF.Max(worst, MathF.Abs(view.Target.x - last.x)); + last = view.Target; + } + view.Scroll(-1); + worst = MathF.Max(worst, MathF.Abs(view.Target.x - last.x)); + last = view.Target; + } + Assert.False(view.Tactical); + for (var i = 0; i < 600; i++) + { + view.Tick(.005f); + view.Track(ship); + var step = MathF.Abs(view.Target.x - last.x); + worst = MathF.Max(worst, step); + last = view.Target; + } + Assert.True(worst < 12000 * .1f, "target stepped " + worst); + Assert.Equal(ship, view.Target); + } + + [Fact] + public void ThePanStartsFromTheShipOnEntry() + { + var view = Make(); + ToFlightEdge(view); + view.Track(new float2(500, 500)); + Cross(view); + Settle(view); + Assert.Equal(new float2(500, 500), view.Target); + } }