dsh-game-material-master 0.2.1 → 0.4.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.
- package/README.en.md +383 -0
- package/README.md +83 -6
- package/lib/client.js +4436 -877
- package/lib/config.js +45 -5
- package/lib/directions.js +120 -5
- package/lib/index.js +375 -12
- package/lib/jsonio.js +49 -3
- package/lib/links.js +10 -2
- package/lib/tilegen.js +1702 -0
- package/lib/tilegeom.js +1392 -0
- package/lib/tilemap.js +560 -0
- package/lib/tilemedia.js +243 -0
- package/lib/tools.js +397 -19
- package/lib/wire.js +100 -4
- package/package.json +3 -2
package/lib/tilemedia.js
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 地图地块模块的图像编解码与基础像素操作。
|
|
3
|
+
*
|
|
4
|
+
* 这一层只做「字节 ↔ RGBA 缓冲区」和几个原语(PNG 头、颜色统计、菱形遮罩)。
|
|
5
|
+
* 一切几何算法都在 `tilegeom.ts`,拼图在 `tilemap.ts`。
|
|
6
|
+
*
|
|
7
|
+
* 为什么 PNG 解码走 ffmpeg:仓库**没有任何运行时依赖**(见 AGENTS.md),
|
|
8
|
+
* 手写 PNG 解码器要处理 5 种 filter 类型,而 ffmpeg 已经是本插件的既有依赖
|
|
9
|
+
* (抽帧、抠像都用它)。这里把调用集中成一个函数,以后要换只改一处。
|
|
10
|
+
*/
|
|
11
|
+
import { spawn } from "node:child_process";
|
|
12
|
+
import { encodePng } from "./png.js";
|
|
13
|
+
import { ffmpegPath } from "./media.js";
|
|
14
|
+
/** PNG 头里的尺寸(不解码像素,用来做廉价校验)。 */
|
|
15
|
+
export function pngSize(bytes) {
|
|
16
|
+
if (bytes.length < 24)
|
|
17
|
+
return undefined;
|
|
18
|
+
if (bytes[0] !== 0x89 || bytes[1] !== 0x50 || bytes[2] !== 0x4e || bytes[3] !== 0x47)
|
|
19
|
+
return undefined;
|
|
20
|
+
return { width: bytes.readUInt32BE(16), height: bytes.readUInt32BE(20) };
|
|
21
|
+
}
|
|
22
|
+
/** 文件头嗅探(与 ark.ts 同一套判据,但只回扩展名)。 */
|
|
23
|
+
export function sniffImageExt(bytes) {
|
|
24
|
+
if (bytes.length >= 8 && bytes[0] === 0x89 && bytes[1] === 0x50)
|
|
25
|
+
return "png";
|
|
26
|
+
if (bytes.length >= 3 && bytes[0] === 0xff && bytes[1] === 0xd8)
|
|
27
|
+
return "jpg";
|
|
28
|
+
if (bytes.length >= 12 && bytes.toString("ascii", 0, 4) === "RIFF" && bytes.toString("ascii", 8, 12) === "WEBP")
|
|
29
|
+
return "webp";
|
|
30
|
+
if (bytes.length >= 6 && bytes.toString("ascii", 0, 3) === "GIF")
|
|
31
|
+
return "gif";
|
|
32
|
+
return "png";
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* 用 ffmpeg 把任意图片解成 RGBA8。
|
|
36
|
+
*
|
|
37
|
+
* `maxEdge > 0` 时先等比缩到长边不超过它(大图测量时省内存;规整必须用原图)。
|
|
38
|
+
* 解出来的尺寸会和 PNG 头对一次账 —— 管道读二进制最容易出的错就是「少读/多读」,
|
|
39
|
+
* 而那种错只会让几何量出偏差值,不会抛异常。
|
|
40
|
+
*/
|
|
41
|
+
export async function decodeImage(file, maxEdge = 0) {
|
|
42
|
+
const args = ["-hide_banner", "-loglevel", "error", "-i", file];
|
|
43
|
+
if (maxEdge > 0) {
|
|
44
|
+
args.push("-vf", `scale='if(gt(iw,ih),min(iw,${maxEdge}),-2)':'if(gt(iw,ih),-2,min(ih,${maxEdge}))'`);
|
|
45
|
+
}
|
|
46
|
+
args.push("-f", "rawvideo", "-pix_fmt", "rgba", "-");
|
|
47
|
+
const rgba = await runFfmpeg(args);
|
|
48
|
+
// 尺寸取自 ffprobe 语义:先按 raw 长度反推单像素宽度不现实,所以单独探一次。
|
|
49
|
+
const { width, height } = await probeDecodedSize(file, maxEdge);
|
|
50
|
+
const expected = width * height * 4;
|
|
51
|
+
if (rgba.length !== expected) {
|
|
52
|
+
throw new Error(`解码 ${file} 得到 ${rgba.length} 字节,按 ${width}×${height} 应为 ${expected} 字节`);
|
|
53
|
+
}
|
|
54
|
+
return { width, height, rgba };
|
|
55
|
+
}
|
|
56
|
+
/** 解出缩放后的真实尺寸。 */
|
|
57
|
+
async function probeDecodedSize(file, maxEdge) {
|
|
58
|
+
const args = ["-hide_banner", "-loglevel", "error", "-i", file];
|
|
59
|
+
if (maxEdge > 0) {
|
|
60
|
+
args.push("-vf", `scale='if(gt(iw,ih),min(iw,${maxEdge}),-2)':'if(gt(iw,ih),-2,min(ih,${maxEdge}))'`);
|
|
61
|
+
}
|
|
62
|
+
args.push("-frames:v", "1", "-f", "image2pipe", "-vcodec", "png", "-");
|
|
63
|
+
const one = await runFfmpeg(args);
|
|
64
|
+
const size = pngSize(one);
|
|
65
|
+
if (size === undefined)
|
|
66
|
+
throw new Error(`无法确定 ${file} 的尺寸`);
|
|
67
|
+
return size;
|
|
68
|
+
}
|
|
69
|
+
function runFfmpeg(args) {
|
|
70
|
+
return new Promise((resolve, reject) => {
|
|
71
|
+
const child = spawn(ffmpegPath(), args, { stdio: ["ignore", "pipe", "pipe"] });
|
|
72
|
+
const chunks = [];
|
|
73
|
+
const errors = [];
|
|
74
|
+
child.stdout.on("data", (chunk) => chunks.push(chunk));
|
|
75
|
+
child.stderr.on("data", (chunk) => errors.push(chunk));
|
|
76
|
+
child.on("error", (error) => reject(new Error(`无法启动 ffmpeg:${error.message}`)));
|
|
77
|
+
child.on("close", (code) => {
|
|
78
|
+
if (code === 0)
|
|
79
|
+
resolve(Buffer.concat(chunks));
|
|
80
|
+
else
|
|
81
|
+
reject(new Error(`ffmpeg 解码失败(退出码 ${code}):${Buffer.concat(errors).toString("utf8").slice(0, 400)}`));
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
/** 读盘 + 解码。 */
|
|
86
|
+
export async function decodeFile(file, maxEdge = 0) {
|
|
87
|
+
return decodeImage(file, maxEdge);
|
|
88
|
+
}
|
|
89
|
+
/** 编码落盘用的 PNG 字节。 */
|
|
90
|
+
export function encodeBitmap(bitmap) {
|
|
91
|
+
return encodePng(bitmap.rgba, bitmap.width, bitmap.height);
|
|
92
|
+
}
|
|
93
|
+
/** 造一张纯色 RGBA 位图。 */
|
|
94
|
+
export function solidBitmap(width, height, color) {
|
|
95
|
+
const rgba = Buffer.alloc(width * height * 4);
|
|
96
|
+
for (let i = 0; i < width * height; i++) {
|
|
97
|
+
rgba[i * 4] = color[0];
|
|
98
|
+
rgba[i * 4 + 1] = color[1];
|
|
99
|
+
rgba[i * 4 + 2] = color[2];
|
|
100
|
+
rgba[i * 4 + 3] = color[3];
|
|
101
|
+
}
|
|
102
|
+
return { width, height, rgba };
|
|
103
|
+
}
|
|
104
|
+
export function pixelAt(bitmap, x, y) {
|
|
105
|
+
const i = (y * bitmap.width + x) * 4;
|
|
106
|
+
return [bitmap.rgba[i], bitmap.rgba[i + 1], bitmap.rgba[i + 2], bitmap.rgba[i + 3]];
|
|
107
|
+
}
|
|
108
|
+
/** 位图里是否至少有一个透明像素(用来判断「这张图到底带不带 alpha」)。 */
|
|
109
|
+
export function hasAlphaChannel(bitmap, threshold = 24) {
|
|
110
|
+
const { rgba } = bitmap;
|
|
111
|
+
for (let i = 3; i < rgba.length; i += 4) {
|
|
112
|
+
if (rgba[i] < threshold)
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* 画布四边的众数颜色 —— 当作“背景色”。
|
|
119
|
+
*
|
|
120
|
+
* 为什么不用「四角平均」:生成图四角常带一点噪声(本次实测边框色有
|
|
121
|
+
* (254,254,254) / (253,253,253) / (251,253,254) 几种),取众数比取平均稳。
|
|
122
|
+
*/
|
|
123
|
+
export function borderColor(bitmap) {
|
|
124
|
+
const counts = new Map();
|
|
125
|
+
const bump = (x, y) => {
|
|
126
|
+
const i = (y * bitmap.width + x) * 4;
|
|
127
|
+
const key = (bitmap.rgba[i] << 16) | (bitmap.rgba[i + 1] << 8) | bitmap.rgba[i + 2];
|
|
128
|
+
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
129
|
+
};
|
|
130
|
+
for (let x = 0; x < bitmap.width; x++) {
|
|
131
|
+
bump(x, 0);
|
|
132
|
+
bump(x, bitmap.height - 1);
|
|
133
|
+
}
|
|
134
|
+
for (let y = 0; y < bitmap.height; y++) {
|
|
135
|
+
bump(0, y);
|
|
136
|
+
bump(bitmap.width - 1, y);
|
|
137
|
+
}
|
|
138
|
+
let best = 0;
|
|
139
|
+
let bestCount = -1;
|
|
140
|
+
for (const [key, count] of counts) {
|
|
141
|
+
if (count > bestCount) {
|
|
142
|
+
bestCount = count;
|
|
143
|
+
best = key;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return [(best >> 16) & 0xff, (best >> 8) & 0xff, best & 0xff];
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* 从四边向内泛洪,标出「与边框连通的背景区域」。
|
|
150
|
+
*
|
|
151
|
+
* 为什么不用「离背景色远 = 前景」:草地地块的表面颜色和白色背景接近,
|
|
152
|
+
* 那个判据会把整张画布当前景 —— 研究期实测拟合出的斜率变成 1.14(实际 2.0),
|
|
153
|
+
* 而且不报任何错。泛洪只吃「与边框连通」的那一片,内部同色区域不会被误伤。
|
|
154
|
+
*/
|
|
155
|
+
export function backgroundMask(bitmap, tolerance = 22) {
|
|
156
|
+
const { width, height, rgba } = bitmap;
|
|
157
|
+
const bg = borderColor(bitmap);
|
|
158
|
+
const near = new Uint8Array(width * height);
|
|
159
|
+
const limit = tolerance * 3;
|
|
160
|
+
for (let i = 0, p = 0; i < near.length; i++, p += 4) {
|
|
161
|
+
const d = Math.abs(rgba[p] - bg[0]) + Math.abs(rgba[p + 1] - bg[1]) + Math.abs(rgba[p + 2] - bg[2]);
|
|
162
|
+
near[i] = d <= limit ? 1 : 0;
|
|
163
|
+
}
|
|
164
|
+
const seen = new Uint8Array(width * height);
|
|
165
|
+
const queue = new Int32Array(width * height);
|
|
166
|
+
let head = 0;
|
|
167
|
+
let tail = 0;
|
|
168
|
+
const push = (index) => {
|
|
169
|
+
if (near[index] === 1 && seen[index] === 0) {
|
|
170
|
+
seen[index] = 1;
|
|
171
|
+
queue[tail++] = index;
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
for (let x = 0; x < width; x++) {
|
|
175
|
+
push(x);
|
|
176
|
+
push((height - 1) * width + x);
|
|
177
|
+
}
|
|
178
|
+
for (let y = 0; y < height; y++) {
|
|
179
|
+
push(y * width);
|
|
180
|
+
push(y * width + width - 1);
|
|
181
|
+
}
|
|
182
|
+
while (head < tail) {
|
|
183
|
+
const index = queue[head++];
|
|
184
|
+
const x = index % width;
|
|
185
|
+
const y = (index - x) / width;
|
|
186
|
+
if (x > 0)
|
|
187
|
+
push(index - 1);
|
|
188
|
+
if (x < width - 1)
|
|
189
|
+
push(index + 1);
|
|
190
|
+
if (y > 0)
|
|
191
|
+
push(index - width);
|
|
192
|
+
if (y < height - 1)
|
|
193
|
+
push(index + width);
|
|
194
|
+
}
|
|
195
|
+
return seen;
|
|
196
|
+
}
|
|
197
|
+
/** 前景掩码:带 alpha 就信 alpha,否则用泛洪找背景。 */
|
|
198
|
+
export function foregroundMask(bitmap, tolerance = 22) {
|
|
199
|
+
const { width, height, rgba } = bitmap;
|
|
200
|
+
if (hasAlphaChannel(bitmap)) {
|
|
201
|
+
const mask = new Uint8Array(width * height);
|
|
202
|
+
for (let i = 0, p = 3; i < mask.length; i++, p += 4)
|
|
203
|
+
mask[i] = rgba[p] >= 24 ? 1 : 0;
|
|
204
|
+
return mask;
|
|
205
|
+
}
|
|
206
|
+
const bg = backgroundMask(bitmap, tolerance);
|
|
207
|
+
const mask = new Uint8Array(width * height);
|
|
208
|
+
for (let i = 0; i < mask.length; i++)
|
|
209
|
+
mask[i] = bg[i] === 1 ? 0 : 1;
|
|
210
|
+
return mask;
|
|
211
|
+
}
|
|
212
|
+
/** 掩码的紧致包围盒;全空时返回 undefined。 */
|
|
213
|
+
export function maskBounds(mask, width, height) {
|
|
214
|
+
let left = width;
|
|
215
|
+
let top = height;
|
|
216
|
+
let right = -1;
|
|
217
|
+
let bottom = -1;
|
|
218
|
+
for (let y = 0; y < height; y++) {
|
|
219
|
+
const row = y * width;
|
|
220
|
+
for (let x = 0; x < width; x++) {
|
|
221
|
+
if (mask[row + x] === 0)
|
|
222
|
+
continue;
|
|
223
|
+
if (x < left)
|
|
224
|
+
left = x;
|
|
225
|
+
if (x > right)
|
|
226
|
+
right = x;
|
|
227
|
+
if (y < top)
|
|
228
|
+
top = y;
|
|
229
|
+
if (y > bottom)
|
|
230
|
+
bottom = y;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (right < 0)
|
|
234
|
+
return undefined;
|
|
235
|
+
return { left, top, right, bottom };
|
|
236
|
+
}
|
|
237
|
+
/** 掩码里 1 的个数。 */
|
|
238
|
+
export function maskCount(mask) {
|
|
239
|
+
let n = 0;
|
|
240
|
+
for (let i = 0; i < mask.length; i++)
|
|
241
|
+
n += mask[i];
|
|
242
|
+
return n;
|
|
243
|
+
}
|