@johnoscott/isometric-engine 0.0.0-stage → 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.
Files changed (310) hide show
  1. package/EDITOR-GUIDE.md +317 -0
  2. package/LICENSE +21 -0
  3. package/LICENSES/ajv.txt +22 -0
  4. package/LICENSES/lucide-static.txt +43 -0
  5. package/LICENSES/yaml.txt +13 -0
  6. package/README.md +62 -2
  7. package/assets/app.css +265 -0
  8. package/assets/default-theme.yaml +99 -0
  9. package/assets/icons.svg +1 -0
  10. package/assets/marks.css +35 -0
  11. package/assets/skin.css +181 -0
  12. package/iso-editor.d.ts +4 -0
  13. package/iso-editor.js +550 -0
  14. package/iso-render.d.ts +3 -0
  15. package/iso-render.js +388 -0
  16. package/iso-systems.d.ts +4 -0
  17. package/iso-systems.js +389 -0
  18. package/js/iso-core/compile-schema.js +22 -0
  19. package/js/iso-core/depth.js +55 -0
  20. package/js/iso-core/errors.js +65 -0
  21. package/js/iso-core/glyphs.js +100 -0
  22. package/js/iso-core/index.js +55 -0
  23. package/js/iso-core/kernel.js +158 -0
  24. package/js/iso-core/metrics.json +1 -0
  25. package/js/iso-core/package.json +6 -0
  26. package/js/iso-core/parse.js +79 -0
  27. package/js/iso-core/schema-errors.js +170 -0
  28. package/js/iso-core/shims/crypto.js +125 -0
  29. package/js/iso-core/shims/path.js +49 -0
  30. package/js/iso-core/svg-dom.js +144 -0
  31. package/js/iso-core/theme.js +48 -0
  32. package/js/iso-core/vdom.js +48 -0
  33. package/js/iso-editor/adapter.js +0 -0
  34. package/js/iso-editor/clipboard.js +152 -0
  35. package/js/iso-editor/commands.js +173 -0
  36. package/js/iso-editor/complete.js +55 -0
  37. package/js/iso-editor/doc.js +182 -0
  38. package/js/iso-editor/index.js +105 -0
  39. package/js/iso-editor/model.js +210 -0
  40. package/js/iso-editor/outline.js +111 -0
  41. package/js/iso-editor/package.json +6 -0
  42. package/js/iso-editor/path.js +19 -0
  43. package/js/iso-editor/providers.js +96 -0
  44. package/js/iso-editor/schema-nav.js +117 -0
  45. package/js/iso-editor/splice.js +441 -0
  46. package/js/iso-editor/storage-keys.js +6 -0
  47. package/js/iso-editor/ui/actions.js +116 -0
  48. package/js/iso-editor/ui/canvas-layer.js +0 -0
  49. package/js/iso-editor/ui/canvas.js +1205 -0
  50. package/js/iso-editor/ui/clip.js +133 -0
  51. package/js/iso-editor/ui/dialog.js +26 -0
  52. package/js/iso-editor/ui/flat-canvas.js +54 -0
  53. package/js/iso-editor/ui/icons.js +15 -0
  54. package/js/iso-editor/ui/keys.js +26 -0
  55. package/js/iso-editor/ui/later.js +28 -0
  56. package/js/iso-editor/ui/layer.js +11 -0
  57. package/js/iso-editor/ui/menu.js +105 -0
  58. package/js/iso-editor/ui/monaco-entry.js +2 -0
  59. package/js/iso-editor/ui/mount-kit.js +0 -0
  60. package/js/iso-editor/ui/mount.js +767 -0
  61. package/js/iso-editor/ui/pane.js +0 -0
  62. package/js/iso-editor/ui/part.js +0 -0
  63. package/js/iso-editor/ui/plain-icons.js +34 -0
  64. package/js/iso-editor/ui/props.js +314 -0
  65. package/js/iso-editor/ui/scope-css.js +81 -0
  66. package/js/iso-editor/ui/setup.js +145 -0
  67. package/js/iso-editor/ui/text.js +323 -0
  68. package/js/iso-editor/ui/tree.js +728 -0
  69. package/js/iso-editor/validate.js +33 -0
  70. package/js/iso-editor/window/protocol.js +8 -0
  71. package/js/iso-objects/assemble.js +43 -0
  72. package/js/iso-objects/assets.js +88 -0
  73. package/js/iso-objects/board.js +162 -0
  74. package/js/iso-objects/compile.js +60 -0
  75. package/js/iso-objects/css.js +154 -0
  76. package/js/iso-objects/flat.js +696 -0
  77. package/js/iso-objects/focus.js +16 -0
  78. package/js/iso-objects/geometry.js +76 -0
  79. package/js/iso-objects/index.js +93 -0
  80. package/js/iso-objects/kinds.js +12 -0
  81. package/js/iso-objects/labels.js +325 -0
  82. package/js/iso-objects/package.json +6 -0
  83. package/js/iso-objects/place.js +347 -0
  84. package/js/iso-objects/route.js +461 -0
  85. package/js/iso-objects/scene.js +350 -0
  86. package/js/iso-objects/steps.js +246 -0
  87. package/js/iso-objects/types.js +0 -0
  88. package/js/iso-systems/catalog.js +42 -0
  89. package/js/iso-systems/compile.js +34 -0
  90. package/js/iso-systems/css.js +8 -0
  91. package/js/iso-systems/index.js +16 -0
  92. package/js/iso-systems/nodes.js +124 -0
  93. package/js/iso-systems/package.json +6 -0
  94. package/js/iso-systems/schema.json +311 -0
  95. package/js/iso-systems/validate.js +35 -0
  96. package/js/iso-viewer/bundle.js +20 -0
  97. package/js/iso-viewer/card-motion.js +228 -0
  98. package/js/iso-viewer/dom.js +14 -0
  99. package/js/iso-viewer/dual-view.js +212 -0
  100. package/js/iso-viewer/flat-view.js +124 -0
  101. package/js/iso-viewer/history.js +990 -0
  102. package/js/iso-viewer/index.js +98 -0
  103. package/js/iso-viewer/morph.js +192 -0
  104. package/js/iso-viewer/overlay.js +199 -0
  105. package/js/iso-viewer/package.json +6 -0
  106. package/js/iso-viewer/page.js +138 -0
  107. package/js/iso-viewer/player.js +409 -0
  108. package/js/iso-viewer/still.js +81 -0
  109. package/js/iso-viewer/timeline-contract.js +14 -0
  110. package/js/iso-viewer/timeline-engine.js +475 -0
  111. package/js/iso-viewer/timeline-export.js +36 -0
  112. package/js/iso-viewer/timeline-scale.js +116 -0
  113. package/js/iso-viewer/timeline-scope.js +58 -0
  114. package/js/iso-viewer/timeline-search.js +216 -0
  115. package/js/iso-viewer/timeline-time.js +55 -0
  116. package/js/iso-viewer/view-css.js +68 -0
  117. package/js/iso-viewer/viewport.js +177 -0
  118. package/package.json +502 -4
  119. package/src/iso-core/compile-schema.ts +23 -0
  120. package/src/iso-core/depth.ts +56 -0
  121. package/src/iso-core/errors.ts +73 -0
  122. package/src/iso-core/glyphs.ts +108 -0
  123. package/src/iso-core/index.ts +27 -0
  124. package/src/iso-core/kernel.ts +163 -0
  125. package/src/iso-core/metrics.json +1 -0
  126. package/src/iso-core/package.json +6 -0
  127. package/src/iso-core/parse.ts +91 -0
  128. package/src/iso-core/schema-errors.ts +163 -0
  129. package/src/iso-core/shims/crypto.ts +37 -0
  130. package/src/iso-core/shims/path.ts +24 -0
  131. package/src/iso-core/svg-dom.ts +103 -0
  132. package/src/iso-core/theme.ts +64 -0
  133. package/src/iso-core/vdom.ts +41 -0
  134. package/src/iso-editor/adapter.ts +128 -0
  135. package/src/iso-editor/clipboard.ts +163 -0
  136. package/src/iso-editor/commands.ts +191 -0
  137. package/src/iso-editor/complete.ts +73 -0
  138. package/src/iso-editor/doc.ts +207 -0
  139. package/src/iso-editor/index.ts +67 -0
  140. package/src/iso-editor/model.ts +234 -0
  141. package/src/iso-editor/outline.ts +126 -0
  142. package/src/iso-editor/package.json +6 -0
  143. package/src/iso-editor/path.ts +18 -0
  144. package/src/iso-editor/providers.ts +113 -0
  145. package/src/iso-editor/schema-nav.ts +129 -0
  146. package/src/iso-editor/splice.ts +472 -0
  147. package/src/iso-editor/storage-keys.ts +6 -0
  148. package/src/iso-editor/ui/actions.ts +119 -0
  149. package/src/iso-editor/ui/app.css +230 -0
  150. package/src/iso-editor/ui/canvas-layer.ts +56 -0
  151. package/src/iso-editor/ui/canvas.ts +1071 -0
  152. package/src/iso-editor/ui/clip.ts +97 -0
  153. package/src/iso-editor/ui/dialog.ts +25 -0
  154. package/src/iso-editor/ui/flat-canvas.ts +51 -0
  155. package/src/iso-editor/ui/icons.ts +21 -0
  156. package/src/iso-editor/ui/keys.ts +30 -0
  157. package/src/iso-editor/ui/later.ts +22 -0
  158. package/src/iso-editor/ui/layer.ts +7 -0
  159. package/src/iso-editor/ui/marks.css +35 -0
  160. package/src/iso-editor/ui/menu.ts +67 -0
  161. package/src/iso-editor/ui/monaco-entry.ts +6 -0
  162. package/src/iso-editor/ui/mount-kit.ts +35 -0
  163. package/src/iso-editor/ui/mount.ts +684 -0
  164. package/src/iso-editor/ui/pane.ts +45 -0
  165. package/src/iso-editor/ui/part.ts +45 -0
  166. package/src/iso-editor/ui/plain-icons.ts +83 -0
  167. package/src/iso-editor/ui/props.ts +347 -0
  168. package/src/iso-editor/ui/scope-css.ts +65 -0
  169. package/src/iso-editor/ui/setup.ts +144 -0
  170. package/src/iso-editor/ui/skin.css +181 -0
  171. package/src/iso-editor/ui/text.ts +262 -0
  172. package/src/iso-editor/ui/tree.ts +657 -0
  173. package/src/iso-editor/validate.ts +62 -0
  174. package/src/iso-editor/window/protocol.ts +33 -0
  175. package/src/iso-objects/assemble.ts +57 -0
  176. package/src/iso-objects/assets.ts +122 -0
  177. package/src/iso-objects/board.ts +169 -0
  178. package/src/iso-objects/compile.ts +76 -0
  179. package/src/iso-objects/css.ts +163 -0
  180. package/src/iso-objects/flat.ts +653 -0
  181. package/src/iso-objects/focus.ts +14 -0
  182. package/src/iso-objects/geometry.ts +86 -0
  183. package/src/iso-objects/index.ts +33 -0
  184. package/src/iso-objects/kinds.ts +56 -0
  185. package/src/iso-objects/labels.ts +310 -0
  186. package/src/iso-objects/package.json +6 -0
  187. package/src/iso-objects/place.ts +318 -0
  188. package/src/iso-objects/route.ts +444 -0
  189. package/src/iso-objects/scene.ts +376 -0
  190. package/src/iso-objects/steps.ts +234 -0
  191. package/src/iso-objects/types.ts +68 -0
  192. package/src/iso-systems/catalog.ts +31 -0
  193. package/src/iso-systems/compile.ts +39 -0
  194. package/src/iso-systems/css.ts +9 -0
  195. package/src/iso-systems/index.ts +12 -0
  196. package/src/iso-systems/nodes.ts +126 -0
  197. package/src/iso-systems/package.json +6 -0
  198. package/src/iso-systems/schema.json +311 -0
  199. package/src/iso-systems/validate.ts +34 -0
  200. package/src/iso-viewer/bundle.ts +15 -0
  201. package/src/iso-viewer/card-motion.ts +237 -0
  202. package/src/iso-viewer/dom.ts +13 -0
  203. package/src/iso-viewer/dual-view.ts +212 -0
  204. package/src/iso-viewer/flat-view.ts +127 -0
  205. package/src/iso-viewer/history.ts +781 -0
  206. package/src/iso-viewer/index.ts +40 -0
  207. package/src/iso-viewer/morph.ts +191 -0
  208. package/src/iso-viewer/overlay.ts +212 -0
  209. package/src/iso-viewer/package.json +6 -0
  210. package/src/iso-viewer/page.ts +150 -0
  211. package/src/iso-viewer/player.ts +321 -0
  212. package/src/iso-viewer/still.ts +74 -0
  213. package/src/iso-viewer/timeline-contract.ts +342 -0
  214. package/src/iso-viewer/timeline-engine.ts +420 -0
  215. package/src/iso-viewer/timeline-export.ts +31 -0
  216. package/src/iso-viewer/timeline-scale.ts +129 -0
  217. package/src/iso-viewer/timeline-scope.ts +56 -0
  218. package/src/iso-viewer/timeline-search.ts +198 -0
  219. package/src/iso-viewer/timeline-time.ts +54 -0
  220. package/src/iso-viewer/view-css.ts +59 -0
  221. package/src/iso-viewer/viewport.ts +142 -0
  222. package/types/iso-core/compile-schema.d.ts +11 -0
  223. package/types/iso-core/depth.d.ts +6 -0
  224. package/types/iso-core/errors.d.ts +24 -0
  225. package/types/iso-core/glyphs.d.ts +19 -0
  226. package/types/iso-core/index.d.ts +17 -0
  227. package/types/iso-core/kernel.d.ts +130 -0
  228. package/types/iso-core/metrics.json +1 -0
  229. package/types/iso-core/parse.d.ts +18 -0
  230. package/types/iso-core/schema-errors.d.ts +14 -0
  231. package/types/iso-core/shims/crypto.d.ts +9 -0
  232. package/types/iso-core/svg-dom.d.ts +15 -0
  233. package/types/iso-core/theme.d.ts +38 -0
  234. package/types/iso-core/vdom.d.ts +26 -0
  235. package/types/iso-editor/adapter.d.ts +145 -0
  236. package/types/iso-editor/clipboard.d.ts +23 -0
  237. package/types/iso-editor/commands.d.ts +32 -0
  238. package/types/iso-editor/complete.d.ts +25 -0
  239. package/types/iso-editor/doc.d.ts +50 -0
  240. package/types/iso-editor/index.d.ts +57 -0
  241. package/types/iso-editor/model.d.ts +96 -0
  242. package/types/iso-editor/outline.d.ts +28 -0
  243. package/types/iso-editor/path.d.ts +7 -0
  244. package/types/iso-editor/providers.d.ts +43 -0
  245. package/types/iso-editor/schema-nav.d.ts +34 -0
  246. package/types/iso-editor/splice.d.ts +53 -0
  247. package/types/iso-editor/storage-keys.d.ts +5 -0
  248. package/types/iso-editor/ui/actions.d.ts +23 -0
  249. package/types/iso-editor/ui/canvas-layer.d.ts +65 -0
  250. package/types/iso-editor/ui/canvas.d.ts +279 -0
  251. package/types/iso-editor/ui/clip.d.ts +14 -0
  252. package/types/iso-editor/ui/dialog.d.ts +7 -0
  253. package/types/iso-editor/ui/flat-canvas.d.ts +2 -0
  254. package/types/iso-editor/ui/icons.d.ts +5 -0
  255. package/types/iso-editor/ui/keys.d.ts +7 -0
  256. package/types/iso-editor/ui/later.d.ts +12 -0
  257. package/types/iso-editor/ui/layer.d.ts +2 -0
  258. package/types/iso-editor/ui/menu.d.ts +21 -0
  259. package/types/iso-editor/ui/mount-kit.d.ts +31 -0
  260. package/types/iso-editor/ui/mount.d.ts +146 -0
  261. package/types/iso-editor/ui/pane.d.ts +60 -0
  262. package/types/iso-editor/ui/part.d.ts +40 -0
  263. package/types/iso-editor/ui/plain-icons.d.ts +10 -0
  264. package/types/iso-editor/ui/props.d.ts +57 -0
  265. package/types/iso-editor/ui/scope-css.d.ts +5 -0
  266. package/types/iso-editor/ui/setup.d.ts +40 -0
  267. package/types/iso-editor/ui/text.d.ts +19 -0
  268. package/types/iso-editor/ui/tree.d.ts +100 -0
  269. package/types/iso-editor/validate.d.ts +36 -0
  270. package/types/iso-editor/window/protocol.d.ts +60 -0
  271. package/types/iso-objects/assemble.d.ts +35 -0
  272. package/types/iso-objects/assets.d.ts +49 -0
  273. package/types/iso-objects/board.d.ts +36 -0
  274. package/types/iso-objects/compile.d.ts +35 -0
  275. package/types/iso-objects/css.d.ts +16 -0
  276. package/types/iso-objects/flat.d.ts +130 -0
  277. package/types/iso-objects/focus.d.ts +1 -0
  278. package/types/iso-objects/geometry.d.ts +59 -0
  279. package/types/iso-objects/index.d.ts +26 -0
  280. package/types/iso-objects/kinds.d.ts +53 -0
  281. package/types/iso-objects/labels.d.ts +92 -0
  282. package/types/iso-objects/place.d.ts +25 -0
  283. package/types/iso-objects/route.d.ts +67 -0
  284. package/types/iso-objects/scene.d.ts +63 -0
  285. package/types/iso-objects/steps.d.ts +90 -0
  286. package/types/iso-objects/types.d.ts +140 -0
  287. package/types/iso-systems/catalog.d.ts +4 -0
  288. package/types/iso-systems/compile.d.ts +6 -0
  289. package/types/iso-systems/css.d.ts +3 -0
  290. package/types/iso-systems/index.d.ts +5 -0
  291. package/types/iso-systems/nodes.d.ts +3 -0
  292. package/types/iso-viewer/card-motion.d.ts +56 -0
  293. package/types/iso-viewer/dom.d.ts +1 -0
  294. package/types/iso-viewer/dual-view.d.ts +44 -0
  295. package/types/iso-viewer/flat-view.d.ts +25 -0
  296. package/types/iso-viewer/history.d.ts +91 -0
  297. package/types/iso-viewer/index.d.ts +31 -0
  298. package/types/iso-viewer/morph.d.ts +28 -0
  299. package/types/iso-viewer/overlay.d.ts +135 -0
  300. package/types/iso-viewer/page.d.ts +13 -0
  301. package/types/iso-viewer/still.d.ts +26 -0
  302. package/types/iso-viewer/timeline-contract.d.ts +345 -0
  303. package/types/iso-viewer/timeline-engine.d.ts +20 -0
  304. package/types/iso-viewer/timeline-export.d.ts +5 -0
  305. package/types/iso-viewer/timeline-scale.d.ts +44 -0
  306. package/types/iso-viewer/timeline-scope.d.ts +33 -0
  307. package/types/iso-viewer/timeline-search.d.ts +52 -0
  308. package/types/iso-viewer/timeline-time.d.ts +8 -0
  309. package/types/iso-viewer/view-css.d.ts +7 -0
  310. package/types/iso-viewer/viewport.d.ts +50 -0
@@ -0,0 +1,318 @@
1
+ // Placement: regions and nodes → absolute boxes in grid units.
2
+ //
3
+ // Every container (the scene, a region, a node with nodes on top) lays out its children in its own frame, whose
4
+ // origin is the container's top-left corner (smallest x and y) on the floor its children stand on. A child is put
5
+ // at `at`, or next to a sibling (`right-of` +x, `left-of` −x, `below` +y, `behind` −y), or, with neither, after the
6
+ // previous sibling in the container's flow. A region without a `size` grows to fit its children plus its padding.
7
+
8
+ import type { Box } from '../iso-core/kernel.ts';
9
+ import { type KindCatalog, regionKind, nodeKind, nodeKindOr, wallHeight } from './kinds.ts';
10
+ import { didYouMean, type Diag } from '../iso-core/errors.ts';
11
+ import type { Placed, RawNode, RawRegion, RawScene, Theme } from './types.ts';
12
+
13
+ export interface Located { locate(path: string, opts?: { key?: boolean }): { line: number; col: number } }
14
+
15
+ /** a walled region's wall thickness */
16
+ export const WALL_T = 0.16;
17
+
18
+ const REL = ['right-of', 'left-of', 'below', 'behind'] as const;
19
+ type Rel = typeof REL[number];
20
+
21
+ interface Local { x: number; y: number; w: number; d: number; h: number; z: number }
22
+
23
+ export interface Layout {
24
+ items: Placed[];
25
+ byId: Map<string, Placed>;
26
+ roots: Placed[];
27
+ diags: Diag[];
28
+ /** the kinds it was laid out with: drawing and routing ask them too */
29
+ catalog: KindCatalog;
30
+ }
31
+
32
+ export function place(scene: RawScene, scenePath: string, src: Located, theme: Theme, catalog: KindCatalog, fileRoles: Record<string, unknown> = {}, snap?: number): Layout {
33
+ const diags: Diag[] = [];
34
+ const err = (path: string, message: string, hint?: string, key = false, severity: 'error' | 'warning' = 'error') => {
35
+ const p = src.locate(path, { key });
36
+ diags.push({ severity, message, line: p.line, col: p.col, path, hint });
37
+ };
38
+ const items: Placed[] = [];
39
+ const byId = new Map<string, Placed>();
40
+ const roots: Placed[] = [];
41
+ const local = new Map<Placed, Local>();
42
+ const roles = new Set([...Object.keys(theme.roles), ...Object.keys(fileRoles)]);
43
+
44
+ // ---- 1. collect ----
45
+ function add(p: Placed, parent: Placed | undefined) {
46
+ const prev = byId.get(p.id);
47
+ if (prev) {
48
+ const at = src.locate(prev.path + '/id');
49
+ err(p.path + '/id', `duplicate id "${p.id}" (first used on line ${at.line})`);
50
+ return false;
51
+ }
52
+ byId.set(p.id, p); items.push(p);
53
+ p.parent = parent;
54
+ if (parent) parent.children.push(p); else roots.push(p);
55
+ return true;
56
+ }
57
+ const blank = (): Box => ({ x0: 0, y0: 0, z0: 0, x1: 0, y1: 0, z1: 0 });
58
+ function addNode(n: RawNode, path: string, parent: Placed | undefined) {
59
+ const p: Placed = { id: n.id, type: 'node', path, children: [], box: blank(), floor: 0, node: n, appears: n.appears, disappears: n.disappears };
60
+ if (!add(p, parent)) return;
61
+ (n.nodes ?? []).forEach((c, i) => addNode(c, `${path}/nodes/${i}`, p));
62
+ }
63
+ function addRegion(r: RawRegion, path: string, parent: Placed | undefined) {
64
+ const p: Placed = { id: r.id, type: 'region', path, children: [], box: blank(), floor: 0, region: r, appears: r.appears, disappears: r.disappears };
65
+ if (!add(p, parent)) return;
66
+ // nodes first: an item with no placement follows the item written before it
67
+ (r.nodes ?? []).forEach((c, i) => addNode(c, `${path}/nodes/${i}`, p));
68
+ (r.regions ?? []).forEach((c, i) => addRegion(c, `${path}/regions/${i}`, p));
69
+ }
70
+ (scene.regions ?? []).forEach((r, i) => addRegion(r, `${scenePath}/regions/${i}`, undefined));
71
+ (scene.nodes ?? []).forEach((n, i) => addNode(n, `${scenePath}/nodes/${i}`, undefined));
72
+ // (at scene level regions come first: the big slabs usually lead, and people and pads follow them)
73
+
74
+ const ids = () => byId.keys();
75
+ const regionIds = () => items.filter(i => i.type === 'region').map(i => i.id);
76
+
77
+ // ---- 2. `in:` moves a scene-level item into a region; check names ----
78
+ for (const p of [...items]) {
79
+ const raw = p.node ?? p.region!;
80
+ if (raw.in !== undefined) {
81
+ const target = byId.get(raw.in);
82
+ if (!target) { err(p.path + '/in', `unknown region "${raw.in}"` + hintFor(raw.in, regionIds())); continue; }
83
+ if (target.type !== 'region') { err(p.path + '/in', `"${raw.in}" is a node, not a region; put nodes on a node with its own "nodes:" list`); continue; }
84
+ if (p.parent && p.parent !== target) { err(p.path + '/in', `"${p.id}" is written inside "${p.parent.id}" but says in: ${raw.in}; use one or the other`); continue; }
85
+ for (let a: Placed | undefined = target; a; a = a.parent) if (a === p) { err(p.path + '/in', `"${p.id}" cannot be inside itself`); continue; }
86
+ if (!p.parent) { roots.splice(roots.indexOf(p), 1); p.parent = target; target.children.push(p); }
87
+ }
88
+ if (p.node) {
89
+ const a = p.node.asset;
90
+ if (a !== undefined && !nodeKind(catalog, a)) err(p.path + '/asset', `unknown asset "${a}"` + hintFor(a, Object.keys(catalog.nodes)), `assets: ${Object.keys(catalog.nodes).join(', ')}`);
91
+ }
92
+ const role = (p.node ?? p.region)!.role;
93
+ if (role !== undefined && !roles.has(role)) err(p.path + '/role', `unknown colour role "${role}"` + hintFor(role, roles), `add it under "roles:" at the top of the file, or use one of: ${[...roles].join(', ')}`);
94
+ }
95
+ // a walled region's gate decides its node's box
96
+ for (const p of items) {
97
+ const g = p.region?.gate;
98
+ if (!g?.node) continue;
99
+ const gn = byId.get(g.node);
100
+ if (!gn) { err(p.path + '/gate/node', `unknown node "${g.node}"` + hintFor(g.node, ids())); continue; }
101
+ if (gn.type !== 'node') { err(p.path + '/gate/node', `"${g.node}" is a region; a gate is a node`); continue; }
102
+ gn.placedBy = p.id;
103
+ const n = gn.node!;
104
+ if (n.at || REL.some(r => n[r])) err(gn.path, `"${gn.id}" is the gate of "${p.id}", which places it; its own placement is ignored`, undefined, false, 'warning');
105
+ }
106
+
107
+ // ---- 3. sizes and local placement, bottom-up ----
108
+ const gapOf = (raw: { gap?: number }) => raw.gap ?? scene.gap ?? 2;
109
+
110
+ function nodeSize(p: Placed): [number, number, number] {
111
+ const n = p.node!, a = nodeKindOr(catalog, n.asset);
112
+ const s = n.size;
113
+ let w = a.size[0], d = a.size[1], h = a.size[2];
114
+ if (s) { w = s[0]; d = s[1]; if (s.length > 2) h = s[2]; }
115
+ if (p.placedBy) {
116
+ const walled = byId.get(p.placedBy)!.region!, g = walled.gate!;
117
+ const span = g.width ?? 2, thick = s ? Math.min(w, d) : 0.3;
118
+ h = s && s.length > 2 ? h : g.height ?? (walled.height ?? wallHeight(catalog)) + 0.3;
119
+ if (g.side === 'e' || g.side === 'w') { w = thick; d = span; } else { w = span; d = thick; }
120
+ }
121
+ return [w, d, h];
122
+ }
123
+
124
+ function layoutContainer(c: Placed | undefined, kids: Placed[], padding: number, flow: 'row' | 'column', fixed: boolean): { w: number; d: number } {
125
+ // children first, so their sizes are known
126
+ for (const k of kids) layoutItem(k);
127
+ const placed = kids.filter(k => !k.placedBy);
128
+ const sib = new Set(placed);
129
+ const done = new Set<Placed>();
130
+ const order: Placed[] = [];
131
+ const visiting = new Set<Placed>();
132
+ const deps = (k: Placed): Placed[] => {
133
+ const raw = (k.node ?? k.region)!, out: Placed[] = [];
134
+ for (const r of REL) {
135
+ const t = raw[r];
136
+ if (t === undefined) continue;
137
+ const tp = byId.get(t);
138
+ if (!tp) { err(`${k.path}/${r}`, `unknown id "${t}"` + hintFor(t, ids())); continue; }
139
+ if (tp === k) { err(`${k.path}/${r}`, `"${k.id}" cannot be placed ${r} itself`); continue; }
140
+ if (!sib.has(tp)) {
141
+ const where = (x: Placed) => x.parent ? `"${x.parent.id}"` : 'the scene';
142
+ err(`${k.path}/${r}`, `"${t}" is in ${where(tp)} but "${k.id}" is in ${where(k)}; relative placement works between items in the same region`,
143
+ tp.placedBy ? `"${t}" is a gate, placed by its ${byId.get(tp.placedBy)?.region?.kind ?? 'region'}` : `place "${k.id}" relative to something in ${where(k)}, or use "at:"`);
144
+ continue;
145
+ }
146
+ out.push(tp);
147
+ }
148
+ if (!out.length && !raw.at) { const i = placed.indexOf(k); if (i > 0) out.push(placed[i - 1]); }
149
+ return out;
150
+ };
151
+ const visit = (k: Placed) => {
152
+ if (done.has(k)) return;
153
+ if (visiting.has(k)) { err(k.path, `placement loop: "${k.id}" is placed relative to something placed relative to it`); return; }
154
+ visiting.add(k);
155
+ deps(k).forEach(visit);
156
+ visiting.delete(k);
157
+ done.add(k); order.push(k);
158
+ };
159
+ // keep the written order where nothing forces otherwise
160
+ placed.forEach(visit);
161
+
162
+ for (const k of order) {
163
+ const raw = (k.node ?? k.region)!, L = local.get(k)!;
164
+ const rel = REL.find(r => raw[r] !== undefined && byId.get(raw[r]!) && sib.has(byId.get(raw[r]!)!));
165
+ if (raw.at && rel) err(`${k.path}/at`, `"${k.id}" has both "at" and "${rel}"; "at" wins`, undefined, true, 'warning');
166
+ if (raw.at) { L.x = raw.at[0]; L.y = raw.at[1]; }
167
+ else if (rel) relTo(L, local.get(byId.get(raw[rel]!)!)!, rel, gapOf(raw), raw.align ?? 'center');
168
+ else {
169
+ // flow after the item written before it
170
+ const i = placed.indexOf(k);
171
+ if (i > 0) relTo(L, local.get(placed[i - 1])!, flow === 'row' ? 'right-of' : 'below', gapOf(raw), 'center');
172
+ else { L.x = padding; L.y = padding; }
173
+ }
174
+ if (raw.shift) { L.x += raw.shift[0]; L.y += raw.shift[1]; }
175
+ }
176
+
177
+ // fit
178
+ if (fixed || !placed.length) return { w: 0, d: 0 };
179
+ let mx = Infinity, my = Infinity, Mx = -Infinity, My = -Infinity;
180
+ for (const k of placed) { const L = local.get(k)!; mx = Math.min(mx, L.x); my = Math.min(my, L.y); Mx = Math.max(Mx, L.x + L.w); My = Math.max(My, L.y + L.d); }
181
+ const sx = Math.min(0, mx - padding), sy = Math.min(0, my - padding);
182
+ if (sx || sy) for (const k of placed) { const L = local.get(k)!; L.x -= sx; L.y -= sy; }
183
+ void c;
184
+ return { w: Mx - sx + padding, d: My - sy + padding };
185
+ }
186
+
187
+ function relTo(L: Local, T: Local, r: Rel, gap: number, align: 'start' | 'center' | 'end') {
188
+ const alignY = () => { L.y = align === 'start' ? T.y : align === 'end' ? T.y + T.d - L.d : T.y + (T.d - L.d) / 2; };
189
+ const alignX = () => { L.x = align === 'start' ? T.x : align === 'end' ? T.x + T.w - L.w : T.x + (T.w - L.w) / 2; };
190
+ if (r === 'right-of') { L.x = T.x + T.w + gap; alignY(); }
191
+ else if (r === 'left-of') { L.x = T.x - gap - L.w; alignY(); }
192
+ else if (r === 'below') { L.y = T.y + T.d + gap; alignX(); }
193
+ else { L.y = T.y - gap - L.d; alignX(); }
194
+ }
195
+
196
+ function layoutItem(p: Placed) {
197
+ if (p.type === 'node') {
198
+ const [w, d, h] = nodeSize(p);
199
+ local.set(p, { x: 0, y: 0, w, d, h, z: p.node!.z ?? 0 });
200
+ if (p.children.length) layoutContainer(p, p.children, 0, 'row', true);
201
+ return;
202
+ }
203
+ const r = p.region!;
204
+ const pad = r.padding ?? regionKind(catalog, r.kind)?.pad!;
205
+ const fixed = !!r.size;
206
+ const fit = layoutContainer(p, p.children, pad, r.flow ?? 'row', fixed);
207
+ let w = r.size ? r.size[0] : Math.max(fit.w, pad * 2 + 0.5);
208
+ let d = r.size ? r.size[1] : Math.max(fit.d, pad * 2 + 0.5);
209
+ if (!r.size && r.gate) {
210
+ // make way for the gate in its wall
211
+ const need = (r.gate.width ?? 2) + 1;
212
+ if (r.gate.side === 'e' || r.gate.side === 'w') d = Math.max(d, need); else w = Math.max(w, need);
213
+ }
214
+ const h = r.size && r.size.length > 2 ? r.size[2] : r.height ?? regionKind(catalog, r.kind)!.z[1];
215
+ local.set(p, { x: 0, y: 0, w, d, h, z: 0 });
216
+ }
217
+
218
+ layoutContainer(undefined, roots, 0, scene.flow ?? 'row', true);
219
+
220
+ // ---- 4. absolute boxes, top-down ----
221
+ function absolute(p: Placed, ox: number, oy: number, floor: number) {
222
+ const L = local.get(p)!;
223
+ const x0 = ox + L.x, y0 = oy + L.y;
224
+ if (p.type === 'region') {
225
+ const rk = regionKind(catalog, p.region!.kind)!, [dz0] = rk.z, onTop = rk.floor;
226
+ // a slab standing on another slab sits on it: no skirt below its floor
227
+ const dz = rk.shape === 'slab' && regionKind(catalog, p.parent?.region?.kind)?.shape === 'slab' ? 0 : dz0;
228
+ const lift = p.region!.z ?? 0;
229
+ const z0 = floor + dz + lift, z1 = floor + L.h + lift;
230
+ p.box = { x0, y0, z0, x1: x0 + L.w, y1: y0 + L.d, z1 };
231
+ p.floor = onTop ? z1 : floor;
232
+ } else {
233
+ const z0 = floor + L.z;
234
+ p.box = { x0, y0, z0, x1: x0 + L.w, y1: y0 + L.d, z1: z0 + L.h };
235
+ p.floor = p.box.z1;
236
+ }
237
+ for (const k of p.children) if (!k.placedBy) absolute(k, x0, y0, p.floor);
238
+ }
239
+ for (const r of roots) if (!r.placedBy) absolute(r, 0, 0, 0);
240
+
241
+ // ---- snapping: round every edge to the grid. Rounding keeps order, so what was inside stays inside and what
242
+ // did not overlap still does not. Heights are left alone. ----
243
+ if (snap) {
244
+ const q = (v: number) => Math.round(v / snap) * snap;
245
+ const fix = (v: number) => Math.round(v * 1e9) / 1e9;
246
+ for (const p of items) {
247
+ if (p.placedBy) continue;
248
+ const b = p.box;
249
+ let x0 = q(b.x0), x1 = q(b.x1), y0 = q(b.y0), y1 = q(b.y1);
250
+ if (x1 - x0 < snap) x1 = x0 + snap;
251
+ if (y1 - y0 < snap) y1 = y0 + snap;
252
+ p.box = { ...b, x0: fix(x0), x1: fix(x1), y0: fix(y0), y1: fix(y1) };
253
+ const raw = (p.node ?? p.region)!;
254
+ if (raw.at) raw.at.forEach((v, i) => {
255
+ const r = v / snap;
256
+ if (Math.abs(r - Math.round(r)) > 1e-6) err(`${p.path}/at/${i}`, `${v} is not on the ${snap} grid; it is drawn at ${fix(q(v))}`, undefined, false, 'warning');
257
+ });
258
+ }
259
+ }
260
+
261
+ // gates
262
+ for (const f of items) {
263
+ const g = f.region?.gate;
264
+ if (!g?.node) continue;
265
+ const gn = byId.get(g.node);
266
+ if (!gn || gn.placedBy !== f.id) continue;
267
+ gn.box = gateBox(f, local.get(gn)!.h, local.get(gn)!.w, local.get(gn)!.d);
268
+ gn.floor = gn.box.z1;
269
+ }
270
+
271
+ // ---- 5. checks ----
272
+ for (const p of items) {
273
+ const kids = p.children.filter(k => !k.placedBy);
274
+ for (let i = 0; i < kids.length; i++) for (let j = i + 1; j < kids.length; j++) {
275
+ const a = kids[i], b = kids[j];
276
+ if (passive(a, catalog) || passive(b, catalog)) continue;
277
+ if (overlap2(a.box, b.box)) err(b.path, `"${b.id}" overlaps "${a.id}"`, `move one with "at", a relative placement, "gap" or "shift"`, false, 'warning');
278
+ }
279
+ if (p.parent && p.parent.type === 'region' && !p.placedBy) {
280
+ const r = p.parent.box, b = p.box, e = 1e-6;
281
+ if (b.x0 < r.x0 - e || b.y0 < r.y0 - e || b.x1 > r.x1 + e || b.y1 > r.y1 + e) err(p.path, `"${p.id}" sticks out of region "${p.parent.id}"`, `make "${p.parent.id}" bigger, or leave out its "size" so it fits its contents`, false, 'warning');
282
+ }
283
+ }
284
+ {
285
+ const top = roots.filter(x => !x.placedBy);
286
+ for (let i = 0; i < top.length; i++) for (let j = i + 1; j < top.length; j++) {
287
+ const a = top[i], b = top[j];
288
+ if (overlap2(a.box, b.box) && !passive(a, catalog) && !passive(b, catalog))
289
+ err(b.path, `"${b.id}" overlaps "${a.id}"`, `move one with "at", a relative placement, "gap" or "shift"`, false, 'warning');
290
+ }
291
+ }
292
+ const seen = new Set<string>();
293
+ const out = diags.filter(d => { const k = d.path + d.message; if (seen.has(k)) return false; seen.add(k); return true; });
294
+ return { items, byId, roots, diags: out, catalog };
295
+ }
296
+
297
+ /** zones and tunnels overlay other things on purpose */
298
+ const passive = (p: Placed, cat: KindCatalog) => p.type === 'region' && ['zone', 'tunnel'].includes(regionKind(cat, p.region!.kind)?.shape ?? '');
299
+
300
+ export function gateBox(f: Placed, h: number, w: number, d: number): Box {
301
+ const g = f.region!.gate!, b = f.box, base = f.box.z0;
302
+ const at = g.at ?? 0.5, span = g.width ?? 2;
303
+ if (g.side === 'w' || g.side === 'e') {
304
+ const x = g.side === 'w' ? b.x0 + WALL_T / 2 : b.x1 - WALL_T / 2, cy = b.y0 + (b.y1 - b.y0) * at;
305
+ return { x0: x - w / 2, x1: x + w / 2, y0: cy - span / 2, y1: cy + span / 2, z0: base, z1: base + h };
306
+ }
307
+ const y = g.side === 'n' ? b.y0 + WALL_T / 2 : b.y1 - WALL_T / 2, cx = b.x0 + (b.x1 - b.x0) * at;
308
+ return { x0: cx - span / 2, x1: cx + span / 2, y0: y - d / 2, y1: y + d / 2, z0: base, z1: base + h };
309
+ }
310
+
311
+ export function overlap2(a: Box, b: Box, e = 1e-6): boolean {
312
+ return a.x0 < b.x1 - e && b.x0 < a.x1 - e && a.y0 < b.y1 - e && b.y0 < a.y1 - e;
313
+ }
314
+
315
+ function hintFor(word: string, candidates: Iterable<string>): string {
316
+ const d = didYouMean(word, candidates);
317
+ return d ? ` (did you mean "${d}"?)` : '';
318
+ }