0.2.0-dev Download ZIP
331 lines ·15.5 KB Raw
| 1 | using System.Diagnostics; |
| 2 | using System.Numerics; |
| 3 | using System.Reflection; |
| 4 | using System.Text; |
| 5 | using EvilEngine.Conversion; |
| 6 | using EvilEngine.Formats; |
| 7 | using EvilEngine.Formats.Maps; |
| 8 | using EvilEngine.Formats.Materials; |
| 9 | using EvilEngine.Formats.Models; |
| 10 | using EvilEngine.Formats.Records; |
| 11 | using EvilEngine.Formats.Textures; |
| 12 | using EvilEngine.Host.Liquids; |
| 13 | |
| 14 | namespace EvilEngine.Host.Tests; |
| 15 | |
| 16 | public sealed class QuakeMapTests(ITestOutputHelper output) |
| 17 | { |
| 18 | private static readonly string Installed = typeof(QuakeMapTests).Assembly |
| 19 | .GetCustomAttributes<AssemblyMetadataAttribute>() |
| 20 | .Single(attribute => attribute.Key == "AdapterOutputRoot").Value!; |
| 21 | |
| 22 | private static EngineHost Open() |
| 23 | { |
| 24 | var path = Environment.GetEnvironmentVariable("EVIL_QUAKE"); |
| 25 | Assert.SkipWhen(path is null, "Set EVIL_QUAKE to a Quake install to convert its maps."); |
| 26 | var host = new EngineHost(); |
| 27 | host.Adapters.LoadAll([Installed]); |
| 28 | foreach (var probe in host.Mounts.Probe(path!)) |
| 29 | host.Conversion.AddNamespace(ContentNamespace.For(host.Mounts.Open(probe))); |
| 30 | return host; |
| 31 | } |
| 32 | |
| 33 | private static ConvertedContent Convert(EngineHost host, ContentKind kind, string id) |
| 34 | { |
| 35 | var result = host.Conversion.Get(kind, ContentId.Parse(id)); |
| 36 | Assert.True(result.Succeeded, string.Join('\n', result.Diagnostics)); |
| 37 | return result.Content!; |
| 38 | } |
| 39 | |
| 40 | private static string Text(ConvertedContent content) |
| 41 | { |
| 42 | return Encoding.UTF8.GetString(content.Data.Span); |
| 43 | } |
| 44 | |
| 45 | private static IReadOnlyList<MapRecord> Records(EngineHost host, string game) |
| 46 | { |
| 47 | var set = RecordText.Read(game, Text(Convert(host, ContentKind.Record, $"{game}:records/maps")), game); |
| 48 | return set.Maps; |
| 49 | } |
| 50 | |
| 51 | private static bool Inside(Solid solid, MapVector point) |
| 52 | { |
| 53 | return solid.Sides.All(side => |
| 54 | side.Plane.Normal.X * point.X + side.Plane.Normal.Y * point.Y + side.Plane.Normal.Z * point.Z <= |
| 55 | side.Plane.Distance + 1e-6); |
| 56 | } |
| 57 | |
| 58 | [Fact] |
| 59 | public void NamesEachLiquidLeafsKindAsQuakesLiquidRecord() |
| 60 | { |
| 61 | var host = Open(); |
| 62 | var kinds = new SortedDictionary<string, int>(StringComparer.Ordinal); |
| 63 | foreach (var map in new[] { "e1m1", "e1m2", "e1m3", "e1m4", "e1m5", "e1m6", "e1m7", "e2m1", "e2m6", "start" }) |
| 64 | { |
| 65 | var cell = MapText.ReadCell(Text(Convert(host, ContentKind.Cell, $"quake:id1/maps/{map}/cells/0")), map); |
| 66 | foreach (var solid in cell.Solids) |
| 67 | { |
| 68 | if (SolidContents.Liquid(solid.Contents) is not { } contents) |
| 69 | { |
| 70 | Assert.Null(solid.Liquid); |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | var id = solid.Liquid!.Base!.Value; |
| 75 | Assert.True(solid.Liquid.OnlyBase); |
| 76 | kinds[$"{contents} {id}"] = kinds.GetValueOrDefault($"{contents} {id}") + 1; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | foreach (var (kind, count) in kinds) |
| 81 | output.WriteLine($"{kind}: {count}"); |
| 82 | Assert.Equal( |
| 83 | [ |
| 84 | "lava quake:liquids/lava", "slime quake:liquids/slime", "water quake:liquids/teleport", |
| 85 | "water quake:liquids/water" |
| 86 | ], |
| 87 | kinds.Keys); |
| 88 | foreach (var (kind, liquidClass) in new[] |
| 89 | { ("water", "water"), ("slime", "slime"), ("lava", "lava"), ("teleport", "water") }) |
| 90 | { |
| 91 | var record = Assert.Single(RecordText.Read("quake", |
| 92 | Text(Convert(host, ContentKind.Record, $"quake:liquids/{kind}")), kind).Liquids); |
| 93 | Assert.Equal(ContentId.Parse($"quake:liquids/{kind}"), record.Id); |
| 94 | Assert.Equal(new Liquid { Class = liquidClass }, record.Liquid); |
| 95 | } |
| 96 | |
| 97 | Assert.False(host.Conversion.Get(ContentKind.Record, ContentId.Parse("quake:liquids/honey")).Succeeded); |
| 98 | |
| 99 | var e1m1 = MapText.ReadCell(Text(Convert(host, ContentKind.Cell, "quake:id1/maps/e1m1/cells/0")), "e1m1"); |
| 100 | var records = new[] { "water", "slime", "lava", "teleport" }.ToDictionary( |
| 101 | kind => ContentId.Parse($"quake:liquids/{kind}"), |
| 102 | kind => Assert |
| 103 | .Single(RecordText.Read("quake", Text(Convert(host, ContentKind.Record, $"quake:liquids/{kind}")), kind) |
| 104 | .Liquids).Liquid); |
| 105 | var warnings = new List<string>(); |
| 106 | var space = LiquidSpace.Build([e1m1], records.GetValueOrDefault, warnings.Add); |
| 107 | output.WriteLine( |
| 108 | $"e1m1: {space.Bodies.Count} bodies, {space.Surfaces.Count} surfaces, {warnings.Count} warnings"); |
| 109 | Assert.Empty(warnings); |
| 110 | Assert.Equal(e1m1.Solids.Count(solid => SolidContents.Liquid(solid.Contents) is not null), space.Bodies.Count); |
| 111 | Assert.All(space.Bodies, body => |
| 112 | { |
| 113 | var centre = new MapVector((body.Min.X + body.Max.X) / 2, (body.Min.Y + body.Max.Y) / 2, |
| 114 | (body.Min.Z + body.Max.Z) / 2); |
| 115 | var hit = space.At(centre); |
| 116 | Assert.NotNull(hit); |
| 117 | Assert.True(hit.Depth >= 0); |
| 118 | }); |
| 119 | Assert.True(space.Surfaces.Count < space.Bodies.Count); |
| 120 | } |
| 121 | |
| 122 | private static (int Triangles, double Agreeing) Facing(Model model) |
| 123 | { |
| 124 | int triangles = 0, agreeing = 0; |
| 125 | foreach (var primitive in model.Meshes.SelectMany(mesh => mesh.Primitives)) |
| 126 | { |
| 127 | var p = primitive.Vertices.Positions; |
| 128 | var n = primitive.Vertices.Normals!; |
| 129 | for (var t = 0; t < primitive.Indices.Length; t += 3) |
| 130 | { |
| 131 | var (a, b, c) = (primitive.Indices[t], primitive.Indices[t + 1], primitive.Indices[t + 2]); |
| 132 | var face = Vector3.Cross(p[b] - p[a], p[c] - p[a]); |
| 133 | if (face.LengthSquared() < 1e-12f) |
| 134 | continue; |
| 135 | triangles++; |
| 136 | if (Vector3.Dot(face, n[a] + n[b] + n[c]) > 0) |
| 137 | agreeing++; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return (triangles, agreeing / (double)triangles); |
| 142 | } |
| 143 | |
| 144 | [Fact] |
| 145 | public void ListsEveryLevelOfBothEditionsWithTheGamesOwnTitles() |
| 146 | { |
| 147 | var host = Open(); |
| 148 | var timer = Stopwatch.StartNew(); |
| 149 | |
| 150 | var original = Records(host, "quake"); |
| 151 | var rerelease = Records(host, "quake-rerelease"); |
| 152 | |
| 153 | output.WriteLine($"listed {original.Count} and {rerelease.Count} maps in {timer.ElapsedMilliseconds} ms"); |
| 154 | Assert.Equal(73, original.Count); |
| 155 | Assert.Equal(171, rerelease.Count); |
| 156 | var e1m1 = Assert.Single(original, map => map.Id.ToString() == "quake:id1/e1m1"); |
| 157 | Assert.Equal(("quake:id1/maps/e1m1", "the Slipgate Complex", "id1"), |
| 158 | (e1m1.Map.ToString(), e1m1.Title, e1m1.Category)); |
| 159 | var map1 = Assert.Single(rerelease, map => map.Id.ToString() == "quake-rerelease:mg3/map1"); |
| 160 | Assert.Equal(("quake-rerelease:mg3/maps/map1", "Through the First Arch", "mg3"), |
| 161 | (map1.Map.ToString(), map1.Title, map1.Category)); |
| 162 | Assert.Contains(rerelease, map => map.Map.ToString() == "quake-rerelease:id1/maps/test/test_aiming"); |
| 163 | Assert.DoesNotContain(original.Concat(rerelease), |
| 164 | map => map.Map.Name.Split('/')[^1].StartsWith("b_", StringComparison.Ordinal)); |
| 165 | Assert.Equal(["hipnotic", "id1", "rogue"], original.Select(map => map.Category).Distinct().Order()); |
| 166 | } |
| 167 | |
| 168 | [Fact] |
| 169 | public void ConvertsTheSlipgateComplex() |
| 170 | { |
| 171 | var host = Open(); |
| 172 | |
| 173 | var manifest = MapText.ReadManifest(Text(Convert(host, ContentKind.Map, "quake:id1/maps/e1m1")), "e1m1"); |
| 174 | var cellContent = Convert(host, ContentKind.Cell, "quake:id1/maps/e1m1/cells/0"); |
| 175 | var cell = MapText.ReadCell(Text(cellContent), "e1m1/cells/0"); |
| 176 | var world = ModelFile.Read(Convert(host, ContentKind.Model, "quake:id1/maps/e1m1/models/0").Data, "world"); |
| 177 | var page = TextureFile.Read(Convert(host, ContentKind.Texture, "quake:id1/maps/e1m1/lightmaps/ldr").Data, |
| 178 | "page"); |
| 179 | var sky = SkyText.Read(Text(Convert(host, ContentKind.Sky, "quake:id1/maps/e1m1/sky")), "sky"); |
| 180 | var skyMaterial = |
| 181 | MaterialText.Read(Text(Convert(host, ContentKind.Material, "quake:id1/maps/e1m1/materials/sky4")), "sky4"); |
| 182 | var water = MaterialText.Read( |
| 183 | Text(Convert(host, ContentKind.Material, "quake:id1/maps/e1m1/materials/*water0")), "water"); |
| 184 | var button = |
| 185 | MaterialText.Read(Text(Convert(host, ContentKind.Material, "quake:id1/maps/e1m1/materials/+0basebtn")), |
| 186 | "basebtn"); |
| 187 | var slip = TextureFile.Read(Convert(host, ContentKind.Texture, "quake:id1/maps/e1m1/textures/+0slip").Data, |
| 188 | "slip"); |
| 189 | |
| 190 | foreach (var line in cellContent.Diagnostics) |
| 191 | output.WriteLine(line); |
| 192 | output.WriteLine( |
| 193 | $"{cell.Solids.Count} solids, {cell.Entities.Count} entities, {cell.Models.Count} placements, page {page.Width}x{page.Height}"); |
| 194 | Assert.Equal(ContentId.Parse("quake:id1/maps/e1m1/sky"), manifest.Sky); |
| 195 | Assert.Equal(new MapLightmap(ContentId.Parse("quake:id1/maps/e1m1/lightmaps/ldr"), null), manifest.Lightmap); |
| 196 | Assert.Equal((TextureFormat.R8Unorm, "rrr1"), (page.Format, page.Swizzle)); |
| 197 | Assert.Equal(ContentId.Parse("quake:id1/maps/e1m1/materials/sky4/dome"), |
| 198 | Assert.IsType<SkyLayer.Dome>(Assert.Single(sky.Layers)).Material); |
| 199 | Assert.Equal(ContentId.Parse("quake:shaders/skydome"), |
| 200 | MaterialText.Read(Text(Convert(host, ContentKind.Material, "quake:id1/maps/e1m1/materials/sky4/dome")), |
| 201 | "dome").Shader); |
| 202 | Assert.Equal(ContentId.Parse("quake:id1/gfx/colormap"), button.Textures["colormap"]); |
| 203 | Assert.Equal(ContentId.Parse("quake:id1/maps/e1m1/textures/+0basebtn/alternate/indices"), |
| 204 | button.Textures["alternate"]); |
| 205 | Assert.Equal(ContentId.Parse("quake:shaders/sky"), skyMaterial.Shader); |
| 206 | Assert.Equal(ContentId.Parse("quake:shaders/turbulent"), water.Shader); |
| 207 | Assert.Equal(ContentId.Parse("quake:shaders/lightmapped"), button.Shader); |
| 208 | Assert.Equal("frame", Assert.Single(button.Drivers).Op); |
| 209 | Assert.Equal((7, 7), (slip.Layers, slip.Levels.Count)); |
| 210 | |
| 211 | Assert.Equal("quake:worldspawn", cell.Entities[0].Class.ToString()); |
| 212 | Assert.Contains(new KeyValuePair<string, string>("message", "the Slipgate Complex"), |
| 213 | cell.Entities[0].Properties); |
| 214 | var start = Assert.Single(cell.Entities, entity => entity.Class.ToString() == "quake:info_player_start"); |
| 215 | Assert.Contains(cell.Spawns, spawn => spawn.Owner == start.Id); |
| 216 | Assert.Contains(cell.Models, |
| 217 | place => place.Model.ToString() == "quake:id1/maps/e1m1/models/0" && place.Lightmap); |
| 218 | var door = cell.Entities.First(entity => entity.Class.ToString() == "quake:func_door"); |
| 219 | Assert.Contains(cell.Models, place => place.Owner == door.Id); |
| 220 | Assert.Contains(cell.Solids, solid => solid.Owner == door.Id); |
| 221 | Assert.Contains(cell.Solids, solid => solid.Contents.Contains(SolidContents.Water) || |
| 222 | solid.Contents.Contains(SolidContents.Slime)); |
| 223 | Assert.All(cell.Solids, solid => Assert.True(solid.Sides.Count >= 4)); |
| 224 | |
| 225 | var feet = start.Transform.Position; |
| 226 | var floor = feet with { Y = feet.Y - 30 * 0.0254 }; |
| 227 | Assert.DoesNotContain(cell.Solids, solid => solid.Owner is null && Inside(solid, feet)); |
| 228 | Assert.Contains(cell.Solids, solid => |
| 229 | solid.Owner is null && solid.Contents.SequenceEqual([SolidContents.Solid]) && |
| 230 | Inside(solid, floor)); |
| 231 | |
| 232 | var (triangles, agreeing) = Facing(world); |
| 233 | output.WriteLine($"{triangles} triangles, {agreeing:P2} facing their normals"); |
| 234 | Assert.True(agreeing > 0.99, $"Only {agreeing:P} of the triangles face the way their normals do."); |
| 235 | var primitives = world.Meshes.SelectMany(mesh => mesh.Primitives).ToArray(); |
| 236 | Assert.All(primitives.SelectMany(primitive => primitive.Vertices.Uv1!), |
| 237 | uv => Assert.True(uv is { X: >= 0 and <= 1, Y: >= 0 and <= 1 }, $"Lightmap UV {uv} leaves the page.")); |
| 238 | } |
| 239 | |
| 240 | [Fact] |
| 241 | public void LightsTheRereleasesSlipgateComplexInColour() |
| 242 | { |
| 243 | var host = Open(); |
| 244 | |
| 245 | var page = TextureFile.Read( |
| 246 | Convert(host, ContentKind.Texture, "quake-rerelease:id1/maps/e1m1/lightmaps/ldr").Data, "page"); |
| 247 | var water = MaterialText.Read( |
| 248 | Text(Convert(host, ContentKind.Material, "quake-rerelease:id1/maps/e1m1/materials/*water0")), "water"); |
| 249 | |
| 250 | Assert.Equal(TextureFormat.R8G8B8Unorm, page.Format); |
| 251 | Assert.Equal(new MaterialValue.Number(0.666), water.Parameters["alpha"]); |
| 252 | Assert.Equal(MaterialBlend.Blend, water.Blend); |
| 253 | } |
| 254 | |
| 255 | [Theory] |
| 256 | [InlineData("quake", "id1/maps/e1m1", "the Slipgate Complex")] |
| 257 | [InlineData("quake-rerelease", "mg3/maps/map1", "Through the First Arch")] |
| 258 | public void LoadsThroughTheMapLoader(string game, string map, string title) |
| 259 | { |
| 260 | var host = Open(); |
| 261 | var record = new MapRecord(ContentId.Parse($"{game}:{map}"), ContentId.Parse($"{game}:{map}")) |
| 262 | { Title = title }; |
| 263 | var lines = new List<string>(); |
| 264 | |
| 265 | var load = new MapLoader(host.Conversion).Load(record, |
| 266 | (tag, text) => lines.Add(new TranscriptLine(TimeSpan.Zero, tag, text).ToString()), |
| 267 | TestContext.Current.CancellationToken); |
| 268 | |
| 269 | foreach (var line in lines) |
| 270 | output.WriteLine(line); |
| 271 | Assert.True(load.Loaded); |
| 272 | Assert.Equal(0, load.Count(ConversionStatus.Failed)); |
| 273 | Assert.DoesNotContain(load.Results, result => result.Status == ConversionStatus.NoConverter); |
| 274 | Assert.NotEmpty(Assert.Single(load.Cells).Cell.Spawns); |
| 275 | } |
| 276 | |
| 277 | [Fact] |
| 278 | public void ConvertsTheSameMapToTheSameBytes() |
| 279 | { |
| 280 | string[] ids = |
| 281 | [ |
| 282 | "map:quake-rerelease:mg3/maps/map1", "cell:quake-rerelease:mg3/maps/map1/cells/0", |
| 283 | "model:quake-rerelease:mg3/maps/map1/models/0", "texture:quake-rerelease:mg3/maps/map1/lightmaps/ldr" |
| 284 | ]; |
| 285 | |
| 286 | string Hashes() |
| 287 | { |
| 288 | var host = Open(); |
| 289 | return string.Join(' ', ids.Select(id => |
| 290 | Convert(host, new ContentKind(id[..id.IndexOf(':')]), id[(id.IndexOf(':') + 1)..]).Hash)); |
| 291 | } |
| 292 | |
| 293 | Assert.Equal(Hashes(), Hashes()); |
| 294 | } |
| 295 | |
| 296 | [Fact] |
| 297 | public void LoadsEveryMapOfBothEditions() |
| 298 | { |
| 299 | var host = Open(); |
| 300 | var timer = Stopwatch.StartNew(); |
| 301 | var failed = new List<string>(); |
| 302 | var counts = new Dictionary<string, int>(); |
| 303 | var maps = 0; |
| 304 | |
| 305 | foreach (var game in new[] { "quake", "quake-rerelease" }) |
| 306 | foreach (var record in Records(host, game)) |
| 307 | { |
| 308 | var load = new MapLoader(host.Conversion).Load(record, (_, _) => { }, |
| 309 | TestContext.Current.CancellationToken); |
| 310 | maps++; |
| 311 | if (!load.Loaded || load.Cells.Count != 1 || load.Cells[0].Cell.Spawns.Count == 0) |
| 312 | failed.Add($"{record.Map} didn't load with a spawn point"); |
| 313 | foreach (var result in load.Results) |
| 314 | { |
| 315 | counts[result.Kind.Name] = counts.GetValueOrDefault(result.Kind.Name) + (result.Succeeded ? 1 : 0); |
| 316 | if (!result.Succeeded) |
| 317 | failed.Add( |
| 318 | $"{result.Kind.Name} {result.Id}: {result.Status} {string.Join(' ', result.Diagnostics)}"); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | output.WriteLine($"{maps} maps in {timer.Elapsed.TotalSeconds:0.0} s: " + |
| 323 | string.Join(", ", |
| 324 | counts.OrderBy(pair => pair.Key, StringComparer.Ordinal) |
| 325 | .Select(pair => $"{pair.Value} {pair.Key}"))); |
| 326 | foreach (var failure in failed.Take(40)) |
| 327 | output.WriteLine(failure); |
| 328 | Assert.Equal(244, maps); |
| 329 | Assert.Empty(failed); |
| 330 | } |
| 331 | } |