0.2.0-dev Download ZIP
309 lines ·16.8 KB Raw
| 1 | using System.Numerics; |
| 2 | using System.Text; |
| 3 | using EvilEngine.Adapters.Hpl3; |
| 4 | using EvilEngine.Conversion; |
| 5 | using EvilEngine.Formats; |
| 6 | using EvilEngine.Formats.Maps; |
| 7 | using EvilEngine.Formats.Models; |
| 8 | using EvilEngine.Mounts; |
| 9 | |
| 10 | namespace EvilEngine.Adapters.Tests; |
| 11 | |
| 12 | public sealed class Hpl3MapTests |
| 13 | { |
| 14 | private const string Map = "amnesia-bunker:maps/trenches/trenches"; |
| 15 | |
| 16 | private const string Settings = """ |
| 17 | <HPLMap ID="FD90" MajorVersion="1" MinorVersion="1"> |
| 18 | <GlobalSettings> |
| 19 | <Fog Active="true" Color="0.5 0.25 0.125 1" FadeStart="2" FadeEnd="40" Exponential="false" Density="0.01" /> |
| 20 | <SkyBox Active="false" Color="1 1 1 1" Texture="" Brightness="1" /> |
| 21 | <DirLight Active="true" DiffuseColor="0.5 1 1 1" Brightness="2 2 2 1" Direction="0 -2 0" /> |
| 22 | </GlobalSettings> |
| 23 | </HPLMap> |
| 24 | """; |
| 25 | |
| 26 | private const string StaticObjects = """ |
| 27 | <HPLMapTrack_StaticObject ID="FD90" MajorVersion="1" MinorVersion="1"> |
| 28 | <Section Name="rasmu@WINDMILL"> |
| 29 | <FileIndex_StaticObjects NumOfFiles="3"> |
| 30 | <File Id="0" Path="static_objects/bunker/wall.dae" /> |
| 31 | <File Id="1" Path="static_objects/_temp/gone.dae" /> |
| 32 | <File Id="2" Path="D:/art/old/Lamp.DAE" /> |
| 33 | </FileIndex_StaticObjects> |
| 34 | <Objects> |
| 35 | <StaticObject ID="1" Name="wall_1" WorldPos="1 2 3" Rotation="0 1.5707963267948966 0" Scale="0.5 1 2" FileIndex="0" /> |
| 36 | <StaticObject ID="2" Name="wall_2" WorldPos="-4 0 10" Rotation="1.5707963267948966 1.5707963267948966 0" Scale="1 1 1" FileIndex="0" /> |
| 37 | <StaticObject ID="3" Name="gone_1" WorldPos="0 0 0" Rotation="0 0 0" Scale="1 1 1" FileIndex="1" /> |
| 38 | <StaticObject ID="4" Name="gone_2" WorldPos="0 0 0" Rotation="0 0 0" Scale="1 1 1" FileIndex="1" /> |
| 39 | <StaticObject ID="5" Name="lamp_1" WorldPos="0 3 0" Rotation="0 0 0" Scale="1 1 1" FileIndex="2" /> |
| 40 | </Objects> |
| 41 | </Section> |
| 42 | </HPLMapTrack_StaticObject> |
| 43 | """; |
| 44 | |
| 45 | private const string Entities = """ |
| 46 | <HPLMapTrack_Entity ID="FD90" MajorVersion="1" MinorVersion="1"> |
| 47 | <Section Name="rasmu@WINDMILL"> |
| 48 | <FileIndex_Entities NumOfFiles="4"> |
| 49 | <File Id="0" Path="entities/bunker/crate/crate.ent" /> |
| 50 | <File Id="1" Path="entities/bunker/missing.ent" /> |
| 51 | <File Id="2" Path="entities/technical/blocker.ent" /> |
| 52 | <File Id="3" Path="entities/technical/collider.ent" /> |
| 53 | </FileIndex_Entities> |
| 54 | <Objects> |
| 55 | <Entity ID="10" Name="crate_1" WorldPos="5 0 5" Rotation="0 0 0" Scale="1 2 1" FileIndex="0" Active="true"> |
| 56 | <UserVariables> |
| 57 | <Var ObjectId="10" Name="CastShadows" Value="true" /> |
| 58 | <Var ObjectId="10" Name="CollideGroup" Value="-agent" /> |
| 59 | </UserVariables> |
| 60 | </Entity> |
| 61 | <Entity ID="11" Name="missing_1" WorldPos="0 0 0" Rotation="0 0 0" Scale="1 1 1" FileIndex="1" /> |
| 62 | <Entity ID="12" Name="blocker_1" WorldPos="0 0 0" Rotation="0 0 0" Scale="1 1 1" FileIndex="2" /> |
| 63 | <Entity ID="13" Name="collider_1" WorldPos="0 0 0" Rotation="0 0 0" Scale="1 1 1" FileIndex="3" /> |
| 64 | </Objects> |
| 65 | </Section> |
| 66 | </HPLMapTrack_Entity> |
| 67 | """; |
| 68 | |
| 69 | private const string Areas = """ |
| 70 | <HPLMapTrack_Area ID="FD90" MajorVersion="1" MinorVersion="1"> |
| 71 | <Section Name="rasmu@WINDMILL"> |
| 72 | <Objects> |
| 73 | <Area ID="20" Name="Start_Begin" WorldPos="-31.125 7.438 3.25" Rotation="0 3.141592653589793 0" Scale="0.7 1.9 0.7" Active="true" Mesh="" AreaType="PlayerStart" /> |
| 74 | <Area ID="21" Name="Trigger_Door" WorldPos="0 1 0" Rotation="0 0 0" Scale="2 2 2" Active="true" Mesh="" AreaType="Trigger" /> |
| 75 | <Area ID="10" Name="Clash" WorldPos="0 0 0" Rotation="0 0 0" Scale="1 1 1" AreaType="Trigger" /> |
| 76 | </Objects> |
| 77 | </Section> |
| 78 | </HPLMapTrack_Area> |
| 79 | """; |
| 80 | |
| 81 | private const string Primitives = """ |
| 82 | <HPLMapTrack_Primitive ID="FD90" MajorVersion="1" MinorVersion="1"> |
| 83 | <Section Name="rasmu@WINDMILL"> |
| 84 | <Objects> |
| 85 | <Plane ID="30" Name="Plane_1" WorldPos="10 1 -2" Rotation="0 0 0" Scale="1 1 1" Material="D:/SVN Repositories/Bunker/redist/static_objects/bunker/floor/concrete.mat" StartCorner="0 0 0" EndCorner="4 0 2" DiffuseColorMul="1 1 1 1" Corner1UV="0 2" Corner2UV="0 0" Corner3UV="4 0" Corner4UV="4 2" /> |
| 86 | </Objects> |
| 87 | </Section> |
| 88 | </HPLMapTrack_Primitive> |
| 89 | """; |
| 90 | |
| 91 | private static ConversionPipeline Pipeline(bool primitives = true) |
| 92 | { |
| 93 | var files = new MemorySource("game", CaseRule.Insensitive) |
| 94 | .Add("resources.cfg", |
| 95 | """ |
| 96 | <Resources> |
| 97 | <Directory Path="/static_objects" AddSubDirs="true"/> |
| 98 | <Directory Path="/entities" AddSubDirs="true"/> |
| 99 | </Resources> |
| 100 | """) |
| 101 | .Add("static_objects/bunker/lamp/lamp.msh", "msh") |
| 102 | .Add("maps/trenches/trenches.hpm", Settings) |
| 103 | .Add("maps/trenches/trenches.hpm_StaticObject", StaticObjects) |
| 104 | .Add("maps/trenches/trenches.hpm_Entity", Entities) |
| 105 | .Add("maps/trenches/trenches.hpm_Area", Areas) |
| 106 | .Add("maps/trenches/trenches.map_cache", "\u001fEA") |
| 107 | .Add("static_objects/bunker/wall.dae", "dae") |
| 108 | .Add("static_objects/bunker/wall.msh", "msh") |
| 109 | .Add("static_objects/bunker/floor/concrete.mat", "<Material/>") |
| 110 | .Add("entities/bunker/crate/crate.ent", |
| 111 | """<Entity><ModelData><Mesh Filename="D:/art/crate_v2/crate.DAE"/></ModelData><UserDefinedVariables EntityType="Prop_Rigid"/></Entity>""") |
| 112 | .Add("entities/bunker/crate/crate.msh", "msh") |
| 113 | .Add("entities/technical/blocker.ent", |
| 114 | """<Entity><ModelData><Mesh Filename="entities/bunker/crate/crate.dae"/></ModelData><UserDefinedVariables EntityType="Prop_Static"><Var Name="ShowMesh" Value="false"/></UserDefinedVariables></Entity>""") |
| 115 | .Add("entities/technical/collider.ent", |
| 116 | """<Entity><ModelData><Mesh Filename="entities/bunker/crate/crate.dae"/></ModelData><UserDefinedVariables EntityType="StaticCollider"/></Entity>"""); |
| 117 | if (primitives) |
| 118 | files.Add("maps/trenches/trenches.hpm_Primitive", Primitives); |
| 119 | var pipeline = new ConversionPipeline(new MemoryConversionCache()); |
| 120 | pipeline.AddNamespace(new ContentNamespace("amnesia-bunker", files)); |
| 121 | pipeline.Bind("amnesia-bunker", new Hpl3SkyConverter()); |
| 122 | pipeline.Bind("amnesia-bunker", new Hpl3ManifestConverter()); |
| 123 | pipeline.Bind("amnesia-bunker", new Hpl3CellConverter()); |
| 124 | pipeline.Bind("amnesia-bunker", new Hpl3PrimitivesConverter()); |
| 125 | return pipeline; |
| 126 | } |
| 127 | |
| 128 | private static ConvertedContent Get(ConversionPipeline pipeline, ContentKind kind, string id) |
| 129 | { |
| 130 | var result = pipeline.Get(kind, ContentId.Parse(id)); |
| 131 | Assert.True(result.Succeeded, string.Join('\n', result.Diagnostics)); |
| 132 | return result.Content!; |
| 133 | } |
| 134 | |
| 135 | private static MapCell Cell(ConversionPipeline pipeline) |
| 136 | { |
| 137 | return MapText.ReadCell(Encoding.UTF8.GetString(Get(pipeline, ContentKind.Cell, $"{Map}/cells/0").Data.Span), |
| 138 | Map); |
| 139 | } |
| 140 | |
| 141 | private static MapVector Turn(MapVector angles, MapVector v) |
| 142 | { |
| 143 | var m = MapAngles.ToBasis(angles); |
| 144 | return new MapVector(m[0, 0] * v.X + m[0, 1] * v.Y + m[0, 2] * v.Z, |
| 145 | m[1, 0] * v.X + m[1, 1] * v.Y + m[1, 2] * v.Z, |
| 146 | m[2, 0] * v.X + m[2, 1] * v.Y + m[2, 2] * v.Z); |
| 147 | } |
| 148 | |
| 149 | private static void Near(MapVector expected, MapVector actual) |
| 150 | { |
| 151 | var (dx, dy, dz) = (expected.X - actual.X, expected.Y - actual.Y, expected.Z - actual.Z); |
| 152 | Assert.True(Math.Sqrt(dx * dx + dy * dy + dz * dz) < 1e-6, $"Expected {expected}, got {actual}."); |
| 153 | } |
| 154 | |
| 155 | [Fact] |
| 156 | public void ConvertsTheManifestWithItsSkyFogAndOneCellAroundEverythingPlaced() |
| 157 | { |
| 158 | var map = MapText.ReadManifest(Encoding.UTF8.GetString(Get(Pipeline(), ContentKind.Map, Map).Data.Span), Map); |
| 159 | |
| 160 | Assert.Equal($"{Map}/sky", map.Sky.ToString()); |
| 161 | var cell = Assert.Single(map.Cells); |
| 162 | Assert.Equal($"{Map}/cells/0", cell.Id.ToString()); |
| 163 | Assert.Equal(new MapVector(-31.125, 0, -2), cell.Min); |
| 164 | Assert.Equal(new MapVector(14, 7.438, 10), cell.Max); |
| 165 | Assert.Equal(MapFogMode.Linear, map.Fog!.Mode); |
| 166 | Assert.Equal(new MapVector(0.5, 0.25, 0.125), map.Fog.Colour); |
| 167 | Assert.Equal((2.0, 40.0), (map.Fog.Start!.Value, map.Fog.End!.Value)); |
| 168 | } |
| 169 | |
| 170 | [Fact] |
| 171 | public void PlacesStaticObjectsAsTheGamesOwnModelsScaledAlongEachAxis() |
| 172 | { |
| 173 | var pipeline = Pipeline(); |
| 174 | var content = Get(pipeline, ContentKind.Cell, $"{Map}/cells/0"); |
| 175 | var cell = Cell(pipeline); |
| 176 | |
| 177 | var walls = cell.Models.Where(place => place.Model.Name == "static_objects/bunker/wall").ToArray(); |
| 178 | Assert.Equal(2, walls.Length); |
| 179 | Assert.All(walls, wall => Assert.Null(wall.Owner)); |
| 180 | Assert.Equal(new MapVector(1, 2, 3), walls[0].Transform.Position); |
| 181 | Assert.Equal(new MapVector(0, 90, 0), walls[0].Transform.Angles); |
| 182 | Assert.Equal(new MapVector(0.5, 1, 2), walls[0].Transform.Scale); |
| 183 | Assert.Contains(content.References, reference => |
| 184 | reference.Kind == ContentKind.Model && |
| 185 | reference.Id.ToString() == "amnesia-bunker:static_objects/bunker/wall"); |
| 186 | Assert.Contains(content.Diagnostics, note => note.Contains("'static_objects/_temp/gone.dae' 2 times")); |
| 187 | } |
| 188 | |
| 189 | [Fact] |
| 190 | public void FindsAMeshItsAuthorsPathMissesByItsNameInTheResourceFolders() |
| 191 | { |
| 192 | var lamp = Cell(Pipeline()).Models.Single(place => place.Transform.Position == new MapVector(0, 3, 0)); |
| 193 | |
| 194 | Assert.Equal("amnesia-bunker:static_objects/bunker/lamp/lamp", lamp.Model.ToString()); |
| 195 | } |
| 196 | |
| 197 | [Fact] |
| 198 | public void TurnsAboutXThenYThenZAsHpl3sEulerOrderDoes() |
| 199 | { |
| 200 | var wall = Cell(Pipeline()).Models.Single(place => place.Transform.Position == new MapVector(-4, 0, 10)); |
| 201 | |
| 202 | Near(new MapVector(1, 0, 0), Turn(wall.Transform.Angles, new MapVector(0, 1, 0))); |
| 203 | Near(new MapVector(0, 0, -1), Turn(wall.Transform.Angles, new MapVector(1, 0, 0))); |
| 204 | } |
| 205 | |
| 206 | [Fact] |
| 207 | public void MakesEntitiesGameEntitiesOfTheirTypeOwningTheirFilesModelWhenItsMeshIsFound() |
| 208 | { |
| 209 | var pipeline = Pipeline(); |
| 210 | var content = Get(pipeline, ContentKind.Cell, $"{Map}/cells/0"); |
| 211 | var cell = Cell(pipeline); |
| 212 | |
| 213 | var crate = cell.Entities.Single(entity => entity.Id == "10"); |
| 214 | Assert.Equal("amnesia-bunker:prop_rigid", crate.Class.ToString()); |
| 215 | Assert.Equal(new MapVector(5, 0, 5), crate.Transform.Position); |
| 216 | Assert.Equal( |
| 217 | [ |
| 218 | "ID", "Name", "WorldPos", "Rotation", "Scale", "FileIndex", "Active", "File", "CastShadows", |
| 219 | "CollideGroup" |
| 220 | ], |
| 221 | crate.Properties.Select(property => property.Key)); |
| 222 | Assert.Equal("entities/bunker/crate/crate.ent", crate.Properties[7].Value); |
| 223 | var placed = cell.Models.Single(place => place.Owner == "10"); |
| 224 | Assert.Equal("amnesia-bunker:entities/bunker/crate/crate/model", placed.Model.ToString()); |
| 225 | Assert.Equal(new MapVector(1, 2, 1), placed.Transform.Scale); |
| 226 | Assert.DoesNotContain(cell.Entities, entity => entity.Id == "11"); |
| 227 | Assert.Contains(content.Diagnostics, |
| 228 | note => note.Contains("entity 11") && note.Contains("isn't in its namespace")); |
| 229 | } |
| 230 | |
| 231 | [Fact] |
| 232 | public void KeepsBlockersAndCollidersAsEntitiesWithoutDrawingTheirMeshes() |
| 233 | { |
| 234 | var cell = Cell(Pipeline()); |
| 235 | |
| 236 | Assert.Equal(["amnesia-bunker:prop_static", "amnesia-bunker:staticcollider"], |
| 237 | cell.Entities.Where(entity => entity.Id is "12" or "13").Select(entity => entity.Class.ToString())); |
| 238 | Assert.DoesNotContain(cell.Models, place => place.Owner is "12" or "13"); |
| 239 | } |
| 240 | |
| 241 | [Fact] |
| 242 | public void SpawnsAtPlayerStartAreasAndKeepsEveryAreaAsAGameEntity() |
| 243 | { |
| 244 | var pipeline = Pipeline(); |
| 245 | var content = Get(pipeline, ContentKind.Cell, $"{Map}/cells/0"); |
| 246 | var cell = Cell(pipeline); |
| 247 | |
| 248 | var spawn = Assert.Single(cell.Spawns); |
| 249 | Assert.Equal("20", spawn.Owner); |
| 250 | Assert.Equal(new MapVector(-31.125, 7.438, 3.25), spawn.Transform.Position); |
| 251 | Assert.Equal(180, Math.Abs(spawn.Transform.Angles.Y), 6); |
| 252 | Assert.Equal("amnesia-bunker:playerstart", cell.Entities.Single(entity => entity.Id == "20").Class.ToString()); |
| 253 | Assert.Equal("amnesia-bunker:trigger", cell.Entities.Single(entity => entity.Id == "21").Class.ToString()); |
| 254 | Assert.Contains(content.Diagnostics, note => note.Contains("second object with ID 10")); |
| 255 | } |
| 256 | |
| 257 | [Fact] |
| 258 | public void DrawsPrimitivePlanesAsOneModelWithTheirCornerUvs() |
| 259 | { |
| 260 | var pipeline = Pipeline(); |
| 261 | var content = Get(pipeline, ContentKind.Model, $"{Map}/primitives"); |
| 262 | var model = ModelFile.Read(content.Data, Map); |
| 263 | |
| 264 | Assert.Contains(Cell(pipeline).Models, |
| 265 | place => place.Model.ToString() == $"{Map}/primitives" && place.Owner is null); |
| 266 | var material = Assert.Single(model.Materials); |
| 267 | Assert.Equal("amnesia-bunker:static_objects/bunker/floor/concrete", material.Id.ToString()); |
| 268 | Assert.Contains(content.References, reference => reference.Kind == ContentKind.Material); |
| 269 | var primitive = Assert.Single(Assert.Single(model.Meshes).Primitives); |
| 270 | Assert.Equal([10, 1, 0], |
| 271 | [primitive.Vertices.Positions[0].X, primitive.Vertices.Positions[0].Y, primitive.Vertices.Positions[0].Z]); |
| 272 | Assert.Equal(new Vector3(14, 1, -2), primitive.Vertices.Positions[2]); |
| 273 | Assert.Equal(new Vector2(0, 2), primitive.Vertices.Uv0![0]); |
| 274 | Assert.Equal(new Vector2(4, 0), primitive.Vertices.Uv0[2]); |
| 275 | Assert.All(primitive.Vertices.Normals!, normal => Assert.Equal(Vector3.UnitY, normal)); |
| 276 | var p = primitive.Vertices.Positions; |
| 277 | var i = primitive.Indices; |
| 278 | Assert.True(Vector3.Dot(Vector3.Cross(p[i[1]] - p[i[0]], p[i[2]] - p[i[0]]), Vector3.UnitY) > 0); |
| 279 | } |
| 280 | |
| 281 | [Fact] |
| 282 | public void PlacesNoPrimitivesModelForAMapWithNoPlanes() |
| 283 | { |
| 284 | Assert.DoesNotContain(Cell(Pipeline(false)).Models, place => place.Model.Name.EndsWith("/primitives")); |
| 285 | } |
| 286 | |
| 287 | [Fact] |
| 288 | public void LightsTheSkyWithTheMapsDirectionalLightTowardWhereItComesFrom() |
| 289 | { |
| 290 | var pipeline = Pipeline(); |
| 291 | |
| 292 | var sky = SkyText.Read(Encoding.UTF8.GetString(Get(pipeline, ContentKind.Sky, $"{Map}/sky").Data.Span), Map); |
| 293 | |
| 294 | Assert.Equal(new MapVector(0, 1, 0), sky.Sun!.Direction); |
| 295 | Assert.Equal(new MapVector(1, 2, 2), sky.Sun.Colour); |
| 296 | Assert.Equal(MapFogMode.Linear, sky.Fog!.Mode); |
| 297 | } |
| 298 | |
| 299 | [Fact] |
| 300 | public void ConvertsTwiceToTheSameBytes() |
| 301 | { |
| 302 | foreach (var (kind, id) in new[] |
| 303 | { |
| 304 | (ContentKind.Map, Map), (ContentKind.Cell, $"{Map}/cells/0"), |
| 305 | (ContentKind.Model, $"{Map}/primitives") |
| 306 | }) |
| 307 | Assert.Equal(Get(Pipeline(), kind, id).Hash, Get(Pipeline(), kind, id).Hash); |
| 308 | } |
| 309 | } |