BlueGit

timfa questward

Add thin/thick stone walls with diagonal corner autotile.

Neighbor-connect now samples diagonals so L-strips and solid 2×2 fills pick different sprites; thick keeps the existing block id, thin is a new placeable.

timfa Tim Falken · pushed by timfa

Authored 2026-08-31 · 2 months ago

Hash
1af95bf749f071674363477414534bfc7af0039f
Parent
7626e49a
103 files changed +472 −163
client/assets/blocks/stone-wall-thin/0.png added Binary file +0 −0

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

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

The contents of this binary file are not shown.

client/scripts/generate-stone-wall-sprites.mjs +279 −135
@@ -1,39 +1,71 @@
1 1 /**
2 * Generate 16×16 gray cobblestone stone-wall PNGs for neighbor-connect masks 0–15.
2 * Generate thin + thick stone-wall PNGs for blob neighbor-connect masks
3 * (cardinals + diagonal corners — 47 valid states).
3 4 *
4 * Truncated-pyramid / frustum look when alone or at free ends. Connected runs use a
5 * constant cross-section extruded to the tile edge (strip-union like tables/fences)
6 * so N/S and E/W seams match.
5 * Shape references (silhouette / shading only, not final texture):
6 * client/assets/blocks/stone-wall/example-stone-thin.png
7 * client/assets/blocks/stone-wall/example-stone-thick.png
8 *
9 * Corner bits: when both adjacent cardinals join but the diagonal does not,
10 * paint an L-strip (concave). When the diagonal also joins, fill the quadrant
11 * so a solid 2×2 reads as one block.
7 12 *
8 13 * node client/scripts/generate-stone-wall-sprites.mjs
9 14 */
10 import { mkdirSync, writeFileSync } from "node:fs";
15 import { mkdirSync, writeFileSync, readdirSync, unlinkSync } from "node:fs";
11 16 import { deflateSync } from "node:zlib";
12 17 import { fileURLToPath } from "node:url";
13 18 import { dirname, join } from "node:path";
14 import { NeighborBit } from "../../shared/world/neighborConnect.js";
19 import {
20 NeighborBit,
21 listValidNeighborConnectCornerMasks
22 } from "../../shared/world/neighborConnect.js";
15 23
16 24 const SIZE = 16;
17 const OUT = join(dirname(fileURLToPath(import.meta.url)), "../assets/blocks/stone-wall");
18
19 const TOP = [168, 168, 172, 255];
20 const TOP_HI = [198, 198, 202, 255];
21 const TOP_LO = [140, 140, 144, 255];
22 const FACE = [112, 112, 116, 255];
23 const FACE_HI = [132, 132, 136, 255];
24 const FACE_LO = [88, 88, 92, 255];
25 const EDGE = [70, 70, 74, 255];
26 const MORTAR = [78, 78, 82, 255];
27
28 /** Constant core used for connected runs (must meet neighbour tile edges cleanly). */
29 const CORE_X0 = 5;
30 const CORE_X1 = 10;
31 const TOP_Y0 = 3;
32 const TOP_Y1 = 6;
33 const FACE_Y1 = 12;
34 /** Alone / free-end base is wider than the top (frustum). */
35 const BASE_X0 = 3;
36 const BASE_X1 = 12;
25 const ROOT = join(dirname(fileURLToPath(import.meta.url)), "../assets/blocks");
26 const OUT_THICK = join(ROOT, "stone-wall");
27 const OUT_THIN = join(ROOT, "stone-wall-thin");
28
29 const OUTLINE = [0x40, 0x40, 0x40, 255];
30 const TOP_HI = [0xaf, 0xaf, 0xaf, 255];
31 const TOP = [0x80, 0x80, 0x80, 255];
32 const TOP_LO = [0x73, 0x73, 0x73, 255];
33 const FACE_L = [0x95, 0x95, 0x95, 255];
34 const FACE = [0x73, 0x73, 0x73, 255];
35 const FACE_R = [0x6c, 0x6c, 0x6c, 255];
36 const FACE_FRONT = [0x54, 0x54, 0x54, 255];
37 const HI_L = [0xb6, 0xb6, 0xb6, 255];
38 const HI_R = [0x80, 0x80, 0x80, 255];
39 const MORTAR = [0x58, 0x58, 0x5c, 255];
40
41 /**
42 * Thin: compact top core (table-style strip-union) so L keeps a concave cutout;
43 * frustum face hangs below on free south edges. Alone matches example-stone-thin.
44 */
45 const THIN = Object.freeze({
46 topY0: 2,
47 coreY0: 3,
48 coreY1: 7,
49 faceY1: 12,
50 coreX0: 5,
51 coreX1: 10,
52 baseX0: 3,
53 baseX1: 12,
54 fullTile: false
55 });
56
57 /** Thick: full-tile footprint; bevels react to free edges / missing diagonals. */
58 const THICK = Object.freeze({
59 topY0: 1,
60 coreY0: 1,
61 coreY1: 8,
62 faceY1: 14,
63 coreX0: 3,
64 coreX1: 12,
65 baseX0: 0,
66 baseX1: 15,
67 fullTile: true
68 });
37 69
38 70 function crcTable ()
39 71 {
@@ -109,148 +141,260 @@ function setPx (rgba, x, y, color)
109 141 rgba[i + 3] = color[3];
110 142 }
111 143
112 /** Frustum half-width in X at row y (alone / free-end silhouette). */
113 function frustumX0X1 (y)
144 function hash2 (x, y)
114 145 {
115 if (y <= TOP_Y1)
116 { return { x0: CORE_X0, x1: CORE_X1 }; }
117 if (y >= FACE_Y1)
118 { return { x0: BASE_X0, x1: BASE_X1 }; }
119 const t = (y - TOP_Y1) / (FACE_Y1 - TOP_Y1);
120 const x0 = Math.round(CORE_X0 + (BASE_X0 - CORE_X0) * t);
121 const x1 = Math.round(CORE_X1 + (BASE_X1 - CORE_X1) * t);
122 return { x0, x1 };
146 let n = (x * 374761393 + y * 668265263) | 0;
147 n = (n ^ (n >>> 13)) * 1274126177;
148 return (n ^ (n >>> 16)) >>> 0;
123 149 }
124 150
125 /**
126 * Strip-union with constant core thickness on connected sides.
127 * Free ends keep the frustum taper so alone / stubs still read as 3D.
128 */
129 function inWall (x, y, connectN, connectE, connectS, connectW)
151 function cobbleShade (base, x, y)
130 152 {
131 const alone = !connectN && !connectE && !connectS && !connectW;
132 const wantH = alone || connectE || connectW;
133 const wantV = alone || connectN || connectS;
134
135 if (wantH)
153 const h = hash2(x, y);
154 const speck = h & 7;
155 if (speck === 0)
156 { return MORTAR; }
157 if (speck === 1)
136 158 {
137 const y0 = connectN ? 0 : TOP_Y0;
138 const y1 = connectS ? SIZE - 1 : FACE_Y1;
139 if (y >= y0 && y <= y1)
140 {
141 const { x0: fx0, x1: fx1 } = frustumX0X1(Math.min(FACE_Y1, Math.max(TOP_Y0, y)));
142 // Connected cardinals run to the tile edge (constant seam).
143 // Free ends keep the frustum taper.
144 const x0 = connectW ? 0 : fx0;
145 const x1 = connectE ? SIZE - 1 : fx1;
146 if (x >= x0 && x <= x1)
147 { return true; }
148 }
159 return [
160 Math.min(255, base[0] + 18),
161 Math.min(255, base[1] + 18),
162 Math.min(255, base[2] + 18),
163 255
164 ];
149 165 }
166 if (speck === 2)
167 {
168 return [
169 Math.max(0, base[0] - 14),
170 Math.max(0, base[1] - 14),
171 Math.max(0, base[2] - 14),
172 255
173 ];
174 }
175 return base;
176 }
177
178 function decodeMask (mask)
179 {
180 return {
181 n: (mask & NeighborBit.n) !== 0,
182 e: (mask & NeighborBit.e) !== 0,
183 s: (mask & NeighborBit.s) !== 0,
184 w: (mask & NeighborBit.w) !== 0,
185 ne: (mask & NeighborBit.ne) !== 0,
186 se: (mask & NeighborBit.se) !== 0,
187 sw: (mask & NeighborBit.sw) !== 0,
188 nw: (mask & NeighborBit.nw) !== 0
189 };
190 }
191
192 function frustumX0X1 (y, profile)
193 {
194 if (y <= 2)
195 { return { x0: 6, x1: 9 }; }
196 if (y <= 4)
197 { return { x0: 5, x1: 10 }; }
198 if (y <= 6)
199 { return { x0: 4, x1: 11 }; }
200 return { x0: profile.baseX0, x1: profile.baseX1 };
201 }
150 202
151 if (wantV)
203 /** Top-plane strip-union + diagonal fills (no front skirt). */
204 function inTopPlane (x, y, c, profile)
205 {
206 const { coreX0, coreX1, coreY0, coreY1 } = profile;
207 const inCoreX = x >= coreX0 && x <= coreX1;
208 const inCoreY = y >= coreY0 && y <= coreY1;
209 const wantH = c.e || c.w;
210 const wantV = c.n || c.s;
211
212 if (inCoreX && inCoreY)
213 { return true; }
214
215 if (wantH && inCoreY)
152 216 {
153 const x0 = CORE_X0;
154 const x1 = CORE_X1;
217 const x0 = c.w ? 0 : coreX0;
218 const x1 = c.e ? SIZE - 1 : coreX1;
155 219 if (x >= x0 && x <= x1)
156 {
157 const y0 = connectN ? 0 : TOP_Y0;
158 const y1 = connectS ? SIZE - 1 : FACE_Y1;
159 if (y >= y0 && y <= y1)
160 { return true; }
161 }
220 { return true; }
162 221 }
163 222
223 if (wantV && inCoreX)
224 {
225 const y0 = c.n ? 0 : coreY0;
226 const y1 = c.s ? SIZE - 1 : coreY1;
227 if (y >= y0 && y <= y1)
228 { return true; }
229 }
230
231 // Close concave L/T cutouts when the diagonal neighbour is present.
232 if (c.se && c.s && c.e && x > coreX1 && y > coreY1)
233 { return true; }
234 if (c.sw && c.s && c.w && x < coreX0 && y > coreY1)
235 { return true; }
236 if (c.ne && c.n && c.e && x > coreX1 && y < coreY0)
237 { return true; }
238 if (c.nw && c.n && c.w && x < coreX0 && y < coreY0)
239 { return true; }
240
164 241 return false;
165 242 }
166 243
167 function hash2 (x, y)
244 function inWall (x, y, c, profile)
168 245 {
169 let n = (x * 374761393 + y * 668265263) | 0;
170 n = (n ^ (n >>> 13)) * 1274126177;
171 return (n ^ (n >>> 16)) >>> 0;
172 }
246 if (profile.fullTile)
247 { return true; }
173 248
174 function cobbleShade (base, x, y)
175 {
176 const h = hash2(x, y);
177 const speck = (h & 7);
178 if (speck === 0)
179 { return MORTAR; }
180 if (speck === 1 || speck === 2)
181 { return base === TOP || base === TOP_HI || base === TOP_LO ? TOP_HI : FACE_HI; }
182 if (speck === 3)
183 { return base === TOP || base === TOP_HI || base === TOP_LO ? TOP_LO : FACE_LO; }
184 return base;
249 const alone = !c.n && !c.e && !c.s && !c.w;
250 if (alone)
251 {
252 if (y < profile.topY0 || y > profile.faceY1)
253 { return false; }
254 const { x0, x1 } = frustumX0X1(y, profile);
255 return x >= x0 && x <= x1;
256 }
257
258 if (inTopPlane(x, y, c, profile))
259 { return true; }
260
261 // Front-face skirt under top-plane pixels that do not continue south.
262 if (y > profile.coreY1 && y <= profile.faceY1)
263 {
264 if (!inTopPlane(x, profile.coreY1, c, profile))
265 { return false; }
266 // Vertical run into a southern neighbour: no skirt in the core column.
267 if (c.s && x >= profile.coreX0 && x <= profile.coreX1)
268 { return false; }
269 // Diagonal-filled SE mass continues as solid top — skirt only at very bottom
270 // of columns that are the south perimeter (no further south neighbour top).
271 if (c.s && ((c.se && c.e && x > profile.coreX1) || (c.sw && c.w && x < profile.coreX0)))
272 { return false; }
273 const { x0, x1 } = frustumX0X1(y, profile);
274 const skirtX0 = c.w ? 0 : x0;
275 const skirtX1 = c.e ? SIZE - 1 : x1;
276 if (x < skirtX0 || x > skirtX1)
277 { return false; }
278 return inTopPlane(x, profile.coreY1, c, profile);
279 }
280
281 return false;
185 282 }
186 283
187 function isTopRow (y, connectN)
284 function shadeAt (x, y, c, profile, occ)
188 285 {
189 if (connectN)
190 { return y <= TOP_Y1; }
191 return y >= TOP_Y0 && y <= TOP_Y1;
286 const n = occ(x, y - 1);
287 const e = occ(x + 1, y);
288 const s = occ(x, y + 1);
289 const w = occ(x - 1, y);
290
291 const openCount = (!n ? 1 : 0) + (!s ? 1 : 0) + (!e ? 1 : 0) + (!w ? 1 : 0);
292 if (openCount > 0)
293 {
294 const edgeHeavy = (!n && y <= (profile.fullTile ? 0 : profile.topY0))
295 || (!s && y >= (profile.fullTile ? SIZE - 1 : profile.faceY1))
296 || (!w && x <= (profile.fullTile ? 0 : profile.baseX0))
297 || (!e && x >= (profile.fullTile ? SIZE - 1 : profile.baseX1));
298 if (edgeHeavy || openCount >= 2)
299 { return OUTLINE; }
300 }
301
302 if (profile.fullTile)
303 {
304 const freeN = !c.n;
305 const freeS = !c.s;
306 const freeW = !c.w;
307 const freeE = !c.e;
308 const openNe = c.n && c.e && !c.ne;
309 const openSe = c.s && c.e && !c.se;
310 const openSw = c.s && c.w && !c.sw;
311 const openNw = c.n && c.w && !c.nw;
312
313 if (y === 0 || x === 0 || x === SIZE - 1 || y === SIZE - 1)
314 { return OUTLINE; }
315 if ((freeW && x <= 2) || (openNw && x <= 2 && y <= 2) || (openSw && x <= 2 && y >= SIZE - 3))
316 { return FACE_L; }
317 if ((freeE && x >= SIZE - 3) || (openNe && x >= SIZE - 3 && y <= 2) || (openSe && x >= SIZE - 3 && y >= SIZE - 3))
318 { return FACE_R; }
319 if (freeN && y === 1)
320 { return TOP_HI; }
321 if (y <= 8 && x >= 3 && x <= 12)
322 { return TOP; }
323 if (freeS || y >= 10)
324 { return FACE_FRONT; }
325 return TOP_LO;
326 }
327
328 if (y <= profile.coreY1)
329 {
330 if (!n)
331 { return OUTLINE; }
332 if (!w)
333 { return HI_L; }
334 if (!e)
335 { return HI_R; }
336 return TOP;
337 }
338
339 if (!s)
340 { return OUTLINE; }
341 if (!w)
342 { return HI_L; }
343 if (!e)
344 { return HI_R; }
345 return FACE;
192 346 }
193 347
194 function paintWall (mask)
348 function paintWall (mask, profile)
195 349 {
196 350 const rgba = new Uint8Array(SIZE * SIZE * 4);
197 const connectN = (mask & NeighborBit.n) !== 0;
198 const connectE = (mask & NeighborBit.e) !== 0;
199 const connectS = (mask & NeighborBit.s) !== 0;
200 const connectW = (mask & NeighborBit.w) !== 0;
351 const c = decodeMask(mask);
352 const occ = (x, y) =>
353 {
354 if (x < 0)
355 { return c.w; }
356 if (x >= SIZE)
357 { return c.e; }
358 if (y < 0)
359 { return c.n; }
360 if (y >= SIZE)
361 { return c.s; }
362 return inWall(x, y, c, profile);
363 };
201 364
202 365 for (let y = 0; y < SIZE; y++)
203 366 {
204 367 for (let x = 0; x < SIZE; x++)
205 368 {
206 if (!inWall(x, y, connectN, connectE, connectS, connectW))
369 if (!occ(x, y))
207 370 { continue; }
208
209 const n = inWall(x, y - 1, connectN, connectE, connectS, connectW);
210 const e = inWall(x + 1, y, connectN, connectE, connectS, connectW);
211 const s = inWall(x, y + 1, connectN, connectE, connectS, connectW);
212 const w = inWall(x - 1, y, connectN, connectE, connectS, connectW);
213
214 let base;
215 if (isTopRow(y, connectN))
216 {
217 if (!n)
218 { base = TOP_HI; }
219 else if (!s && y === TOP_Y1)
220 { base = TOP_LO; }
221 else
222 { base = TOP; }
223 }
224 else
225 {
226 if (!s)
227 { base = FACE_LO; }
228 else if (!n)
229 { base = FACE_HI; }
230 else
231 { base = FACE; }
232 }
233
234 // Free side bevels (not on edges that continue into a neighbour).
235 const freeW = !w && !(connectW && x === 0);
236 const freeE = !e && !(connectE && x === SIZE - 1);
237 if (freeW || freeE)
238 {
239 if (base === TOP || base === TOP_HI)
240 { base = TOP_LO; }
241 else if (base === FACE || base === FACE_HI)
242 { base = EDGE; }
243 }
244
245 setPx(rgba, x, y, cobbleShade(base, x, y));
371 setPx(rgba, x, y, cobbleShade(shadeAt(x, y, c, profile, occ), x, y));
246 372 }
247 373 }
248 374 return rgba;
249 375 }
250 376
251 mkdirSync(OUT, { recursive: true });
252 for (let mask = 0; mask < 16; mask++)
377 function clearGenerated (dir)
378 {
379 mkdirSync(dir, { recursive: true });
380 for (const name of readdirSync(dir))
381 {
382 if (!/^\d+\.png$/i.test(name))
383 { continue; }
384 unlinkSync(join(dir, name));
385 }
386 }
387
388 function writeSet (dir, profile, label)
253 389 {
254 writeFileSync(join(OUT, `${mask}.png`), encodePng(paintWall(mask)));
390 clearGenerated(dir);
391 const masks = listValidNeighborConnectCornerMasks();
392 for (const mask of masks)
393 {
394 writeFileSync(join(dir, `${mask}.png`), encodePng(paintWall(mask, profile)));
395 }
396 console.log(`Wrote ${masks.length} ${label} stone-wall sprites to ${dir}`);
255 397 }
256 console.log(`Wrote cobblestone frustum stone-wall sprites 0–15 to ${OUT}`);
398
399 writeSet(OUT_THICK, THICK, "thick");
400 writeSet(OUT_THIN, THIN, "thin");
@@ -1,39 +1,71 @@
1/**
1/**
2 * Generate 16×16 gray cobblestone stone-wall PNGs for neighbor-connect masks 0–15.
2 * Generate thin + thick stone-wall PNGs for blob neighbor-connect masks
3 * (cardinals + diagonal corners — 47 valid states).
3 *
4 *
4 * Truncated-pyramid / frustum look when alone or at free ends. Connected runs use a
5 * Shape references (silhouette / shading only, not final texture):
5 * constant cross-section extruded to the tile edge (strip-union like tables/fences)
6 * client/assets/blocks/stone-wall/example-stone-thin.png
6 * so N/S and E/W seams match.
7 * client/assets/blocks/stone-wall/example-stone-thick.png
8 *
9 * Corner bits: when both adjacent cardinals join but the diagonal does not,
10 * paint an L-strip (concave). When the diagonal also joins, fill the quadrant
11 * so a solid 2×2 reads as one block.
7 *
12 *
8 * node client/scripts/generate-stone-wall-sprites.mjs
13 * node client/scripts/generate-stone-wall-sprites.mjs
9 */
14 */
10import { mkdirSync, writeFileSync } from "node:fs";
15import { mkdirSync, writeFileSync, readdirSync, unlinkSync } from "node:fs";
11import { deflateSync } from "node:zlib";
16import { deflateSync } from "node:zlib";
12import { fileURLToPath } from "node:url";
17import { fileURLToPath } from "node:url";
13import { dirname, join } from "node:path";
18import { dirname, join } from "node:path";
14import { NeighborBit } from "../../shared/world/neighborConnect.js";
19import {
20 NeighborBit,
21 listValidNeighborConnectCornerMasks
22} from "../../shared/world/neighborConnect.js";
15
23
16const SIZE = 16;
24const SIZE = 16;
17const OUT = join(dirname(fileURLToPath(import.meta.url)), "../assets/blocks/stone-wall");
25const ROOT = join(dirname(fileURLToPath(import.meta.url)), "../assets/blocks");
18
26const OUT_THICK = join(ROOT, "stone-wall");
19const TOP = [168, 168, 172, 255];
27const OUT_THIN = join(ROOT, "stone-wall-thin");
20const TOP_HI = [198, 198, 202, 255];
28
21const TOP_LO = [140, 140, 144, 255];
29const OUTLINE = [0x40, 0x40, 0x40, 255];
22const FACE = [112, 112, 116, 255];
30const TOP_HI = [0xaf, 0xaf, 0xaf, 255];
23const FACE_HI = [132, 132, 136, 255];
31const TOP = [0x80, 0x80, 0x80, 255];
24const FACE_LO = [88, 88, 92, 255];
32const TOP_LO = [0x73, 0x73, 0x73, 255];
25const EDGE = [70, 70, 74, 255];
33const FACE_L = [0x95, 0x95, 0x95, 255];
26const MORTAR = [78, 78, 82, 255];
34const FACE = [0x73, 0x73, 0x73, 255];
27
35const FACE_R = [0x6c, 0x6c, 0x6c, 255];
28/** Constant core used for connected runs (must meet neighbour tile edges cleanly). */
36const FACE_FRONT = [0x54, 0x54, 0x54, 255];
29const CORE_X0 = 5;
37const HI_L = [0xb6, 0xb6, 0xb6, 255];
30const CORE_X1 = 10;
38const HI_R = [0x80, 0x80, 0x80, 255];
31const TOP_Y0 = 3;
39const MORTAR = [0x58, 0x58, 0x5c, 255];
32const TOP_Y1 = 6;
40
33const FACE_Y1 = 12;
41/**
34/** Alone / free-end base is wider than the top (frustum). */
42 * Thin: compact top core (table-style strip-union) so L keeps a concave cutout;
35const BASE_X0 = 3;
43 * frustum face hangs below on free south edges. Alone matches example-stone-thin.
36const BASE_X1 = 12;
44 */
45const THIN = Object.freeze({
46 topY0: 2,
47 coreY0: 3,
48 coreY1: 7,
49 faceY1: 12,
50 coreX0: 5,
51 coreX1: 10,
52 baseX0: 3,
53 baseX1: 12,
54 fullTile: false
55});
56
57/** Thick: full-tile footprint; bevels react to free edges / missing diagonals. */
58const THICK = Object.freeze({
59 topY0: 1,
60 coreY0: 1,
61 coreY1: 8,
62 faceY1: 14,
63 coreX0: 3,
64 coreX1: 12,
65 baseX0: 0,
66 baseX1: 15,
67 fullTile: true
68});
37
69
38function crcTable ()
70function crcTable ()
39{
71{
@@ -109,148 +141,260 @@ function setPx (rgba, x, y, color)
109 rgba[i + 3] = color[3];
141 rgba[i + 3] = color[3];
110}
142}
111
143
112/** Frustum half-width in X at row y (alone / free-end silhouette). */
144function hash2 (x, y)
113function frustumX0X1 (y)
114{
145{
115 if (y <= TOP_Y1)
146 let n = (x * 374761393 + y * 668265263) | 0;
116 { return { x0: CORE_X0, x1: CORE_X1 }; }
147 n = (n ^ (n >>> 13)) * 1274126177;
117 if (y >= FACE_Y1)
148 return (n ^ (n >>> 16)) >>> 0;
118 { return { x0: BASE_X0, x1: BASE_X1 }; }
119 const t = (y - TOP_Y1) / (FACE_Y1 - TOP_Y1);
120 const x0 = Math.round(CORE_X0 + (BASE_X0 - CORE_X0) * t);
121 const x1 = Math.round(CORE_X1 + (BASE_X1 - CORE_X1) * t);
122 return { x0, x1 };
123}
149}
124
150
125/**
151function cobbleShade (base, x, y)
126 * Strip-union with constant core thickness on connected sides.
127 * Free ends keep the frustum taper so alone / stubs still read as 3D.
128 */
129function inWall (x, y, connectN, connectE, connectS, connectW)
130{
152{
131 const alone = !connectN && !connectE && !connectS && !connectW;
153 const h = hash2(x, y);
132 const wantH = alone || connectE || connectW;
154 const speck = h & 7;
133 const wantV = alone || connectN || connectS;
155 if (speck === 0)
134
156 { return MORTAR; }
135 if (wantH)
157 if (speck === 1)
136 {
158 {
137 const y0 = connectN ? 0 : TOP_Y0;
159 return [
138 const y1 = connectS ? SIZE - 1 : FACE_Y1;
160 Math.min(255, base[0] + 18),
139 if (y >= y0 && y <= y1)
161 Math.min(255, base[1] + 18),
140 {
162 Math.min(255, base[2] + 18),
141 const { x0: fx0, x1: fx1 } = frustumX0X1(Math.min(FACE_Y1, Math.max(TOP_Y0, y)));
163 255
142 // Connected cardinals run to the tile edge (constant seam).
164 ];
143 // Free ends keep the frustum taper.
144 const x0 = connectW ? 0 : fx0;
145 const x1 = connectE ? SIZE - 1 : fx1;
146 if (x >= x0 && x <= x1)
147 { return true; }
148 }
149 }
165 }
166 if (speck === 2)
167 {
168 return [
169 Math.max(0, base[0] - 14),
170 Math.max(0, base[1] - 14),
171 Math.max(0, base[2] - 14),
172 255
173 ];
174 }
175 return base;
176}
177
178function decodeMask (mask)
179{
180 return {
181 n: (mask & NeighborBit.n) !== 0,
182 e: (mask & NeighborBit.e) !== 0,
183 s: (mask & NeighborBit.s) !== 0,
184 w: (mask & NeighborBit.w) !== 0,
185 ne: (mask & NeighborBit.ne) !== 0,
186 se: (mask & NeighborBit.se) !== 0,
187 sw: (mask & NeighborBit.sw) !== 0,
188 nw: (mask & NeighborBit.nw) !== 0
189 };
190}
191
192function frustumX0X1 (y, profile)
193{
194 if (y <= 2)
195 { return { x0: 6, x1: 9 }; }
196 if (y <= 4)
197 { return { x0: 5, x1: 10 }; }
198 if (y <= 6)
199 { return { x0: 4, x1: 11 }; }
200 return { x0: profile.baseX0, x1: profile.baseX1 };
201}
150
202
151 if (wantV)
203/** Top-plane strip-union + diagonal fills (no front skirt). */
204function inTopPlane (x, y, c, profile)
205{
206 const { coreX0, coreX1, coreY0, coreY1 } = profile;
207 const inCoreX = x >= coreX0 && x <= coreX1;
208 const inCoreY = y >= coreY0 && y <= coreY1;
209 const wantH = c.e || c.w;
210 const wantV = c.n || c.s;
211
212 if (inCoreX && inCoreY)
213 { return true; }
214
215 if (wantH && inCoreY)
152 {
216 {
153 const x0 = CORE_X0;
217 const x0 = c.w ? 0 : coreX0;
154 const x1 = CORE_X1;
218 const x1 = c.e ? SIZE - 1 : coreX1;
155 if (x >= x0 && x <= x1)
219 if (x >= x0 && x <= x1)
156 {
220 { return true; }
157 const y0 = connectN ? 0 : TOP_Y0;
158 const y1 = connectS ? SIZE - 1 : FACE_Y1;
159 if (y >= y0 && y <= y1)
160 { return true; }
161 }
162 }
221 }
163
222
223 if (wantV && inCoreX)
224 {
225 const y0 = c.n ? 0 : coreY0;
226 const y1 = c.s ? SIZE - 1 : coreY1;
227 if (y >= y0 && y <= y1)
228 { return true; }
229 }
230
231 // Close concave L/T cutouts when the diagonal neighbour is present.
232 if (c.se && c.s && c.e && x > coreX1 && y > coreY1)
233 { return true; }
234 if (c.sw && c.s && c.w && x < coreX0 && y > coreY1)
235 { return true; }
236 if (c.ne && c.n && c.e && x > coreX1 && y < coreY0)
237 { return true; }
238 if (c.nw && c.n && c.w && x < coreX0 && y < coreY0)
239 { return true; }
240
164 return false;
241 return false;
165}
242}
166
243
167function hash2 (x, y)
244function inWall (x, y, c, profile)
168{
245{
169 let n = (x * 374761393 + y * 668265263) | 0;
246 if (profile.fullTile)
170 n = (n ^ (n >>> 13)) * 1274126177;
247 { return true; }
171 return (n ^ (n >>> 16)) >>> 0;
172}
173
248
174function cobbleShade (base, x, y)
249 const alone = !c.n && !c.e && !c.s && !c.w;
175{
250 if (alone)
176 const h = hash2(x, y);
251 {
177 const speck = (h & 7);
252 if (y < profile.topY0 || y > profile.faceY1)
178 if (speck === 0)
253 { return false; }
179 { return MORTAR; }
254 const { x0, x1 } = frustumX0X1(y, profile);
180 if (speck === 1 || speck === 2)
255 return x >= x0 && x <= x1;
181 { return base === TOP || base === TOP_HI || base === TOP_LO ? TOP_HI : FACE_HI; }
256 }
182 if (speck === 3)
257
183 { return base === TOP || base === TOP_HI || base === TOP_LO ? TOP_LO : FACE_LO; }
258 if (inTopPlane(x, y, c, profile))
184 return base;
259 { return true; }
260
261 // Front-face skirt under top-plane pixels that do not continue south.
262 if (y > profile.coreY1 && y <= profile.faceY1)
263 {
264 if (!inTopPlane(x, profile.coreY1, c, profile))
265 { return false; }
266 // Vertical run into a southern neighbour: no skirt in the core column.
267 if (c.s && x >= profile.coreX0 && x <= profile.coreX1)
268 { return false; }
269 // Diagonal-filled SE mass continues as solid top — skirt only at very bottom
270 // of columns that are the south perimeter (no further south neighbour top).
271 if (c.s && ((c.se && c.e && x > profile.coreX1) || (c.sw && c.w && x < profile.coreX0)))
272 { return false; }
273 const { x0, x1 } = frustumX0X1(y, profile);
274 const skirtX0 = c.w ? 0 : x0;
275 const skirtX1 = c.e ? SIZE - 1 : x1;
276 if (x < skirtX0 || x > skirtX1)
277 { return false; }
278 return inTopPlane(x, profile.coreY1, c, profile);
279 }
280
281 return false;
185}
282}
186
283
187function isTopRow (y, connectN)
284function shadeAt (x, y, c, profile, occ)
188{
285{
189 if (connectN)
286 const n = occ(x, y - 1);
190 { return y <= TOP_Y1; }
287 const e = occ(x + 1, y);
191 return y >= TOP_Y0 && y <= TOP_Y1;
288 const s = occ(x, y + 1);
289 const w = occ(x - 1, y);
290
291 const openCount = (!n ? 1 : 0) + (!s ? 1 : 0) + (!e ? 1 : 0) + (!w ? 1 : 0);
292 if (openCount > 0)
293 {
294 const edgeHeavy = (!n && y <= (profile.fullTile ? 0 : profile.topY0))
295 || (!s && y >= (profile.fullTile ? SIZE - 1 : profile.faceY1))
296 || (!w && x <= (profile.fullTile ? 0 : profile.baseX0))
297 || (!e && x >= (profile.fullTile ? SIZE - 1 : profile.baseX1));
298 if (edgeHeavy || openCount >= 2)
299 { return OUTLINE; }
300 }
301
302 if (profile.fullTile)
303 {
304 const freeN = !c.n;
305 const freeS = !c.s;
306 const freeW = !c.w;
307 const freeE = !c.e;
308 const openNe = c.n && c.e && !c.ne;
309 const openSe = c.s && c.e && !c.se;
310 const openSw = c.s && c.w && !c.sw;
311 const openNw = c.n && c.w && !c.nw;
312
313 if (y === 0 || x === 0 || x === SIZE - 1 || y === SIZE - 1)
314 { return OUTLINE; }
315 if ((freeW && x <= 2) || (openNw && x <= 2 && y <= 2) || (openSw && x <= 2 && y >= SIZE - 3))
316 { return FACE_L; }
317 if ((freeE && x >= SIZE - 3) || (openNe && x >= SIZE - 3 && y <= 2) || (openSe && x >= SIZE - 3 && y >= SIZE - 3))
318 { return FACE_R; }
319 if (freeN && y === 1)
320 { return TOP_HI; }
321 if (y <= 8 && x >= 3 && x <= 12)
322 { return TOP; }
323 if (freeS || y >= 10)
324 { return FACE_FRONT; }
325 return TOP_LO;
326 }
327
328 if (y <= profile.coreY1)
329 {
330 if (!n)
331 { return OUTLINE; }
332 if (!w)
333 { return HI_L; }
334 if (!e)
335 { return HI_R; }
336 return TOP;
337 }
338
339 if (!s)
340 { return OUTLINE; }
341 if (!w)
342 { return HI_L; }
343 if (!e)
344 { return HI_R; }
345 return FACE;
192}
346}
193
347
194function paintWall (mask)
348function paintWall (mask, profile)
195{
349{
196 const rgba = new Uint8Array(SIZE * SIZE * 4);
350 const rgba = new Uint8Array(SIZE * SIZE * 4);
197 const connectN = (mask & NeighborBit.n) !== 0;
351 const c = decodeMask(mask);
198 const connectE = (mask & NeighborBit.e) !== 0;
352 const occ = (x, y) =>
199 const connectS = (mask & NeighborBit.s) !== 0;
353 {
200 const connectW = (mask & NeighborBit.w) !== 0;
354 if (x < 0)
355 { return c.w; }
356 if (x >= SIZE)
357 { return c.e; }
358 if (y < 0)
359 { return c.n; }
360 if (y >= SIZE)
361 { return c.s; }
362 return inWall(x, y, c, profile);
363 };
201
364
202 for (let y = 0; y < SIZE; y++)
365 for (let y = 0; y < SIZE; y++)
203 {
366 {
204 for (let x = 0; x < SIZE; x++)
367 for (let x = 0; x < SIZE; x++)
205 {
368 {
206 if (!inWall(x, y, connectN, connectE, connectS, connectW))
369 if (!occ(x, y))
207 { continue; }
370 { continue; }
208
371 setPx(rgba, x, y, cobbleShade(shadeAt(x, y, c, profile, occ), x, y));
209 const n = inWall(x, y - 1, connectN, connectE, connectS, connectW);
210 const e = inWall(x + 1, y, connectN, connectE, connectS, connectW);
211 const s = inWall(x, y + 1, connectN, connectE, connectS, connectW);
212 const w = inWall(x - 1, y, connectN, connectE, connectS, connectW);
213
214 let base;
215 if (isTopRow(y, connectN))
216 {
217 if (!n)
218 { base = TOP_HI; }
219 else if (!s && y === TOP_Y1)
220 { base = TOP_LO; }
221 else
222 { base = TOP; }
223 }
224 else
225 {
226 if (!s)
227 { base = FACE_LO; }
228 else if (!n)
229 { base = FACE_HI; }
230 else
231 { base = FACE; }
232 }
233
234 // Free side bevels (not on edges that continue into a neighbour).
235 const freeW = !w && !(connectW && x === 0);
236 const freeE = !e && !(connectE && x === SIZE - 1);
237 if (freeW || freeE)
238 {
239 if (base === TOP || base === TOP_HI)
240 { base = TOP_LO; }
241 else if (base === FACE || base === FACE_HI)
242 { base = EDGE; }
243 }
244
245 setPx(rgba, x, y, cobbleShade(base, x, y));
246 }
372 }
247 }
373 }
248 return rgba;
374 return rgba;
249}
375}
250
376
251mkdirSync(OUT, { recursive: true });
377function clearGenerated (dir)
252for (let mask = 0; mask < 16; mask++)
378{
379 mkdirSync(dir, { recursive: true });
380 for (const name of readdirSync(dir))
381 {
382 if (!/^\d+\.png$/i.test(name))
383 { continue; }
384 unlinkSync(join(dir, name));
385 }
386}
387
388function writeSet (dir, profile, label)
253{
389{
254 writeFileSync(join(OUT, `${mask}.png`), encodePng(paintWall(mask)));
390 clearGenerated(dir);
391 const masks = listValidNeighborConnectCornerMasks();
392 for (const mask of masks)
393 {
394 writeFileSync(join(dir, `${mask}.png`), encodePng(paintWall(mask, profile)));
395 }
396 console.log(`Wrote ${masks.length} ${label} stone-wall sprites to ${dir}`);
255}
397}
256console.log(`Wrote cobblestone frustum stone-wall sprites 0–15 to ${OUT}`);
398
399writeSet(OUT_THICK, THICK, "thick");
400writeSet(OUT_THIN, THIN, "thin");
client/src/editor/main.js +4 −2
@@ -26,6 +26,7 @@ import
26 26 computeNeighborConnectMask,
27 27 composeNeighborConnectState,
28 28 definitionUsesNeighborConnect,
29 definitionUsesNeighborConnectCorners,
29 30 neighborConnectBlocksMatch,
30 31 neighborConnectRefreshTiles,
31 32 worldTileToChunkLocal
@@ -667,9 +668,10 @@ function refreshNeighborConnect (localX, localY, typeId)
667 668 const def = BLOCK_DEFINITIONS[typeId];
668 669 if (!draft || !definitionUsesNeighborConnect(def))
669 670 { return; }
671 const corners = definitionUsesNeighborConnectCorners(def);
670 672 const originTileX = chunkX * CHUNK_SIZE + localX;
671 673 const originTileY = chunkY * CHUNK_SIZE + localY;
672 for (const [tileX, tileY] of neighborConnectRefreshTiles(originTileX, originTileY))
674 for (const [tileX, tileY] of neighborConnectRefreshTiles(originTileX, originTileY, { corners }))
673 675 {
674 676 const block = blockAtWorld(tileX, tileY);
675 677 if (!block || block.typeId !== typeId)
@@ -678,7 +680,7 @@ function refreshNeighborConnect (localX, localY, typeId)
678 680 {
679 681 const neighbor = blockAtWorld(tileX + dx, tileY + dy);
680 682 return neighborConnectBlocksMatch(block, neighbor, def);
681 });
683 }, { corners });
682 684 const nextState = composeNeighborConnectState(def, block.state, mask);
683 685 if (block.state === nextState)
684 686 { continue; }
@@ -26,6 +26,7 @@ import
26 computeNeighborConnectMask,
26 computeNeighborConnectMask,
27 composeNeighborConnectState,
27 composeNeighborConnectState,
28 definitionUsesNeighborConnect,
28 definitionUsesNeighborConnect,
29 definitionUsesNeighborConnectCorners,
29 neighborConnectBlocksMatch,
30 neighborConnectBlocksMatch,
30 neighborConnectRefreshTiles,
31 neighborConnectRefreshTiles,
31 worldTileToChunkLocal
32 worldTileToChunkLocal
@@ -667,9 +668,10 @@ function refreshNeighborConnect (localX, localY, typeId)
667 const def = BLOCK_DEFINITIONS[typeId];
668 const def = BLOCK_DEFINITIONS[typeId];
668 if (!draft || !definitionUsesNeighborConnect(def))
669 if (!draft || !definitionUsesNeighborConnect(def))
669 { return; }
670 { return; }
671 const corners = definitionUsesNeighborConnectCorners(def);
670 const originTileX = chunkX * CHUNK_SIZE + localX;
672 const originTileX = chunkX * CHUNK_SIZE + localX;
671 const originTileY = chunkY * CHUNK_SIZE + localY;
673 const originTileY = chunkY * CHUNK_SIZE + localY;
672 for (const [tileX, tileY] of neighborConnectRefreshTiles(originTileX, originTileY))
674 for (const [tileX, tileY] of neighborConnectRefreshTiles(originTileX, originTileY, { corners }))
673 {
675 {
674 const block = blockAtWorld(tileX, tileY);
676 const block = blockAtWorld(tileX, tileY);
675 if (!block || block.typeId !== typeId)
677 if (!block || block.typeId !== typeId)
@@ -678,7 +680,7 @@ function refreshNeighborConnect (localX, localY, typeId)
678 {
680 {
679 const neighbor = blockAtWorld(tileX + dx, tileY + dy);
681 const neighbor = blockAtWorld(tileX + dx, tileY + dy);
680 return neighborConnectBlocksMatch(block, neighbor, def);
682 return neighborConnectBlocksMatch(block, neighbor, def);
681 });
683 }, { corners });
682 const nextState = composeNeighborConnectState(def, block.state, mask);
684 const nextState = composeNeighborConnectState(def, block.state, mask);
683 if (block.state === nextState)
685 if (block.state === nextState)
684 { continue; }
686 { continue; }
server/src/assets/spriteCatalog.js +1 −0
@@ -504,5 +504,6 @@ function listPngStems (folder)
504 504 return entries
505 505 .filter((entry) => entry.isFile() && extname(entry.name).toLowerCase() === ".png")
506 506 .map((entry) => entry.name.slice(0, -extname(entry.name).length))
507 .filter((stem) => !stem.startsWith("example") && !stem.startsWith("_"))
507 508 .sort((a, b) => a.localeCompare(b, "en", { numeric: true, sensitivity: "base" }));
508 509 }
@@ -504,5 +504,6 @@ function listPngStems (folder)
504 return entries
504 return entries
505 .filter((entry) => entry.isFile() && extname(entry.name).toLowerCase() === ".png")
505 .filter((entry) => entry.isFile() && extname(entry.name).toLowerCase() === ".png")
506 .map((entry) => entry.name.slice(0, -extname(entry.name).length))
506 .map((entry) => entry.name.slice(0, -extname(entry.name).length))
507 .filter((stem) => !stem.startsWith("example") && !stem.startsWith("_"))
507 .sort((a, b) => a.localeCompare(b, "en", { numeric: true, sensitivity: "base" }));
508 .sort((a, b) => a.localeCompare(b, "en", { numeric: true, sensitivity: "base" }));
508}
509}
server/tests/central-exchange.test.js +24 −1
@@ -9,7 +9,8 @@ import {
9 9 neighborConnectBlocksMatch,
10 10 parseMaterialNeighborConnectState,
11 11 worldTileToChunkLocal,
12 neighborConnectRefreshTiles
12 neighborConnectRefreshTiles,
13 listValidNeighborConnectCornerMasks
13 14 } from "../../shared/world/neighborConnect.js";
14 15 import { CENTRAL_EXCHANGE_STAFF, pickExchangeClerk } from "../../shared/market/exchange.js";
15 16 import { MobId, normalizeSpawnerConfig, isCentralExchangeSpawner } from "../../shared/definitions/mobs.js";
@@ -30,6 +31,28 @@ test("neighbor connect masks cover all NESW combinations", () =>
30 31 assert.equal(mask, NeighborBit.n | NeighborBit.e);
31 32 });
32 33
34 test("corner-aware neighbor connect sets diagonals only with both cardinals", () =>
35 {
36 const isL = (dx, dy) => (dx === 1 && dy === 0) || (dx === 0 && dy === 1);
37 assert.equal(
38 computeNeighborConnectMask(isL, { corners: true }),
39 NeighborBit.e | NeighborBit.s
40 );
41 const isSolid2x2 = (dx, dy) =>
42 (dx === 1 && dy === 0)
43 || (dx === 0 && dy === 1)
44 || (dx === 1 && dy === 1);
45 assert.equal(
46 computeNeighborConnectMask(isSolid2x2, { corners: true }),
47 NeighborBit.e | NeighborBit.s | NeighborBit.se
48 );
49 assert.equal(listValidNeighborConnectCornerMasks().length, 47);
50 assert.deepEqual(
51 neighborConnectRefreshTiles(5, 5, { corners: true }).length,
52 9
53 );
54 });
55
33 56 test("material neighbor-connect states keep wood id and only match same material", () =>
34 57 {
35 58 const def = BLOCK_DEFINITIONS[BlockType.table];
@@ -9,7 +9,8 @@ import {
9 neighborConnectBlocksMatch,
9 neighborConnectBlocksMatch,
10 parseMaterialNeighborConnectState,
10 parseMaterialNeighborConnectState,
11 worldTileToChunkLocal,
11 worldTileToChunkLocal,
12 neighborConnectRefreshTiles
12 neighborConnectRefreshTiles,
13 listValidNeighborConnectCornerMasks
13} from "../../shared/world/neighborConnect.js";
14} from "../../shared/world/neighborConnect.js";
14import { CENTRAL_EXCHANGE_STAFF, pickExchangeClerk } from "../../shared/market/exchange.js";
15import { CENTRAL_EXCHANGE_STAFF, pickExchangeClerk } from "../../shared/market/exchange.js";
15import { MobId, normalizeSpawnerConfig, isCentralExchangeSpawner } from "../../shared/definitions/mobs.js";
16import { MobId, normalizeSpawnerConfig, isCentralExchangeSpawner } from "../../shared/definitions/mobs.js";
@@ -30,6 +31,28 @@ test("neighbor connect masks cover all NESW combinations", () =>
30 assert.equal(mask, NeighborBit.n | NeighborBit.e);
31 assert.equal(mask, NeighborBit.n | NeighborBit.e);
31});
32});
32
33
34test("corner-aware neighbor connect sets diagonals only with both cardinals", () =>
35{
36 const isL = (dx, dy) => (dx === 1 && dy === 0) || (dx === 0 && dy === 1);
37 assert.equal(
38 computeNeighborConnectMask(isL, { corners: true }),
39 NeighborBit.e | NeighborBit.s
40 );
41 const isSolid2x2 = (dx, dy) =>
42 (dx === 1 && dy === 0)
43 || (dx === 0 && dy === 1)
44 || (dx === 1 && dy === 1);
45 assert.equal(
46 computeNeighborConnectMask(isSolid2x2, { corners: true }),
47 NeighborBit.e | NeighborBit.s | NeighborBit.se
48 );
49 assert.equal(listValidNeighborConnectCornerMasks().length, 47);
50 assert.deepEqual(
51 neighborConnectRefreshTiles(5, 5, { corners: true }).length,
52 9
53 );
54});
55
33test("material neighbor-connect states keep wood id and only match same material", () =>
56test("material neighbor-connect states keep wood id and only match same material", () =>
34{
57{
35 const def = BLOCK_DEFINITIONS[BlockType.table];
58 const def = BLOCK_DEFINITIONS[BlockType.table];
server/tests/tile-states.test.js +8 −4
@@ -13,11 +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.deepEqual(catalog.blocks["stone-wall"].states, [
17 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"
18 ]);
16 assert.equal(catalog.blocks["stone-wall"].states.length, 47);
19 17 assert.equal(catalog.blocks["stone-wall"].defaultState, "0");
20 assert.ok(catalog.images.some((image) => image.name === "block:stone-wall:15"));
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"));
21 25 assert.equal(catalog.images[0].subImgTotal, 1);
22 26 assert.equal(catalog.images[0].perRow, 1);
23 27 });
@@ -13,11 +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.deepEqual(catalog.blocks["stone-wall"].states, [
16 assert.equal(catalog.blocks["stone-wall"].states.length, 47);
17 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"
18 ]);
19 assert.equal(catalog.blocks["stone-wall"].defaultState, "0");
17 assert.equal(catalog.blocks["stone-wall"].defaultState, "0");
20 assert.ok(catalog.images.some((image) => image.name === "block:stone-wall:15"));
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"));
21 assert.equal(catalog.images[0].subImgTotal, 1);
25 assert.equal(catalog.images[0].subImgTotal, 1);
22 assert.equal(catalog.images[0].perRow, 1);
26 assert.equal(catalog.images[0].perRow, 1);
23});
27});
shared/definitions/tiles.js +22 −4
@@ -75,7 +75,8 @@ export const BlockType = Object.freeze({
75 75 jeweleringStation: 34,
76 76 flaxPlant: 35,
77 77 silkCocoon: 36,
78 fence: 37
78 fence: 37,
79 stoneWallThin: 38
79 80 });
80 81
81 82 export const GROUND_DEFINITIONS = Object.freeze({
@@ -169,7 +170,7 @@ export const BLOCK_DEFINITIONS = Object.freeze({
169 170 [BlockType.stoneWall]: Object.freeze({
170 171 id: BlockType.stoneWall,
171 172 key: "stone-wall",
172 displayName: "Stone wall",
173 displayName: "Thick stone wall",
173 174 passable: false,
174 175 lightRangeTiles: 0,
175 176 frames: 1,
@@ -177,8 +178,25 @@ export const BLOCK_DEFINITIONS = Object.freeze({
177 178 animationSpeed: 0,
178 179 /** Building walls count as indoor (no seasonal art required). */
179 180 outdoor: false,
180 /** Autotile via N/E/S/W bitmask states `"0"`…`"15"` (frustum cobble sprites). */
181 neighborConnect: true
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 */
185 neighborConnect: true,
186 neighborConnectCorners: true
187 }),
188 [BlockType.stoneWallThin]: Object.freeze({
189 id: BlockType.stoneWallThin,
190 key: "stone-wall-thin",
191 displayName: "Thin stone wall",
192 passable: false,
193 lightRangeTiles: 0,
194 frames: 1,
195 framesPerRow: 1,
196 animationSpeed: 0,
197 outdoor: false,
198 neighborConnect: true,
199 neighborConnectCorners: true
182 200 }),
183 201 [BlockType.torch]: Object.freeze({
184 202 id: BlockType.torch,
@@ -75,7 +75,8 @@ export const BlockType = Object.freeze({
75 jeweleringStation: 34,
75 jeweleringStation: 34,
76 flaxPlant: 35,
76 flaxPlant: 35,
77 silkCocoon: 36,
77 silkCocoon: 36,
78 fence: 37
78 fence: 37,
79 stoneWallThin: 38
79});
80});
80
81
81export const GROUND_DEFINITIONS = Object.freeze({
82export const GROUND_DEFINITIONS = Object.freeze({
@@ -169,7 +170,7 @@ export const BLOCK_DEFINITIONS = Object.freeze({
169 [BlockType.stoneWall]: Object.freeze({
170 [BlockType.stoneWall]: Object.freeze({
170 id: BlockType.stoneWall,
171 id: BlockType.stoneWall,
171 key: "stone-wall",
172 key: "stone-wall",
172 displayName: "Stone wall",
173 displayName: "Thick stone wall",
173 passable: false,
174 passable: false,
174 lightRangeTiles: 0,
175 lightRangeTiles: 0,
175 frames: 1,
176 frames: 1,
@@ -177,8 +178,25 @@ export const BLOCK_DEFINITIONS = Object.freeze({
177 animationSpeed: 0,
178 animationSpeed: 0,
178 /** Building walls count as indoor (no seasonal art required). */
179 /** Building walls count as indoor (no seasonal art required). */
179 outdoor: false,
180 outdoor: false,
180 /** Autotile via N/E/S/W bitmask states `"0"`…`"15"` (frustum cobble sprites). */
181 /**
181 neighborConnect: true
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,
186 neighborConnectCorners: true
187 }),
188 [BlockType.stoneWallThin]: Object.freeze({
189 id: BlockType.stoneWallThin,
190 key: "stone-wall-thin",
191 displayName: "Thin stone wall",
192 passable: false,
193 lightRangeTiles: 0,
194 frames: 1,
195 framesPerRow: 1,
196 animationSpeed: 0,
197 outdoor: false,
198 neighborConnect: true,
199 neighborConnectCorners: true
182 }),
200 }),
183 [BlockType.torch]: Object.freeze({
201 [BlockType.torch]: Object.freeze({
184 id: BlockType.torch,
202 id: BlockType.torch,
shared/world/neighborConnect.js +134 −17
@@ -1,29 +1,50 @@
1 1 /**
2 * Generic neighbor-connect autotile masks (N/E/S/W).
3 * Stored as block/ground `state` string `"0"`…`"15"` for bakeable sprite variants.
4 * Same pattern can later apply to walls and selected grounds.
2 * Generic neighbor-connect autotile masks (N/E/S/W, optional diagonals).
3 * Stored as block/ground `state` string for bakeable sprite variants.
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 8 */
6 9
7 10 export const NeighborBit = Object.freeze({
8 11 n: 1,
9 12 e: 2,
10 13 s: 4,
11 w: 8
14 w: 8,
15 ne: 16,
16 se: 32,
17 sw: 64,
18 nw: 128
12 19 });
13 20
14 21 export const NEIGHBOR_CONNECT_MASK_COUNT = 16;
22 export const NEIGHBOR_CONNECT_CORNER_MASK_MAX = 255;
15 23
16 export function neighborConnectMask ({ n = false, e = false, s = false, w = false } = {})
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 } = {})
17 34 {
18 35 return (n ? NeighborBit.n : 0)
19 36 | (e ? NeighborBit.e : 0)
20 37 | (s ? NeighborBit.s : 0)
21 | (w ? NeighborBit.w : 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);
22 43 }
23 44
24 45 export function neighborConnectStateName (mask)
25 46 {
26 const m = Math.max(0, Math.min(15, mask | 0));
47 const m = Math.max(0, Math.min(NEIGHBOR_CONNECT_CORNER_MASK_MAX, mask | 0));
27 48 return String(m);
28 49 }
29 50
@@ -34,7 +55,7 @@ export function parseNeighborConnectMask (state)
34 55 const n = Number(state);
35 56 if (!Number.isFinite(n))
36 57 { return 0; }
37 return Math.max(0, Math.min(15, n | 0));
58 return Math.max(0, Math.min(NEIGHBOR_CONNECT_CORNER_MASK_MAX, n | 0));
38 59 }
39 60
40 61 export function hasNeighborBit (mask, bit)
@@ -50,11 +71,36 @@ export const NEIGHBOR_CARDINALS = Object.freeze([
50 71 Object.freeze({ bit: NeighborBit.w, dx: -1, dy: 0, key: "w" })
51 72 ]);
52 73
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
53 97 /**
54 98 * @param {(dx: number, dy: number) => boolean} isConnectedNeighbor
55 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).
56 102 */
57 export function computeNeighborConnectMask (isConnectedNeighbor)
103 export function computeNeighborConnectMask (isConnectedNeighbor, options = {})
58 104 {
59 105 let mask = 0;
60 106 for (const dir of NEIGHBOR_CARDINALS)
@@ -62,20 +108,65 @@ export function computeNeighborConnectMask (isConnectedNeighbor)
62 108 if (isConnectedNeighbor(dir.dx, dir.dy))
63 109 { mask |= dir.bit; }
64 110 }
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 }
65 121 return mask;
66 122 }
67 123
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
68 154 export function definitionUsesNeighborConnect (def)
69 155 {
70 156 return !!def?.neighborConnect;
71 157 }
72 158
159 export function definitionUsesNeighborConnectCorners (def)
160 {
161 return !!def?.neighborConnect && !!def?.neighborConnectCorners;
162 }
163
73 164 export function definitionUsesMaterialNeighborConnect (def)
74 165 {
75 166 return !!def?.neighborConnect && !!def?.neighborConnectByMaterial;
76 167 }
77 168
78 function clampMask (value)
169 function clampCardinalMask (value)
79 170 {
80 171 const n = Number(value);
81 172 if (!Number.isFinite(n))
@@ -83,9 +174,20 @@ function clampMask (value)
83 174 return Math.max(0, Math.min(15, n | 0));
84 175 }
85 176
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
86 188 /**
87 189 * Tables (and similar) store `${material}-${mask}` e.g. `pine-3`.
88 * Plain masks (`"7"`) stay valid for counters.
190 * Plain masks (`"7"`) stay valid for counters / walls.
89 191 */
90 192 export function parseMaterialNeighborConnectState (state, def = null)
91 193 {
@@ -96,12 +198,12 @@ export function parseMaterialNeighborConnectState (state, def = null)
96 198 {
97 199 return {
98 200 material: paired[1].toLowerCase(),
99 mask: clampMask(paired[2])
201 mask: clampCardinalMask(paired[2])
100 202 };
101 203 }
102 204 if (/^\d+$/.test(s))
103 205 {
104 return { material: fallback, mask: clampMask(s) };
206 return { material: fallback, mask: clampCardinalMask(s) };
105 207 }
106 208 if (/^[a-z][a-z0-9]*$/i.test(s))
107 209 {
@@ -112,7 +214,7 @@ export function parseMaterialNeighborConnectState (state, def = null)
112 214
113 215 export function composeNeighborConnectState (def, currentState, mask)
114 216 {
115 const m = clampMask(mask);
217 const m = clampMaskForDef(mask, def);
116 218 if (definitionUsesMaterialNeighborConnect(def))
117 219 {
118 220 const { material } = parseMaterialNeighborConnectState(currentState, def);
@@ -160,14 +262,29 @@ export function worldTileToChunkLocal (tileX, tileY, chunkSize = 16)
160 262 };
161 263 }
162 264
163 /** Self + cardinal neighbors in world tile space (for connect mask refresh). */
164 export function neighborConnectRefreshTiles (tileX, tileY)
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 = {})
165 272 {
166 return [
273 const tiles = [
167 274 [tileX, tileY],
168 275 [tileX, tileY - 1],
169 276 [tileX + 1, tileY],
170 277 [tileX, tileY + 1],
171 278 [tileX - 1, tileY]
172 279 ];
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;
173 290 }
@@ -1,29 +1,50 @@
1/**
1/**
2 * Generic neighbor-connect autotile masks (N/E/S/W).
2 * Generic neighbor-connect autotile masks (N/E/S/W, optional diagonals).
3 * Stored as block/ground `state` string `"0"`…`"15"` for bakeable sprite variants.
3 * Stored as block/ground `state` string for bakeable sprite variants.
4 * Same pattern can later apply to walls and selected grounds.
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 */
8 */
6
9
7export const NeighborBit = Object.freeze({
10export const NeighborBit = Object.freeze({
8 n: 1,
11 n: 1,
9 e: 2,
12 e: 2,
10 s: 4,
13 s: 4,
11 w: 8
14 w: 8,
15 ne: 16,
16 se: 32,
17 sw: 64,
18 nw: 128
12});
19});
13
20
14export const NEIGHBOR_CONNECT_MASK_COUNT = 16;
21export const NEIGHBOR_CONNECT_MASK_COUNT = 16;
22export const NEIGHBOR_CONNECT_CORNER_MASK_MAX = 255;
15
23
16export function neighborConnectMask ({ n = false, e = false, s = false, w = false } = {})
24export 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} = {})
17{
34{
18 return (n ? NeighborBit.n : 0)
35 return (n ? NeighborBit.n : 0)
19 | (e ? NeighborBit.e : 0)
36 | (e ? NeighborBit.e : 0)
20 | (s ? NeighborBit.s : 0)
37 | (s ? NeighborBit.s : 0)
21 | (w ? NeighborBit.w : 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);
22}
43}
23
44
24export function neighborConnectStateName (mask)
45export function neighborConnectStateName (mask)
25{
46{
26 const m = Math.max(0, Math.min(15, mask | 0));
47 const m = Math.max(0, Math.min(NEIGHBOR_CONNECT_CORNER_MASK_MAX, mask | 0));
27 return String(m);
48 return String(m);
28}
49}
29
50
@@ -34,7 +55,7 @@ export function parseNeighborConnectMask (state)
34 const n = Number(state);
55 const n = Number(state);
35 if (!Number.isFinite(n))
56 if (!Number.isFinite(n))
36 { return 0; }
57 { return 0; }
37 return Math.max(0, Math.min(15, n | 0));
58 return Math.max(0, Math.min(NEIGHBOR_CONNECT_CORNER_MASK_MAX, n | 0));
38}
59}
39
60
40export function hasNeighborBit (mask, bit)
61export function hasNeighborBit (mask, bit)
@@ -50,11 +71,36 @@ export const NEIGHBOR_CARDINALS = Object.freeze([
50 Object.freeze({ bit: NeighborBit.w, dx: -1, dy: 0, key: "w" })
71 Object.freeze({ bit: NeighborBit.w, dx: -1, dy: 0, key: "w" })
51]);
72]);
52
73
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
53/**
97/**
54 * @param {(dx: number, dy: number) => boolean} isConnectedNeighbor
98 * @param {(dx: number, dy: number) => boolean} isConnectedNeighbor
55 * true when the adjacent tile should join (same connectable type).
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).
56 */
102 */
57export function computeNeighborConnectMask (isConnectedNeighbor)
103export function computeNeighborConnectMask (isConnectedNeighbor, options = {})
58{
104{
59 let mask = 0;
105 let mask = 0;
60 for (const dir of NEIGHBOR_CARDINALS)
106 for (const dir of NEIGHBOR_CARDINALS)
@@ -62,20 +108,65 @@ export function computeNeighborConnectMask (isConnectedNeighbor)
62 if (isConnectedNeighbor(dir.dx, dir.dy))
108 if (isConnectedNeighbor(dir.dx, dir.dy))
63 { mask |= dir.bit; }
109 { mask |= dir.bit; }
64 }
110 }
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 }
65 return mask;
121 return mask;
66}
122}
67
123
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
68export function definitionUsesNeighborConnect (def)
154export function definitionUsesNeighborConnect (def)
69{
155{
70 return !!def?.neighborConnect;
156 return !!def?.neighborConnect;
71}
157}
72
158
159export function definitionUsesNeighborConnectCorners (def)
160{
161 return !!def?.neighborConnect && !!def?.neighborConnectCorners;
162}
163
73export function definitionUsesMaterialNeighborConnect (def)
164export function definitionUsesMaterialNeighborConnect (def)
74{
165{
75 return !!def?.neighborConnect && !!def?.neighborConnectByMaterial;
166 return !!def?.neighborConnect && !!def?.neighborConnectByMaterial;
76}
167}
77
168
78function clampMask (value)
169function clampCardinalMask (value)
79{
170{
80 const n = Number(value);
171 const n = Number(value);
81 if (!Number.isFinite(n))
172 if (!Number.isFinite(n))
@@ -83,9 +174,20 @@ function clampMask (value)
83 return Math.max(0, Math.min(15, n | 0));
174 return Math.max(0, Math.min(15, n | 0));
84}
175}
85
176
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
86/**
188/**
87 * Tables (and similar) store `${material}-${mask}` e.g. `pine-3`.
189 * Tables (and similar) store `${material}-${mask}` e.g. `pine-3`.
88 * Plain masks (`"7"`) stay valid for counters.
190 * Plain masks (`"7"`) stay valid for counters / walls.
89 */
191 */
90export function parseMaterialNeighborConnectState (state, def = null)
192export function parseMaterialNeighborConnectState (state, def = null)
91{
193{
@@ -96,12 +198,12 @@ export function parseMaterialNeighborConnectState (state, def = null)
96 {
198 {
97 return {
199 return {
98 material: paired[1].toLowerCase(),
200 material: paired[1].toLowerCase(),
99 mask: clampMask(paired[2])
201 mask: clampCardinalMask(paired[2])
100 };
202 };
101 }
203 }
102 if (/^\d+$/.test(s))
204 if (/^\d+$/.test(s))
103 {
205 {
104 return { material: fallback, mask: clampMask(s) };
206 return { material: fallback, mask: clampCardinalMask(s) };
105 }
207 }
106 if (/^[a-z][a-z0-9]*$/i.test(s))
208 if (/^[a-z][a-z0-9]*$/i.test(s))
107 {
209 {
@@ -112,7 +214,7 @@ export function parseMaterialNeighborConnectState (state, def = null)
112
214
113export function composeNeighborConnectState (def, currentState, mask)
215export function composeNeighborConnectState (def, currentState, mask)
114{
216{
115 const m = clampMask(mask);
217 const m = clampMaskForDef(mask, def);
116 if (definitionUsesMaterialNeighborConnect(def))
218 if (definitionUsesMaterialNeighborConnect(def))
117 {
219 {
118 const { material } = parseMaterialNeighborConnectState(currentState, def);
220 const { material } = parseMaterialNeighborConnectState(currentState, def);
@@ -160,14 +262,29 @@ export function worldTileToChunkLocal (tileX, tileY, chunkSize = 16)
160 };
262 };
161}
263}
162
264
163/** Self + cardinal neighbors in world tile space (for connect mask refresh). */
265/**
164export function neighborConnectRefreshTiles (tileX, tileY)
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 */
271export function neighborConnectRefreshTiles (tileX, tileY, options = {})
165{
272{
166 return [
273 const tiles = [
167 [tileX, tileY],
274 [tileX, tileY],
168 [tileX, tileY - 1],
275 [tileX, tileY - 1],
169 [tileX + 1, tileY],
276 [tileX + 1, tileY],
170 [tileX, tileY + 1],
277 [tileX, tileY + 1],
171 [tileX - 1, tileY]
278 [tileX - 1, tileY]
172 ];
279 ];
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;
173}
290}