0.2.0-dev Download ZIP
183 lines ·8.6 KB Raw
| 1 | using System.Numerics; |
| 2 | using System.Reflection; |
| 3 | using System.Text; |
| 4 | using EvilEngine.Conversion; |
| 5 | using EvilEngine.Formats; |
| 6 | using EvilEngine.Formats.Collision; |
| 7 | using EvilEngine.Formats.Maps; |
| 8 | using EvilEngine.Formats.Models; |
| 9 | using EvilEngine.Host.Collision; |
| 10 | using EvilEngine.Host.Movement; |
| 11 | |
| 12 | namespace EvilEngine.Host.Tests; |
| 13 | |
| 14 | public sealed class SourceCollisionTests(ITestOutputHelper output) |
| 15 | { |
| 16 | private static readonly string Installed = typeof(SourceCollisionTests).Assembly |
| 17 | .GetCustomAttributes<AssemblyMetadataAttribute>() |
| 18 | .Single(attribute => attribute.Key == "AdapterOutputRoot").Value!; |
| 19 | |
| 20 | private static EngineHost Open() |
| 21 | { |
| 22 | var path = Environment.GetEnvironmentVariable("EVIL_GMOD"); |
| 23 | Assert.SkipWhen(path is null, "Set EVIL_GMOD to a Garry's Mod install to convert its collision."); |
| 24 | var host = new EngineHost(); |
| 25 | host.Adapters.LoadAll([Installed]); |
| 26 | host.Conversion.AddNamespace(ContentNamespace.For(host.Mounts.Open(Assert.Single(host.Mounts.Probe(path!))))); |
| 27 | return host; |
| 28 | } |
| 29 | |
| 30 | private static ReadOnlyMemory<byte> Convert(EngineHost host, ContentKind kind, string id) |
| 31 | { |
| 32 | var result = host.Conversion.Get(kind, ContentId.Parse(id)); |
| 33 | Assert.True(result.Succeeded, string.Join('\n', result.Diagnostics)); |
| 34 | return result.Content!.Data; |
| 35 | } |
| 36 | |
| 37 | private static (Vector3 Min, Vector3 Max) Bounds(IEnumerable<Vector3> points) |
| 38 | { |
| 39 | var all = points.ToArray(); |
| 40 | return (all.Aggregate(new Vector3(float.MaxValue), Vector3.Min), |
| 41 | all.Aggregate(new Vector3(float.MinValue), Vector3.Max)); |
| 42 | } |
| 43 | |
| 44 | private static IEnumerable<Vector3> Drawn(Model model) |
| 45 | { |
| 46 | return model.Meshes.SelectMany(mesh => mesh.Primitives).SelectMany(primitive => primitive.Vertices.Positions); |
| 47 | } |
| 48 | |
| 49 | [Theory] |
| 50 | [InlineData("gmod:models/props_c17/oildrum001", 1, 0.02f)] |
| 51 | [InlineData("gmod:models/alyx", 15, 0.03f)] |
| 52 | public void PutsAModelsPhyHullsInsideWhatItDraws(string model, int hulls, float slack) |
| 53 | { |
| 54 | var host = Open(); |
| 55 | var collision = CollisionFile.Read(Convert(host, ContentKind.Collision, $"{model}/collision"), model); |
| 56 | var drawn = Bounds(Drawn(ModelFile.Read(Convert(host, ContentKind.Model, model), model))); |
| 57 | |
| 58 | Assert.Equal(hulls, collision.Colliders.Count); |
| 59 | var solid = Bounds(collision.Colliders.SelectMany(collider => |
| 60 | Assert.IsType<ColliderShape.Hull>(collider.Shape).Points)); |
| 61 | output.WriteLine($"{model}: collision {solid.Min} {solid.Max}, drawn {drawn.Min} {drawn.Max}"); |
| 62 | Assert.True(Vector3.Min(solid.Min - drawn.Min + new Vector3(slack), Vector3.Zero) == Vector3.Zero); |
| 63 | Assert.True(Vector3.Max(solid.Max - drawn.Max - new Vector3(slack), Vector3.Zero) == Vector3.Zero); |
| 64 | Assert.InRange(solid.Max.Y - solid.Min.Y, (drawn.Max.Y - drawn.Min.Y) * 0.9f, |
| 65 | drawn.Max.Y - drawn.Min.Y + slack); |
| 66 | } |
| 67 | |
| 68 | [Theory] |
| 69 | [InlineData("gmod:models/props_c17/oildrum001", 30, 27002.146484)] |
| 70 | [InlineData("gmod:models/props_junk/watermelon01", 10, 1962.163574)] |
| 71 | [InlineData("gmod:models/props_junk/wood_crate001a", 30, 62976.679688)] |
| 72 | public void WeighsAPropByItsPhyAndMeasuresTheVolumeItsPhySays(string model, double mass, double cubicInches) |
| 73 | { |
| 74 | var host = Open(); |
| 75 | var collision = CollisionFile.Read(Convert(host, ContentKind.Collision, $"{model}/collision"), model); |
| 76 | var shape = Physics.BodyShape.Of(collision); |
| 77 | |
| 78 | Assert.Equal(mass, collision.Motion?.Mass); |
| 79 | Assert.True(shape.Convex); |
| 80 | output.WriteLine($"{model}: {shape.Volume:0.0000} m³ against the .phy's {cubicInches * 1.6387064e-5:0.0000}"); |
| 81 | Assert.Equal(cubicInches * Math.Pow(0.0254, 3), shape.Volume, cubicInches * Math.Pow(0.0254, 3) * 0.02); |
| 82 | Assert.Null(CollisionFile.Read(Convert(host, ContentKind.Collision, "gmod:models/alyx/collision"), "alyx") |
| 83 | .Motion); |
| 84 | } |
| 85 | |
| 86 | [Fact] |
| 87 | public void GivesAModelWithoutAPhyNoCollision() |
| 88 | { |
| 89 | var host = Open(); |
| 90 | var collision = |
| 91 | CollisionFile.Read(Convert(host, ContentKind.Collision, "gmod:models/editor/playerstart/collision"), |
| 92 | "playerstart"); |
| 93 | |
| 94 | Assert.Empty(collision.Colliders); |
| 95 | } |
| 96 | |
| 97 | [Fact] |
| 98 | public void LaysGmConstructsDisplacementsOverTheTrianglesItDraws() |
| 99 | { |
| 100 | var host = Open(); |
| 101 | var collision = CollisionFile.Read(Convert(host, ContentKind.Collision, "gmod:maps/gm_construct/displacements"), |
| 102 | "displacements"); |
| 103 | var world = ModelFile.Read(Convert(host, ContentKind.Model, "gmod:maps/gm_construct/models/0"), "world"); |
| 104 | var drawn = Drawn(world) |
| 105 | .Select(point => (MathF.Round(point.X, 2), MathF.Round(point.Y, 2), MathF.Round(point.Z, 2))) |
| 106 | .ToHashSet(); |
| 107 | |
| 108 | var mesh = Assert.IsType<ColliderShape.Mesh>(Assert.Single(collision.Colliders).Shape); |
| 109 | var points = mesh.Parts.SelectMany(part => part.Positions).ToArray(); |
| 110 | var on = points.Count(point => |
| 111 | drawn.Contains((MathF.Round(point.X, 2), MathF.Round(point.Y, 2), MathF.Round(point.Z, 2)))); |
| 112 | output.WriteLine($"{points.Length} corners, {on} on drawn vertices, {mesh.Parts.Count} surfaces"); |
| 113 | Assert.True(on >= points.Length * 0.998); |
| 114 | Assert.Contains(mesh.Parts, part => part.Surface.Surface == ContentId.Parse("gmod:surfaces/grass")); |
| 115 | } |
| 116 | |
| 117 | [Fact] |
| 118 | public void BuildsGmConstructsCollidersFromItsSolidsDisplacementsAndProps() |
| 119 | { |
| 120 | var host = Open(); |
| 121 | var cell = MapText.ReadCell( |
| 122 | Encoding.UTF8.GetString(Convert(host, ContentKind.Cell, "gmod:maps/gm_construct/cells/0").Span), "cell"); |
| 123 | var lines = new List<string>(); |
| 124 | |
| 125 | var set = MapColliders.Build([cell], host.Conversion.Get, (_, text) => lines.Add(text)); |
| 126 | |
| 127 | foreach (var line in lines) |
| 128 | output.WriteLine(line); |
| 129 | Assert.Contains(cell.Solids, solid => solid.Contents.SequenceEqual([SolidContents.VehicleClip])); |
| 130 | Assert.DoesNotContain(cell.Models, place => place.Model.Name.EndsWith("models/0") && place.Collides); |
| 131 | Assert.InRange(set.Count(ColliderOrigin.Solid), 2000, 2117); |
| 132 | Assert.Contains(set.Colliders, |
| 133 | collider => collider.From == ContentId.Parse("gmod:maps/gm_construct/displacements")); |
| 134 | Assert.Contains(set.Colliders, |
| 135 | collider => collider.From == ContentId.Parse("gmod:models/props_c17/furniturecouch001a/collision")); |
| 136 | Assert.Equal(0, set.Count(ColliderOrigin.Drawn)); |
| 137 | Assert.Single(lines); |
| 138 | } |
| 139 | |
| 140 | [Fact] |
| 141 | public void StandsAndWalksAPlayerFromEveryGmConstructSpawnPoint() |
| 142 | { |
| 143 | var host = Open(); |
| 144 | var cell = MapText.ReadCell( |
| 145 | Encoding.UTF8.GetString(Convert(host, ContentKind.Cell, "gmod:maps/gm_construct/cells/0").Span), "cell"); |
| 146 | var space = HullSpace.Build(MapColliders.Build([cell], host.Conversion.Get, (_, _) => { }), |
| 147 | HullSpace.BlocksPlayers); |
| 148 | output.WriteLine($"{space.Brushes} brushes"); |
| 149 | |
| 150 | foreach (var spawn in cell.Spawns) |
| 151 | { |
| 152 | var at = spawn.Transform.Position; |
| 153 | var player = new MovementController(MovementRules.Standard, new MapVector(at.X, at.Y + 0.05, at.Z)); |
| 154 | for (var tick = 0; tick < 133; tick++) |
| 155 | player.Step(new MoveCommand(0, 0, 0, 0, spawn.Transform.Angles.Y, MoveButtons.None), 0.015, space); |
| 156 | Assert.True(player.OnGround, $"spawn {spawn.Owner} at {at} didn't land: now {player.Origin}"); |
| 157 | Assert.InRange(player.Origin.Y, at.Y - 0.5, at.Y + 0.1); |
| 158 | for (var tick = 0; tick < 200; tick++) |
| 159 | player.Step(new MoveCommand(1, 0.3, 0, 0, spawn.Transform.Angles.Y + tick * 0.5, MoveButtons.Run), |
| 160 | 0.015, space); |
| 161 | Assert.True(player.Origin.Y > at.Y - 3, $"spawn {spawn.Owner} fell from {at} to {player.Origin}"); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | [Fact] |
| 166 | public void WalksStraightOntoGmConstructsDisplacementFloorWithoutStalling() |
| 167 | { |
| 168 | var host = Open(); |
| 169 | var cell = MapText.ReadCell( |
| 170 | Encoding.UTF8.GetString(Convert(host, ContentKind.Cell, "gmod:maps/gm_construct/cells/0").Span), "cell"); |
| 171 | var space = HullSpace.Build(MapColliders.Build([cell], host.Conversion.Get, (_, _) => { }), |
| 172 | HullSpace.BlocksPlayers); |
| 173 | var player = new MovementController(MovementRules.Standard, new MapVector(-8.932088129, -3.6575, 24.1808)); |
| 174 | for (var tick = 0; tick < 60; tick++) |
| 175 | player.Step(new MoveCommand(0, 0, 0, 0, 0, MoveButtons.None), 0.015, space); |
| 176 | |
| 177 | for (var tick = 0; tick < 200; tick++) |
| 178 | player.Step(new MoveCommand(1, 0, 0, 0, 0, MoveButtons.None), 0.015, space); |
| 179 | |
| 180 | Assert.True(player.OnGround); |
| 181 | Assert.True(player.Origin.Z < 10, $"stalled at {player.Origin}"); |
| 182 | } |
| 183 | } |