Skip to content
0.2.0-dev Download ZIP
219 lines ·8.6 KB Raw
1using System.Buffers.Binary;
2using System.Numerics;
3using System.Text;
4
5namespace EvilEngine.Adapters.Tests.Support;
6
7public static class QuakeBoxRoom
8{
9 public const int Size = 64;
10 public const int LuxelsPerSide = Size / 16 + 1;
11 public const byte FullbrightIndex = 250;
12
13 public const string DefaultEntities = """
14 {
15 "classname" "worldspawn"
16 "message" "Box Room"
17 }
18 // a start in the middle of the floor
19 {
20 "classname" "info_player_start"
21 "origin" "32 32 24"
22 "angle" "90"
23 }
24 """;
25
26 public static byte[] Palette()
27 {
28 var palette = new byte[768];
29 for (var i = 0; i < 256; i++)
30 {
31 palette[i * 3] = (byte)i;
32 palette[i * 3 + 1] = (byte)(255 - i);
33 palette[i * 3 + 2] = (byte)(i / 2);
34 }
35
36 return palette;
37 }
38
39 public static byte[] Colormap()
40 {
41 var colormap = new byte[64 * 256 + 1];
42 for (var row = 0; row < 64; row++)
43 for (var i = 0; i < 256; i++)
44 colormap[row * 256 + i] = i >= 224 ? (byte)i : (byte)((i + row) % 224);
45 colormap[^1] = 32;
46 return colormap;
47 }
48
49 public static byte[] Lit()
50 {
51 var lit = new byte[8 + LuxelsPerSide * LuxelsPerSide * 3];
52 "QLIT"u8.CopyTo(lit);
53 BinaryPrimitives.WriteInt32LittleEndian(lit.AsSpan(4), 1);
54 for (var i = 8; i < lit.Length; i += 3)
55 (lit[i], lit[i + 1], lit[i + 2]) = (200, 100, 50);
56 return lit;
57 }
58
59 public static byte[] Build(bool bsp2, string entities = DefaultEntities, string floor = "floor")
60 {
61 return new QuakeBspWriter(bsp2).Build(entities, floor);
62 }
63}
64
65public sealed class QuakeBspWriter(bool bsp2)
66{
67 private const int Size = QuakeBoxRoom.Size;
68
69 public byte[] Build(string entities, string floorTexture)
70 {
71 var lumps = new byte[15][];
72 lumps[0] = Encoding.Latin1.GetBytes(entities + "\0");
73 (Vector3 Normal, float Distance)[] planes =
74 [
75 (Vector3.UnitZ, 0), (Vector3.UnitZ, Size), (Vector3.UnitX, 0), (Vector3.UnitX, Size), (Vector3.UnitY, 0),
76 (Vector3.UnitY, Size)
77 ];
78 lumps[1] = Join(planes.Select(plane => Floats(plane.Normal.X, plane.Normal.Y, plane.Normal.Z, plane.Distance)
79 .Concat(Ints(0)).ToArray()));
80 lumps[2] = MipTex([(floorTexture, 16, 16), ("sky1", 32, 16)]);
81 Vector3[] vertices =
82 [
83 new(0, 0, 0), new(0, Size, 0), new(Size, Size, 0), new(Size, 0, 0),
84 new(0, 0, Size), new(Size, 0, Size), new(Size, Size, Size), new(0, Size, Size)
85 ];
86 lumps[3] = Join(vertices.Select(v => Floats(v.X, v.Y, v.Z)));
87 lumps[4] = [];
88 const int solid = -1;
89 const int empty = -2;
90 (int Plane, int Front, int Back, int FirstFace, int Faces)[] nodes =
91 [
92 (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),
93 (5, solid, empty, 0, 0)
94 ];
95 lumps[5] = Join(nodes.Select(node => bsp2
96 ? Ints(node.Plane, node.Front, node.Back).Concat(Floats(0, 0, 0, Size, Size, Size))
97 .Concat(Ints(node.FirstFace, node.Faces)).ToArray()
98 : Ints(node.Plane).Concat(Shorts(node.Front, node.Back, 0, 0, 0, Size, Size, Size))
99 .Concat(UShorts(node.FirstFace, node.Faces)).ToArray()));
100 lumps[6] = Join(
101 Floats(1, 0, 0, 0, 0, 1, 0, 0).Concat(Ints(0, 0)).ToArray(),
102 Floats(1, 0, 0, 0, 0, 1, 0, 0).Concat(Ints(1, 1)).ToArray());
103 (int Plane, bool Back, int FirstEdge, int TexInfo, uint Styles, int Light)[] faces =
104 [(0, false, 0, 0, 0xFFFFFF00, 0), (1, true, 4, 1, 0xFFFFFFFF, -1)];
105 lumps[7] = Join(faces.Select(face => bsp2
106 ? Ints(face.Plane, face.Back ? 1 : 0, face.FirstEdge, 4, face.TexInfo).Concat(UInts(face.Styles))
107 .Concat(Ints(face.Light)).ToArray()
108 : Shorts(face.Plane, face.Back ? 1 : 0).Concat(Ints(face.FirstEdge)).Concat(Shorts(4, face.TexInfo))
109 .Concat(UInts(face.Styles)).Concat(Ints(face.Light)).ToArray()));
110 lumps[8] = Enumerable.Range(0, QuakeBoxRoom.LuxelsPerSide * QuakeBoxRoom.LuxelsPerSide)
111 .Select(i => (byte)(64 + i)).ToArray();
112 lumps[9] = [];
113 (int Contents, int FirstMark, int Marks)[] leafs = [(-2, 0, 0), (-1, 0, 2)];
114 lumps[10] = Join(leafs.Select(leaf => bsp2
115 ? Ints(leaf.Contents, -1).Concat(Floats(0, 0, 0, Size, Size, Size)).Concat(Ints(leaf.FirstMark, leaf.Marks))
116 .Concat(new byte[4]).ToArray()
117 : Ints(leaf.Contents, -1).Concat(Shorts(0, 0, 0, Size, Size, Size))
118 .Concat(UShorts(leaf.FirstMark, leaf.Marks)).Concat(new byte[4]).ToArray()));
119 lumps[11] = bsp2 ? Ints(0, 1) : UShorts(0, 1);
120 (int A, int B)[] edges = [(0, 0), (0, 1), (1, 2), (2, 3), (3, 0), (4, 5), (5, 6), (6, 7), (7, 4)];
121 lumps[12] = Join(edges.Select(edge => bsp2 ? Ints(edge.A, edge.B) : UShorts(edge.A, edge.B)));
122 lumps[13] = Ints(1, 2, 3, 4, 5, 6, 7, 8);
123 lumps[14] = Floats(0, 0, 0, Size, Size, Size, 0, 0, 0).Concat(Ints(0, 0, 0, 0, 1, 0, 2)).ToArray();
124
125 var header = 4 + 15 * 8;
126 var output = new List<byte>();
127 output.AddRange(bsp2 ? "BSP2"u8.ToArray() : Ints(29));
128 var offset = header;
129 foreach (var lump in lumps)
130 {
131 output.AddRange(Ints(offset, lump.Length));
132 offset += (lump.Length + 3) & ~3;
133 }
134
135 foreach (var lump in lumps)
136 {
137 output.AddRange(lump);
138 output.AddRange(new byte[((lump.Length + 3) & ~3) - lump.Length]);
139 }
140
141 return [.. output];
142 }
143
144 private static byte[] MipTex(IReadOnlyList<(string Name, int Width, int Height)> textures)
145 {
146 var directory = Ints(textures.Count).ToList();
147 var bodies = new List<byte>();
148 var start = 4 + textures.Count * 4;
149 foreach (var (name, width, height) in textures)
150 {
151 directory.AddRange(Ints(start + bodies.Count));
152 var body = new List<byte>();
153 var nameBytes = new byte[16];
154 Encoding.ASCII.GetBytes(name).CopyTo(nameBytes, 0);
155 body.AddRange(nameBytes);
156 body.AddRange(Ints(width, height));
157 var level = 40;
158 var offsets = new int[4];
159 for (var i = 0; i < 4; i++)
160 {
161 offsets[i] = level;
162 level += (width >> i) * (height >> i);
163 }
164
165 body.AddRange(Ints(offsets));
166 for (var i = 0; i < 4; i++)
167 for (var p = 0; p < (width >> i) * (height >> i); p++)
168 body.Add(p == 0 && name != "sky1" ? QuakeBoxRoom.FullbrightIndex : (byte)(p % 200));
169 bodies.AddRange(body);
170 }
171
172 return [.. directory, .. bodies];
173 }
174
175 private static byte[] Join(params IEnumerable<byte[]> parts)
176 {
177 return parts.SelectMany(part => part).ToArray();
178 }
179
180 private static byte[] Ints(params int[] values)
181 {
182 var bytes = new byte[values.Length * 4];
183 for (var i = 0; i < values.Length; i++)
184 BinaryPrimitives.WriteInt32LittleEndian(bytes.AsSpan(i * 4), values[i]);
185 return bytes;
186 }
187
188 private static byte[] UInts(params uint[] values)
189 {
190 var bytes = new byte[values.Length * 4];
191 for (var i = 0; i < values.Length; i++)
192 BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(i * 4), values[i]);
193 return bytes;
194 }
195
196 private static byte[] Shorts(params int[] values)
197 {
198 var bytes = new byte[values.Length * 2];
199 for (var i = 0; i < values.Length; i++)
200 BinaryPrimitives.WriteInt16LittleEndian(bytes.AsSpan(i * 2), (short)values[i]);
201 return bytes;
202 }
203
204 private static byte[] UShorts(params int[] values)
205 {
206 var bytes = new byte[values.Length * 2];
207 for (var i = 0; i < values.Length; i++)
208 BinaryPrimitives.WriteUInt16LittleEndian(bytes.AsSpan(i * 2), (ushort)values[i]);
209 return bytes;
210 }
211
212 private static byte[] Floats(params float[] values)
213 {
214 var bytes = new byte[values.Length * 4];
215 for (var i = 0; i < values.Length; i++)
216 BinaryPrimitives.WriteSingleLittleEndian(bytes.AsSpan(i * 4), values[i]);
217 return bytes;
218 }
219}