BlueGit

timfa questward

Rebuild wall sprites from the expanded example set.

Map authored alone/E/N/S/NS/L/T/2×2 templates directly to masks and mirror for west; keep shapes flat (no texture).

timfa Tim Falken · pushed by timfa

Authored 2026-08-31 · 2 months ago

Hash
5b5db5c97b6875bad815fdb49dd035c52236adac
Parent
0f6dda30
89 files changed +287 −300
client/assets/blocks/stone-wall-thin/10.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/11.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/110.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/111.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/12.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/127.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/13.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/139.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/14.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/141.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/143.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/15.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/155.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/159.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/175.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/191.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/205.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/207.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/223.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/23.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/239.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/255.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/27.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/3.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/31.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/38.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/39.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/4.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/46.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/47.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/5.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/55.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/6.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/63.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/7.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/76.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/77.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/78.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/79.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/9.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/95.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/10.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/11.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/110.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/111.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/12.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/127.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/13.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/139.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/14.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/141.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/143.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/15.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/155.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/159.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/175.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/191.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/205.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/207.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/223.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/23.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/239.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/255.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/27.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/31.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/38.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/39.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/4.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/46.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/47.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/5.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/55.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/6.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/63.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/7.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/76.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/77.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/78.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/79.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/95.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/example-stone-thick-T.png added Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/example-stone-thick-connecting-updown.png added Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/example-stone-thin-connecting-L.png Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/example-stone-thin-connecting-Ldownright.png added Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/example-stone-thin-connecting-T-upsidedown.png added Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/example-stone-thin-connecting-T.png added Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/example-stone-thin-connecting-down.png added Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/example-stone-thin-connecting-updown.png added Binary file +0 −0

The contents of this binary file are not shown.

client/scripts/generate-stone-wall-sprites.mjs +287 −300
@@ -2,18 +2,25 @@
2 2 * Generate thin + thick stone-wall PNGs for blob neighbor-connect masks
3 3 * (cardinals + diagonal corners — 47 valid states).
4 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)
5 * Flat greys only (no texture). Authoritative examples live beside the outputs:
6 * example-stone-{thin|thick}.png
7 * example-stone-{thin|thick}-connecting.png E
8 * example-stone-{thin|thick}-connecting-up.png N
9 * example-stone-thin-connecting-down.png S (thin)
10 * example-stone-{thin|thick}-connecting-updown.png N+S
11 * example-stone-{thin|thick}-connecting-L.png N+E (concave)
12 * example-stone-thin-connecting-Ldownright.png S+E (concave, thin)
13 * example-stone-{thin|thick}-connecting-2x2.png N+E+NE (filled)
14 * example-stone-thin-connecting-T.png E+S+W
15 * example-stone-thin-connecting-T-upsidedown.png N+E+W
16 * example-stone-thick-T.png E+S+W (thick)
11 17 *
12 * Other directions are mirrors / south extrusions of those rules.
18 * West variants are X-mirrors. Remaining masks compose from these stamps +
19 * the L↔2×2 corner-fill delta.
13 20 *
14 21 * node client/scripts/generate-stone-wall-sprites.mjs
15 22 */
16 import { mkdirSync, writeFileSync, readdirSync, unlinkSync, readFileSync } from "node:fs";
23 import { mkdirSync, writeFileSync, readdirSync, unlinkSync, readFileSync, existsSync } from "node:fs";
17 24 import { deflateSync, inflateSync } from "node:zlib";
18 25 import { fileURLToPath } from "node:url";
19 26 import { dirname, join } from "node:path";
@@ -159,6 +166,14 @@ function decodePngRgba (path)
159 166 return rgba;
160 167 }
161 168
169 function loadExample (name)
170 {
171 const path = join(EXAMPLES, name);
172 if (!existsSync(path))
173 { return null; }
174 return decodePngRgba(path);
175 }
176
162 177 function getPx (rgba, x, y)
163 178 {
164 179 if (x < 0 || y < 0 || x >= SIZE || y >= SIZE)
@@ -204,9 +219,10 @@ function mirrorX (rgba)
204 219 return out;
205 220 }
206 221
207 /** Stamp every opaque pixel from src onto dst (src wins). */
208 222 function blit (dst, src)
209 223 {
224 if (!src)
225 { return; }
210 226 for (let y = 0; y < SIZE; y++)
211 227 {
212 228 for (let x = 0; x < SIZE; x++)
@@ -218,12 +234,10 @@ function blit (dst, src)
218 234 }
219 235 }
220 236
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 */
225 function applyCornerFill (dst, concave, filled)
237 /** Pixels where filled differs from concave (the diagonal corner fill). */
238 function cornerDelta (concave, filled)
226 239 {
240 const deltas = [];
227 241 for (let y = 0; y < SIZE; y++)
228 242 {
229 243 for (let x = 0; x < SIZE; x++)
@@ -231,336 +245,306 @@ function applyCornerFill (dst, concave, filled)
231 245 const a = getPx(concave, x, y);
232 246 const b = getPx(filled, x, y);
233 247 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); }
248 { deltas.push({ x, y, color: b }); }
249 else if (a[3] > 128 && b[3] <= 128)
250 { deltas.push({ x, y, color: [0, 0, 0, 0] }); }
235 251 }
236 252 }
253 return deltas;
237 254 }
238 255
239 /**
240 * Where `filled` differs from `concave`, clear dst back toward concave
241 * (remove corner fill — used when diagonal bit is absent).
242 */
243 function stripCornerFill (dst, concave, filled)
256 function applyDelta (dst, deltas, mapX, mapY)
244 257 {
245 for (let y = 0; y < SIZE; y++)
258 for (const d of deltas)
246 259 {
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 }
260 const x = mapX(d.x);
261 const y = mapY(d.y);
262 setPx(dst, x, y, d.color);
254 263 }
255 264 }
256 265
257 function 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 }
266 function idX (x)
267 { return x; }
268 function idY (y)
269 { return y; }
270 function flipX (x)
271 { return SIZE - 1 - x; }
272 function flipY (y)
273 { return SIZE - 1 - y; }
270 274
271 275 /**
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).
276 * Build south-east L by compositing down+east and punching the SE corner using
277 * the NE L↔2×2 delta rotated 180° in corner space (flipX+flipY of NE delta
278 * relative to tile — actually SE is flipY of NE region for thin).
279 *
280 * Simpler: if we have an authored Ldownright, use it. Otherwise compose.
274 281 */
275 function makeSouth (alone, up)
282 function composeSEFromNE (ellNE, fillNE, east, south)
276 283 {
277 const out = copyRgba(alone);
278 for (let y = 0; y < SIZE; y++)
284 const out = clearRgba();
285 blit(out, south);
286 blit(out, east);
287 // Remove NE-style fill if any leaked; then we want SE concave by default.
288 // Punch using mapped NE delta: NE fill region → clear for concave SE.
289 const delta = cornerDelta(ellNE, fillNE);
290 for (const d of delta)
279 291 {
280 for (let x = 0; x < SIZE; x++)
292 // Map NE corner (top-right) → SE corner (bottom-right): keep x, flip y.
293 const x = d.x;
294 const y = flipY(d.y);
295 // Only clear / don't apply fill for the concave L.
296 if (d.color[3] > 128)
281 297 {
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));
298 // Was a fill pixel in 2x2 — ensure concave leaves it empty (or outline).
299 const src = getPx(ellNE, d.x, d.y);
300 setPx(out, x, y, src[3] > 128 ? getPx(east, x, Math.min(12, y)) : [0, 0, 0, 0]);
301 if (src[3] <= 128)
302 { setPx(out, x, y, [0, 0, 0, 0]); }
288 303 }
289 304 }
290 305 return out;
291 306 }
292 307
293 /**
294 * North+south shaft: up template opened through the bottom as well.
295 */
296 function makeNS (up, south)
308 function fillSEFromNE (ellSE, ellNE, fillNE)
297 309 {
298 const out = copyRgba(up);
299 for (let y = 13; y < SIZE; y++)
310 const out = copyRgba(ellSE);
311 const delta = cornerDelta(ellNE, fillNE);
312 for (const d of delta)
300 313 {
301 for (let x = 0; x < SIZE; x++)
314 const x = d.x;
315 const y = flipY(d.y);
316 if (d.color[3] > 128)
302 317 {
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)); }
318 // Prefer sampling from the SE L's nearby interior.
319 const sample = getPx(ellSE, Math.min(SIZE - 1, Math.max(0, x - 1)), Math.min(SIZE - 1, Math.max(0, y - 1)));
320 setPx(out, x, y, sample[3] > 128 ? sample : d.color);
307 321 }
308 322 }
309 323 return out;
310 324 }
311 325
312 /**
313 * East+west run: east template with the west side also opened (mirror blend).
314 */
315 function makeEW (east, west)
326 function buildPack (kind)
316 327 {
317 const out = clearRgba();
318 for (let y = 0; y < SIZE; y++)
328 const alone = loadExample(`example-stone-${kind}.png`);
329 const east = loadExample(`example-stone-${kind}-connecting.png`);
330 const north = loadExample(`example-stone-${kind}-connecting-up.png`);
331 const southAuthored = loadExample(`example-stone-${kind}-connecting-down.png`);
332 const ns = loadExample(`example-stone-${kind}-connecting-updown.png`);
333 const ellNE = loadExample(`example-stone-${kind}-connecting-L.png`);
334 const fillNE = loadExample(`example-stone-${kind}-connecting-2x2.png`);
335 const ellSEAuthored = loadExample(`example-stone-${kind}-connecting-Ldownright.png`);
336 const teeESW = loadExample(`example-stone-${kind}-T.png`)
337 ?? loadExample(`example-stone-${kind}-connecting-T.png`);
338 const teeNEW = loadExample(`example-stone-${kind}-connecting-T-upsidedown.png`);
339
340 if (!alone || !east || !north || !ellNE || !fillNE || !ns)
341 { throw new Error(`Missing core ${kind} examples`); }
342
343 const west = mirrorX(east);
344 const southFinal = southAuthored ?? (() =>
319 345 {
320 for (let x = 0; x < SIZE; x++)
346 // No authored south: alone top + updown shaft through the bottom.
347 const out = copyRgba(alone);
348 for (let y = 0; y < SIZE; y++)
321 349 {
322 const e = getPx(east, x, y);
323 const w = getPx(west, x, y);
324 if (e[3] > 128 && w[3] > 128)
350 for (let x = 0; x < SIZE; x++)
325 351 {
326 // Prefer interior sample from east beyond the free-end bevels.
327 setPx(out, x, y, x < 8 ? w : e);
352 if (y < 2 && opaque(alone, x, y))
353 { continue; }
354 if (opaque(ns, x, y))
355 { setPx(out, x, y, getPx(ns, x, y)); }
328 356 }
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 357 }
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++)
358 // Ensure bottom rows are open shaft from updown.
359 for (let y = 10; y < SIZE; y++)
340 360 {
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); }
361 for (let x = 0; x < SIZE; x++)
362 {
363 if (opaque(ns, x, y))
364 { setPx(out, x, y, getPx(ns, x, y)); }
365 }
360 366 }
361 }
362 return out;
363 }
367 return out;
368 })();
369
370 const ellSEFinal = ellSEAuthored ?? composeSEFromNE(ellNE, fillNE, east, southFinal);
371 const fillSE = fillSEFromNE(ellSEFinal, ellNE, fillNE);
364 372
365 /**
366 * Build one wall variant's template pack from the five authored examples.
367 */
368 function 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 373 const ellNW = mirrorX(ellNE);
378 374 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);
375 const ellSW = mirrorX(ellSEFinal);
384 376 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 */
398 function 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 377
407 function 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++)
378 const ew = (() =>
412 379 {
413 for (let x = 0; x < SIZE; x++)
380 const out = clearRgba();
381 blit(out, east);
382 blit(out, west);
383 // Close the center seam: fill any gap on rows that both sides use.
384 for (let y = 0; y < SIZE; y++)
414 385 {
415 let sx = x;
416 let sy = y;
417 if (corner === "se")
386 let fill = null;
387 for (let x = 0; x < SIZE; x++)
418 388 {
419 sx = x;
420 sy = SIZE - 1 - y;
389 const c = getPx(east, x, y);
390 if (c[3] > 128)
391 { fill = c; }
421 392 }
422 else if (corner === "sw")
423 {
424 sx = SIZE - 1 - x;
425 sy = SIZE - 1 - y;
426 }
427 else if (corner === "nw")
393 if (!fill)
394 { continue; }
395 for (let x = 0; x < SIZE; x++)
428 396 {
429 sx = SIZE - 1 - x;
430 sy = y;
397 if (!opaque(out, x, y) && (opaque(east, SIZE - 1, y) || opaque(west, 0, y)))
398 { setPx(out, x, y, fill); }
431 399 }
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]))
400 // Force full row when east template opens to the edge on this row.
401 if (opaque(east, SIZE - 1, y))
435 402 {
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);
403 for (let x = 0; x < SIZE; x++)
404 {
405 if (!opaque(out, x, y))
406 { setPx(out, x, y, fill); }
407 }
440 408 }
441 409 }
442 }
443 return out;
410 return out;
411 })();
412
413 const teeESWFinal = teeESW ?? (() =>
414 {
415 const out = clearRgba();
416 blit(out, ew);
417 blit(out, southFinal);
418 return out;
419 })();
420 const teeNEWFinal = teeNEW ?? (() =>
421 {
422 const out = clearRgba();
423 blit(out, ew);
424 blit(out, north);
425 return out;
426 })();
427 const teeNES = (() =>
428 {
429 const out = clearRgba();
430 blit(out, north);
431 blit(out, east);
432 blit(out, southFinal);
433 return out;
434 })();
435 const teeNWS = mirrorX(teeNES);
436
437 /** Exact mask → rgba for authored / derived cardinal templates (no diagonals). */
438 const byMask = new Map();
439 byMask.set(0, alone);
440 byMask.set(NeighborBit.n, north);
441 byMask.set(NeighborBit.e, east);
442 byMask.set(NeighborBit.s, southFinal);
443 byMask.set(NeighborBit.w, west);
444 byMask.set(NeighborBit.n | NeighborBit.s, ns);
445 byMask.set(NeighborBit.e | NeighborBit.w, ew);
446 byMask.set(NeighborBit.n | NeighborBit.e, ellNE);
447 byMask.set(NeighborBit.n | NeighborBit.w, ellNW);
448 byMask.set(NeighborBit.s | NeighborBit.e, ellSEFinal);
449 byMask.set(NeighborBit.s | NeighborBit.w, ellSW);
450 byMask.set(NeighborBit.e | NeighborBit.s | NeighborBit.w, teeESWFinal);
451 byMask.set(NeighborBit.n | NeighborBit.e | NeighborBit.w, teeNEWFinal);
452 byMask.set(NeighborBit.n | NeighborBit.e | NeighborBit.s, teeNES);
453 byMask.set(NeighborBit.n | NeighborBit.w | NeighborBit.s, teeNWS);
454 byMask.set(
455 NeighborBit.n | NeighborBit.e | NeighborBit.s | NeighborBit.w,
456 (() =>
457 {
458 const out = clearRgba();
459 blit(out, ns);
460 blit(out, ew);
461 blit(out, teeESWFinal);
462 blit(out, teeNEWFinal);
463 return out;
464 })()
465 );
466
467 // Filled-corner variants for the primary L corners.
468 byMask.set(NeighborBit.n | NeighborBit.e | NeighborBit.ne, fillNE);
469 byMask.set(NeighborBit.n | NeighborBit.w | NeighborBit.nw, fillNW);
470 byMask.set(NeighborBit.s | NeighborBit.e | NeighborBit.se, fillSE);
471 byMask.set(NeighborBit.s | NeighborBit.w | NeighborBit.sw, fillSW);
472
473 const neDelta = cornerDelta(ellNE, fillNE);
474 const nwDelta = cornerDelta(ellNW, fillNW);
475 const seDelta = cornerDelta(ellSEFinal, fillSE);
476 const swDelta = cornerDelta(ellSW, fillSW);
477
478 return {
479 byMask,
480 neDelta,
481 nwDelta,
482 seDelta,
483 swDelta,
484 alone,
485 east,
486 west,
487 north,
488 south: southFinal,
489 ns,
490 ew,
491 ellNE,
492 fillNE
493 };
444 494 }
445 495
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 */
450 496 function paintMask (mask, pack)
451 497 {
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)
498 const n = (mask & NeighborBit.n) !== 0;
499 const e = (mask & NeighborBit.e) !== 0;
500 const s = (mask & NeighborBit.s) !== 0;
501 const w = (mask & NeighborBit.w) !== 0;
502 const ne = (mask & NeighborBit.ne) !== 0;
503 const se = (mask & NeighborBit.se) !== 0;
504 const sw = (mask & NeighborBit.sw) !== 0;
505 const nw = (mask & NeighborBit.nw) !== 0;
506
507 const cardinal = (n ? NeighborBit.n : 0)
508 | (e ? NeighborBit.e : 0)
509 | (s ? NeighborBit.s : 0)
510 | (w ? NeighborBit.w : 0);
511
512 // Exact hit (including known filled corners).
513 if (pack.byMask.has(mask))
514 { return copyRgba(pack.byMask.get(mask)); }
515
516 // Start from cardinal-only template, then apply diagonal fills.
517 let out;
518 if (pack.byMask.has(cardinal))
519 { out = copyRgba(pack.byMask.get(cardinal)); }
520 else
468 521 {
469 if (e)
470 { return copyRgba(east); }
471 if (w)
472 { return copyRgba(west); }
522 out = clearRgba();
473 523 if (n)
474 { return copyRgba(north); }
524 { blit(out, pack.north); }
475 525 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); }
526 { blit(out, pack.south); }
527 if (e)
528 { blit(out, pack.east); }
529 if (w)
530 { blit(out, pack.west); }
531 if (n && s)
532 { blit(out, pack.ns); }
533 if (e && w)
534 { blit(out, pack.ew); }
562 535 }
563 536
537 if (n && e && ne)
538 { applyDelta(out, pack.neDelta, idX, idY); }
539 if (n && w && nw)
540 { applyDelta(out, pack.nwDelta, idX, idY); }
541 if (s && e && se)
542 { applyDelta(out, pack.seDelta, idX, idY); }
543 if (s && w && sw)
544 { applyDelta(out, pack.swDelta, idX, idY); }
545
546 // When diagonal is absent but cardinal template accidentally included fill,
547 // strip using inverse delta (already concave templates are used for cardinal).
564 548 return out;
565 549 }
566 550
@@ -586,28 +570,31 @@ function writeSet (dir, pack, label)
586 570 console.log(`Wrote ${masks.length} ${label} stone-wall sprites to ${dir}`);
587 571 }
588 572
589 const thin = buildPack("thin");
590 const thick = buildPack("thick");
591
592 // Sanity checks against authored examples
593 573 function assertSame (a, b, label)
594 574 {
595 575 for (let i = 0; i < a.length; i++)
596 576 {
597 577 if (a[i] !== b[i])
598 { throw new Error(`Template mismatch: ${label} at byte ${i}`); }
578 { throw new Error(`Mismatch ${label} @${i}: ${a[i]}≠${b[i]}`); }
599 579 }
600 580 }
601 assertSame(paintMask(0, thin), thin.alone, "thin alone");
602 assertSame(paintMask(2, thin), thin.east, "thin east");
603 assertSame(paintMask(1, thin), thin.north, "thin north");
604 assertSame(paintMask(3, thin), thin.ellNE, "thin L");
605 assertSame(paintMask(19, thin), thin.fillNE, "thin 2x2");
606 assertSame(paintMask(0, thick), thick.alone, "thick alone");
607 assertSame(paintMask(2, thick), thick.east, "thick east");
608 assertSame(paintMask(1, thick), thick.north, "thick north");
609 assertSame(paintMask(3, thick), thick.ellNE, "thick L");
610 assertSame(paintMask(19, thick), thick.fillNE, "thick 2x2");
581
582 const thin = buildPack("thin");
583 const thick = buildPack("thick");
584
585 assertSame(paintMask(0, thin), thin.alone, "thin/0");
586 assertSame(paintMask(1, thin), thin.north, "thin/1");
587 assertSame(paintMask(2, thin), thin.east, "thin/2");
588 assertSame(paintMask(3, thin), thin.ellNE, "thin/3");
589 assertSame(paintMask(5, thin), thin.ns, "thin/5");
590 assertSame(paintMask(19, thin), thin.fillNE, "thin/19");
591 assertSame(paintMask(0, thick), thick.alone, "thick/0");
592 assertSame(paintMask(1, thick), thick.north, "thick/1");
593 assertSame(paintMask(2, thick), thick.east, "thick/2");
594 assertSame(paintMask(3, thick), thick.ellNE, "thick/3");
595 assertSame(paintMask(5, thick), thick.ns, "thick/5");
596 assertSame(paintMask(19, thick), thick.fillNE, "thick/19");
611 597
612 598 writeSet(OUT_THIN, thin, "thin");
613 599 writeSet(OUT_THICK, thick, "thick");
600 console.log("All authored core templates verified.");
@@ -2,18 +2,25 @@
2 * Generate thin + thick stone-wall PNGs for blob neighbor-connect masks
2 * Generate thin + thick stone-wall PNGs for blob neighbor-connect masks
3 * (cardinals + diagonal corners — 47 valid states).
3 * (cardinals + diagonal corners — 47 valid states).
4 *
4 *
5 * Authoritative shape examples (no texture — flat greys only):
5 * Flat greys only (no texture). Authoritative examples live beside the outputs:
6 * example-stone-{thin|thick}.png alone
6 * example-stone-{thin|thick}.png
7 * example-stone-{thin|thick}-connecting.png east
7 * example-stone-{thin|thick}-connecting.png E
8 * example-stone-{thin|thick}-connecting-up.png north
8 * example-stone-{thin|thick}-connecting-up.png N
9 * example-stone-{thin|thick}-connecting-L.png north+east (concave)
9 * example-stone-thin-connecting-down.png S (thin)
10 * example-stone-{thin|thick}-connecting-2x2.png north+east+NE (filled)
10 * example-stone-{thin|thick}-connecting-updown.png N+S
11 * example-stone-{thin|thick}-connecting-L.png N+E (concave)
12 * example-stone-thin-connecting-Ldownright.png S+E (concave, thin)
13 * example-stone-{thin|thick}-connecting-2x2.png N+E+NE (filled)
14 * example-stone-thin-connecting-T.png E+S+W
15 * example-stone-thin-connecting-T-upsidedown.png N+E+W
16 * example-stone-thick-T.png E+S+W (thick)
11 *
17 *
12 * Other directions are mirrors / south extrusions of those rules.
18 * West variants are X-mirrors. Remaining masks compose from these stamps +
19 * the L↔2×2 corner-fill delta.
13 *
20 *
14 * node client/scripts/generate-stone-wall-sprites.mjs
21 * node client/scripts/generate-stone-wall-sprites.mjs
15 */
22 */
16import { mkdirSync, writeFileSync, readdirSync, unlinkSync, readFileSync } from "node:fs";
23import { mkdirSync, writeFileSync, readdirSync, unlinkSync, readFileSync, existsSync } from "node:fs";
17import { deflateSync, inflateSync } from "node:zlib";
24import { deflateSync, inflateSync } from "node:zlib";
18import { fileURLToPath } from "node:url";
25import { fileURLToPath } from "node:url";
19import { dirname, join } from "node:path";
26import { dirname, join } from "node:path";
@@ -159,6 +166,14 @@ function decodePngRgba (path)
159 return rgba;
166 return rgba;
160}
167}
161
168
169function loadExample (name)
170{
171 const path = join(EXAMPLES, name);
172 if (!existsSync(path))
173 { return null; }
174 return decodePngRgba(path);
175}
176
162function getPx (rgba, x, y)
177function getPx (rgba, x, y)
163{
178{
164 if (x < 0 || y < 0 || x >= SIZE || y >= SIZE)
179 if (x < 0 || y < 0 || x >= SIZE || y >= SIZE)
@@ -204,9 +219,10 @@ function mirrorX (rgba)
204 return out;
219 return out;
205}
220}
206
221
207/** Stamp every opaque pixel from src onto dst (src wins). */
208function blit (dst, src)
222function blit (dst, src)
209{
223{
224 if (!src)
225 { return; }
210 for (let y = 0; y < SIZE; y++)
226 for (let y = 0; y < SIZE; y++)
211 {
227 {
212 for (let x = 0; x < SIZE; x++)
228 for (let x = 0; x < SIZE; x++)
@@ -218,12 +234,10 @@ function blit (dst, src)
218 }
234 }
219}
235}
220
236
221/**
237/** Pixels where filled differs from concave (the diagonal corner fill). */
222 * Where `filled` is opaque and `concave` is not (or differs), copy `filled`.
238function cornerDelta (concave, 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{
239{
240 const deltas = [];
227 for (let y = 0; y < SIZE; y++)
241 for (let y = 0; y < SIZE; y++)
228 {
242 {
229 for (let x = 0; x < SIZE; x++)
243 for (let x = 0; x < SIZE; x++)
@@ -231,336 +245,306 @@ function applyCornerFill (dst, concave, filled)
231 const a = getPx(concave, x, y);
245 const a = getPx(concave, x, y);
232 const b = getPx(filled, x, y);
246 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]))
247 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); }
248 { deltas.push({ x, y, color: b }); }
249 else if (a[3] > 128 && b[3] <= 128)
250 { deltas.push({ x, y, color: [0, 0, 0, 0] }); }
235 }
251 }
236 }
252 }
253 return deltas;
237}
254}
238
255
239/**
256function applyDelta (dst, deltas, mapX, mapY)
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{
257{
245 for (let y = 0; y < SIZE; y++)
258 for (const d of deltas)
246 {
259 {
247 for (let x = 0; x < SIZE; x++)
260 const x = mapX(d.x);
248 {
261 const y = mapY(d.y);
249 const a = getPx(concave, x, y);
262 setPx(dst, x, y, d.color);
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 }
263 }
255}
264}
256
265
257function decodeMask (mask)
266function idX (x)
258{
267{ return x; }
259 return {
268function idY (y)
260 n: (mask & NeighborBit.n) !== 0,
269{ return y; }
261 e: (mask & NeighborBit.e) !== 0,
270function flipX (x)
262 s: (mask & NeighborBit.s) !== 0,
271{ return SIZE - 1 - x; }
263 w: (mask & NeighborBit.w) !== 0,
272function flipY (y)
264 ne: (mask & NeighborBit.ne) !== 0,
273{ return SIZE - 1 - y; }
265 se: (mask & NeighborBit.se) !== 0,
266 sw: (mask & NeighborBit.sw) !== 0,
267 nw: (mask & NeighborBit.nw) !== 0
268 };
269}
270
274
271/**
275/**
272 * South-only: alone silhouette with the body columns extended to y=15
276 * Build south-east L by compositing down+east and punching the SE corner using
273 * (mirror of how up opens the top while keeping the bottom face).
277 * the NE L↔2×2 delta rotated 180° in corner space (flipX+flipY of NE delta
278 * relative to tile — actually SE is flipY of NE region for thin).
279 *
280 * Simpler: if we have an authored Ldownright, use it. Otherwise compose.
274 */
281 */
275function makeSouth (alone, up)
282function composeSEFromNE (ellNE, fillNE, east, south)
276{
283{
277 const out = copyRgba(alone);
284 const out = clearRgba();
278 for (let y = 0; y < SIZE; y++)
285 blit(out, south);
286 blit(out, east);
287 // Remove NE-style fill if any leaked; then we want SE concave by default.
288 // Punch using mapped NE delta: NE fill region → clear for concave SE.
289 const delta = cornerDelta(ellNE, fillNE);
290 for (const d of delta)
279 {
291 {
280 for (let x = 0; x < SIZE; x++)
292 // Map NE corner (top-right) → SE corner (bottom-right): keep x, flip y.
293 const x = d.x;
294 const y = flipY(d.y);
295 // Only clear / don't apply fill for the concave L.
296 if (d.color[3] > 128)
281 {
297 {
282 if (y <= 12)
298 // Was a fill pixel in 2x2 — ensure concave leaves it empty (or outline).
283 { continue; }
299 const src = getPx(ellNE, d.x, d.y);
284 // Extend columns that belong to the vertical shaft (up body width).
300 setPx(out, x, y, src[3] > 128 ? getPx(east, x, Math.min(12, y)) : [0, 0, 0, 0]);
285 if (!opaque(up, x, 12))
301 if (src[3] <= 128)
286 { continue; }
302 { setPx(out, x, y, [0, 0, 0, 0]); }
287 setPx(out, x, y, getPx(alone, x, 12));
288 }
303 }
289 }
304 }
290 return out;
305 return out;
291}
306}
292
307
293/**
308function fillSEFromNE (ellSE, ellNE, fillNE)
294 * North+south shaft: up template opened through the bottom as well.
295 */
296function makeNS (up, south)
297{
309{
298 const out = copyRgba(up);
310 const out = copyRgba(ellSE);
299 for (let y = 13; y < SIZE; y++)
311 const delta = cornerDelta(ellNE, fillNE);
312 for (const d of delta)
300 {
313 {
301 for (let x = 0; x < SIZE; x++)
314 const x = d.x;
315 const y = flipY(d.y);
316 if (d.color[3] > 128)
302 {
317 {
303 if (opaque(south, x, y))
318 // Prefer sampling from the SE L's nearby interior.
304 { setPx(out, x, y, getPx(south, x, y)); }
319 const sample = getPx(ellSE, Math.min(SIZE - 1, Math.max(0, x - 1)), Math.min(SIZE - 1, Math.max(0, y - 1)));
305 else if (opaque(up, x, 12))
320 setPx(out, x, y, sample[3] > 128 ? sample : d.color);
306 { setPx(out, x, y, getPx(up, x, 12)); }
307 }
321 }
308 }
322 }
309 return out;
323 return out;
310}
324}
311
325
312/**
326function buildPack (kind)
313 * East+west run: east template with the west side also opened (mirror blend).
314 */
315function makeEW (east, west)
316{
327{
317 const out = clearRgba();
328 const alone = loadExample(`example-stone-${kind}.png`);
318 for (let y = 0; y < SIZE; y++)
329 const east = loadExample(`example-stone-${kind}-connecting.png`);
330 const north = loadExample(`example-stone-${kind}-connecting-up.png`);
331 const southAuthored = loadExample(`example-stone-${kind}-connecting-down.png`);
332 const ns = loadExample(`example-stone-${kind}-connecting-updown.png`);
333 const ellNE = loadExample(`example-stone-${kind}-connecting-L.png`);
334 const fillNE = loadExample(`example-stone-${kind}-connecting-2x2.png`);
335 const ellSEAuthored = loadExample(`example-stone-${kind}-connecting-Ldownright.png`);
336 const teeESW = loadExample(`example-stone-${kind}-T.png`)
337 ?? loadExample(`example-stone-${kind}-connecting-T.png`);
338 const teeNEW = loadExample(`example-stone-${kind}-connecting-T-upsidedown.png`);
339
340 if (!alone || !east || !north || !ellNE || !fillNE || !ns)
341 { throw new Error(`Missing core ${kind} examples`); }
342
343 const west = mirrorX(east);
344 const southFinal = southAuthored ?? (() =>
319 {
345 {
320 for (let x = 0; x < SIZE; x++)
346 // No authored south: alone top + updown shaft through the bottom.
347 const out = copyRgba(alone);
348 for (let y = 0; y < SIZE; y++)
321 {
349 {
322 const e = getPx(east, x, y);
350 for (let x = 0; x < SIZE; x++)
323 const w = getPx(west, x, y);
324 if (e[3] > 128 && w[3] > 128)
325 {
351 {
326 // Prefer interior sample from east beyond the free-end bevels.
352 if (y < 2 && opaque(alone, x, y))
327 setPx(out, x, y, x < 8 ? w : e);
353 { continue; }
354 if (opaque(ns, x, y))
355 { setPx(out, x, y, getPx(ns, x, y)); }
328 }
356 }
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 }
357 }
334 }
358 // Ensure bottom rows are open shaft from updown.
335 // Ensure full-width rows where either side has the band.
359 for (let y = 10; y < SIZE; y++)
336 for (let y = 0; y < SIZE; y++)
337 {
338 let any = false;
339 for (let x = 0; x < SIZE; x++)
340 {
360 {
341 if (opaque(out, x, y))
361 for (let x = 0; x < SIZE; x++)
342 { any = true; break; }
362 {
343 }
363 if (opaque(ns, x, y))
344 if (!any)
364 { setPx(out, x, y, getPx(ns, x, y)); }
345 { continue; }
365 }
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 }
366 }
361 }
367 return out;
362 return out;
368 })();
363}
369
370 const ellSEFinal = ellSEAuthored ?? composeSEFromNE(ellNE, fillNE, east, southFinal);
371 const fillSE = fillSEFromNE(ellSEFinal, ellNE, fillNE);
364
372
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);
373 const ellNW = mirrorX(ellNE);
378 const fillNW = mirrorX(fillNE);
374 const fillNW = mirrorX(fillNE);
379 // South-east / south-west L: mirror the north L vertically in occupancy terms
375 const ellSW = mirrorX(ellSEFinal);
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);
376 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
377
407function makeCardinalFill (ell, vert, horiz, corner, ellNE, fillNE)
378 const ew = (() =>
408{
409 const out = copyRgba(ell);
410 // Map NE fill delta onto the target corner.
411 for (let y = 0; y < SIZE; y++)
412 {
379 {
413 for (let x = 0; x < SIZE; x++)
380 const out = clearRgba();
381 blit(out, east);
382 blit(out, west);
383 // Close the center seam: fill any gap on rows that both sides use.
384 for (let y = 0; y < SIZE; y++)
414 {
385 {
415 let sx = x;
386 let fill = null;
416 let sy = y;
387 for (let x = 0; x < SIZE; x++)
417 if (corner === "se")
418 {
388 {
419 sx = x;
389 const c = getPx(east, x, y);
420 sy = SIZE - 1 - y;
390 if (c[3] > 128)
391 { fill = c; }
421 }
392 }
422 else if (corner === "sw")
393 if (!fill)
423 {
394 { continue; }
424 sx = SIZE - 1 - x;
395 for (let x = 0; x < SIZE; x++)
425 sy = SIZE - 1 - y;
426 }
427 else if (corner === "nw")
428 {
396 {
429 sx = SIZE - 1 - x;
397 if (!opaque(out, x, y) && (opaque(east, SIZE - 1, y) || opaque(west, 0, y)))
430 sy = y;
398 { setPx(out, x, y, fill); }
431 }
399 }
432 const a = getPx(ellNE, sx, sy);
400 // Force full row when east template opens to the edge on this row.
433 const b = getPx(fillNE, sx, sy);
401 if (opaque(east, SIZE - 1, y))
434 if (b[3] > 128 && (a[3] <= 128 || a[0] !== b[0]))
435 {
402 {
436 // Sample fill colour from the horizontal arm interior.
403 for (let x = 0; x < SIZE; x++)
437 const sample = getPx(horiz, Math.min(SIZE - 1, x), Math.min(SIZE - 1, Math.max(0, y)));
404 {
438 const col = sample[3] > 128 ? sample : b;
405 if (!opaque(out, x, y))
439 setPx(out, x, y, col);
406 { setPx(out, x, y, fill); }
407 }
440 }
408 }
441 }
409 }
442 }
410 return out;
443 return out;
411 })();
412
413 const teeESWFinal = teeESW ?? (() =>
414 {
415 const out = clearRgba();
416 blit(out, ew);
417 blit(out, southFinal);
418 return out;
419 })();
420 const teeNEWFinal = teeNEW ?? (() =>
421 {
422 const out = clearRgba();
423 blit(out, ew);
424 blit(out, north);
425 return out;
426 })();
427 const teeNES = (() =>
428 {
429 const out = clearRgba();
430 blit(out, north);
431 blit(out, east);
432 blit(out, southFinal);
433 return out;
434 })();
435 const teeNWS = mirrorX(teeNES);
436
437 /** Exact mask → rgba for authored / derived cardinal templates (no diagonals). */
438 const byMask = new Map();
439 byMask.set(0, alone);
440 byMask.set(NeighborBit.n, north);
441 byMask.set(NeighborBit.e, east);
442 byMask.set(NeighborBit.s, southFinal);
443 byMask.set(NeighborBit.w, west);
444 byMask.set(NeighborBit.n | NeighborBit.s, ns);
445 byMask.set(NeighborBit.e | NeighborBit.w, ew);
446 byMask.set(NeighborBit.n | NeighborBit.e, ellNE);
447 byMask.set(NeighborBit.n | NeighborBit.w, ellNW);
448 byMask.set(NeighborBit.s | NeighborBit.e, ellSEFinal);
449 byMask.set(NeighborBit.s | NeighborBit.w, ellSW);
450 byMask.set(NeighborBit.e | NeighborBit.s | NeighborBit.w, teeESWFinal);
451 byMask.set(NeighborBit.n | NeighborBit.e | NeighborBit.w, teeNEWFinal);
452 byMask.set(NeighborBit.n | NeighborBit.e | NeighborBit.s, teeNES);
453 byMask.set(NeighborBit.n | NeighborBit.w | NeighborBit.s, teeNWS);
454 byMask.set(
455 NeighborBit.n | NeighborBit.e | NeighborBit.s | NeighborBit.w,
456 (() =>
457 {
458 const out = clearRgba();
459 blit(out, ns);
460 blit(out, ew);
461 blit(out, teeESWFinal);
462 blit(out, teeNEWFinal);
463 return out;
464 })()
465 );
466
467 // Filled-corner variants for the primary L corners.
468 byMask.set(NeighborBit.n | NeighborBit.e | NeighborBit.ne, fillNE);
469 byMask.set(NeighborBit.n | NeighborBit.w | NeighborBit.nw, fillNW);
470 byMask.set(NeighborBit.s | NeighborBit.e | NeighborBit.se, fillSE);
471 byMask.set(NeighborBit.s | NeighborBit.w | NeighborBit.sw, fillSW);
472
473 const neDelta = cornerDelta(ellNE, fillNE);
474 const nwDelta = cornerDelta(ellNW, fillNW);
475 const seDelta = cornerDelta(ellSEFinal, fillSE);
476 const swDelta = cornerDelta(ellSW, fillSW);
477
478 return {
479 byMask,
480 neDelta,
481 nwDelta,
482 seDelta,
483 swDelta,
484 alone,
485 east,
486 west,
487 north,
488 south: southFinal,
489 ns,
490 ew,
491 ellNE,
492 fillNE
493 };
444}
494}
445
495
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)
496function paintMask (mask, pack)
451{
497{
452 const c = decodeMask(mask);
498 const n = (mask & NeighborBit.n) !== 0;
453 const {
499 const e = (mask & NeighborBit.e) !== 0;
454 alone, east, west, north, south, ns, ew,
500 const s = (mask & NeighborBit.s) !== 0;
455 ellNE, fillNE, ellNW, fillNW, ellSE, fillSE, ellSW, fillSW
501 const w = (mask & NeighborBit.w) !== 0;
456 } = pack;
502 const ne = (mask & NeighborBit.ne) !== 0;
457
503 const se = (mask & NeighborBit.se) !== 0;
458 const n = c.n;
504 const sw = (mask & NeighborBit.sw) !== 0;
459 const e = c.e;
505 const nw = (mask & NeighborBit.nw) !== 0;
460 const s = c.s;
506
461 const w = c.w;
507 const cardinal = (n ? NeighborBit.n : 0)
462 const cardinal = (n ? 1 : 0) + (e ? 1 : 0) + (s ? 1 : 0) + (w ? 1 : 0);
508 | (e ? NeighborBit.e : 0)
463
509 | (s ? NeighborBit.s : 0)
464 // --- Exact / near-exact authored cases ---
510 | (w ? NeighborBit.w : 0);
465 if (cardinal === 0)
511
466 { return copyRgba(alone); }
512 // Exact hit (including known filled corners).
467 if (cardinal === 1)
513 if (pack.byMask.has(mask))
514 { return copyRgba(pack.byMask.get(mask)); }
515
516 // Start from cardinal-only template, then apply diagonal fills.
517 let out;
518 if (pack.byMask.has(cardinal))
519 { out = copyRgba(pack.byMask.get(cardinal)); }
520 else
468 {
521 {
469 if (e)
522 out = clearRgba();
470 { return copyRgba(east); }
471 if (w)
472 { return copyRgba(west); }
473 if (n)
523 if (n)
474 { return copyRgba(north); }
524 { blit(out, pack.north); }
475 if (s)
525 if (s)
476 { return copyRgba(south); }
526 { blit(out, pack.south); }
477 }
527 if (e)
478
528 { blit(out, pack.east); }
479 // Pure L corners (two adjacent cardinals)
529 if (w)
480 if (cardinal === 2 && n && e && !s && !w)
530 { blit(out, pack.west); }
481 { return c.ne ? copyRgba(fillNE) : copyRgba(ellNE); }
531 if (n && s)
482 if (cardinal === 2 && n && w && !s && !e)
532 { blit(out, pack.ns); }
483 { return c.nw ? copyRgba(fillNW) : copyRgba(ellNW); }
533 if (e && w)
484 if (cardinal === 2 && s && e && !n && !w)
534 { blit(out, pack.ew); }
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 }
535 }
563
536
537 if (n && e && ne)
538 { applyDelta(out, pack.neDelta, idX, idY); }
539 if (n && w && nw)
540 { applyDelta(out, pack.nwDelta, idX, idY); }
541 if (s && e && se)
542 { applyDelta(out, pack.seDelta, idX, idY); }
543 if (s && w && sw)
544 { applyDelta(out, pack.swDelta, idX, idY); }
545
546 // When diagonal is absent but cardinal template accidentally included fill,
547 // strip using inverse delta (already concave templates are used for cardinal).
564 return out;
548 return out;
565}
549}
566
550
@@ -586,28 +570,31 @@ function writeSet (dir, pack, label)
586 console.log(`Wrote ${masks.length} ${label} stone-wall sprites to ${dir}`);
570 console.log(`Wrote ${masks.length} ${label} stone-wall sprites to ${dir}`);
587}
571}
588
572
589const thin = buildPack("thin");
590const thick = buildPack("thick");
591
592// Sanity checks against authored examples
593function assertSame (a, b, label)
573function assertSame (a, b, label)
594{
574{
595 for (let i = 0; i < a.length; i++)
575 for (let i = 0; i < a.length; i++)
596 {
576 {
597 if (a[i] !== b[i])
577 if (a[i] !== b[i])
598 { throw new Error(`Template mismatch: ${label} at byte ${i}`); }
578 { throw new Error(`Mismatch ${label} @${i}: ${a[i]}≠${b[i]}`); }
599 }
579 }
600}
580}
601assertSame(paintMask(0, thin), thin.alone, "thin alone");
581
602assertSame(paintMask(2, thin), thin.east, "thin east");
582const thin = buildPack("thin");
603assertSame(paintMask(1, thin), thin.north, "thin north");
583const thick = buildPack("thick");
604assertSame(paintMask(3, thin), thin.ellNE, "thin L");
584
605assertSame(paintMask(19, thin), thin.fillNE, "thin 2x2");
585assertSame(paintMask(0, thin), thin.alone, "thin/0");
606assertSame(paintMask(0, thick), thick.alone, "thick alone");
586assertSame(paintMask(1, thin), thin.north, "thin/1");
607assertSame(paintMask(2, thick), thick.east, "thick east");
587assertSame(paintMask(2, thin), thin.east, "thin/2");
608assertSame(paintMask(1, thick), thick.north, "thick north");
588assertSame(paintMask(3, thin), thin.ellNE, "thin/3");
609assertSame(paintMask(3, thick), thick.ellNE, "thick L");
589assertSame(paintMask(5, thin), thin.ns, "thin/5");
610assertSame(paintMask(19, thick), thick.fillNE, "thick 2x2");
590assertSame(paintMask(19, thin), thin.fillNE, "thin/19");
591assertSame(paintMask(0, thick), thick.alone, "thick/0");
592assertSame(paintMask(1, thick), thick.north, "thick/1");
593assertSame(paintMask(2, thick), thick.east, "thick/2");
594assertSame(paintMask(3, thick), thick.ellNE, "thick/3");
595assertSame(paintMask(5, thick), thick.ns, "thick/5");
596assertSame(paintMask(19, thick), thick.fillNE, "thick/19");
611
597
612writeSet(OUT_THIN, thin, "thin");
598writeSet(OUT_THIN, thin, "thin");
613writeSet(OUT_THICK, thick, "thick");
599writeSet(OUT_THICK, thick, "thick");
600console.log("All authored core templates verified.");