dsh-game-material-master 0.7.0 → 0.9.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/lib/links.js CHANGED
@@ -14,8 +14,10 @@
14
14
  * 连 <a> 都不生成,模型写了也点不动。
15
15
  *
16
16
  * 为什么带 origin:
17
- * 浏览器半区在 apply 时把自己真实的 `location.origin` 报给宿主(reportClientOrigin),
18
- * 宿主据此拼出可点链接。宿主自己并不知道对外 origin(可能被反代改写过)。
17
+ * 浏览器半区在 apply 时把自己真实的 `location.origin` 与 `location.pathname`
18
+ * 报给宿主(reportClientOrigin),宿主据此拼出可点链接。宿主自己并不知道对外
19
+ * origin(可能被反代改写过);DSH 0.2.1 的 `--public-url` 还允许带路径前缀,
20
+ * 所以 `pathname` 也要一起报。
19
21
  * 没收到过上报时退化成 http://localhost —— 即使 origin 不对也没关系:
20
22
  * 浏览器半区装了一个捕获阶段的点击拦截器,只按 **路径与查询参数** 匹配,
21
23
  * 命中就 preventDefault 并原地切面板,根本不会发生跳转。
@@ -28,10 +30,14 @@
28
30
  export const OPEN_QUERY_KEY = "dsh-gmm";
29
31
  /** 工作台在 `main` 槽里的 key,也是侧栏 `sidebar.panellist` 的 id。 */
30
32
  export const PANEL_KEY = "gameStudio";
31
- /** 六个模块的 key。 */
32
- export const STUDIO_MODULES = ["sprite", "image", "sequence", "rig", "tile", "map"];
33
+ /** 八个模块的 key。 */
34
+ export const STUDIO_MODULES = ["sprite", "image", "sequence", "rig", "tile", "map", "music", "sfx"];
33
35
  /** 八方向图的四个阶段。 */
34
36
  export const SPRITE_STAGES = ["images", "videos", "frames", "sheet"];
37
+ /** 音乐生成编辑(模块七)的三个阶段:生成 / 编辑 / 导出。 */
38
+ export const MUSIC_STAGES = ["compose", "generate", "edit", "export"];
39
+ /** 音效生成(模块八)的两个阶段:生成 / 导出。 */
40
+ export const SFX_STAGES = ["generate", "export"];
35
41
  /**
36
42
  * 地图地块的四个阶段。
37
43
  *
@@ -49,9 +55,62 @@ export const TILE_STAGES = ["generate", "review", "map", "export"];
49
55
  * `stage=assets|rules|paint|export` 契约不能变。
50
56
  */
51
57
  export const MAP_STAGES = ["assets", "rules", "paint", "export"];
58
+ /** 深链接里 `scale` 允许的取值范围(与 `maptiles.parseAssetScale` 同一套)。 */
59
+ export const OPEN_SCALE_MIN = 1;
60
+ export const OPEN_SCALE_MAX = 8;
61
+ /**
62
+ * 深链接里整图占格的取值范围(与 `maptiles.STAMP_MIN_CELLS / STAMP_MAX_CELLS` 同一套)。
63
+ * 真正的业务上限是**地图大小**,那条由宿主按项目校验;这里只挡明显离谱的值。
64
+ */
65
+ export const OPEN_STAMP_MIN = 2;
66
+ export const OPEN_STAMP_MAX = 512;
67
+ /** 深链接里 `dialog` 允许的取值(别的值一律丢掉,别把链接当任意指令用)。 */
68
+ export const OPEN_DIALOGS = ["add"];
69
+ /** 深链接里 `shape` 允许的取值(与 `maptiles.TilesetShape` 同一套)。 */
70
+ export const OPEN_SHAPES = ["plain", "corner16", "a2quad", "stamp"];
71
+ /**
72
+ * `asset` 必须是**项目资源目录下的相对路径**:不接受绝对路径、不接受 `..`
73
+ * (资源路由另有目录穿越拦截,这里先把脏值挡在意图之外,免得把坏链接带进界面)。
74
+ */
75
+ export function isSafeAssetPath(value) {
76
+ if (typeof value !== "string")
77
+ return false;
78
+ const trimmed = value.trim();
79
+ if (trimmed === "" || trimmed.startsWith("/") || trimmed.startsWith("\\"))
80
+ return false;
81
+ if (trimmed.includes("..") || trimmed.includes("\\"))
82
+ return false;
83
+ return true;
84
+ }
52
85
  let clientOrigin = "";
53
- /** 浏览器半区上报真实 origin。只接受 http(s),其它一律忽略。 */
54
- export function rememberClientOrigin(origin) {
86
+ /**
87
+ * 应用挂在哪个路径前缀下(DSH 0.2.1 的 `--public-url https://host/prefix/`)。
88
+ *
89
+ * 反代把 `/prefix/*` 剥掉再转给 DSH,所以浏览器看到的地址是
90
+ * `https://host/prefix/`,而宿主只知道根路径。界面挂载时把自己的
91
+ * `location.pathname` 一起报上来,宿主拼深链接与产物 URL 时补回去。
92
+ * 空串 = 挂在根路径(本地默认)。
93
+ */
94
+ let clientBasePath = "";
95
+ /** 把 `location.pathname` 收敛成「不带尾部斜杠、也不带 /index.html」的前缀。 */
96
+ function normalizeBasePath(value) {
97
+ if (typeof value !== "string")
98
+ return "";
99
+ const raw = value.trim();
100
+ if (raw === "" || raw === "/")
101
+ return "";
102
+ if (!raw.startsWith("/") || raw.includes("\\") || raw.includes(".."))
103
+ return "";
104
+ const path = raw.replace(/\/index\.html?$/iu, "").replace(/\/+$/u, "");
105
+ return path === "" || path === "/" ? "" : path;
106
+ }
107
+ /**
108
+ * 浏览器半区上报真实 origin(可选带应用路径前缀)。只接受 http(s),其它一律忽略。
109
+ *
110
+ * `basePath` 省略时退回 `origin` 自带的路径:传 `https://host/prefix` 与
111
+ * `{ origin: "https://host", basePath: "/prefix" }` 等价。
112
+ */
113
+ export function rememberClientOrigin(origin, basePath) {
55
114
  if (typeof origin !== "string")
56
115
  return false;
57
116
  let parsed;
@@ -64,12 +123,27 @@ export function rememberClientOrigin(origin) {
64
123
  if (parsed.protocol !== "http:" && parsed.protocol !== "https:")
65
124
  return false;
66
125
  clientOrigin = parsed.origin;
126
+ clientBasePath = normalizeBasePath(basePath === undefined ? parsed.pathname : basePath);
67
127
  return true;
68
128
  }
69
129
  /** 已上报的 origin;没有就返回空串。 */
70
130
  export function clientOriginOf() {
71
131
  return clientOrigin;
72
132
  }
133
+ /** 已上报的应用路径前缀(`""` = 根路径)。 */
134
+ export function appBasePath() {
135
+ return clientBasePath;
136
+ }
137
+ /**
138
+ * 把宿主里的根路径(`/dsh-game-material-master/...`)补上应用前缀。
139
+ *
140
+ * 所有回给浏览器的 `assetBase` 都走这里:反代只把 `/prefix/*` 转给 DSH,
141
+ * 少了前缀就会 404(界面表现为「图裂了」)。
142
+ */
143
+ export function assetBasePath(path) {
144
+ const normalized = path.startsWith("/") ? path : `/${path}`;
145
+ return `${clientBasePath}${normalized}`;
146
+ }
73
147
  /**
74
148
  * 拼链接用的 origin。
75
149
  *
@@ -93,8 +167,27 @@ export function normalizeIntent(intent) {
93
167
  out.jobId = intent.jobId;
94
168
  if (typeof intent.stage === "string" && intent.stage !== "")
95
169
  out.stage = intent.stage;
170
+ if (typeof intent.scoreId === "string" && intent.scoreId !== "")
171
+ out.scoreId = intent.scoreId;
96
172
  if (typeof intent.direction === "string" && intent.direction !== "")
97
173
  out.direction = intent.direction;
174
+ if (typeof intent.dialog === "string" && OPEN_DIALOGS.includes(intent.dialog))
175
+ out.dialog = intent.dialog;
176
+ if (isSafeAssetPath(intent.asset))
177
+ out.asset = intent.asset.trim();
178
+ if (typeof intent.frames === "number" && Number.isInteger(intent.frames) && intent.frames > 0)
179
+ out.frames = intent.frames;
180
+ if (typeof intent.shape === "string" && OPEN_SHAPES.includes(intent.shape))
181
+ out.shape = intent.shape;
182
+ if (typeof intent.scale === "number" && Number.isInteger(intent.scale) && intent.scale >= OPEN_SCALE_MIN && intent.scale <= OPEN_SCALE_MAX)
183
+ out.scale = intent.scale;
184
+ for (const key of ["cols", "rows"]) {
185
+ const value = intent[key];
186
+ if (typeof value === "number" && Number.isInteger(value) && value >= OPEN_STAMP_MIN && value <= OPEN_STAMP_MAX)
187
+ out[key] = value;
188
+ }
189
+ if (intent.keyed === 1)
190
+ out.keyed = 1;
98
191
  return out;
99
192
  }
100
193
  /** 意图 → 查询串(不含 `?`)。 */
@@ -102,15 +195,39 @@ export function intentQuery(intent) {
102
195
  const normalized = normalizeIntent(intent);
103
196
  const params = new URLSearchParams();
104
197
  params.set(OPEN_QUERY_KEY, "1");
105
- for (const key of ["module", "project", "job", "stage", "direction"]) {
106
- const value = key === "project" ? normalized.projectId : key === "job" ? normalized.jobId : normalized[key];
198
+ for (const key of ["module", "project", "job", "stage", "score", "direction", "dialog", "asset", "frames", "shape", "scale", "cols", "rows", "keyed"]) {
199
+ const value = key === "project"
200
+ ? normalized.projectId
201
+ : key === "job"
202
+ ? normalized.jobId
203
+ : key === "score"
204
+ ? normalized.scoreId
205
+ : key === "frames"
206
+ ? normalized.frames === undefined
207
+ ? undefined
208
+ : String(normalized.frames)
209
+ : key === "scale"
210
+ ? normalized.scale === undefined
211
+ ? undefined
212
+ : String(normalized.scale)
213
+ : key === "cols" || key === "rows" || key === "keyed"
214
+ ? normalized[key] === undefined
215
+ ? undefined
216
+ : String(normalized[key])
217
+ : normalized[key];
107
218
  if (value !== undefined)
108
219
  params.set(key, value);
109
220
  }
110
221
  return params.toString();
111
222
  }
112
- /** 拼出可点击的绝对链接。 */
113
- export function buildOpenLink(intent, origin = originForLinks()) {
223
+ /**
224
+ * 拼出可点击的绝对链接。
225
+ *
226
+ * 默认用 `origin + 应用前缀`:DSH 0.2.1 的 `--public-url` 允许带路径前缀,
227
+ * 只拼 origin 会让回链落到反代根路径而不是应用本身。显式传 `origin` 的
228
+ * 调用点(自检 / 测试)保持原语义,不会额外补前缀。
229
+ */
230
+ export function buildOpenLink(intent, origin = `${originForLinks()}${clientBasePath}`) {
114
231
  return `${origin}/?${intentQuery(intent)}`;
115
232
  }
116
233
  /**
@@ -135,8 +252,33 @@ export function parseIntentQuery(query) {
135
252
  const stage = params.get("stage");
136
253
  if (stage !== null)
137
254
  intent.stage = stage;
255
+ const scoreId = params.get("score");
256
+ if (scoreId !== null)
257
+ intent.scoreId = scoreId;
138
258
  const direction = params.get("direction");
139
259
  if (direction !== null)
140
260
  intent.direction = direction;
261
+ const dialog = params.get("dialog");
262
+ if (dialog !== null)
263
+ intent.dialog = dialog;
264
+ const asset = params.get("asset");
265
+ if (asset !== null)
266
+ intent.asset = asset;
267
+ const frames = Number(params.get("frames"));
268
+ if (params.get("frames") !== null && Number.isFinite(frames))
269
+ intent.frames = frames;
270
+ const shape = params.get("shape");
271
+ if (shape !== null)
272
+ intent.shape = shape;
273
+ const scale = Number(params.get("scale"));
274
+ if (params.get("scale") !== null && Number.isFinite(scale))
275
+ intent.scale = scale;
276
+ for (const key of ["cols", "rows"]) {
277
+ const value = Number(params.get(key));
278
+ if (params.get(key) !== null && Number.isFinite(value))
279
+ intent[key] = value;
280
+ }
281
+ if (params.get("keyed") === "1")
282
+ intent.keyed = 1;
141
283
  return normalizeIntent(intent);
142
284
  }
package/lib/mapdoc.js CHANGED
@@ -22,6 +22,11 @@
22
22
  * 却写在文档里、还会让 op 静默整批跳过。AsLib 的做法就是「只有名字的叠放顺序」。
23
23
  */
24
24
  import { DEFAULT_GRID, MAX_MAP_DIM, assertMapSize, layoutOf, wrapCell } from "./mapgeom.js";
25
+ /** 整图旋转后占几格(顺时针 90° 的长宽互换)—— 与 `maptiles.stampSpan` 同一口径。 */
26
+ function stampSpanOf(spec, rot) {
27
+ const turn = ((Math.round(rot) % 4) + 4) % 4;
28
+ return turn % 2 === 1 ? { cols: spec.rows, rows: spec.cols } : { cols: spec.cols, rows: spec.rows };
29
+ }
25
30
  export function cellIndex(doc, r, c) {
26
31
  return r * doc.cols + c;
27
32
  }
@@ -49,7 +54,7 @@ export function createMapDoc(id, name, cols, rows) {
49
54
  const data = {};
50
55
  for (const layer of layers)
51
56
  data[layer.id] = emptyData(cols, rows);
52
- return { id, name, cols, rows, layers, data, markers: [], createdAt: now, updatedAt: now };
57
+ return { id, name, cols, rows, layers, data, markers: [], stamps: [], createdAt: now, updatedAt: now };
53
58
  }
54
59
  /** 读盘 / 外部输入 → 合法文档:补缺字段、把图层数据裁齐到 `cols × rows`。 */
55
60
  export function normalizeMapDoc(raw, fallbackId) {
@@ -115,6 +120,7 @@ export function normalizeMapDoc(raw, fallbackId) {
115
120
  layers,
116
121
  data,
117
122
  markers: normalizeMarkers(input.markers, cols, rows),
123
+ stamps: normalizeStamps(input.stamps, cols, rows),
118
124
  createdAt: typeof input.createdAt === "string" ? input.createdAt : now,
119
125
  updatedAt: typeof input.updatedAt === "string" ? input.updatedAt : now
120
126
  };
@@ -154,6 +160,46 @@ function normalizeMarkers(raw, cols, rows) {
154
160
  }
155
161
  return out;
156
162
  }
163
+ /**
164
+ * 读盘 / 外部输入 → 合法整图表。
165
+ *
166
+ * - 缺字段(老文档 @3)→ 空表,不抛;
167
+ * - **越界的丢掉**(地图被改小过):留着的话它有一半格子在地图外面,导出坐标也对不上;
168
+ * - 引用了不存在的素材 / 图层 → 也丢掉(素材被删过)——这一步在 `mapgen` 拿到 project 之后
169
+ * 还会再核一次(`pruneStamps`),这里只保证结构与坐标合法。
170
+ */
171
+ function normalizeStamps(raw, cols, rows) {
172
+ if (!Array.isArray(raw))
173
+ return [];
174
+ const used = new Set();
175
+ const out = [];
176
+ let seq = 0;
177
+ const nextId = () => {
178
+ seq++;
179
+ while (used.has(`s${seq}`))
180
+ seq++;
181
+ return `s${seq}`;
182
+ };
183
+ for (const item of raw) {
184
+ const entry = item;
185
+ const r = Math.round(Number(entry?.r));
186
+ const c = Math.round(Number(entry?.c));
187
+ const rot = Math.round(Number(entry?.rot));
188
+ if (!Number.isFinite(r) || !Number.isFinite(c))
189
+ continue;
190
+ if (r < 0 || c < 0 || r >= rows || c >= cols)
191
+ continue;
192
+ const tilesetId = typeof entry?.tilesetId === "string" ? entry.tilesetId : "";
193
+ const layerId = typeof entry?.layerId === "string" ? entry.layerId : "";
194
+ if (tilesetId === "" || layerId === "")
195
+ continue;
196
+ const given = typeof entry?.id === "string" && entry.id !== "" ? entry.id : "";
197
+ const id = given !== "" && !used.has(given) ? given : nextId();
198
+ used.add(id);
199
+ out.push({ id, tilesetId, layerId, r, c, rot: Number.isFinite(rot) ? ((rot % 4) + 4) % 4 : 0 });
200
+ }
201
+ return out;
202
+ }
157
203
  export function cloneDoc(doc) {
158
204
  const data = {};
159
205
  for (const [key, value] of Object.entries(doc.data))
@@ -161,6 +207,7 @@ export function cloneDoc(doc) {
161
207
  return {
162
208
  ...doc,
163
209
  layers: doc.layers.map((layer) => ({ ...layer })),
210
+ stamps: doc.stamps.map((stamp) => ({ ...stamp })),
164
211
  data,
165
212
  markers: doc.markers.map((marker) => ({ ...marker }))
166
213
  };
@@ -304,23 +351,15 @@ function tileValue(input) {
304
351
  const value = Number(input);
305
352
  return Number.isFinite(value) && value > 0 ? Math.round(value) : 0;
306
353
  }
307
- /**
308
- * 应用一批 op。
309
- *
310
- * **不会抛异常**:拖动时指针滑出地图是常事,越界条目记进 `skipped` 由界面提示。
311
- * 坐标一律不取整(不合法就是 `越界格`)—— `Math.round` 会把用户根本没点到的格子
312
- * 悄悄涂上,这正是旧模块「涂在隔壁格」那类问题的来源。
313
- *
314
- * `layout` 是**双层保险**:上游(`mapPlan` / 客户端命中测试)已经给过规范坐标,
315
- * 这里再按 `layout` wrap 一次。`layout.wrap` 为 false 时越界就是越界,直接跳过。
316
- */
317
- export function applyOps(doc, ops, layout) {
354
+ export function applyOps(doc, ops, layout, options = {}) {
318
355
  const changed = new Set();
319
356
  const dirty = new Map();
320
357
  const undo = [];
321
358
  const skipped = [];
322
359
  /** 有不占格的改动(标记改名)。它不进 `changed`,但文档真的变了。 */
323
360
  let touched = false;
361
+ /** 动到的整图张数(新增 + 删除)。 */
362
+ let stampsTouched = 0;
324
363
  const canonical = (r, c) => {
325
364
  if (!Number.isInteger(r) || !Number.isInteger(c))
326
365
  return undefined;
@@ -368,6 +407,46 @@ export function applyOps(doc, ops, layout) {
368
407
  markCell(marker.r, marker.c);
369
408
  }
370
409
  };
410
+ /** 整图占的格子(规范坐标的索引)。规格缺失时返回空表 —— 那张素材多半被删了。 */
411
+ const stampCellIndices = (stamp) => {
412
+ const spec = options.stampSpecOf?.(stamp.tilesetId);
413
+ if (spec === undefined)
414
+ return [];
415
+ const span = stampSpanOf(spec, stamp.rot);
416
+ const out = [];
417
+ for (let dr = 0; dr < span.rows; dr++) {
418
+ for (let dc = 0; dc < span.cols; dc++) {
419
+ const cell = canonical(stamp.r + dr, stamp.c + dc);
420
+ if (cell === undefined)
421
+ continue;
422
+ out.push(cell.r * doc.cols + cell.c);
423
+ }
424
+ }
425
+ return out;
426
+ };
427
+ /**
428
+ * 落笔前把压在这些格子上的整图摘掉(同一图层)。
429
+ *
430
+ * 为什么连「值没变」也要摘:橡皮擦一格时那一格的图块值本来就是 0,`paintValue` 里
431
+ * `previous === tile` 会直接跳过 —— 不在这里先摘的话,用户对着雕像按橡皮什么都不发生。
432
+ */
433
+ const dropStampsAt = (layerId, indices) => {
434
+ if (doc.stamps.length === 0 || indices.size === 0)
435
+ return;
436
+ for (let i = doc.stamps.length - 1; i >= 0; i--) {
437
+ const stamp = doc.stamps[i];
438
+ if (stamp.layerId !== layerId)
439
+ continue;
440
+ const cells = stampCellIndices(stamp);
441
+ if (!cells.some((index) => indices.has(index)))
442
+ continue;
443
+ doc.stamps.splice(i, 1);
444
+ undo.push({ kind: "stampPlace", layerId: stamp.layerId, tilesetId: stamp.tilesetId, r: stamp.r, c: stamp.c, rot: stamp.rot, id: stamp.id });
445
+ for (const index of cells)
446
+ markCell(Math.floor(index / doc.cols), index % doc.cols);
447
+ stampsTouched++;
448
+ }
449
+ };
371
450
  for (const raw of ops) {
372
451
  const op = raw;
373
452
  if (op === null || typeof op !== "object" || typeof op.kind !== "string") {
@@ -378,6 +457,74 @@ export function applyOps(doc, ops, layout) {
378
457
  touched = applyMarkerOp(doc, op, { canonical, markCell, markNumberingFrom }, skipped, undo) || touched;
379
458
  continue;
380
459
  }
460
+ if (op.kind === "stampRemove") {
461
+ const at = typeof op.id === "string" ? doc.stamps.findIndex((stamp) => stamp.id === op.id) : -1;
462
+ if (at < 0) {
463
+ skipped.push(`没有这张整图:${String(op.id)}`);
464
+ continue;
465
+ }
466
+ const stamp = doc.stamps[at];
467
+ const cells = stampCellIndices(stamp);
468
+ doc.stamps.splice(at, 1);
469
+ undo.push({ kind: "stampPlace", layerId: stamp.layerId, tilesetId: stamp.tilesetId, r: stamp.r, c: stamp.c, rot: stamp.rot, id: stamp.id });
470
+ for (const index of cells)
471
+ markCell(Math.floor(index / doc.cols), index % doc.cols);
472
+ stampsTouched++;
473
+ continue;
474
+ }
475
+ if (op.kind === "stampPlace") {
476
+ if (typeof op.layerId !== "string" || doc.data[op.layerId] === undefined) {
477
+ skipped.push(`图层不存在:${String(op.layerId)}`);
478
+ continue;
479
+ }
480
+ const spec = options.stampSpecOf?.(String(op.tilesetId));
481
+ if (spec === undefined) {
482
+ skipped.push(`整图素材不存在或不是整图:${String(op.tilesetId)}`);
483
+ continue;
484
+ }
485
+ const start = canonical(op.r, op.c);
486
+ if (start === undefined) {
487
+ skipped.push(`越界格 (${op.r},${op.c})`);
488
+ continue;
489
+ }
490
+ const span = stampSpanOf(spec, op.rot);
491
+ if (span.cols > doc.cols || span.rows > doc.rows) {
492
+ skipped.push(`整图 ${span.cols}×${span.rows} 格比地图还大`);
493
+ continue;
494
+ }
495
+ // 整图的每一格都得落在地图内(不环绕时 wrapCell 会原样返回,这里逐格核)。
496
+ const cells = [];
497
+ let outside = false;
498
+ for (let dr = 0; dr < span.rows && !outside; dr++) {
499
+ for (let dc = 0; dc < span.cols && !outside; dc++) {
500
+ const cell = canonical(start.r + dr, start.c + dc);
501
+ if (cell === undefined)
502
+ outside = true;
503
+ else
504
+ cells.push(cell.r * doc.cols + cell.c);
505
+ }
506
+ }
507
+ if (outside) {
508
+ skipped.push(`整图放不下(参考点 ${start.r},${start.c} 处会越界)`);
509
+ continue;
510
+ }
511
+ // 同层已有整图压在这些格子上:替换掉(与「后画的在上」一致)。
512
+ dropStampsAt(op.layerId, new Set(cells));
513
+ const used = new Set(doc.stamps.map((stamp) => stamp.id));
514
+ let id = typeof op.id === "string" && op.id !== "" ? op.id : "";
515
+ if (id === "" || used.has(id)) {
516
+ let seq = doc.stamps.length + 1;
517
+ while (used.has(`s${seq}`))
518
+ seq++;
519
+ id = `s${seq}`;
520
+ }
521
+ doc.stamps.push({ id, tilesetId: String(op.tilesetId), layerId: op.layerId, r: start.r, c: start.c, rot: ((Math.round(Number(op.rot)) % 4) + 4) % 4 });
522
+ undo.push({ kind: "stampRemove", id });
523
+ for (const index of cells)
524
+ markCell(Math.floor(index / doc.cols), index % doc.cols);
525
+ stampsTouched++;
526
+ continue;
527
+ }
381
528
  if (typeof op.layerId !== "string" || doc.data[op.layerId] === undefined) {
382
529
  skipped.push(`图层不存在:${String(op.layerId)}`);
383
530
  continue;
@@ -387,6 +534,10 @@ export function applyOps(doc, ops, layout) {
387
534
  skipped.push("paint 缺 cells");
388
535
  continue;
389
536
  }
537
+ dropStampsAt(op.layerId, new Set(op.cells.map((cell) => {
538
+ const target = cell === null || typeof cell !== "object" ? undefined : canonical(cell.r, cell.c);
539
+ return target === undefined ? -1 : target.r * doc.cols + target.c;
540
+ }).filter((index) => index >= 0)));
390
541
  const before = paintValue(op.layerId, op.cells);
391
542
  if (before.length > 0)
392
543
  undo.push({ kind: "paint", layerId: op.layerId, cells: before });
@@ -420,6 +571,10 @@ export function applyOps(doc, ops, layout) {
420
571
  cells.push({ r: cell.r, c: cell.c, tile });
421
572
  stack.push({ r: cell.r + 1, c: cell.c }, { r: cell.r - 1, c: cell.c }, { r: cell.r, c: cell.c + 1 }, { r: cell.r, c: cell.c - 1 });
422
573
  }
574
+ dropStampsAt(op.layerId, new Set(cells.map((cell) => {
575
+ const target = canonical(cell.r, cell.c);
576
+ return target === undefined ? -1 : target.r * doc.cols + target.c;
577
+ }).filter((index) => index >= 0)));
423
578
  const before = paintValue(op.layerId, cells);
424
579
  if (before.length > 0)
425
580
  undo.push({ kind: "paint", layerId: op.layerId, cells: before });
@@ -444,6 +599,10 @@ export function applyOps(doc, ops, layout) {
444
599
  cells.push({ r, c, tile });
445
600
  }
446
601
  }
602
+ dropStampsAt(op.layerId, new Set(cells.map((cell) => {
603
+ const target = canonical(cell.r, cell.c);
604
+ return target === undefined ? -1 : target.r * doc.cols + target.c;
605
+ }).filter((index) => index >= 0)));
447
606
  const before = paintValue(op.layerId, cells);
448
607
  if (before.length > 0)
449
608
  undo.push({ kind: "paint", layerId: op.layerId, cells: before });
@@ -458,6 +617,10 @@ export function applyOps(doc, ops, layout) {
458
617
  continue;
459
618
  cells.push({ r: Math.floor(index / doc.cols), c: index % doc.cols, tile: to });
460
619
  }
620
+ dropStampsAt(op.layerId, new Set(cells.map((cell) => {
621
+ const target = canonical(cell.r, cell.c);
622
+ return target === undefined ? -1 : target.r * doc.cols + target.c;
623
+ }).filter((index) => index >= 0)));
461
624
  const before = paintValue(op.layerId, cells);
462
625
  if (before.length > 0)
463
626
  undo.push({ kind: "paint", layerId: op.layerId, cells: before });
@@ -471,6 +634,7 @@ export function applyOps(doc, ops, layout) {
471
634
  return {
472
635
  changed: changed.size,
473
636
  touched,
637
+ stamps: stampsTouched,
474
638
  skipped,
475
639
  dirty: [...dirty.values()].sort((a, b) => a.r - b.r || a.c - b.c),
476
640
  undo: undo.reverse()
@@ -570,9 +734,9 @@ export function nextMarkerId(doc) {
570
734
  * 契约里没有 `layout` 参数 —— 逆操作记下来的坐标本来就是**规范坐标**,这里补一个
571
735
  * 只为了能复用 `applyOps`;wrap 对规范坐标是恒等变换,所以用哪个网格都不影响结果。
572
736
  */
573
- export function revertOps(doc, undo) {
737
+ export function revertOps(doc, undo, options = {}) {
574
738
  const layout = layoutOf(DEFAULT_GRID, doc.cols, doc.rows);
575
- return applyOps(doc, undo, layout).dirty.map((cell) => cell.r * doc.cols + cell.c);
739
+ return applyOps(doc, undo, layout, options).dirty.map((cell) => cell.r * doc.cols + cell.c);
576
740
  }
577
741
  /** 用到的图块序号(删图集 / 改切分前查引用)。 */
578
742
  export function usedTileIndices(doc) {
package/lib/mapexport.js CHANGED
@@ -38,14 +38,16 @@ import { bitmapToPng, planBounds, rasterize } from "./maprender.js";
38
38
  // ⚠️ `mapgen` 反过来也 import 本文件(`runExportStage` 调 `exportMap`)。ESM 的循环
39
39
  // 引用对「只在函数体里用到的绑定」是安全的(下面只在调用时读 `mapAssetPath`)。
40
40
  import { mapAssetPath } from "./mapgen.js";
41
+ import { stampCellsOf } from "./maprender.js";
41
42
  import { zipEntries } from "./zip.js";
42
43
  /**
43
44
  * 自有 JSON 的格式标识。
44
45
  *
45
46
  * - `@2`:单项目单地图(去掉 `project` 段)。
46
47
  * - `@3`:顶层多了 `markers`(标记点:编号 + 名字 + 格坐标)。
48
+ * - `@4`:顶层多了 `stamps`(整图:占 N×M 格的多格印花 + 每格通行性)。
47
49
  */
48
- export const OWN_JSON_FORMAT = "dsh-gem-map@3";
50
+ export const OWN_JSON_FORMAT = "dsh-gem-map@4";
49
51
  /** 文件名安全化:名字直接进目录会踩到 `/`、`:` 与超长名。 */
50
52
  export function safeFileName(input, fallback = "map") {
51
53
  const cleaned = (input ?? "")
@@ -69,6 +71,17 @@ export function safeFileName(input, fallback = "map") {
69
71
  * `markers` 是**源数据**(位置 + 名字),不是画出来的圆圈:`map.png` 里没有标记,
70
72
  * 引擎按坐标自己渲染。编号 `n` 写出来是为了让人一眼对得上界面上的数字,
71
73
  * 它等于数组下标 + 1(删掉一个,后面自动往前顶)。
74
+ *
75
+ * `stamps` 是整图的**实例**(参考点 + 旋转)与**占到的每一格**(`r/c` + `pass`):
76
+ *
77
+ * | `pass` | 含义 | 引擎怎么用 |
78
+ * |---|---|---|
79
+ * | `0` | 不可通过 | 挡路(墙脚、烛台、底座) |
80
+ * | `1` | 可通过 · 被人物遮挡 | 能走,**角色画在它上面**(地毯、地面装饰) |
81
+ * | `2` | 可通过 · 遮挡人物 | 能走,**它画在角色上面**(雕像上身,绕后) |
82
+ *
83
+ * 这些值是**源数据**(导入时逐格点的),不是从图里推出来的 —— 所以引擎不用自己做
84
+ * 任何像素分析就能拿到寻路与遮挡关系。
72
85
  */
73
86
  export function buildOwnJson(context, doc, project) {
74
87
  void project;
@@ -88,7 +101,22 @@ export function buildOwnJson(context, doc, project) {
88
101
  })),
89
102
  legend: flatTiles(context.tilesets).map((flat) => ({ index: flat.index, tilesetId: flat.tilesetId, rect: flat.tile.rect })),
90
103
  layers: doc.layers.map((layer) => ({ name: layer.name, data: [...(doc.data[layer.id] ?? [])] })),
91
- markers: doc.markers.map((marker, index) => ({ n: index + 1, name: marker.name, r: marker.r, c: marker.c }))
104
+ markers: doc.markers.map((marker, index) => ({ n: index + 1, name: marker.name, r: marker.r, c: marker.c })),
105
+ stamps: doc.stamps.map((stamp) => {
106
+ const tileset = context.tilesets.find((entry) => entry.id === stamp.tilesetId);
107
+ const spec = tileset?.stamp;
108
+ return {
109
+ id: stamp.id,
110
+ tilesetId: stamp.tilesetId,
111
+ name: tileset?.name ?? stamp.tilesetId,
112
+ layerId: stamp.layerId,
113
+ r: stamp.r,
114
+ c: stamp.c,
115
+ rot: stamp.rot,
116
+ // 占格与通行性由宿主现算(取模落到地图内、旋转取转前那一格)。
117
+ cells: stampCellsOf(context, doc, stamp)
118
+ };
119
+ })
92
120
  };
93
121
  }
94
122
  /** 解出导出用的图集位图。解不出来的留给 `rasterize` 报警(不能静默缺图)。 */