@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,125 @@
1
+ 'use client';
2
+
3
+ import { useCallback, useSyncExternalStore } from 'react';
4
+ import {
5
+ newCalcEntry,
6
+ readCalcActive,
7
+ readCalcHistory,
8
+ readCalcStorage,
9
+ readCalcTabs,
10
+ removeCalcHistory,
11
+ subscribeCalcStorage,
12
+ updateCalcTab,
13
+ writeCalcActive,
14
+ writeCalcStorage,
15
+ writeCalcTabs,
16
+ type CalcTreeEntry,
17
+ } from '@terrafirmagreg-team/recipe/lib/calc/storage';
18
+ import type { CalculatorState } from '@terrafirmagreg-team/recipe/types/calc';
19
+
20
+ const EMPTY_TABS: CalcTreeEntry[] = [];
21
+ const EMPTY_CALC: CalculatorState = {
22
+ target: null,
23
+ inventory: [],
24
+ selections: {},
25
+ tagSelections: {},
26
+ };
27
+
28
+ export function useCalcStorage(storageKey: string) {
29
+ const readSnapshot = useCallback(
30
+ () => readCalcStorage(storageKey),
31
+ [storageKey],
32
+ );
33
+ const calcState = useSyncExternalStore(
34
+ subscribeCalcStorage,
35
+ readSnapshot,
36
+ () => null,
37
+ );
38
+ const tabs = useSyncExternalStore(
39
+ subscribeCalcStorage,
40
+ () => readCalcTabs(storageKey),
41
+ () => EMPTY_TABS,
42
+ );
43
+ const activeTabId = useSyncExternalStore(
44
+ subscribeCalcStorage,
45
+ () => readCalcActive(storageKey),
46
+ () => null,
47
+ );
48
+ const history = useSyncExternalStore(
49
+ subscribeCalcStorage,
50
+ () => readCalcHistory(storageKey),
51
+ () => EMPTY_TABS,
52
+ );
53
+
54
+ const persist = useCallback(
55
+ (next: CalculatorState) => {
56
+ writeCalcStorage(storageKey, next);
57
+ },
58
+ [storageKey],
59
+ );
60
+
61
+ const openTab = useCallback(
62
+ (id: string | null) => writeCalcActive(storageKey, id),
63
+ [storageKey],
64
+ );
65
+
66
+ const openEntry = useCallback(
67
+ (entry: CalcTreeEntry) => {
68
+ const list = readCalcTabs(storageKey);
69
+ const existing = list.find((tab) => tab.id === entry.id);
70
+ writeCalcTabs(
71
+ storageKey,
72
+ existing ? list : [...list, entry],
73
+ entry.id,
74
+ );
75
+ },
76
+ [storageKey],
77
+ );
78
+ const closeTab = useCallback(
79
+ (id: string) => {
80
+ const list = readCalcTabs(storageKey).filter((tab) => tab.id !== id);
81
+ const active =
82
+ readCalcActive(storageKey) === id ? null : readCalcActive(storageKey);
83
+ writeCalcTabs(storageKey, list, active);
84
+ },
85
+ [storageKey],
86
+ );
87
+ const newTab = useCallback(() => {
88
+ const entry = newCalcEntry({ ...EMPTY_CALC });
89
+ writeCalcTabs(storageKey, [...readCalcTabs(storageKey), entry], entry.id);
90
+ }, [storageKey]);
91
+ const renameTab = useCallback(
92
+ (id: string, name: string) => {
93
+ updateCalcTab(storageKey, id, (tab) => ({
94
+ ...tab,
95
+ name: name.trim() || undefined,
96
+ }));
97
+ },
98
+ [storageKey],
99
+ );
100
+ const setTabIcon = useCallback(
101
+ (id: string, icon: CalcTreeEntry['icon']) => {
102
+ updateCalcTab(storageKey, id, (tab) => ({ ...tab, icon }));
103
+ },
104
+ [storageKey],
105
+ );
106
+ const removeHistory = useCallback(
107
+ (id: string) => removeCalcHistory(storageKey, id),
108
+ [storageKey],
109
+ );
110
+
111
+ return {
112
+ calcState,
113
+ tabs,
114
+ activeTabId,
115
+ history,
116
+ persist,
117
+ openTab,
118
+ openEntry,
119
+ closeTab,
120
+ newTab,
121
+ renameTab,
122
+ setTabIcon,
123
+ removeHistory,
124
+ };
125
+ }
@@ -0,0 +1,12 @@
1
+ 'use client';
2
+
3
+ import { createContext, useContext } from 'react';
4
+ import type { CalcTier } from '@terrafirmagreg-team/recipe/lib/calc/flow';
5
+
6
+ const CalcTierContext = createContext<CalcTier>('near');
7
+
8
+ export const CalcTierProvider = CalcTierContext.Provider;
9
+
10
+ export function useCalcTier(): CalcTier {
11
+ return useContext(CalcTierContext);
12
+ }
@@ -0,0 +1,23 @@
1
+ 'use client';
2
+
3
+ import { startTransition, useCallback } from 'react';
4
+ import {
5
+ createStoredValue,
6
+ literalCodec,
7
+ } from '@terrafirmagreg-team/ui/lib/utils/stored-value';
8
+ import type { CalcViewMode } from '@terrafirmagreg-team/recipe/lib/calc/flow';
9
+
10
+ const MODES: CalcViewMode[] = ['mini', 'normal', 'detailed'];
11
+ const useStoredMode = createStoredValue(
12
+ 'calc:view',
13
+ literalCodec(MODES, 'normal'),
14
+ );
15
+
16
+ export function useCalcViewMode() {
17
+ const [mode, setStored] = useStoredMode();
18
+ const setMode = useCallback(
19
+ (next: CalcViewMode) => startTransition(() => setStored(next)),
20
+ [setStored],
21
+ );
22
+ return [mode, setMode] as const;
23
+ }
@@ -0,0 +1,269 @@
1
+ 'use client';
2
+
3
+ import {
4
+ startTransition,
5
+ useCallback,
6
+ useEffect,
7
+ useRef,
8
+ useState,
9
+ } from 'react';
10
+ import { proxy, wrap, type Remote } from 'comlink';
11
+ import type {
12
+ CalcWorkerApi,
13
+ CalcWorkerFlush,
14
+ } from '@terrafirmagreg-team/recipe/lib/calc/worker';
15
+ import type { CalcGoal } from '@terrafirmagreg-team/recipe/lib/calc/tree';
16
+ import type { RecipeCompactIndex } from '@terrafirmagreg-team/recipe/types';
17
+ import type { CalcMaterial } from '@terrafirmagreg-team/recipe/types/calc';
18
+ import type { CalcMapsSlice } from './use-maps';
19
+
20
+ type CalcWorkerHandle = {
21
+ worker: Worker;
22
+ api: Remote<CalcWorkerApi>;
23
+ inFlight: number;
24
+ rejecters: Set<(error: Error) => void>;
25
+ };
26
+
27
+ export type ResolvedCacheEntry = {
28
+ inputKey: string;
29
+ goals: CalcGoal[];
30
+ summary: { raw: CalcMaterial[]; catalysts: CalcMaterial[] } | null;
31
+ };
32
+
33
+ export function useCalcWorker({
34
+ dataset,
35
+ catalog,
36
+ activeTabId,
37
+ inputKey,
38
+ applyMaps,
39
+ resetMaps,
40
+ }: {
41
+ dataset: string;
42
+ catalog: RecipeCompactIndex | null;
43
+ activeTabId: string | null;
44
+ inputKey: string;
45
+ applyMaps: (src: CalcMapsSlice, extra?: () => void) => void;
46
+ resetMaps: () => void;
47
+ }) {
48
+ const workerRef = useRef<CalcWorkerHandle | null>(null);
49
+ const [workerState, setWorkerState] = useState<
50
+ 'pending' | 'ready' | 'failed'
51
+ >('pending');
52
+
53
+ const [workerGoals, setWorkerGoals] = useState<CalcGoal[] | null>(null);
54
+ const [workerSummary, setWorkerSummary] = useState<{
55
+ raw: CalcMaterial[];
56
+ catalysts: CalcMaterial[];
57
+ } | null>(null);
58
+ const [workerSettled, setWorkerSettled] = useState(false);
59
+
60
+ const resolvedKeyRef = useRef<string | null>(null);
61
+
62
+ const resolvedCacheRef = useRef(new Map<string, ResolvedCacheEntry>());
63
+
64
+ const [prevActiveTab, setPrevActiveTab] = useState(activeTabId);
65
+ if (prevActiveTab !== activeTabId) {
66
+ setPrevActiveTab(activeTabId);
67
+ setWorkerGoals(null);
68
+ setWorkerSummary(null);
69
+ setWorkerSettled(false);
70
+ }
71
+
72
+ const minFlushEpochRef = useRef(0);
73
+ useEffect(() => {
74
+ resolvedKeyRef.current = null;
75
+ minFlushEpochRef.current += 1;
76
+ const handle = workerRef.current;
77
+ if (handle) void handle.api.invalidate().catch(() => {});
78
+ }, [activeTabId]);
79
+
80
+ useEffect(() => {
81
+ if (!workerSettled || !activeTabId) return;
82
+ if (resolvedKeyRef.current !== inputKey) return;
83
+ resolvedCacheRef.current.set(activeTabId, {
84
+ inputKey,
85
+ goals: workerGoals ?? [],
86
+ summary: workerSummary,
87
+ });
88
+ }, [workerSettled, workerGoals, workerSummary, activeTabId, inputKey]);
89
+
90
+ const applyWorkerFlush = useCallback(
91
+ (msg: CalcWorkerFlush) => {
92
+
93
+ const stale = msg.epoch < minFlushEpochRef.current;
94
+ applyMaps(
95
+ {
96
+ out: new Map(msg.out),
97
+ tag: new Map(msg.tag),
98
+ recipes: new Map(msg.recipes),
99
+ bodies: new Map(msg.bodies),
100
+ dead: new Set(msg.dead),
101
+ },
102
+ () => {
103
+ if (!stale) {
104
+ if (msg.goals) setWorkerGoals(msg.goals);
105
+ if (msg.summary) setWorkerSummary(msg.summary);
106
+ }
107
+ },
108
+ );
109
+ },
110
+ [applyMaps],
111
+ );
112
+
113
+ const bufferedFlush = useRef<CalcWorkerFlush | null>(null);
114
+ const flushTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
115
+ const drainFlush = useCallback(() => {
116
+ flushTimer.current = null;
117
+ const msg = bufferedFlush.current;
118
+ bufferedFlush.current = null;
119
+ if (msg) applyWorkerFlush(msg);
120
+ }, [applyWorkerFlush]);
121
+ const scheduleWorkerFlush = useCallback(
122
+ (msg: CalcWorkerFlush) => {
123
+ const prev = bufferedFlush.current;
124
+ bufferedFlush.current = prev
125
+ ? {
126
+ out: [...prev.out, ...msg.out],
127
+ tag: [...prev.tag, ...msg.tag],
128
+ recipes: [...prev.recipes, ...msg.recipes],
129
+ bodies: [...prev.bodies, ...msg.bodies],
130
+ dead: [...prev.dead, ...msg.dead],
131
+
132
+ epoch: Math.max(prev.epoch, msg.epoch),
133
+ goals:
134
+ msg.epoch > prev.epoch
135
+ ? msg.goals
136
+ : msg.epoch === prev.epoch
137
+ ? (msg.goals ?? prev.goals)
138
+ : prev.goals,
139
+ summary:
140
+ msg.epoch > prev.epoch
141
+ ? msg.summary
142
+ : msg.epoch === prev.epoch
143
+ ? (msg.summary ?? prev.summary)
144
+ : prev.summary,
145
+ }
146
+ : msg;
147
+ if (flushTimer.current) return;
148
+ flushTimer.current = setTimeout(drainFlush, 350);
149
+ },
150
+ [drainFlush],
151
+ );
152
+
153
+ useEffect(() => {
154
+ if (!catalog) return;
155
+ if (typeof Worker === 'undefined') {
156
+ queueMicrotask(() => setWorkerState('failed'));
157
+ return;
158
+ }
159
+ let worker: Worker;
160
+ try {
161
+ worker = new Worker(
162
+ new URL('../../lib/calc/worker/index.ts', import.meta.url),
163
+ { type: 'module' },
164
+ );
165
+ } catch {
166
+ queueMicrotask(() => setWorkerState('failed'));
167
+ return;
168
+ }
169
+ const fail = () => {
170
+ workerRef.current = null;
171
+ worker.terminate();
172
+ setWorkerState('failed');
173
+ setWorkerGoals(null);
174
+ setWorkerSummary(null);
175
+ setWorkerSettled(false);
176
+ };
177
+ const handle: CalcWorkerHandle = {
178
+ worker,
179
+ api: wrap<CalcWorkerApi>(worker),
180
+ inFlight: 0,
181
+ rejecters: new Set(),
182
+ };
183
+ worker.onerror = () => {
184
+ fail();
185
+ const error = new Error('calc worker failed');
186
+ for (const reject of handle.rejecters) reject(error);
187
+ handle.rejecters.clear();
188
+ };
189
+ handle.api
190
+ .init(dataset, catalog, proxy(scheduleWorkerFlush))
191
+ .then(() => setWorkerState('ready'))
192
+ .catch(() => fail());
193
+ workerRef.current = handle;
194
+ resolvedKeyRef.current = null;
195
+ resolvedCacheRef.current.clear();
196
+ minFlushEpochRef.current = 0;
197
+ resetMaps();
198
+ startTransition(() => {
199
+ setWorkerGoals(null);
200
+ setWorkerSummary(null);
201
+ setWorkerSettled(false);
202
+ });
203
+ return () => {
204
+ if (flushTimer.current) {
205
+ clearTimeout(flushTimer.current);
206
+ flushTimer.current = null;
207
+ }
208
+ bufferedFlush.current = null;
209
+ workerRef.current = null;
210
+ worker.terminate();
211
+ };
212
+ }, [dataset, catalog, scheduleWorkerFlush, resetMaps]);
213
+
214
+ const call = useCallback(
215
+ <T,>(fn: (api: Remote<CalcWorkerApi>) => Promise<T>): Promise<T> => {
216
+ const handle = workerRef.current;
217
+ if (!handle) {
218
+ return Promise.reject(new Error('calc worker unavailable'));
219
+ }
220
+ handle.inFlight += 1;
221
+ setWorkerSettled(false);
222
+ return new Promise<T>((resolve, reject) => {
223
+ handle.rejecters.add(reject);
224
+ fn(handle.api)
225
+ .then(resolve)
226
+ .catch(reject)
227
+ .finally(() => {
228
+ handle.rejecters.delete(reject);
229
+ handle.inFlight -= 1;
230
+ if (handle.inFlight === 0) {
231
+ if (flushTimer.current) {
232
+ clearTimeout(flushTimer.current);
233
+ flushTimer.current = null;
234
+ }
235
+ drainFlush();
236
+ setWorkerSettled(true);
237
+ }
238
+ });
239
+ });
240
+ },
241
+ [drainFlush],
242
+ );
243
+
244
+ const applyResolved = useCallback((entry: ResolvedCacheEntry) => {
245
+ startTransition(() => {
246
+ setWorkerGoals(entry.goals);
247
+ setWorkerSummary(entry.summary);
248
+ setWorkerSettled(true);
249
+ });
250
+ }, []);
251
+
252
+ return {
253
+ ref: workerRef,
254
+ state: workerState,
255
+ goals: workerGoals,
256
+ summary: workerSummary,
257
+ settled: workerSettled,
258
+ call,
259
+ isResolved: useCallback((key: string) => resolvedKeyRef.current === key, []),
260
+ markResolved: useCallback((key: string) => {
261
+ resolvedKeyRef.current = key;
262
+ }, []),
263
+ getCached: useCallback(
264
+ (tabId: string) => resolvedCacheRef.current.get(tabId),
265
+ [],
266
+ ),
267
+ applyResolved,
268
+ };
269
+ }
@@ -0,0 +1,14 @@
1
+ 'use client';
2
+ import { useSyncExternalStore } from 'react';
3
+ import {
4
+ dumpLocation,
5
+ dumpPublicUrl,
6
+ subscribeDumpLocations,
7
+ } from '@terrafirmagreg-team/game/lib/dump/paths';
8
+
9
+ export const ANVIL_GUI_REL = 'assets/tfc/textures/gui/anvil.webp';
10
+
11
+ export function useAnvilGui(dataset: string): string | undefined {
12
+ useSyncExternalStore(subscribeDumpLocations, () => dumpLocation(dataset));
13
+ return dumpPublicUrl(dataset, ANVIL_GUI_REL);
14
+ }
@@ -0,0 +1,134 @@
1
+ 'use client';
2
+
3
+ import { useEffect, useMemo, useRef, useState } from 'react';
4
+ import {
5
+ FORGE_ACTIONS,
6
+ forgeRulesMatch,
7
+ } from '@terrafirmagreg-team/recipe/lib/forge/actions';
8
+ import { solveForgeSteps } from '@terrafirmagreg-team/recipe/lib/forge/solve';
9
+ import type { ForgeRecipe, ForgeRuleAction } from '@terrafirmagreg-team/recipe/types/forge';
10
+
11
+ export type ForgeStep = {
12
+ id: string;
13
+ action: ForgeRuleAction;
14
+ delta: number;
15
+ work: number;
16
+ };
17
+
18
+ function lastActions(applied: readonly number[]): ForgeRuleAction[] {
19
+ return applied
20
+ .slice(-3)
21
+ .map((index) => FORGE_ACTIONS[index].action)
22
+ .reverse();
23
+ }
24
+
25
+ function parseWork(value: string): number | null {
26
+ const parsed = Number(value);
27
+ return Number.isInteger(parsed) && parsed >= 0 && parsed < 150
28
+ ? parsed
29
+ : null;
30
+ }
31
+
32
+ export function useForgeState(recipe: ForgeRecipe) {
33
+ const [startInput, setStartInput] = useState('0');
34
+ const [targetInput, setTargetInput] = useState('');
35
+ const [applied, setApplied] = useState<number[]>([]);
36
+ const [expanded, setExpanded] = useState(false);
37
+ const [failed, setFailed] = useState(false);
38
+ const [solving, setSolving] = useState(false);
39
+ const replayTimer = useRef<ReturnType<typeof setInterval> | null>(null);
40
+ const startWork = parseWork(startInput) ?? 0;
41
+ const target = targetInput === '' ? null : (parseWork(targetInput) ?? 0);
42
+
43
+ const { work, steps } = useMemo(() => {
44
+ let work = startWork;
45
+ const steps: ForgeStep[] = [];
46
+ for (const index of applied) {
47
+ const action = FORGE_ACTIONS[index];
48
+ work += action.delta;
49
+ steps.push({ ...action, work });
50
+ }
51
+ return { work, steps };
52
+ }, [startWork, applied]);
53
+ const last = lastActions(applied);
54
+ const finished = work === target && forgeRulesMatch(recipe.rules, last);
55
+
56
+ useEffect(
57
+ () => () => {
58
+ if (replayTimer.current) clearInterval(replayTimer.current);
59
+ },
60
+ [],
61
+ );
62
+
63
+ const push = (index: number) => {
64
+ const next = work + FORGE_ACTIONS[index].delta;
65
+ if (next < 0 || next >= 150 || solving) return;
66
+ setFailed(false);
67
+ setApplied((prev) => [...prev, index]);
68
+ };
69
+ const solve = () => {
70
+ if (solving) return;
71
+ const found = solveForgeSteps(
72
+ { work: startWork, last: [] },
73
+ target ?? 0,
74
+ recipe.rules,
75
+ );
76
+ if (!found) return setFailed(true);
77
+ setFailed(false);
78
+ setApplied([]);
79
+ setExpanded(false);
80
+ setSolving(true);
81
+ let i = 0;
82
+ replayTimer.current = setInterval(() => {
83
+ const step = found[i];
84
+ i += 1;
85
+ if (step === undefined) {
86
+ if (replayTimer.current) clearInterval(replayTimer.current);
87
+ replayTimer.current = null;
88
+ setSolving(false);
89
+ return;
90
+ }
91
+ setApplied((prev) => [...prev, step]);
92
+ }, 170);
93
+ };
94
+
95
+ const ruleMet = useMemo(() => {
96
+ const last = lastActions(applied);
97
+ return recipe.rules.map(
98
+ (rule) => applied.length > 0 && forgeRulesMatch([rule], last),
99
+ );
100
+ }, [recipe, applied]);
101
+
102
+ const setStart = (value: string | number) => {
103
+ setStartInput(String(value));
104
+ setApplied([]);
105
+ };
106
+ const setTarget = (value: string | number) => {
107
+ setTargetInput(String(value));
108
+ setFailed(false);
109
+ };
110
+
111
+ return {
112
+ startInput,
113
+ targetInput,
114
+ applied,
115
+ expanded,
116
+ failed,
117
+ solving,
118
+ target,
119
+ work,
120
+ steps,
121
+ last,
122
+ finished,
123
+ ruleMet,
124
+ push,
125
+ solve,
126
+ setStart,
127
+ setTarget,
128
+ setTargetInput,
129
+ setApplied,
130
+ setExpanded,
131
+ };
132
+ }
133
+
134
+ export type ForgeState = ReturnType<typeof useForgeState>;
@@ -0,0 +1,99 @@
1
+ 'use client';
2
+
3
+ import { useRef, useState } from 'react';
4
+
5
+ export const RULER = { numbersTop: 88, markerX: 13, trackBottom: 110 } as const;
6
+
7
+ export function useRuler({
8
+ panelRef,
9
+ scale,
10
+ work,
11
+ target,
12
+ solving,
13
+ onWork,
14
+ onTarget,
15
+ }: {
16
+ panelRef: React.RefObject<HTMLDivElement | null>;
17
+ scale: number;
18
+ work: number;
19
+ target: number | null;
20
+ solving: boolean;
21
+ onWork: (value: number) => void;
22
+ onTarget: (value: number) => void;
23
+ }) {
24
+ const [dragging, setDragging] = useState(false);
25
+ const dragKind = useRef<'work' | 'target' | null>(null);
26
+ const applyRuler = (value: number) => {
27
+ if (dragKind.current === 'work') {
28
+ onWork(value);
29
+ } else {
30
+ onTarget(value);
31
+ }
32
+ };
33
+ const rulerValue = (clientX: number): number => {
34
+ const panel = panelRef.current?.getBoundingClientRect();
35
+ if (!panel) return 0;
36
+ const atlasX = (clientX - panel.left) / scale;
37
+ return Math.max(0, Math.min(149, Math.round(atlasX - RULER.markerX)));
38
+ };
39
+ const beginDrag = (
40
+ event: React.PointerEvent<HTMLDivElement>,
41
+ kind: 'work' | 'target',
42
+ ) => {
43
+ if (solving) return;
44
+ dragKind.current = kind;
45
+ applyRuler(rulerValue(event.clientX));
46
+ try {
47
+ event.currentTarget.setPointerCapture(event.pointerId);
48
+ } catch {
49
+
50
+ }
51
+ setDragging(true);
52
+ };
53
+
54
+ const onRulerDown = (event: React.PointerEvent<HTMLDivElement>) => {
55
+ const value = rulerValue(event.clientX);
56
+ const nearWork = Math.abs(value - work) <= 4;
57
+ const nearTarget = target !== null && Math.abs(value - target) <= 4;
58
+ beginDrag(
59
+ event,
60
+ nearWork &&
61
+ (!nearTarget || Math.abs(value - work) <= Math.abs(value - target))
62
+ ? 'work'
63
+ : 'target',
64
+ );
65
+ };
66
+ const onRulerMove = (event: React.PointerEvent<HTMLDivElement>) => {
67
+ if (!dragKind.current) return;
68
+ applyRuler(rulerValue(event.clientX));
69
+ };
70
+ const onRulerUp = () => {
71
+ dragKind.current = null;
72
+ setDragging(false);
73
+ };
74
+ const onHandleKey =
75
+ (kind: 'work' | 'target') =>
76
+ (event: React.KeyboardEvent<HTMLDivElement>) => {
77
+ if (event.key !== 'ArrowLeft' && event.key !== 'ArrowRight') return;
78
+ event.preventDefault();
79
+ const step = event.shiftKey ? 10 : 1;
80
+ const base = kind === 'work' ? work : (target ?? 0);
81
+ const value = Math.max(
82
+ 0,
83
+ Math.min(149, base + (event.key === 'ArrowRight' ? step : -step)),
84
+ );
85
+ if (kind === 'work') {
86
+ onWork(value);
87
+ } else {
88
+ onTarget(value);
89
+ }
90
+ };
91
+ return {
92
+ dragging,
93
+ beginDrag,
94
+ onRulerDown,
95
+ onRulerMove,
96
+ onRulerUp,
97
+ onHandleKey,
98
+ };
99
+ }