Skip to content
0.2.0-dev Download ZIP
90 lines ·4.1 KB Raw
1using System.Reflection;
2using System.Text;
3using EvilEngine.Formats;
4using EvilEngine.Formats.Maps;
5using EvilEngine.Host.Collision;
6using EvilEngine.Host.Liquids;
7using EvilEngine.Host.Movement;
8using EvilEngine.Conversion;
9
10namespace EvilEngine.Host.Tests;
11
12public sealed class GmConstructSwimmingTests(ITestOutputHelper output)
13{
14 private const double Inch = 0.0254;
15 private const double Tick = 0.015;
16
17 private static readonly string Installed = typeof(GmConstructSwimmingTests).Assembly
18 .GetCustomAttributes<AssemblyMetadataAttribute>()
19 .Single(attribute => attribute.Key == "AdapterOutputRoot").Value!;
20
21 private static (HullSpace Hulls, LiquidSpace Liquids) Map()
22 {
23 var path = Environment.GetEnvironmentVariable("EVIL_GMOD");
24 Assert.SkipWhen(path is null, "Set EVIL_GMOD to a Garry's Mod install to swim in gm_construct.");
25 var host = new EngineHost();
26 host.Adapters.LoadAll([Installed]);
27 host.Conversion.AddNamespace(ContentNamespace.For(host.Mounts.Open(Assert.Single(host.Mounts.Probe(path!)))));
28 var result = host.Conversion.Get(ContentKind.Cell, ContentId.Parse("gmod:maps/gm_construct/cells/0"));
29 Assert.True(result.Succeeded, string.Join('\n', result.Diagnostics));
30 var cell = MapText.ReadCell(Encoding.UTF8.GetString(result.Content!.Data.Span), "cell");
31 var hulls = HullSpace.Build(MapColliders.Build([cell], host.Conversion.Get, (_, _) => { }),
32 HullSpace.BlocksPlayers);
33 return (hulls, LiquidSpace.Build([cell], _ => null, _ => { }));
34 }
35
36 private static MapVector Source(double x, double y, double z)
37 {
38 return new MapVector(y * Inch, z * Inch, x * Inch);
39 }
40
41 private static void Run(MovementController player, MoveCommand command, int ticks, HullSpace hulls,
42 LiquidSpace liquids, Action? each = null)
43 {
44 for (var tick = 0; tick < ticks; tick++)
45 {
46 player.Step(command, Tick, hulls, liquids);
47 each?.Invoke();
48 }
49 }
50
51 [Fact]
52 public void SwimsBobsAndWadesInGmConstructsOwnWater()
53 {
54 var (hulls, liquids) = Map();
55 output.WriteLine($"{liquids.Bodies.Count} liquid bodies, surfaces at " +
56 string.Join(", ", liquids.Surfaces.Select(surface => $"{surface.Height / Inch:0.##}")));
57
58 var sea = new MovementController(MovementRules.Standard, Source(768, 4740, -241));
59 Run(sea, new MoveCommand(1, 0, 0, 0, 315, MoveButtons.None), 200, hulls, liquids);
60 Assert.Equal(LiquidLevel.Eyes, sea.WaterLevel);
61 Assert.Equal(4.064, Math.Sqrt(sea.Velocity.X * sea.Velocity.X + sea.Velocity.Z * sea.Velocity.Z), 3);
62
63 var levels = new HashSet<LiquidLevel>();
64 var (low, high) = (double.MaxValue, double.MinValue);
65 Run(sea, new MoveCommand(0, 0, 0, 0, 315, MoveButtons.Jump), 300, hulls, liquids);
66 Run(sea, new MoveCommand(0, 0, 0, 0, 315, MoveButtons.Jump), 270, hulls, liquids, () =>
67 {
68 levels.Add(sea.WaterLevel);
69 (low, high) = (Math.Min(low, sea.Origin.Y), Math.Max(high, sea.Origin.Y));
70 });
71 output.WriteLine($"bob: feet {low / Inch:0.##} to {high / Inch:0.##} units, levels {string.Join(", ", levels)}");
72 Assert.Equal([LiquidLevel.Feet, LiquidLevel.Waist], levels.Order());
73 Assert.InRange(low / Inch, -198, -196);
74 Assert.InRange(high / Inch, -186, -183.5);
75
76 var shore = new MovementController(MovementRules.Standard, Source(-2734.33, 5081.35, -162));
77 var deepest = LiquidLevel.None;
78 Run(shore, new MoveCommand(1, 0, 0, 0, 292.5 + 180, MoveButtons.None), 600, hulls, liquids, () =>
79 {
80 if (shore.WaterLevel <= deepest)
81 return;
82 deepest = shore.WaterLevel;
83 output.WriteLine($"level {deepest} at {shore.Origin}");
84 });
85 var speed = Math.Sqrt(shore.Velocity.X * shore.Velocity.X + shore.Velocity.Z * shore.Velocity.Z);
86 output.WriteLine($"wading at {shore.WaterLevel}, {speed / Inch:0.##} u/s");
87 Assert.True(deepest >= LiquidLevel.Waist, $"walked in only to {deepest}");
88 Assert.Equal(160 * Inch, speed, 2);
89 }
90}