BlueGit

timfa questward

Files

questward / client / scripts / generate-stone-wall-sprites.mjs

generate-stone-wall-sprites.mjs

generate-stone-wall-sprites.mjs 9.5 KiB Download Raw
1/**
2 * Generate thin + thick stone-wall PNGs for blob neighbor-connect masks
3 * (cardinals + diagonal corners — 47 valid states).
4 *
5 * Shape references (silhouette / shading only, not final texture):
6 * client/assets/blocks/stone-wall/example-stone-thin.png
7 * client/assets/blocks/stone-wall/example-stone-thick.png
8 *
9 * Corner bits: when both adjacent cardinals join but the diagonal does not,
10 * paint an L-strip (concave). When the diagonal also joins, fill the quadrant
11 * so a solid 2×2 reads as one block.
12 *
13 * node client/scripts/generate-stone-wall-sprites.mjs
14 */
15import { mkdirSync, writeFileSync, readdirSync, unlinkSync } from "node:fs";
16import { deflateSync } from "node:zlib";
17import { fileURLToPath } from "node:url";
18import { dirname, join } from "node:path";
19import {
20 NeighborBit,
21 listValidNeighborConnectCornerMasks
22} from "../../shared/world/neighborConnect.js";
23
24const SIZE = 16;
25const ROOT = join(dirname(fileURLToPath(import.meta.url)), "../assets/blocks");
26const OUT_THICK = join(ROOT, "stone-wall");
27const OUT_THIN = join(ROOT, "stone-wall-thin");
28
29const OUTLINE = [0x40, 0x40, 0x40, 255];
30const TOP_HI = [0xaf, 0xaf, 0xaf, 255];
31const TOP = [0x80, 0x80, 0x80, 255];
32const TOP_LO = [0x73, 0x73, 0x73, 255];
33const FACE_L = [0x95, 0x95, 0x95, 255];
34const FACE = [0x73, 0x73, 0x73, 255];
35const FACE_R = [0x6c, 0x6c, 0x6c, 255];
36const FACE_FRONT = [0x54, 0x54, 0x54, 255];
37const HI_L = [0xb6, 0xb6, 0xb6, 255];
38const HI_R = [0x80, 0x80, 0x80, 255];
39const MORTAR = [0x58, 0x58, 0x5c, 255];
40
41/**
42 * Thin: compact top core (table-style strip-union) so L keeps a concave cutout;
43 * frustum face hangs below on free south edges. Alone matches example-stone-thin.
44 */
45const THIN = Object.freeze({
46 topY0: 2,
47 coreY0: 3,
48 coreY1: 7,
49 faceY1: 12,
50 coreX0: 5,
51 coreX1: 10,
52 baseX0: 3,
53 baseX1: 12,
54 fullTile: false
55});
56
57/** Thick: full-tile footprint; bevels react to free edges / missing diagonals. */
58const THICK = Object.freeze({
59 topY0: 1,
60 coreY0: 1,
61 coreY1: 8,
62 faceY1: 14,
63 coreX0: 3,
64 coreX1: 12,
65 baseX0: 0,
66 baseX1: 15,
67 fullTile: true
68});
69
70function crcTable ()
71{
72 const table = new Uint32Array(256);
73 for (let n = 0; n < 256; n++)
74 {
75 let c = n;
76 for (let k = 0; k < 8; k++)
77 { c = (c & 1) ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1); }
78 table[n] = c >>> 0;
79 }
80 return table;
81}
82
83const CRC_TABLE = crcTable();
84
85function crc32 (buf)
86{
87 let c = 0xffffffff;
88 for (let i = 0; i < buf.length; i++)
89 { c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8); }
90 return (c ^ 0xffffffff) >>> 0;
91}
92
93function chunk (type, data)
94{
95 const len = Buffer.alloc(4);
96 len.writeUInt32BE(data.length);
97 const typeBuf = Buffer.from(type, "ascii");
98 const crcBuf = Buffer.alloc(4);
99 crcBuf.writeUInt32BE(crc32(Buffer.concat([typeBuf, data])));
100 return Buffer.concat([len, typeBuf, data, crcBuf]);
101}
102
103function encodePng (rgba)
104{
105 const raw = Buffer.alloc((SIZE * 4 + 1) * SIZE);
106 for (let y = 0; y < SIZE; y++)
107 {
108 const row = y * (SIZE * 4 + 1);
109 raw[row] = 0;
110 for (let x = 0; x < SIZE; x++)
111 {
112 const i = (y * SIZE + x) * 4;
113 const o = row + 1 + x * 4;
114 raw[o] = rgba[i];
115 raw[o + 1] = rgba[i + 1];
116 raw[o + 2] = rgba[i + 2];
117 raw[o + 3] = rgba[i + 3];
118 }
119 }
120 const ihdr = Buffer.alloc(13);
121 ihdr.writeUInt32BE(SIZE, 0);
122 ihdr.writeUInt32BE(SIZE, 4);
123 ihdr[8] = 8;
124 ihdr[9] = 6;
125 return Buffer.concat([
126 Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]),
127 chunk("IHDR", ihdr),
128 chunk("IDAT", deflateSync(raw, { level: 9 })),
129 chunk("IEND", Buffer.alloc(0))
130 ]);
131}
132
133function setPx (rgba, x, y, color)
134{
135 if (x < 0 || y < 0 || x >= SIZE || y >= SIZE)
136 { return; }
137 const i = (y * SIZE + x) * 4;
138 rgba[i] = color[0];
139 rgba[i + 1] = color[1];
140 rgba[i + 2] = color[2];
141 rgba[i + 3] = color[3];
142}
143
144function hash2 (x, y)
145{
146 let n = (x * 374761393 + y * 668265263) | 0;
147 n = (n ^ (n >>> 13)) * 1274126177;
148 return (n ^ (n >>> 16)) >>> 0;
149}
150
151function cobbleShade (base, x, y)
152{
153 const h = hash2(x, y);
154 const speck = h & 7;
155 if (speck === 0)
156 { return MORTAR; }
157 if (speck === 1)
158 {
159 return [
160 Math.min(255, base[0] + 18),
161 Math.min(255, base[1] + 18),
162 Math.min(255, base[2] + 18),
163 255
164 ];
165 }
166 if (speck === 2)
167 {
168 return [
169 Math.max(0, base[0] - 14),
170 Math.max(0, base[1] - 14),
171 Math.max(0, base[2] - 14),
172 255
173 ];
174 }
175 return base;
176}
177
178function decodeMask (mask)
179{
180 return {
181 n: (mask & NeighborBit.n) !== 0,
182 e: (mask & NeighborBit.e) !== 0,
183 s: (mask & NeighborBit.s) !== 0,
184 w: (mask & NeighborBit.w) !== 0,
185 ne: (mask & NeighborBit.ne) !== 0,
186 se: (mask & NeighborBit.se) !== 0,
187 sw: (mask & NeighborBit.sw) !== 0,
188 nw: (mask & NeighborBit.nw) !== 0
189 };
190}
191
192function frustumX0X1 (y, profile)
193{
194 if (y <= 2)
195 { return { x0: 6, x1: 9 }; }
196 if (y <= 4)
197 { return { x0: 5, x1: 10 }; }
198 if (y <= 6)
199 { return { x0: 4, x1: 11 }; }
200 return { x0: profile.baseX0, x1: profile.baseX1 };
201}
202
203/** Top-plane strip-union + diagonal fills (no front skirt). */
204function inTopPlane (x, y, c, profile)
205{
206 const { coreX0, coreX1, coreY0, coreY1 } = profile;
207 const inCoreX = x >= coreX0 && x <= coreX1;
208 const inCoreY = y >= coreY0 && y <= coreY1;
209 const wantH = c.e || c.w;
210 const wantV = c.n || c.s;
211
212 if (inCoreX && inCoreY)
213 { return true; }
214
215 if (wantH && inCoreY)
216 {
217 const x0 = c.w ? 0 : coreX0;
218 const x1 = c.e ? SIZE - 1 : coreX1;
219 if (x >= x0 && x <= x1)
220 { return true; }
221 }
222
223 if (wantV && inCoreX)
224 {
225 const y0 = c.n ? 0 : coreY0;
226 const y1 = c.s ? SIZE - 1 : coreY1;
227 if (y >= y0 && y <= y1)
228 { return true; }
229 }
230
231 // Close concave L/T cutouts when the diagonal neighbour is present.
232 if (c.se && c.s && c.e && x > coreX1 && y > coreY1)
233 { return true; }
234 if (c.sw && c.s && c.w && x < coreX0 && y > coreY1)
235 { return true; }
236 if (c.ne && c.n && c.e && x > coreX1 && y < coreY0)
237 { return true; }
238 if (c.nw && c.n && c.w && x < coreX0 && y < coreY0)
239 { return true; }
240
241 return false;
242}
243
244function inWall (x, y, c, profile)
245{
246 if (profile.fullTile)
247 { return true; }
248
249 const alone = !c.n && !c.e && !c.s && !c.w;
250 if (alone)
251 {
252 if (y < profile.topY0 || y > profile.faceY1)
253 { return false; }
254 const { x0, x1 } = frustumX0X1(y, profile);
255 return x >= x0 && x <= x1;
256 }
257
258 if (inTopPlane(x, y, c, profile))
259 { return true; }
260
261 // Front-face skirt under top-plane pixels that do not continue south.
262 if (y > profile.coreY1 && y <= profile.faceY1)
263 {
264 if (!inTopPlane(x, profile.coreY1, c, profile))
265 { return false; }
266 // Vertical run into a southern neighbour: no skirt in the core column.
267 if (c.s && x >= profile.coreX0 && x <= profile.coreX1)
268 { return false; }
269 // Diagonal-filled SE mass continues as solid top — skirt only at very bottom
270 // of columns that are the south perimeter (no further south neighbour top).
271 if (c.s && ((c.se && c.e && x > profile.coreX1) || (c.sw && c.w && x < profile.coreX0)))
272 { return false; }
273 const { x0, x1 } = frustumX0X1(y, profile);
274 const skirtX0 = c.w ? 0 : x0;
275 const skirtX1 = c.e ? SIZE - 1 : x1;
276 if (x < skirtX0 || x > skirtX1)
277 { return false; }
278 return inTopPlane(x, profile.coreY1, c, profile);
279 }
280
281 return false;
282}
283
284function shadeAt (x, y, c, profile, occ)
285{
286 const n = occ(x, y - 1);
287 const e = occ(x + 1, y);
288 const s = occ(x, y + 1);
289 const w = occ(x - 1, y);
290
291 const openCount = (!n ? 1 : 0) + (!s ? 1 : 0) + (!e ? 1 : 0) + (!w ? 1 : 0);
292 if (openCount > 0)
293 {
294 const edgeHeavy = (!n && y <= (profile.fullTile ? 0 : profile.topY0))
295 || (!s && y >= (profile.fullTile ? SIZE - 1 : profile.faceY1))
296 || (!w && x <= (profile.fullTile ? 0 : profile.baseX0))
297 || (!e && x >= (profile.fullTile ? SIZE - 1 : profile.baseX1));
298 if (edgeHeavy || openCount >= 2)
299 { return OUTLINE; }
300 }
301
302 if (profile.fullTile)
303 {
304 const freeN = !c.n;
305 const freeS = !c.s;
306 const freeW = !c.w;
307 const freeE = !c.e;
308 const openNe = c.n && c.e && !c.ne;
309 const openSe = c.s && c.e && !c.se;
310 const openSw = c.s && c.w && !c.sw;
311 const openNw = c.n && c.w && !c.nw;
312
313 if (y === 0 || x === 0 || x === SIZE - 1 || y === SIZE - 1)
314 { return OUTLINE; }
315 if ((freeW && x <= 2) || (openNw && x <= 2 && y <= 2) || (openSw && x <= 2 && y >= SIZE - 3))
316 { return FACE_L; }
317 if ((freeE && x >= SIZE - 3) || (openNe && x >= SIZE - 3 && y <= 2) || (openSe && x >= SIZE - 3 && y >= SIZE - 3))
318 { return FACE_R; }
319 if (freeN && y === 1)
320 { return TOP_HI; }
321 if (y <= 8 && x >= 3 && x <= 12)
322 { return TOP; }
323 if (freeS || y >= 10)
324 { return FACE_FRONT; }
325 return TOP_LO;
326 }
327
328 if (y <= profile.coreY1)
329 {
330 if (!n)
331 { return OUTLINE; }
332 if (!w)
333 { return HI_L; }
334 if (!e)
335 { return HI_R; }
336 return TOP;
337 }
338
339 if (!s)
340 { return OUTLINE; }
341 if (!w)
342 { return HI_L; }
343 if (!e)
344 { return HI_R; }
345 return FACE;
346}
347
348function paintWall (mask, profile)
349{
350 const rgba = new Uint8Array(SIZE * SIZE * 4);
351 const c = decodeMask(mask);
352 const occ = (x, y) =>
353 {
354 if (x < 0)
355 { return c.w; }
356 if (x >= SIZE)
357 { return c.e; }
358 if (y < 0)
359 { return c.n; }
360 if (y >= SIZE)
361 { return c.s; }
362 return inWall(x, y, c, profile);
363 };
364
365 for (let y = 0; y < SIZE; y++)
366 {
367 for (let x = 0; x < SIZE; x++)
368 {
369 if (!occ(x, y))
370 { continue; }
371 setPx(rgba, x, y, cobbleShade(shadeAt(x, y, c, profile, occ), x, y));
372 }
373 }
374 return rgba;
375}
376
377function clearGenerated (dir)
378{
379 mkdirSync(dir, { recursive: true });
380 for (const name of readdirSync(dir))
381 {
382 if (!/^\d+\.png$/i.test(name))
383 { continue; }
384 unlinkSync(join(dir, name));
385 }
386}
387
388function writeSet (dir, profile, label)
389{
390 clearGenerated(dir);
391 const masks = listValidNeighborConnectCornerMasks();
392 for (const mask of masks)
393 {
394 writeFileSync(join(dir, `${mask}.png`), encodePng(paintWall(mask, profile)));
395 }
396 console.log(`Wrote ${masks.length} ${label} stone-wall sprites to ${dir}`);
397}
398
399writeSet(OUT_THICK, THICK, "thick");
400writeSet(OUT_THIN, THIN, "thin");