using System.Reflection; using System.Text; using EvilEngine.Formats; using EvilEngine.Formats.Maps; using EvilEngine.Host.Collision; using EvilEngine.Host.Liquids; using EvilEngine.Host.Movement; using EvilEngine.Conversion; namespace EvilEngine.Host.Tests; public sealed class GmConstructSwimmingTests(ITestOutputHelper output) { private const double Inch = 0.0254; private const double Tick = 0.015; private static readonly string Installed = typeof(GmConstructSwimmingTests).Assembly .GetCustomAttributes() .Single(attribute => attribute.Key == "AdapterOutputRoot").Value!; private static (HullSpace Hulls, LiquidSpace Liquids) Map() { var path = Environment.GetEnvironmentVariable("EVIL_GMOD"); Assert.SkipWhen(path is null, "Set EVIL_GMOD to a Garry's Mod install to swim in gm_construct."); var host = new EngineHost(); host.Adapters.LoadAll([Installed]); host.Conversion.AddNamespace(ContentNamespace.For(host.Mounts.Open(Assert.Single(host.Mounts.Probe(path!))))); var result = host.Conversion.Get(ContentKind.Cell, ContentId.Parse("gmod:maps/gm_construct/cells/0")); Assert.True(result.Succeeded, string.Join('\n', result.Diagnostics)); var cell = MapText.ReadCell(Encoding.UTF8.GetString(result.Content!.Data.Span), "cell"); var hulls = HullSpace.Build(MapColliders.Build([cell], host.Conversion.Get, (_, _) => { }), HullSpace.BlocksPlayers); return (hulls, LiquidSpace.Build([cell], _ => null, _ => { })); } private static MapVector Source(double x, double y, double z) { return new MapVector(y * Inch, z * Inch, x * Inch); } private static void Run(MovementController player, MoveCommand command, int ticks, HullSpace hulls, LiquidSpace liquids, Action? each = null) { for (var tick = 0; tick < ticks; tick++) { player.Step(command, Tick, hulls, liquids); each?.Invoke(); } } [Fact] public void SwimsBobsAndWadesInGmConstructsOwnWater() { var (hulls, liquids) = Map(); output.WriteLine($"{liquids.Bodies.Count} liquid bodies, surfaces at " + string.Join(", ", liquids.Surfaces.Select(surface => $"{surface.Height / Inch:0.##}"))); var sea = new MovementController(MovementRules.Standard, Source(768, 4740, -241)); Run(sea, new MoveCommand(1, 0, 0, 0, 315, MoveButtons.None), 200, hulls, liquids); Assert.Equal(LiquidLevel.Eyes, sea.WaterLevel); Assert.Equal(4.064, Math.Sqrt(sea.Velocity.X * sea.Velocity.X + sea.Velocity.Z * sea.Velocity.Z), 3); var levels = new HashSet(); var (low, high) = (double.MaxValue, double.MinValue); Run(sea, new MoveCommand(0, 0, 0, 0, 315, MoveButtons.Jump), 300, hulls, liquids); Run(sea, new MoveCommand(0, 0, 0, 0, 315, MoveButtons.Jump), 270, hulls, liquids, () => { levels.Add(sea.WaterLevel); (low, high) = (Math.Min(low, sea.Origin.Y), Math.Max(high, sea.Origin.Y)); }); output.WriteLine($"bob: feet {low / Inch:0.##} to {high / Inch:0.##} units, levels {string.Join(", ", levels)}"); Assert.Equal([LiquidLevel.Feet, LiquidLevel.Waist], levels.Order()); Assert.InRange(low / Inch, -198, -196); Assert.InRange(high / Inch, -186, -183.5); var shore = new MovementController(MovementRules.Standard, Source(-2734.33, 5081.35, -162)); var deepest = LiquidLevel.None; Run(shore, new MoveCommand(1, 0, 0, 0, 292.5 + 180, MoveButtons.None), 600, hulls, liquids, () => { if (shore.WaterLevel <= deepest) return; deepest = shore.WaterLevel; output.WriteLine($"level {deepest} at {shore.Origin}"); }); var speed = Math.Sqrt(shore.Velocity.X * shore.Velocity.X + shore.Velocity.Z * shore.Velocity.Z); output.WriteLine($"wading at {shore.WaterLevel}, {speed / Inch:0.##} u/s"); Assert.True(deepest >= LiquidLevel.Waist, $"walked in only to {deepest}"); Assert.Equal(160 * Inch, speed, 2); } }