@terrafirmagreg-team/recipe 0.0.0-stage → 0.1.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 (231) hide show
  1. package/package.json +62 -3
  2. package/src/components/button-copy.tsx +42 -0
  3. package/src/components/cards/card/index.tsx +212 -0
  4. package/src/components/cards/card/model.ts +64 -0
  5. package/src/components/cards/card/scene.tsx +105 -0
  6. package/src/components/cards/card/strip.tsx +196 -0
  7. package/src/components/cards/card/text.tsx +45 -0
  8. package/src/components/cards/card/tips.tsx +62 -0
  9. package/src/components/cards/default-item.tsx +43 -0
  10. package/src/components/cards/fit-card.tsx +26 -0
  11. package/src/components/cards/glare-filter.tsx +28 -0
  12. package/src/components/cards/paged-card.tsx +221 -0
  13. package/src/components/cards/slot-hitbox.tsx +211 -0
  14. package/src/components/cards/strip-card/buttons.tsx +101 -0
  15. package/src/components/cards/strip-card/index.tsx +212 -0
  16. package/src/components/cards/strip-card/member-cell.tsx +165 -0
  17. package/src/components/cards/strip-card/sprite.tsx +35 -0
  18. package/src/components/mdx/recipe.tsx +111 -0
  19. package/src/components/tool-list.tsx +48 -0
  20. package/src/components/views/alloy/index.tsx +105 -0
  21. package/src/components/views/alloy/list.tsx +84 -0
  22. package/src/components/views/alloy/mix/index.tsx +34 -0
  23. package/src/components/views/alloy/mix/ratio.tsx +93 -0
  24. package/src/components/views/alloy/mix/rows.tsx +146 -0
  25. package/src/components/views/alloy/mix/status.tsx +121 -0
  26. package/src/components/views/alloy/mix/total.tsx +131 -0
  27. package/src/components/views/alloy/range-bar.tsx +64 -0
  28. package/src/components/views/alloy/stack-bar.tsx +86 -0
  29. package/src/components/views/browser/axis-switcher.tsx +53 -0
  30. package/src/components/views/browser/card-grid.tsx +274 -0
  31. package/src/components/views/browser/favorite-button.tsx +45 -0
  32. package/src/components/views/browser/frame.tsx +111 -0
  33. package/src/components/views/browser/index.tsx +96 -0
  34. package/src/components/views/browser/item-panel/index.tsx +227 -0
  35. package/src/components/views/browser/item-panel/skeleton.tsx +25 -0
  36. package/src/components/views/browser/item-panel-grid/cell.tsx +176 -0
  37. package/src/components/views/browser/item-panel-grid/favorite-cell.tsx +127 -0
  38. package/src/components/views/browser/item-panel-grid/index.tsx +103 -0
  39. package/src/components/views/browser/item-view/index.tsx +191 -0
  40. package/src/components/views/browser/item-view/tags.tsx +83 -0
  41. package/src/components/views/browser/item-view-header.tsx +130 -0
  42. package/src/components/views/browser/pane-tabs.tsx +60 -0
  43. package/src/components/views/browser/place-header.tsx +71 -0
  44. package/src/components/views/browser/query-controls.tsx +103 -0
  45. package/src/components/views/browser/recipe-item-header.tsx +59 -0
  46. package/src/components/views/browser/settings.tsx +51 -0
  47. package/src/components/views/browser/toolbar.tsx +53 -0
  48. package/src/components/views/browser/type-tabs.tsx +69 -0
  49. package/src/components/views/browser/type-view.tsx +189 -0
  50. package/src/components/views/browser/workstation-rail.tsx +42 -0
  51. package/src/components/views/calc/calculator/actions.tsx +57 -0
  52. package/src/components/views/calc/calculator/help.tsx +148 -0
  53. package/src/components/views/calc/calculator/index.tsx +220 -0
  54. package/src/components/views/calc/dot-overlay.tsx +79 -0
  55. package/src/components/views/calc/edge.tsx +50 -0
  56. package/src/components/views/calc/flow/index.tsx +267 -0
  57. package/src/components/views/calc/flow/picker-dialogs.tsx +68 -0
  58. package/src/components/views/calc/flow/toolbar.tsx +72 -0
  59. package/src/components/views/calc/flow/view-menu.tsx +221 -0
  60. package/src/components/views/calc/member-picker.tsx +118 -0
  61. package/src/components/views/calc/node-card/actions.tsx +168 -0
  62. package/src/components/views/calc/node-card/badges.tsx +77 -0
  63. package/src/components/views/calc/node-card/chips.tsx +219 -0
  64. package/src/components/views/calc/node-card/far.tsx +175 -0
  65. package/src/components/views/calc/node-card/full.tsx +282 -0
  66. package/src/components/views/calc/node-card/index.tsx +26 -0
  67. package/src/components/views/calc/node-card/menu.tsx +211 -0
  68. package/src/components/views/calc/recipe-picker/body.tsx +204 -0
  69. package/src/components/views/calc/recipe-picker/footer.tsx +129 -0
  70. package/src/components/views/calc/recipe-picker/index.tsx +47 -0
  71. package/src/components/views/calc/sidebar.tsx +162 -0
  72. package/src/components/views/calc/target.tsx +271 -0
  73. package/src/components/views/calc/trees/icon-picker.tsx +96 -0
  74. package/src/components/views/calc/trees/index.tsx +133 -0
  75. package/src/components/views/calc/trees/label.tsx +65 -0
  76. package/src/components/views/calc/trees/rows.tsx +139 -0
  77. package/src/components/views/forge/gui.tsx +130 -0
  78. package/src/components/views/forge/index.tsx +105 -0
  79. package/src/components/views/forge/list.tsx +112 -0
  80. package/src/components/views/forge/workbench/controls.tsx +96 -0
  81. package/src/components/views/forge/workbench/index.tsx +84 -0
  82. package/src/components/views/forge/workbench/panel.tsx +227 -0
  83. package/src/components/views/forge/workbench/ruler.tsx +132 -0
  84. package/src/components/views/forge/workbench/status.tsx +54 -0
  85. package/src/components/views/forge/workbench/tape.tsx +69 -0
  86. package/src/hooks/alloy/use-fluid-colors.ts +122 -0
  87. package/src/hooks/alloy/use-mix-state.ts +239 -0
  88. package/src/hooks/calc/flow/engage.ts +89 -0
  89. package/src/hooks/calc/flow/handlers.ts +115 -0
  90. package/src/hooks/calc/flow/layout.ts +149 -0
  91. package/src/hooks/calc/flow/mount.ts +96 -0
  92. package/src/hooks/calc/flow/nodes.ts +251 -0
  93. package/src/hooks/calc/flow/pickers.ts +50 -0
  94. package/src/hooks/calc/flow/props.ts +40 -0
  95. package/src/hooks/calc/flow/viewport.ts +154 -0
  96. package/src/hooks/calc/flow/window.ts +86 -0
  97. package/src/hooks/calc/use-actions.ts +376 -0
  98. package/src/hooks/calc/use-amounts.ts +101 -0
  99. package/src/hooks/calc/use-calc-fill.ts +91 -0
  100. package/src/hooks/calc/use-data.ts +289 -0
  101. package/src/hooks/calc/use-defaults.ts +38 -0
  102. package/src/hooks/calc/use-density.ts +28 -0
  103. package/src/hooks/calc/use-derived.ts +258 -0
  104. package/src/hooks/calc/use-direction.ts +23 -0
  105. package/src/hooks/calc/use-edge-style.ts +40 -0
  106. package/src/hooks/calc/use-far-mode.ts +36 -0
  107. package/src/hooks/calc/use-fill.ts +213 -0
  108. package/src/hooks/calc/use-focus.ts +30 -0
  109. package/src/hooks/calc/use-maps.ts +75 -0
  110. package/src/hooks/calc/use-reset.ts +14 -0
  111. package/src/hooks/calc/use-resolver.ts +234 -0
  112. package/src/hooks/calc/use-session.ts +12 -0
  113. package/src/hooks/calc/use-share.ts +61 -0
  114. package/src/hooks/calc/use-storage.ts +125 -0
  115. package/src/hooks/calc/use-tier.ts +12 -0
  116. package/src/hooks/calc/use-view-mode.ts +23 -0
  117. package/src/hooks/calc/use-worker.ts +269 -0
  118. package/src/hooks/forge/use-anvil-gui.ts +14 -0
  119. package/src/hooks/forge/use-forge-state.ts +134 -0
  120. package/src/hooks/forge/use-ruler.ts +99 -0
  121. package/src/hooks/use-add-to-calc.ts +47 -0
  122. package/src/hooks/use-favorite-hotkey.ts +38 -0
  123. package/src/hooks/use-favorites.ts +134 -0
  124. package/src/hooks/use-group-outlines.ts +64 -0
  125. package/src/hooks/use-hide-unused.ts +11 -0
  126. package/src/hooks/use-index.ts +68 -0
  127. package/src/hooks/use-item-feeds.ts +44 -0
  128. package/src/hooks/use-item-pane.ts +104 -0
  129. package/src/hooks/use-item-press.ts +85 -0
  130. package/src/hooks/use-item-tag-groups.ts +52 -0
  131. package/src/hooks/use-item-tooltips.ts +16 -0
  132. package/src/hooks/use-left-sidebar-tree.ts +26 -0
  133. package/src/hooks/use-mod-names.ts +15 -0
  134. package/src/hooks/use-mount-limit.ts +21 -0
  135. package/src/hooks/use-panel-cells.ts +115 -0
  136. package/src/hooks/use-panel-page.ts +51 -0
  137. package/src/hooks/use-panel-tabs.ts +42 -0
  138. package/src/hooks/use-query.ts +184 -0
  139. package/src/hooks/use-session.ts +84 -0
  140. package/src/hooks/use-sidebar-group.ts +20 -0
  141. package/src/hooks/use-tag-members.ts +14 -0
  142. package/src/hooks/use-tool-recipes.ts +25 -0
  143. package/src/hooks/use-type-filter.ts +72 -0
  144. package/src/lib/alloy/alloy.test.ts +186 -0
  145. package/src/lib/alloy/mix.ts +209 -0
  146. package/src/lib/alloy/parse.ts +36 -0
  147. package/src/lib/calc/defaults.ts +45 -0
  148. package/src/lib/calc/flow.ts +279 -0
  149. package/src/lib/calc/focus.ts +14 -0
  150. package/src/lib/calc/meta.ts +15 -0
  151. package/src/lib/calc/pack.ts +184 -0
  152. package/src/lib/calc/resolve.ts +230 -0
  153. package/src/lib/calc/route/buckets.ts +48 -0
  154. package/src/lib/calc/route/geometry.ts +62 -0
  155. package/src/lib/calc/route/index.ts +2 -0
  156. package/src/lib/calc/route/polyline.ts +103 -0
  157. package/src/lib/calc/route/router.ts +270 -0
  158. package/src/lib/calc/storage/entry.ts +89 -0
  159. package/src/lib/calc/storage/index.ts +18 -0
  160. package/src/lib/calc/storage/keys.ts +42 -0
  161. package/src/lib/calc/storage/store.ts +223 -0
  162. package/src/lib/calc/tree/build.ts +196 -0
  163. package/src/lib/calc/tree/context.ts +42 -0
  164. package/src/lib/calc/tree/goal.ts +64 -0
  165. package/src/lib/calc/tree/ideal.ts +36 -0
  166. package/src/lib/calc/tree/index.ts +18 -0
  167. package/src/lib/calc/tree/materials.ts +28 -0
  168. package/src/lib/calc/tree/pick.ts +68 -0
  169. package/src/lib/calc/tree/recipe.ts +103 -0
  170. package/src/lib/calc/tree/share.ts +198 -0
  171. package/src/lib/calc/tree/tree.test.ts +169 -0
  172. package/src/lib/calc/worker/api.ts +33 -0
  173. package/src/lib/calc/worker/fetch.ts +65 -0
  174. package/src/lib/calc/worker/fill.ts +123 -0
  175. package/src/lib/calc/worker/flush.ts +52 -0
  176. package/src/lib/calc/worker/index.ts +8 -0
  177. package/src/lib/calc/worker/protocol.ts +32 -0
  178. package/src/lib/calc/worker/resolve.ts +101 -0
  179. package/src/lib/calc/worker/state.ts +49 -0
  180. package/src/lib/client/recipe-categories.ts +96 -0
  181. package/src/lib/client/recipe.ts +83 -0
  182. package/src/lib/client-index/cache.ts +13 -0
  183. package/src/lib/client-index/catalog.ts +85 -0
  184. package/src/lib/client-index/chrome.ts +180 -0
  185. package/src/lib/client-index/index.ts +19 -0
  186. package/src/lib/client-index/registry.ts +110 -0
  187. package/src/lib/client-index/rows.ts +254 -0
  188. package/src/lib/client-index/tooltips.ts +80 -0
  189. package/src/lib/config.ts +17 -0
  190. package/src/lib/data/category/dto.ts +24 -0
  191. package/src/lib/data/category/load.ts +46 -0
  192. package/src/lib/data/category/parse.ts +70 -0
  193. package/src/lib/data/dto.ts +151 -0
  194. package/src/lib/data/grid.ts +39 -0
  195. package/src/lib/data/parse/entry.ts +84 -0
  196. package/src/lib/data/parse/index.ts +2 -0
  197. package/src/lib/data/parse/layout.ts +135 -0
  198. package/src/lib/data/parse/slots.ts +84 -0
  199. package/src/lib/data/parse/widgets.ts +80 -0
  200. package/src/lib/data/scene.ts +103 -0
  201. package/src/lib/data/slot-io.ts +43 -0
  202. package/src/lib/dump.ts +5 -0
  203. package/src/lib/favorite-hover.ts +12 -0
  204. package/src/lib/fluid-outputs.ts +72 -0
  205. package/src/lib/forge/actions.ts +58 -0
  206. package/src/lib/forge/data.ts +50 -0
  207. package/src/lib/forge/forge.test.ts +123 -0
  208. package/src/lib/forge/solve.ts +79 -0
  209. package/src/lib/group.ts +22 -0
  210. package/src/lib/index-store.ts +64 -0
  211. package/src/lib/links.ts +125 -0
  212. package/src/lib/lookup.ts +117 -0
  213. package/src/lib/panel-outline.ts +85 -0
  214. package/src/lib/panel.ts +104 -0
  215. package/src/lib/preview.ts +28 -0
  216. package/src/lib/recipe-slots.ts +7 -0
  217. package/src/lib/search.ts +138 -0
  218. package/src/lib/sidebar.ts +46 -0
  219. package/src/lib/tree.ts +90 -0
  220. package/src/lib/used-items.ts +24 -0
  221. package/src/locales/en_us.json +228 -0
  222. package/src/locales/ru_ru.json +228 -0
  223. package/src/locales.ts +5 -0
  224. package/src/types/alloy.ts +19 -0
  225. package/src/types/calc.ts +95 -0
  226. package/src/types/forge.ts +20 -0
  227. package/src/types/index.ts +134 -0
  228. package/styles/calc.css +47 -0
  229. package/styles/strip.css +7 -0
  230. package/styles.css +2 -0
  231. package/README.md +0 -3
@@ -0,0 +1,270 @@
1
+ import PF from 'pathfinding';
2
+ import type { Edge } from '@terrafirmagreg-team/ui/lib/canvas/xyflow';
3
+ import type { FlowPoint } from '@terrafirmagreg-team/ui/types/canvas';
4
+ import type { CalcDirection } from '@terrafirmagreg-team/recipe/lib/calc/flow';
5
+ import { createRectBuckets } from './buckets';
6
+ import {
7
+ CELL,
8
+ PAD,
9
+ cellKey,
10
+ keyX,
11
+ keyY,
12
+ portAnchor,
13
+ sameRect,
14
+ toRect,
15
+ zRoute,
16
+ type Rect,
17
+ type RouteNode,
18
+ } from './geometry';
19
+ import { cellsToPolyline, orthoPoints, simplifyCells } from './polyline';
20
+
21
+ const WINDOW = 160;
22
+
23
+ const MAX_CELLS = 60_000;
24
+
25
+ const STUB = 24;
26
+
27
+ type Committed = { keys: number[]; x0: number; y0: number; x1: number; y1: number };
28
+
29
+ type EdgeState = { points: FlowPoint[]; committed?: Committed };
30
+
31
+ export function createCircuitRouter(): (
32
+ nodes: RouteNode[],
33
+ edges: Pick<Edge, 'id' | 'source' | 'target'>[],
34
+ direction: CalcDirection,
35
+ ) => Map<string, FlowPoint[]> {
36
+ let prevDir: CalcDirection | null = null;
37
+ let prevRects = new Map<string, Rect>();
38
+ let prevEdges = new Map<string, EdgeState>();
39
+
40
+ return (nodes, edges, direction) => {
41
+ const out = new Map<string, FlowPoint[]>();
42
+ const nextEdges = new Map<string, EdgeState>();
43
+ if (!edges.length) {
44
+ prevDir = direction;
45
+ prevEdges = nextEdges;
46
+ prevRects = new Map(nodes.map((n) => [n.id, toRect(n)]));
47
+ return out;
48
+ }
49
+
50
+ const byId = new Map(nodes.map((node) => [node.id, node]));
51
+ const rects = new Map(nodes.map((node) => [node.id, toRect(node)]));
52
+ const rectsInWindow = createRectBuckets(rects.values());
53
+
54
+ const rebuild = prevDir !== direction;
55
+ const committed: Committed[] = [];
56
+ const used = new Set<number>();
57
+ const pending: {
58
+ edge: Pick<Edge, 'id' | 'source' | 'target'>;
59
+ s: RouteNode;
60
+ t: RouteNode;
61
+ a: FlowPoint;
62
+ b: FlowPoint;
63
+ dist: number;
64
+ }[] = [];
65
+
66
+ for (const edge of edges) {
67
+ const s = byId.get(edge.source);
68
+ const t = byId.get(edge.target);
69
+ const sRect = rects.get(edge.source);
70
+ const tRect = rects.get(edge.target);
71
+ const prev = prevEdges.get(edge.id);
72
+ if (
73
+ !rebuild &&
74
+ prev &&
75
+ sRect &&
76
+ tRect &&
77
+ prevRects.get(edge.source) &&
78
+ prevRects.get(edge.target) &&
79
+ sameRect(prevRects.get(edge.source)!, sRect) &&
80
+ sameRect(prevRects.get(edge.target)!, tRect)
81
+ ) {
82
+
83
+ out.set(edge.id, prev.points);
84
+ nextEdges.set(edge.id, prev);
85
+ if (prev.committed) {
86
+ committed.push(prev.committed);
87
+ for (const key of prev.committed.keys) used.add(key);
88
+ }
89
+ continue;
90
+ }
91
+ if (!s || !t || !sRect || !tRect) continue;
92
+ const a = portAnchor(s, 'src', direction);
93
+ const b = portAnchor(t, 'tgt', direction);
94
+ pending.push({
95
+ edge,
96
+ s,
97
+ t,
98
+ a,
99
+ b,
100
+ dist: Math.abs(a.x - b.x) + Math.abs(a.y - b.y),
101
+ });
102
+ }
103
+
104
+ pending.sort((x, y) => x.dist - y.dist || x.edge.id.localeCompare(y.edge.id));
105
+
106
+ const finder = new PF.AStarFinder({
107
+ diagonalMovement: PF.DiagonalMovement.Never,
108
+ });
109
+
110
+ for (const job of pending) {
111
+ const { a, b } = job;
112
+ const horizontal = direction === 'horizontal';
113
+
114
+ const gap = horizontal ? a.x - b.x : a.y - b.y;
115
+ if (gap > 0 && (horizontal ? a.y === b.y : a.x === b.x)) {
116
+
117
+ const points = [a, b];
118
+ out.set(job.edge.id, points);
119
+ nextEdges.set(job.edge.id, { points });
120
+ continue;
121
+ }
122
+
123
+ const stub = gap > 0 ? Math.min(STUB, gap / 2) : STUB;
124
+ const sa = horizontal
125
+ ? { x: a.x - stub, y: a.y }
126
+ : { x: a.x, y: a.y - stub };
127
+ const sb = horizontal
128
+ ? { x: b.x + stub, y: b.y }
129
+ : { x: b.x, y: b.y + stub };
130
+ const wx0 = Math.min(sa.x, sb.x) - WINDOW;
131
+ const wy0 = Math.min(sa.y, sb.y) - WINDOW;
132
+ const wx1 = Math.max(sa.x, sb.x) + WINDOW;
133
+ const wy1 = Math.max(sa.y, sb.y) + WINDOW;
134
+ const ox = Math.floor(wx0 / CELL);
135
+ const oy = Math.floor(wy0 / CELL);
136
+ const gw = Math.ceil(wx1 / CELL) - ox + 1;
137
+ const gh = Math.ceil(wy1 / CELL) - oy + 1;
138
+
139
+ let cells: number[][] | null = null;
140
+ let passGrid: InstanceType<typeof PF.Grid> | null = null;
141
+ if (gw > 0 && gh > 0 && gw * gh <= MAX_CELLS) {
142
+ const base = new PF.Grid(gw, gh);
143
+ for (const rect of rectsInWindow(wx0, wy0, wx1, wy1)) {
144
+ const rx0 = Math.max(0, Math.floor((rect.x - PAD) / CELL) - ox);
145
+ const ry0 = Math.max(0, Math.floor((rect.y - PAD) / CELL) - oy);
146
+ const rx1 = Math.min(
147
+ gw - 1,
148
+ Math.ceil((rect.x + rect.w + PAD) / CELL) - ox - 1,
149
+ );
150
+ const ry1 = Math.min(
151
+ gh - 1,
152
+ Math.ceil((rect.y + rect.h + PAD) / CELL) - oy - 1,
153
+ );
154
+ for (let cy = ry0; cy <= ry1; cy += 1) {
155
+ for (let cx = rx0; cx <= rx1; cx += 1) {
156
+ base.setWalkableAt(cx, cy, false);
157
+ }
158
+ }
159
+ }
160
+ const sCell = {
161
+ x: Math.min(gw - 1, Math.max(0, Math.round(sa.x / CELL) - ox)),
162
+ y: Math.min(gh - 1, Math.max(0, Math.round(sa.y / CELL) - oy)),
163
+ };
164
+ const tCell = {
165
+ x: Math.min(gw - 1, Math.max(0, Math.round(sb.x / CELL) - ox)),
166
+ y: Math.min(gh - 1, Math.max(0, Math.round(sb.y / CELL) - oy)),
167
+ };
168
+ for (let pass = 0; pass < 2 && !cells; pass += 1) {
169
+ const grid = base.clone();
170
+ if (pass === 0) {
171
+
172
+ for (const c of committed) {
173
+ if (
174
+ c.x1 < ox ||
175
+ c.x0 > ox + gw - 1 ||
176
+ c.y1 < oy ||
177
+ c.y0 > oy + gh - 1
178
+ ) {
179
+ continue;
180
+ }
181
+ for (const key of c.keys) {
182
+ const cx = keyX(key) - ox;
183
+ const cy = keyY(key) - oy;
184
+ if (cx >= 0 && cx < gw && cy >= 0 && cy < gh) {
185
+ grid.setWalkableAt(cx, cy, false);
186
+ }
187
+ }
188
+ }
189
+ }
190
+ grid.setWalkableAt(sCell.x, sCell.y, true);
191
+ grid.setWalkableAt(tCell.x, tCell.y, true);
192
+ const path = finder.findPath(
193
+ sCell.x,
194
+ sCell.y,
195
+ tCell.x,
196
+ tCell.y,
197
+ grid,
198
+ );
199
+ if (path.length) {
200
+ cells = path.map(([cx, cy]) => [cx + ox, cy + oy]);
201
+ passGrid = grid;
202
+ }
203
+ }
204
+ }
205
+
206
+ if (!cells || !passGrid) {
207
+ const points = orthoPoints(zRoute(a, b, sa, sb, direction), direction);
208
+ out.set(job.edge.id, points);
209
+ nextEdges.set(job.edge.id, { points });
210
+ continue;
211
+ }
212
+
213
+ const g = passGrid;
214
+ const simple = simplifyCells(
215
+ cells,
216
+ (x, y) =>
217
+ x - ox >= 0 &&
218
+ x - ox < gw &&
219
+ y - oy >= 0 &&
220
+ y - oy < gh &&
221
+ g.isWalkableAt(x - ox, y - oy),
222
+ );
223
+ const polyline = cellsToPolyline(simple, sa, sb);
224
+ const points = orthoPoints([a, sa, ...polyline, sb, b], direction);
225
+ out.set(job.edge.id, points);
226
+
227
+ const keys: number[] = [];
228
+ let cx0 = Infinity;
229
+ let cy0 = Infinity;
230
+ let cx1 = -Infinity;
231
+ let cy1 = -Infinity;
232
+ const mark = (cx: number, cy: number) => {
233
+ keys.push(cellKey(cx, cy));
234
+ cx0 = Math.min(cx0, cx);
235
+ cy0 = Math.min(cy0, cy);
236
+ cx1 = Math.max(cx1, cx);
237
+ cy1 = Math.max(cy1, cy);
238
+ };
239
+ for (let i = 0; i < simple.length - 1; i += 1) {
240
+ const [fx, fy] = simple[i]!;
241
+ const [tx, ty] = simple[i + 1]!;
242
+ const dx = Math.sign(tx - fx);
243
+ const dy = Math.sign(ty - fy);
244
+ let cx = fx;
245
+ let cy = fy;
246
+ mark(cx, cy);
247
+ while (cx !== tx || cy !== ty) {
248
+ cx += dx;
249
+ cy += dy;
250
+ mark(cx, cy);
251
+ }
252
+ }
253
+ for (const key of keys) used.add(key);
254
+ const entry: Committed = {
255
+ keys,
256
+ x0: cx0 === Infinity ? 0 : cx0,
257
+ y0: cy0 === Infinity ? 0 : cy0,
258
+ x1: cx1 === -Infinity ? 0 : cx1,
259
+ y1: cy1 === -Infinity ? 0 : cy1,
260
+ };
261
+ committed.push(entry);
262
+ nextEdges.set(job.edge.id, { points, committed: entry });
263
+ }
264
+
265
+ prevDir = direction;
266
+ prevRects = rects;
267
+ prevEdges = nextEdges;
268
+ return out;
269
+ };
270
+ }
@@ -0,0 +1,89 @@
1
+ import type { CalculatorState } from '@terrafirmagreg-team/recipe/types/calc';
2
+
3
+ const MAX_HISTORY = 20;
4
+
5
+ export type CalcTreeEntry = {
6
+ id: string;
7
+
8
+ name?: string;
9
+
10
+ icon?: { id: string; kind: 'item' | 'fluid' };
11
+ savedAt: number;
12
+ state: CalculatorState;
13
+ };
14
+
15
+ export const EMPTY_ENTRIES: CalcTreeEntry[] = [];
16
+
17
+ export function newCalcEntry(state: CalculatorState, name?: string): CalcTreeEntry {
18
+ return {
19
+ id: `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 6)}`,
20
+ name,
21
+ savedAt: Date.now(),
22
+ state,
23
+ };
24
+ }
25
+
26
+ const isTargetRow = (t: unknown): boolean =>
27
+ !!t &&
28
+ typeof t === 'object' &&
29
+ typeof (t as { id?: unknown }).id === 'string';
30
+
31
+ export function isCalcState(value: unknown): value is CalculatorState {
32
+ if (!value || typeof value !== 'object' || Array.isArray(value)) return false;
33
+ const row = value as Record<string, unknown>;
34
+ const targetOk = Array.isArray(row.targets)
35
+ ?
36
+ row.targets.every(isTargetRow)
37
+ : row.target === null || row.target === undefined || isTargetRow(row.target);
38
+ return (
39
+ targetOk &&
40
+ !!row.selections &&
41
+ typeof row.selections === 'object' &&
42
+ !!row.tagSelections &&
43
+ typeof row.tagSelections === 'object'
44
+ );
45
+ }
46
+
47
+ export function migrateCalcState(state: CalculatorState): CalculatorState {
48
+ const legacy = (state as CalculatorState & { targets?: unknown }).targets;
49
+ if (state.target !== undefined || !Array.isArray(legacy)) {
50
+ return { inventory: [], ...state, target: state.target ?? null };
51
+ }
52
+ const [first] = legacy as { id: string; kind: 'item' | 'fluid'; amount: number }[];
53
+ const { targets: _drop, ...rest } = state as CalculatorState & {
54
+ targets?: unknown;
55
+ };
56
+ return { inventory: [], ...rest, target: first ?? null };
57
+ }
58
+
59
+ function isEntry(value: unknown): value is CalcTreeEntry {
60
+ if (!value || typeof value !== 'object' || Array.isArray(value)) return false;
61
+ const row = value as Record<string, unknown>;
62
+ const icon = row.icon as Record<string, unknown> | undefined;
63
+ return (
64
+ typeof row.id === 'string' &&
65
+ typeof row.savedAt === 'number' &&
66
+ isCalcState(row.state) &&
67
+ (icon === undefined ||
68
+ (typeof icon === 'object' &&
69
+ icon !== null &&
70
+ typeof icon.id === 'string' &&
71
+ (icon.kind === 'item' || icon.kind === 'fluid')))
72
+ );
73
+ }
74
+
75
+ export const isCalcTabs = (v: unknown): v is CalcTreeEntry[] =>
76
+ Array.isArray(v) && v.every(isEntry);
77
+ export const isCalcHistory = isCalcTabs;
78
+
79
+ export function capHistory(entries: CalcTreeEntry[]): CalcTreeEntry[] {
80
+ const seen = new Set<string>();
81
+ const out: CalcTreeEntry[] = [];
82
+ for (const entry of [...entries].sort((a, b) => b.savedAt - a.savedAt)) {
83
+ if (seen.has(entry.id)) continue;
84
+ seen.add(entry.id);
85
+ out.push(entry);
86
+ if (out.length >= MAX_HISTORY) break;
87
+ }
88
+ return out;
89
+ }
@@ -0,0 +1,18 @@
1
+ export {
2
+ migrateCalcState,
3
+ newCalcEntry,
4
+ type CalcTreeEntry,
5
+ } from './entry';
6
+ export { calcStorageKey } from './keys';
7
+ export {
8
+ readCalcActive,
9
+ readCalcHistory,
10
+ readCalcStorage,
11
+ readCalcTabs,
12
+ removeCalcHistory,
13
+ subscribeCalcStorage,
14
+ updateCalcTab,
15
+ writeCalcActive,
16
+ writeCalcStorage,
17
+ writeCalcTabs,
18
+ } from './store';
@@ -0,0 +1,42 @@
1
+ export const CALC_STATE_EVENT = 'tfg:calc-state';
2
+ const TAB_KEY = 'tfg:calc-tab';
3
+ const ACTIVE_PREFIX = 'calc:active:';
4
+ const HIST_PREFIX = 'calc:hist:';
5
+
6
+ export const LEGACY_PREFIX = 'calc:';
7
+
8
+ let tabId: string | null = null;
9
+
10
+ function calcTabId(): string {
11
+ if (tabId) return tabId;
12
+ try {
13
+ tabId = window.sessionStorage.getItem(TAB_KEY);
14
+ if (!tabId) {
15
+ tabId = `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
16
+ window.sessionStorage.setItem(TAB_KEY, tabId);
17
+ }
18
+ } catch {
19
+ tabId ??= `tab-${Math.random().toString(36).slice(2, 8)}`;
20
+ }
21
+ return tabId;
22
+ }
23
+
24
+ export function calcStorageKey(section: string, dataset: string): string {
25
+ void calcTabId();
26
+ return `calc:tabs:${section}:${dataset}`;
27
+ }
28
+
29
+ export function activeKey(key: string): string {
30
+ return `${ACTIVE_PREFIX}${key}@${calcTabId()}`;
31
+ }
32
+
33
+ export function historyKey(key: string): string {
34
+ return `${HIST_PREFIX}${key}`;
35
+ }
36
+
37
+ export function datasetTail(key: string): string {
38
+ return key.slice('calc:tabs:'.length);
39
+ }
40
+
41
+ export const emitCalcStorage = () =>
42
+ window.dispatchEvent(new Event(CALC_STATE_EVENT));
@@ -0,0 +1,223 @@
1
+ import type { CalculatorState } from '@terrafirmagreg-team/recipe/types/calc';
2
+ import {
3
+ capHistory,
4
+ EMPTY_ENTRIES,
5
+ isCalcHistory,
6
+ isCalcState,
7
+ isCalcTabs,
8
+ migrateCalcState,
9
+ newCalcEntry,
10
+ type CalcTreeEntry,
11
+ } from './entry';
12
+ import {
13
+ activeKey,
14
+ CALC_STATE_EVENT,
15
+ datasetTail,
16
+ emitCalcStorage,
17
+ historyKey,
18
+ LEGACY_PREFIX,
19
+ } from './keys';
20
+
21
+ function readJSON<T>(raw: string | null, guard: (v: unknown) => v is T): T | null {
22
+ if (raw === null) return null;
23
+ try {
24
+ const value = JSON.parse(raw) as unknown;
25
+ return guard(value) ? value : null;
26
+ } catch {
27
+ return null;
28
+ }
29
+ }
30
+
31
+ const snapshots = new Map<string, { raw: string | null; value: unknown }>();
32
+
33
+ function snap<T>(
34
+ storeKey: string,
35
+ raw: string | null,
36
+ guard: (v: unknown) => v is T,
37
+ normalize?: (value: T) => void,
38
+ ): T | null {
39
+ const hit = snapshots.get(storeKey);
40
+ if (hit && hit.raw === raw) return hit.value as T | null;
41
+ const value = readJSON(raw, guard);
42
+ if (value) normalize?.(value);
43
+ snapshots.set(storeKey, { raw, value });
44
+ return value;
45
+ }
46
+
47
+ const migrateEntries = (entries: CalcTreeEntry[]): void => {
48
+ for (const entry of entries) {
49
+ entry.state = migrateCalcState(entry.state);
50
+ }
51
+ };
52
+
53
+ const migrated = new Set<string>();
54
+
55
+ function migrateLegacy(hKey: string, tail: string): void {
56
+ if (migrated.has(hKey) || typeof window === 'undefined') return;
57
+ migrated.add(hKey);
58
+ const legacyKey = `${LEGACY_PREFIX}${tail}`;
59
+ try {
60
+ const legacy = readJSON(
61
+ window.localStorage.getItem(legacyKey),
62
+ isCalcState,
63
+ );
64
+ const migratedState = legacy && migrateCalcState(legacy);
65
+ if (migratedState?.target) {
66
+ const entry = newCalcEntry(migratedState);
67
+ const entries =
68
+ readJSON(window.localStorage.getItem(hKey), isCalcHistory) ?? [];
69
+ window.localStorage.setItem(
70
+ hKey,
71
+ JSON.stringify(capHistory([entry, ...entries])),
72
+ );
73
+ }
74
+ for (let i = window.localStorage.length - 1; i >= 0; i -= 1) {
75
+ const name = window.localStorage.key(i);
76
+ if (name?.startsWith(legacyKey)) window.localStorage.removeItem(name);
77
+ }
78
+ } catch {}
79
+ }
80
+
81
+ export function readCalcTabs(key: string): CalcTreeEntry[] {
82
+ if (typeof window === 'undefined') return [];
83
+ migrateLegacy(historyKey(key), datasetTail(key));
84
+ return (
85
+ snap(key, window.localStorage.getItem(key), isCalcTabs, migrateEntries) ??
86
+ EMPTY_ENTRIES
87
+ );
88
+ }
89
+
90
+ export function readCalcActive(key: string): string | null {
91
+ if (typeof window === 'undefined') return null;
92
+ try {
93
+ return window.sessionStorage.getItem(activeKey(key)) || null;
94
+ } catch {
95
+ return null;
96
+ }
97
+ }
98
+
99
+ export function readCalcHistory(key: string): CalcTreeEntry[] {
100
+ if (typeof window === 'undefined') return [];
101
+ const hKey = historyKey(key);
102
+ migrateLegacy(hKey, datasetTail(key));
103
+ return (
104
+ snap(hKey, window.localStorage.getItem(hKey), isCalcHistory, migrateEntries) ??
105
+ EMPTY_ENTRIES
106
+ );
107
+ }
108
+
109
+ function writeTabsRaw(key: string, tabs: CalcTreeEntry[]): void {
110
+ window.localStorage.setItem(key, JSON.stringify(tabs));
111
+ }
112
+
113
+ function writeHistoryRaw(key: string, entries: CalcTreeEntry[]): void {
114
+ window.localStorage.setItem(
115
+ historyKey(key),
116
+ JSON.stringify(capHistory(entries)),
117
+ );
118
+ }
119
+
120
+ function syncHistory(key: string, tabs: CalcTreeEntry[]): void {
121
+ const live = tabs.filter((tab) => tab.state.target);
122
+ if (!live.length) return;
123
+ const prev = readCalcHistory(key);
124
+ const ids = new Set(live.map((tab) => tab.id));
125
+ writeHistoryRaw(key, [...live, ...prev.filter((e) => !ids.has(e.id))]);
126
+ }
127
+
128
+ export function writeCalcTabs(
129
+ key: string,
130
+ tabs: CalcTreeEntry[],
131
+ active: string | null,
132
+ ): void {
133
+ if (typeof window === 'undefined') return;
134
+ try {
135
+ writeTabsRaw(key, tabs);
136
+ syncHistory(key, tabs);
137
+ window.sessionStorage.setItem(activeKey(key), active ?? '');
138
+ } catch {
139
+ return;
140
+ }
141
+ emitCalcStorage();
142
+ }
143
+
144
+ export function writeCalcActive(key: string, id: string | null): void {
145
+ if (typeof window === 'undefined') return;
146
+ try {
147
+ window.sessionStorage.setItem(activeKey(key), id ?? '');
148
+ } catch {
149
+ return;
150
+ }
151
+ emitCalcStorage();
152
+ }
153
+
154
+ export function updateCalcTab(
155
+ key: string,
156
+ id: string,
157
+ update: (entry: CalcTreeEntry) => CalcTreeEntry,
158
+ ): void {
159
+ if (typeof window === 'undefined') return;
160
+ const tabs = readCalcTabs(key);
161
+ const index = tabs.findIndex((tab) => tab.id === id);
162
+ if (index < 0) return;
163
+ const next = tabs.slice();
164
+ next[index] = update(next[index]!);
165
+ try {
166
+ writeTabsRaw(key, next);
167
+ syncHistory(key, next);
168
+ } catch {
169
+ return;
170
+ }
171
+ emitCalcStorage();
172
+ }
173
+
174
+ export function removeCalcHistory(key: string, id: string): void {
175
+ if (typeof window === 'undefined') return;
176
+ try {
177
+ writeHistoryRaw(
178
+ key,
179
+ readCalcHistory(key).filter((entry) => entry.id !== id),
180
+ );
181
+ const tabs = readCalcTabs(key).filter((tab) => tab.id !== id);
182
+ writeTabsRaw(key, tabs);
183
+ if (readCalcActive(key) === id) {
184
+ window.sessionStorage.setItem(activeKey(key), '');
185
+ }
186
+ } catch {
187
+ return;
188
+ }
189
+ emitCalcStorage();
190
+ }
191
+
192
+ export function readCalcStorage(key: string): CalculatorState | null {
193
+ const tabs = readCalcTabs(key);
194
+ const active = readCalcActive(key);
195
+ return tabs.find((tab) => tab.id === active)?.state ?? null;
196
+ }
197
+
198
+ export function writeCalcStorage(
199
+ key: string,
200
+ state: CalculatorState,
201
+ ): void {
202
+ const tabs = readCalcTabs(key);
203
+ const active = readCalcActive(key);
204
+ const index = tabs.findIndex((tab) => tab.id === active);
205
+ if (index >= 0) {
206
+ const next = tabs.slice();
207
+ next[index] = { ...next[index]!, savedAt: Date.now(), state };
208
+ writeCalcTabs(key, next, active);
209
+ return;
210
+ }
211
+ const entry = newCalcEntry(state);
212
+ writeCalcTabs(key, [...tabs, entry], entry.id);
213
+ }
214
+
215
+ export function subscribeCalcStorage(listener: () => void): () => void {
216
+ if (typeof window === 'undefined') return () => {};
217
+ window.addEventListener(CALC_STATE_EVENT, listener);
218
+ window.addEventListener('storage', listener);
219
+ return () => {
220
+ window.removeEventListener(CALC_STATE_EVENT, listener);
221
+ window.removeEventListener('storage', listener);
222
+ };
223
+ }