Skip to content
0.2.0-dev Download ZIP
154 lines ·6.2 KB Raw
1using System.Numerics;
2using EvilEngine.Conversion;
3using EvilEngine.Formats;
4using EvilEngine.Formats.Collision;
5using EvilEngine.Formats.Maps;
6using EvilEngine.Formats.Models;
7using EvilEngine.Host.Collision;
8
9namespace EvilEngine.Host.Tests;
10
11public sealed class MapCollidersTests
12{
13 private static readonly ContentId Material = ContentId.Parse("evil:dev/floor");
14 private static readonly ContentId Drum = ContentId.Parse("gmod:models/props_c17/oildrum001");
15 private static readonly ContentId Crate = ContentId.Parse("hpl3:static_objects/crate");
16
17 private static Solid Box(MapVector min, MapVector max, params string[] contents)
18 {
19 SolidSide[] sides =
20 [
21 new(new MapPlane(new MapVector(1, 0, 0), max.X), Material, null),
22 new(new MapPlane(new MapVector(-1, 0, 0), -min.X), Material, null),
23 new(new MapPlane(new MapVector(0, 1, 0), max.Y), Material, null),
24 new(new MapPlane(new MapVector(0, -1, 0), -min.Y), Material, null),
25 new(new MapPlane(new MapVector(0, 0, 1), max.Z), Material, null),
26 new(new MapPlane(new MapVector(0, 0, -1), -min.Z), Material, null)
27 ];
28 return new Solid(null, contents.Length == 0 ? SolidContents.Default : contents, sides);
29 }
30
31 private static ConversionResult Content(ContentKind kind, ContentId id, byte[] data)
32 {
33 return new ConversionResult(kind, id, ConversionStatus.Converted)
34 {
35 Content = new ConvertedContent(kind, id, data, "hash")
36 { Converter = "test", ConverterVersion = 1, Bindings = "test@1" }
37 };
38 }
39
40 private static ConversionResult Get(ContentKind kind, ContentId id)
41 {
42 if (kind == ContentKind.Collision && id == MapColliders.Own(Drum))
43 return Content(kind, id, CollisionFile.Write(new CollisionContent([
44 new Collider(new ColliderShape.Box(new Vector3(0.5f, 1, 0.5f)), ["solid"])
45 { Position = new Vector3(0, 0.5f, 0) }
46 ])));
47 if (kind == ContentKind.Model && id == Crate)
48 return Content(kind, id, ModelFile.Write(new Model(
49 [new ModelNode("crate", [], new Vector3(0, 1, 0), Quaternion.Identity, Vector3.One, 0)], [0],
50 [
51 new ModelMesh(null,
52 [
53 new ModelPrimitive(null, [0, 1, 2],
54 new ModelVertices([Vector3.Zero, Vector3.UnitX, Vector3.UnitZ]), [], [])
55 ], [], [])
56 ],
57 [], [], [], [], [], [], ModelExtras.Empty)));
58 return new ConversionResult(kind, id,
59 id.Namespace == "hpl3" ? ConversionStatus.NoConverter : ConversionStatus.Failed);
60 }
61
62 private static MapColliderSet Build(MapCell cell)
63 {
64 return MapColliders.Build([cell], Get, (_, _) => { });
65 }
66
67 [Fact]
68 public void TurnsASolidIntoTheHullOfItsCorners()
69 {
70 var hull = MapColliders.Hull(Box(new MapVector(-1, 0, -2), new MapVector(1, 0.5, 2)));
71
72 Assert.NotNull(hull);
73 Assert.Equal(8, hull.Points.Length);
74 Assert.Equal(6, hull.Faces.Count);
75 Assert.Contains(new MapVector(1, 0.5, -2), hull.Points);
76 Assert.All(hull.Faces, face => Assert.Equal(Material, face.Surface.Material));
77 }
78
79 [Fact]
80 public void LeavesOutSidesThatDontTouchTheSolid()
81 {
82 var box = Box(new MapVector(0, 0, 0), new MapVector(1, 1, 1));
83 var extra = box with
84 {
85 Sides = [.. box.Sides, new SolidSide(new MapPlane(new MapVector(0, 1, 0), 5), Material, null)]
86 };
87
88 Assert.Equal(6, MapColliders.Hull(extra)!.Faces.Count);
89 }
90
91 [Fact]
92 public void BuildsOnlySolidsThatBlockSomething()
93 {
94 var cell = new MapCell([
95 Box(new MapVector(0, 0, 0), new MapVector(1, 1, 1)),
96 Box(new MapVector(0, 0, 0), new MapVector(1, 1, 1), SolidContents.PlayerClip),
97 Box(new MapVector(0, 0, 0), new MapVector(1, 1, 1), SolidContents.Trigger),
98 Box(new MapVector(0, 0, 0), new MapVector(1, 1, 1), SolidContents.Water, SolidContents.Translucent),
99 Box(new MapVector(0, 0, 0), new MapVector(1, 1, 1), SolidContents.Ladder)
100 ], [], [], [], []);
101
102 var set = Build(cell);
103
104 Assert.Equal(2, set.Count(ColliderOrigin.Solid));
105 Assert.Equal([SolidContents.PlayerClip], set.Colliders[1].Contents);
106 }
107
108 [Fact]
109 public void PlacesAModelsOwnCollisionWhereTheModelStands()
110 {
111 var cell = new MapCell([], [], [
112 new ModelPlacement("7", Drum, new MapTransform(new MapVector(10, 0, 0), new MapVector(0, 90, 0)))
113 ], [], []);
114
115 var collider = Assert.Single(Build(cell).Colliders);
116
117 Assert.Equal(ColliderOrigin.Placed, collider.Origin);
118 Assert.Equal("7", collider.Owner);
119 var box = Assert.IsType<MapColliderShape.Primitive>(collider.Shape);
120 Assert.Equal(10, box.Position.X, 6);
121 Assert.Equal(0.5, box.Position.Y, 6);
122 }
123
124 [Fact]
125 public void FallsBackToTheTrianglesAModelDrawsWhenItsGameHasNoCollision()
126 {
127 var cell = new MapCell([], [], [
128 new ModelPlacement(null, Crate,
129 new MapTransform(new MapVector(0, 2, 0), MapVector.Zero) { Scale = new MapVector(2, 2, 2) })
130 ], [], []);
131
132 var collider = Assert.Single(Build(cell).Colliders);
133
134 Assert.Equal(ColliderOrigin.Drawn, collider.Origin);
135 var triangles = Assert.IsType<MapColliderShape.Triangles>(collider.Shape);
136 Assert.Equal(new MapVector(2, 4, 0), triangles.Transform.Apply(triangles.Mesh.Positions[1]));
137 }
138
139 [Fact]
140 public void LeavesOutPlacementsThatDontCollideTheSkyboxAndCollisionThatFailed()
141 {
142 var problems = new List<string>();
143 var cell = new MapCell([], [], [
144 new ModelPlacement(null, Drum, MapTransform.Identity) { Collides = false },
145 new ModelPlacement(null, Drum, MapTransform.Identity) { Skybox = true },
146 new ModelPlacement(null, ContentId.Parse("gmod:models/broken"), MapTransform.Identity)
147 ], [], []);
148
149 var set = MapColliders.Build([cell], Get, (_, text) => problems.Add(text));
150
151 Assert.Empty(set.Colliders);
152 Assert.Contains(problems, text => text.Contains("gmod:models/broken/collision"));
153 }
154}