BlueGit

timfa questward

Files

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

generate-counter-sprites.mjs

rweaks
timfa Tim Falken · pushed by timfa · 2 months ago
2b34e2dd History
generate-counter-sprites.mjs 5.3 KiB Download Raw
1/**
2 * Generate 16×16 top-down wooden counter PNGs for neighbor-connect masks 0–15.
3 * Perspective: orthographic top-down (grid-aligned, not diamond isometric).
4 * Connections remove the shared edge rim so adjacent tiles read as one counter.
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/marketplace");
14
15/** Wood palette (RGBA). */
16const RIM = [92, 58, 32, 255];
17const RIM_DARK = [62, 38, 20, 255];
18const TOP = [196, 148, 78, 255];
19const TOP_LIGHT = [214, 170, 98, 255];
20const TOP_SHADE = [168, 120, 58, 255];
21const GRAIN = [180, 132, 64, 255];
22
23function crcTable ()
24{
25 const table = new Uint32Array(256);
26 for (let n = 0; n < 256; n++)
27 {
28 let c = n;
29 for (let k = 0; k < 8; k++)
30 { c = (c & 1) ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1); }
31 table[n] = c >>> 0;
32 }
33 return table;
34}
35
36const CRC_TABLE = crcTable();
37
38function crc32 (buf)
39{
40 let c = 0xffffffff;
41 for (let i = 0; i < buf.length; i++)
42 { c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8); }
43 return (c ^ 0xffffffff) >>> 0;
44}
45
46function chunk (type, data)
47{
48 const len = Buffer.alloc(4);
49 len.writeUInt32BE(data.length);
50 const typeBuf = Buffer.from(type, "ascii");
51 const crcBuf = Buffer.alloc(4);
52 crcBuf.writeUInt32BE(crc32(Buffer.concat([typeBuf, data])));
53 return Buffer.concat([len, typeBuf, data, crcBuf]);
54}
55
56function encodePng (rgba)
57{
58 const raw = Buffer.alloc((SIZE * 4 + 1) * SIZE);
59 for (let y = 0; y < SIZE; y++)
60 {
61 const row = y * (SIZE * 4 + 1);
62 raw[row] = 0;
63 for (let x = 0; x < SIZE; x++)
64 {
65 const i = (y * SIZE + x) * 4;
66 const o = row + 1 + x * 4;
67 raw[o] = rgba[i];
68 raw[o + 1] = rgba[i + 1];
69 raw[o + 2] = rgba[i + 2];
70 raw[o + 3] = rgba[i + 3];
71 }
72 }
73 const ihdr = Buffer.alloc(13);
74 ihdr.writeUInt32BE(SIZE, 0);
75 ihdr.writeUInt32BE(SIZE, 4);
76 ihdr[8] = 8;
77 ihdr[9] = 6;
78 ihdr[10] = 0;
79 ihdr[11] = 0;
80 ihdr[12] = 0;
81 return Buffer.concat([
82 Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]),
83 chunk("IHDR", ihdr),
84 chunk("IDAT", deflateSync(raw, { level: 9 })),
85 chunk("IEND", Buffer.alloc(0))
86 ]);
87}
88
89function setPx (rgba, x, y, color)
90{
91 if (x < 0 || y < 0 || x >= SIZE || y >= SIZE)
92 { return; }
93 const i = (y * SIZE + x) * 4;
94 rgba[i] = color[0];
95 rgba[i + 1] = color[1];
96 rgba[i + 2] = color[2];
97 rgba[i + 3] = color[3];
98}
99
100function fillRect (rgba, x0, y0, x1, y1, color)
101{
102 for (let y = y0; y <= y1; y++)
103 {
104 for (let x = x0; x <= x1; x++)
105 { setPx(rgba, x, y, color); }
106 }
107}
108
109function paintCounter (mask)
110{
111 const rgba = new Uint8Array(SIZE * SIZE * 4);
112 const connectN = (mask & NeighborBit.n) !== 0;
113 const connectE = (mask & NeighborBit.e) !== 0;
114 const connectS = (mask & NeighborBit.s) !== 0;
115 const connectW = (mask & NeighborBit.w) !== 0;
116
117 const topY0 = connectN ? 0 : 2;
118 const topY1 = connectS ? SIZE - 1 : SIZE - 3;
119 const topX0 = connectW ? 0 : 2;
120 const topX1 = connectE ? SIZE - 1 : SIZE - 3;
121
122 // Base fill transparent then rim body where not connected.
123 for (let i = 0; i < rgba.length; i += 4)
124 { rgba[i + 3] = 0; }
125
126 // Outer rim (only on non-connected sides).
127 if (!connectN)
128 { fillRect(rgba, 0, 0, SIZE - 1, 1, RIM_DARK); }
129 if (!connectS)
130 { fillRect(rgba, 0, SIZE - 2, SIZE - 1, SIZE - 1, RIM_DARK); }
131 if (!connectW)
132 { fillRect(rgba, 0, 0, 1, SIZE - 1, RIM_DARK); }
133 if (!connectE)
134 { fillRect(rgba, SIZE - 2, 0, SIZE - 1, SIZE - 1, RIM_DARK); }
135
136 // Inner rim bevel
137 if (!connectN)
138 { fillRect(rgba, topX0, 1, topX1, 1, RIM); }
139 if (!connectS)
140 { fillRect(rgba, topX0, SIZE - 2, topX1, SIZE - 2, RIM); }
141 if (!connectW)
142 { fillRect(rgba, 1, topY0, 1, topY1, RIM); }
143 if (!connectE)
144 { fillRect(rgba, SIZE - 2, topY0, SIZE - 2, topY1, RIM); }
145
146 // Countertop
147 fillRect(rgba, topX0, topY0, topX1, topY1, TOP);
148 // Subtle grain / highlight
149 for (let y = topY0; y <= topY1; y++)
150 {
151 for (let x = topX0; x <= topX1; x++)
152 {
153 if (((x + y * 3) % 7) === 0)
154 { setPx(rgba, x, y, GRAIN); }
155 if (y === topY0 && !connectN)
156 { setPx(rgba, x, y, TOP_LIGHT); }
157 if (y === topY1 && !connectS)
158 { setPx(rgba, x, y, TOP_SHADE); }
159 }
160 }
161
162 // Corner posts when both adjacent sides are free (standalone corners).
163 const corners = [
164 { x: 0, y: 0, n: connectN, w: connectW },
165 { x: SIZE - 1, y: 0, n: connectN, e: connectE },
166 { x: 0, y: SIZE - 1, s: connectS, w: connectW },
167 { x: SIZE - 1, y: SIZE - 1, s: connectS, e: connectE }
168 ];
169 for (const c of corners)
170 {
171 const openN = c.n === false || c.n == null;
172 const openS = c.s === false || c.s == null;
173 const openE = c.e === false || c.e == null;
174 const openW = c.w === false || c.w == null;
175 if ((c.n != null || c.s != null) && (c.e != null || c.w != null))
176 {
177 const vertOpen = (c.n != null ? !c.n : !c.s);
178 const horzOpen = (c.e != null ? !c.e : !c.w);
179 if (vertOpen && horzOpen)
180 { setPx(rgba, c.x, c.y, RIM_DARK); }
181 }
182 void openN;
183 void openS;
184 void openE;
185 void openW;
186 }
187
188 return rgba;
189}
190
191mkdirSync(OUT, { recursive: true });
192for (let mask = 0; mask < 16; mask++)
193{
194 const png = encodePng(paintCounter(mask));
195 writeFileSync(join(OUT, `${mask}.png`), png);
196}
197// Keep legacy fallback name pointing at isolated counter.
198writeFileSync(join(OUT, "marketplace.png"), encodePng(paintCounter(0)));
199console.log(`Wrote marketplace counter sprites 0–15 to ${OUT}`);