BlueGit

timfa questward

Switch stone walls to named cardinal connect states.

Drop diagonals; rename assets to thick/thin-stone-wall with suffixes like esw/wne/we/ns, generated from the authored examples for correction.

timfa Tim Falken · pushed by timfa

Authored 2026-08-31 · 2 months ago

Hash
f356efeda97770725d1d5b02adf8c232f9fabe17
Parent
5b5db5c9
126 files changed +306 −457
client/assets/blocks/stone-wall/example-stone-thick-T.png client/assets/blocks/_examples/stone-wall/example-stone-thick-T.png renamed +0 −0
client/assets/blocks/stone-wall/example-stone-thick-connecting-2x2.png client/assets/blocks/_examples/stone-wall/example-stone-thick-connecting-2x2.png renamed +0 −0
client/assets/blocks/stone-wall/example-stone-thick-connecting-L.png client/assets/blocks/_examples/stone-wall/example-stone-thick-connecting-L.png renamed +0 −0
client/assets/blocks/stone-wall/example-stone-thick-connecting-up.png client/assets/blocks/_examples/stone-wall/example-stone-thick-connecting-up.png renamed +0 −0
client/assets/blocks/stone-wall/example-stone-thick-connecting-updown.png client/assets/blocks/_examples/stone-wall/example-stone-thick-connecting-updown.png renamed +0 −0
client/assets/blocks/stone-wall/example-stone-thick-connecting.png client/assets/blocks/_examples/stone-wall/example-stone-thick-connecting.png renamed +0 −0
client/assets/blocks/stone-wall/example-stone-thick.png client/assets/blocks/_examples/stone-wall/example-stone-thick.png renamed +0 −0
client/assets/blocks/stone-wall/example-stone-thin-connecting-2x2.png client/assets/blocks/_examples/stone-wall/example-stone-thin-connecting-2x2.png renamed +0 −0
client/assets/blocks/stone-wall/example-stone-thin-connecting-L.png client/assets/blocks/_examples/stone-wall/example-stone-thin-connecting-L.png renamed +0 −0
client/assets/blocks/stone-wall/example-stone-thin-connecting-Ldownright.png client/assets/blocks/_examples/stone-wall/example-stone-thin-connecting-Ldownright.png renamed +0 −0
client/assets/blocks/stone-wall/example-stone-thin-connecting-T-upsidedown.png client/assets/blocks/_examples/stone-wall/example-stone-thin-connecting-T-upsidedown.png renamed +0 −0
client/assets/blocks/stone-wall/example-stone-thin-connecting-T.png client/assets/blocks/_examples/stone-wall/example-stone-thin-connecting-T.png renamed +0 −0
client/assets/blocks/stone-wall/example-stone-thin-connecting-down.png client/assets/blocks/_examples/stone-wall/example-stone-thin-connecting-down.png renamed +0 −0
client/assets/blocks/stone-wall/example-stone-thin-connecting-up.png client/assets/blocks/_examples/stone-wall/example-stone-thin-connecting-up.png renamed +0 −0
client/assets/blocks/stone-wall/example-stone-thin-connecting-updown.png client/assets/blocks/_examples/stone-wall/example-stone-thin-connecting-updown.png renamed +0 −0
client/assets/blocks/stone-wall/example-stone-thin-connecting.png client/assets/blocks/_examples/stone-wall/example-stone-thin-connecting.png renamed +0 −0
client/assets/blocks/stone-wall/example-stone-thin.png client/assets/blocks/_examples/stone-wall/example-stone-thin.png renamed +0 −0
client/assets/blocks/stone-wall-thin/110.png deleted Binary file +0 −0

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/2.png client/assets/blocks/thick-stone-wall/thick-stone-wall-e.png renamed +0 −0
client/assets/blocks/thick-stone-wall/thick-stone-wall-es.png added Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/110.png client/assets/blocks/thick-stone-wall/thick-stone-wall-esw.png renamed +0 −0
client/assets/blocks/stone-wall/1.png client/assets/blocks/thick-stone-wall/thick-stone-wall-n.png renamed +0 −0
client/assets/blocks/stone-wall/3.png client/assets/blocks/thick-stone-wall/thick-stone-wall-ne.png renamed +0 −0
client/assets/blocks/thick-stone-wall/thick-stone-wall-nes.png added Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/10.png client/assets/blocks/thick-stone-wall/thick-stone-wall-nesw.png renamed +0 −0
client/assets/blocks/stone-wall/5.png client/assets/blocks/thick-stone-wall/thick-stone-wall-ns.png renamed +0 −0
client/assets/blocks/thick-stone-wall/thick-stone-wall-s.png added Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/8.png client/assets/blocks/thick-stone-wall/thick-stone-wall-sw.png renamed +0 −0
client/assets/blocks/thick-stone-wall/thick-stone-wall-swn.png added Binary file +0 −0

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

client/assets/blocks/stone-wall/9.png client/assets/blocks/thick-stone-wall/thick-stone-wall-wn.png renamed +0 −0
client/assets/blocks/stone-wall/11.png client/assets/blocks/thick-stone-wall/thick-stone-wall-wne.png renamed +0 −0
client/assets/blocks/stone-wall/0.png client/assets/blocks/thick-stone-wall/thick-stone-wall.png renamed +0 −0
client/assets/blocks/stone-wall-thin/2.png client/assets/blocks/thin-stone-wall/thin-stone-wall-e.png renamed +0 −0
client/assets/blocks/stone-wall-thin/6.png client/assets/blocks/thin-stone-wall/thin-stone-wall-es.png renamed +0 −0
client/assets/blocks/stone-wall-thin/14.png client/assets/blocks/thin-stone-wall/thin-stone-wall-esw.png renamed +0 −0
client/assets/blocks/stone-wall-thin/1.png client/assets/blocks/thin-stone-wall/thin-stone-wall-n.png renamed +0 −0
client/assets/blocks/stone-wall-thin/3.png client/assets/blocks/thin-stone-wall/thin-stone-wall-ne.png renamed +0 −0
client/assets/blocks/stone-wall-thin/7.png client/assets/blocks/thin-stone-wall/thin-stone-wall-nes.png renamed +0 −0
client/assets/blocks/thin-stone-wall/thin-stone-wall-nesw.png added Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/5.png client/assets/blocks/thin-stone-wall/thin-stone-wall-ns.png renamed +0 −0
client/assets/blocks/stone-wall-thin/4.png client/assets/blocks/thin-stone-wall/thin-stone-wall-s.png renamed +0 −0
client/assets/blocks/stone-wall-thin/12.png client/assets/blocks/thin-stone-wall/thin-stone-wall-sw.png renamed +0 −0
client/assets/blocks/stone-wall-thin/13.png client/assets/blocks/thin-stone-wall/thin-stone-wall-swn.png renamed +0 −0
client/assets/blocks/stone-wall-thin/8.png client/assets/blocks/thin-stone-wall/thin-stone-wall-w.png renamed +0 −0
client/assets/blocks/stone-wall-thin/10.png client/assets/blocks/thin-stone-wall/thin-stone-wall-we.png renamed +0 −0
client/assets/blocks/thin-stone-wall/thin-stone-wall-wn.png added Binary file +0 −0

The contents of this binary file are not shown.

client/assets/blocks/stone-wall-thin/11.png client/assets/blocks/thin-stone-wall/thin-stone-wall-wne.png renamed +0 −0
client/assets/blocks/stone-wall-thin/0.png client/assets/blocks/thin-stone-wall/thin-stone-wall.png renamed +0 −0
client/scripts/generate-stone-wall-sprites.mjs +122 −278
@@ -1,39 +1,41 @@
1 1 /**
2 * Generate thin + thick stone-wall PNGs for blob neighbor-connect masks
3 * (cardinals + diagonal corners — 47 valid states).
2 * Generate thin + thick stone-wall PNGs for the 16 cardinal neighbor masks.
4 3 *
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)
4 * Naming: `{key}.png` alone, `{key}-{suffix}.png` when connected.
5 * Suffixes: n/e/s/w, ns, we, ne/es/sw/wn, esw/wne/swn/nes, nesw.
17 6 *
18 * West variants are X-mirrors. Remaining masks compose from these stamps +
19 * the L↔2×2 corner-fill delta.
7 * Examples (shape only, no texture) live in
8 * client/assets/blocks/_examples/stone-wall/
20 9 *
21 10 * node client/scripts/generate-stone-wall-sprites.mjs
22 11 */
23 import { mkdirSync, writeFileSync, readdirSync, unlinkSync, readFileSync, existsSync } from "node:fs";
12 import {
13 mkdirSync,
14 writeFileSync,
15 readdirSync,
16 unlinkSync,
17 readFileSync,
18 existsSync,
19 cpSync,
20 rmSync
21 } from "node:fs";
24 22 import { deflateSync, inflateSync } from "node:zlib";
25 23 import { fileURLToPath } from "node:url";
26 24 import { dirname, join } from "node:path";
27 25 import {
28 26 NeighborBit,
29 listValidNeighborConnectCornerMasks
27 cardinalConnectionSuffix,
28 composeNamedNeighborConnectState
30 29 } from "../../shared/world/neighborConnect.js";
31 30
32 31 const SIZE = 16;
33 32 const ROOT = join(dirname(fileURLToPath(import.meta.url)), "../assets/blocks");
34 const OUT_THICK = join(ROOT, "stone-wall");
35 const OUT_THIN = join(ROOT, "stone-wall-thin");
36 const EXAMPLES = join(ROOT, "stone-wall");
33 const EXAMPLES = join(ROOT, "_examples/stone-wall");
34 const OUT_THICK = join(ROOT, "thick-stone-wall");
35 const OUT_THIN = join(ROOT, "thin-stone-wall");
36
37 const THICK = { key: "thick-stone-wall", neighborConnect: true, neighborConnectNamed: true };
38 const THIN = { key: "thin-stone-wall", neighborConnect: true, neighborConnectNamed: true };
37 39
38 40 function crcTable ()
39 41 {
@@ -234,93 +236,21 @@ function blit (dst, src)
234 236 }
235 237 }
236 238
237 /** Pixels where filled differs from concave (the diagonal corner fill). */
238 function cornerDelta (concave, filled)
239 {
240 const deltas = [];
241 for (let y = 0; y < SIZE; y++)
242 {
243 for (let x = 0; x < SIZE; x++)
244 {
245 const a = getPx(concave, x, y);
246 const b = getPx(filled, x, y);
247 if (b[3] > 128 && (a[3] <= 128 || a[0] !== b[0] || a[1] !== b[1] || a[2] !== b[2]))
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] }); }
251 }
252 }
253 return deltas;
254 }
255
256 function applyDelta (dst, deltas, mapX, mapY)
257 {
258 for (const d of deltas)
259 {
260 const x = mapX(d.x);
261 const y = mapY(d.y);
262 setPx(dst, x, y, d.color);
263 }
264 }
265
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; }
274
275 /**
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.
281 */
282 function composeSEFromNE (ellNE, fillNE, east, south)
283 {
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)
291 {
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)
297 {
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]); }
303 }
304 }
305 return out;
306 }
307
308 function fillSEFromNE (ellSE, ellNE, fillNE)
239 /** Ensure example folder exists; migrate from legacy stone-wall/ if needed. */
240 function ensureExamples ()
309 241 {
310 const out = copyRgba(ellSE);
311 const delta = cornerDelta(ellNE, fillNE);
312 for (const d of delta)
242 mkdirSync(EXAMPLES, { recursive: true });
243 const legacy = join(ROOT, "stone-wall");
244 if (!existsSync(legacy))
245 { return; }
246 for (const name of readdirSync(legacy))
313 247 {
314 const x = d.x;
315 const y = flipY(d.y);
316 if (d.color[3] > 128)
317 {
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);
321 }
248 if (!name.startsWith("example-") || !name.endsWith(".png"))
249 { continue; }
250 const dest = join(EXAMPLES, name);
251 if (!existsSync(dest))
252 { cpSync(join(legacy, name), dest); }
322 253 }
323 return out;
324 254 }
325 255
326 256 function buildPack (kind)
@@ -328,34 +258,21 @@ function buildPack (kind)
328 258 const alone = loadExample(`example-stone-${kind}.png`);
329 259 const east = loadExample(`example-stone-${kind}-connecting.png`);
330 260 const north = loadExample(`example-stone-${kind}-connecting-up.png`);
331 const southAuthored = loadExample(`example-stone-${kind}-connecting-down.png`);
261 const south = loadExample(`example-stone-${kind}-connecting-down.png`);
332 262 const ns = loadExample(`example-stone-${kind}-connecting-updown.png`);
333 263 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`);
264 const ellSE = loadExample(`example-stone-${kind}-connecting-Ldownright.png`);
336 265 const teeESW = loadExample(`example-stone-${kind}-T.png`)
337 266 ?? loadExample(`example-stone-${kind}-connecting-T.png`);
338 267 const teeNEW = loadExample(`example-stone-${kind}-connecting-T-upsidedown.png`);
339 268
340 if (!alone || !east || !north || !ellNE || !fillNE || !ns)
341 { throw new Error(`Missing core ${kind} examples`); }
269 if (!alone || !east || !north || !ns)
270 { throw new Error(`Missing core ${kind} examples in ${EXAMPLES}`); }
342 271
343 272 const west = mirrorX(east);
344 const southFinal = southAuthored ?? (() =>
273 const southFinal = south ?? (() =>
345 274 {
346 // No authored south: alone top + updown shaft through the bottom.
347 275 const out = copyRgba(alone);
348 for (let y = 0; y < SIZE; y++)
349 {
350 for (let x = 0; x < SIZE; x++)
351 {
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)); }
356 }
357 }
358 // Ensure bottom rows are open shaft from updown.
359 276 for (let y = 10; y < SIZE; y++)
360 277 {
361 278 for (let x = 0; x < SIZE; x++)
@@ -367,45 +284,46 @@ function buildPack (kind)
367 284 return out;
368 285 })();
369 286
370 const ellSEFinal = ellSEAuthored ?? composeSEFromNE(ellNE, fillNE, east, southFinal);
371 const fillSE = fillSEFromNE(ellSEFinal, ellNE, fillNE);
372
373 const ellNW = mirrorX(ellNE);
374 const fillNW = mirrorX(fillNE);
287 const ellNEFinal = ellNE ?? (() =>
288 {
289 const out = clearRgba();
290 blit(out, north);
291 blit(out, east);
292 return out;
293 })();
294 const ellSEFinal = ellSE ?? (() =>
295 {
296 const out = clearRgba();
297 blit(out, southFinal);
298 blit(out, east);
299 return out;
300 })();
301 const ellNW = mirrorX(ellNEFinal);
375 302 const ellSW = mirrorX(ellSEFinal);
376 const fillSW = mirrorX(fillSE);
377 303
378 304 const ew = (() =>
379 305 {
380 306 const out = clearRgba();
381 307 blit(out, east);
382 308 blit(out, west);
383 // Close the center seam: fill any gap on rows that both sides use.
384 309 for (let y = 0; y < SIZE; y++)
385 310 {
311 if (!opaque(east, SIZE - 1, y))
312 { continue; }
386 313 let fill = null;
387 for (let x = 0; x < SIZE; x++)
314 for (let x = SIZE - 1; x >= 0; x--)
388 315 {
389 316 const c = getPx(east, x, y);
390 317 if (c[3] > 128)
391 { fill = c; }
318 { fill = c; break; }
392 319 }
393 320 if (!fill)
394 321 { continue; }
395 322 for (let x = 0; x < SIZE; x++)
396 323 {
397 if (!opaque(out, x, y) && (opaque(east, SIZE - 1, y) || opaque(west, 0, y)))
324 if (!opaque(out, x, y))
398 325 { setPx(out, x, y, fill); }
399 326 }
400 // Force full row when east template opens to the edge on this row.
401 if (opaque(east, SIZE - 1, y))
402 {
403 for (let x = 0; x < SIZE; x++)
404 {
405 if (!opaque(out, x, y))
406 { setPx(out, x, y, fill); }
407 }
408 }
409 327 }
410 328 return out;
411 329 })();
@@ -433,119 +351,34 @@ function buildPack (kind)
433 351 return out;
434 352 })();
435 353 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 };
494 }
495
496 function paintMask (mask, pack)
497 {
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
354 const all = (() =>
521 355 {
522 out = clearRgba();
523 if (n)
524 { blit(out, pack.north); }
525 if (s)
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); }
535 }
356 const out = clearRgba();
357 blit(out, ns);
358 blit(out, ew);
359 return out;
360 })();
536 361
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).
548 return out;
362 const byMask = new Map([
363 [0, alone],
364 [NeighborBit.n, north],
365 [NeighborBit.e, east],
366 [NeighborBit.s, southFinal],
367 [NeighborBit.w, west],
368 [NeighborBit.n | NeighborBit.s, ns],
369 [NeighborBit.e | NeighborBit.w, ew],
370 [NeighborBit.n | NeighborBit.e, ellNEFinal],
371 [NeighborBit.e | NeighborBit.s, ellSEFinal],
372 [NeighborBit.s | NeighborBit.w, ellSW],
373 [NeighborBit.w | NeighborBit.n, ellNW],
374 [NeighborBit.e | NeighborBit.s | NeighborBit.w, teeESWFinal],
375 [NeighborBit.w | NeighborBit.n | NeighborBit.e, teeNEWFinal],
376 [NeighborBit.n | NeighborBit.e | NeighborBit.s, teeNES],
377 [NeighborBit.n | NeighborBit.w | NeighborBit.s, teeNWS],
378 [NeighborBit.n | NeighborBit.e | NeighborBit.s | NeighborBit.w, all]
379 ]);
380
381 return { byMask, alone, east, north, ns, ellNE: ellNEFinal };
549 382 }
550 383
551 384 function clearGenerated (dir)
@@ -553,48 +386,59 @@ function clearGenerated (dir)
553 386 mkdirSync(dir, { recursive: true });
554 387 for (const name of readdirSync(dir))
555 388 {
556 if (!/^\d+\.png$/i.test(name))
389 if (!name.endsWith(".png") || name.startsWith("example"))
557 390 { continue; }
558 391 unlinkSync(join(dir, name));
559 392 }
560 393 }
561 394
562 function writeSet (dir, pack, label)
395 function writeSet (dir, def, pack)
563 396 {
564 397 clearGenerated(dir);
565 const masks = listValidNeighborConnectCornerMasks();
566 for (const mask of masks)
398 for (let mask = 0; mask < 16; mask++)
399 {
400 const rgba = pack.byMask.get(mask);
401 if (!rgba)
402 { throw new Error(`Missing mask ${mask} for ${def.key}`); }
403 const state = composeNamedNeighborConnectState(def, mask);
404 writeFileSync(join(dir, `${state}.png`), encodePng(rgba));
405 }
406 console.log(`Wrote 16 ${def.key} sprites → ${dir}`);
407 }
408
409 function removeLegacyFolders ()
410 {
411 for (const name of ["stone-wall", "stone-wall-thin"])
567 412 {
568 writeFileSync(join(dir, `${mask}.png`), encodePng(paintMask(mask, pack)));
413 const dir = join(ROOT, name);
414 if (!existsSync(dir))
415 { continue; }
416 // Keep examples only in _examples/; remove entire legacy folder.
417 rmSync(dir, { recursive: true, force: true });
418 console.log(`Removed legacy ${name}/`);
569 419 }
570 console.log(`Wrote ${masks.length} ${label} stone-wall sprites to ${dir}`);
571 420 }
572 421
422 ensureExamples();
423 const thin = buildPack("thin");
424 const thick = buildPack("thick");
425
573 426 function assertSame (a, b, label)
574 427 {
575 428 for (let i = 0; i < a.length; i++)
576 429 {
577 430 if (a[i] !== b[i])
578 { throw new Error(`Mismatch ${label} @${i}: ${a[i]}≠${b[i]}`); }
431 { throw new Error(`Mismatch ${label}`); }
579 432 }
580 433 }
434 assertSame(thin.byMask.get(0), thin.alone, "thin alone");
435 assertSame(thin.byMask.get(1), thin.north, "thin n");
436 assertSame(thin.byMask.get(2), thin.east, "thin e");
437 assertSame(thin.byMask.get(5), thin.ns, "thin ns");
438 assertSame(thick.byMask.get(0), thick.alone, "thick alone");
581 439
582 const thin = buildPack("thin");
583 const thick = buildPack("thick");
440 writeSet(OUT_THIN, THIN, thin);
441 writeSet(OUT_THICK, THICK, thick);
442 removeLegacyFolders();
584 443
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");
597
598 writeSet(OUT_THIN, thin, "thin");
599 writeSet(OUT_THICK, thick, "thick");
600 console.log("All authored core templates verified.");
444 console.log("Suffixes:", [...Array(16)].map((_, m) => `${m}=${cardinalConnectionSuffix(m) || "(alone)"}`).join(", "));
@@ -1,39 +1,41 @@
1/**
1/**
2 * Generate thin + thick stone-wall PNGs for blob neighbor-connect masks
2 * Generate thin + thick stone-wall PNGs for the 16 cardinal neighbor masks.
3 * (cardinals + diagonal corners — 47 valid states).
4 *
3 *
5 * Flat greys only (no texture). Authoritative examples live beside the outputs:
4 * Naming: `{key}.png` alone, `{key}-{suffix}.png` when connected.
6 * example-stone-{thin|thick}.png
5 * Suffixes: n/e/s/w, ns, we, ne/es/sw/wn, esw/wne/swn/nes, nesw.
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)
17 *
6 *
18 * West variants are X-mirrors. Remaining masks compose from these stamps +
7 * Examples (shape only, no texture) live in
19 * the L↔2×2 corner-fill delta.
8 * client/assets/blocks/_examples/stone-wall/
20 *
9 *
21 * node client/scripts/generate-stone-wall-sprites.mjs
10 * node client/scripts/generate-stone-wall-sprites.mjs
22 */
11 */
23import { mkdirSync, writeFileSync, readdirSync, unlinkSync, readFileSync, existsSync } from "node:fs";
12import {
13 mkdirSync,
14 writeFileSync,
15 readdirSync,
16 unlinkSync,
17 readFileSync,
18 existsSync,
19 cpSync,
20 rmSync
21} from "node:fs";
24import { deflateSync, inflateSync } from "node:zlib";
22import { deflateSync, inflateSync } from "node:zlib";
25import { fileURLToPath } from "node:url";
23import { fileURLToPath } from "node:url";
26import { dirname, join } from "node:path";
24import { dirname, join } from "node:path";
27import {
25import {
28 NeighborBit,
26 NeighborBit,
29 listValidNeighborConnectCornerMasks
27 cardinalConnectionSuffix,
28 composeNamedNeighborConnectState
30} from "../../shared/world/neighborConnect.js";
29} from "../../shared/world/neighborConnect.js";
31
30
32const SIZE = 16;
31const SIZE = 16;
33const ROOT = join(dirname(fileURLToPath(import.meta.url)), "../assets/blocks");
32const ROOT = join(dirname(fileURLToPath(import.meta.url)), "../assets/blocks");
34const OUT_THICK = join(ROOT, "stone-wall");
33const EXAMPLES = join(ROOT, "_examples/stone-wall");
35const OUT_THIN = join(ROOT, "stone-wall-thin");
34const OUT_THICK = join(ROOT, "thick-stone-wall");
36const EXAMPLES = join(ROOT, "stone-wall");
35const OUT_THIN = join(ROOT, "thin-stone-wall");
36
37const THICK = { key: "thick-stone-wall", neighborConnect: true, neighborConnectNamed: true };
38const THIN = { key: "thin-stone-wall", neighborConnect: true, neighborConnectNamed: true };
37
39
38function crcTable ()
40function crcTable ()
39{
41{
@@ -234,93 +236,21 @@ function blit (dst, src)
234 }
236 }
235}
237}
236
238
237/** Pixels where filled differs from concave (the diagonal corner fill). */
239/** Ensure example folder exists; migrate from legacy stone-wall/ if needed. */
238function cornerDelta (concave, filled)
240function ensureExamples ()
239{
240 const deltas = [];
241 for (let y = 0; y < SIZE; y++)
242 {
243 for (let x = 0; x < SIZE; x++)
244 {
245 const a = getPx(concave, x, y);
246 const b = getPx(filled, x, y);
247 if (b[3] > 128 && (a[3] <= 128 || a[0] !== b[0] || a[1] !== b[1] || a[2] !== b[2]))
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] }); }
251 }
252 }
253 return deltas;
254}
255
256function applyDelta (dst, deltas, mapX, mapY)
257{
258 for (const d of deltas)
259 {
260 const x = mapX(d.x);
261 const y = mapY(d.y);
262 setPx(dst, x, y, d.color);
263 }
264}
265
266function idX (x)
267{ return x; }
268function idY (y)
269{ return y; }
270function flipX (x)
271{ return SIZE - 1 - x; }
272function flipY (y)
273{ return SIZE - 1 - y; }
274
275/**
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.
281 */
282function composeSEFromNE (ellNE, fillNE, east, south)
283{
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)
291 {
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)
297 {
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]); }
303 }
304 }
305 return out;
306}
307
308function fillSEFromNE (ellSE, ellNE, fillNE)
309{
241{
310 const out = copyRgba(ellSE);
242 mkdirSync(EXAMPLES, { recursive: true });
311 const delta = cornerDelta(ellNE, fillNE);
243 const legacy = join(ROOT, "stone-wall");
312 for (const d of delta)
244 if (!existsSync(legacy))
245 { return; }
246 for (const name of readdirSync(legacy))
313 {
247 {
314 const x = d.x;
248 if (!name.startsWith("example-") || !name.endsWith(".png"))
315 const y = flipY(d.y);
249 { continue; }
316 if (d.color[3] > 128)
250 const dest = join(EXAMPLES, name);
317 {
251 if (!existsSync(dest))
318 // Prefer sampling from the SE L's nearby interior.
252 { cpSync(join(legacy, name), dest); }
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);
321 }
322 }
253 }
323 return out;
324}
254}
325
255
326function buildPack (kind)
256function buildPack (kind)
@@ -328,34 +258,21 @@ function buildPack (kind)
328 const alone = loadExample(`example-stone-${kind}.png`);
258 const alone = loadExample(`example-stone-${kind}.png`);
329 const east = loadExample(`example-stone-${kind}-connecting.png`);
259 const east = loadExample(`example-stone-${kind}-connecting.png`);
330 const north = loadExample(`example-stone-${kind}-connecting-up.png`);
260 const north = loadExample(`example-stone-${kind}-connecting-up.png`);
331 const southAuthored = loadExample(`example-stone-${kind}-connecting-down.png`);
261 const south = loadExample(`example-stone-${kind}-connecting-down.png`);
332 const ns = loadExample(`example-stone-${kind}-connecting-updown.png`);
262 const ns = loadExample(`example-stone-${kind}-connecting-updown.png`);
333 const ellNE = loadExample(`example-stone-${kind}-connecting-L.png`);
263 const ellNE = loadExample(`example-stone-${kind}-connecting-L.png`);
334 const fillNE = loadExample(`example-stone-${kind}-connecting-2x2.png`);
264 const ellSE = loadExample(`example-stone-${kind}-connecting-Ldownright.png`);
335 const ellSEAuthored = loadExample(`example-stone-${kind}-connecting-Ldownright.png`);
336 const teeESW = loadExample(`example-stone-${kind}-T.png`)
265 const teeESW = loadExample(`example-stone-${kind}-T.png`)
337 ?? loadExample(`example-stone-${kind}-connecting-T.png`);
266 ?? loadExample(`example-stone-${kind}-connecting-T.png`);
338 const teeNEW = loadExample(`example-stone-${kind}-connecting-T-upsidedown.png`);
267 const teeNEW = loadExample(`example-stone-${kind}-connecting-T-upsidedown.png`);
339
268
340 if (!alone || !east || !north || !ellNE || !fillNE || !ns)
269 if (!alone || !east || !north || !ns)
341 { throw new Error(`Missing core ${kind} examples`); }
270 { throw new Error(`Missing core ${kind} examples in ${EXAMPLES}`); }
342
271
343 const west = mirrorX(east);
272 const west = mirrorX(east);
344 const southFinal = southAuthored ?? (() =>
273 const southFinal = south ?? (() =>
345 {
274 {
346 // No authored south: alone top + updown shaft through the bottom.
347 const out = copyRgba(alone);
275 const out = copyRgba(alone);
348 for (let y = 0; y < SIZE; y++)
349 {
350 for (let x = 0; x < SIZE; x++)
351 {
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)); }
356 }
357 }
358 // Ensure bottom rows are open shaft from updown.
359 for (let y = 10; y < SIZE; y++)
276 for (let y = 10; y < SIZE; y++)
360 {
277 {
361 for (let x = 0; x < SIZE; x++)
278 for (let x = 0; x < SIZE; x++)
@@ -367,45 +284,46 @@ function buildPack (kind)
367 return out;
284 return out;
368 })();
285 })();
369
286
370 const ellSEFinal = ellSEAuthored ?? composeSEFromNE(ellNE, fillNE, east, southFinal);
287 const ellNEFinal = ellNE ?? (() =>
371 const fillSE = fillSEFromNE(ellSEFinal, ellNE, fillNE);
288 {
372
289 const out = clearRgba();
373 const ellNW = mirrorX(ellNE);
290 blit(out, north);
374 const fillNW = mirrorX(fillNE);
291 blit(out, east);
292 return out;
293 })();
294 const ellSEFinal = ellSE ?? (() =>
295 {
296 const out = clearRgba();
297 blit(out, southFinal);
298 blit(out, east);
299 return out;
300 })();
301 const ellNW = mirrorX(ellNEFinal);
375 const ellSW = mirrorX(ellSEFinal);
302 const ellSW = mirrorX(ellSEFinal);
376 const fillSW = mirrorX(fillSE);
377
303
378 const ew = (() =>
304 const ew = (() =>
379 {
305 {
380 const out = clearRgba();
306 const out = clearRgba();
381 blit(out, east);
307 blit(out, east);
382 blit(out, west);
308 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++)
309 for (let y = 0; y < SIZE; y++)
385 {
310 {
311 if (!opaque(east, SIZE - 1, y))
312 { continue; }
386 let fill = null;
313 let fill = null;
387 for (let x = 0; x < SIZE; x++)
314 for (let x = SIZE - 1; x >= 0; x--)
388 {
315 {
389 const c = getPx(east, x, y);
316 const c = getPx(east, x, y);
390 if (c[3] > 128)
317 if (c[3] > 128)
391 { fill = c; }
318 { fill = c; break; }
392 }
319 }
393 if (!fill)
320 if (!fill)
394 { continue; }
321 { continue; }
395 for (let x = 0; x < SIZE; x++)
322 for (let x = 0; x < SIZE; x++)
396 {
323 {
397 if (!opaque(out, x, y) && (opaque(east, SIZE - 1, y) || opaque(west, 0, y)))
324 if (!opaque(out, x, y))
398 { setPx(out, x, y, fill); }
325 { setPx(out, x, y, fill); }
399 }
326 }
400 // Force full row when east template opens to the edge on this row.
401 if (opaque(east, SIZE - 1, y))
402 {
403 for (let x = 0; x < SIZE; x++)
404 {
405 if (!opaque(out, x, y))
406 { setPx(out, x, y, fill); }
407 }
408 }
409 }
327 }
410 return out;
328 return out;
411 })();
329 })();
@@ -433,119 +351,34 @@ function buildPack (kind)
433 return out;
351 return out;
434 })();
352 })();
435 const teeNWS = mirrorX(teeNES);
353 const teeNWS = mirrorX(teeNES);
436
354 const all = (() =>
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 };
494}
495
496function paintMask (mask, pack)
497{
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
521 {
355 {
522 out = clearRgba();
356 const out = clearRgba();
523 if (n)
357 blit(out, ns);
524 { blit(out, pack.north); }
358 blit(out, ew);
525 if (s)
359 return out;
526 { blit(out, pack.south); }
360 })();
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); }
535 }
536
361
537 if (n && e && ne)
362 const byMask = new Map([
538 { applyDelta(out, pack.neDelta, idX, idY); }
363 [0, alone],
539 if (n && w && nw)
364 [NeighborBit.n, north],
540 { applyDelta(out, pack.nwDelta, idX, idY); }
365 [NeighborBit.e, east],
541 if (s && e && se)
366 [NeighborBit.s, southFinal],
542 { applyDelta(out, pack.seDelta, idX, idY); }
367 [NeighborBit.w, west],
543 if (s && w && sw)
368 [NeighborBit.n | NeighborBit.s, ns],
544 { applyDelta(out, pack.swDelta, idX, idY); }
369 [NeighborBit.e | NeighborBit.w, ew],
545
370 [NeighborBit.n | NeighborBit.e, ellNEFinal],
546 // When diagonal is absent but cardinal template accidentally included fill,
371 [NeighborBit.e | NeighborBit.s, ellSEFinal],
547 // strip using inverse delta (already concave templates are used for cardinal).
372 [NeighborBit.s | NeighborBit.w, ellSW],
548 return out;
373 [NeighborBit.w | NeighborBit.n, ellNW],
374 [NeighborBit.e | NeighborBit.s | NeighborBit.w, teeESWFinal],
375 [NeighborBit.w | NeighborBit.n | NeighborBit.e, teeNEWFinal],
376 [NeighborBit.n | NeighborBit.e | NeighborBit.s, teeNES],
377 [NeighborBit.n | NeighborBit.w | NeighborBit.s, teeNWS],
378 [NeighborBit.n | NeighborBit.e | NeighborBit.s | NeighborBit.w, all]
379 ]);
380
381 return { byMask, alone, east, north, ns, ellNE: ellNEFinal };
549}
382}
550
383
551function clearGenerated (dir)
384function clearGenerated (dir)
@@ -553,48 +386,59 @@ function clearGenerated (dir)
553 mkdirSync(dir, { recursive: true });
386 mkdirSync(dir, { recursive: true });
554 for (const name of readdirSync(dir))
387 for (const name of readdirSync(dir))
555 {
388 {
556 if (!/^\d+\.png$/i.test(name))
389 if (!name.endsWith(".png") || name.startsWith("example"))
557 { continue; }
390 { continue; }
558 unlinkSync(join(dir, name));
391 unlinkSync(join(dir, name));
559 }
392 }
560}
393}
561
394
562function writeSet (dir, pack, label)
395function writeSet (dir, def, pack)
563{
396{
564 clearGenerated(dir);
397 clearGenerated(dir);
565 const masks = listValidNeighborConnectCornerMasks();
398 for (let mask = 0; mask < 16; mask++)
566 for (const mask of masks)
399 {
400 const rgba = pack.byMask.get(mask);
401 if (!rgba)
402 { throw new Error(`Missing mask ${mask} for ${def.key}`); }
403 const state = composeNamedNeighborConnectState(def, mask);
404 writeFileSync(join(dir, `${state}.png`), encodePng(rgba));
405 }
406 console.log(`Wrote 16 ${def.key} sprites → ${dir}`);
407}
408
409function removeLegacyFolders ()
410{
411 for (const name of ["stone-wall", "stone-wall-thin"])
567 {
412 {
568 writeFileSync(join(dir, `${mask}.png`), encodePng(paintMask(mask, pack)));
413 const dir = join(ROOT, name);
414 if (!existsSync(dir))
415 { continue; }
416 // Keep examples only in _examples/; remove entire legacy folder.
417 rmSync(dir, { recursive: true, force: true });
418 console.log(`Removed legacy ${name}/`);
569 }
419 }
570 console.log(`Wrote ${masks.length} ${label} stone-wall sprites to ${dir}`);
571}
420}
572
421
422ensureExamples();
423const thin = buildPack("thin");
424const thick = buildPack("thick");
425
573function assertSame (a, b, label)
426function assertSame (a, b, label)
574{
427{
575 for (let i = 0; i < a.length; i++)
428 for (let i = 0; i < a.length; i++)
576 {
429 {
577 if (a[i] !== b[i])
430 if (a[i] !== b[i])
578 { throw new Error(`Mismatch ${label} @${i}: ${a[i]}≠${b[i]}`); }
431 { throw new Error(`Mismatch ${label}`); }
579 }
432 }
580}
433}
434assertSame(thin.byMask.get(0), thin.alone, "thin alone");
435assertSame(thin.byMask.get(1), thin.north, "thin n");
436assertSame(thin.byMask.get(2), thin.east, "thin e");
437assertSame(thin.byMask.get(5), thin.ns, "thin ns");
438assertSame(thick.byMask.get(0), thick.alone, "thick alone");
581
439
582const thin = buildPack("thin");
440writeSet(OUT_THIN, THIN, thin);
583const thick = buildPack("thick");
441writeSet(OUT_THICK, THICK, thick);
442removeLegacyFolders();
584
443
585assertSame(paintMask(0, thin), thin.alone, "thin/0");
444console.log("Suffixes:", [...Array(16)].map((_, m) => `${m}=${cardinalConnectionSuffix(m) || "(alone)"}`).join(", "));
586assertSame(paintMask(1, thin), thin.north, "thin/1");
587assertSame(paintMask(2, thin), thin.east, "thin/2");
588assertSame(paintMask(3, thin), thin.ellNE, "thin/3");
589assertSame(paintMask(5, thin), thin.ns, "thin/5");
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");
597
598writeSet(OUT_THIN, thin, "thin");
599writeSet(OUT_THICK, thick, "thick");
600console.log("All authored core templates verified.");
client/src/editor/main.js +2 −4
@@ -26,7 +26,6 @@ import
26 26 computeNeighborConnectMask,
27 27 composeNeighborConnectState,
28 28 definitionUsesNeighborConnect,
29 definitionUsesNeighborConnectCorners,
30 29 neighborConnectBlocksMatch,
31 30 neighborConnectRefreshTiles,
32 31 worldTileToChunkLocal
@@ -668,10 +667,9 @@ function refreshNeighborConnect (localX, localY, typeId)
668 667 const def = BLOCK_DEFINITIONS[typeId];
669 668 if (!draft || !definitionUsesNeighborConnect(def))
670 669 { return; }
671 const corners = definitionUsesNeighborConnectCorners(def);
672 670 const originTileX = chunkX * CHUNK_SIZE + localX;
673 671 const originTileY = chunkY * CHUNK_SIZE + localY;
674 for (const [tileX, tileY] of neighborConnectRefreshTiles(originTileX, originTileY, { corners }))
672 for (const [tileX, tileY] of neighborConnectRefreshTiles(originTileX, originTileY))
675 673 {
676 674 const block = blockAtWorld(tileX, tileY);
677 675 if (!block || block.typeId !== typeId)
@@ -680,7 +678,7 @@ function refreshNeighborConnect (localX, localY, typeId)
680 678 {
681 679 const neighbor = blockAtWorld(tileX + dx, tileY + dy);
682 680 return neighborConnectBlocksMatch(block, neighbor, def);
683 }, { corners });
681 });
684 682 const nextState = composeNeighborConnectState(def, block.state, mask);
685 683 if (block.state === nextState)
686 684 { continue; }
@@ -26,7 +26,6 @@ import
26 computeNeighborConnectMask,
26 computeNeighborConnectMask,
27 composeNeighborConnectState,
27 composeNeighborConnectState,
28 definitionUsesNeighborConnect,
28 definitionUsesNeighborConnect,
29 definitionUsesNeighborConnectCorners,
30 neighborConnectBlocksMatch,
29 neighborConnectBlocksMatch,
31 neighborConnectRefreshTiles,
30 neighborConnectRefreshTiles,
32 worldTileToChunkLocal
31 worldTileToChunkLocal
@@ -668,10 +667,9 @@ function refreshNeighborConnect (localX, localY, typeId)
668 const def = BLOCK_DEFINITIONS[typeId];
667 const def = BLOCK_DEFINITIONS[typeId];
669 if (!draft || !definitionUsesNeighborConnect(def))
668 if (!draft || !definitionUsesNeighborConnect(def))
670 { return; }
669 { return; }
671 const corners = definitionUsesNeighborConnectCorners(def);
672 const originTileX = chunkX * CHUNK_SIZE + localX;
670 const originTileX = chunkX * CHUNK_SIZE + localX;
673 const originTileY = chunkY * CHUNK_SIZE + localY;
671 const originTileY = chunkY * CHUNK_SIZE + localY;
674 for (const [tileX, tileY] of neighborConnectRefreshTiles(originTileX, originTileY, { corners }))
672 for (const [tileX, tileY] of neighborConnectRefreshTiles(originTileX, originTileY))
675 {
673 {
676 const block = blockAtWorld(tileX, tileY);
674 const block = blockAtWorld(tileX, tileY);
677 if (!block || block.typeId !== typeId)
675 if (!block || block.typeId !== typeId)
@@ -680,7 +678,7 @@ function refreshNeighborConnect (localX, localY, typeId)
680 {
678 {
681 const neighbor = blockAtWorld(tileX + dx, tileY + dy);
679 const neighbor = blockAtWorld(tileX + dx, tileY + dy);
682 return neighborConnectBlocksMatch(block, neighbor, def);
680 return neighborConnectBlocksMatch(block, neighbor, def);
683 }, { corners });
681 });
684 const nextState = composeNeighborConnectState(def, block.state, mask);
682 const nextState = composeNeighborConnectState(def, block.state, mask);
685 if (block.state === nextState)
683 if (block.state === nextState)
686 { continue; }
684 { continue; }
server/tests/central-exchange.test.js +23 −15
@@ -10,7 +10,9 @@ import {
10 10 parseMaterialNeighborConnectState,
11 11 worldTileToChunkLocal,
12 12 neighborConnectRefreshTiles,
13 listValidNeighborConnectCornerMasks
13 cardinalConnectionSuffix,
14 composeNamedNeighborConnectState,
15 parseNamedNeighborConnectMask
14 16 } from "../../shared/world/neighborConnect.js";
15 17 import { CENTRAL_EXCHANGE_STAFF, pickExchangeClerk } from "../../shared/market/exchange.js";
16 18 import { MobId, normalizeSpawnerConfig, isCentralExchangeSpawner } from "../../shared/definitions/mobs.js";
@@ -31,26 +33,32 @@ test("neighbor connect masks cover all NESW combinations", () =>
31 33 assert.equal(mask, NeighborBit.n | NeighborBit.e);
32 34 });
33 35
34 test("corner-aware neighbor connect sets diagonals only with both cardinals", () =>
36 test("named wall connection suffixes follow nesw / we / esw / wne", () =>
35 37 {
36 const isL = (dx, dy) => (dx === 1 && dy === 0) || (dx === 0 && dy === 1);
38 assert.equal(cardinalConnectionSuffix(0), "");
39 assert.equal(cardinalConnectionSuffix(NeighborBit.e | NeighborBit.w), "we");
40 assert.equal(cardinalConnectionSuffix(NeighborBit.n | NeighborBit.s), "ns");
37 41 assert.equal(
38 computeNeighborConnectMask(isL, { corners: true }),
39 NeighborBit.e | NeighborBit.s
42 cardinalConnectionSuffix(NeighborBit.e | NeighborBit.s | NeighborBit.w),
43 "esw"
40 44 );
41 const isSolid2x2 = (dx, dy) =>
42 (dx === 1 && dy === 0)
43 || (dx === 0 && dy === 1)
44 || (dx === 1 && dy === 1);
45 45 assert.equal(
46 computeNeighborConnectMask(isSolid2x2, { corners: true }),
47 NeighborBit.e | NeighborBit.s | NeighborBit.se
46 cardinalConnectionSuffix(NeighborBit.w | NeighborBit.n | NeighborBit.e),
47 "wne"
48 48 );
49 assert.equal(listValidNeighborConnectCornerMasks().length, 47);
50 assert.deepEqual(
51 neighborConnectRefreshTiles(5, 5, { corners: true }).length,
52 9
49 assert.equal(
50 cardinalConnectionSuffix(NeighborBit.n | NeighborBit.e | NeighborBit.s | NeighborBit.w),
51 "nesw"
52 );
53 const def = { key: "thick-stone-wall", neighborConnect: true, neighborConnectNamed: true };
54 assert.equal(composeNamedNeighborConnectState(def, 0), "thick-stone-wall");
55 assert.equal(
56 composeNamedNeighborConnectState(def, NeighborBit.e | NeighborBit.s | NeighborBit.w),
57 "thick-stone-wall-esw"
53 58 );
59 assert.equal(parseNamedNeighborConnectMask("thick-stone-wall-wne", def), NeighborBit.w | NeighborBit.n | NeighborBit.e);
60 assert.equal(parseNamedNeighborConnectMask("14", def), 14);
61 assert.deepEqual(neighborConnectRefreshTiles(15, 8).length, 5);
54 62 });
55 63
56 64 test("material neighbor-connect states keep wood id and only match same material", () =>
@@ -10,7 +10,9 @@ import {
10 parseMaterialNeighborConnectState,
10 parseMaterialNeighborConnectState,
11 worldTileToChunkLocal,
11 worldTileToChunkLocal,
12 neighborConnectRefreshTiles,
12 neighborConnectRefreshTiles,
13 listValidNeighborConnectCornerMasks
13 cardinalConnectionSuffix,
14 composeNamedNeighborConnectState,
15 parseNamedNeighborConnectMask
14} from "../../shared/world/neighborConnect.js";
16} from "../../shared/world/neighborConnect.js";
15import { CENTRAL_EXCHANGE_STAFF, pickExchangeClerk } from "../../shared/market/exchange.js";
17import { CENTRAL_EXCHANGE_STAFF, pickExchangeClerk } from "../../shared/market/exchange.js";
16import { MobId, normalizeSpawnerConfig, isCentralExchangeSpawner } from "../../shared/definitions/mobs.js";
18import { MobId, normalizeSpawnerConfig, isCentralExchangeSpawner } from "../../shared/definitions/mobs.js";
@@ -31,26 +33,32 @@ test("neighbor connect masks cover all NESW combinations", () =>
31 assert.equal(mask, NeighborBit.n | NeighborBit.e);
33 assert.equal(mask, NeighborBit.n | NeighborBit.e);
32});
34});
33
35
34test("corner-aware neighbor connect sets diagonals only with both cardinals", () =>
36test("named wall connection suffixes follow nesw / we / esw / wne", () =>
35{
37{
36 const isL = (dx, dy) => (dx === 1 && dy === 0) || (dx === 0 && dy === 1);
38 assert.equal(cardinalConnectionSuffix(0), "");
39 assert.equal(cardinalConnectionSuffix(NeighborBit.e | NeighborBit.w), "we");
40 assert.equal(cardinalConnectionSuffix(NeighborBit.n | NeighborBit.s), "ns");
37 assert.equal(
41 assert.equal(
38 computeNeighborConnectMask(isL, { corners: true }),
42 cardinalConnectionSuffix(NeighborBit.e | NeighborBit.s | NeighborBit.w),
39 NeighborBit.e | NeighborBit.s
43 "esw"
40 );
44 );
41 const isSolid2x2 = (dx, dy) =>
42 (dx === 1 && dy === 0)
43 || (dx === 0 && dy === 1)
44 || (dx === 1 && dy === 1);
45 assert.equal(
45 assert.equal(
46 computeNeighborConnectMask(isSolid2x2, { corners: true }),
46 cardinalConnectionSuffix(NeighborBit.w | NeighborBit.n | NeighborBit.e),
47 NeighborBit.e | NeighborBit.s | NeighborBit.se
47 "wne"
48 );
48 );
49 assert.equal(listValidNeighborConnectCornerMasks().length, 47);
49 assert.equal(
50 assert.deepEqual(
50 cardinalConnectionSuffix(NeighborBit.n | NeighborBit.e | NeighborBit.s | NeighborBit.w),
51 neighborConnectRefreshTiles(5, 5, { corners: true }).length,
51 "nesw"
52 9
52 );
53 const def = { key: "thick-stone-wall", neighborConnect: true, neighborConnectNamed: true };
54 assert.equal(composeNamedNeighborConnectState(def, 0), "thick-stone-wall");
55 assert.equal(
56 composeNamedNeighborConnectState(def, NeighborBit.e | NeighborBit.s | NeighborBit.w),
57 "thick-stone-wall-esw"
53 );
58 );
59 assert.equal(parseNamedNeighborConnectMask("thick-stone-wall-wne", def), NeighborBit.w | NeighborBit.n | NeighborBit.e);
60 assert.equal(parseNamedNeighborConnectMask("14", def), 14);
61 assert.deepEqual(neighborConnectRefreshTiles(15, 8).length, 5);
54});
62});
55
63
56test("material neighbor-connect states keep wood id and only match same material", () =>
64test("material neighbor-connect states keep wood id and only match same material", () =>
server/tests/season.test.js +2 −2
@@ -143,8 +143,8 @@ test("catalog exposes seasonal grass and pine-tree states", () =>
143 143 assert.equal(catalog.blocks["pine-tree"].defaultState, "summer");
144 144 assert.equal(catalog.blocks["pine-tree"].outdoor, true);
145 145 assert.equal(catalog.blocks["pine-tree"].seasonal, true);
146 assert.equal(catalog.blocks["stone-wall"].outdoor, false);
147 assert.equal(catalog.blocks["stone-wall"].seasonal, false);
146 assert.equal(catalog.blocks["thick-stone-wall"].outdoor, false);
147 assert.equal(catalog.blocks["thick-stone-wall"].seasonal, false);
148 148 assert.ok(catalog.images.some((image) => image.name === "block:boulder:boulder-winter"));
149 149 assert.ok(catalog.images.some((image) => image.name === "block:boulder:copper-ore-winter"));
150 150 assert.ok(catalog.images.some((image) => image.url.includes("boulder-ore-winter.png")));
@@ -143,8 +143,8 @@ test("catalog exposes seasonal grass and pine-tree states", () =>
143 assert.equal(catalog.blocks["pine-tree"].defaultState, "summer");
143 assert.equal(catalog.blocks["pine-tree"].defaultState, "summer");
144 assert.equal(catalog.blocks["pine-tree"].outdoor, true);
144 assert.equal(catalog.blocks["pine-tree"].outdoor, true);
145 assert.equal(catalog.blocks["pine-tree"].seasonal, true);
145 assert.equal(catalog.blocks["pine-tree"].seasonal, true);
146 assert.equal(catalog.blocks["stone-wall"].outdoor, false);
146 assert.equal(catalog.blocks["thick-stone-wall"].outdoor, false);
147 assert.equal(catalog.blocks["stone-wall"].seasonal, false);
147 assert.equal(catalog.blocks["thick-stone-wall"].seasonal, false);
148 assert.ok(catalog.images.some((image) => image.name === "block:boulder:boulder-winter"));
148 assert.ok(catalog.images.some((image) => image.name === "block:boulder:boulder-winter"));
149 assert.ok(catalog.images.some((image) => image.name === "block:boulder:copper-ore-winter"));
149 assert.ok(catalog.images.some((image) => image.name === "block:boulder:copper-ore-winter"));
150 assert.ok(catalog.images.some((image) => image.url.includes("boulder-ore-winter.png")));
150 assert.ok(catalog.images.some((image) => image.url.includes("boulder-ore-winter.png")));
server/tests/tile-states.test.js +24 −15
@@ -5,7 +5,7 @@ import { join } from "node:path";
5 5 import { buildSpriteCatalog } from "../src/assets/spriteCatalog.js";
6 6 import { cycleState, isCompanionSpriteStem, parseSpriteStem, resolveState } from "../../shared/definitions/tileSprites.js";
7 7 import { decodeChunkBlob, createEmptyChunkData } from "../src/world/chunkData.js";
8 import { GroundType, BlockType, normalizeGround, setBlockVisualState } from "../../shared/definitions/tiles.js";
8 import { GroundType, BlockType, normalizeGround, normalizeBlock, setBlockVisualState } from "../../shared/definitions/tiles.js";
9 9 import { diffChunks } from "../src/editor/service.js";
10 10
11 11 const ASSET_ROOT = join(fileURLToPath(new URL("../../client/assets", import.meta.url)));
@@ -13,15 +13,15 @@ const ASSET_ROOT = join(fileURLToPath(new URL("../../client/assets", import.meta
13 13 test("sprite folders expose alphabetical states; first is default", () =>
14 14 {
15 15 const catalog = buildSpriteCatalog(ASSET_ROOT);
16 assert.equal(catalog.blocks["stone-wall"].states.length, 47);
17 assert.equal(catalog.blocks["stone-wall"].defaultState, "0");
18 assert.ok(catalog.blocks["stone-wall"].states.includes("15"));
19 assert.ok(catalog.blocks["stone-wall"].states.includes("38"));
20 assert.ok(catalog.blocks["stone-wall"].states.includes("255"));
21 assert.ok(catalog.images.some((image) => image.name === "block:stone-wall:255"));
22 assert.equal(catalog.blocks["stone-wall-thin"].states.length, 47);
23 assert.equal(catalog.blocks["stone-wall-thin"].defaultState, "0");
24 assert.ok(catalog.images.some((image) => image.name === "block:stone-wall-thin:38"));
16 assert.equal(catalog.blocks["thick-stone-wall"].defaultState, "thick-stone-wall");
17 assert.ok(catalog.blocks["thick-stone-wall"].states.includes("thick-stone-wall"));
18 assert.ok(catalog.blocks["thick-stone-wall"].states.includes("thick-stone-wall-esw"));
19 assert.ok(catalog.blocks["thick-stone-wall"].states.includes("thick-stone-wall-nesw"));
20 assert.equal(catalog.blocks["thick-stone-wall"].states.length, 16);
21 assert.ok(catalog.images.some((image) => image.name === "block:thick-stone-wall:thick-stone-wall-nesw"));
22 assert.equal(catalog.blocks["thin-stone-wall"].defaultState, "thin-stone-wall");
23 assert.equal(catalog.blocks["thin-stone-wall"].states.length, 16);
24 assert.ok(catalog.images.some((image) => image.name === "block:thin-stone-wall:thin-stone-wall-wne"));
25 25 assert.equal(catalog.images[0].subImgTotal, 1);
26 26 assert.equal(catalog.images[0].perRow, 1);
27 27 });
@@ -121,17 +121,26 @@ test("gameplay can set a block visual state", () =>
121 121 {
122 122 const data = createEmptyChunkData();
123 123 data.blocks[0] = { typeId: BlockType.stoneWall, state: null, config: null };
124 setBlockVisualState(data.blocks[0], "2");
125 assert.equal(data.blocks[0].state, "2");
124 setBlockVisualState(data.blocks[0], "thick-stone-wall-e");
125 assert.equal(data.blocks[0].state, "thick-stone-wall-e");
126 126 });
127 127
128 128 test("chunk diffs mention visual state changes", () =>
129 129 {
130 130 const previous = createEmptyChunkData();
131 131 const next = createEmptyChunkData();
132 previous.blocks[0] = { typeId: BlockType.stoneWall, state: "1", config: null };
133 next.blocks[0] = { typeId: BlockType.stoneWall, state: "2", config: null };
132 previous.blocks[0] = { typeId: BlockType.stoneWall, state: "thick-stone-wall-n", config: null };
133 next.blocks[0] = { typeId: BlockType.stoneWall, state: "thick-stone-wall-e", config: null };
134 134 const bullets = diffChunks(0, 0, previous, next, true);
135 assert.ok(bullets.some((line) => line.includes("state changed from 1 to 2")));
135 assert.ok(bullets.some((line) =>
136 line.includes("state changed from thick-stone-wall-n to thick-stone-wall-e")));
136 137 assert.equal(normalizeGround(GroundType.grass).typeId, GroundType.grass);
137 138 });
139
140 test("normalizeBlock migrates legacy numeric wall states to named suffixes", () =>
141 {
142 const migrated = normalizeBlock({ typeId: BlockType.stoneWall, state: "14", config: null });
143 assert.equal(migrated.state, "thick-stone-wall-esw");
144 const alone = normalizeBlock({ typeId: BlockType.stoneWallThin, state: "0", config: null });
145 assert.equal(alone.state, "thin-stone-wall");
146 });
@@ -5,7 +5,7 @@ import { join } from "node:path";
5import { buildSpriteCatalog } from "../src/assets/spriteCatalog.js";
5import { buildSpriteCatalog } from "../src/assets/spriteCatalog.js";
6import { cycleState, isCompanionSpriteStem, parseSpriteStem, resolveState } from "../../shared/definitions/tileSprites.js";
6import { cycleState, isCompanionSpriteStem, parseSpriteStem, resolveState } from "../../shared/definitions/tileSprites.js";
7import { decodeChunkBlob, createEmptyChunkData } from "../src/world/chunkData.js";
7import { decodeChunkBlob, createEmptyChunkData } from "../src/world/chunkData.js";
8import { GroundType, BlockType, normalizeGround, setBlockVisualState } from "../../shared/definitions/tiles.js";
8import { GroundType, BlockType, normalizeGround, normalizeBlock, setBlockVisualState } from "../../shared/definitions/tiles.js";
9import { diffChunks } from "../src/editor/service.js";
9import { diffChunks } from "../src/editor/service.js";
10
10
11const ASSET_ROOT = join(fileURLToPath(new URL("../../client/assets", import.meta.url)));
11const ASSET_ROOT = join(fileURLToPath(new URL("../../client/assets", import.meta.url)));
@@ -13,15 +13,15 @@ const ASSET_ROOT = join(fileURLToPath(new URL("../../client/assets", import.meta
13test("sprite folders expose alphabetical states; first is default", () =>
13test("sprite folders expose alphabetical states; first is default", () =>
14{
14{
15 const catalog = buildSpriteCatalog(ASSET_ROOT);
15 const catalog = buildSpriteCatalog(ASSET_ROOT);
16 assert.equal(catalog.blocks["stone-wall"].states.length, 47);
16 assert.equal(catalog.blocks["thick-stone-wall"].defaultState, "thick-stone-wall");
17 assert.equal(catalog.blocks["stone-wall"].defaultState, "0");
17 assert.ok(catalog.blocks["thick-stone-wall"].states.includes("thick-stone-wall"));
18 assert.ok(catalog.blocks["stone-wall"].states.includes("15"));
18 assert.ok(catalog.blocks["thick-stone-wall"].states.includes("thick-stone-wall-esw"));
19 assert.ok(catalog.blocks["stone-wall"].states.includes("38"));
19 assert.ok(catalog.blocks["thick-stone-wall"].states.includes("thick-stone-wall-nesw"));
20 assert.ok(catalog.blocks["stone-wall"].states.includes("255"));
20 assert.equal(catalog.blocks["thick-stone-wall"].states.length, 16);
21 assert.ok(catalog.images.some((image) => image.name === "block:stone-wall:255"));
21 assert.ok(catalog.images.some((image) => image.name === "block:thick-stone-wall:thick-stone-wall-nesw"));
22 assert.equal(catalog.blocks["stone-wall-thin"].states.length, 47);
22 assert.equal(catalog.blocks["thin-stone-wall"].defaultState, "thin-stone-wall");
23 assert.equal(catalog.blocks["stone-wall-thin"].defaultState, "0");
23 assert.equal(catalog.blocks["thin-stone-wall"].states.length, 16);
24 assert.ok(catalog.images.some((image) => image.name === "block:stone-wall-thin:38"));
24 assert.ok(catalog.images.some((image) => image.name === "block:thin-stone-wall:thin-stone-wall-wne"));
25 assert.equal(catalog.images[0].subImgTotal, 1);
25 assert.equal(catalog.images[0].subImgTotal, 1);
26 assert.equal(catalog.images[0].perRow, 1);
26 assert.equal(catalog.images[0].perRow, 1);
27});
27});
@@ -121,17 +121,26 @@ test("gameplay can set a block visual state", () =>
121{
121{
122 const data = createEmptyChunkData();
122 const data = createEmptyChunkData();
123 data.blocks[0] = { typeId: BlockType.stoneWall, state: null, config: null };
123 data.blocks[0] = { typeId: BlockType.stoneWall, state: null, config: null };
124 setBlockVisualState(data.blocks[0], "2");
124 setBlockVisualState(data.blocks[0], "thick-stone-wall-e");
125 assert.equal(data.blocks[0].state, "2");
125 assert.equal(data.blocks[0].state, "thick-stone-wall-e");
126});
126});
127
127
128test("chunk diffs mention visual state changes", () =>
128test("chunk diffs mention visual state changes", () =>
129{
129{
130 const previous = createEmptyChunkData();
130 const previous = createEmptyChunkData();
131 const next = createEmptyChunkData();
131 const next = createEmptyChunkData();
132 previous.blocks[0] = { typeId: BlockType.stoneWall, state: "1", config: null };
132 previous.blocks[0] = { typeId: BlockType.stoneWall, state: "thick-stone-wall-n", config: null };
133 next.blocks[0] = { typeId: BlockType.stoneWall, state: "2", config: null };
133 next.blocks[0] = { typeId: BlockType.stoneWall, state: "thick-stone-wall-e", config: null };
134 const bullets = diffChunks(0, 0, previous, next, true);
134 const bullets = diffChunks(0, 0, previous, next, true);
135 assert.ok(bullets.some((line) => line.includes("state changed from 1 to 2")));
135 assert.ok(bullets.some((line) =>
136 line.includes("state changed from thick-stone-wall-n to thick-stone-wall-e")));
136 assert.equal(normalizeGround(GroundType.grass).typeId, GroundType.grass);
137 assert.equal(normalizeGround(GroundType.grass).typeId, GroundType.grass);
137});
138});
139
140test("normalizeBlock migrates legacy numeric wall states to named suffixes", () =>
141{
142 const migrated = normalizeBlock({ typeId: BlockType.stoneWall, state: "14", config: null });
143 assert.equal(migrated.state, "thick-stone-wall-esw");
144 const alone = normalizeBlock({ typeId: BlockType.stoneWallThin, state: "0", config: null });
145 assert.equal(alone.state, "thin-stone-wall");
146});
shared/definitions/tiles.js +22 −12
@@ -25,7 +25,9 @@ import { isSeasonalStumpState, SeasonId } from "../world/season.js";
25 25 import
26 26 {
27 27 composeNeighborConnectState,
28 definitionUsesMaterialNeighborConnect
28 definitionUsesMaterialNeighborConnect,
29 definitionUsesNamedNeighborConnect,
30 parseNeighborConnectMaskFromState
29 31 } from "../world/neighborConnect.js";
30 32
31 33 export const GroundType = Object.freeze({
@@ -169,7 +171,7 @@ function treeBlockDef (id, woodTier)
169 171 export const BLOCK_DEFINITIONS = Object.freeze({
170 172 [BlockType.stoneWall]: Object.freeze({
171 173 id: BlockType.stoneWall,
172 key: "stone-wall",
174 key: "thick-stone-wall",
173 175 displayName: "Thick stone wall",
174 176 passable: false,
175 177 lightRangeTiles: 0,
@@ -178,16 +180,13 @@ export const BLOCK_DEFINITIONS = Object.freeze({
178 180 animationSpeed: 0,
179 181 /** Building walls count as indoor (no seasonal art required). */
180 182 outdoor: false,
181 /**
182 * Autotile via N/E/S/W + diagonal corner bits (blob masks).
183 * Diagonals distinguish L-strip joins from solid 2×2 fills.
184 */
183 /** Autotile via named cardinal states (`thick-stone-wall`, `thick-stone-wall-esw`, …). */
185 184 neighborConnect: true,
186 neighborConnectCorners: true
185 neighborConnectNamed: true
187 186 }),
188 187 [BlockType.stoneWallThin]: Object.freeze({
189 188 id: BlockType.stoneWallThin,
190 key: "stone-wall-thin",
189 key: "thin-stone-wall",
191 190 displayName: "Thin stone wall",
192 191 passable: false,
193 192 lightRangeTiles: 0,
@@ -196,7 +195,7 @@ export const BLOCK_DEFINITIONS = Object.freeze({
196 195 animationSpeed: 0,
197 196 outdoor: false,
198 197 neighborConnect: true,
199 neighborConnectCorners: true
198 neighborConnectNamed: true
200 199 }),
201 200 [BlockType.torch]: Object.freeze({
202 201 id: BlockType.torch,
@@ -814,10 +813,14 @@ export function listPlaceableBlockPaletteEntries (catalog = null)
814 813 }
815 814 entries.push({
816 815 typeId: def.id,
817 state: "0",
816 state: definitionUsesNamedNeighborConnect(def) ? def.key : "0",
818 817 key: def.key,
819 818 displayName: def.displayName,
820 spriteName: spriteImageName("block", def.key, "0")
819 spriteName: spriteImageName(
820 "block",
821 def.key,
822 definitionUsesNamedNeighborConnect(def) ? def.key : "0"
823 )
821 824 });
822 825 continue;
823 826 }
@@ -985,7 +988,14 @@ export function normalizeBlock (value)
985 988 {
986 989 if (!value || !Number.isInteger(value.typeId))
987 990 { return null; }
988 return { typeId: value.typeId, state: value.state ?? null, config: value.config ?? null };
991 let state = value.state ?? null;
992 const def = BLOCK_DEFINITIONS[value.typeId];
993 if (def && definitionUsesNamedNeighborConnect(def) && state != null)
994 {
995 const mask = parseNeighborConnectMaskFromState(state, def);
996 state = composeNeighborConnectState(def, state, mask);
997 }
998 return { typeId: value.typeId, state, config: value.config ?? null };
989 999 }
990 1000
991 1001 export function setBlockVisualState (block, state)
@@ -25,7 +25,9 @@ import { isSeasonalStumpState, SeasonId } from "../world/season.js";
25import
25import
26{
26{
27 composeNeighborConnectState,
27 composeNeighborConnectState,
28 definitionUsesMaterialNeighborConnect
28 definitionUsesMaterialNeighborConnect,
29 definitionUsesNamedNeighborConnect,
30 parseNeighborConnectMaskFromState
29} from "../world/neighborConnect.js";
31} from "../world/neighborConnect.js";
30
32
31export const GroundType = Object.freeze({
33export const GroundType = Object.freeze({
@@ -169,7 +171,7 @@ function treeBlockDef (id, woodTier)
169export const BLOCK_DEFINITIONS = Object.freeze({
171export const BLOCK_DEFINITIONS = Object.freeze({
170 [BlockType.stoneWall]: Object.freeze({
172 [BlockType.stoneWall]: Object.freeze({
171 id: BlockType.stoneWall,
173 id: BlockType.stoneWall,
172 key: "stone-wall",
174 key: "thick-stone-wall",
173 displayName: "Thick stone wall",
175 displayName: "Thick stone wall",
174 passable: false,
176 passable: false,
175 lightRangeTiles: 0,
177 lightRangeTiles: 0,
@@ -178,16 +180,13 @@ export const BLOCK_DEFINITIONS = Object.freeze({
178 animationSpeed: 0,
180 animationSpeed: 0,
179 /** Building walls count as indoor (no seasonal art required). */
181 /** Building walls count as indoor (no seasonal art required). */
180 outdoor: false,
182 outdoor: false,
181 /**
183 /** Autotile via named cardinal states (`thick-stone-wall`, `thick-stone-wall-esw`, …). */
182 * Autotile via N/E/S/W + diagonal corner bits (blob masks).
183 * Diagonals distinguish L-strip joins from solid 2×2 fills.
184 */
185 neighborConnect: true,
184 neighborConnect: true,
186 neighborConnectCorners: true
185 neighborConnectNamed: true
187 }),
186 }),
188 [BlockType.stoneWallThin]: Object.freeze({
187 [BlockType.stoneWallThin]: Object.freeze({
189 id: BlockType.stoneWallThin,
188 id: BlockType.stoneWallThin,
190 key: "stone-wall-thin",
189 key: "thin-stone-wall",
191 displayName: "Thin stone wall",
190 displayName: "Thin stone wall",
192 passable: false,
191 passable: false,
193 lightRangeTiles: 0,
192 lightRangeTiles: 0,
@@ -196,7 +195,7 @@ export const BLOCK_DEFINITIONS = Object.freeze({
196 animationSpeed: 0,
195 animationSpeed: 0,
197 outdoor: false,
196 outdoor: false,
198 neighborConnect: true,
197 neighborConnect: true,
199 neighborConnectCorners: true
198 neighborConnectNamed: true
200 }),
199 }),
201 [BlockType.torch]: Object.freeze({
200 [BlockType.torch]: Object.freeze({
202 id: BlockType.torch,
201 id: BlockType.torch,
@@ -814,10 +813,14 @@ export function listPlaceableBlockPaletteEntries (catalog = null)
814 }
813 }
815 entries.push({
814 entries.push({
816 typeId: def.id,
815 typeId: def.id,
817 state: "0",
816 state: definitionUsesNamedNeighborConnect(def) ? def.key : "0",
818 key: def.key,
817 key: def.key,
819 displayName: def.displayName,
818 displayName: def.displayName,
820 spriteName: spriteImageName("block", def.key, "0")
819 spriteName: spriteImageName(
820 "block",
821 def.key,
822 definitionUsesNamedNeighborConnect(def) ? def.key : "0"
823 )
821 });
824 });
822 continue;
825 continue;
823 }
826 }
@@ -985,7 +988,14 @@ export function normalizeBlock (value)
985{
988{
986 if (!value || !Number.isInteger(value.typeId))
989 if (!value || !Number.isInteger(value.typeId))
987 { return null; }
990 { return null; }
988 return { typeId: value.typeId, state: value.state ?? null, config: value.config ?? null };
991 let state = value.state ?? null;
992 const def = BLOCK_DEFINITIONS[value.typeId];
993 if (def && definitionUsesNamedNeighborConnect(def) && state != null)
994 {
995 const mask = parseNeighborConnectMaskFromState(state, def);
996 state = composeNeighborConnectState(def, state, mask);
997 }
998 return { typeId: value.typeId, state, config: value.config ?? null };
989}
999}
990
1000
991export function setBlockVisualState (block, state)
1001export function setBlockVisualState (block, state)
shared/world/neighborConnect.js +111 −131
@@ -1,53 +1,124 @@
1 1 /**
2 * Generic neighbor-connect autotile masks (N/E/S/W, optional diagonals).
3 * Stored as block/ground `state` string for bakeable sprite variants.
2 * Generic neighbor-connect autotile masks (N/E/S/W).
3 * Stored as block/ground `state` for bakeable sprite variants.
4 4 *
5 * Cardinal-only types (tables, fences): mask `"0"`…`"15"`.
6 * Corner-aware types (stone walls): also set NE/SE/SW/NW when both adjacent
7 * cardinals join *and* the diagonal tile joins — distinguishes L-strip from solid 2×2.
5 * - Cardinal numeric (tables/fences/counters): `"0"`…`"15"` (or `pine-3` with material).
6 * - Named cardinal (stone walls): `"thick-stone-wall"`, `"thick-stone-wall-esw"`, …
8 7 */
9 8
10 9 export const NeighborBit = Object.freeze({
11 10 n: 1,
12 11 e: 2,
13 12 s: 4,
14 w: 8,
15 ne: 16,
16 se: 32,
17 sw: 64,
18 nw: 128
13 w: 8
19 14 });
20 15
21 16 export const NEIGHBOR_CONNECT_MASK_COUNT = 16;
22 export const NEIGHBOR_CONNECT_CORNER_MASK_MAX = 255;
23 17
24 export function neighborConnectMask ({
25 n = false,
26 e = false,
27 s = false,
28 w = false,
29 ne = false,
30 se = false,
31 sw = false,
32 nw = false
33 } = {})
18 export function neighborConnectMask ({ n = false, e = false, s = false, w = false } = {})
34 19 {
35 20 return (n ? NeighborBit.n : 0)
36 21 | (e ? NeighborBit.e : 0)
37 22 | (s ? NeighborBit.s : 0)
38 | (w ? NeighborBit.w : 0)
39 | (ne ? NeighborBit.ne : 0)
40 | (se ? NeighborBit.se : 0)
41 | (sw ? NeighborBit.sw : 0)
42 | (nw ? NeighborBit.nw : 0);
23 | (w ? NeighborBit.w : 0);
43 24 }
44 25
45 26 export function neighborConnectStateName (mask)
46 27 {
47 const m = Math.max(0, Math.min(NEIGHBOR_CONNECT_CORNER_MASK_MAX, mask | 0));
28 const m = Math.max(0, Math.min(15, mask | 0));
48 29 return String(m);
49 30 }
50 31
32 /**
33 * Cardinal connection suffix for named wall sprites.
34 * Opposite runs: `ns`, `we`. Three-ways clockwise from the side after the gap
35 * (`esw` = T open north, `wne` = T open south). Full: `nesw`. Alone: `""`.
36 */
37 export function cardinalConnectionSuffix (mask)
38 {
39 const m = (mask | 0) & 15;
40 if (m === 0)
41 { return ""; }
42 const n = (m & NeighborBit.n) !== 0;
43 const e = (m & NeighborBit.e) !== 0;
44 const s = (m & NeighborBit.s) !== 0;
45 const w = (m & NeighborBit.w) !== 0;
46 const count = (n ? 1 : 0) + (e ? 1 : 0) + (s ? 1 : 0) + (w ? 1 : 0);
47 if (count === 1)
48 {
49 if (n)
50 { return "n"; }
51 if (e)
52 { return "e"; }
53 if (s)
54 { return "s"; }
55 return "w";
56 }
57 if (count === 2)
58 {
59 if (n && s)
60 { return "ns"; }
61 if (e && w)
62 { return "we"; }
63 if (n && e)
64 { return "ne"; }
65 if (e && s)
66 { return "es"; }
67 if (s && w)
68 { return "sw"; }
69 return "wn";
70 }
71 if (count === 3)
72 {
73 const bits = [NeighborBit.n, NeighborBit.e, NeighborBit.s, NeighborBit.w];
74 const letters = ["n", "e", "s", "w"];
75 let miss = 0;
76 for (let i = 0; i < 4; i++)
77 {
78 if ((m & bits[i]) === 0)
79 { miss = i; }
80 }
81 return letters[(miss + 1) % 4] + letters[(miss + 2) % 4] + letters[(miss + 3) % 4];
82 }
83 return "nesw";
84 }
85
86 export function composeNamedNeighborConnectState (def, mask)
87 {
88 const key = def?.key ?? "block";
89 const suffix = cardinalConnectionSuffix(mask);
90 return suffix ? `${key}-${suffix}` : key;
91 }
92
93 /** Parse named (`thick-stone-wall-esw`) or bare (`esw`) or legacy numeric states. */
94 export function parseNamedNeighborConnectMask (state, def = null)
95 {
96 const s = String(state ?? "").trim().toLowerCase();
97 if (!s)
98 { return 0; }
99 if (/^\d+$/.test(s))
100 { return Math.max(0, Math.min(15, Number(s) | 0)); }
101 const key = String(def?.key ?? "").toLowerCase();
102 if (key && s === key)
103 { return 0; }
104 let suffix = s;
105 if (key && s.startsWith(`${key}-`))
106 { suffix = s.slice(key.length + 1); }
107 let mask = 0;
108 for (const ch of suffix)
109 {
110 if (ch === "n")
111 { mask |= NeighborBit.n; }
112 else if (ch === "e")
113 { mask |= NeighborBit.e; }
114 else if (ch === "s")
115 { mask |= NeighborBit.s; }
116 else if (ch === "w")
117 { mask |= NeighborBit.w; }
118 }
119 return mask;
120 }
121
51 122 export function parseNeighborConnectMask (state)
52 123 {
53 124 if (state == null || state === "")
@@ -55,7 +126,7 @@ export function parseNeighborConnectMask (state)
55 126 const n = Number(state);
56 127 if (!Number.isFinite(n))
57 128 { return 0; }
58 return Math.max(0, Math.min(NEIGHBOR_CONNECT_CORNER_MASK_MAX, n | 0));
129 return Math.max(0, Math.min(15, n | 0));
59 130 }
60 131
61 132 export function hasNeighborBit (mask, bit)
@@ -71,36 +142,10 @@ export const NEIGHBOR_CARDINALS = Object.freeze([
71 142 Object.freeze({ bit: NeighborBit.w, dx: -1, dy: 0, key: "w" })
72 143 ]);
73 144
74 /**
75 * Diagonal offsets. A diagonal bit is only meaningful when both adjacent
76 * cardinals are also connected (blob / “47-tile” corner fill).
77 */
78 export const NEIGHBOR_DIAGONALS = Object.freeze([
79 Object.freeze({
80 bit: NeighborBit.ne, dx: 1, dy: -1, key: "ne",
81 needA: NeighborBit.n, needB: NeighborBit.e
82 }),
83 Object.freeze({
84 bit: NeighborBit.se, dx: 1, dy: 1, key: "se",
85 needA: NeighborBit.s, needB: NeighborBit.e
86 }),
87 Object.freeze({
88 bit: NeighborBit.sw, dx: -1, dy: 1, key: "sw",
89 needA: NeighborBit.s, needB: NeighborBit.w
90 }),
91 Object.freeze({
92 bit: NeighborBit.nw, dx: -1, dy: -1, key: "nw",
93 needA: NeighborBit.n, needB: NeighborBit.w
94 })
95 ]);
96
97 145 /**
98 146 * @param {(dx: number, dy: number) => boolean} isConnectedNeighbor
99 * true when the adjacent tile should join (same connectable type).
100 * @param {{ corners?: boolean }} [options]
101 * when `corners`, also sample diagonals (only set if both cardinals join).
102 147 */
103 export function computeNeighborConnectMask (isConnectedNeighbor, options = {})
148 export function computeNeighborConnectMask (isConnectedNeighbor)
104 149 {
105 150 let mask = 0;
106 151 for (const dir of NEIGHBOR_CARDINALS)
@@ -108,57 +153,17 @@ export function computeNeighborConnectMask (isConnectedNeighbor, options = {})
108 153 if (isConnectedNeighbor(dir.dx, dir.dy))
109 154 { mask |= dir.bit; }
110 155 }
111 if (options.corners)
112 {
113 for (const dir of NEIGHBOR_DIAGONALS)
114 {
115 if ((mask & dir.needA) === 0 || (mask & dir.needB) === 0)
116 { continue; }
117 if (isConnectedNeighbor(dir.dx, dir.dy))
118 { mask |= dir.bit; }
119 }
120 }
121 156 return mask;
122 157 }
123 158
124 /**
125 * All valid blob masks: every cardinal combo, with each relevant corner free or filled.
126 * Yields 47 values (classic blob tileset).
127 */
128 export function listValidNeighborConnectCornerMasks ()
129 {
130 const out = [];
131 for (let cardinal = 0; cardinal < NEIGHBOR_CONNECT_MASK_COUNT; cardinal++)
132 {
133 const cornerBits = [];
134 for (const dir of NEIGHBOR_DIAGONALS)
135 {
136 if ((cardinal & dir.needA) && (cardinal & dir.needB))
137 { cornerBits.push(dir.bit); }
138 }
139 const n = cornerBits.length;
140 for (let subset = 0; subset < (1 << n); subset++)
141 {
142 let mask = cardinal;
143 for (let i = 0; i < n; i++)
144 {
145 if (subset & (1 << i))
146 { mask |= cornerBits[i]; }
147 }
148 out.push(mask);
149 }
150 }
151 return out;
152 }
153
154 159 export function definitionUsesNeighborConnect (def)
155 160 {
156 161 return !!def?.neighborConnect;
157 162 }
158 163
159 export function definitionUsesNeighborConnectCorners (def)
164 export function definitionUsesNamedNeighborConnect (def)
160 165 {
161 return !!def?.neighborConnect && !!def?.neighborConnectCorners;
166 return !!def?.neighborConnect && !!def?.neighborConnectNamed;
162 167 }
163 168
164 169 export function definitionUsesMaterialNeighborConnect (def)
@@ -174,20 +179,9 @@ function clampCardinalMask (value)
174 179 return Math.max(0, Math.min(15, n | 0));
175 180 }
176 181
177 function clampMaskForDef (value, def)
178 {
179 const n = Number(value);
180 if (!Number.isFinite(n))
181 { return 0; }
182 const max = definitionUsesNeighborConnectCorners(def)
183 ? NEIGHBOR_CONNECT_CORNER_MASK_MAX
184 : 15;
185 return Math.max(0, Math.min(max, n | 0));
186 }
187
188 182 /**
189 183 * Tables (and similar) store `${material}-${mask}` e.g. `pine-3`.
190 * Plain masks (`"7"`) stay valid for counters / walls.
184 * Plain masks (`"7"`) stay valid for counters.
191 185 */
192 186 export function parseMaterialNeighborConnectState (state, def = null)
193 187 {
@@ -214,12 +208,14 @@ export function parseMaterialNeighborConnectState (state, def = null)
214 208
215 209 export function composeNeighborConnectState (def, currentState, mask)
216 210 {
217 const m = clampMaskForDef(mask, def);
211 const m = clampCardinalMask(mask);
218 212 if (definitionUsesMaterialNeighborConnect(def))
219 213 {
220 214 const { material } = parseMaterialNeighborConnectState(currentState, def);
221 215 return `${material}-${m}`;
222 216 }
217 if (definitionUsesNamedNeighborConnect(def))
218 { return composeNamedNeighborConnectState(def, m); }
223 219 return neighborConnectStateName(m);
224 220 }
225 221
@@ -240,14 +236,13 @@ export function parseNeighborConnectMaskFromState (state, def = null)
240 236 {
241 237 if (definitionUsesMaterialNeighborConnect(def))
242 238 { return parseMaterialNeighborConnectState(state, def).mask; }
239 if (definitionUsesNamedNeighborConnect(def))
240 { return parseNamedNeighborConnectMask(state, def); }
243 241 return parseNeighborConnectMask(state);
244 242 }
245 243
246 244 /**
247 245 * Map a world tile to chunk coords + local tile (handles negative world tiles).
248 * @param {number} tileX
249 * @param {number} tileY
250 * @param {number} [chunkSize=16]
251 246 */
252 247 export function worldTileToChunkLocal (tileX, tileY, chunkSize = 16)
253 248 {
@@ -262,29 +257,14 @@ export function worldTileToChunkLocal (tileX, tileY, chunkSize = 16)
262 257 };
263 258 }
264 259
265 /**
266 * Self + cardinal neighbors (and diagonals when `corners`) in world tile space.
267 * @param {number} tileX
268 * @param {number} tileY
269 * @param {{ corners?: boolean }} [options]
270 */
271 export function neighborConnectRefreshTiles (tileX, tileY, options = {})
260 /** Self + cardinal neighbors in world tile space (for connect mask refresh). */
261 export function neighborConnectRefreshTiles (tileX, tileY)
272 262 {
273 const tiles = [
263 return [
274 264 [tileX, tileY],
275 265 [tileX, tileY - 1],
276 266 [tileX + 1, tileY],
277 267 [tileX, tileY + 1],
278 268 [tileX - 1, tileY]
279 269 ];
280 if (options.corners)
281 {
282 tiles.push(
283 [tileX + 1, tileY - 1],
284 [tileX + 1, tileY + 1],
285 [tileX - 1, tileY + 1],
286 [tileX - 1, tileY - 1]
287 );
288 }
289 return tiles;
290 270 }
@@ -1,53 +1,124 @@
1/**
1/**
2 * Generic neighbor-connect autotile masks (N/E/S/W, optional diagonals).
2 * Generic neighbor-connect autotile masks (N/E/S/W).
3 * Stored as block/ground `state` string for bakeable sprite variants.
3 * Stored as block/ground `state` for bakeable sprite variants.
4 *
4 *
5 * Cardinal-only types (tables, fences): mask `"0"`…`"15"`.
5 * - Cardinal numeric (tables/fences/counters): `"0"`…`"15"` (or `pine-3` with material).
6 * Corner-aware types (stone walls): also set NE/SE/SW/NW when both adjacent
6 * - Named cardinal (stone walls): `"thick-stone-wall"`, `"thick-stone-wall-esw"`, …
7 * cardinals join *and* the diagonal tile joins — distinguishes L-strip from solid 2×2.
8 */
7 */
9
8
10export const NeighborBit = Object.freeze({
9export const NeighborBit = Object.freeze({
11 n: 1,
10 n: 1,
12 e: 2,
11 e: 2,
13 s: 4,
12 s: 4,
14 w: 8,
13 w: 8
15 ne: 16,
16 se: 32,
17 sw: 64,
18 nw: 128
19});
14});
20
15
21export const NEIGHBOR_CONNECT_MASK_COUNT = 16;
16export const NEIGHBOR_CONNECT_MASK_COUNT = 16;
22export const NEIGHBOR_CONNECT_CORNER_MASK_MAX = 255;
23
17
24export function neighborConnectMask ({
18export function neighborConnectMask ({ n = false, e = false, s = false, w = false } = {})
25 n = false,
26 e = false,
27 s = false,
28 w = false,
29 ne = false,
30 se = false,
31 sw = false,
32 nw = false
33} = {})
34{
19{
35 return (n ? NeighborBit.n : 0)
20 return (n ? NeighborBit.n : 0)
36 | (e ? NeighborBit.e : 0)
21 | (e ? NeighborBit.e : 0)
37 | (s ? NeighborBit.s : 0)
22 | (s ? NeighborBit.s : 0)
38 | (w ? NeighborBit.w : 0)
23 | (w ? NeighborBit.w : 0);
39 | (ne ? NeighborBit.ne : 0)
40 | (se ? NeighborBit.se : 0)
41 | (sw ? NeighborBit.sw : 0)
42 | (nw ? NeighborBit.nw : 0);
43}
24}
44
25
45export function neighborConnectStateName (mask)
26export function neighborConnectStateName (mask)
46{
27{
47 const m = Math.max(0, Math.min(NEIGHBOR_CONNECT_CORNER_MASK_MAX, mask | 0));
28 const m = Math.max(0, Math.min(15, mask | 0));
48 return String(m);
29 return String(m);
49}
30}
50
31
32/**
33 * Cardinal connection suffix for named wall sprites.
34 * Opposite runs: `ns`, `we`. Three-ways clockwise from the side after the gap
35 * (`esw` = T open north, `wne` = T open south). Full: `nesw`. Alone: `""`.
36 */
37export function cardinalConnectionSuffix (mask)
38{
39 const m = (mask | 0) & 15;
40 if (m === 0)
41 { return ""; }
42 const n = (m & NeighborBit.n) !== 0;
43 const e = (m & NeighborBit.e) !== 0;
44 const s = (m & NeighborBit.s) !== 0;
45 const w = (m & NeighborBit.w) !== 0;
46 const count = (n ? 1 : 0) + (e ? 1 : 0) + (s ? 1 : 0) + (w ? 1 : 0);
47 if (count === 1)
48 {
49 if (n)
50 { return "n"; }
51 if (e)
52 { return "e"; }
53 if (s)
54 { return "s"; }
55 return "w";
56 }
57 if (count === 2)
58 {
59 if (n && s)
60 { return "ns"; }
61 if (e && w)
62 { return "we"; }
63 if (n && e)
64 { return "ne"; }
65 if (e && s)
66 { return "es"; }
67 if (s && w)
68 { return "sw"; }
69 return "wn";
70 }
71 if (count === 3)
72 {
73 const bits = [NeighborBit.n, NeighborBit.e, NeighborBit.s, NeighborBit.w];
74 const letters = ["n", "e", "s", "w"];
75 let miss = 0;
76 for (let i = 0; i < 4; i++)
77 {
78 if ((m & bits[i]) === 0)
79 { miss = i; }
80 }
81 return letters[(miss + 1) % 4] + letters[(miss + 2) % 4] + letters[(miss + 3) % 4];
82 }
83 return "nesw";
84}
85
86export function composeNamedNeighborConnectState (def, mask)
87{
88 const key = def?.key ?? "block";
89 const suffix = cardinalConnectionSuffix(mask);
90 return suffix ? `${key}-${suffix}` : key;
91}
92
93/** Parse named (`thick-stone-wall-esw`) or bare (`esw`) or legacy numeric states. */
94export function parseNamedNeighborConnectMask (state, def = null)
95{
96 const s = String(state ?? "").trim().toLowerCase();
97 if (!s)
98 { return 0; }
99 if (/^\d+$/.test(s))
100 { return Math.max(0, Math.min(15, Number(s) | 0)); }
101 const key = String(def?.key ?? "").toLowerCase();
102 if (key && s === key)
103 { return 0; }
104 let suffix = s;
105 if (key && s.startsWith(`${key}-`))
106 { suffix = s.slice(key.length + 1); }
107 let mask = 0;
108 for (const ch of suffix)
109 {
110 if (ch === "n")
111 { mask |= NeighborBit.n; }
112 else if (ch === "e")
113 { mask |= NeighborBit.e; }
114 else if (ch === "s")
115 { mask |= NeighborBit.s; }
116 else if (ch === "w")
117 { mask |= NeighborBit.w; }
118 }
119 return mask;
120}
121
51export function parseNeighborConnectMask (state)
122export function parseNeighborConnectMask (state)
52{
123{
53 if (state == null || state === "")
124 if (state == null || state === "")
@@ -55,7 +126,7 @@ export function parseNeighborConnectMask (state)
55 const n = Number(state);
126 const n = Number(state);
56 if (!Number.isFinite(n))
127 if (!Number.isFinite(n))
57 { return 0; }
128 { return 0; }
58 return Math.max(0, Math.min(NEIGHBOR_CONNECT_CORNER_MASK_MAX, n | 0));
129 return Math.max(0, Math.min(15, n | 0));
59}
130}
60
131
61export function hasNeighborBit (mask, bit)
132export function hasNeighborBit (mask, bit)
@@ -71,36 +142,10 @@ export const NEIGHBOR_CARDINALS = Object.freeze([
71 Object.freeze({ bit: NeighborBit.w, dx: -1, dy: 0, key: "w" })
142 Object.freeze({ bit: NeighborBit.w, dx: -1, dy: 0, key: "w" })
72]);
143]);
73
144
74/**
75 * Diagonal offsets. A diagonal bit is only meaningful when both adjacent
76 * cardinals are also connected (blob / “47-tile” corner fill).
77 */
78export const NEIGHBOR_DIAGONALS = Object.freeze([
79 Object.freeze({
80 bit: NeighborBit.ne, dx: 1, dy: -1, key: "ne",
81 needA: NeighborBit.n, needB: NeighborBit.e
82 }),
83 Object.freeze({
84 bit: NeighborBit.se, dx: 1, dy: 1, key: "se",
85 needA: NeighborBit.s, needB: NeighborBit.e
86 }),
87 Object.freeze({
88 bit: NeighborBit.sw, dx: -1, dy: 1, key: "sw",
89 needA: NeighborBit.s, needB: NeighborBit.w
90 }),
91 Object.freeze({
92 bit: NeighborBit.nw, dx: -1, dy: -1, key: "nw",
93 needA: NeighborBit.n, needB: NeighborBit.w
94 })
95]);
96
97/**
145/**
98 * @param {(dx: number, dy: number) => boolean} isConnectedNeighbor
146 * @param {(dx: number, dy: number) => boolean} isConnectedNeighbor
99 * true when the adjacent tile should join (same connectable type).
100 * @param {{ corners?: boolean }} [options]
101 * when `corners`, also sample diagonals (only set if both cardinals join).
102 */
147 */
103export function computeNeighborConnectMask (isConnectedNeighbor, options = {})
148export function computeNeighborConnectMask (isConnectedNeighbor)
104{
149{
105 let mask = 0;
150 let mask = 0;
106 for (const dir of NEIGHBOR_CARDINALS)
151 for (const dir of NEIGHBOR_CARDINALS)
@@ -108,57 +153,17 @@ export function computeNeighborConnectMask (isConnectedNeighbor, options = {})
108 if (isConnectedNeighbor(dir.dx, dir.dy))
153 if (isConnectedNeighbor(dir.dx, dir.dy))
109 { mask |= dir.bit; }
154 { mask |= dir.bit; }
110 }
155 }
111 if (options.corners)
112 {
113 for (const dir of NEIGHBOR_DIAGONALS)
114 {
115 if ((mask & dir.needA) === 0 || (mask & dir.needB) === 0)
116 { continue; }
117 if (isConnectedNeighbor(dir.dx, dir.dy))
118 { mask |= dir.bit; }
119 }
120 }
121 return mask;
156 return mask;
122}
157}
123
158
124/**
125 * All valid blob masks: every cardinal combo, with each relevant corner free or filled.
126 * Yields 47 values (classic blob tileset).
127 */
128export function listValidNeighborConnectCornerMasks ()
129{
130 const out = [];
131 for (let cardinal = 0; cardinal < NEIGHBOR_CONNECT_MASK_COUNT; cardinal++)
132 {
133 const cornerBits = [];
134 for (const dir of NEIGHBOR_DIAGONALS)
135 {
136 if ((cardinal & dir.needA) && (cardinal & dir.needB))
137 { cornerBits.push(dir.bit); }
138 }
139 const n = cornerBits.length;
140 for (let subset = 0; subset < (1 << n); subset++)
141 {
142 let mask = cardinal;
143 for (let i = 0; i < n; i++)
144 {
145 if (subset & (1 << i))
146 { mask |= cornerBits[i]; }
147 }
148 out.push(mask);
149 }
150 }
151 return out;
152}
153
154export function definitionUsesNeighborConnect (def)
159export function definitionUsesNeighborConnect (def)
155{
160{
156 return !!def?.neighborConnect;
161 return !!def?.neighborConnect;
157}
162}
158
163
159export function definitionUsesNeighborConnectCorners (def)
164export function definitionUsesNamedNeighborConnect (def)
160{
165{
161 return !!def?.neighborConnect && !!def?.neighborConnectCorners;
166 return !!def?.neighborConnect && !!def?.neighborConnectNamed;
162}
167}
163
168
164export function definitionUsesMaterialNeighborConnect (def)
169export function definitionUsesMaterialNeighborConnect (def)
@@ -174,20 +179,9 @@ function clampCardinalMask (value)
174 return Math.max(0, Math.min(15, n | 0));
179 return Math.max(0, Math.min(15, n | 0));
175}
180}
176
181
177function clampMaskForDef (value, def)
178{
179 const n = Number(value);
180 if (!Number.isFinite(n))
181 { return 0; }
182 const max = definitionUsesNeighborConnectCorners(def)
183 ? NEIGHBOR_CONNECT_CORNER_MASK_MAX
184 : 15;
185 return Math.max(0, Math.min(max, n | 0));
186}
187
188/**
182/**
189 * Tables (and similar) store `${material}-${mask}` e.g. `pine-3`.
183 * Tables (and similar) store `${material}-${mask}` e.g. `pine-3`.
190 * Plain masks (`"7"`) stay valid for counters / walls.
184 * Plain masks (`"7"`) stay valid for counters.
191 */
185 */
192export function parseMaterialNeighborConnectState (state, def = null)
186export function parseMaterialNeighborConnectState (state, def = null)
193{
187{
@@ -214,12 +208,14 @@ export function parseMaterialNeighborConnectState (state, def = null)
214
208
215export function composeNeighborConnectState (def, currentState, mask)
209export function composeNeighborConnectState (def, currentState, mask)
216{
210{
217 const m = clampMaskForDef(mask, def);
211 const m = clampCardinalMask(mask);
218 if (definitionUsesMaterialNeighborConnect(def))
212 if (definitionUsesMaterialNeighborConnect(def))
219 {
213 {
220 const { material } = parseMaterialNeighborConnectState(currentState, def);
214 const { material } = parseMaterialNeighborConnectState(currentState, def);
221 return `${material}-${m}`;
215 return `${material}-${m}`;
222 }
216 }
217 if (definitionUsesNamedNeighborConnect(def))
218 { return composeNamedNeighborConnectState(def, m); }
223 return neighborConnectStateName(m);
219 return neighborConnectStateName(m);
224}
220}
225
221
@@ -240,14 +236,13 @@ export function parseNeighborConnectMaskFromState (state, def = null)
240{
236{
241 if (definitionUsesMaterialNeighborConnect(def))
237 if (definitionUsesMaterialNeighborConnect(def))
242 { return parseMaterialNeighborConnectState(state, def).mask; }
238 { return parseMaterialNeighborConnectState(state, def).mask; }
239 if (definitionUsesNamedNeighborConnect(def))
240 { return parseNamedNeighborConnectMask(state, def); }
243 return parseNeighborConnectMask(state);
241 return parseNeighborConnectMask(state);
244}
242}
245
243
246/**
244/**
247 * Map a world tile to chunk coords + local tile (handles negative world tiles).
245 * Map a world tile to chunk coords + local tile (handles negative world tiles).
248 * @param {number} tileX
249 * @param {number} tileY
250 * @param {number} [chunkSize=16]
251 */
246 */
252export function worldTileToChunkLocal (tileX, tileY, chunkSize = 16)
247export function worldTileToChunkLocal (tileX, tileY, chunkSize = 16)
253{
248{
@@ -262,29 +257,14 @@ export function worldTileToChunkLocal (tileX, tileY, chunkSize = 16)
262 };
257 };
263}
258}
264
259
265/**
260/** Self + cardinal neighbors in world tile space (for connect mask refresh). */
266 * Self + cardinal neighbors (and diagonals when `corners`) in world tile space.
261export function neighborConnectRefreshTiles (tileX, tileY)
267 * @param {number} tileX
268 * @param {number} tileY
269 * @param {{ corners?: boolean }} [options]
270 */
271export function neighborConnectRefreshTiles (tileX, tileY, options = {})
272{
262{
273 const tiles = [
263 return [
274 [tileX, tileY],
264 [tileX, tileY],
275 [tileX, tileY - 1],
265 [tileX, tileY - 1],
276 [tileX + 1, tileY],
266 [tileX + 1, tileY],
277 [tileX, tileY + 1],
267 [tileX, tileY + 1],
278 [tileX - 1, tileY]
268 [tileX - 1, tileY]
279 ];
269 ];
280 if (options.corners)
281 {
282 tiles.push(
283 [tileX + 1, tileY - 1],
284 [tileX + 1, tileY + 1],
285 [tileX - 1, tileY + 1],
286 [tileX - 1, tileY - 1]
287 );
288 }
289 return tiles;
290}
270}