@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,196 @@
1
+ import type { BuildTreeInput, CalcKind, CalcNode } from '@terrafirmagreg-team/recipe/types/calc';
2
+ import { AUTO_MAX_DEPTH, type BuildContext, type RemainderPool } from './context';
3
+ import { materialKey } from './materials';
4
+ import { calcAutoPick } from './pick';
5
+ import { recipeDivisor } from './recipe';
6
+
7
+ const MAX_BUILD_DEPTH = 64;
8
+
9
+ function poolTake(pool: RemainderPool, kind: CalcKind, id: string, want: number) {
10
+ const key = materialKey(kind, id);
11
+ const have = pool.get(key) ?? 0;
12
+ const taken = Math.min(have, want);
13
+ const left = have - taken;
14
+ if (left > 0) pool.set(key, left);
15
+ else pool.delete(key);
16
+ return taken;
17
+ }
18
+
19
+ function poolAdd(pool: RemainderPool, kind: CalcKind, id: string, amount: number) {
20
+ if (amount <= 0) return;
21
+ const key = materialKey(kind, id);
22
+ pool.set(key, (pool.get(key) ?? 0) + amount);
23
+ }
24
+
25
+ function resolveTagMember(ctx: BuildContext, tagId: string) {
26
+ const tag = ctx.tagMembers(tagId);
27
+ if (!tag || !tag.members.length) return undefined;
28
+ const picked = ctx.tagSelections[tagId];
29
+ const memberId =
30
+ picked && tag.members.includes(picked) ? picked : tag.members[0]!;
31
+ return { memberId, members: tag.members, kind: tag.kind };
32
+ }
33
+
34
+ export function buildTree(input: BuildTreeInput, ctx: BuildContext): CalcNode {
35
+ const { materialId, kind, amount, depth, tagId, catalyst, consumeChance, path } =
36
+ input;
37
+ const matKey = materialKey(kind, materialId);
38
+ const availableRecipes = ctx.getRecipesForItem(materialId, kind);
39
+ const tag = tagId ? resolveTagMember(ctx, tagId) : undefined;
40
+
41
+ const base: CalcNode = {
42
+ materialId,
43
+ kind,
44
+ amount,
45
+ satisfied: 0,
46
+ need: amount,
47
+ recipe: null,
48
+ batches: 0,
49
+ produced: 0,
50
+ cycle: false,
51
+ catalyst: catalyst === true,
52
+ children: [],
53
+ availableRecipes,
54
+ depth,
55
+ ...(path ? { path } : {}),
56
+ ...(consumeChance !== undefined && consumeChance < 1
57
+ ? { consumeChance }
58
+ : {}),
59
+ ...(tagId ? { tagId, members: tag?.members } : {}),
60
+ };
61
+
62
+ const userCollapsed =
63
+ path != null && (ctx.collapsed?.has(path) ?? false);
64
+ const underAuto =
65
+ path != null &&
66
+ (ctx.autoRoots?.some(
67
+ (root) => path === root || path.startsWith(`${root}/`),
68
+ ) ??
69
+ false);
70
+ const auto =
71
+ underAuto &&
72
+ depth < AUTO_MAX_DEPTH &&
73
+ (ctx.autoBudget ? ctx.autoBudget.left-- > 0 : true);
74
+ const pickOnly = !auto || userCollapsed;
75
+ let selectedRecipeId = pickOnly
76
+ ? path
77
+ ? ctx.selections[path]
78
+ : undefined
79
+ : ((path ? ctx.selections[path] : undefined) ??
80
+ ctx.selections[materialId]);
81
+ let pickPending = false;
82
+ if (!availableRecipes.some((row) => row.recipeId === selectedRecipeId)) {
83
+ if (pickOnly) {
84
+ selectedRecipeId = undefined;
85
+ } else {
86
+ const autoPick = calcAutoPick(
87
+ availableRecipes,
88
+ materialId,
89
+ ctx.usedMaterials,
90
+ ctx,
91
+ );
92
+ selectedRecipeId = autoPick.pick;
93
+ pickPending = !autoPick.pick;
94
+ }
95
+ }
96
+ const recipe = selectedRecipeId ? ctx.recipeMap.get(selectedRecipeId) : null;
97
+ base.recipe = recipe ?? null;
98
+ if (underAuto) base.auto = auto;
99
+ if (!recipe && !catalyst && availableRecipes.length > 0 && path != null) {
100
+ if (pickPending) base.loading = true;
101
+ else base.expandable = true;
102
+ } else if (
103
+ !recipe &&
104
+ !availableRecipes.length &&
105
+ path != null &&
106
+ !(ctx.recipesResolved?.(materialId) ?? true)
107
+ ) {
108
+ base.loading = true;
109
+ }
110
+ if (userCollapsed && !recipe) base.collapsed = true;
111
+ if (catalyst && !recipe) return base;
112
+ if (depth >= MAX_BUILD_DEPTH) return base;
113
+
114
+ const satisfied = ctx.pool
115
+ ? poolTake(ctx.pool, kind, materialId, amount)
116
+ : 0;
117
+ const need = amount - satisfied;
118
+ base.satisfied = satisfied;
119
+ base.need = need;
120
+ if (need <= 0) return base;
121
+
122
+ if (!recipe) return base;
123
+ if (
124
+ ctx.used.has(recipe.recipeId) ||
125
+ ctx.usedMaterials.has(matKey)
126
+ ) {
127
+ base.cycle = true;
128
+ return base;
129
+ }
130
+
131
+ const divisor = recipeDivisor(recipe, materialId, kind);
132
+ const batches = Math.ceil(need / divisor);
133
+ const produced = batches * divisor;
134
+ base.batches = batches;
135
+ base.produced = produced;
136
+
137
+ ctx.used.add(recipe.recipeId);
138
+ ctx.usedMaterials.add(matKey);
139
+ for (const inp of recipe.inputs) {
140
+ if (inp.catalyst) {
141
+ if (inp.id) {
142
+ const resolved = inp.tagId
143
+ ? resolveTagMember(ctx, inp.tagId)?.memberId
144
+ : undefined;
145
+ base.children.push(
146
+ buildTree(
147
+ {
148
+ materialId: resolved ?? inp.id,
149
+ kind: inp.kind,
150
+ amount: inp.amount,
151
+ depth: depth + 1,
152
+ tagId: inp.tagId,
153
+ catalyst: true,
154
+ path: path ? `${path}/${base.children.length}` : undefined,
155
+ },
156
+ ctx,
157
+ ),
158
+ );
159
+ }
160
+ continue;
161
+ }
162
+
163
+ const childAmount = inp.amount * batches * (inp.chance ?? 1);
164
+ if (childAmount <= 0 || !inp.id) continue;
165
+ const resolved = inp.tagId ? resolveTagMember(ctx, inp.tagId) : undefined;
166
+ base.children.push(
167
+ buildTree(
168
+ {
169
+ materialId: resolved?.memberId ?? inp.id,
170
+ kind: resolved?.kind ?? inp.kind,
171
+ amount: childAmount,
172
+ depth: depth + 1,
173
+ tagId: inp.tagId,
174
+ consumeChance: inp.chance,
175
+ path: path ? `${path}/${base.children.length}` : undefined,
176
+ },
177
+ ctx,
178
+ ),
179
+ );
180
+ }
181
+ ctx.used.delete(recipe.recipeId);
182
+ ctx.usedMaterials.delete(matKey);
183
+
184
+ poolAdd(ctx.pool, kind, materialId, produced - need);
185
+ for (const out of recipe.outputs) {
186
+ if (out.id !== materialId || out.kind !== kind) {
187
+ poolAdd(
188
+ ctx.pool,
189
+ out.kind,
190
+ out.id,
191
+ out.amount * batches * (out.chance ?? 1),
192
+ );
193
+ }
194
+ }
195
+ return base;
196
+ }
@@ -0,0 +1,42 @@
1
+ import type {
2
+ CalcKind,
3
+ CalcNode,
4
+ CalcMaterial,
5
+ CalcRecipe,
6
+ CalcRecipeSummary,
7
+ } from '@terrafirmagreg-team/recipe/types/calc';
8
+
9
+ export type RemainderPool = Map<string, number>;
10
+
11
+ export type BuildContext = {
12
+ recipeMap: Map<string, CalcRecipe>;
13
+ getRecipesForItem: (
14
+ materialId: string,
15
+ kind: CalcKind,
16
+ ) => CalcRecipeSummary[];
17
+ selections: Record<string, string>;
18
+ tagSelections: Record<string, string>;
19
+ tagMembers: (tagId: string) => { members: string[]; kind: CalcKind } | undefined;
20
+ pool: RemainderPool;
21
+ used: Set<string>;
22
+
23
+ usedMaterials: Set<string>;
24
+
25
+ collapsed?: Set<string>;
26
+
27
+ autoRoots?: readonly string[];
28
+
29
+ autoBudget?: { left: number };
30
+
31
+ recipesResolved?: (materialId: string) => boolean;
32
+
33
+ deadIds?: ReadonlySet<string>;
34
+ };
35
+
36
+ export const AUTO_MAX_DEPTH = 32;
37
+ export const AUTO_NODE_BUDGET = 800;
38
+
39
+ export type CalcGoal = {
40
+ node: CalcNode;
41
+ remainders: CalcMaterial[];
42
+ };
@@ -0,0 +1,64 @@
1
+ import type {
2
+ CalcKind,
3
+ CalcMaterial,
4
+ CalcNode,
5
+ CalculatorTarget,
6
+ } from '@terrafirmagreg-team/recipe/types/calc';
7
+ import { buildTree } from './build';
8
+ import type { BuildContext, CalcGoal, RemainderPool } from './context';
9
+ import { deduplicateMaterials, mergeAmounts } from './materials';
10
+
11
+ export function buildGoal(
12
+ target: CalculatorTarget,
13
+ ctx: Omit<BuildContext, 'pool' | 'used' | 'usedMaterials'>,
14
+ path?: string,
15
+ inventory?: readonly CalcMaterial[],
16
+ ): CalcGoal {
17
+
18
+ const pool: RemainderPool = new Map();
19
+ for (const row of inventory ?? []) {
20
+ if (row.amount > 0) {
21
+ const key = `${row.kind}:${row.id}`;
22
+ pool.set(key, (pool.get(key) ?? 0) + row.amount);
23
+ }
24
+ }
25
+ const node = buildTree(
26
+ {
27
+ materialId: target.id,
28
+ kind: target.kind,
29
+ amount: target.amount,
30
+ depth: 0,
31
+ path,
32
+ },
33
+ { ...ctx, pool, used: new Set(), usedMaterials: new Set() },
34
+ );
35
+ const remainders: CalcMaterial[] = [...pool.entries()].map(([key, amount]) => {
36
+ const sep = key.indexOf(':');
37
+ return { kind: key.slice(0, sep) as CalcKind, id: key.slice(sep + 1), amount };
38
+ });
39
+ return { node, remainders };
40
+ }
41
+
42
+ export function flattenTree(node: CalcNode): {
43
+ cost: CalcMaterial[];
44
+ catalysts: CalcMaterial[];
45
+ } {
46
+ const cost: CalcMaterial[] = [];
47
+ const catalysts: CalcMaterial[] = [];
48
+
49
+ function recurse(n: CalcNode) {
50
+ if (n.catalyst) {
51
+ catalysts.push({ id: n.materialId, kind: n.kind, amount: n.amount });
52
+ return;
53
+ }
54
+ if (n.need <= 0) return;
55
+ if (n.recipe === null || n.cycle) {
56
+ cost.push({ id: n.materialId, kind: n.kind, amount: n.need });
57
+ return;
58
+ }
59
+ for (const child of n.children) recurse(child);
60
+ }
61
+
62
+ recurse(node);
63
+ return { cost: mergeAmounts(cost), catalysts: deduplicateMaterials(catalysts) };
64
+ }
@@ -0,0 +1,36 @@
1
+ import type { CalcNode } from '@terrafirmagreg-team/recipe/types/calc';
2
+ import { recipeDivisor } from './recipe';
3
+
4
+ const SCALE = 1000;
5
+ const CAP = 1_000_000;
6
+
7
+ function gcd(a: number, b: number): number {
8
+ a = Math.round(Math.abs(a));
9
+ b = Math.round(Math.abs(b));
10
+ while (b) {
11
+ const t = a % b;
12
+ a = b;
13
+ b = t;
14
+ }
15
+ return a;
16
+ }
17
+
18
+ export function idealBatchMultiplier(node: CalcNode): number {
19
+ let acc = 1;
20
+ const walk = (n: CalcNode): void => {
21
+ if (!Number.isFinite(acc)) return;
22
+ if (n.recipe && n.need > 0 && !n.cycle && n.batches > 0) {
23
+ const divisor = recipeDivisor(n.recipe, n.materialId, n.kind);
24
+ const mod =
25
+ (divisor * SCALE) / gcd(divisor * SCALE, n.need * SCALE);
26
+ acc = (acc * mod) / gcd(acc, mod);
27
+ if (!Number.isFinite(acc) || acc > CAP) {
28
+ acc = Infinity;
29
+ return;
30
+ }
31
+ }
32
+ for (const child of n.children) walk(child);
33
+ };
34
+ walk(node);
35
+ return acc;
36
+ }
@@ -0,0 +1,18 @@
1
+ export {
2
+ AUTO_MAX_DEPTH,
3
+ AUTO_NODE_BUDGET,
4
+ type BuildContext,
5
+ type CalcGoal,
6
+ type RemainderPool,
7
+ } from './context';
8
+ export { mergeAmounts } from './materials';
9
+ export { recipeDivisor, toCalcRecipe } from './recipe';
10
+ export { calcAutoPick, type AutoPickResult } from './pick';
11
+ export { buildTree } from './build';
12
+ export { buildGoal, flattenTree } from './goal';
13
+ export { idealBatchMultiplier } from './ideal';
14
+ export {
15
+ decodeCalcState,
16
+ encodeCalcShare,
17
+ encodeCalcState,
18
+ } from './share';
@@ -0,0 +1,28 @@
1
+ import type { CalcKind, CalcMaterial } from '@terrafirmagreg-team/recipe/types/calc';
2
+
3
+ export function materialKey(kind: CalcKind, id: string): string {
4
+ return `${kind}:${id}`;
5
+ }
6
+
7
+ export function mergeAmounts(list: CalcMaterial[]): CalcMaterial[] {
8
+ const map = new Map<string, CalcMaterial>();
9
+ for (const row of list) {
10
+ const key = materialKey(row.kind, row.id);
11
+ const existing = map.get(key);
12
+ if (existing) existing.amount += row.amount;
13
+ else map.set(key, { ...row });
14
+ }
15
+ return [...map.values()];
16
+ }
17
+
18
+ export function deduplicateMaterials(list: CalcMaterial[]): CalcMaterial[] {
19
+ const seen = new Set<string>();
20
+ const result: CalcMaterial[] = [];
21
+ for (const row of list) {
22
+ const key = materialKey(row.kind, row.id);
23
+ if (seen.has(key)) continue;
24
+ seen.add(key);
25
+ result.push({ ...row });
26
+ }
27
+ return result;
28
+ }
@@ -0,0 +1,68 @@
1
+ import type { CalcRecipeSummary } from '@terrafirmagreg-team/recipe/types/calc';
2
+ import type { BuildContext } from './context';
3
+
4
+ export type AutoPickResult = {
5
+
6
+ pick?: string;
7
+
8
+ wantRecipes: string[];
9
+
10
+ wantOut: string[];
11
+ };
12
+
13
+ const TIER_WEIGHT = 2;
14
+
15
+ export function calcAutoPick(
16
+ rows: CalcRecipeSummary[],
17
+ materialId: string,
18
+ ancestors: ReadonlySet<string>,
19
+ ctx: Pick<
20
+ BuildContext,
21
+ 'recipeMap' | 'getRecipesForItem' | 'recipesResolved' | 'deadIds'
22
+ >,
23
+ ): AutoPickResult {
24
+ const alive = rows.filter((row) => !ctx.deadIds?.has(row.recipeId));
25
+
26
+ const crafted = alive.filter((row) => !/(^|_)loot(_|$)/.test(row.category));
27
+ const pool = crafted.length ? crafted : alive;
28
+ const isCycle = (input: string) =>
29
+ input === materialId ||
30
+ ancestors.has(`item:${input}`) ||
31
+ ancestors.has(`fluid:${input}`);
32
+ const wantRecipes: string[] = [];
33
+ const wantOut = new Set<string>();
34
+ for (const row of pool) {
35
+ if (!ctx.recipeMap.has(row.recipeId)) wantRecipes.push(row.recipeId);
36
+ for (const input of row.row.in) {
37
+
38
+ if (input.startsWith('#') || isCycle(input)) continue;
39
+ if (!(ctx.recipesResolved?.(input) ?? true)) wantOut.add(input);
40
+ }
41
+ }
42
+ if (wantRecipes.length || wantOut.size) {
43
+ return { wantRecipes, wantOut: [...wantOut] };
44
+ }
45
+ const inputGuess = (input: string): number => {
46
+ if (isCycle(input)) return Infinity;
47
+ if (input.startsWith('#')) return 1;
48
+ return ctx.getRecipesForItem(input, 'item').length ? 1 : 0;
49
+ };
50
+ let best: CalcRecipeSummary | undefined;
51
+ let bestCost = Infinity;
52
+ for (const row of pool) {
53
+ let cost = 1 + TIER_WEIGHT * (ctx.recipeMap.get(row.recipeId)?.tier ?? 0);
54
+ for (const input of new Set(row.row.in)) {
55
+ cost += inputGuess(input);
56
+ if (cost === Infinity) break;
57
+ }
58
+ if (cost < bestCost) {
59
+ best = row;
60
+ bestCost = cost;
61
+ }
62
+ }
63
+ return {
64
+ pick: best?.recipeId ?? pool[0]?.recipeId ?? rows[0]?.recipeId,
65
+ wantRecipes,
66
+ wantOut: [],
67
+ };
68
+ }
@@ -0,0 +1,103 @@
1
+ import type { Recipe, RecipeIngredient } from '@terrafirmagreg-team/recipe/lib/data/dto';
2
+ import type {
3
+ CalcKind,
4
+ CalcMaterial,
5
+ CalcRecipe,
6
+ CalcRecipeInput,
7
+ } from '@terrafirmagreg-team/recipe/types/calc';
8
+
9
+ function slotKind(slot: { kind: RecipeIngredient['kind'] }): CalcKind {
10
+ return slot.kind === 'fluid' ? 'fluid' : 'item';
11
+ }
12
+
13
+ const GT_TIERS: Record<string, number> = {
14
+ ULV: 0,
15
+ LV: 1,
16
+ MV: 2,
17
+ HV: 3,
18
+ EV: 4,
19
+ IV: 5,
20
+ LuV: 6,
21
+ ZPM: 7,
22
+ UV: 8,
23
+ UHV: 9,
24
+ UEV: 10,
25
+ UIV: 11,
26
+ UXV: 12,
27
+ OpV: 13,
28
+ MAX: 14,
29
+ };
30
+
31
+ function recipeTier(recipe: Recipe): number | undefined {
32
+ for (const widget of recipe.widgets) {
33
+ if (widget.type !== 'text' || !widget.component) continue;
34
+ for (const part of [
35
+ widget.component,
36
+ ...(widget.component.extra ?? []),
37
+ ]) {
38
+ if (part.translate !== 'gtceu.recipe.eu') continue;
39
+ for (const arg of part.with ?? []) {
40
+ if (arg.text && arg.text in GT_TIERS) return GT_TIERS[arg.text];
41
+ }
42
+ }
43
+ }
44
+ return undefined;
45
+ }
46
+
47
+ export function toCalcRecipe(recipe: Recipe): CalcRecipe {
48
+ const inputs: CalcRecipeInput[] = recipe.inputs.map((slot) => ({
49
+ id: slot.id,
50
+ kind: slotKind(slot),
51
+ amount: slot.count && slot.count > 0 ? slot.count : 1,
52
+ catalyst: false,
53
+ ...(slot.chance !== undefined ? { chance: slot.chance } : {}),
54
+ ...(slot.kind === 'tag' ? { tagId: slot.id } : {}),
55
+ }));
56
+ for (const widget of recipe.widgets) {
57
+ if (widget.type !== 'slot' || widget.role !== 'catalyst') continue;
58
+ inputs.push({
59
+ id: widget.id,
60
+ kind: widget.kind === 'fluid' ? 'fluid' : 'item',
61
+ amount: 1,
62
+ catalyst: true,
63
+ ...(widget.chance !== undefined ? { chance: widget.chance } : {}),
64
+ ...(widget.kind === 'tag' ? { tagId: widget.id } : {}),
65
+ });
66
+ }
67
+ const merged: CalcRecipeInput[] = [];
68
+ for (const input of inputs) {
69
+ const key = `${input.kind}:${input.id}:${input.catalyst}:${input.tagId ?? ''}:${input.chance ?? ''}`;
70
+ const existing = merged.find(
71
+ (row) =>
72
+ `${row.kind}:${row.id}:${row.catalyst}:${row.tagId ?? ''}:${row.chance ?? ''}` ===
73
+ key,
74
+ );
75
+ if (existing) existing.amount += input.amount;
76
+ else merged.push({ ...input });
77
+ }
78
+ const outputs: CalcMaterial[] = recipe.outputs.map((slot) => ({
79
+ id: slot.id,
80
+ kind: slotKind(slot),
81
+ amount: slot.count && slot.count > 0 ? slot.count : 1,
82
+ ...(slot.chance !== undefined ? { chance: slot.chance } : {}),
83
+ }));
84
+ const tier = recipeTier(recipe);
85
+ return {
86
+ recipeId: recipe.id,
87
+ category: recipe.category ?? recipe.type,
88
+ inputs: merged,
89
+ outputs,
90
+ ...(tier !== undefined ? { tier } : {}),
91
+ };
92
+ }
93
+
94
+ export function recipeDivisor(
95
+ recipe: CalcRecipe,
96
+ materialId: string,
97
+ kind: CalcKind,
98
+ ): number {
99
+ const amount = recipe.outputs
100
+ .filter((out) => out.id === materialId && out.kind === kind)
101
+ .reduce((sum, out) => sum + out.amount * (out.chance ?? 1), 0);
102
+ return amount > 0 ? amount : 1;
103
+ }