using EvilEngine.Formats.Maps; using EvilEngine.Host.Physics; using EvilEngine.Host.Views; namespace EvilEngine.Host.Tests; public sealed class FlashlightTests { private static readonly MapVector Eye = new(0, 1.6, 0); private static void Near(MapVector expected, MapVector actual, double within = 1e-6) { Assert.True(Math.Abs(expected.X - actual.X) < within && Math.Abs(expected.Y - actual.Y) < within && Math.Abs(expected.Z - actual.Z) < within, $"expected {expected}, got {actual}"); } [Fact] public void ShinesFromAheadOfTheEyeAlongTheAimInTheOpen() { var beam = Flashlight.Place(Eye, 0, 0, new HullQueries(new HullBoxWorld())); Near(new MapVector(0, 1.6, -Flashlight.Ahead), beam.Origin); Near(new MapVector(0, 0, -1), beam.Direction); var up = Flashlight.Place(Eye, 90, 90, null); Near(new MapVector(0, 1.6 + Flashlight.Ahead, 0), up.Origin); Near(new MapVector(0, 1, 0), up.Direction); } [Fact] public void ANearWallPullsTheBeamInToTheEye() { var wall = new HullBoxWorld().Box(new MapVector(-5, -5, -1), new MapVector(5, 5, -0.1)); var beam = Flashlight.Place(Eye, 0, 0, new HullQueries(wall)); Assert.InRange(-beam.Origin.Z, 0, 0.1 - Flashlight.Probe + 1e-3); Assert.True(-beam.Origin.Z < Flashlight.Ahead); var inside = new HullBoxWorld().Box(new MapVector(-5, -5, -5), new MapVector(5, 5, 5)); Near(Eye, Flashlight.Place(Eye, 0, 0, new HullQueries(inside)).Origin); } [Fact] public void TheCookieIsBrightestInTheCentreWithARingAndADarkEdge() { Assert.Equal(1, Flashlight.Cookie(0)); Assert.Equal(1, Flashlight.Cookie(Flashlight.Core)); Assert.Equal(0, Flashlight.Cookie(1)); Assert.Equal(0, Flashlight.Cookie(1.5)); for (var r = 0.0; r < Flashlight.BodyStart; r += 0.01) Assert.True(Flashlight.Cookie(r + 0.01) <= Flashlight.Cookie(r) + 1e-12, $"rises at {r}"); Assert.True(Flashlight.Cookie(Flashlight.Ring) > Flashlight.Cookie(Flashlight.Ring - 3 * Flashlight.RingWidth)); Assert.True(Flashlight.Cookie(Flashlight.Ring) > Flashlight.Cookie(Flashlight.Ring + 2 * Flashlight.RingWidth)); for (var r = 0.0; r <= 1; r += 0.001) Assert.InRange(Flashlight.Cookie(r), 0, 1); } [Fact] public void TheCookieImageIsRoundAboutItsCentre() { const int size = Flashlight.CookieSize; Assert.Equal(Flashlight.Cookie(size / 2, size / 2), Flashlight.Cookie(size / 2 - 1, size / 2 - 1)); Assert.Equal(Flashlight.Cookie(10, size / 2), Flashlight.Cookie(size - 11, size / 2), 12); Assert.Equal(Flashlight.Cookie(size / 2, 10), Flashlight.Cookie(10, size / 2), 12); Assert.Equal(0, Flashlight.Cookie(0, 0)); } [Fact] public void TheFalloffIsTheShareOfTheRadiusItsEdgeFadesOver() { Assert.Equal(1, Flashlight.Cookie(0.49, 0.5, false)); Assert.Equal(1, Flashlight.Cookie(0.5, 0.5, false)); Assert.Equal(0.5, Flashlight.Cookie(0.75, 0.5, false), 12); Assert.Equal(0, Flashlight.Cookie(1, 0.5, false)); Assert.Equal(1, Flashlight.Cookie(0.999, 0, false)); Assert.Equal(Flashlight.Cookie(0.3, 1, false), Flashlight.Cookie(0.3, 2, false)); for (var r = 0.0; r < 1; r += 0.01) { Assert.True(Flashlight.Cookie(r + 0.01, 0.4, false) <= Flashlight.Cookie(r, 0.4, false) + 1e-12); Assert.InRange(Flashlight.Cookie(r, 0.4), 0, Flashlight.Cookie(r, 0.4, false)); } } [Fact] public void WithoutTheCookieTheBeamIsAFlatDisc() { for (var r = 0.0; r <= 1 - Flashlight.Falloff; r += 0.01) Assert.Equal(1, Flashlight.Cookie(r, shaped: false)); Assert.True(Flashlight.Cookie(Flashlight.BodyEnd, shaped: false) > Flashlight.Cookie(Flashlight.BodyEnd)); } }