using System.Numerics; using System.Text; using EvilEngine.Adapters.Source.Maps; namespace EvilEngine.Adapters.Tests.Support; internal static class BoxMap { public const int Luxels = 9 * 9; public const string Floor = "maps/box/dev/dev_floor_0_0_0"; public static readonly Vector3[] Vertices = [ new(-64, 64, 128), new(64, 64, 128), new(64, -64, 128), new(-64, -64, 128), new(-64, 64, 0), new(64, 64, 0), new(64, -64, 0), new(-64, -64, 0), new(-64, 64, 2000), new(64, 64, 2000), new(64, -64, 2000), new(-64, -64, 2000), new(-8, 8, 16), new(8, 8, 16), new(8, -8, 16), new(-8, -8, 16) ]; public static readonly (Vector3 Normal, float Distance)[] Planes = [ (Vector3.UnitX, 64), (-Vector3.UnitX, 64), (Vector3.UnitY, 64), (-Vector3.UnitY, 64), (Vector3.UnitZ, 128), (-Vector3.UnitZ, 0), (Vector3.UnitZ, 1000), (Vector3.UnitZ, 0), (Vector3.UnitZ, 2000), (Vector3.UnitX, 8), (-Vector3.UnitX, 8), (Vector3.UnitY, 8), (-Vector3.UnitY, 8), (Vector3.UnitZ, 16) ]; public static readonly (int Plane, int First, int TexInfo, int Disp, byte[] Styles, int Light)[] Faces = [ (4, 0, 0, -1, [0, 32, 255, 255], 0), (7, 4, 1, 0, [0, 255, 255, 255], 2 * Luxels * 4), (4, 8, 2, -1, [255, 255, 255, 255], -1), (8, 12, 3, -1, [255, 255, 255, 255], -1), (13, 16, 3, -1, [255, 255, 255, 255], -1) ]; public static readonly string Patch = """ patch { include "materials/dev/dev_floor.vmt" insert { "$envmap" "maps/box/c0_0_0" } } """; private static readonly (string, string)[][] Plain = [ [("classname", "worldspawn"), ("skyname", "sky_day01_01")], [("origin", "0 0 1"), ("angles", "0 90 0"), ("classname", "info_player_start")], [ ("model", "*1"), ("origin", "100 0 0"), ("OnUser1", "a,Kill"), ("OnUser1", "b,Kill"), ("classname", "func_brush") ], [("origin", "0 0 2008"), ("scale", "16"), ("classname", "sky_camera")], [ ("model", "models/props_c17/oildrum001.mdl"), ("origin", "10 20 30"), ("angles", "0 45 0"), ("classname", "prop_dynamic") ], [("origin", "0 0 64"), ("classname", "light")] ]; private static readonly (string, string)[][] Painted = [ [("classname", "worldspawn"), ("skyname", "painted")], [ ("origin", "0 0 2008"), ("scale", "32"), ("fogenable", "1"), ("fogcolor", "255 128 0"), ("fogcolor2", "0 0 255"), ("fogblend", "1"), ("fogdir", "1 0 0"), ("fogstart", "100"), ("fogend", "1000"), ("fogmaxdensity", "0.5"), ("classname", "sky_camera") ], [ ("topcolor", "0.1 0.2 0.3"), ("drawstars", "1"), ("starlayers", "2"), ("startexture", "skybox/clouds"), ("starspeed", "0.03"), ("sunnormal", "0 0 1"), ("classname", "env_skypaint") ], [("angles", "0 90 0"), ("pitch", "-45"), ("use_angles", "1"), ("classname", "env_sun")], [ ("angles", "0 90 0"), ("pitch", "-30"), ("_light", "255 255 255 200"), ("_ambient", "128 128 128 50"), ("classname", "light_environment") ], [ ("fogenable", "1"), ("fogcolor", "10 20 30"), ("fogstart", "0"), ("fogend", "100"), ("classname", "env_fog_controller") ], [("targetname", "other"), ("classname", "env_tonemap_controller")], [("targetname", "Tonemap"), ("spawnflags", "1"), ("classname", "env_tonemap_controller")], [ ("spawnflags", "1"), ("OnMapSpawn", "Tonemap\u001bSetBloomScale\u001b0.5\u001b1\u001b-1"), ("OnMapSpawn", "Tonemap\u001bSetAutoExposureMax\u001b2.5\u001b0\u001b-1"), ("OnNewGame", "tone*,SetAutoExposureMin,.3,0,-1"), ("OnMapSpawn", "Tonemap\u001bSetBloomScale\u001b0\u001b0\u001b-1"), ("OnMapSpawn", "other\u001bSetAutoExposureMin\u001b0.1\u001b0\u001b-1"), ("OnMapSpawn", "Tonemap\u001bSetTonemapPercentTarget\u001b45\u001b0\u001b-1"), ("OnUser1", "Tonemap\u001bSetTonemapRate\u001b9\u001b0\u001b-1"), ("classname", "logic_auto") ] ]; private static byte[] Lighting(byte blue) { var data = new List(); for (var style = 0; style < 2; style++) for (var i = 0; i < Luxels; i++) data.AddRange([(byte)i, (byte)(10 * style), blue, 0]); for (var page = 0; page < 4; page++) for (var i = 0; i < Luxels; i++) data.AddRange([(byte)i, (byte)(40 * page + 1), blue, 0]); return data.ToArray(); } public static byte[] Write(int propsVersion = 6, bool painted = false) { var names = new[] { Floor, "NATURE/BLENDGRASS", "TOOLS/TOOLSSKYBOX" }; var strings = new List(); var offsets = new List(); foreach (var name in names) { offsets.Add(strings.Count); strings.AddRange([.. Encoding.ASCII.GetBytes(name), 0]); } var props = new MemoryStream(); using (var w = new BinaryWriter(props)) { w.Write(1); var model = new byte[128]; Encoding.ASCII.GetBytes("models/props_c17/oildrum001.mdl").CopyTo(model, 0); w.Write(model); w.Write(2); w.Write((ushort)0); w.Write((ushort)1); w.Write(2); foreach (var (origin, yaw, leaf) in new[] { (new Vector3(32, 0, 0), 90f, 0), (new Vector3(0, 0, 1500), 0f, 1) }) { BspWriter.Vector(w, origin); BspWriter.Vector(w, new Vector3(0, yaw, 0)); w.Write((ushort)0); w.Write((ushort)leaf); w.Write((ushort)1); w.Write((byte)6); w.Write((byte)0); w.Write(0); w.Write(0f); w.Write(0f); BspWriter.Vector(w, origin); w.Write(1f); w.Write((ushort)0); w.Write((ushort)0); } } var lzma = Convert.FromBase64String(UnityFsTests.TextLzma); void Face(BinaryWriter w, (int Plane, int First, int TexInfo, int Disp, byte[] Styles, int Light) face) { w.Write((ushort)face.Plane); w.Write((byte)0); w.Write((byte)0); w.Write(face.First); w.Write((short)4); w.Write((short)face.TexInfo); w.Write((short)face.Disp); w.Write((short)0); w.Write(face.Styles); w.Write(face.Light); w.Write(0f); w.Write(-4); w.Write(-4); w.Write(8); w.Write(8); w.Write(0); w.Write((ushort)0); w.Write((ushort)0); w.Write(0u); } return new BspWriter() .Entities(painted ? Painted : Plain) .Records(BspLump.Planes, Planes, (w, p) => { BspWriter.Vector(w, p.Normal); w.Write(p.Distance); w.Write(0); }) .Records(BspLump.TexData, new[] { (0, 128), (1, 512), (2, 64) }, (w, t) => { BspWriter.Vector(w, Vector3.One); w.Write(t.Item1); w.Write(t.Item2); w.Write(t.Item2); w.Write(t.Item2); w.Write(t.Item2); }) .Lump(BspLump.TexDataStringData, strings.ToArray()) .Records(BspLump.TexDataStringTable, offsets, (w, o) => w.Write(o)) .Records(BspLump.TexInfo, new[] { (BspSurface.None, 0), (BspSurface.BumpLight, 1), (BspSurface.Sky | BspSurface.NoLight, 2), (BspSurface.NoLight, 0) }, (w, t) => { foreach (var v in new[] { new Vector4(1, 0, 0, 0), new Vector4(0, -1, 0, 0), new Vector4(1 / 16f, 0, 0, 0), new Vector4(0, -1 / 16f, 0, 0) }) { w.Write(v.X); w.Write(v.Y); w.Write(v.Z); w.Write(v.W); } w.Write((int)t.Item1); w.Write(t.Item2); }) .Records(BspLump.Vertices, Vertices, BspWriter.Vector) .Records(BspLump.Edges, Enumerable.Range(-1, 21), (w, e) => { var f = e / 4; var corners = f < 2 ? f * 4 : f == 2 ? 0 : (f - 1) * 4; w.Write((ushort)(e < 0 ? 0 : corners + e % 4)); w.Write((ushort)(e < 0 ? 0 : corners + (e + 1) % 4)); }) .Records(BspLump.SurfEdges, Enumerable.Range(1, 20), (w, e) => w.Write(e)) .Records(BspLump.Faces, Faces, Face, 1) .Records(BspLump.FacesHdr, Faces, Face, 1) .Lump(BspLump.Lighting, Lighting(7), 1) .Lump(BspLump.LightingHdr, Lighting(9), 1) .Records(BspLump.Models, new[] { (new Vector3(-64, -64, 0), new Vector3(64, 64, 2000), 0, 0, 4), (new Vector3(-8, -8, 0), new Vector3(8, 8, 16), -3, 4, 1) }, (w, m) => { BspWriter.Vector(w, m.Item1); BspWriter.Vector(w, m.Item2); BspWriter.Vector(w, Vector3.Zero); w.Write(m.Item3); w.Write(m.Item4); w.Write(m.Item5); }) .Records(BspLump.Nodes, [0], (w, _) => { w.Write(6); w.Write(-2); w.Write(-1); w.Write(new byte[20]); }) .Records(BspLump.Leafs, new[] { (1, 0, 2, 0, 1), (2, 2, 1, 0, 0), (1, 0, 0, 1, 1) }, (w, l) => { w.Write(0); w.Write((short)0); w.Write((ushort)l.Item1); w.Write(new byte[12]); w.Write((ushort)l.Item2); w.Write((ushort)l.Item3); w.Write((ushort)l.Item4); w.Write((ushort)l.Item5); w.Write((short)-1); w.Write((short)0); }, 1) .Records(BspLump.LeafFaces, new ushort[] { 0, 2, 3 }, (w, f) => w.Write(f)) .Records(BspLump.LeafBrushes, new ushort[] { 0, 1 }, (w, b) => w.Write(b)) .Records(BspLump.Brushes, new[] { (0, 7, BspContents.Solid), (7, 6, BspContents.Water | BspContents.Current0) }, (w, b) => { w.Write(b.Item1); w.Write(b.Item2); w.Write((int)b.Item3); }) .Records(BspLump.BrushSides, new[] { (0, 0, false), (1, 0, false), (2, 0, false), (3, 0, false), (4, 0, false), (5, 0, false), (0, -1, true), (9, 0, false), (10, 0, false), (11, 0, false), (12, 0, false), (13, 2, false), (5, 0, false) }, (w, s) => { w.Write((ushort)s.Item1); w.Write((short)s.Item2); w.Write((short)-1); w.Write((byte)(s.Item3 ? 1 : 0)); w.Write((byte)0); }) .Records(BspLump.DispInfo, [0], (w, _) => { BspWriter.Vector(w, new Vector3(-64, -64, 0)); w.Write(0); w.Write(0); w.Write(2); w.Write(0); w.Write(0f); w.Write(1); w.Write((ushort)1); w.Write(new byte[176 - 38]); }) .Records(BspLump.DispVerts, Enumerable.Range(0, 25), (w, i) => { BspWriter.Vector(w, Vector3.UnitZ); w.Write(i / 5 * 4f); w.Write(i % 5 * 60f); }) .Records(BspLump.VertNormals, [Vector3.UnitZ], BspWriter.Vector) .Records(BspLump.VertNormalIndices, Enumerable.Repeat((ushort)0, 20), (w, i) => w.Write(i)) .Lump(BspLump.Pakfile, BspWriter.Zip( ($"materials/{Floor}.vmt", Encoding.ASCII.GetBytes(Patch), 0), ("materials/maps/box/c0_0_0.vtf", Encoding.ASCII.GetBytes("not really a vtf"), 8), ("Scripts\\Notes.txt", BspWriter.ZipLzma(lzma, UnityFsTests.Text().Length), 14))) .GameLump("sprp", (ushort)propsVersion, props.ToArray()) .GameLump("zzzz", 1, BspWriter.ValveLzma(lzma, UnityFsTests.Text().Length), true) .Write(); } }