dsh-game-material-master 0.4.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,760 @@
1
+ /**
2
+ * 地图编辑器(模块六)的**内置示例素材生成器**(纯程序化,零成本、确定性)。
3
+ *
4
+ * ## 为什么要自己产一套
5
+ *
6
+ * AsLib RPG Map Maker 自带的 `Picture/MapChip/*_pipo.png` 是**第三方版权素材,不能打包**
7
+ * 进本插件。所以内置示例素材改成**本机现算**:不需要下载、不需要 API Key、不花一分钱,
8
+ * 而且同一份输入两次生成的 PNG **逐字节一致**(可以直接用哈希比对,见
9
+ * `scripts/make-map-sample-tileset.mjs --check`)。
10
+ *
11
+ * ## 产物长什么样(全部 32px 逻辑格)
12
+ *
13
+ * - **4 张 A2 图**(草地 / 土路 / 水 / 石墙):`64×160` = 4 列 × 10 行**小格**(16×16),
14
+ * 即 2 个块列 × 5 个块行、每块 32×32。块列号 = 变体号(0..1),块行号 = **形状号**(0..4)。
15
+ * 形状语义照 AsLib:0 = 左/上都不连通(孤岛)、1 = 只上连通、2 = 只左连通、
16
+ * 3 = 左上都连通但没有对角、4 = 左/上/左上都连通(满格)。
17
+ * 每个形状块内是 2×2 个小格,**四个小格是镜像的**:形状 0 在小格的「横向外边 + 纵向外边」
18
+ * 各压一条 5px 宽的邻接地貌过渡带(孤岛 → 整块四周一圈带),形状 1 只压横向外边、
19
+ * 形状 2 只压纵向外边、形状 3 只在朝对角那一角画 L 形内角带、形状 4 不压(纯本体)。
20
+ * 过渡带靠内的 2px 用**有序抖动矩阵**做过渡(不用随机数发生器,任何调用顺序下都一样)。
21
+ * - **1 张道具网格图**:`8×5` 个 32px 图块(树 / 花 / 石头 / 木箱 / 栅栏 / 蘑菇 / 断枝 / 石头堆)。
22
+ * - **变体 = 帧**:变体 1 是噪声种子 + 1(水再错开 90° 波纹相位)的同一地貌,
23
+ * 两帧逐像素不同 —— 这是内置素材里唯一的动画来源(水最明显)。
24
+ *
25
+ * ## 纹理从哪来
26
+ *
27
+ * 默认走 `renderLocalTexture(kind, size, seed)`:基色 + 抖动噪声 + 少量像素细节。
28
+ * 留了一条可选覆盖通道:`SAMPLE_TEXTURE_OVERRIDE`(由
29
+ * `scripts/make-map-sample-tileset.mjs --ark` 用 Seedream 生成并降采样成 32×32 后写回),
30
+ * 里面某个地貌有 base64 PNG 就用它当纹理源,没有就程序化。默认那份是**空对象**。
31
+ * 覆盖图走 `bitmap.ts` 的统一解码入口 `decodeFile`(ffmpeg),所以调用方要先
32
+ * `await primeSampleTextures()` 一次 —— `renderSampleTilesets()` 本身保持同步契约;
33
+ * 配了覆盖却没 prime 会**明确抛错**,不会静默退回程序化纹理。
34
+ *
35
+ * ## 确定性怎么保证(这是硬要求)
36
+ *
37
+ * - **不许 `Math.random()` / `Date.now()`**:所有「随机」都来自 `mulberry32(seed)`
38
+ * 与整数哈希 `noise2d(x, y, seed)`,同样的种子永远给同样的序列。
39
+ * - 像素颜色全部走整数运算 + 位运算截断,没有任何浮点累积误差参与分支
40
+ * (唯一的浮点是 `Math.sin` 的波纹相位,同样的参数在同一个 JS 引擎里结果一致)。
41
+ * - 行序、遍历顺序固定(行优先),不依赖 Map / Set 的插入顺序做输出。
42
+ * - zlib 压缩级别固定为 6(见 `png.ts`),所以 PNG 字节也是稳定的。
43
+ *
44
+ * 依赖只有 `./bitmap.js` + `./png.js` + Node 内置模块,**不联网、不新增依赖**。
45
+ */
46
+ import { encodePng } from "./png.js";
47
+ import { decodeFile } from "./bitmap.js";
48
+ import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
49
+ import { tmpdir } from "node:os";
50
+ import { join } from "node:path";
51
+ import { SAMPLE_TEXTURE_OVERRIDE } from "./mapsample.js";
52
+ /** A2 图的块形状数(0..4)。 */
53
+ export const SAMPLE_SHAPES = 5;
54
+ /** A2 图的变体数(块列数)。 */
55
+ export const SAMPLE_VARIANTS = 2;
56
+ /** 逻辑格边长(像素)。 */
57
+ export const SAMPLE_CELL = 32;
58
+ /** A2 小格边长(像素)—— 一个逻辑格 = 2×2 个小格。 */
59
+ const A2_CELL = SAMPLE_CELL / 2;
60
+ /** 过渡带宽度(像素):贴着小格的外边,靠内侧 2px 做抖动过渡。 */
61
+ const BAND = 5;
62
+ /** 过渡带内侧的抖动宽度(像素)。 */
63
+ const BAND_DITHER = 2;
64
+ /**
65
+ * 内角带(形状 3)向角内延伸的长度(像素)。
66
+ *
67
+ * 为什么不画一个 5×5 的小方块:那只有整块 1% 的面积,逐像素平均绝对差最多 3~5 色阶
68
+ * —— 界面上根本看不出形状 3 与形状 4 的区别(`--check` 会断言这个差值 > 6)。
69
+ * 改成 5px 宽、向角内延伸 9px 的 **L 形**内角带(约占整块 29%),仍然紧贴小格的角,
70
+ * 缩小视野后也能一眼看出「这里缺一个对角邻居」。
71
+ */
72
+ const CORNER_ARM = 9;
73
+ /**
74
+ * 四种地貌与它们的邻接关系(**固定表**,不随调用变化)。
75
+ *
76
+ * 邻接是**成对**的:草地↔土路要能互相压边;水只挨着草地(岸边);石墙没有邻接地貌,
77
+ * 过渡带画深色阴影(墙脚)。表里没写的组合不会出现在内置素材里。
78
+ */
79
+ export const SAMPLE_KINDS = [
80
+ { kind: "grass", name: "内置 · 草地", file: "sample-grass.png", neighbor: "dirt", seed: 1001 },
81
+ { kind: "dirt", name: "内置 · 土路", file: "sample-dirt.png", neighbor: "grass", seed: 2002 },
82
+ { kind: "water", name: "内置 · 水", file: "sample-water.png", neighbor: "grass", seed: 3003 },
83
+ { kind: "stone", name: "内置 · 石墙", file: "sample-stone.png", neighbor: undefined, seed: 4004 }
84
+ ];
85
+ /** 道具网格图的一行 8 个道具(中文名,顺序即图块从左到右的顺序)。 */
86
+ export const SAMPLE_PROP_NAMES = [
87
+ "树",
88
+ "花",
89
+ "石头",
90
+ "木箱",
91
+ "栅栏",
92
+ "蘑菇",
93
+ "断枝",
94
+ "石头堆"
95
+ ];
96
+ /** 道具网格图的行数(每一行是同一组道具的一帧,界面上按行切)。 */
97
+ export const SAMPLE_PROP_ROWS = 5;
98
+ /** 道具网格图的文件名。 */
99
+ export const SAMPLE_PROP_FILE = "sample-props.png";
100
+ /** 道具网格图的中文名。 */
101
+ export const SAMPLE_PROP_NAME = "内置 · 道具";
102
+ // ---------------------------------------------------------------------------
103
+ // 确定性随机:所有「随机」都必须经过这里
104
+ // ---------------------------------------------------------------------------
105
+ /**
106
+ * mulberry32:32 位状态的确定性 PRNG(同一 seed 永远给同一序列)。
107
+ *
108
+ * 为什么不用 `Math.random`:内置素材的要求是「两次生成逐字节一致」,
109
+ * 而 `Math.random` 不可播种;出问题也没法复现到某一次的具体输出。
110
+ */
111
+ export function mulberry32(seed) {
112
+ let state = seed >>> 0;
113
+ return () => {
114
+ state = (state + 0x6d2b79f5) >>> 0;
115
+ let t = state;
116
+ t = Math.imul(t ^ (t >>> 15), t | 1);
117
+ t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
118
+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
119
+ };
120
+ }
121
+ /** 整数哈希噪声:同 (x, y, seed) 恒等;用来做逐像素抖动,不受调用顺序影响。 */
122
+ export function noise2d(x, y, seed) {
123
+ let h = (Math.imul(x | 0, 0x27d4eb2d) ^ Math.imul(y | 0, 0x165667b1) ^ Math.imul(seed | 0, 0x9e3779b1)) >>> 0;
124
+ h = Math.imul(h ^ (h >>> 15), 0x2c1b3c6d) >>> 0;
125
+ h = Math.imul(h ^ (h >>> 12), 0x297a2d39) >>> 0;
126
+ return ((h ^ (h >>> 15)) >>> 0) / 4294967296;
127
+ }
128
+ // ---------------------------------------------------------------------------
129
+ // 位图小工具(只在本文件内用;不动 bitmap.ts)
130
+ // ---------------------------------------------------------------------------
131
+ /** 造一张全透明的位图。 */
132
+ function createBitmap(width, height) {
133
+ return { width, height, rgba: Buffer.alloc(width * height * 4) };
134
+ }
135
+ /** 写一个像素;越界静默忽略(画过渡带时经常画到边上)。 */
136
+ function setPixel(bitmap, x, y, color) {
137
+ if (x < 0 || y < 0 || x >= bitmap.width || y >= bitmap.height)
138
+ return;
139
+ const i = (y * bitmap.width + x) * 4;
140
+ bitmap.rgba[i] = color[0];
141
+ bitmap.rgba[i + 1] = color[1];
142
+ bitmap.rgba[i + 2] = color[2];
143
+ bitmap.rgba[i + 3] = color[3];
144
+ }
145
+ /** 读一个像素;越界给全透明。 */
146
+ function getPixel(bitmap, x, y) {
147
+ if (x < 0 || y < 0 || x >= bitmap.width || y >= bitmap.height)
148
+ return [0, 0, 0, 0];
149
+ const i = (y * bitmap.width + x) * 4;
150
+ return [bitmap.rgba[i], bitmap.rgba[i + 1], bitmap.rgba[i + 2], bitmap.rgba[i + 3]];
151
+ }
152
+ /** 把 `src` 整张盖到 `dst` 的 (dx, dy) 上(不透明覆盖,不做 alpha 混合 —— 素材本来就全不透明)。 */
153
+ function blit(dst, src, dx, dy) {
154
+ for (let y = 0; y < src.height; y++) {
155
+ for (let x = 0; x < src.width; x++) {
156
+ const color = getPixel(src, x, y);
157
+ if (color[3] === 0)
158
+ continue;
159
+ setPixel(dst, dx + x, dy + y, color);
160
+ }
161
+ }
162
+ }
163
+ /** 复制一张位图。 */
164
+ function cloneBitmap(src) {
165
+ return { width: src.width, height: src.height, rgba: Buffer.from(src.rgba) };
166
+ }
167
+ /** 整数收口到 0..255。 */
168
+ function clamp255(value) {
169
+ if (value < 0)
170
+ return 0;
171
+ if (value > 255)
172
+ return 255;
173
+ return value | 0;
174
+ }
175
+ /** 手画线段(Bresenham)—— 道具的树枝、花瓣靠它。 */
176
+ function drawLine(bitmap, x0, y0, x1, y1, color, width = 1) {
177
+ let x = x0 | 0;
178
+ let y = y0 | 0;
179
+ const dx = Math.abs(x1 - x0);
180
+ const dy = Math.abs(y1 - y0);
181
+ const sx = x0 < x1 ? 1 : -1;
182
+ const sy = y0 < y1 ? 1 : -1;
183
+ let err = dx - dy;
184
+ for (;;) {
185
+ if (width <= 1)
186
+ setPixel(bitmap, x, y, color);
187
+ else
188
+ for (let oy = 0; oy < width; oy++)
189
+ for (let ox = 0; ox < width; ox++)
190
+ setPixel(bitmap, x + ox, y + oy, color);
191
+ if (x === x1 && y === y1)
192
+ break;
193
+ const e2 = 2 * err;
194
+ if (e2 > -dy) {
195
+ err -= dy;
196
+ x += sx;
197
+ }
198
+ if (e2 < dx) {
199
+ err += dx;
200
+ y += sy;
201
+ }
202
+ }
203
+ }
204
+ /** 手画实心圆(道具的树冠、花心、石头)。 */
205
+ function drawDisc(bitmap, cx, cy, radius, color) {
206
+ const r2 = radius * radius;
207
+ for (let y = Math.floor(cy - radius); y <= Math.ceil(cy + radius); y++) {
208
+ for (let x = Math.floor(cx - radius); x <= Math.ceil(cx + radius); x++) {
209
+ const dx = x - cx;
210
+ const dy = y - cy;
211
+ if (dx * dx + dy * dy <= r2)
212
+ setPixel(bitmap, x, y, color);
213
+ }
214
+ }
215
+ }
216
+ /** 手画实心椭圆(道具的树冠、蘑菇伞盖)。 */
217
+ function drawEllipse(bitmap, cx, cy, rx, ry, color) {
218
+ for (let y = Math.floor(cy - ry); y <= Math.ceil(cy + ry); y++) {
219
+ for (let x = Math.floor(cx - rx); x <= Math.ceil(cx + rx); x++) {
220
+ const dx = (x - cx) / rx;
221
+ const dy = (y - cy) / ry;
222
+ if (dx * dx + dy * dy <= 1)
223
+ setPixel(bitmap, x, y, color);
224
+ }
225
+ }
226
+ }
227
+ /** 手画空心矩形(木箱的边框、栅栏的横杆)。 */
228
+ function strokeRect(bitmap, x, y, w, h, color, thickness = 1) {
229
+ for (let t = 0; t < thickness; t++) {
230
+ for (let i = x + t; i < x + w - t; i++) {
231
+ setPixel(bitmap, i, y + t, color);
232
+ setPixel(bitmap, i, y + h - 1 - t, color);
233
+ }
234
+ for (let j = y + t; j < y + h - t; j++) {
235
+ setPixel(bitmap, x + t, j, color);
236
+ setPixel(bitmap, x + w - 1 - t, j, color);
237
+ }
238
+ }
239
+ }
240
+ /** 手画实心矩形。 */
241
+ function fillRect(bitmap, x, y, w, h, color) {
242
+ for (let j = 0; j < h; j++) {
243
+ for (let i = 0; i < w; i++)
244
+ setPixel(bitmap, x + i, y + j, color);
245
+ }
246
+ }
247
+ const PALETTES = {
248
+ grass: { dark: [58, 104, 48], light: [96, 156, 74], accent: [140, 196, 104] },
249
+ dirt: { dark: [112, 84, 54], light: [160, 128, 86], accent: [90, 68, 46] },
250
+ water: { dark: [40, 92, 158], light: [74, 140, 206], accent: [186, 226, 246] },
251
+ stone: { dark: [104, 104, 112], light: [152, 152, 160], accent: [78, 78, 86] }
252
+ };
253
+ function mixColor(from, to, t) {
254
+ return [clamp255(from[0] + (to[0] - from[0]) * t), clamp255(from[1] + (to[1] - from[1]) * t), clamp255(from[2] + (to[2] - from[2]) * t)];
255
+ }
256
+ /**
257
+ * 程序化纹理:基色 + 抖动噪声 + 少量像素细节。
258
+ *
259
+ * `phase` 只对**水**有意义(波纹相位);`frame`(帧号)影响**所有**地貌:
260
+ * 它同时改噪声种子与图案的空间偏移,所以「同一地貌的第 1 帧」与第 0 帧是逐像素不同的
261
+ * 两帧,而不是同一张图复制两遍。这对有结构的地貌(石墙的砖缝)尤其重要 ——
262
+ * 只换噪声种子时,砖缝位置一模一样,两帧的平均绝对差只有 4 色阶(实测),
263
+ * 界面上看不出在动。
264
+ */
265
+ export function renderLocalTexture(kind, size, seed, phase = 0, frame = 0) {
266
+ const bitmap = createBitmap(size, size);
267
+ const palette = PALETTES[kind];
268
+ // 帧号 → 图案来源坐标偏移 8px + 噪声种子 +17:两者一起保证两帧没有相关性。
269
+ const shift = frame * 8;
270
+ const noiseSeed = seed + frame * 17;
271
+ const wrap = (value) => ((value % size) + size) % size;
272
+ // 逐像素基色 + 抖动。噪声来自整数哈希,所以与遍历顺序无关。
273
+ for (let y = 0; y < size; y++) {
274
+ for (let x = 0; x < size; x++) {
275
+ const sx = wrap(x + shift);
276
+ const sy = wrap(y + shift);
277
+ let t = 0.18 + 0.64 * noise2d(sx, sy, noiseSeed);
278
+ if (kind === "water") {
279
+ // 两层正弦叠加做波纹:相位差让变体 0 / 1 的亮点位置明显错开。
280
+ const wave = Math.sin((sx / size) * Math.PI * 4 + phase) * 0.5 +
281
+ Math.sin((sy / size) * Math.PI * 2 + phase * 1.5) * 0.5;
282
+ t = 0.24 + 0.42 * (0.5 + 0.5 * wave) + 0.16 * noise2d(sx, sy, noiseSeed);
283
+ }
284
+ else if (kind === "stone") {
285
+ // 砌砖:每 8 行一条砖缝,奇数行错半块。
286
+ const row = Math.floor(sy / 8);
287
+ const offset = row % 2 === 0 ? 0 : 8;
288
+ const brickX = ((sx + offset) % 16 + 16) % 16;
289
+ const brickY = sy % 8;
290
+ if (brickY === 0 || brickX === 0)
291
+ t = 0.06;
292
+ else if (brickY === 1 || brickX === 1)
293
+ t = 0.88;
294
+ else
295
+ t = 0.3 + 0.4 * noise2d(sx, sy, noiseSeed) + (row % 2 === 0 ? 0.04 : 0);
296
+ }
297
+ const color = mixColor(palette.dark, palette.light, t);
298
+ setPixel(bitmap, x, y, [color[0], color[1], color[2], 255]);
299
+ }
300
+ }
301
+ // 细节(草叶 / 石子 / 波光 / 石斑)也必须跟着帧走:只用 `seed` 的话,两帧的基色纹理
302
+ // 变了、细节却在原地不动,逐像素平均绝对差只有 4 色阶(实测),看起来像「贴图闪了一下」。
303
+ const rand = mulberry32(noiseSeed ^ 0x5f3759df);
304
+ const accent = [palette.accent[0], palette.accent[1], palette.accent[2], 255];
305
+ if (kind === "grass" || kind === "dirt") {
306
+ // 草叶 / 石子:短笔画,数量与位置都由上面的 PRNG 决定。
307
+ const strokes = kind === "grass" ? size * 3 : size;
308
+ for (let i = 0; i < strokes; i++) {
309
+ const x = Math.floor(rand() * size);
310
+ const y = Math.floor(rand() * size);
311
+ const length = kind === "grass" ? 1 + Math.floor(rand() * 3) : 1 + Math.floor(rand() * 2);
312
+ for (let k = 0; k < length; k++) {
313
+ setPixel(bitmap, x + (kind === "grass" && rand() < 0.4 ? 1 : 0), y - k, accent);
314
+ }
315
+ }
316
+ }
317
+ if (kind === "water") {
318
+ // 波光高光:跟着相位一起移动,两帧才看得出「在流动」。
319
+ for (let i = 0; i < size / 2; i++) {
320
+ const x = Math.floor(rand() * size);
321
+ const y = Math.floor(rand() * size);
322
+ const shifted = ((x + Math.round((phase / (Math.PI * 2)) * size)) % size + size) % size;
323
+ setPixel(bitmap, shifted, y, accent);
324
+ setPixel(bitmap, (shifted + 1) % size, y, accent);
325
+ }
326
+ }
327
+ if (kind === "stone") {
328
+ for (let i = 0; i < size; i++) {
329
+ const x = Math.floor(rand() * size);
330
+ const y = Math.floor(rand() * size);
331
+ setPixel(bitmap, x, y, accent);
332
+ }
333
+ }
334
+ return bitmap;
335
+ }
336
+ /** 覆盖图里的地貌清单:`SAMPLE_TEXTURE_OVERRIDE` 里真正填了 base64 的那些。 */
337
+ export function overrideKinds() {
338
+ return SAMPLE_KINDS.map((item) => item.kind).filter((kind) => hasOverride(kind));
339
+ }
340
+ function hasOverride(kind) {
341
+ const value = SAMPLE_TEXTURE_OVERRIDE[kind];
342
+ return typeof value === "string" && value.trim() !== "";
343
+ }
344
+ /** `primeSampleTextures()` 解出来的覆盖纹理(键 = 地貌)。 */
345
+ const overrideTextures = new Map();
346
+ /** 是否已经 prime 过:用来把「没配覆盖」和「配了但忘了 prime」区分开(后者要明确报错)。 */
347
+ let overridesPrimed = false;
348
+ /**
349
+ * 把 `SAMPLE_TEXTURE_OVERRIDE` 里的 base64 PNG 解码成纹理(**异步**,只在配了覆盖时才需要)。
350
+ *
351
+ * 为什么是独立的异步函数:解码走 `bitmap.ts` 的统一入口 `decodeFile`(内部是 ffmpeg),
352
+ * 它能吃任意色深 / 调色板 / 隔行的 PNG,而 Seedream 返回的图未必是 8 位 RGBA 直存 ——
353
+ * 自己手写解码器在真实返回上大概率一跑就炸(而且那条路径没法在自检里实测)。
354
+ * 但 `renderSampleTilesets()` 的契约是**同步**的(见 §9 的接口),所以解码拆出来:
355
+ * 调用方 `await primeSampleTextures()` 一次,之后同步渲染。
356
+ *
357
+ * 覆盖表是空对象(默认)时它是空操作,不需要调用 —— 但**配了覆盖却没 prime** 时
358
+ * `textureFor()` 会明确抛错,绝不静默退回程序化纹理(那会得到「界面看着能用、
359
+ * 纹理却不是他生成的那张」,最难查)。
360
+ */
361
+ export async function primeSampleTextures() {
362
+ overridesPrimed = true;
363
+ overrideTextures.clear();
364
+ const kinds = overrideKinds();
365
+ if (kinds.length === 0)
366
+ return;
367
+ const dir = mkdtempSync(join(tmpdir(), "gmm-sample-"));
368
+ try {
369
+ for (const kind of kinds) {
370
+ const file = join(dir, `${kind}.png`);
371
+ writeFileSync(file, Buffer.from(SAMPLE_TEXTURE_OVERRIDE[kind], "base64"));
372
+ overrideTextures.set(kind, await decodeFile(file));
373
+ }
374
+ }
375
+ finally {
376
+ rmSync(dir, { recursive: true, force: true });
377
+ }
378
+ }
379
+ /**
380
+ * 取某个地貌的纹理:优先用 `SAMPLE_TEXTURE_OVERRIDE` 里的(`--ark` 生成、已降采样成 32×32),
381
+ * 没有就程序化。
382
+ *
383
+ * `variant` 是**帧号**:程序化纹理按它改噪声种子、图案偏移与波纹相位,所以同一个地貌的
384
+ * 两个变体是逐像素不同的两帧,而不是同一张图复制两遍。覆盖表里只有一张静态纹理,
385
+ * 两个变体就只能是同一张 —— 想要真动画就不要给这个地貌配覆盖图。
386
+ */
387
+ export function textureFor(kind, size, phase = 0, variant = 0) {
388
+ if (hasOverride(kind)) {
389
+ const decoded = overrideTextures.get(kind);
390
+ if (decoded === undefined) {
391
+ const reason = overridesPrimed
392
+ ? "已经 prime 过,但缓存里没有它(prime 中途失败?)"
393
+ : "纹理还没解码(没有调用过 primeSampleTextures)";
394
+ throw new Error(`内置素材的「${kind}」配了 SAMPLE_TEXTURE_OVERRIDE,但${reason}:` +
395
+ `先 \`await primeSampleTextures()\` 再生成内置素材(解码走 ffmpeg,是异步的)。`);
396
+ }
397
+ return normalizeTexture(decoded, size);
398
+ }
399
+ const spec = SAMPLE_KINDS.find((item) => item.kind === kind);
400
+ return renderLocalTexture(kind, size, spec?.seed ?? 1, phase, variant);
401
+ }
402
+ /** 把任意尺寸的纹理源收成 `size × size`(多则居中裁剪,少则平铺)。 */
403
+ function normalizeTexture(source, size) {
404
+ if (source.width === size && source.height === size)
405
+ return cloneBitmap(source);
406
+ const out = createBitmap(size, size);
407
+ const originX = Math.floor((source.width - size) / 2);
408
+ const originY = Math.floor((source.height - size) / 2);
409
+ for (let y = 0; y < size; y++) {
410
+ for (let x = 0; x < size; x++) {
411
+ const sx = ((x + originX) % source.width + source.width) % source.width;
412
+ const sy = ((y + originY) % source.height + source.height) % source.height;
413
+ setPixel(out, x, y, getPixel(source, sx, sy));
414
+ }
415
+ }
416
+ return out;
417
+ }
418
+ // ---------------------------------------------------------------------------
419
+ // A2 拼块图
420
+ // ---------------------------------------------------------------------------
421
+ /**
422
+ * 抖动过渡:用 4×4 有序抖动矩阵决定「这一像素盖邻接地貌还是露本体」。
423
+ *
424
+ * 这里故意不用随机数发生器:矩阵是常量,过渡带在任何调用顺序下都长得一模一样,
425
+ * 而且天然严格限制在固定宽度内(不会有一条像素随机飘出去的毛边)。
426
+ */
427
+ const DITHER_4X4 = [0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5];
428
+ /** `depth` = 从带外缘往里数第几像素(0 = 最外面)。 */
429
+ function ditherHit(x, y, depth) {
430
+ const threshold = DITHER_4X4[(y % 4) * 4 + (x % 4)];
431
+ // 外缘 3 行(5-2)全实心,内侧 2 行按矩阵交错,形成 5px 宽、边缘毛糙的过渡。
432
+ return depth < BAND - BAND_DITHER || threshold < (BAND - depth) * 5;
433
+ }
434
+ /** 取过渡带该用哪个像素:有邻接地貌就取它的纹理,没有就用深色阴影。 */
435
+ function bandPixel(neighbor, shadow, x, y) {
436
+ if (neighbor === undefined)
437
+ return [shadow[0], shadow[1], shadow[2], 255];
438
+ const color = getPixel(neighbor, x & (SAMPLE_CELL - 1), y & (SAMPLE_CELL - 1));
439
+ return [color[0], color[1], color[2], 255];
440
+ }
441
+ /**
442
+ * 在一个**小格**上压一条过渡带。
443
+ *
444
+ * `edge` 是小格的外边(`left` / `right` / `top` / `bottom`)—— 同一个小格在块里的位置
445
+ * 不同,同一条边可能是「外」也可能是「内」,所以边由调用方按 (dx, dy) 显式给出,
446
+ * **不能写死往左边画**(写死会让 TR / BR 两个小格的接缝对不上)。
447
+ */
448
+ function applyBandEdge(target, originX, originY, edge, neighbor, shadow) {
449
+ for (let d = 0; d < BAND; d++) {
450
+ for (let i = 0; i < A2_CELL; i++) {
451
+ let lx;
452
+ let ly;
453
+ if (edge === "left") {
454
+ lx = d;
455
+ ly = i;
456
+ }
457
+ else if (edge === "right") {
458
+ lx = A2_CELL - 1 - d;
459
+ ly = i;
460
+ }
461
+ else if (edge === "top") {
462
+ lx = i;
463
+ ly = d;
464
+ }
465
+ else {
466
+ lx = i;
467
+ ly = A2_CELL - 1 - d;
468
+ }
469
+ if (!ditherHit(lx, ly, d))
470
+ continue;
471
+ // 最外 1px 压一条深色描边:地形边界有一条暗线,缩小视野后也能一眼看出「这里是边界」,
472
+ // 而且让「形状 0 vs 形状 1」这类只差一条带的对比有足够的色阶差(见 `--check` 的断言)。
473
+ const color = d === 0
474
+ ? [shadow[0], shadow[1], shadow[2], 255]
475
+ : bandPixel(neighbor, shadow, originX + lx, originY + ly);
476
+ setPixel(target, originX + lx, originY + ly, color);
477
+ }
478
+ }
479
+ }
480
+ /**
481
+ * 形状 3 的记号:在**朝对角的那一角**画一段 L 形内角带,紧贴小格的角。
482
+ *
483
+ * 形状 3 = 左/上都连通但没有对角,所以整块里只有四个角各缺一块「邻接地貌」;
484
+ * 形状 4 什么都不画。这一块就是「有没有对角邻居」在贴图上的唯一区别,
485
+ * 所以它必须足够大(见 `CORNER_ARM`),否则大小两个形状在界面上看不出差别。
486
+ *
487
+ * L 形朝向对角的**内侧**边界再压 1px 深色描边(和石墙过渡带同一个阴影色),
488
+ * 让「缺角」有立体感,而不是平贴一块别的颜色。
489
+ */
490
+ function applyDiagonalCorner(target, originX, originY, dx, dy, neighbor, shadow) {
491
+ for (let j = 0; j < CORNER_ARM; j++) {
492
+ for (let i = 0; i < CORNER_ARM; i++) {
493
+ const vertical = i < BAND;
494
+ const horizontal = j < BAND;
495
+ if (!vertical && !horizontal)
496
+ continue;
497
+ // 朝向本体的一侧(L 的内缘)画描边。
498
+ const rim = (vertical && j === CORNER_ARM - 1) || (horizontal && i === CORNER_ARM - 1);
499
+ const lx = dx === 0 ? i : A2_CELL - 1 - i;
500
+ const ly = dy === 0 ? j : A2_CELL - 1 - j;
501
+ const color = rim
502
+ ? [shadow[0], shadow[1], shadow[2], 255]
503
+ : bandPixel(neighbor, shadow, originX + lx, originY + ly);
504
+ setPixel(target, originX + lx, originY + ly, color);
505
+ }
506
+ }
507
+ }
508
+ /**
509
+ * 单个形状块的完整像素(32×32)。
510
+ *
511
+ * 块内是 2×2 个小格,**四个小格是镜像的**:每个小格的「横向外边」「纵向外边」
512
+ * 「对角角」由它在块里的位置决定(见下表)。形状号给的是「这两个方向的邻居连通不连通」:
513
+ *
514
+ * | 形状 | 含义 | 每个小格画什么 |
515
+ * |---|---|---|
516
+ * | 0 | 左、上都不连通 | 横向外边 + 纵向外边(孤岛,整块四周一圈带) |
517
+ * | 1 | 上连通、左不连通 | 只画横向外边 |
518
+ * | 2 | 左连通、上不连通 | 只画纵向外边 |
519
+ * | 3 | 左上都连通、对角不连通 | 只在朝对角那一角画 L 形内角带(5px 宽、向角内延伸 9px) |
520
+ * | 4 | 全连通 | 不画带(纯本体纹理) |
521
+ *
522
+ * | 小格 | 横向外边 | 纵向外边 | 对角角 |
523
+ * |---|---|---|---|
524
+ * | TL (0,0) | 左 | 上 | 左上 |
525
+ * | TR (1,0) | 右 | 上 | 右上 |
526
+ * | BL (0,1) | 左 | 下 | 左下 |
527
+ * | BR (1,1) | 右 | 下 | 右下 |
528
+ */
529
+ function renderShapeBlock(base, shape, neighbor) {
530
+ const block = cloneBitmap(base);
531
+ // 石墙没有邻接地貌:过渡带用同色系的深色阴影(墙脚),而不是凭空造一种地形。
532
+ const shadow = [20, 18, 26, 255];
533
+ for (const dx of [0, 1]) {
534
+ for (const dy of [0, 1]) {
535
+ const originX = dx * A2_CELL;
536
+ const originY = dy * A2_CELL;
537
+ const horizontal = dx === 0 ? "left" : "right";
538
+ const vertical = dy === 0 ? "top" : "bottom";
539
+ if (shape === 0) {
540
+ applyBandEdge(block, originX, originY, horizontal, neighbor, shadow);
541
+ applyBandEdge(block, originX, originY, vertical, neighbor, shadow);
542
+ }
543
+ else if (shape === 1) {
544
+ applyBandEdge(block, originX, originY, horizontal, neighbor, shadow);
545
+ }
546
+ else if (shape === 2) {
547
+ applyBandEdge(block, originX, originY, vertical, neighbor, shadow);
548
+ }
549
+ else if (shape === 3) {
550
+ applyDiagonalCorner(block, originX, originY, dx, dy, neighbor, shadow);
551
+ }
552
+ }
553
+ }
554
+ return block;
555
+ }
556
+ /**
557
+ * 帧间亮度差(色阶)。见 `applyFrameTint` 的说明。
558
+ */
559
+ const FRAME_TINT = 7;
560
+ /**
561
+ * 帧级微调:第 1 帧的**整块**(本体纹理 + 过渡带)统一提亮 `FRAME_TINT` 色阶。
562
+ *
563
+ * 为什么需要它:过渡带在两个变体里是同一份邻接地貌纹理(它不属于本图的帧),
564
+ * 而形状 0 的带占了整块一半以上的面积 —— 于是「只换本体纹理」的帧差会被稀释。
565
+ * 石墙尤其极端:它的带是一整片纯色阴影、零纹理,只换本体时两帧的逐像素平均绝对差
566
+ * 只有 3.7 色阶(实测),界面上等于没有动画。
567
+ *
568
+ * 这是**补充**手段,不是唯一手段:本体的噪声种子 / 图案偏移 / 波纹相位仍然逐帧不同
569
+ * (那才是「能看出在动」的部分)。`--check` 会断言每张图的两个变体平均绝对差 > 6。
570
+ */
571
+ function applyFrameTint(bitmap, frame) {
572
+ if (frame === 0)
573
+ return;
574
+ const delta = frame * FRAME_TINT;
575
+ const rgba = bitmap.rgba;
576
+ for (let i = 0; i < rgba.length; i += 4) {
577
+ rgba[i] = clamp255(rgba[i] + delta);
578
+ rgba[i + 1] = clamp255(rgba[i + 1] + delta);
579
+ rgba[i + 2] = clamp255(rgba[i + 2] + delta);
580
+ }
581
+ }
582
+ /**
583
+ * A2 图:`64×160` = 4 列 × 10 行的 16×16 小格。
584
+ *
585
+ * 块列号 = 变体(0..1),块行号 = 形状(0..4);每个逻辑块 32×32,
586
+ * 内部是 2×2 个 16×16 小格 —— 也就是 `detectLayout` 认出来的 `a2quad` 结构。
587
+ *
588
+ * **变体就是帧**:变体 1 是「相位 / 噪声种子错开的同一地貌」,两帧平均绝对差要显著
589
+ * (水的波纹要能看出在动)。这是内置素材里唯一的动画来源。
590
+ */
591
+ export function renderA2Sheet(spec) {
592
+ const bitmap = createBitmap(A2_CELL * 4, A2_CELL * 10);
593
+ const neighborKind = spec.neighbor;
594
+ const neighborSpec = SAMPLE_KINDS.find((item) => item.kind === neighborKind);
595
+ // 邻接地貌取变体 0 的相位:它是「别的地貌」,不该跟着本图的帧一起动。
596
+ const neighbor = neighborKind === undefined ? undefined : renderLocalTexture(neighborKind, SAMPLE_CELL, neighborSpec?.seed ?? 7, 0);
597
+ for (let variant = 0; variant < SAMPLE_VARIANTS; variant++) {
598
+ // 变体 1:噪声种子 +17、图案偏移 8px、水再错开 90° 波纹相位 —— 两帧逐像素就不同。
599
+ const phase = spec.kind === "water" ? (variant * Math.PI) / 2 : 0;
600
+ const base = textureFor(spec.kind, SAMPLE_CELL, phase, variant);
601
+ for (let shape = 0; shape < SAMPLE_SHAPES; shape++) {
602
+ const block = renderShapeBlock(base, shape, neighbor);
603
+ applyFrameTint(block, variant);
604
+ blit(bitmap, block, variant * SAMPLE_CELL, shape * SAMPLE_CELL);
605
+ }
606
+ }
607
+ return bitmap;
608
+ }
609
+ // ---------------------------------------------------------------------------
610
+ // 道具网格图
611
+ // ---------------------------------------------------------------------------
612
+ const P = {
613
+ trunk: [92, 62, 36, 255],
614
+ trunkDark: [64, 42, 26, 255],
615
+ leafDark: [40, 92, 46, 255],
616
+ leaf: [62, 126, 58, 255],
617
+ leafLight: [96, 164, 74, 255],
618
+ petal: [232, 108, 150, 255],
619
+ petalLight: [248, 176, 200, 255],
620
+ center: [250, 208, 96, 255],
621
+ stem: [64, 118, 54, 255],
622
+ stoneDark: [96, 96, 104, 255],
623
+ stone: [132, 132, 140, 255],
624
+ stoneLight: [172, 172, 180, 255],
625
+ wood: [162, 118, 66, 255],
626
+ woodDark: [110, 76, 42, 255],
627
+ woodLight: [198, 156, 96, 255],
628
+ fence: [148, 126, 92, 255],
629
+ fenceDark: [102, 86, 62, 255],
630
+ cap: [196, 62, 54, 255],
631
+ capDark: [150, 40, 36, 255],
632
+ cream: [238, 226, 198, 255]
633
+ };
634
+ /** 画一个道具(32×32,透明底)。`name` 用中文名,见 `SAMPLE_PROP_NAMES`。 */
635
+ function drawProp(bitmap, name) {
636
+ switch (name) {
637
+ case "树": {
638
+ fillRect(bitmap, 14, 18, 5, 12, P.trunk);
639
+ fillRect(bitmap, 14, 18, 2, 12, P.trunkDark);
640
+ drawDisc(bitmap, 16, 14, 10, P.leafDark);
641
+ drawDisc(bitmap, 15, 12, 8, P.leaf);
642
+ drawDisc(bitmap, 12, 10, 4, P.leafLight);
643
+ break;
644
+ }
645
+ case "花": {
646
+ fillRect(bitmap, 15, 18, 2, 10, P.stem);
647
+ setPixel(bitmap, 18, 22, P.stem);
648
+ setPixel(bitmap, 13, 24, P.stem);
649
+ drawDisc(bitmap, 16, 13, 4, P.petal);
650
+ drawDisc(bitmap, 16, 13, 2, P.petalLight);
651
+ drawDisc(bitmap, 16, 13, 1, P.center);
652
+ break;
653
+ }
654
+ case "石头": {
655
+ drawEllipse(bitmap, 16, 20, 11, 8, P.stoneDark);
656
+ drawEllipse(bitmap, 16, 19, 9, 6, P.stone);
657
+ drawDisc(bitmap, 12, 17, 3, P.stoneLight);
658
+ break;
659
+ }
660
+ case "木箱": {
661
+ fillRect(bitmap, 6, 10, 20, 17, P.wood);
662
+ strokeRect(bitmap, 6, 10, 20, 17, P.woodDark, 1);
663
+ drawLine(bitmap, 7, 11, 24, 26, P.woodDark);
664
+ drawLine(bitmap, 24, 11, 7, 26, P.woodDark);
665
+ fillRect(bitmap, 7, 11, 18, 1, P.woodLight);
666
+ break;
667
+ }
668
+ case "栅栏": {
669
+ fillRect(bitmap, 4, 8, 4, 20, P.fence);
670
+ fillRect(bitmap, 14, 6, 4, 22, P.fence);
671
+ fillRect(bitmap, 24, 8, 4, 20, P.fence);
672
+ fillRect(bitmap, 2, 12, 28, 3, P.fenceDark);
673
+ fillRect(bitmap, 2, 20, 28, 3, P.fenceDark);
674
+ fillRect(bitmap, 4, 8, 1, 20, P.fenceDark);
675
+ fillRect(bitmap, 14, 6, 1, 22, P.fenceDark);
676
+ fillRect(bitmap, 24, 8, 1, 20, P.fenceDark);
677
+ break;
678
+ }
679
+ case "蘑菇": {
680
+ fillRect(bitmap, 14, 18, 4, 10, P.cream);
681
+ drawEllipse(bitmap, 16, 16, 11, 7, P.capDark);
682
+ drawEllipse(bitmap, 16, 15, 9, 6, P.cap);
683
+ drawDisc(bitmap, 12, 13, 2, P.cream);
684
+ drawDisc(bitmap, 20, 15, 2, P.cream);
685
+ drawDisc(bitmap, 16, 18, 1, P.cream);
686
+ break;
687
+ }
688
+ case "断枝": {
689
+ drawLine(bitmap, 5, 24, 26, 12, P.trunkDark, 3);
690
+ drawLine(bitmap, 5, 24, 26, 12, P.trunk, 2);
691
+ drawLine(bitmap, 16, 17, 12, 10, P.trunk, 2);
692
+ drawLine(bitmap, 21, 14, 27, 8, P.trunk, 2);
693
+ drawDisc(bitmap, 27, 8, 2, P.leaf);
694
+ drawDisc(bitmap, 12, 10, 2, P.leaf);
695
+ break;
696
+ }
697
+ case "石头堆": {
698
+ drawEllipse(bitmap, 10, 22, 8, 6, P.stoneDark);
699
+ drawEllipse(bitmap, 10, 21, 6, 4, P.stone);
700
+ drawEllipse(bitmap, 22, 23, 7, 5, P.stoneDark);
701
+ drawEllipse(bitmap, 22, 22, 5, 4, P.stoneLight);
702
+ drawDisc(bitmap, 17, 14, 5, P.stone);
703
+ drawDisc(bitmap, 15, 12, 2, P.stoneLight);
704
+ break;
705
+ }
706
+ default:
707
+ // 认不出的名字不静默画空图:那是自检夹具和实现脱节的信号。
708
+ throw new Error(`未知道具名:${name}`);
709
+ }
710
+ // 底部一圈极浅的接地阴影,让道具不是「浮」在地块上。
711
+ for (let x = 4; x < 28; x++) {
712
+ if (getPixel(bitmap, x, 30)[3] === 0)
713
+ setPixel(bitmap, x, 30, [0, 0, 0, 60]);
714
+ }
715
+ }
716
+ /** 道具网格图:`8×5` 个 32px 图块(列 = 道具,行 = 帧)。 */
717
+ export function renderPropSheet() {
718
+ const bitmap = createBitmap(SAMPLE_CELL * SAMPLE_PROP_NAMES.length, SAMPLE_CELL * SAMPLE_PROP_ROWS);
719
+ for (let row = 0; row < SAMPLE_PROP_ROWS; row++) {
720
+ for (let col = 0; col < SAMPLE_PROP_NAMES.length; col++) {
721
+ const tile = createBitmap(SAMPLE_CELL, SAMPLE_CELL);
722
+ drawProp(tile, SAMPLE_PROP_NAMES[col]);
723
+ blit(bitmap, tile, col * SAMPLE_CELL, row * SAMPLE_CELL);
724
+ }
725
+ }
726
+ return bitmap;
727
+ }
728
+ // ---------------------------------------------------------------------------
729
+ // 对外入口
730
+ // ---------------------------------------------------------------------------
731
+ /**
732
+ * 生成整套内置示例素材:4 张 A2 图 + 1 张道具网格图。
733
+ *
734
+ * **确定性**:同一个进程里调两次,返回的 `bytes` 逐字节一致
735
+ * (`scripts/make-map-sample-tileset.mjs --check` 会真的断言这一点)。
736
+ */
737
+ export function renderSampleTilesets() {
738
+ const out = [];
739
+ for (const spec of SAMPLE_KINDS) {
740
+ const sheet = renderA2Sheet(spec);
741
+ out.push({
742
+ name: spec.name,
743
+ file: spec.file,
744
+ bytes: encodePng(sheet.rgba, sheet.width, sheet.height),
745
+ shape: "a2quad",
746
+ cell: { width: SAMPLE_CELL, height: SAMPLE_CELL },
747
+ animRows: false
748
+ });
749
+ }
750
+ const props = renderPropSheet();
751
+ out.push({
752
+ name: SAMPLE_PROP_NAME,
753
+ file: SAMPLE_PROP_FILE,
754
+ bytes: encodePng(props.rgba, props.width, props.height),
755
+ shape: "plain",
756
+ cell: { width: SAMPLE_CELL, height: SAMPLE_CELL },
757
+ animRows: false
758
+ });
759
+ return out;
760
+ }