dsh-game-material-master 0.10.2 → 0.11.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,185 @@
1
+ /**
2
+ * 位图上的极简绘制原语(Agent 检查图 / 网格 / 标记叠加用)。
3
+ *
4
+ * 这里只做「像素画布上画线 / 圆 / 菱形 / 数字」这几件事,不参与地图几何:
5
+ * 坐标由 `maprender` / `isorender` 从宿主计划算好,本文件只负责落像素。
6
+ *
7
+ * 为什么不用 canvas:宿主半区没有 DOM;PNG 导出走的是同一套 RGBA 缓冲区,
8
+ * 预览与导出必须共享同一条路径。数字用内置 3×5 点阵,中文名不画在图上
9
+ * (几十个中文名字常驻会糊成一片,界面悬停看名字即可)。
10
+ */
11
+ function clampByte(value) {
12
+ return Math.max(0, Math.min(255, Math.round(value)));
13
+ }
14
+ function clampAlpha(value) {
15
+ return Math.max(0, Math.min(1, value));
16
+ }
17
+ /** source-over 混合一个像素;越界直接忽略。 */
18
+ export function blendPixel(bitmap, x, y, color, alpha = 1) {
19
+ const px = Math.round(x);
20
+ const py = Math.round(y);
21
+ if (px < 0 || py < 0 || px >= bitmap.width || py >= bitmap.height)
22
+ return;
23
+ const a = clampAlpha(alpha);
24
+ if (a <= 0)
25
+ return;
26
+ const index = (py * bitmap.width + px) * 4;
27
+ const dst = bitmap.rgba;
28
+ const da = dst[index + 3] / 255;
29
+ const outA = a + da * (1 - a);
30
+ if (outA <= 0)
31
+ return;
32
+ const mix = (source, target) => clampByte((source * a + target * da * (1 - a)) / outA);
33
+ dst[index] = mix(color[0], dst[index]);
34
+ dst[index + 1] = mix(color[1], dst[index + 1]);
35
+ dst[index + 2] = mix(color[2], dst[index + 2]);
36
+ dst[index + 3] = clampByte(outA * 255);
37
+ }
38
+ export function fillRect(bitmap, x, y, width, height, color, alpha = 1) {
39
+ const x0 = Math.max(0, Math.floor(x));
40
+ const y0 = Math.max(0, Math.floor(y));
41
+ const x1 = Math.min(bitmap.width, Math.ceil(x + width));
42
+ const y1 = Math.min(bitmap.height, Math.ceil(y + height));
43
+ for (let py = y0; py < y1; py++)
44
+ for (let px = x0; px < x1; px++)
45
+ blendPixel(bitmap, px, py, color, alpha);
46
+ }
47
+ export function strokeRect(bitmap, x, y, width, height, color, alpha = 1, thickness = 1) {
48
+ const t = Math.max(1, Math.round(thickness));
49
+ fillRect(bitmap, x, y, width, t, color, alpha);
50
+ fillRect(bitmap, x, y + height - t, width, t, color, alpha);
51
+ fillRect(bitmap, x, y, t, height, color, alpha);
52
+ fillRect(bitmap, x + width - t, y, t, height, color, alpha);
53
+ }
54
+ /** Bresenham 直线,厚度用方形刷子近似。 */
55
+ export function drawLine(bitmap, x0, y0, x1, y1, color, alpha = 1, thickness = 1) {
56
+ let ax = Math.round(x0);
57
+ let ay = Math.round(y0);
58
+ const bx = Math.round(x1);
59
+ const by = Math.round(y1);
60
+ const dx = Math.abs(bx - ax);
61
+ const dy = Math.abs(by - ay);
62
+ const sx = ax < bx ? 1 : -1;
63
+ const sy = ay < by ? 1 : -1;
64
+ let err = dx - dy;
65
+ const t = Math.max(1, Math.round(thickness));
66
+ const half = Math.floor((t - 1) / 2);
67
+ while (true) {
68
+ fillRect(bitmap, ax - half, ay - half, t, t, color, alpha);
69
+ if (ax === bx && ay === by)
70
+ break;
71
+ const e2 = err * 2;
72
+ if (e2 > -dy) {
73
+ err -= dy;
74
+ ax += sx;
75
+ }
76
+ if (e2 < dx) {
77
+ err += dx;
78
+ ay += sy;
79
+ }
80
+ }
81
+ }
82
+ export function fillCircle(bitmap, cx, cy, radius, color, alpha = 1) {
83
+ const r = Math.max(0, radius);
84
+ const x0 = Math.floor(cx - r);
85
+ const x1 = Math.ceil(cx + r);
86
+ const y0 = Math.floor(cy - r);
87
+ const y1 = Math.ceil(cy + r);
88
+ for (let y = y0; y <= y1; y++) {
89
+ for (let x = x0; x <= x1; x++) {
90
+ const dx = x - cx;
91
+ const dy = y - cy;
92
+ if (dx * dx + dy * dy <= r * r)
93
+ blendPixel(bitmap, x, y, color, alpha);
94
+ }
95
+ }
96
+ }
97
+ export function strokeCircle(bitmap, cx, cy, radius, color, alpha = 1, thickness = 1) {
98
+ const r = Math.max(0, radius);
99
+ const inner = Math.max(0, r - Math.max(1, thickness));
100
+ const x0 = Math.floor(cx - r);
101
+ const x1 = Math.ceil(cx + r);
102
+ const y0 = Math.floor(cy - r);
103
+ const y1 = Math.ceil(cy + r);
104
+ for (let y = y0; y <= y1; y++) {
105
+ for (let x = x0; x <= x1; x++) {
106
+ const dx = x - cx;
107
+ const dy = y - cy;
108
+ const d2 = dx * dx + dy * dy;
109
+ if (d2 <= r * r && d2 >= inner * inner)
110
+ blendPixel(bitmap, x, y, color, alpha);
111
+ }
112
+ }
113
+ }
114
+ /** 2:1 菱形(等距地格 / 地图 cell 轮廓)。 */
115
+ export function fillDiamond(bitmap, cx, cy, rx, ry, color, alpha = 1) {
116
+ if (rx <= 0 || ry <= 0)
117
+ return;
118
+ for (let dy = -ry; dy <= ry; dy++) {
119
+ const half = rx * (1 - Math.abs(dy) / ry);
120
+ const x0 = Math.round(cx - half);
121
+ const x1 = Math.round(cx + half);
122
+ for (let x = x0; x <= x1; x++)
123
+ blendPixel(bitmap, x, cy + dy, color, alpha);
124
+ }
125
+ }
126
+ export function strokeDiamond(bitmap, cx, cy, rx, ry, color, alpha = 1, thickness = 1) {
127
+ drawLine(bitmap, cx - rx, cy, cx, cy - ry, color, alpha, thickness);
128
+ drawLine(bitmap, cx, cy - ry, cx + rx, cy, color, alpha, thickness);
129
+ drawLine(bitmap, cx + rx, cy, cx, cy + ry, color, alpha, thickness);
130
+ drawLine(bitmap, cx, cy + ry, cx - rx, cy, color, alpha, thickness);
131
+ }
132
+ /** 3×5 点阵数字;只画 0-9,其他字符留空。 */
133
+ const DIGITS = {
134
+ "0": ["111", "101", "101", "101", "111"],
135
+ "1": ["010", "110", "010", "010", "111"],
136
+ "2": ["111", "001", "111", "100", "111"],
137
+ "3": ["111", "001", "111", "001", "111"],
138
+ "4": ["101", "101", "111", "001", "001"],
139
+ "5": ["111", "100", "111", "001", "111"],
140
+ "6": ["111", "100", "111", "101", "111"],
141
+ "7": ["111", "001", "010", "010", "010"],
142
+ "8": ["111", "101", "111", "101", "111"],
143
+ "9": ["111", "101", "111", "001", "111"]
144
+ };
145
+ /**
146
+ * 以 (cx,cy) 为中心画一串数字。`height` 是字高(像素),宽度按 3:5 推。
147
+ * 返回实际绘制宽度,方便调用方排版。
148
+ */
149
+ export function drawDigits(bitmap, text, cx, cy, height, color, alpha = 1) {
150
+ const chars = String(text).split("").filter((char) => DIGITS[char] !== undefined);
151
+ if (chars.length === 0)
152
+ return 0;
153
+ const scale = Math.max(1, Math.floor(Math.max(1, height) / 5));
154
+ const glyphWidth = 3 * scale;
155
+ const gap = scale;
156
+ const total = chars.length * glyphWidth + (chars.length - 1) * gap;
157
+ let x = Math.round(cx - total / 2);
158
+ const y = Math.round(cy - (5 * scale) / 2);
159
+ for (const char of chars) {
160
+ const rows = DIGITS[char];
161
+ for (let row = 0; row < 5; row++) {
162
+ for (let col = 0; col < 3; col++) {
163
+ if (rows[row][col] === "1")
164
+ fillRect(bitmap, x + col * scale, y + row * scale, scale, scale, color, alpha);
165
+ }
166
+ }
167
+ x += glyphWidth + gap;
168
+ }
169
+ return total;
170
+ }
171
+ /** 棋盘格背景(透明图在聊天里不容易看清;检查图默认铺一层很淡的格)。 */
172
+ export function checkerBackground(bitmap, size = 8, light = [244, 244, 244], dark = [224, 224, 224]) {
173
+ const cell = Math.max(1, Math.round(size));
174
+ for (let y = 0; y < bitmap.height; y++) {
175
+ const rowDark = Math.floor(y / cell) % 2 === 1;
176
+ for (let x = 0; x < bitmap.width; x++) {
177
+ const color = (Math.floor(x / cell) % 2 === 1) !== rowDark ? light : dark;
178
+ const index = (y * bitmap.width + x) * 4;
179
+ bitmap.rgba[index] = color[0];
180
+ bitmap.rgba[index + 1] = color[1];
181
+ bitmap.rgba[index + 2] = color[2];
182
+ bitmap.rgba[index + 3] = 255;
183
+ }
184
+ }
185
+ }
package/lib/index.js CHANGED
@@ -10,8 +10,8 @@ import { assertVideoBackendReady, videoBackendOf } from "./videogen.js";
10
10
  * 直接发给浏览器——否则界面每帧都要靠 RPC 传 base64,又慢又费内存。
11
11
  */
12
12
  import { createReadStream, readdirSync, statSync } from "node:fs";
13
- import { mkdir, stat, writeFile } from "node:fs/promises";
14
- import { basename, dirname, extname, join } from "node:path";
13
+ import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
14
+ import { basename, dirname, extname, join, resolve } from "node:path";
15
15
  import { fileURLToPath } from "node:url";
16
16
  import { spawn } from "node:child_process";
17
17
  import { TypertRemoteService } from "@deepseek-ai/dsh-typert-protocol";
@@ -200,6 +200,19 @@ function safeFileName(input) {
200
200
  const base = basename(input).replace(/[^\w.\-()\u4e00-\u9fa5]+/g, "_");
201
201
  return base === "" ? "source" : base.slice(0, 120);
202
202
  }
203
+ /** 工具面路径导入:宿主读盘、按文件头嗅探,返回 base64(供 import* 复用)。 */
204
+ async function readImageBase64FromPath(path) {
205
+ const absolute = resolve(path);
206
+ const info = await stat(absolute);
207
+ if (!info.isFile())
208
+ throw new Error(`不是文件:${absolute}`);
209
+ if (info.size > 30 * 1024 * 1024)
210
+ throw new Error(`素材图 ${(info.size / 1024 / 1024).toFixed(1)}MB 超过 30MB 上限:${absolute}`);
211
+ const base64 = (await readFile(absolute)).toString("base64");
212
+ if (sniffImage(base64) === undefined)
213
+ throw new Error(`无法识别的图片格式(支持 PNG / JPEG / WebP / GIF / BMP):${absolute}`);
214
+ return base64;
215
+ }
203
216
  export class GameStudioGateway extends TypertRemoteService {
204
217
  ffmpegCache;
205
218
  constructor(ctx) {
@@ -946,7 +959,10 @@ export class GameStudioGateway extends TypertRemoteService {
946
959
  }
947
960
  async importIsoTileset(payload) {
948
961
  const input = asRecord(payload);
949
- const result = await isogen.importIsoTileset(asString(input.projectId), asString(input.name), asString(input.data), {
962
+ const data = asString(input.path) !== "" ? await readImageBase64FromPath(asString(input.path)) : asString(input.data);
963
+ if (data === "")
964
+ throw new Error("importIsoTileset 需要 data(base64)或 path(本机绝对路径)。");
965
+ const result = await isogen.importIsoTileset(asString(input.projectId), asString(input.name), data, {
950
966
  kind: input.kind,
951
967
  sourceCell: input.sourceCell,
952
968
  animRows: input.animRows,
@@ -1013,7 +1029,9 @@ export class GameStudioGateway extends TypertRemoteService {
1013
1029
  const input = asRecord(payload);
1014
1030
  return isogen.applyIsoOps(asString(input.projectId), Array.isArray(input.ops) ? input.ops : [], {
1015
1031
  stroke: typeof input.stroke === "string" ? input.stroke : undefined,
1016
- have: Array.isArray(input.have) ? input.have : undefined
1032
+ have: Array.isArray(input.have) ? input.have : undefined,
1033
+ dryRun: input.dryRun === true,
1034
+ ifRevision: typeof input.ifRevision === "string" ? input.ifRevision : undefined
1017
1035
  });
1018
1036
  }
1019
1037
  async isoUndo(payload) {
@@ -1034,6 +1052,30 @@ export class GameStudioGateway extends TypertRemoteService {
1034
1052
  pass: input.pass === true
1035
1053
  });
1036
1054
  }
1055
+ async isoInspect(payload) {
1056
+ const input = asRecord(payload);
1057
+ return isogen.isoInspect(asString(input.projectId), {
1058
+ region: input.region,
1059
+ layerIds: Array.isArray(input.layerIds) ? input.layerIds.map((entry) => String(entry)) : undefined,
1060
+ include: Array.isArray(input.include) ? input.include.map((entry) => String(entry)) : undefined,
1061
+ format: input.format === "text" || input.format === "json" || input.format === "both" ? input.format : undefined,
1062
+ maxCells: typeof input.maxCells === "number" ? input.maxCells : undefined,
1063
+ maxItems: typeof input.maxItems === "number" ? input.maxItems : undefined
1064
+ });
1065
+ }
1066
+ async isoRender(payload) {
1067
+ const input = asRecord(payload);
1068
+ return isogen.runIsoRender(asString(input.projectId), {
1069
+ region: input.region,
1070
+ scale: typeof input.scale === "number" ? input.scale : undefined,
1071
+ layerIds: Array.isArray(input.layerIds) ? input.layerIds.map((entry) => String(entry)) : undefined,
1072
+ frame: typeof input.frame === "number" ? input.frame : undefined,
1073
+ background: input.background,
1074
+ overlay: input.overlay,
1075
+ maxPixels: typeof input.maxPixels === "number" ? input.maxPixels : undefined,
1076
+ name: typeof input.name === "string" ? input.name : undefined
1077
+ });
1078
+ }
1037
1079
  async runIsoGenerate(payload) {
1038
1080
  const input = asRecord(payload);
1039
1081
  const kind = input.kind === "maze" || input.kind === "continent" ? input.kind : "random";
@@ -1130,7 +1172,10 @@ export class GameStudioGateway extends TypertRemoteService {
1130
1172
  }
1131
1173
  async importMapTileset(payload) {
1132
1174
  const input = asRecord(payload);
1133
- const result = await mapgen.importMapTileset(asString(input.projectId), asString(input.name), asString(input.data), {
1175
+ const data = asString(input.path) !== "" ? await readImageBase64FromPath(asString(input.path)) : asString(input.data);
1176
+ if (data === "")
1177
+ throw new Error("importMapTileset 需要 data(base64)或 path(本机绝对路径)。");
1178
+ const result = await mapgen.importMapTileset(asString(input.projectId), asString(input.name), data, {
1134
1179
  cell: input.cell,
1135
1180
  shape: input.shape,
1136
1181
  animRows: input.animRows,
@@ -1196,7 +1241,9 @@ export class GameStudioGateway extends TypertRemoteService {
1196
1241
  animRows: typeof input.animRows === "boolean" ? input.animRows : undefined,
1197
1242
  // `null` = 清掉自己的速度(跟全局);`undefined` = 这次不动它。
1198
1243
  fps: input.fps === undefined ? undefined : input.fps === null ? null : Number(input.fps),
1199
- loop: input.loop
1244
+ loop: input.loop,
1245
+ // 仅整图:逐格通行区域(长度 / 取值由 mapgen 校验,这里只透传)。
1246
+ stampPass: Array.isArray(input.stampPass) ? input.stampPass : undefined
1200
1247
  });
1201
1248
  return { tileset: mapTilesetBrief(result.tileset), warnings: result.warnings };
1202
1249
  }
@@ -1232,7 +1279,9 @@ export class GameStudioGateway extends TypertRemoteService {
1232
1279
  const input = asRecord(payload);
1233
1280
  return mapgen.applyMapOps(asString(input.projectId), Array.isArray(input.ops) ? input.ops : [], {
1234
1281
  stroke: typeof input.stroke === "string" && input.stroke !== "" ? input.stroke : undefined,
1235
- have: mapHave(input.have)
1282
+ have: mapHave(input.have),
1283
+ dryRun: input.dryRun === true,
1284
+ ifRevision: typeof input.ifRevision === "string" ? input.ifRevision : undefined
1236
1285
  });
1237
1286
  }
1238
1287
  async mapUndo(payload) {
@@ -1254,6 +1303,30 @@ export class GameStudioGateway extends TypertRemoteService {
1254
1303
  pass: input.pass === true
1255
1304
  });
1256
1305
  }
1306
+ async mapInspect(payload) {
1307
+ const input = asRecord(payload);
1308
+ return mapgen.mapInspect(asString(input.projectId), {
1309
+ region: input.region,
1310
+ layerIds: Array.isArray(input.layerIds) ? input.layerIds.map((entry) => String(entry)) : undefined,
1311
+ include: Array.isArray(input.include) ? input.include.map((entry) => String(entry)) : undefined,
1312
+ format: input.format === "text" || input.format === "json" || input.format === "both" ? input.format : undefined,
1313
+ maxCells: typeof input.maxCells === "number" ? input.maxCells : undefined,
1314
+ maxItems: typeof input.maxItems === "number" ? input.maxItems : undefined
1315
+ });
1316
+ }
1317
+ async mapRender(payload) {
1318
+ const input = asRecord(payload);
1319
+ return mapgen.runMapRender(asString(input.projectId), {
1320
+ region: input.region,
1321
+ scale: typeof input.scale === "number" ? input.scale : undefined,
1322
+ layerIds: Array.isArray(input.layerIds) ? input.layerIds.map((entry) => String(entry)) : undefined,
1323
+ frame: typeof input.frame === "number" ? input.frame : undefined,
1324
+ background: input.background,
1325
+ overlay: input.overlay,
1326
+ maxPixels: typeof input.maxPixels === "number" ? input.maxPixels : undefined,
1327
+ name: typeof input.name === "string" ? input.name : undefined
1328
+ });
1329
+ }
1257
1330
  /** 一键生成:整层一次提交(一次撤销就能回去),同种子逐字节可复现。 */
1258
1331
  async runMapGenerate(payload) {
1259
1332
  const input = asRecord(payload);
package/lib/isodoc.js CHANGED
@@ -147,7 +147,20 @@ function normalizeMarkers(raw, cols, rows) {
147
147
  const given = typeof marker?.id === "string" && marker.id !== "" ? marker.id : "";
148
148
  const id = given !== "" && !used.has(given) ? given : nextId();
149
149
  used.add(id);
150
- out.push({ id, r, c, name: typeof marker?.name === "string" ? marker.name : "" });
150
+ const kind = typeof marker?.kind === "string" && marker.kind.trim() !== "" ? marker.kind.trim() : undefined;
151
+ const color = typeof marker?.color === "string" && /^#[0-9a-f]{6}$/i.test(marker.color.trim()) ? marker.color.trim() : undefined;
152
+ const note = typeof marker?.note === "string" && marker.note !== "" ? marker.note : undefined;
153
+ const tags = Array.isArray(marker?.tags) ? marker.tags.filter((tag) => typeof tag === "string" && tag !== "") : undefined;
154
+ out.push({
155
+ id,
156
+ r,
157
+ c,
158
+ name: typeof marker?.name === "string" ? marker.name : "",
159
+ ...(kind === undefined ? {} : { kind }),
160
+ ...(color === undefined ? {} : { color }),
161
+ ...(note === undefined ? {} : { note }),
162
+ ...(tags === undefined || tags.length === 0 ? {} : { tags })
163
+ });
151
164
  }
152
165
  return out;
153
166
  }
@@ -435,6 +448,41 @@ export function applyIsoOps(doc, ops, layout, options = {}) {
435
448
  redo.push({ kind: "object", op: "move", id: object.id, r: op.r, c: op.c });
436
449
  }
437
450
  };
451
+ const applyObjectDuplicate = (op, recordUndo) => {
452
+ const object = doc.objects.find((entry) => entry.id === op.id);
453
+ if (object === undefined) {
454
+ skipped.push(`没有这个物件:${op.id}`);
455
+ return;
456
+ }
457
+ const fp = footprintOf(object.tilesetId) ?? { cols: 1, rows: 1 };
458
+ if (!footprintInside(layout, op.r, op.c, fp)) {
459
+ skipped.push("物件放不下:占格超出了地图");
460
+ return;
461
+ }
462
+ if (doc.objects.length >= MAX_ISO_OBJECTS) {
463
+ skipped.push(`物件太多了(上限 ${MAX_ISO_OBJECTS} 个)`);
464
+ return;
465
+ }
466
+ const occupied = objectOccupied(doc, layout, footprintOf);
467
+ for (const cell of footprintCells(op.r, op.c, fp)) {
468
+ const wrapped = wrapPoint(layout, cell.r, cell.c);
469
+ if (wrapped === undefined)
470
+ continue;
471
+ if (occupied.has(`${object.layerId}|${wrapped.r},${wrapped.c}`)) {
472
+ skipped.push("目标位置已经有物件了");
473
+ return;
474
+ }
475
+ }
476
+ let id = typeof op.newId === "string" && op.newId !== "" && !doc.objects.some((entry) => entry.id === op.newId) ? op.newId : makeObjectId(doc);
477
+ doc.objects.push({ id, tilesetId: object.tilesetId, layerId: object.layerId, r: op.r, c: op.c });
478
+ for (const cell of footprintCells(op.r, op.c, fp))
479
+ markDirty(changedCells, cell.r, cell.c);
480
+ changed++;
481
+ if (recordUndo) {
482
+ undo.push({ kind: "object", op: "remove", id });
483
+ redo.push({ kind: "object", op: "add", layerId: object.layerId, tilesetId: object.tilesetId, r: op.r, c: op.c, id });
484
+ }
485
+ };
438
486
  const applyObjectRemove = (op, recordUndo) => {
439
487
  const index = doc.objects.findIndex((entry) => entry.id === op.id);
440
488
  if (index < 0) {
@@ -561,6 +609,16 @@ export function applyIsoOps(doc, ops, layout, options = {}) {
561
609
  else if (action === "move") {
562
610
  applyObjectMove({ kind: "object", op: "move", id: String(op.id ?? ""), r: Math.round(Number(op.r)), c: Math.round(Number(op.c)) }, true);
563
611
  }
612
+ else if (action === "duplicate") {
613
+ applyObjectDuplicate({
614
+ kind: "object",
615
+ op: "duplicate",
616
+ id: String(op.id ?? ""),
617
+ r: Math.round(Number(op.r)),
618
+ c: Math.round(Number(op.c)),
619
+ newId: typeof op.newId === "string" ? op.newId : undefined
620
+ }, true);
621
+ }
564
622
  else if (action === "remove") {
565
623
  applyObjectRemove({ kind: "object", op: "remove", id: String(op.id ?? "") }, true);
566
624
  }
@@ -571,6 +629,19 @@ export function applyIsoOps(doc, ops, layout, options = {}) {
571
629
  }
572
630
  if (kind === "marker") {
573
631
  const action = op.op;
632
+ const meta = (input, includeKind = true) => {
633
+ const rawKind = input.markerKind !== undefined ? input.markerKind : includeKind ? input.kind : undefined;
634
+ const kindValue = typeof rawKind === "string" && rawKind.trim() !== "" ? rawKind.trim() : undefined;
635
+ const color = typeof input.color === "string" && /^#[0-9a-f]{6}$/i.test(input.color.trim()) ? input.color.trim() : undefined;
636
+ const note = typeof input.note === "string" && input.note !== "" ? input.note : undefined;
637
+ const tags = Array.isArray(input.tags) ? input.tags.filter((tag) => typeof tag === "string" && tag !== "") : undefined;
638
+ return {
639
+ ...(kindValue === undefined ? {} : { kind: kindValue }),
640
+ ...(color === undefined ? {} : { color }),
641
+ ...(note === undefined ? {} : { note }),
642
+ ...(tags === undefined || tags.length === 0 ? {} : { tags })
643
+ };
644
+ };
574
645
  if (action === "add") {
575
646
  const hit = wrapPoint(layout, Math.round(Number(op.r)), Math.round(Number(op.c)));
576
647
  if (hit === undefined) {
@@ -583,12 +654,24 @@ export function applyIsoOps(doc, ops, layout, options = {}) {
583
654
  }
584
655
  const id = typeof op.id === "string" && op.id !== "" && !doc.markers.some((marker) => marker.id === op.id) ? op.id : makeMarkerId(doc);
585
656
  const name = typeof op.name === "string" ? op.name : "";
586
- doc.markers.push({ id, r: hit.r, c: hit.c, name });
657
+ doc.markers.push({ id, r: hit.r, c: hit.c, name, ...meta(op, false) });
587
658
  markDirty(changedCells, hit.r, hit.c);
588
659
  changed++;
589
660
  touched = true;
590
661
  undo.push({ kind: "marker", op: "remove", id });
591
- redo.push({ kind: "marker", op: "add", r: hit.r, c: hit.c, name, id });
662
+ const added = meta(op, false);
663
+ redo.push({
664
+ kind: "marker",
665
+ op: "add",
666
+ r: hit.r,
667
+ c: hit.c,
668
+ name,
669
+ id,
670
+ ...(added.kind === undefined ? {} : { markerKind: added.kind }),
671
+ ...(added.color === undefined ? {} : { color: added.color }),
672
+ ...(added.note === undefined ? {} : { note: added.note }),
673
+ ...(added.tags === undefined ? {} : { tags: added.tags })
674
+ });
592
675
  continue;
593
676
  }
594
677
  if (action === "remove") {
@@ -604,7 +687,19 @@ export function applyIsoOps(doc, ops, layout, options = {}) {
604
687
  markDirty(changedCells, marker.r, marker.c);
605
688
  changed++;
606
689
  touched = true;
607
- undo.push({ kind: "marker", op: "add", r: marker.r, c: marker.c, name: marker.name, id: marker.id });
690
+ const saved = meta(marker);
691
+ undo.push({
692
+ kind: "marker",
693
+ op: "add",
694
+ r: marker.r,
695
+ c: marker.c,
696
+ name: marker.name,
697
+ id: marker.id,
698
+ ...(saved.kind === undefined ? {} : { markerKind: saved.kind }),
699
+ ...(saved.color === undefined ? {} : { color: saved.color }),
700
+ ...(saved.note === undefined ? {} : { note: saved.note }),
701
+ ...(saved.tags === undefined ? {} : { tags: saved.tags })
702
+ });
608
703
  redo.push({ kind: "marker", op: "remove", id: marker.id });
609
704
  continue;
610
705
  }
@@ -634,6 +729,60 @@ export function applyIsoOps(doc, ops, layout, options = {}) {
634
729
  redo.push({ kind: "marker", op: "move", id: marker.id, r: hit.r, c: hit.c });
635
730
  continue;
636
731
  }
732
+ if (action === "update") {
733
+ const marker = doc.markers.find((entry) => entry.id === op.id);
734
+ if (marker === undefined) {
735
+ skipped.push(`没有这个标记:${String(op.id)}`);
736
+ continue;
737
+ }
738
+ const updateOp = op;
739
+ const patch = updateOp.patch ?? {};
740
+ const before = { ...marker };
741
+ if (typeof patch.name === "string")
742
+ marker.name = patch.name;
743
+ if (patch.kind !== undefined) {
744
+ if (patch.kind.trim() === "")
745
+ delete marker.kind;
746
+ else
747
+ marker.kind = patch.kind.trim();
748
+ }
749
+ if (patch.color !== undefined) {
750
+ if (patch.color.trim() === "")
751
+ delete marker.color;
752
+ else if (/^#[0-9a-f]{6}$/i.test(patch.color.trim()))
753
+ marker.color = patch.color.trim();
754
+ }
755
+ if (patch.note !== undefined) {
756
+ if (patch.note === "")
757
+ delete marker.note;
758
+ else
759
+ marker.note = patch.note;
760
+ }
761
+ if (patch.tags !== undefined) {
762
+ const tags = patch.tags.filter((tag) => typeof tag === "string" && tag !== "");
763
+ if (tags.length === 0)
764
+ delete marker.tags;
765
+ else
766
+ marker.tags = tags;
767
+ }
768
+ if (JSON.stringify(before) === JSON.stringify(marker))
769
+ continue;
770
+ touched = true;
771
+ undo.push({
772
+ kind: "marker",
773
+ op: "update",
774
+ id: marker.id,
775
+ patch: {
776
+ name: before.name,
777
+ kind: before.kind ?? "",
778
+ color: before.color ?? "",
779
+ note: before.note ?? "",
780
+ tags: before.tags ?? []
781
+ }
782
+ });
783
+ redo.push({ kind: "marker", op: "update", id: marker.id, patch });
784
+ continue;
785
+ }
637
786
  if (action === "rename") {
638
787
  const marker = doc.markers.find((entry) => entry.id === op.id);
639
788
  if (marker === undefined) {
package/lib/isoexport.js CHANGED
@@ -16,11 +16,14 @@ import { buildIsoPlan, isoBitmapToPng, passCellsOf, planBounds, rasterizeIso } f
16
16
  import { safeFileName } from "./mapexport.js";
17
17
  import { zipEntries } from "./zip.js";
18
18
  export const OWN_ISO_JSON_FORMAT = "dsh-gem-iso-map@1";
19
+ /** 带标记语义字段时使用;没有新字段的老文档仍然写 @1。 */
20
+ export const OWN_ISO_JSON_FORMAT_META = "dsh-gem-iso-map@2";
19
21
  /** `dsh-gem-iso-map@1` 的内容。 */
20
22
  export function buildIsoJson(context, doc, project) {
21
23
  const legend = buildIsoPlan(context, doc, { includeLegend: true }).legend;
24
+ const hasMarkerMeta = doc.markers.some((marker) => marker.kind !== undefined || marker.color !== undefined || marker.note !== undefined || marker.tags !== undefined);
22
25
  return {
23
- format: OWN_ISO_JSON_FORMAT,
26
+ format: hasMarkerMeta ? OWN_ISO_JSON_FORMAT_META : OWN_ISO_JSON_FORMAT,
24
27
  projection: "isometric-2:1",
25
28
  cell: { width: context.cell.width, height: context.cell.height },
26
29
  grid: { stepX: context.layout.stepX, stepY: context.layout.stepY, wrap: context.layout.wrap },
@@ -48,7 +51,16 @@ export function buildIsoJson(context, doc, project) {
48
51
  .filter((layer) => layer.kind === "ground")
49
52
  .map((layer) => ({ layerId: layer.id, data: doc.data[layer.id] ?? [] })),
50
53
  objects: doc.objects.map((object) => ({ id: object.id, layerId: object.layerId, tilesetId: object.tilesetId, r: object.r, c: object.c })),
51
- markers: doc.markers.map((marker, index) => ({ n: index + 1, name: marker.name, r: marker.r, c: marker.c })),
54
+ markers: doc.markers.map((marker, index) => ({
55
+ n: index + 1,
56
+ name: marker.name,
57
+ r: marker.r,
58
+ c: marker.c,
59
+ ...(marker.kind === undefined ? {} : { kind: marker.kind }),
60
+ ...(marker.color === undefined ? {} : { color: marker.color }),
61
+ ...(marker.note === undefined ? {} : { note: marker.note }),
62
+ ...(marker.tags === undefined ? {} : { tags: marker.tags })
63
+ })),
52
64
  pass: passCellsOf(context, doc)
53
65
  };
54
66
  }