sci2-ts 0.0.0-stage → 0.2.1

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.
Files changed (357) hide show
  1. package/CHANGELOG.md +72 -0
  2. package/LICENSE +21 -0
  3. package/README.md +113 -2
  4. package/apps/viewer/editor.html +43 -0
  5. package/apps/viewer/explore.html +27 -0
  6. package/apps/viewer/index.html +26 -0
  7. package/apps/viewer/package.json +27 -0
  8. package/apps/viewer/play.html +26 -0
  9. package/apps/viewer/plugin.d.ts +18 -0
  10. package/apps/viewer/plugin.js +75 -0
  11. package/apps/viewer/plugin.ts +84 -0
  12. package/apps/viewer/src/adlib.d.ts +20 -0
  13. package/apps/viewer/src/adlib.js +55 -0
  14. package/apps/viewer/src/adlib.ts +62 -0
  15. package/apps/viewer/src/app.d.ts +5 -0
  16. package/apps/viewer/src/app.js +5 -0
  17. package/apps/viewer/src/app.ts +5 -0
  18. package/apps/viewer/src/asset-index.d.ts +22 -0
  19. package/apps/viewer/src/asset-index.js +74 -0
  20. package/apps/viewer/src/asset-index.ts +90 -0
  21. package/apps/viewer/src/audio.d.ts +28 -0
  22. package/apps/viewer/src/audio.js +102 -0
  23. package/apps/viewer/src/audio.ts +110 -0
  24. package/apps/viewer/src/editor-handles.d.ts +63 -0
  25. package/apps/viewer/src/editor-handles.js +332 -0
  26. package/apps/viewer/src/editor-handles.ts +368 -0
  27. package/apps/viewer/src/editor-languages.d.ts +23 -0
  28. package/apps/viewer/src/editor-languages.js +157 -0
  29. package/apps/viewer/src/editor-languages.ts +135 -0
  30. package/apps/viewer/src/editor.css +213 -0
  31. package/apps/viewer/src/editor.d.ts +1 -0
  32. package/apps/viewer/src/editor.js +393 -0
  33. package/apps/viewer/src/editor.ts +412 -0
  34. package/apps/viewer/src/explore/classes.d.ts +7 -0
  35. package/apps/viewer/src/explore/classes.js +63 -0
  36. package/apps/viewer/src/explore/classes.ts +61 -0
  37. package/apps/viewer/src/explore/context.d.ts +33 -0
  38. package/apps/viewer/src/explore/context.js +96 -0
  39. package/apps/viewer/src/explore/context.ts +124 -0
  40. package/apps/viewer/src/explore/explore.css +93 -0
  41. package/apps/viewer/src/explore/explore.d.ts +1 -0
  42. package/apps/viewer/src/explore/explore.js +102 -0
  43. package/apps/viewer/src/explore/explore.ts +104 -0
  44. package/apps/viewer/src/explore/inspect.d.ts +5 -0
  45. package/apps/viewer/src/explore/inspect.js +208 -0
  46. package/apps/viewer/src/explore/inspect.ts +279 -0
  47. package/apps/viewer/src/explore/live.d.ts +22 -0
  48. package/apps/viewer/src/explore/live.js +318 -0
  49. package/apps/viewer/src/explore/live.ts +365 -0
  50. package/apps/viewer/src/explore/map.d.ts +11 -0
  51. package/apps/viewer/src/explore/map.js +182 -0
  52. package/apps/viewer/src/explore/map.ts +193 -0
  53. package/apps/viewer/src/files.d.ts +21 -0
  54. package/apps/viewer/src/files.js +69 -0
  55. package/apps/viewer/src/files.ts +72 -0
  56. package/apps/viewer/src/game-profile.d.ts +19 -0
  57. package/apps/viewer/src/game-profile.js +4 -0
  58. package/apps/viewer/src/game-profile.ts +26 -0
  59. package/apps/viewer/src/main.d.ts +1 -0
  60. package/apps/viewer/src/main.js +473 -0
  61. package/apps/viewer/src/main.ts +527 -0
  62. package/apps/viewer/src/music.d.ts +14 -0
  63. package/apps/viewer/src/music.js +32 -0
  64. package/apps/viewer/src/music.ts +34 -0
  65. package/apps/viewer/src/opl-worklet.d.ts +1 -0
  66. package/apps/viewer/src/opl-worklet.js +47 -0
  67. package/apps/viewer/src/opl-worklet.ts +55 -0
  68. package/apps/viewer/src/overlay.d.ts +31 -0
  69. package/apps/viewer/src/overlay.js +103 -0
  70. package/apps/viewer/src/overlay.ts +123 -0
  71. package/apps/viewer/src/play.css +14 -0
  72. package/apps/viewer/src/play.d.ts +1 -0
  73. package/apps/viewer/src/play.js +179 -0
  74. package/apps/viewer/src/play.ts +177 -0
  75. package/apps/viewer/src/saves.d.ts +10 -0
  76. package/apps/viewer/src/saves.js +50 -0
  77. package/apps/viewer/src/saves.ts +51 -0
  78. package/apps/viewer/src/session.d.ts +91 -0
  79. package/apps/viewer/src/session.js +301 -0
  80. package/apps/viewer/src/session.ts +321 -0
  81. package/apps/viewer/src/sound-preview.d.ts +7 -0
  82. package/apps/viewer/src/sound-preview.js +214 -0
  83. package/apps/viewer/src/sound-preview.ts +228 -0
  84. package/apps/viewer/src/style.css +64 -0
  85. package/apps/viewer/vite.config.ts +25 -0
  86. package/bin/sci-ts.mjs +9 -0
  87. package/lib/994.sc +49 -0
  88. package/lib/995.sc +53 -0
  89. package/lib/996.sc +299 -0
  90. package/lib/997.sc +198 -0
  91. package/lib/998.sc +338 -0
  92. package/lib/999.sc +189 -0
  93. package/lib/resources.d.ts +2 -0
  94. package/lib/resources.js +84 -0
  95. package/lib/resources.ts +88 -0
  96. package/lib/system.sh +62 -0
  97. package/package.json +75 -4
  98. package/packages/content/room.schema.json +267 -0
  99. package/packages/content/src/index.d.ts +3 -0
  100. package/packages/content/src/index.js +3 -0
  101. package/packages/content/src/index.ts +3 -0
  102. package/packages/content/src/room.d.ts +111 -0
  103. package/packages/content/src/room.js +886 -0
  104. package/packages/content/src/room.ts +1011 -0
  105. package/packages/content/src/target.d.ts +80 -0
  106. package/packages/content/src/target.js +1 -0
  107. package/packages/content/src/target.ts +60 -0
  108. package/packages/content/src/yarn.d.ts +90 -0
  109. package/packages/content/src/yarn.js +214 -0
  110. package/packages/content/src/yarn.ts +261 -0
  111. package/packages/sci/src/analysis/game.d.ts +90 -0
  112. package/packages/sci/src/analysis/game.js +221 -0
  113. package/packages/sci/src/analysis/game.ts +305 -0
  114. package/packages/sci/src/analysis/runtime.d.ts +58 -0
  115. package/packages/sci/src/analysis/runtime.js +133 -0
  116. package/packages/sci/src/analysis/runtime.ts +163 -0
  117. package/packages/sci/src/audio/adlib.d.ts +73 -0
  118. package/packages/sci/src/audio/adlib.js +280 -0
  119. package/packages/sci/src/audio/adlib.ts +329 -0
  120. package/packages/sci/src/audio/index.d.ts +43 -0
  121. package/packages/sci/src/audio/index.js +99 -0
  122. package/packages/sci/src/audio/index.ts +122 -0
  123. package/packages/sci/src/audio/midi.d.ts +92 -0
  124. package/packages/sci/src/audio/midi.js +242 -0
  125. package/packages/sci/src/audio/midi.ts +262 -0
  126. package/packages/sci/src/audio/opl2.d.ts +74 -0
  127. package/packages/sci/src/audio/opl2.js +283 -0
  128. package/packages/sci/src/audio/opl2.ts +283 -0
  129. package/packages/sci/src/audio/sol.d.ts +32 -0
  130. package/packages/sci/src/audio/sol.js +81 -0
  131. package/packages/sci/src/audio/sol.ts +100 -0
  132. package/packages/sci/src/audio/write.d.ts +35 -0
  133. package/packages/sci/src/audio/write.js +91 -0
  134. package/packages/sci/src/audio/write.ts +108 -0
  135. package/packages/sci/src/gfx/cel.d.ts +36 -0
  136. package/packages/sci/src/gfx/cel.js +82 -0
  137. package/packages/sci/src/gfx/cel.ts +110 -0
  138. package/packages/sci/src/gfx/palette.d.ts +24 -0
  139. package/packages/sci/src/gfx/palette.js +49 -0
  140. package/packages/sci/src/gfx/palette.ts +56 -0
  141. package/packages/sci/src/gfx/pic-file.d.ts +46 -0
  142. package/packages/sci/src/gfx/pic-file.js +114 -0
  143. package/packages/sci/src/gfx/pic-file.ts +155 -0
  144. package/packages/sci/src/gfx/pic.d.ts +27 -0
  145. package/packages/sci/src/gfx/pic.js +34 -0
  146. package/packages/sci/src/gfx/pic.ts +53 -0
  147. package/packages/sci/src/gfx/quantize.d.ts +42 -0
  148. package/packages/sci/src/gfx/quantize.js +199 -0
  149. package/packages/sci/src/gfx/quantize.ts +218 -0
  150. package/packages/sci/src/gfx/view-file.d.ts +62 -0
  151. package/packages/sci/src/gfx/view-file.js +182 -0
  152. package/packages/sci/src/gfx/view-file.ts +233 -0
  153. package/packages/sci/src/gfx/view.d.ts +28 -0
  154. package/packages/sci/src/gfx/view.js +45 -0
  155. package/packages/sci/src/gfx/view.ts +62 -0
  156. package/packages/sci/src/index.d.ts +38 -0
  157. package/packages/sci/src/index.js +38 -0
  158. package/packages/sci/src/index.ts +38 -0
  159. package/packages/sci/src/motion/pathfinding.d.ts +40 -0
  160. package/packages/sci/src/motion/pathfinding.js +140 -0
  161. package/packages/sci/src/motion/pathfinding.ts +147 -0
  162. package/packages/sci/src/resource/archive.d.ts +16 -0
  163. package/packages/sci/src/resource/archive.js +51 -0
  164. package/packages/sci/src/resource/archive.ts +56 -0
  165. package/packages/sci/src/resource/lzs.d.ts +12 -0
  166. package/packages/sci/src/resource/lzs.js +64 -0
  167. package/packages/sci/src/resource/lzs.ts +66 -0
  168. package/packages/sci/src/resource/manager.d.ts +45 -0
  169. package/packages/sci/src/resource/manager.js +158 -0
  170. package/packages/sci/src/resource/manager.ts +184 -0
  171. package/packages/sci/src/resource/map.d.ts +18 -0
  172. package/packages/sci/src/resource/map.js +34 -0
  173. package/packages/sci/src/resource/map.ts +44 -0
  174. package/packages/sci/src/resource/types.d.ts +37 -0
  175. package/packages/sci/src/resource/types.js +45 -0
  176. package/packages/sci/src/resource/types.ts +57 -0
  177. package/packages/sci/src/script/assembly.d.ts +67 -0
  178. package/packages/sci/src/script/assembly.js +748 -0
  179. package/packages/sci/src/script/assembly.ts +714 -0
  180. package/packages/sci/src/script/compiler.d.ts +75 -0
  181. package/packages/sci/src/script/compiler.js +993 -0
  182. package/packages/sci/src/script/compiler.ts +1040 -0
  183. package/packages/sci/src/script/disasm.d.ts +81 -0
  184. package/packages/sci/src/script/disasm.js +318 -0
  185. package/packages/sci/src/script/disasm.ts +323 -0
  186. package/packages/sci/src/script/kernel-names.d.ts +6 -0
  187. package/packages/sci/src/script/kernel-names.js +165 -0
  188. package/packages/sci/src/script/kernel-names.ts +165 -0
  189. package/packages/sci/src/script/opcodes.d.ts +22 -0
  190. package/packages/sci/src/script/opcodes.js +45 -0
  191. package/packages/sci/src/script/opcodes.ts +72 -0
  192. package/packages/sci/src/script/script.d.ts +56 -0
  193. package/packages/sci/src/script/script.js +63 -0
  194. package/packages/sci/src/script/script.ts +111 -0
  195. package/packages/sci/src/script/sexpr.d.ts +45 -0
  196. package/packages/sci/src/script/sexpr.js +151 -0
  197. package/packages/sci/src/script/sexpr.ts +139 -0
  198. package/packages/sci/src/script/vocab.d.ts +15 -0
  199. package/packages/sci/src/script/vocab.js +44 -0
  200. package/packages/sci/src/script/vocab.ts +48 -0
  201. package/packages/sci/src/script/world.d.ts +25 -0
  202. package/packages/sci/src/script/world.js +65 -0
  203. package/packages/sci/src/script/world.ts +70 -0
  204. package/packages/sci/src/text/font.d.ts +37 -0
  205. package/packages/sci/src/text/font.js +103 -0
  206. package/packages/sci/src/text/font.ts +125 -0
  207. package/packages/sci/src/text/message.d.ts +69 -0
  208. package/packages/sci/src/text/message.js +171 -0
  209. package/packages/sci/src/text/message.ts +205 -0
  210. package/packages/sci/src/vm/kernels/arrays.d.ts +19 -0
  211. package/packages/sci/src/vm/kernels/arrays.js +182 -0
  212. package/packages/sci/src/vm/kernels/arrays.ts +164 -0
  213. package/packages/sci/src/vm/kernels/audio.d.ts +70 -0
  214. package/packages/sci/src/vm/kernels/audio.js +347 -0
  215. package/packages/sci/src/vm/kernels/audio.ts +349 -0
  216. package/packages/sci/src/vm/kernels/core.d.ts +5 -0
  217. package/packages/sci/src/vm/kernels/core.js +87 -0
  218. package/packages/sci/src/vm/kernels/core.ts +91 -0
  219. package/packages/sci/src/vm/kernels/graphics.d.ts +141 -0
  220. package/packages/sci/src/vm/kernels/graphics.js +467 -0
  221. package/packages/sci/src/vm/kernels/graphics.ts +502 -0
  222. package/packages/sci/src/vm/kernels/index.d.ts +8 -0
  223. package/packages/sci/src/vm/kernels/index.js +29 -0
  224. package/packages/sci/src/vm/kernels/index.ts +32 -0
  225. package/packages/sci/src/vm/kernels/lists.d.ts +6 -0
  226. package/packages/sci/src/vm/kernels/lists.js +127 -0
  227. package/packages/sci/src/vm/kernels/lists.ts +111 -0
  228. package/packages/sci/src/vm/kernels/motion.d.ts +7 -0
  229. package/packages/sci/src/vm/kernels/motion.js +204 -0
  230. package/packages/sci/src/vm/kernels/motion.ts +194 -0
  231. package/packages/sci/src/vm/kernels/palette.d.ts +82 -0
  232. package/packages/sci/src/vm/kernels/palette.js +292 -0
  233. package/packages/sci/src/vm/kernels/palette.ts +303 -0
  234. package/packages/sci/src/vm/kernels/saves.d.ts +45 -0
  235. package/packages/sci/src/vm/kernels/saves.js +123 -0
  236. package/packages/sci/src/vm/kernels/saves.ts +149 -0
  237. package/packages/sci/src/vm/kernels/system.d.ts +62 -0
  238. package/packages/sci/src/vm/kernels/system.js +110 -0
  239. package/packages/sci/src/vm/kernels/system.ts +128 -0
  240. package/packages/sci/src/vm/kernels/text.d.ts +13 -0
  241. package/packages/sci/src/vm/kernels/text.js +338 -0
  242. package/packages/sci/src/vm/kernels/text.ts +328 -0
  243. package/packages/sci/src/vm/memory.d.ts +138 -0
  244. package/packages/sci/src/vm/memory.js +168 -0
  245. package/packages/sci/src/vm/memory.ts +266 -0
  246. package/packages/sci/src/vm/replace.d.ts +11 -0
  247. package/packages/sci/src/vm/replace.js +14 -0
  248. package/packages/sci/src/vm/replace.ts +16 -0
  249. package/packages/sci/src/vm/savegame.d.ts +89 -0
  250. package/packages/sci/src/vm/savegame.js +161 -0
  251. package/packages/sci/src/vm/savegame.ts +188 -0
  252. package/packages/sci/src/vm/value.d.ts +17 -0
  253. package/packages/sci/src/vm/value.js +12 -0
  254. package/packages/sci/src/vm/value.ts +25 -0
  255. package/packages/sci/src/vm/vm.d.ts +155 -0
  256. package/packages/sci/src/vm/vm.js +1053 -0
  257. package/packages/sci/src/vm/vm.ts +914 -0
  258. package/tools/adlib-render.d.ts +1 -0
  259. package/tools/adlib-render.js +49 -0
  260. package/tools/adlib-render.ts +42 -0
  261. package/tools/analyze.d.ts +1 -0
  262. package/tools/analyze.js +25 -0
  263. package/tools/analyze.ts +25 -0
  264. package/tools/art/build.d.ts +34 -0
  265. package/tools/art/build.js +175 -0
  266. package/tools/art/build.ts +153 -0
  267. package/tools/art.d.ts +1 -0
  268. package/tools/art.js +31 -0
  269. package/tools/art.ts +29 -0
  270. package/tools/asm.d.ts +1 -0
  271. package/tools/asm.js +61 -0
  272. package/tools/asm.ts +55 -0
  273. package/tools/cli.d.ts +1 -0
  274. package/tools/cli.js +99 -0
  275. package/tools/cli.ts +100 -0
  276. package/tools/editor-server.d.ts +29 -0
  277. package/tools/editor-server.js +162 -0
  278. package/tools/editor-server.ts +176 -0
  279. package/tools/explore.d.ts +1 -0
  280. package/tools/explore.js +305 -0
  281. package/tools/explore.ts +250 -0
  282. package/tools/game/audio.d.ts +19 -0
  283. package/tools/game/audio.js +143 -0
  284. package/tools/game/audio.ts +130 -0
  285. package/tools/game/build.d.ts +46 -0
  286. package/tools/game/build.js +259 -0
  287. package/tools/game/build.ts +283 -0
  288. package/tools/game/defaults.d.ts +26 -0
  289. package/tools/game/defaults.js +82 -0
  290. package/tools/game/defaults.ts +83 -0
  291. package/tools/game/font.d.ts +3 -0
  292. package/tools/game/font.js +124 -0
  293. package/tools/game/font.ts +126 -0
  294. package/tools/game/kit.d.ts +7 -0
  295. package/tools/game/kit.js +7 -0
  296. package/tools/game/kit.ts +10 -0
  297. package/tools/game/new.d.ts +8 -0
  298. package/tools/game/new.js +39 -0
  299. package/tools/game/new.ts +38 -0
  300. package/tools/game/target.d.ts +7 -0
  301. package/tools/game/target.js +41 -0
  302. package/tools/game/target.ts +42 -0
  303. package/tools/game/template/resources.ts.tmpl +43 -0
  304. package/tools/game/template/rooms/1.room.yaml +9 -0
  305. package/tools/game/template/rooms/1.yarn +17 -0
  306. package/tools/game/template/scripts/0.sc +24 -0
  307. package/tools/game.d.ts +1 -0
  308. package/tools/game.js +77 -0
  309. package/tools/game.ts +70 -0
  310. package/tools/mod-build.d.ts +28 -0
  311. package/tools/mod-build.js +173 -0
  312. package/tools/mod-build.ts +179 -0
  313. package/tools/mod-target.d.ts +16 -0
  314. package/tools/mod-target.js +26 -0
  315. package/tools/mod-target.ts +32 -0
  316. package/tools/mod.d.ts +1 -0
  317. package/tools/mod.js +47 -0
  318. package/tools/mod.ts +45 -0
  319. package/tools/msg.d.ts +1 -0
  320. package/tools/msg.js +40 -0
  321. package/tools/msg.ts +36 -0
  322. package/tools/node-files.d.ts +3 -0
  323. package/tools/node-files.js +34 -0
  324. package/tools/node-files.ts +33 -0
  325. package/tools/patches.d.ts +3 -0
  326. package/tools/patches.js +13 -0
  327. package/tools/patches.ts +13 -0
  328. package/tools/pic-build.d.ts +9 -0
  329. package/tools/pic-build.js +26 -0
  330. package/tools/pic-build.ts +27 -0
  331. package/tools/pic.d.ts +1 -0
  332. package/tools/pic.js +64 -0
  333. package/tools/pic.ts +61 -0
  334. package/tools/play.d.ts +1 -0
  335. package/tools/play.js +54 -0
  336. package/tools/play.ts +54 -0
  337. package/tools/png.d.ts +16 -0
  338. package/tools/png.js +124 -0
  339. package/tools/png.ts +123 -0
  340. package/tools/res.d.ts +1 -0
  341. package/tools/res.js +48 -0
  342. package/tools/res.ts +50 -0
  343. package/tools/scenario.d.ts +1 -0
  344. package/tools/scenario.js +74 -0
  345. package/tools/scenario.ts +80 -0
  346. package/tools/site/build.d.ts +8 -0
  347. package/tools/site/build.js +110 -0
  348. package/tools/site/build.ts +116 -0
  349. package/tools/site/markdown.d.ts +11 -0
  350. package/tools/site/markdown.js +88 -0
  351. package/tools/site/markdown.ts +84 -0
  352. package/tools/view-build.d.ts +34 -0
  353. package/tools/view-build.js +47 -0
  354. package/tools/view-build.ts +71 -0
  355. package/tools/view.d.ts +1 -0
  356. package/tools/view.js +54 -0
  357. package/tools/view.ts +52 -0
@@ -0,0 +1,332 @@
1
+ import { isSeq, parseDocument } from "yaml";
2
+ // --- Editing YAML text in place ----------------------------------------------------------
3
+ const flow = (v) => (Array.isArray(v) ? `[${v.map(flow).join(", ")}]` : String(v));
4
+ /** `text` with the value at `path` replaced by `value`, keeping the rest as written. */
5
+ export function setYamlValue(text, path, value) {
6
+ const doc = parseDocument(text);
7
+ const node = doc.getIn(path, true);
8
+ if (node?.range) {
9
+ const start = node.range[0];
10
+ let end = node.range[1];
11
+ while (end > start && text[end - 1] === "\n")
12
+ end--;
13
+ let replacement;
14
+ if (isSeq(node) && !node.flow && Array.isArray(value[0])) {
15
+ // A block list of points stays one "- [x, y]" per line, at its indentation.
16
+ const indent = " ".repeat(start - (text.lastIndexOf("\n", start - 1) + 1));
17
+ replacement = value.map((p) => `- ${flow(p)}`).join(`\n${indent}`);
18
+ }
19
+ else
20
+ replacement = flow(value);
21
+ return text.slice(0, start) + replacement + text.slice(end);
22
+ }
23
+ // Not there yet: let the YAML library add it (it may tidy the layout).
24
+ doc.setIn(path, flowNode(doc, value));
25
+ return doc.toString();
26
+ }
27
+ /** `text` with `value` added at the end of the list at `path` (made if missing). */
28
+ export function appendYamlItem(text, path, value) {
29
+ const doc = parseDocument(text);
30
+ const node = doc.getIn(path, true);
31
+ if (node?.range && isSeq(node) && !node.flow) {
32
+ const start = node.range[0];
33
+ let end = node.range[1];
34
+ while (end > start && text[end - 1] === "\n")
35
+ end--;
36
+ const indent = " ".repeat(start - (text.lastIndexOf("\n", start - 1) + 1));
37
+ return `${text.slice(0, end)}\n${indent}- ${flow(value)}${text.slice(end)}`;
38
+ }
39
+ if (!node)
40
+ return `${text.replace(/\n*$/, "\n")}\n${path.join(".")}:\n - ${flow(value)}\n`;
41
+ doc.addIn(path, flowNode(doc, value));
42
+ return doc.toString();
43
+ }
44
+ function flowNode(doc, value) {
45
+ const node = doc.createNode(value);
46
+ if (isSeq(node)) {
47
+ node.flow = true;
48
+ for (const item of node.items)
49
+ if (isSeq(item))
50
+ item.flow = true;
51
+ }
52
+ return node;
53
+ }
54
+ const NS = "http://www.w3.org/2000/svg";
55
+ const el = (tag, attrs = {}, text) => {
56
+ const node = document.createElementNS(NS, tag);
57
+ for (const [k, v] of Object.entries(attrs))
58
+ node.setAttribute(k, String(v));
59
+ if (text)
60
+ node.textContent = text;
61
+ return node;
62
+ };
63
+ const COLORS = { feature: "#e8b04a", exit: "#5fd08c", prop: "#7fb3e8", hero: "#f08fd0", floor: "#5fa0e8", obstacle: "#e05555" };
64
+ const clampX = (x) => Math.max(0, Math.min(319, Math.round(x)));
65
+ const clampY = (y) => Math.max(0, Math.min(189, Math.round(y)));
66
+ /**
67
+ * An SVG layer over the game. In "move" mode, everything the YAML places has a handle;
68
+ * "floor" and "obstacle" are brushes that draw a polygon. In "play" it's out of the way.
69
+ */
70
+ export class EditLayer {
71
+ host;
72
+ svg;
73
+ mode = "play";
74
+ spec = {};
75
+ specText = "";
76
+ drag;
77
+ stroke;
78
+ lastWrite = 0;
79
+ constructor(host) {
80
+ this.host = host;
81
+ this.svg = el("svg", { viewBox: "0 0 320 200", preserveAspectRatio: "none" });
82
+ this.svg.style.touchAction = "none";
83
+ this.svg.addEventListener("pointerdown", (e) => this.down(e));
84
+ this.svg.addEventListener("pointermove", (e) => this.move(e));
85
+ this.svg.addEventListener("pointerup", (e) => this.up(e));
86
+ this.svg.addEventListener("pointercancel", (e) => this.up(e));
87
+ this.setMode("play");
88
+ }
89
+ setMode(mode) {
90
+ this.mode = mode;
91
+ this.svg.style.pointerEvents = mode === "play" ? "none" : "auto";
92
+ this.svg.style.cursor = mode === "move" ? "default" : mode === "play" ? "" : "crosshair";
93
+ this.render();
94
+ }
95
+ /** Room coordinates under the pointer. */
96
+ point(e) {
97
+ const m = this.svg.getScreenCTM();
98
+ if (!m)
99
+ return [0, 0];
100
+ const p = new DOMPoint(e.clientX, e.clientY).matrixTransform(m.inverse());
101
+ const [ox, oy] = this.host.roomOffset();
102
+ return [p.x - ox, p.y - oy];
103
+ }
104
+ readSpec() {
105
+ const text = this.host.yaml();
106
+ if (text !== this.specText) {
107
+ this.specText = text;
108
+ try {
109
+ this.spec = parseDocument(text).toJS() ?? {};
110
+ }
111
+ catch {
112
+ /* mid-edit: keep the last good one */
113
+ }
114
+ }
115
+ return this.spec;
116
+ }
117
+ // --- Drawing ---
118
+ render() {
119
+ this.svg.replaceChildren();
120
+ if (this.mode === "play")
121
+ return;
122
+ const spec = this.readSpec();
123
+ const [ox, oy] = this.host.roomOffset();
124
+ const g = el("g", { transform: `translate(${ox} ${oy})` });
125
+ this.svg.append(g);
126
+ const handle = (x, y, color, start, shape = "square") => {
127
+ const attrs = { fill: color, stroke: "#000", "stroke-width": 0.4, cursor: "grab" };
128
+ const h = shape === "circle"
129
+ ? el("circle", { cx: x, cy: y, r: 2.4, ...attrs })
130
+ : shape === "diamond"
131
+ ? el("path", { d: `M${x} ${y - 2.8}L${x + 2.8} ${y}L${x} ${y + 2.8}L${x - 2.8} ${y}Z`, ...attrs })
132
+ : el("rect", { x: x - 2, y: y - 2, width: 4, height: 4, ...attrs });
133
+ h.addEventListener("pointerdown", (e) => this.begin(e, start));
134
+ g.append(h);
135
+ return h;
136
+ };
137
+ const label = (x, y, text, color) => g.append(el("text", { x, y, fill: color, stroke: "#000", "stroke-width": 1.2, "paint-order": "stroke", "font-size": 5, "font-family": "ui-monospace, Menlo, monospace", "pointer-events": "none" }, text));
138
+ if (this.mode === "move") {
139
+ // Walk areas: every vertex can be dragged; Alt-click removes one.
140
+ const polygon = (points, path, color) => {
141
+ g.append(el("polygon", { points: points.map((p) => p.join(",")).join(" "), fill: `${color}22`, stroke: color, "stroke-width": 0.6, "pointer-events": "none" }));
142
+ points.forEach((p, i) => {
143
+ const h = handle(p[0], p[1], color, () => ({
144
+ start: p,
145
+ apply: (dx, dy) => setYamlValue(this.host.yaml(), path, points.map((q, j) => (j === i ? [clampX(p[0] + dx), clampY(p[1] + dy)] : q))),
146
+ }), "circle");
147
+ h.addEventListener("click", (e) => {
148
+ if (!e.altKey || points.length <= 3)
149
+ return;
150
+ this.host.setYaml(setYamlValue(this.host.yaml(), path, points.filter((_, j) => j !== i)));
151
+ });
152
+ });
153
+ };
154
+ if (spec.walkable)
155
+ polygon(spec.walkable, ["walkable"], COLORS.floor);
156
+ spec.obstacles?.forEach((o, i) => polygon(o, ["obstacles", i], COLORS.obstacle));
157
+ // Features and exits: drag the box to move it (and its look-at point), corners to resize.
158
+ for (const group of ["features", "exits"]) {
159
+ for (const [name, f] of Object.entries(spec[group] ?? {})) {
160
+ const color = group === "exits" ? COLORS.exit : COLORS.feature;
161
+ const r = f.rect;
162
+ if (r?.length === 4) {
163
+ const [l, t, rt, b] = r;
164
+ const box = el("rect", { x: l, y: t, width: rt - l, height: b - t, fill: `${color}22`, stroke: color, "stroke-width": 0.6, cursor: "move" });
165
+ box.addEventListener("pointerdown", (e) => this.begin(e, () => ({
166
+ start: [l, t],
167
+ apply: (dx, dy) => {
168
+ let text = setYamlValue(this.host.yaml(), [group, name, "rect"], [clampX(l + dx), clampY(t + dy), clampX(rt + dx), clampY(b + dy)]);
169
+ if (f.at)
170
+ text = setYamlValue(text, [group, name, "at"], [clampX(f.at[0] + dx), clampY(f.at[1] + dy)]);
171
+ return text;
172
+ },
173
+ })));
174
+ g.append(box);
175
+ label(l + 2, t + 6, name, color);
176
+ [[l, t, 0, 1], [rt, t, 2, 1], [l, b, 0, 3], [rt, b, 2, 3]].forEach(([x, y, xi, yi]) => {
177
+ handle(x, y, color, () => ({
178
+ start: [x, y],
179
+ apply: (dx, dy) => {
180
+ const next = [l, t, rt, b];
181
+ next[xi] = clampX(x + dx);
182
+ next[yi] = clampY(y + dy);
183
+ const [nl, nt, nr, nb] = next;
184
+ return setYamlValue(this.host.yaml(), [group, name, "rect"], [Math.min(nl, nr), Math.min(nt, nb), Math.max(nl, nr), Math.max(nt, nb)]);
185
+ },
186
+ }));
187
+ });
188
+ }
189
+ if (f.approach) {
190
+ const [ax, ay] = f.approach;
191
+ label(ax + 3, ay + 2, "approach", color);
192
+ handle(ax, ay, color, () => ({ start: [ax, ay], apply: (dx, dy) => setYamlValue(this.host.yaml(), [group, name, "approach"], [clampX(ax + dx), clampY(ay + dy)]) }), "diamond");
193
+ }
194
+ }
195
+ }
196
+ // Props: grab the sprite (or its anchor) to move its `at`.
197
+ for (const [name, p] of Object.entries(spec.props ?? {})) {
198
+ if (!p.at)
199
+ continue;
200
+ const [x, y] = p.at;
201
+ const box = this.host.spriteBox(name);
202
+ const start = () => ({
203
+ start: [x, y],
204
+ apply: (dx, dy) => setYamlValue(this.host.yaml(), ["props", name, "at"], [clampX(x + dx), clampY(y + dy)]),
205
+ preview: (dx, dy) => this.host.previewProp(name, [clampX(x + dx), clampY(y + dy)]),
206
+ });
207
+ if (box) {
208
+ const r = el("rect", { x: box.x, y: box.y, width: box.w, height: box.h, fill: `${COLORS.prop}22`, stroke: COLORS.prop, "stroke-width": 0.6, "stroke-dasharray": "2 1", cursor: "move" });
209
+ r.addEventListener("pointerdown", (e) => this.begin(e, start));
210
+ g.append(r);
211
+ }
212
+ label(x + 3, y + 2, name, COLORS.prop);
213
+ handle(x, y, COLORS.prop, start, "circle");
214
+ }
215
+ // The hero: where it appears, and where it walks to on arrival.
216
+ const hero = spec.hero;
217
+ if (hero?.at && hero.enterTo) {
218
+ g.append(el("line", { x1: hero.at[0], y1: hero.at[1], x2: hero.enterTo[0], y2: hero.enterTo[1], stroke: COLORS.hero, "stroke-width": 0.6, "stroke-dasharray": "2 1.5", "pointer-events": "none" }));
219
+ }
220
+ for (const key of ["at", "enterTo"]) {
221
+ const p = hero?.[key];
222
+ if (!p)
223
+ continue;
224
+ label(p[0] + 3, p[1] + 2, key === "at" ? "hero" : "walks to", COLORS.hero);
225
+ handle(p[0], p[1], COLORS.hero, () => ({ start: p, apply: (dx, dy) => setYamlValue(this.host.yaml(), ["hero", key], [clampX(p[0] + dx), clampY(p[1] + dy)]) }), key === "at" ? "circle" : "diamond");
226
+ }
227
+ }
228
+ else if (this.stroke) {
229
+ // A brush stroke in progress.
230
+ const color = this.mode === "floor" ? COLORS.floor : COLORS.obstacle;
231
+ g.append(el("polyline", { points: this.stroke.map((p) => p.join(",")).join(" "), fill: `${color}33`, stroke: color, "stroke-width": 1.2, "pointer-events": "none" }));
232
+ }
233
+ }
234
+ // --- Pointer handling ---
235
+ begin(e, start) {
236
+ if (this.mode !== "move" || e.button !== 0 || e.altKey)
237
+ return;
238
+ e.stopPropagation();
239
+ e.preventDefault();
240
+ this.drag = start();
241
+ this.drag.from = this.point(e);
242
+ this.capture(e, true);
243
+ this.host.holdBuild(true);
244
+ }
245
+ /** Keep getting the pointer's moves outside the layer (not for synthetic events). */
246
+ capture(e, on) {
247
+ try {
248
+ if (on)
249
+ this.svg.setPointerCapture(e.pointerId);
250
+ else if (this.svg.hasPointerCapture(e.pointerId))
251
+ this.svg.releasePointerCapture(e.pointerId);
252
+ }
253
+ catch {
254
+ /* no such pointer */
255
+ }
256
+ }
257
+ down(e) {
258
+ if (this.mode !== "floor" && this.mode !== "obstacle")
259
+ return;
260
+ e.preventDefault();
261
+ this.capture(e, true);
262
+ this.stroke = [this.point(e).map(Math.round)];
263
+ this.host.holdBuild(true);
264
+ this.render();
265
+ }
266
+ move(e) {
267
+ const p = this.point(e);
268
+ if (this.drag) {
269
+ const from = this.drag.from;
270
+ const [dx, dy] = [Math.round(p[0] - from[0]), Math.round(p[1] - from[1])];
271
+ this.drag.preview?.(dx, dy);
272
+ const text = this.drag.apply(dx, dy);
273
+ // Show it as it moves: the YAML (and these handles) follow the pointer.
274
+ if (text !== this.host.yaml())
275
+ this.host.setYaml(text);
276
+ this.render();
277
+ }
278
+ else if (this.stroke) {
279
+ const last = this.stroke.at(-1);
280
+ if (Math.hypot(p[0] - last[0], p[1] - last[1]) < 4)
281
+ return;
282
+ this.stroke.push([clampX(p[0]), clampY(p[1])]);
283
+ this.render();
284
+ // The list in the editor follows the brush, a few times a second.
285
+ if (performance.now() - this.lastWrite > 120)
286
+ this.writeStroke(false);
287
+ }
288
+ }
289
+ up(e) {
290
+ this.capture(e, false);
291
+ if (this.drag) {
292
+ this.drag = undefined;
293
+ this.host.holdBuild(false);
294
+ this.render();
295
+ }
296
+ else if (this.stroke) {
297
+ this.writeStroke(true);
298
+ this.stroke = undefined;
299
+ this.host.holdBuild(false);
300
+ this.render();
301
+ }
302
+ }
303
+ /** The stroke so far as a polygon in the YAML (simplified to its corners). */
304
+ writeStroke(final) {
305
+ this.lastWrite = performance.now();
306
+ const points = simplify(this.stroke, final ? 2.5 : 2);
307
+ if (points.length < 3)
308
+ return;
309
+ const yaml = this.host.yaml();
310
+ if (this.mode === "floor")
311
+ this.host.setYaml(setYamlValue(yaml, ["walkable"], points));
312
+ else if (final)
313
+ this.host.setYaml(appendYamlItem(yaml, ["obstacles"], points));
314
+ }
315
+ }
316
+ /** Ramer-Douglas-Peucker: drop points within `epsilon` of the line through their neighbours. */
317
+ export function simplify(points, epsilon) {
318
+ if (points.length < 3)
319
+ return points;
320
+ const [a, b] = [points[0], points.at(-1)];
321
+ let index = 0, max = 0;
322
+ for (let i = 1; i < points.length - 1; i++) {
323
+ const p = points[i];
324
+ const len = Math.hypot(b[0] - a[0], b[1] - a[1]) || 1;
325
+ const d = Math.abs((b[0] - a[0]) * (a[1] - p[1]) - (a[0] - p[0]) * (b[1] - a[1])) / len;
326
+ if (d > max)
327
+ (max = d), (index = i);
328
+ }
329
+ if (max <= epsilon)
330
+ return [a, b];
331
+ return [...simplify(points.slice(0, index + 1), epsilon).slice(0, -1), ...simplify(points.slice(index), epsilon)];
332
+ }