using System.Numerics; using EvilEngine.Conversion; using EvilEngine.Formats; using EvilEngine.Formats.Collision; using EvilEngine.Formats.Maps; using EvilEngine.Formats.Models; using EvilEngine.Host.Collision; namespace EvilEngine.Host.Tests; public sealed class MapCollidersTests { private static readonly ContentId Material = ContentId.Parse("evil:dev/floor"); private static readonly ContentId Drum = ContentId.Parse("gmod:models/props_c17/oildrum001"); private static readonly ContentId Crate = ContentId.Parse("hpl3:static_objects/crate"); private static Solid Box(MapVector min, MapVector max, params string[] contents) { SolidSide[] sides = [ new(new MapPlane(new MapVector(1, 0, 0), max.X), Material, null), new(new MapPlane(new MapVector(-1, 0, 0), -min.X), Material, null), new(new MapPlane(new MapVector(0, 1, 0), max.Y), Material, null), new(new MapPlane(new MapVector(0, -1, 0), -min.Y), Material, null), new(new MapPlane(new MapVector(0, 0, 1), max.Z), Material, null), new(new MapPlane(new MapVector(0, 0, -1), -min.Z), Material, null) ]; return new Solid(null, contents.Length == 0 ? SolidContents.Default : contents, sides); } private static ConversionResult Content(ContentKind kind, ContentId id, byte[] data) { return new ConversionResult(kind, id, ConversionStatus.Converted) { Content = new ConvertedContent(kind, id, data, "hash") { Converter = "test", ConverterVersion = 1, Bindings = "test@1" } }; } private static ConversionResult Get(ContentKind kind, ContentId id) { if (kind == ContentKind.Collision && id == MapColliders.Own(Drum)) return Content(kind, id, CollisionFile.Write(new CollisionContent([ new Collider(new ColliderShape.Box(new Vector3(0.5f, 1, 0.5f)), ["solid"]) { Position = new Vector3(0, 0.5f, 0) } ]))); if (kind == ContentKind.Model && id == Crate) return Content(kind, id, ModelFile.Write(new Model( [new ModelNode("crate", [], new Vector3(0, 1, 0), Quaternion.Identity, Vector3.One, 0)], [0], [ new ModelMesh(null, [ new ModelPrimitive(null, [0, 1, 2], new ModelVertices([Vector3.Zero, Vector3.UnitX, Vector3.UnitZ]), [], []) ], [], []) ], [], [], [], [], [], [], ModelExtras.Empty))); return new ConversionResult(kind, id, id.Namespace == "hpl3" ? ConversionStatus.NoConverter : ConversionStatus.Failed); } private static MapColliderSet Build(MapCell cell) { return MapColliders.Build([cell], Get, (_, _) => { }); } [Fact] public void TurnsASolidIntoTheHullOfItsCorners() { var hull = MapColliders.Hull(Box(new MapVector(-1, 0, -2), new MapVector(1, 0.5, 2))); Assert.NotNull(hull); Assert.Equal(8, hull.Points.Length); Assert.Equal(6, hull.Faces.Count); Assert.Contains(new MapVector(1, 0.5, -2), hull.Points); Assert.All(hull.Faces, face => Assert.Equal(Material, face.Surface.Material)); } [Fact] public void LeavesOutSidesThatDontTouchTheSolid() { var box = Box(new MapVector(0, 0, 0), new MapVector(1, 1, 1)); var extra = box with { Sides = [.. box.Sides, new SolidSide(new MapPlane(new MapVector(0, 1, 0), 5), Material, null)] }; Assert.Equal(6, MapColliders.Hull(extra)!.Faces.Count); } [Fact] public void BuildsOnlySolidsThatBlockSomething() { var cell = new MapCell([ Box(new MapVector(0, 0, 0), new MapVector(1, 1, 1)), Box(new MapVector(0, 0, 0), new MapVector(1, 1, 1), SolidContents.PlayerClip), Box(new MapVector(0, 0, 0), new MapVector(1, 1, 1), SolidContents.Trigger), Box(new MapVector(0, 0, 0), new MapVector(1, 1, 1), SolidContents.Water, SolidContents.Translucent), Box(new MapVector(0, 0, 0), new MapVector(1, 1, 1), SolidContents.Ladder) ], [], [], [], []); var set = Build(cell); Assert.Equal(2, set.Count(ColliderOrigin.Solid)); Assert.Equal([SolidContents.PlayerClip], set.Colliders[1].Contents); } [Fact] public void PlacesAModelsOwnCollisionWhereTheModelStands() { var cell = new MapCell([], [], [ new ModelPlacement("7", Drum, new MapTransform(new MapVector(10, 0, 0), new MapVector(0, 90, 0))) ], [], []); var collider = Assert.Single(Build(cell).Colliders); Assert.Equal(ColliderOrigin.Placed, collider.Origin); Assert.Equal("7", collider.Owner); var box = Assert.IsType(collider.Shape); Assert.Equal(10, box.Position.X, 6); Assert.Equal(0.5, box.Position.Y, 6); } [Fact] public void FallsBackToTheTrianglesAModelDrawsWhenItsGameHasNoCollision() { var cell = new MapCell([], [], [ new ModelPlacement(null, Crate, new MapTransform(new MapVector(0, 2, 0), MapVector.Zero) { Scale = new MapVector(2, 2, 2) }) ], [], []); var collider = Assert.Single(Build(cell).Colliders); Assert.Equal(ColliderOrigin.Drawn, collider.Origin); var triangles = Assert.IsType(collider.Shape); Assert.Equal(new MapVector(2, 4, 0), triangles.Transform.Apply(triangles.Mesh.Positions[1])); } [Fact] public void LeavesOutPlacementsThatDontCollideTheSkyboxAndCollisionThatFailed() { var problems = new List(); var cell = new MapCell([], [], [ new ModelPlacement(null, Drum, MapTransform.Identity) { Collides = false }, new ModelPlacement(null, Drum, MapTransform.Identity) { Skybox = true }, new ModelPlacement(null, ContentId.Parse("gmod:models/broken"), MapTransform.Identity) ], [], []); var set = MapColliders.Build([cell], Get, (_, text) => problems.Add(text)); Assert.Empty(set.Colliders); Assert.Contains(problems, text => text.Contains("gmod:models/broken/collision")); } }