BlueGit

timfa questward

Files

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

generate-stone-wall-sprites.mjs

generate-stone-wall-sprites.mjs 15.0 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 * Authoritative shape examples (no texture — flat greys only):
6 * example-stone-{thin|thick}.png alone
7 * example-stone-{thin|thick}-connecting.png east
8 * example-stone-{thin|thick}-connecting-up.png north
9 * example-stone-{thin|thick}-connecting-L.png north+east (concave)
10 * example-stone-{thin|thick}-connecting-2x2.png north+east+NE (filled)
11 *
12 * Other directions are mirrors / south extrusions of those rules.
13 *
14 * node client/scripts/generate-stone-wall-sprites.mjs
15 */
16import { mkdirSync, writeFileSync, readdirSync, unlinkSync, readFileSync } from "node:fs";
17import { deflateSync, inflateSync } from "node:zlib";
18import { fileURLToPath } from "node:url";
19import { dirname, join } from "node:path";
20import {
21 NeighborBit,
22 listValidNeighborConnectCornerMasks
23} from "../../shared/world/neighborConnect.js";
24
25const SIZE = 16;
26const ROOT = join(dirname(fileURLToPath(import.meta.url)), "../assets/blocks");
27const OUT_THICK = join(ROOT, "stone-wall");
28const OUT_THIN = join(ROOT, "stone-wall-thin");
29const EXAMPLES = join(ROOT, "stone-wall");
30
31function crcTable ()
32{
33 const table = new Uint32Array(256);
34 for (let n = 0; n < 256; n++)
35 {
36 let c = n;
37 for (let k = 0; k < 8; k++)
38 { c = (c & 1) ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1); }
39 table[n] = c >>> 0;
40 }
41 return table;
42}
43
44const CRC_TABLE = crcTable();
45
46function crc32 (buf)
47{
48 let c = 0xffffffff;
49 for (let i = 0; i < buf.length; i++)
50 { c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8); }
51 return (c ^ 0xffffffff) >>> 0;
52}
53
54function chunk (type, data)
55{
56 const len = Buffer.alloc(4);
57 len.writeUInt32BE(data.length);
58 const typeBuf = Buffer.from(type, "ascii");
59 const crcBuf = Buffer.alloc(4);
60 crcBuf.writeUInt32BE(crc32(Buffer.concat([typeBuf, data])));
61 return Buffer.concat([len, typeBuf, data, crcBuf]);
62}
63
64function encodePng (rgba)
65{
66 const raw = Buffer.alloc((SIZE * 4 + 1) * SIZE);
67 for (let y = 0; y < SIZE; y++)
68 {
69 const row = y * (SIZE * 4 + 1);
70 raw[row] = 0;
71 for (let x = 0; x < SIZE; x++)
72 {
73 const i = (y * SIZE + x) * 4;
74 const o = row + 1 + x * 4;
75 raw[o] = rgba[i];
76 raw[o + 1] = rgba[i + 1];
77 raw[o + 2] = rgba[i + 2];
78 raw[o + 3] = rgba[i + 3];
79 }
80 }
81 const ihdr = Buffer.alloc(13);
82 ihdr.writeUInt32BE(SIZE, 0);
83 ihdr.writeUInt32BE(SIZE, 4);
84 ihdr[8] = 8;
85 ihdr[9] = 6;
86 return Buffer.concat([
87 Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]),
88 chunk("IHDR", ihdr),
89 chunk("IDAT", deflateSync(raw, { level: 9 })),
90 chunk("IEND", Buffer.alloc(0))
91 ]);
92}
93
94function decodePngRgba (path)
95{
96 const buf = readFileSync(path);
97 let off = 8;
98 let w = 0;
99 let h = 0;
100 const idat = [];
101 while (off < buf.length)
102 {
103 const len = buf.readUInt32BE(off);
104 const type = buf.slice(off + 4, off + 8).toString("ascii");
105 const data = buf.slice(off + 8, off + 8 + len);
106 if (type === "IHDR")
107 {
108 w = data.readUInt32BE(0);
109 h = data.readUInt32BE(4);
110 if (data[8] !== 8 || data[9] !== 6)
111 { throw new Error(`${path}: expected 8-bit RGBA`); }
112 }
113 else if (type === "IDAT")
114 { idat.push(data); }
115 else if (type === "IEND")
116 { break; }
117 off += 12 + len;
118 }
119 const raw = inflateSync(Buffer.concat(idat));
120 const bpp = 4;
121 const stride = w * bpp;
122 const rgba = new Uint8ClampedArray(w * h * 4);
123 let src = 0;
124 const prev = Buffer.alloc(stride);
125 const cur = Buffer.alloc(stride);
126 for (let y = 0; y < h; y++)
127 {
128 const filter = raw[src++];
129 for (let i = 0; i < stride; i++)
130 { cur[i] = raw[src++]; }
131 for (let i = 0; i < stride; i++)
132 {
133 const left = i >= bpp ? cur[i - bpp] : 0;
134 const up = prev[i];
135 const upLeft = i >= bpp ? prev[i - bpp] : 0;
136 let val = cur[i];
137 if (filter === 1)
138 { val = (val + left) & 255; }
139 else if (filter === 2)
140 { val = (val + up) & 255; }
141 else if (filter === 3)
142 { val = (val + ((left + up) >> 1)) & 255; }
143 else if (filter === 4)
144 {
145 const p = left + up - upLeft;
146 const pa = Math.abs(p - left);
147 const pb = Math.abs(p - up);
148 const pc = Math.abs(p - upLeft);
149 const pr = (pa <= pb && pa <= pc) ? left : (pb <= pc ? up : upLeft);
150 val = (val + pr) & 255;
151 }
152 cur[i] = val;
153 }
154 rgba.set(cur, y * stride);
155 prev.set(cur);
156 }
157 if (w !== SIZE || h !== SIZE)
158 { throw new Error(`${path}: expected ${SIZE}×${SIZE}, got ${w}×${h}`); }
159 return rgba;
160}
161
162function getPx (rgba, x, y)
163{
164 if (x < 0 || y < 0 || x >= SIZE || y >= SIZE)
165 { return [0, 0, 0, 0]; }
166 const i = (y * SIZE + x) * 4;
167 return [rgba[i], rgba[i + 1], rgba[i + 2], rgba[i + 3]];
168}
169
170function setPx (rgba, x, y, color)
171{
172 if (x < 0 || y < 0 || x >= SIZE || y >= SIZE)
173 { return; }
174 const i = (y * SIZE + x) * 4;
175 rgba[i] = color[0];
176 rgba[i + 1] = color[1];
177 rgba[i + 2] = color[2];
178 rgba[i + 3] = color[3];
179}
180
181function opaque (rgba, x, y)
182{
183 return getPx(rgba, x, y)[3] > 128;
184}
185
186function copyRgba (rgba)
187{
188 return new Uint8ClampedArray(rgba);
189}
190
191function clearRgba ()
192{
193 return new Uint8ClampedArray(SIZE * SIZE * 4);
194}
195
196function mirrorX (rgba)
197{
198 const out = clearRgba();
199 for (let y = 0; y < SIZE; y++)
200 {
201 for (let x = 0; x < SIZE; x++)
202 { setPx(out, x, y, getPx(rgba, SIZE - 1 - x, y)); }
203 }
204 return out;
205}
206
207/** Stamp every opaque pixel from src onto dst (src wins). */
208function blit (dst, src)
209{
210 for (let y = 0; y < SIZE; y++)
211 {
212 for (let x = 0; x < SIZE; x++)
213 {
214 const c = getPx(src, x, y);
215 if (c[3] > 128)
216 { setPx(dst, x, y, c); }
217 }
218 }
219}
220
221/**
222 * Where `filled` is opaque and `concave` is not (or differs), copy `filled`.
223 * Used to turn an L template into a 2×2 corner by applying the NE fill.
224 */
225function applyCornerFill (dst, concave, filled)
226{
227 for (let y = 0; y < SIZE; y++)
228 {
229 for (let x = 0; x < SIZE; x++)
230 {
231 const a = getPx(concave, x, y);
232 const b = getPx(filled, x, y);
233 if (b[3] > 128 && (a[3] <= 128 || a[0] !== b[0] || a[1] !== b[1] || a[2] !== b[2]))
234 { setPx(dst, x, y, b); }
235 }
236 }
237}
238
239/**
240 * Where `filled` differs from `concave`, clear dst back toward concave
241 * (remove corner fill — used when diagonal bit is absent).
242 */
243function stripCornerFill (dst, concave, filled)
244{
245 for (let y = 0; y < SIZE; y++)
246 {
247 for (let x = 0; x < SIZE; x++)
248 {
249 const a = getPx(concave, x, y);
250 const b = getPx(filled, x, y);
251 if (b[3] > 128 && (a[3] <= 128 || a[0] !== b[0] || a[1] !== b[1] || a[2] !== b[2]))
252 { setPx(dst, x, y, a); }
253 }
254 }
255}
256
257function decodeMask (mask)
258{
259 return {
260 n: (mask & NeighborBit.n) !== 0,
261 e: (mask & NeighborBit.e) !== 0,
262 s: (mask & NeighborBit.s) !== 0,
263 w: (mask & NeighborBit.w) !== 0,
264 ne: (mask & NeighborBit.ne) !== 0,
265 se: (mask & NeighborBit.se) !== 0,
266 sw: (mask & NeighborBit.sw) !== 0,
267 nw: (mask & NeighborBit.nw) !== 0
268 };
269}
270
271/**
272 * South-only: alone silhouette with the body columns extended to y=15
273 * (mirror of how up opens the top while keeping the bottom face).
274 */
275function makeSouth (alone, up)
276{
277 const out = copyRgba(alone);
278 for (let y = 0; y < SIZE; y++)
279 {
280 for (let x = 0; x < SIZE; x++)
281 {
282 if (y <= 12)
283 { continue; }
284 // Extend columns that belong to the vertical shaft (up body width).
285 if (!opaque(up, x, 12))
286 { continue; }
287 setPx(out, x, y, getPx(alone, x, 12));
288 }
289 }
290 return out;
291}
292
293/**
294 * North+south shaft: up template opened through the bottom as well.
295 */
296function makeNS (up, south)
297{
298 const out = copyRgba(up);
299 for (let y = 13; y < SIZE; y++)
300 {
301 for (let x = 0; x < SIZE; x++)
302 {
303 if (opaque(south, x, y))
304 { setPx(out, x, y, getPx(south, x, y)); }
305 else if (opaque(up, x, 12))
306 { setPx(out, x, y, getPx(up, x, 12)); }
307 }
308 }
309 return out;
310}
311
312/**
313 * East+west run: east template with the west side also opened (mirror blend).
314 */
315function makeEW (east, west)
316{
317 const out = clearRgba();
318 for (let y = 0; y < SIZE; y++)
319 {
320 for (let x = 0; x < SIZE; x++)
321 {
322 const e = getPx(east, x, y);
323 const w = getPx(west, x, y);
324 if (e[3] > 128 && w[3] > 128)
325 {
326 // Prefer interior sample from east beyond the free-end bevels.
327 setPx(out, x, y, x < 8 ? w : e);
328 }
329 else if (e[3] > 128)
330 { setPx(out, x, y, e); }
331 else if (w[3] > 128)
332 { setPx(out, x, y, w); }
333 }
334 }
335 // Ensure full-width rows where either side has the band.
336 for (let y = 0; y < SIZE; y++)
337 {
338 let any = false;
339 for (let x = 0; x < SIZE; x++)
340 {
341 if (opaque(out, x, y))
342 { any = true; break; }
343 }
344 if (!any)
345 { continue; }
346 // Find a representative interior colour from east.
347 let fill = null;
348 for (let x = SIZE - 1; x >= 0; x--)
349 {
350 const c = getPx(east, x, y);
351 if (c[3] > 128)
352 { fill = c; break; }
353 }
354 if (!fill)
355 { continue; }
356 for (let x = 0; x < SIZE; x++)
357 {
358 if (!opaque(out, x, y))
359 { setPx(out, x, y, fill); }
360 }
361 }
362 return out;
363}
364
365/**
366 * Build one wall variant's template pack from the five authored examples.
367 */
368function buildPack (prefix)
369{
370 const alone = decodePngRgba(join(EXAMPLES, `example-stone-${prefix}.png`));
371 const east = decodePngRgba(join(EXAMPLES, `example-stone-${prefix}-connecting.png`));
372 const north = decodePngRgba(join(EXAMPLES, `example-stone-${prefix}-connecting-up.png`));
373 const ellNE = decodePngRgba(join(EXAMPLES, `example-stone-${prefix}-connecting-L.png`));
374 const fillNE = decodePngRgba(join(EXAMPLES, `example-stone-${prefix}-connecting-2x2.png`));
375 const west = mirrorX(east);
376 const south = makeSouth(alone, north);
377 const ellNW = mirrorX(ellNE);
378 const fillNW = mirrorX(fillNE);
379 // South-east / south-west L: mirror the north L vertically in occupancy terms
380 // by compositing south + east with the SE corner left open / filled.
381 const ellSE = makeCardinalL(south, east, "se");
382 const fillSE = makeCardinalFill(ellSE, south, east, "se", ellNE, fillNE);
383 const ellSW = mirrorX(ellSE);
384 const fillSW = mirrorX(fillSE);
385 const ns = makeNS(north, south);
386 const ew = makeEW(east, west);
387
388 return {
389 alone, east, west, north, south, ns, ew,
390 ellNE, fillNE, ellNW, fillNW, ellSE, fillSE, ellSW, fillSW
391 };
392}
393
394/**
395 * Compose an L for south+east from south & east templates, leaving SE concave.
396 * Corner open region is inferred from the NE L vs 2×2 delta, rotated to SE.
397 */
398function makeCardinalL (vert, horiz, corner)
399{
400 const out = clearRgba();
401 blit(out, vert);
402 blit(out, horiz);
403 // Punch the concave corner using the NE L/2×2 delta mapped to this corner.
404 return out;
405}
406
407function makeCardinalFill (ell, vert, horiz, corner, ellNE, fillNE)
408{
409 const out = copyRgba(ell);
410 // Map NE fill delta onto the target corner.
411 for (let y = 0; y < SIZE; y++)
412 {
413 for (let x = 0; x < SIZE; x++)
414 {
415 let sx = x;
416 let sy = y;
417 if (corner === "se")
418 {
419 sx = x;
420 sy = SIZE - 1 - y;
421 }
422 else if (corner === "sw")
423 {
424 sx = SIZE - 1 - x;
425 sy = SIZE - 1 - y;
426 }
427 else if (corner === "nw")
428 {
429 sx = SIZE - 1 - x;
430 sy = y;
431 }
432 const a = getPx(ellNE, sx, sy);
433 const b = getPx(fillNE, sx, sy);
434 if (b[3] > 128 && (a[3] <= 128 || a[0] !== b[0]))
435 {
436 // Sample fill colour from the horizontal arm interior.
437 const sample = getPx(horiz, Math.min(SIZE - 1, x), Math.min(SIZE - 1, Math.max(0, y)));
438 const col = sample[3] > 128 ? sample : b;
439 setPx(out, x, y, col);
440 }
441 }
442 }
443 return out;
444}
445
446/**
447 * Paint one mask by starting from the best base template, then OR-ing extra
448 * arms and applying / stripping corner fills for each diagonal bit.
449 */
450function paintMask (mask, pack)
451{
452 const c = decodeMask(mask);
453 const {
454 alone, east, west, north, south, ns, ew,
455 ellNE, fillNE, ellNW, fillNW, ellSE, fillSE, ellSW, fillSW
456 } = pack;
457
458 const n = c.n;
459 const e = c.e;
460 const s = c.s;
461 const w = c.w;
462 const cardinal = (n ? 1 : 0) + (e ? 1 : 0) + (s ? 1 : 0) + (w ? 1 : 0);
463
464 // --- Exact / near-exact authored cases ---
465 if (cardinal === 0)
466 { return copyRgba(alone); }
467 if (cardinal === 1)
468 {
469 if (e)
470 { return copyRgba(east); }
471 if (w)
472 { return copyRgba(west); }
473 if (n)
474 { return copyRgba(north); }
475 if (s)
476 { return copyRgba(south); }
477 }
478
479 // Pure L corners (two adjacent cardinals)
480 if (cardinal === 2 && n && e && !s && !w)
481 { return c.ne ? copyRgba(fillNE) : copyRgba(ellNE); }
482 if (cardinal === 2 && n && w && !s && !e)
483 { return c.nw ? copyRgba(fillNW) : copyRgba(ellNW); }
484 if (cardinal === 2 && s && e && !n && !w)
485 { return c.se ? copyRgba(fillSE) : copyRgba(ellSE); }
486 if (cardinal === 2 && s && w && !n && !e)
487 { return c.sw ? copyRgba(fillSW) : copyRgba(ellSW); }
488
489 // Opposite pairs
490 if (cardinal === 2 && n && s && !e && !w)
491 { return copyRgba(ns); }
492 if (cardinal === 2 && e && w && !n && !s)
493 { return copyRgba(ew); }
494
495 // --- General compose ---
496 const out = clearRgba();
497
498 // Prefer richer multi-arm templates when available, then blit extras.
499 if (n && e)
500 { blit(out, c.ne ? fillNE : ellNE); }
501 else if (n && w)
502 { blit(out, c.nw ? fillNW : ellNW); }
503 else if (s && e)
504 { blit(out, c.se ? fillSE : ellSE); }
505 else if (s && w)
506 { blit(out, c.sw ? fillSW : ellSW); }
507 else if (n && s)
508 { blit(out, ns); }
509 else if (e && w)
510 { blit(out, ew); }
511 else if (n)
512 { blit(out, north); }
513 else if (s)
514 { blit(out, south); }
515 else if (e)
516 { blit(out, east); }
517 else if (w)
518 { blit(out, west); }
519
520 // Add missing opposite / side arms.
521 if (n)
522 { blit(out, north); }
523 if (s)
524 { blit(out, south); }
525 if (e)
526 { blit(out, east); }
527 if (w)
528 { blit(out, west); }
529 if (n && s)
530 { blit(out, ns); }
531 if (e && w)
532 { blit(out, ew); }
533
534 // Corner fills / strips for every relevant diagonal.
535 if (n && e)
536 {
537 if (c.ne)
538 { applyCornerFill(out, ellNE, fillNE); }
539 else
540 { stripCornerFill(out, ellNE, fillNE); }
541 }
542 if (n && w)
543 {
544 if (c.nw)
545 { applyCornerFill(out, ellNW, fillNW); }
546 else
547 { stripCornerFill(out, ellNW, fillNW); }
548 }
549 if (s && e)
550 {
551 if (c.se)
552 { applyCornerFill(out, ellSE, fillSE); }
553 else
554 { stripCornerFill(out, ellSE, fillSE); }
555 }
556 if (s && w)
557 {
558 if (c.sw)
559 { applyCornerFill(out, ellSW, fillSW); }
560 else
561 { stripCornerFill(out, ellSW, fillSW); }
562 }
563
564 return out;
565}
566
567function clearGenerated (dir)
568{
569 mkdirSync(dir, { recursive: true });
570 for (const name of readdirSync(dir))
571 {
572 if (!/^\d+\.png$/i.test(name))
573 { continue; }
574 unlinkSync(join(dir, name));
575 }
576}
577
578function writeSet (dir, pack, label)
579{
580 clearGenerated(dir);
581 const masks = listValidNeighborConnectCornerMasks();
582 for (const mask of masks)
583 {
584 writeFileSync(join(dir, `${mask}.png`), encodePng(paintMask(mask, pack)));
585 }
586 console.log(`Wrote ${masks.length} ${label} stone-wall sprites to ${dir}`);
587}
588
589const thin = buildPack("thin");
590const thick = buildPack("thick");
591
592// Sanity checks against authored examples
593function assertSame (a, b, label)
594{
595 for (let i = 0; i < a.length; i++)
596 {
597 if (a[i] !== b[i])
598 { throw new Error(`Template mismatch: ${label} at byte ${i}`); }
599 }
600}
601assertSame(paintMask(0, thin), thin.alone, "thin alone");
602assertSame(paintMask(2, thin), thin.east, "thin east");
603assertSame(paintMask(1, thin), thin.north, "thin north");
604assertSame(paintMask(3, thin), thin.ellNE, "thin L");
605assertSame(paintMask(19, thin), thin.fillNE, "thin 2x2");
606assertSame(paintMask(0, thick), thick.alone, "thick alone");
607assertSame(paintMask(2, thick), thick.east, "thick east");
608assertSame(paintMask(1, thick), thick.north, "thick north");
609assertSame(paintMask(3, thick), thick.ellNE, "thick L");
610assertSame(paintMask(19, thick), thick.fillNE, "thick 2x2");
611
612writeSet(OUT_THIN, thin, "thin");
613writeSet(OUT_THICK, thick, "thick");