using System.Buffers.Binary; using System.Numerics; using System.Text; namespace EvilEngine.Adapters.Tests.Support; public static class QuakeBoxRoom { public const int Size = 64; public const int LuxelsPerSide = Size / 16 + 1; public const byte FullbrightIndex = 250; public const string DefaultEntities = """ { "classname" "worldspawn" "message" "Box Room" } // a start in the middle of the floor { "classname" "info_player_start" "origin" "32 32 24" "angle" "90" } """; public static byte[] Palette() { var palette = new byte[768]; for (var i = 0; i < 256; i++) { palette[i * 3] = (byte)i; palette[i * 3 + 1] = (byte)(255 - i); palette[i * 3 + 2] = (byte)(i / 2); } return palette; } public static byte[] Colormap() { var colormap = new byte[64 * 256 + 1]; for (var row = 0; row < 64; row++) for (var i = 0; i < 256; i++) colormap[row * 256 + i] = i >= 224 ? (byte)i : (byte)((i + row) % 224); colormap[^1] = 32; return colormap; } public static byte[] Lit() { var lit = new byte[8 + LuxelsPerSide * LuxelsPerSide * 3]; "QLIT"u8.CopyTo(lit); BinaryPrimitives.WriteInt32LittleEndian(lit.AsSpan(4), 1); for (var i = 8; i < lit.Length; i += 3) (lit[i], lit[i + 1], lit[i + 2]) = (200, 100, 50); return lit; } public static byte[] Build(bool bsp2, string entities = DefaultEntities, string floor = "floor") { return new QuakeBspWriter(bsp2).Build(entities, floor); } } public sealed class QuakeBspWriter(bool bsp2) { private const int Size = QuakeBoxRoom.Size; public byte[] Build(string entities, string floorTexture) { var lumps = new byte[15][]; lumps[0] = Encoding.Latin1.GetBytes(entities + "\0"); (Vector3 Normal, float Distance)[] planes = [ (Vector3.UnitZ, 0), (Vector3.UnitZ, Size), (Vector3.UnitX, 0), (Vector3.UnitX, Size), (Vector3.UnitY, 0), (Vector3.UnitY, Size) ]; lumps[1] = Join(planes.Select(plane => Floats(plane.Normal.X, plane.Normal.Y, plane.Normal.Z, plane.Distance) .Concat(Ints(0)).ToArray())); lumps[2] = MipTex([(floorTexture, 16, 16), ("sky1", 32, 16)]); Vector3[] vertices = [ new(0, 0, 0), new(0, Size, 0), new(Size, Size, 0), new(Size, 0, 0), new(0, 0, Size), new(Size, 0, Size), new(Size, Size, Size), new(0, Size, Size) ]; lumps[3] = Join(vertices.Select(v => Floats(v.X, v.Y, v.Z))); lumps[4] = []; const int solid = -1; const int empty = -2; (int Plane, int Front, int Back, int FirstFace, int Faces)[] nodes = [ (0, 1, solid, 0, 1), (1, solid, 2, 1, 1), (2, 3, solid, 0, 0), (3, solid, 4, 0, 0), (4, 5, solid, 0, 0), (5, solid, empty, 0, 0) ]; lumps[5] = Join(nodes.Select(node => bsp2 ? Ints(node.Plane, node.Front, node.Back).Concat(Floats(0, 0, 0, Size, Size, Size)) .Concat(Ints(node.FirstFace, node.Faces)).ToArray() : Ints(node.Plane).Concat(Shorts(node.Front, node.Back, 0, 0, 0, Size, Size, Size)) .Concat(UShorts(node.FirstFace, node.Faces)).ToArray())); lumps[6] = Join( Floats(1, 0, 0, 0, 0, 1, 0, 0).Concat(Ints(0, 0)).ToArray(), Floats(1, 0, 0, 0, 0, 1, 0, 0).Concat(Ints(1, 1)).ToArray()); (int Plane, bool Back, int FirstEdge, int TexInfo, uint Styles, int Light)[] faces = [(0, false, 0, 0, 0xFFFFFF00, 0), (1, true, 4, 1, 0xFFFFFFFF, -1)]; lumps[7] = Join(faces.Select(face => bsp2 ? Ints(face.Plane, face.Back ? 1 : 0, face.FirstEdge, 4, face.TexInfo).Concat(UInts(face.Styles)) .Concat(Ints(face.Light)).ToArray() : Shorts(face.Plane, face.Back ? 1 : 0).Concat(Ints(face.FirstEdge)).Concat(Shorts(4, face.TexInfo)) .Concat(UInts(face.Styles)).Concat(Ints(face.Light)).ToArray())); lumps[8] = Enumerable.Range(0, QuakeBoxRoom.LuxelsPerSide * QuakeBoxRoom.LuxelsPerSide) .Select(i => (byte)(64 + i)).ToArray(); lumps[9] = []; (int Contents, int FirstMark, int Marks)[] leafs = [(-2, 0, 0), (-1, 0, 2)]; lumps[10] = Join(leafs.Select(leaf => bsp2 ? Ints(leaf.Contents, -1).Concat(Floats(0, 0, 0, Size, Size, Size)).Concat(Ints(leaf.FirstMark, leaf.Marks)) .Concat(new byte[4]).ToArray() : Ints(leaf.Contents, -1).Concat(Shorts(0, 0, 0, Size, Size, Size)) .Concat(UShorts(leaf.FirstMark, leaf.Marks)).Concat(new byte[4]).ToArray())); lumps[11] = bsp2 ? Ints(0, 1) : UShorts(0, 1); (int A, int B)[] edges = [(0, 0), (0, 1), (1, 2), (2, 3), (3, 0), (4, 5), (5, 6), (6, 7), (7, 4)]; lumps[12] = Join(edges.Select(edge => bsp2 ? Ints(edge.A, edge.B) : UShorts(edge.A, edge.B))); lumps[13] = Ints(1, 2, 3, 4, 5, 6, 7, 8); lumps[14] = Floats(0, 0, 0, Size, Size, Size, 0, 0, 0).Concat(Ints(0, 0, 0, 0, 1, 0, 2)).ToArray(); var header = 4 + 15 * 8; var output = new List(); output.AddRange(bsp2 ? "BSP2"u8.ToArray() : Ints(29)); var offset = header; foreach (var lump in lumps) { output.AddRange(Ints(offset, lump.Length)); offset += (lump.Length + 3) & ~3; } foreach (var lump in lumps) { output.AddRange(lump); output.AddRange(new byte[((lump.Length + 3) & ~3) - lump.Length]); } return [.. output]; } private static byte[] MipTex(IReadOnlyList<(string Name, int Width, int Height)> textures) { var directory = Ints(textures.Count).ToList(); var bodies = new List(); var start = 4 + textures.Count * 4; foreach (var (name, width, height) in textures) { directory.AddRange(Ints(start + bodies.Count)); var body = new List(); var nameBytes = new byte[16]; Encoding.ASCII.GetBytes(name).CopyTo(nameBytes, 0); body.AddRange(nameBytes); body.AddRange(Ints(width, height)); var level = 40; var offsets = new int[4]; for (var i = 0; i < 4; i++) { offsets[i] = level; level += (width >> i) * (height >> i); } body.AddRange(Ints(offsets)); for (var i = 0; i < 4; i++) for (var p = 0; p < (width >> i) * (height >> i); p++) body.Add(p == 0 && name != "sky1" ? QuakeBoxRoom.FullbrightIndex : (byte)(p % 200)); bodies.AddRange(body); } return [.. directory, .. bodies]; } private static byte[] Join(params IEnumerable parts) { return parts.SelectMany(part => part).ToArray(); } private static byte[] Ints(params int[] values) { var bytes = new byte[values.Length * 4]; for (var i = 0; i < values.Length; i++) BinaryPrimitives.WriteInt32LittleEndian(bytes.AsSpan(i * 4), values[i]); return bytes; } private static byte[] UInts(params uint[] values) { var bytes = new byte[values.Length * 4]; for (var i = 0; i < values.Length; i++) BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(i * 4), values[i]); return bytes; } private static byte[] Shorts(params int[] values) { var bytes = new byte[values.Length * 2]; for (var i = 0; i < values.Length; i++) BinaryPrimitives.WriteInt16LittleEndian(bytes.AsSpan(i * 2), (short)values[i]); return bytes; } private static byte[] UShorts(params int[] values) { var bytes = new byte[values.Length * 2]; for (var i = 0; i < values.Length; i++) BinaryPrimitives.WriteUInt16LittleEndian(bytes.AsSpan(i * 2), (ushort)values[i]); return bytes; } private static byte[] Floats(params float[] values) { var bytes = new byte[values.Length * 4]; for (var i = 0; i < values.Length; i++) BinaryPrimitives.WriteSingleLittleEndian(bytes.AsSpan(i * 4), values[i]); return bytes; } }