| 1 | import { AlchemyStepKind, ALCHEMY_POTION_DEFINITIONS, UNSTABLE_POTION_EFFECTS, CAULDRON_FLAVOR_LINES, CAULDRON_EMPTY_FLAVOR } from "../definitions/alchemy.js"; |
| 2 | import { ITEM_DEFINITIONS, itemDefinition } from "../definitions/items.js"; |
| 3 | import { skillLevel, SkillId } from "../definitions/skills.js"; |
| 4 | |
| 5 | /** Serialize a step for hashing / recipe papers. */ |
| 6 | export function formatAlchemyStep (step) |
| 7 | { |
| 8 | if (step.kind === AlchemyStepKind.insert) |
| 9 | { |
| 10 | const def = itemDefinition(step.itemId); |
| 11 | return `insert ${def?.displayName ?? step.itemKey ?? step.itemId}`; |
| 12 | } |
| 13 | return step.kind; |
| 14 | } |
| 15 | |
| 16 | export function hashAlchemySteps (steps) |
| 17 | { |
| 18 | const text = (steps ?? []).map((step) => |
| 19 | { |
| 20 | if (step.kind === AlchemyStepKind.insert) |
| 21 | { return `insert:${step.itemId ?? step.itemKey}`; } |
| 22 | return step.kind; |
| 23 | }).join("|"); |
| 24 | let hash = 2166136261; |
| 25 | for (let i = 0; i < text.length; i++) |
| 26 | { |
| 27 | hash ^= text.charCodeAt(i); |
| 28 | hash = Math.imul(hash, 16777619); |
| 29 | } |
| 30 | return hash >>> 0; |
| 31 | } |
| 32 | |
| 33 | /** Latest cauldron look line from the step hash (or empty copy). */ |
| 34 | export function cauldronFlavorText (steps) |
| 35 | { |
| 36 | if (!(steps?.length > 0)) |
| 37 | { return CAULDRON_EMPTY_FLAVOR; } |
| 38 | const hash = hashAlchemySteps(steps); |
| 39 | return CAULDRON_FLAVOR_LINES[hash % CAULDRON_FLAVOR_LINES.length]; |
| 40 | } |
| 41 | |
| 42 | export function unstablePotionFromSteps (steps) |
| 43 | { |
| 44 | const hash = hashAlchemySteps(steps); |
| 45 | const index = hash % UNSTABLE_POTION_EFFECTS.length; |
| 46 | const effect = UNSTABLE_POTION_EFFECTS[index]; |
| 47 | return { |
| 48 | potionKey: `unstable:${effect.effectKey}`, |
| 49 | unstable: true, |
| 50 | stepsHash: hash, |
| 51 | displayName: effect.displayName, |
| 52 | color: effect.color, |
| 53 | fillLevel: effect.fillLevel, |
| 54 | effect: effect.effect, |
| 55 | effectKey: effect.effectKey |
| 56 | }; |
| 57 | } |
| 58 | |
| 59 | export function itemIdByKeyMap () |
| 60 | { |
| 61 | const map = new Map(); |
| 62 | for (const def of Object.values(ITEM_DEFINITIONS)) |
| 63 | { |
| 64 | if (def?.key != null) |
| 65 | { map.set(def.key, def.itemId); } |
| 66 | } |
| 67 | return map; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Resolve ingredient itemIds on potion defs that use item keys (after items are loaded). |
| 72 | */ |
| 73 | export function resolvePotionIngredientIds (potion, byKey = itemIdByKeyMap()) |
| 74 | { |
| 75 | const ingredients = (potion.ingredients ?? []).map((row) => |
| 76 | { |
| 77 | const itemId = row.itemId ?? byKey.get(row.key) ?? null; |
| 78 | return { itemId, key: row.key, quantity: row.quantity }; |
| 79 | }); |
| 80 | const steps = (potion.steps ?? []).map((step) => |
| 81 | { |
| 82 | if (step.kind !== AlchemyStepKind.insert) |
| 83 | { return { ...step }; } |
| 84 | const itemId = step.itemId ?? byKey.get(step.itemKey) ?? null; |
| 85 | return { kind: AlchemyStepKind.insert, itemId, itemKey: step.itemKey }; |
| 86 | }); |
| 87 | return { ...potion, ingredients, steps }; |
| 88 | } |
| 89 | |
| 90 | let cachedResolvedPotions = null; |
| 91 | |
| 92 | export function resolvedAlchemyPotions () |
| 93 | { |
| 94 | if (!cachedResolvedPotions) |
| 95 | { |
| 96 | const byKey = itemIdByKeyMap(); |
| 97 | cachedResolvedPotions = Object.values(ALCHEMY_POTION_DEFINITIONS) |
| 98 | .map((potion) => resolvePotionIngredientIds(potion, byKey)); |
| 99 | } |
| 100 | return cachedResolvedPotions; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Greatest integer batch size for reserved slot quantities vs recipe ingredient ratios. |
| 105 | * Returns 0 if ratios do not match (wrong items or non-integer multiple). |
| 106 | */ |
| 107 | export function alchemyBatchCount (recipeIngredients, reservedStacks) |
| 108 | { |
| 109 | const need = new Map(); |
| 110 | for (const row of recipeIngredients ?? []) |
| 111 | { |
| 112 | if (row.itemId == null || row.quantity <= 0) |
| 113 | { return 0; } |
| 114 | need.set(row.itemId, (need.get(row.itemId) ?? 0) + row.quantity); |
| 115 | } |
| 116 | if (need.size === 0 || need.size > 4) |
| 117 | { return 0; } |
| 118 | |
| 119 | const have = new Map(); |
| 120 | for (const stack of reservedStacks ?? []) |
| 121 | { |
| 122 | if (!stack?.itemId || !(stack.quantity > 0)) |
| 123 | { continue; } |
| 124 | have.set(stack.itemId, (have.get(stack.itemId) ?? 0) + stack.quantity); |
| 125 | } |
| 126 | if (have.size !== need.size) |
| 127 | { return 0; } |
| 128 | |
| 129 | let batch = Infinity; |
| 130 | for (const [itemId, qty] of need) |
| 131 | { |
| 132 | const got = have.get(itemId); |
| 133 | if (got == null || got % qty !== 0) |
| 134 | { return 0; } |
| 135 | batch = Math.min(batch, got / qty); |
| 136 | } |
| 137 | for (const itemId of have.keys()) |
| 138 | { |
| 139 | if (!need.has(itemId)) |
| 140 | { return 0; } |
| 141 | } |
| 142 | return Number.isFinite(batch) && batch > 0 ? batch | 0 : 0; |
| 143 | } |
| 144 | |
| 145 | export function stepsMatchRecipe (takenSteps, recipeSteps) |
| 146 | { |
| 147 | if (!Array.isArray(takenSteps) || !Array.isArray(recipeSteps)) |
| 148 | { return false; } |
| 149 | if (takenSteps.length !== recipeSteps.length) |
| 150 | { return false; } |
| 151 | for (let i = 0; i < recipeSteps.length; i++) |
| 152 | { |
| 153 | const want = recipeSteps[i]; |
| 154 | const got = takenSteps[i]; |
| 155 | if (want.kind !== got.kind) |
| 156 | { return false; } |
| 157 | if (want.kind === AlchemyStepKind.insert && want.itemId !== got.itemId) |
| 158 | { return false; } |
| 159 | } |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Evaluate bottling: success potion + batch, or unstable failure. |
| 165 | * Underleveled Alchemy on a correct recipe also yields unstable. |
| 166 | */ |
| 167 | export function evaluateAlchemyBottle ({ |
| 168 | skills, |
| 169 | brewStacks, |
| 170 | takenSteps, |
| 171 | potions = resolvedAlchemyPotions() |
| 172 | }) |
| 173 | { |
| 174 | const level = skillLevel(skills, SkillId.alchemy); |
| 175 | for (const potion of potions) |
| 176 | { |
| 177 | const batch = alchemyBatchCount(potion.ingredients, brewStacks); |
| 178 | if (batch <= 0) |
| 179 | { continue; } |
| 180 | if (!stepsMatchRecipe(takenSteps, potion.steps)) |
| 181 | { continue; } |
| 182 | if (level < (potion.minAlchemyLevel ?? 1)) |
| 183 | { |
| 184 | return { |
| 185 | ok: true, |
| 186 | success: false, |
| 187 | batch: 1, |
| 188 | result: unstablePotionFromSteps(takenSteps), |
| 189 | reason: "alchemy-level" |
| 190 | }; |
| 191 | } |
| 192 | return { |
| 193 | ok: true, |
| 194 | success: true, |
| 195 | batch, |
| 196 | result: { |
| 197 | potionKey: potion.potionKey, |
| 198 | unstable: false, |
| 199 | displayName: potion.displayName, |
| 200 | color: potion.color, |
| 201 | fillLevel: potion.fillLevel ?? 4, |
| 202 | effect: potion.effect, |
| 203 | effectKey: potion.effect?.type ?? potion.potionKey, |
| 204 | minAlchemyLevel: potion.minAlchemyLevel ?? 1 |
| 205 | }, |
| 206 | recipe: potion |
| 207 | }; |
| 208 | } |
| 209 | return { |
| 210 | ok: true, |
| 211 | success: false, |
| 212 | batch: 1, |
| 213 | result: unstablePotionFromSteps(takenSteps), |
| 214 | reason: "no-match" |
| 215 | }; |
| 216 | } |
| 217 | |
| 218 | export function recipePaperBody (potion) |
| 219 | { |
| 220 | const lines = [ |
| 221 | `${potion.displayName}`, |
| 222 | `Alchemy level required: ${potion.minAlchemyLevel ?? 1}`, |
| 223 | "", |
| 224 | "Ingredients (ratios):" |
| 225 | ]; |
| 226 | for (const row of potion.ingredients ?? []) |
| 227 | { |
| 228 | const def = itemDefinition(row.itemId); |
| 229 | lines.push(`- ${row.quantity}× ${def?.displayName ?? row.key ?? row.itemId}`); |
| 230 | } |
| 231 | lines.push("", "Steps:"); |
| 232 | for (const step of potion.steps ?? []) |
| 233 | { lines.push(`- ${formatAlchemyStep(step)}`); } |
| 234 | lines.push("", "Finish by bottling at the cauldron."); |
| 235 | return lines.join("\n"); |
| 236 | } |
| 237 | |
| 238 | export function potionItemInstance (result) |
| 239 | { |
| 240 | return { |
| 241 | kind: "potion", |
| 242 | potionKey: result.potionKey, |
| 243 | unstable: !!result.unstable, |
| 244 | displayName: result.displayName, |
| 245 | color: result.color, |
| 246 | fillLevel: Math.max(1, Math.min(5, result.fillLevel | 0 || 4)), |
| 247 | effect: result.effect ?? null, |
| 248 | effectKey: result.effectKey ?? result.effect?.type ?? result.potionKey, |
| 249 | stepsHash: result.stepsHash ?? null, |
| 250 | minAlchemyLevel: result.minAlchemyLevel ?? null |
| 251 | }; |
| 252 | } |