Skip to content
0.2.0-dev Download ZIP
310 lines ·12.7 KB Raw
1using System.Numerics;
2using System.Text;
3using EvilEngine.Adapters.Source.Maps;
4
5namespace EvilEngine.Adapters.Tests.Support;
6
7internal static class BoxMap
8{
9 public const int Luxels = 9 * 9;
10 public const string Floor = "maps/box/dev/dev_floor_0_0_0";
11
12 public static readonly Vector3[] Vertices =
13 [
14 new(-64, 64, 128), new(64, 64, 128), new(64, -64, 128), new(-64, -64, 128),
15 new(-64, 64, 0), new(64, 64, 0), new(64, -64, 0), new(-64, -64, 0),
16 new(-64, 64, 2000), new(64, 64, 2000), new(64, -64, 2000), new(-64, -64, 2000),
17 new(-8, 8, 16), new(8, 8, 16), new(8, -8, 16), new(-8, -8, 16)
18 ];
19
20 public static readonly (Vector3 Normal, float Distance)[] Planes =
21 [
22 (Vector3.UnitX, 64), (-Vector3.UnitX, 64), (Vector3.UnitY, 64), (-Vector3.UnitY, 64), (Vector3.UnitZ, 128),
23 (-Vector3.UnitZ, 0), (Vector3.UnitZ, 1000), (Vector3.UnitZ, 0), (Vector3.UnitZ, 2000), (Vector3.UnitX, 8),
24 (-Vector3.UnitX, 8), (Vector3.UnitY, 8), (-Vector3.UnitY, 8), (Vector3.UnitZ, 16)
25 ];
26
27 public static readonly (int Plane, int First, int TexInfo, int Disp, byte[] Styles, int Light)[] Faces =
28 [
29 (4, 0, 0, -1, [0, 32, 255, 255], 0),
30 (7, 4, 1, 0, [0, 255, 255, 255], 2 * Luxels * 4),
31 (4, 8, 2, -1, [255, 255, 255, 255], -1),
32 (8, 12, 3, -1, [255, 255, 255, 255], -1),
33 (13, 16, 3, -1, [255, 255, 255, 255], -1)
34 ];
35
36 public static readonly string Patch = """
37 patch
38 {
39 include "materials/dev/dev_floor.vmt"
40 insert { "$envmap" "maps/box/c0_0_0" }
41 }
42 """;
43
44 private static readonly (string, string)[][] Plain =
45 [
46 [("classname", "worldspawn"), ("skyname", "sky_day01_01")],
47 [("origin", "0 0 1"), ("angles", "0 90 0"), ("classname", "info_player_start")],
48 [
49 ("model", "*1"), ("origin", "100 0 0"), ("OnUser1", "a,Kill"), ("OnUser1", "b,Kill"),
50 ("classname", "func_brush")
51 ],
52 [("origin", "0 0 2008"), ("scale", "16"), ("classname", "sky_camera")],
53 [
54 ("model", "models/props_c17/oildrum001.mdl"), ("origin", "10 20 30"), ("angles", "0 45 0"),
55 ("classname", "prop_dynamic")
56 ],
57 [("origin", "0 0 64"), ("classname", "light")]
58 ];
59
60 private static readonly (string, string)[][] Painted =
61 [
62 [("classname", "worldspawn"), ("skyname", "painted")],
63 [
64 ("origin", "0 0 2008"), ("scale", "32"), ("fogenable", "1"), ("fogcolor", "255 128 0"),
65 ("fogcolor2", "0 0 255"),
66 ("fogblend", "1"), ("fogdir", "1 0 0"), ("fogstart", "100"), ("fogend", "1000"), ("fogmaxdensity", "0.5"),
67 ("classname", "sky_camera")
68 ],
69 [
70 ("topcolor", "0.1 0.2 0.3"), ("drawstars", "1"), ("starlayers", "2"), ("startexture", "skybox/clouds"),
71 ("starspeed", "0.03"), ("sunnormal", "0 0 1"), ("classname", "env_skypaint")
72 ],
73 [("angles", "0 90 0"), ("pitch", "-45"), ("use_angles", "1"), ("classname", "env_sun")],
74 [
75 ("angles", "0 90 0"), ("pitch", "-30"), ("_light", "255 255 255 200"), ("_ambient", "128 128 128 50"),
76 ("classname", "light_environment")
77 ],
78 [
79 ("fogenable", "1"), ("fogcolor", "10 20 30"), ("fogstart", "0"), ("fogend", "100"),
80 ("classname", "env_fog_controller")
81 ],
82 [("targetname", "other"), ("classname", "env_tonemap_controller")],
83 [("targetname", "Tonemap"), ("spawnflags", "1"), ("classname", "env_tonemap_controller")],
84 [
85 ("spawnflags", "1"), ("OnMapSpawn", "Tonemap\u001bSetBloomScale\u001b0.5\u001b1\u001b-1"),
86 ("OnMapSpawn", "Tonemap\u001bSetAutoExposureMax\u001b2.5\u001b0\u001b-1"),
87 ("OnNewGame", "tone*,SetAutoExposureMin,.3,0,-1"),
88 ("OnMapSpawn", "Tonemap\u001bSetBloomScale\u001b0\u001b0\u001b-1"),
89 ("OnMapSpawn", "other\u001bSetAutoExposureMin\u001b0.1\u001b0\u001b-1"),
90 ("OnMapSpawn", "Tonemap\u001bSetTonemapPercentTarget\u001b45\u001b0\u001b-1"),
91 ("OnUser1", "Tonemap\u001bSetTonemapRate\u001b9\u001b0\u001b-1"), ("classname", "logic_auto")
92 ]
93 ];
94
95 private static byte[] Lighting(byte blue)
96 {
97 var data = new List<byte>();
98 for (var style = 0; style < 2; style++)
99 for (var i = 0; i < Luxels; i++)
100 data.AddRange([(byte)i, (byte)(10 * style), blue, 0]);
101 for (var page = 0; page < 4; page++)
102 for (var i = 0; i < Luxels; i++)
103 data.AddRange([(byte)i, (byte)(40 * page + 1), blue, 0]);
104 return data.ToArray();
105 }
106
107 public static byte[] Write(int propsVersion = 6, bool painted = false)
108 {
109 var names = new[] { Floor, "NATURE/BLENDGRASS", "TOOLS/TOOLSSKYBOX" };
110 var strings = new List<byte>();
111 var offsets = new List<int>();
112 foreach (var name in names)
113 {
114 offsets.Add(strings.Count);
115 strings.AddRange([.. Encoding.ASCII.GetBytes(name), 0]);
116 }
117
118 var props = new MemoryStream();
119 using (var w = new BinaryWriter(props))
120 {
121 w.Write(1);
122 var model = new byte[128];
123 Encoding.ASCII.GetBytes("models/props_c17/oildrum001.mdl").CopyTo(model, 0);
124 w.Write(model);
125 w.Write(2);
126 w.Write((ushort)0);
127 w.Write((ushort)1);
128 w.Write(2);
129 foreach (var (origin, yaw, leaf) in new[]
130 { (new Vector3(32, 0, 0), 90f, 0), (new Vector3(0, 0, 1500), 0f, 1) })
131 {
132 BspWriter.Vector(w, origin);
133 BspWriter.Vector(w, new Vector3(0, yaw, 0));
134 w.Write((ushort)0);
135 w.Write((ushort)leaf);
136 w.Write((ushort)1);
137 w.Write((byte)6);
138 w.Write((byte)0);
139 w.Write(0);
140 w.Write(0f);
141 w.Write(0f);
142 BspWriter.Vector(w, origin);
143 w.Write(1f);
144 w.Write((ushort)0);
145 w.Write((ushort)0);
146 }
147 }
148
149 var lzma = Convert.FromBase64String(UnityFsTests.TextLzma);
150
151 void Face(BinaryWriter w, (int Plane, int First, int TexInfo, int Disp, byte[] Styles, int Light) face)
152 {
153 w.Write((ushort)face.Plane);
154 w.Write((byte)0);
155 w.Write((byte)0);
156 w.Write(face.First);
157 w.Write((short)4);
158 w.Write((short)face.TexInfo);
159 w.Write((short)face.Disp);
160 w.Write((short)0);
161 w.Write(face.Styles);
162 w.Write(face.Light);
163 w.Write(0f);
164 w.Write(-4);
165 w.Write(-4);
166 w.Write(8);
167 w.Write(8);
168 w.Write(0);
169 w.Write((ushort)0);
170 w.Write((ushort)0);
171 w.Write(0u);
172 }
173
174 return new BspWriter()
175 .Entities(painted ? Painted : Plain)
176 .Records(BspLump.Planes, Planes, (w, p) =>
177 {
178 BspWriter.Vector(w, p.Normal);
179 w.Write(p.Distance);
180 w.Write(0);
181 })
182 .Records(BspLump.TexData, new[] { (0, 128), (1, 512), (2, 64) }, (w, t) =>
183 {
184 BspWriter.Vector(w, Vector3.One);
185 w.Write(t.Item1);
186 w.Write(t.Item2);
187 w.Write(t.Item2);
188 w.Write(t.Item2);
189 w.Write(t.Item2);
190 })
191 .Lump(BspLump.TexDataStringData, strings.ToArray())
192 .Records(BspLump.TexDataStringTable, offsets, (w, o) => w.Write(o))
193 .Records(BspLump.TexInfo, new[]
194 {
195 (BspSurface.None, 0), (BspSurface.BumpLight, 1), (BspSurface.Sky | BspSurface.NoLight, 2),
196 (BspSurface.NoLight, 0)
197 }, (w, t) =>
198 {
199 foreach (var v in new[]
200 {
201 new Vector4(1, 0, 0, 0), new Vector4(0, -1, 0, 0), new Vector4(1 / 16f, 0, 0, 0),
202 new Vector4(0, -1 / 16f, 0, 0)
203 })
204 {
205 w.Write(v.X);
206 w.Write(v.Y);
207 w.Write(v.Z);
208 w.Write(v.W);
209 }
210
211 w.Write((int)t.Item1);
212 w.Write(t.Item2);
213 })
214 .Records(BspLump.Vertices, Vertices, BspWriter.Vector)
215 .Records(BspLump.Edges, Enumerable.Range(-1, 21), (w, e) =>
216 {
217 var f = e / 4;
218 var corners = f < 2 ? f * 4 : f == 2 ? 0 : (f - 1) * 4;
219 w.Write((ushort)(e < 0 ? 0 : corners + e % 4));
220 w.Write((ushort)(e < 0 ? 0 : corners + (e + 1) % 4));
221 })
222 .Records(BspLump.SurfEdges, Enumerable.Range(1, 20), (w, e) => w.Write(e))
223 .Records(BspLump.Faces, Faces, Face, 1)
224 .Records(BspLump.FacesHdr, Faces, Face, 1)
225 .Lump(BspLump.Lighting, Lighting(7), 1)
226 .Lump(BspLump.LightingHdr, Lighting(9), 1)
227 .Records(BspLump.Models, new[]
228 {
229 (new Vector3(-64, -64, 0), new Vector3(64, 64, 2000), 0, 0, 4),
230 (new Vector3(-8, -8, 0), new Vector3(8, 8, 16), -3, 4, 1)
231 }, (w, m) =>
232 {
233 BspWriter.Vector(w, m.Item1);
234 BspWriter.Vector(w, m.Item2);
235 BspWriter.Vector(w, Vector3.Zero);
236 w.Write(m.Item3);
237 w.Write(m.Item4);
238 w.Write(m.Item5);
239 })
240 .Records(BspLump.Nodes, [0], (w, _) =>
241 {
242 w.Write(6);
243 w.Write(-2);
244 w.Write(-1);
245 w.Write(new byte[20]);
246 })
247 .Records(BspLump.Leafs, new[] { (1, 0, 2, 0, 1), (2, 2, 1, 0, 0), (1, 0, 0, 1, 1) }, (w, l) =>
248 {
249 w.Write(0);
250 w.Write((short)0);
251 w.Write((ushort)l.Item1);
252 w.Write(new byte[12]);
253 w.Write((ushort)l.Item2);
254 w.Write((ushort)l.Item3);
255 w.Write((ushort)l.Item4);
256 w.Write((ushort)l.Item5);
257 w.Write((short)-1);
258 w.Write((short)0);
259 }, 1)
260 .Records(BspLump.LeafFaces, new ushort[] { 0, 2, 3 }, (w, f) => w.Write(f))
261 .Records(BspLump.LeafBrushes, new ushort[] { 0, 1 }, (w, b) => w.Write(b))
262 .Records(BspLump.Brushes,
263 new[] { (0, 7, BspContents.Solid), (7, 6, BspContents.Water | BspContents.Current0) },
264 (w, b) =>
265 {
266 w.Write(b.Item1);
267 w.Write(b.Item2);
268 w.Write((int)b.Item3);
269 })
270 .Records(BspLump.BrushSides, new[]
271 {
272 (0, 0, false), (1, 0, false), (2, 0, false), (3, 0, false), (4, 0, false), (5, 0, false), (0, -1, true),
273 (9, 0, false), (10, 0, false), (11, 0, false), (12, 0, false), (13, 2, false), (5, 0, false)
274 }, (w, s) =>
275 {
276 w.Write((ushort)s.Item1);
277 w.Write((short)s.Item2);
278 w.Write((short)-1);
279 w.Write((byte)(s.Item3 ? 1 : 0));
280 w.Write((byte)0);
281 })
282 .Records(BspLump.DispInfo, [0], (w, _) =>
283 {
284 BspWriter.Vector(w, new Vector3(-64, -64, 0));
285 w.Write(0);
286 w.Write(0);
287 w.Write(2);
288 w.Write(0);
289 w.Write(0f);
290 w.Write(1);
291 w.Write((ushort)1);
292 w.Write(new byte[176 - 38]);
293 })
294 .Records(BspLump.DispVerts, Enumerable.Range(0, 25), (w, i) =>
295 {
296 BspWriter.Vector(w, Vector3.UnitZ);
297 w.Write(i / 5 * 4f);
298 w.Write(i % 5 * 60f);
299 })
300 .Records(BspLump.VertNormals, [Vector3.UnitZ], BspWriter.Vector)
301 .Records(BspLump.VertNormalIndices, Enumerable.Repeat((ushort)0, 20), (w, i) => w.Write(i))
302 .Lump(BspLump.Pakfile, BspWriter.Zip(
303 ($"materials/{Floor}.vmt", Encoding.ASCII.GetBytes(Patch), 0),
304 ("materials/maps/box/c0_0_0.vtf", Encoding.ASCII.GetBytes("not really a vtf"), 8),
305 ("Scripts\\Notes.txt", BspWriter.ZipLzma(lzma, UnityFsTests.Text().Length), 14)))
306 .GameLump("sprp", (ushort)propsVersion, props.ToArray())
307 .GameLump("zzzz", 1, BspWriter.ValveLzma(lzma, UnityFsTests.Text().Length), true)
308 .Write();
309 }
310}