BlueGit

timfa questward

Files

questward / client / scripts / generate-fence-sprites.mjs

generate-fence-sprites.mjs

2f0bc2de History
generate-fence-sprites.mjs 5.0 KiB Download Raw
1/**
2 * Generate 16×16 magenta wooden fence PNGs for neighbor-connect masks 0–15.
3 * Top-down posts + dual rails; rails extend to tile edges where connected so
4 * adjacent same-wood fences read as one continuous run (same as tables).
5 */
6import { mkdirSync, writeFileSync } from "node:fs";
7import { deflateSync } from "node:zlib";
8import { fileURLToPath } from "node:url";
9import { dirname, join } from "node:path";
10import { NeighborBit } from "../../shared/world/neighborConnect.js";
11
12const SIZE = 16;
13const OUT = join(dirname(fileURLToPath(import.meta.url)), "../assets/blocks/fence");
14
15/** Magenta tint family (R≈B, G<R) — catalog tints per wood tier. */
16const POST = [120, 28, 120, 255];
17const POST_LIGHT = [170, 45, 170, 255];
18const POST_SHADE = [90, 18, 90, 255];
19const RAIL = [200, 55, 200, 255];
20const RAIL_LIGHT = [230, 75, 230, 255];
21const RAIL_SHADE = [150, 35, 150, 255];
22
23const POST_X0 = 6;
24const POST_X1 = 9;
25const POST_Y0 = 4;
26const POST_Y1 = 12;
27/** Dual horizontal rail bands (pixel y inclusive). */
28const H_RAILS = Object.freeze([
29 Object.freeze({ y0: 6, y1: 7 }),
30 Object.freeze({ y0: 9, y1: 10 })
31]);
32/** Dual vertical rail bands (pixel x inclusive). */
33const V_RAILS = Object.freeze([
34 Object.freeze({ x0: 6, x1: 7 }),
35 Object.freeze({ x0: 9, x1: 10 })
36]);
37
38function crcTable ()
39{
40 const table = new Uint32Array(256);
41 for (let n = 0; n < 256; n++)
42 {
43 let c = n;
44 for (let k = 0; k < 8; k++)
45 { c = (c & 1) ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1); }
46 table[n] = c >>> 0;
47 }
48 return table;
49}
50
51const CRC_TABLE = crcTable();
52
53function crc32 (buf)
54{
55 let c = 0xffffffff;
56 for (let i = 0; i < buf.length; i++)
57 { c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8); }
58 return (c ^ 0xffffffff) >>> 0;
59}
60
61function chunk (type, data)
62{
63 const len = Buffer.alloc(4);
64 len.writeUInt32BE(data.length);
65 const typeBuf = Buffer.from(type, "ascii");
66 const crcBuf = Buffer.alloc(4);
67 crcBuf.writeUInt32BE(crc32(Buffer.concat([typeBuf, data])));
68 return Buffer.concat([len, typeBuf, data, crcBuf]);
69}
70
71function encodePng (rgba)
72{
73 const raw = Buffer.alloc((SIZE * 4 + 1) * SIZE);
74 for (let y = 0; y < SIZE; y++)
75 {
76 const row = y * (SIZE * 4 + 1);
77 raw[row] = 0;
78 for (let x = 0; x < SIZE; x++)
79 {
80 const i = (y * SIZE + x) * 4;
81 const o = row + 1 + x * 4;
82 raw[o] = rgba[i];
83 raw[o + 1] = rgba[i + 1];
84 raw[o + 2] = rgba[i + 2];
85 raw[o + 3] = rgba[i + 3];
86 }
87 }
88 const ihdr = Buffer.alloc(13);
89 ihdr.writeUInt32BE(SIZE, 0);
90 ihdr.writeUInt32BE(SIZE, 4);
91 ihdr[8] = 8;
92 ihdr[9] = 6;
93 ihdr[10] = 0;
94 ihdr[11] = 0;
95 ihdr[12] = 0;
96 return Buffer.concat([
97 Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]),
98 chunk("IHDR", ihdr),
99 chunk("IDAT", deflateSync(raw, { level: 9 })),
100 chunk("IEND", Buffer.alloc(0))
101 ]);
102}
103
104function setPx (rgba, x, y, color)
105{
106 if (x < 0 || y < 0 || x >= SIZE || y >= SIZE)
107 { return; }
108 const i = (y * SIZE + x) * 4;
109 rgba[i] = color[0];
110 rgba[i + 1] = color[1];
111 rgba[i + 2] = color[2];
112 rgba[i + 3] = color[3];
113}
114
115function fillRect (rgba, x0, y0, x1, y1, color)
116{
117 for (let y = y0; y <= y1; y++)
118 {
119 for (let x = x0; x <= x1; x++)
120 { setPx(rgba, x, y, color); }
121 }
122}
123
124function paintHorizontalRails (rgba, x0, x1)
125{
126 for (const band of H_RAILS)
127 {
128 fillRect(rgba, x0, band.y0, x1, band.y1, RAIL);
129 fillRect(rgba, x0, band.y0, x1, band.y0, RAIL_LIGHT);
130 fillRect(rgba, x0, band.y1, x1, band.y1, RAIL_SHADE);
131 }
132}
133
134function paintVerticalRails (rgba, y0, y1)
135{
136 for (const band of V_RAILS)
137 {
138 fillRect(rgba, band.x0, y0, band.x1, y1, RAIL);
139 fillRect(rgba, band.x0, y0, band.x0, y1, RAIL_LIGHT);
140 fillRect(rgba, band.x1, y0, band.x1, y1, RAIL_SHADE);
141 }
142}
143
144function paintPost (rgba)
145{
146 fillRect(rgba, POST_X0, POST_Y0, POST_X1, POST_Y1, POST);
147 fillRect(rgba, POST_X0, POST_Y0, POST_X1, POST_Y0, POST_LIGHT);
148 fillRect(rgba, POST_X0, POST_Y1, POST_X1, POST_Y1, POST_SHADE);
149 fillRect(rgba, POST_X0, POST_Y0, POST_X0, POST_Y1, POST_SHADE);
150 fillRect(rgba, POST_X1, POST_Y0, POST_X1, POST_Y1, POST_LIGHT);
151 // Cap
152 fillRect(rgba, POST_X0 - 1, POST_Y0 - 1, POST_X1 + 1, POST_Y0 - 1, POST_SHADE);
153 fillRect(rgba, POST_X0, POST_Y0 - 1, POST_X1, POST_Y0 - 1, POST_LIGHT);
154}
155
156function paintFence (mask)
157{
158 const rgba = new Uint8Array(SIZE * SIZE * 4);
159 const connectN = (mask & NeighborBit.n) !== 0;
160 const connectE = (mask & NeighborBit.e) !== 0;
161 const connectS = (mask & NeighborBit.s) !== 0;
162 const connectW = (mask & NeighborBit.w) !== 0;
163 const alone = mask === 0;
164
165 const wantH = alone || connectE || connectW;
166 const wantV = alone || connectN || connectS;
167
168 if (wantH)
169 {
170 const x0 = alone ? POST_X0 - 2 : (connectW ? 0 : POST_X0);
171 const x1 = alone ? POST_X1 + 2 : (connectE ? SIZE - 1 : POST_X1);
172 paintHorizontalRails(rgba, x0, x1);
173 }
174 if (wantV)
175 {
176 const y0 = alone ? POST_Y0 + 1 : (connectN ? 0 : POST_Y0);
177 const y1 = alone ? POST_Y1 - 1 : (connectS ? SIZE - 1 : POST_Y1);
178 paintVerticalRails(rgba, y0, y1);
179 }
180
181 paintPost(rgba);
182 return rgba;
183}
184
185mkdirSync(OUT, { recursive: true });
186for (let mask = 0; mask < 16; mask++)
187{
188 writeFileSync(join(OUT, `${mask}.png`), encodePng(paintFence(mask)));
189}
190console.log(`Wrote magenta fence sprites 0–15 to ${OUT}`);