| 1 | import { Component } from "../ecs/components.js"; |
| 2 | import { BlockType } from "../../../shared/definitions/tiles.js"; |
| 3 | import { |
| 4 | AlchemyStepKind, |
| 5 | CAULDRON_SLOT_COUNT, |
| 6 | DYE_CAULDRON_SLOT_COUNT |
| 7 | } from "../../../shared/definitions/alchemy.js"; |
| 8 | import { ItemId, itemDefinition } from "../../../shared/definitions/items.js"; |
| 9 | import { |
| 10 | evaluateAlchemyBottle, |
| 11 | potionItemInstance, |
| 12 | recipePaperBody, |
| 13 | resolvedAlchemyPotions, |
| 14 | cauldronFlavorText |
| 15 | } from "../../../shared/alchemy/recipes.js"; |
| 16 | import { |
| 17 | DEFAULT_DYE_FILL, |
| 18 | mixFlowersForDye |
| 19 | } from "../../../shared/definitions/dyes.js"; |
| 20 | import { bagView } from "../items/bags.js"; |
| 21 | import { |
| 22 | countItemInSlots, |
| 23 | emptySlots, |
| 24 | insertStack, |
| 25 | normalizeInventorySlots, |
| 26 | takeMatchingFromSlots, |
| 27 | stackQuantity |
| 28 | } from "../../../shared/items/inventory.js"; |
| 29 | import { attachOrigins, craftOrigin, CraftOriginVerb } from "../../../shared/items/origin.js"; |
| 30 | import { tileToChunk, tileToLocal } from "../world/coordinates.js"; |
| 31 | import { CHUNK_SIZE } from "../../../shared/protocol/constants.js"; |
| 32 | import { randomUUID } from "node:crypto"; |
| 33 | import { tileShoveTowardBlock } from "../simulation/tileInteract.js"; |
| 34 | |
| 35 | /** |
| 36 | * Cauldron sessions live on the worker world (per character while the UI is open). |
| 37 | * Clients send intents only; all brew outcomes are evaluated here. |
| 38 | */ |
| 39 | |
| 40 | function userError (message) |
| 41 | { |
| 42 | const error = new Error(message); |
| 43 | error.userFacing = true; |
| 44 | return error; |
| 45 | } |
| 46 | |
| 47 | function characterDisplayName (world, handle) |
| 48 | { |
| 49 | const appearance = world.ecs.get(handle, Component.appearance); |
| 50 | const name = [appearance?.firstName, appearance?.lastName].filter(Boolean).join(" ").trim(); |
| 51 | return name || "someone"; |
| 52 | } |
| 53 | |
| 54 | function ensureSessions (world) |
| 55 | { |
| 56 | if (!world.cauldronSessions) |
| 57 | { world.cauldronSessions = new Map(); } |
| 58 | return world.cauldronSessions; |
| 59 | } |
| 60 | |
| 61 | function emptySession () |
| 62 | { |
| 63 | return { |
| 64 | slots: Array.from({ length: CAULDRON_SLOT_COUNT }, () => null), |
| 65 | dyeSlots: Array.from({ length: DYE_CAULDRON_SLOT_COUNT }, () => null), |
| 66 | brew: [], |
| 67 | steps: [], |
| 68 | lastSuccessRecipe: null |
| 69 | }; |
| 70 | } |
| 71 | |
| 72 | function normalizeSession (session) |
| 73 | { |
| 74 | if (!Array.isArray(session.dyeSlots) || session.dyeSlots.length !== DYE_CAULDRON_SLOT_COUNT) |
| 75 | { session.dyeSlots = Array.from({ length: DYE_CAULDRON_SLOT_COUNT }, () => null); } |
| 76 | return session; |
| 77 | } |
| 78 | |
| 79 | function slotView (slot) |
| 80 | { |
| 81 | if (!slot) |
| 82 | { return null; } |
| 83 | return { |
| 84 | itemId: slot.itemId, |
| 85 | quantity: slot.quantity | 0, |
| 86 | instance: slot.instance ? structuredClone(slot.instance) : null |
| 87 | }; |
| 88 | } |
| 89 | |
| 90 | function sessionView (session) |
| 91 | { |
| 92 | const steps = session.steps ?? []; |
| 93 | return { |
| 94 | slots: (session.slots ?? []).map(slotView), |
| 95 | dyeSlots: (session.dyeSlots ?? []).map(slotView), |
| 96 | brew: (session.brew ?? []).map((row) => ({ |
| 97 | itemId: row.itemId, |
| 98 | quantity: row.quantity | 0, |
| 99 | instance: row.instance ? structuredClone(row.instance) : null |
| 100 | })), |
| 101 | steps: steps.map((step) => ({ ...step })), |
| 102 | flavorText: cauldronFlavorText(steps), |
| 103 | canProcessActions: steps.some((step) => step.kind === AlchemyStepKind.insert), |
| 104 | canSaveRecipe: !!session.lastSuccessRecipe, |
| 105 | lastSuccess: session.lastSuccessRecipe |
| 106 | ? { |
| 107 | potionKey: session.lastSuccessRecipe.potionKey, |
| 108 | displayName: session.lastSuccessRecipe.displayName, |
| 109 | minAlchemyLevel: session.lastSuccessRecipe.minAlchemyLevel ?? 1 |
| 110 | } |
| 111 | : null |
| 112 | }; |
| 113 | } |
| 114 | |
| 115 | function nearCauldron (world, tileX, tileY) |
| 116 | { |
| 117 | for (let dy = -1; dy <= 1; dy++) |
| 118 | { |
| 119 | for (let dx = -1; dx <= 1; dx++) |
| 120 | { |
| 121 | const data = world.chunkData(tileToChunk(tileX + dx), tileToChunk(tileY + dy)); |
| 122 | const index = tileToLocal(tileY + dy) * CHUNK_SIZE + tileToLocal(tileX + dx); |
| 123 | if (data.blocks[index]?.typeId === BlockType.cauldron) |
| 124 | { return true; } |
| 125 | } |
| 126 | } |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | function requireNearCauldron (world, handle) |
| 131 | { |
| 132 | const position = world.ecs.get(handle, Component.position); |
| 133 | if (!nearCauldron(world, position.tileX, position.tileY)) |
| 134 | { throw userError("Stand next to a cauldron."); } |
| 135 | } |
| 136 | |
| 137 | function requireSession (world, characterId) |
| 138 | { |
| 139 | const session = ensureSessions(world).get(characterId); |
| 140 | if (!session) |
| 141 | { throw userError("Open a cauldron first."); } |
| 142 | return normalizeSession(session); |
| 143 | } |
| 144 | |
| 145 | function clearAlchemy (session) |
| 146 | { |
| 147 | session.slots = Array.from({ length: CAULDRON_SLOT_COUNT }, () => null); |
| 148 | session.brew = []; |
| 149 | session.steps = []; |
| 150 | } |
| 151 | |
| 152 | function clearDyeSlots (session) |
| 153 | { |
| 154 | session.dyeSlots = Array.from({ length: DYE_CAULDRON_SLOT_COUNT }, () => null); |
| 155 | } |
| 156 | |
| 157 | function ensureInventory (world, handle) |
| 158 | { |
| 159 | let inventory = world.ecs.get(handle, Component.inventory); |
| 160 | if (!inventory) |
| 161 | { inventory = world.ecs.add(handle, Component.inventory, { slots: emptySlots() }); } |
| 162 | inventory.slots = normalizeInventorySlots(inventory.slots ?? emptySlots()); |
| 163 | return inventory; |
| 164 | } |
| 165 | |
| 166 | export function handleAlchemyCommand (world, sessionId, command) |
| 167 | { |
| 168 | switch (command.type) |
| 169 | { |
| 170 | case "open-cauldron": |
| 171 | return openCauldron(world, sessionId, command); |
| 172 | case "cauldron-set-slot": |
| 173 | return setSlot(world, sessionId, command); |
| 174 | case "cauldron-set-dye-slot": |
| 175 | return setDyeSlot(world, sessionId, command); |
| 176 | case "cauldron-insert": |
| 177 | return insertFromSlot(world, sessionId, command); |
| 178 | case "cauldron-action": |
| 179 | return action(world, sessionId, command); |
| 180 | case "cauldron-bottle": |
| 181 | return bottle(world, sessionId, command); |
| 182 | case "cauldron-create-dye": |
| 183 | return createDye(world, sessionId, command); |
| 184 | case "cauldron-clear": |
| 185 | return clear(world, sessionId, command); |
| 186 | case "cauldron-clear-dye": |
| 187 | return clearDye(world, sessionId, command); |
| 188 | case "cauldron-save-recipe": |
| 189 | return saveRecipe(world, sessionId, command); |
| 190 | case "cauldron-close": |
| 191 | return closeCauldron(world, sessionId, command); |
| 192 | default: |
| 193 | return null; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | function openCauldron (world, sessionId, command) |
| 198 | { |
| 199 | const { handle, entityId } = world.requireOwnedEntity(sessionId, command.characterId); |
| 200 | requireNearCauldron(world, handle); |
| 201 | const inventory = ensureInventory(world, handle); |
| 202 | if (countItemInSlots(inventory.slots, ItemId.glassBottle) < 1) |
| 203 | { throw userError("I don't have any empty bottles."); } |
| 204 | |
| 205 | const sessions = ensureSessions(world); |
| 206 | const existing = normalizeSession(sessions.get(command.characterId) ?? emptySession()); |
| 207 | sessions.set(command.characterId, existing); |
| 208 | const shove = tileShoveTowardBlock(world, handle, entityId, BlockType.cauldron); |
| 209 | return { |
| 210 | events: shove ? [shove] : [], |
| 211 | private: bagView(world.ecs, handle), |
| 212 | cauldron: true, |
| 213 | cauldronState: sessionView(existing) |
| 214 | }; |
| 215 | } |
| 216 | |
| 217 | function closeCauldron (world, sessionId, command) |
| 218 | { |
| 219 | const { handle } = world.requireOwnedEntity(sessionId, command.characterId); |
| 220 | ensureSessions(world).delete(command.characterId); |
| 221 | return { events: [], private: bagView(world.ecs, handle), cauldron: false }; |
| 222 | } |
| 223 | |
| 224 | function setSlot (world, sessionId, command) |
| 225 | { |
| 226 | const { handle } = world.requireOwnedEntity(sessionId, command.characterId); |
| 227 | requireNearCauldron(world, handle); |
| 228 | const session = requireSession(world, command.characterId); |
| 229 | const slotIndex = command.slotIndex | 0; |
| 230 | if (slotIndex < 0 || slotIndex >= CAULDRON_SLOT_COUNT) |
| 231 | { throw userError("Invalid cauldron slot."); } |
| 232 | |
| 233 | const itemId = command.itemId | 0; |
| 234 | const quantity = Math.max(0, command.quantity | 0); |
| 235 | if (!itemId || quantity <= 0) |
| 236 | { |
| 237 | session.slots[slotIndex] = null; |
| 238 | return { |
| 239 | events: [], |
| 240 | private: bagView(world.ecs, handle), |
| 241 | cauldron: true, |
| 242 | cauldronState: sessionView(session) |
| 243 | }; |
| 244 | } |
| 245 | |
| 246 | const def = itemDefinition(itemId); |
| 247 | if (!def || def.uniqueInstance || def.equipment |
| 248 | || itemId === ItemId.potion || itemId === ItemId.glassBottle |
| 249 | || itemId === ItemId.flower || itemId === ItemId.dye || def.flower || def.dye) |
| 250 | { throw userError("Use the Dyes tab for flowers."); } |
| 251 | |
| 252 | const inventory = ensureInventory(world, handle); |
| 253 | const have = countItemInSlots(inventory.slots, itemId); |
| 254 | if (have < quantity) |
| 255 | { throw userError("Not enough of that item in your inventory."); } |
| 256 | |
| 257 | session.slots[slotIndex] = { itemId, quantity, instance: null }; |
| 258 | return { |
| 259 | events: [], |
| 260 | private: bagView(world.ecs, handle), |
| 261 | cauldron: true, |
| 262 | cauldronState: sessionView(session) |
| 263 | }; |
| 264 | } |
| 265 | |
| 266 | function setDyeSlot (world, sessionId, command) |
| 267 | { |
| 268 | const { handle } = world.requireOwnedEntity(sessionId, command.characterId); |
| 269 | requireNearCauldron(world, handle); |
| 270 | const session = requireSession(world, command.characterId); |
| 271 | const slotIndex = command.slotIndex | 0; |
| 272 | if (slotIndex < 0 || slotIndex >= DYE_CAULDRON_SLOT_COUNT) |
| 273 | { throw userError("Invalid dye slot."); } |
| 274 | |
| 275 | const itemId = command.itemId | 0; |
| 276 | const quantity = Math.max(0, command.quantity | 0); |
| 277 | if (!itemId || quantity <= 0) |
| 278 | { |
| 279 | session.dyeSlots[slotIndex] = null; |
| 280 | return { |
| 281 | events: [], |
| 282 | private: bagView(world.ecs, handle), |
| 283 | cauldron: true, |
| 284 | cauldronState: sessionView(session) |
| 285 | }; |
| 286 | } |
| 287 | |
| 288 | if (itemId !== ItemId.flower) |
| 289 | { throw userError("Only flowers go in dye slots."); } |
| 290 | |
| 291 | const inventory = ensureInventory(world, handle); |
| 292 | const wanted = command.instance ? structuredClone(command.instance) : null; |
| 293 | let instance = wanted; |
| 294 | if (!instance) |
| 295 | { |
| 296 | for (const slot of inventory.slots) |
| 297 | { |
| 298 | if (slot?.itemId === ItemId.flower && slot.instance) |
| 299 | { |
| 300 | instance = structuredClone(slot.instance); |
| 301 | break; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | if (!instance?.color) |
| 306 | { throw userError("That flower has no colour."); } |
| 307 | |
| 308 | const have = countMatchingInstance(inventory.slots, ItemId.flower, instance); |
| 309 | if (have < quantity) |
| 310 | { throw userError("Not enough of that flower in your inventory."); } |
| 311 | |
| 312 | session.dyeSlots[slotIndex] = { itemId: ItemId.flower, quantity, instance }; |
| 313 | return { |
| 314 | events: [], |
| 315 | private: bagView(world.ecs, handle), |
| 316 | cauldron: true, |
| 317 | cauldronState: sessionView(session) |
| 318 | }; |
| 319 | } |
| 320 | |
| 321 | function insertFromSlot (world, sessionId, command) |
| 322 | { |
| 323 | const { handle } = world.requireOwnedEntity(sessionId, command.characterId); |
| 324 | requireNearCauldron(world, handle); |
| 325 | const session = requireSession(world, command.characterId); |
| 326 | const slotIndex = command.slotIndex | 0; |
| 327 | const reserved = session.slots[slotIndex]; |
| 328 | if (!reserved) |
| 329 | { throw userError("That cauldron slot is empty."); } |
| 330 | |
| 331 | const inventory = ensureInventory(world, handle); |
| 332 | const pulled = takeMatchingFromSlots(inventory.slots, { |
| 333 | itemId: reserved.itemId, |
| 334 | quantity: reserved.quantity, |
| 335 | instance: reserved.instance ?? null, |
| 336 | compact: false |
| 337 | }); |
| 338 | if (!pulled.ok) |
| 339 | { throw userError("Not enough of that item in your inventory."); } |
| 340 | inventory.slots = pulled.slots; |
| 341 | session.slots[slotIndex] = null; |
| 342 | |
| 343 | const brewRow = { |
| 344 | itemId: reserved.itemId, |
| 345 | quantity: reserved.quantity, |
| 346 | instance: reserved.instance |
| 347 | ? structuredClone(reserved.instance) |
| 348 | : (pulled.taken?.instance ? structuredClone(pulled.taken.instance) : null) |
| 349 | }; |
| 350 | |
| 351 | const existing = session.brew.find((row) => |
| 352 | row.itemId === brewRow.itemId |
| 353 | && JSON.stringify(row.instance ?? null) === JSON.stringify(brewRow.instance ?? null)); |
| 354 | if (existing) |
| 355 | { existing.quantity += brewRow.quantity; } |
| 356 | else |
| 357 | { session.brew.push(brewRow); } |
| 358 | |
| 359 | session.steps.push({ kind: AlchemyStepKind.insert, itemId: reserved.itemId }); |
| 360 | session.lastSuccessRecipe = null; |
| 361 | |
| 362 | return { |
| 363 | events: [], |
| 364 | private: bagView(world.ecs, handle), |
| 365 | cauldron: true, |
| 366 | cauldronState: sessionView(session) |
| 367 | }; |
| 368 | } |
| 369 | |
| 370 | function action (world, sessionId, command) |
| 371 | { |
| 372 | const { handle } = world.requireOwnedEntity(sessionId, command.characterId); |
| 373 | requireNearCauldron(world, handle); |
| 374 | const session = requireSession(world, command.characterId); |
| 375 | const kind = command.action; |
| 376 | if (kind !== AlchemyStepKind.stir && kind !== AlchemyStepKind.heat && kind !== AlchemyStepKind.cool) |
| 377 | { throw userError("Unknown cauldron action."); } |
| 378 | if (!session.steps.some((step) => step.kind === AlchemyStepKind.insert)) |
| 379 | { throw userError("Insert an ingredient first."); } |
| 380 | session.steps.push({ kind }); |
| 381 | session.lastSuccessRecipe = null; |
| 382 | return { |
| 383 | events: [], |
| 384 | private: bagView(world.ecs, handle), |
| 385 | cauldron: true, |
| 386 | cauldronState: sessionView(session) |
| 387 | }; |
| 388 | } |
| 389 | |
| 390 | function bottle (world, sessionId, command) |
| 391 | { |
| 392 | const { handle } = world.requireOwnedEntity(sessionId, command.characterId); |
| 393 | requireNearCauldron(world, handle); |
| 394 | const session = requireSession(world, command.characterId); |
| 395 | if (!(session.brew?.length > 0) && !(session.steps?.length > 0)) |
| 396 | { throw userError("The cauldron is empty."); } |
| 397 | |
| 398 | const inventory = ensureInventory(world, handle); |
| 399 | const skills = world.ecs.get(handle, Component.skills) ?? {}; |
| 400 | |
| 401 | const evaluation = evaluateAlchemyBottle({ |
| 402 | skills, |
| 403 | brewStacks: session.brew, |
| 404 | takenSteps: session.steps, |
| 405 | potions: resolvedAlchemyPotions() |
| 406 | }); |
| 407 | |
| 408 | const wanted = Math.max(1, evaluation.batch | 0); |
| 409 | const bottles = countItemInSlots(inventory.slots, ItemId.glassBottle); |
| 410 | const produced = Math.min(wanted, bottles); |
| 411 | if (produced <= 0) |
| 412 | { throw userError("You need glass bottles to bottle the brew."); } |
| 413 | |
| 414 | const takeBottles = takeMatchingFromSlots(inventory.slots, { |
| 415 | itemId: ItemId.glassBottle, |
| 416 | quantity: produced, |
| 417 | compact: false |
| 418 | }); |
| 419 | if (!takeBottles.ok) |
| 420 | { throw userError("You need glass bottles to bottle the brew."); } |
| 421 | inventory.slots = takeBottles.slots; |
| 422 | |
| 423 | const instance = potionItemInstance(evaluation.result); |
| 424 | const potionDef = itemDefinition(ItemId.potion); |
| 425 | const origin = craftOrigin(CraftOriginVerb.brewed, characterDisplayName(world, handle)); |
| 426 | const inserted = insertStack(inventory.slots, attachOrigins({ |
| 427 | itemId: ItemId.potion, |
| 428 | quantity: produced, |
| 429 | instance, |
| 430 | origins: [{ origin, quantity: produced }] |
| 431 | }), potionDef); |
| 432 | if (!inserted.ok) |
| 433 | { |
| 434 | const refund = insertStack(inventory.slots, { |
| 435 | itemId: ItemId.glassBottle, |
| 436 | quantity: produced |
| 437 | }, itemDefinition(ItemId.glassBottle)); |
| 438 | inventory.slots = refund.ok ? refund.slots : inventory.slots; |
| 439 | throw userError("Not enough inventory space for potions."); |
| 440 | } |
| 441 | inventory.slots = inserted.slots; |
| 442 | |
| 443 | session.lastSuccessRecipe = evaluation.success ? evaluation.recipe : null; |
| 444 | clearAlchemy(session); |
| 445 | |
| 446 | return { |
| 447 | events: [], |
| 448 | private: bagView(world.ecs, handle), |
| 449 | cauldron: true, |
| 450 | cauldronState: sessionView(session), |
| 451 | alchemyResult: { |
| 452 | success: !!evaluation.success, |
| 453 | reason: evaluation.reason ?? null, |
| 454 | produced, |
| 455 | wanted, |
| 456 | lost: Math.max(0, wanted - produced), |
| 457 | potion: { |
| 458 | potionKey: instance.potionKey, |
| 459 | displayName: instance.displayName, |
| 460 | unstable: instance.unstable, |
| 461 | color: instance.color, |
| 462 | fillLevel: instance.fillLevel |
| 463 | } |
| 464 | } |
| 465 | }; |
| 466 | } |
| 467 | |
| 468 | function createDye (world, sessionId, command) |
| 469 | { |
| 470 | const { handle } = world.requireOwnedEntity(sessionId, command.characterId); |
| 471 | requireNearCauldron(world, handle); |
| 472 | const session = requireSession(world, command.characterId); |
| 473 | const rows = (session.dyeSlots ?? []).filter((slot) => slot?.itemId === ItemId.flower); |
| 474 | if (!rows.length) |
| 475 | { throw userError("Add flowers to make dye."); } |
| 476 | |
| 477 | const mixed = mixFlowersForDye(rows); |
| 478 | if (!mixed.ok) |
| 479 | { throw userError(mixed.reason); } |
| 480 | |
| 481 | const wanted = mixed.quantity; |
| 482 | const inventory = ensureInventory(world, handle); |
| 483 | const bottles = countItemInSlots(inventory.slots, ItemId.glassBottle); |
| 484 | if (bottles < wanted && !command.confirmOverflow) |
| 485 | { |
| 486 | return { |
| 487 | events: [], |
| 488 | private: bagView(world.ecs, handle), |
| 489 | cauldron: true, |
| 490 | cauldronState: sessionView(session), |
| 491 | dyeConfirm: { |
| 492 | wanted, |
| 493 | bottles, |
| 494 | lost: Math.max(0, wanted - bottles) |
| 495 | } |
| 496 | }; |
| 497 | } |
| 498 | |
| 499 | const produced = Math.min(wanted, bottles); |
| 500 | if (produced <= 0) |
| 501 | { throw userError("You need glass bottles to bottle the dye."); } |
| 502 | |
| 503 | for (const row of rows) |
| 504 | { |
| 505 | const pulled = takeMatchingFromSlots(inventory.slots, { |
| 506 | itemId: ItemId.flower, |
| 507 | quantity: row.quantity, |
| 508 | instance: row.instance, |
| 509 | compact: false |
| 510 | }); |
| 511 | if (!pulled.ok) |
| 512 | { throw userError("Not enough of those flowers in your inventory."); } |
| 513 | inventory.slots = pulled.slots; |
| 514 | } |
| 515 | |
| 516 | const takeBottles = takeMatchingFromSlots(inventory.slots, { |
| 517 | itemId: ItemId.glassBottle, |
| 518 | quantity: produced, |
| 519 | compact: false |
| 520 | }); |
| 521 | if (!takeBottles.ok) |
| 522 | { throw userError("You need glass bottles to bottle the dye."); } |
| 523 | inventory.slots = takeBottles.slots; |
| 524 | |
| 525 | const dyeDef = itemDefinition(ItemId.dye); |
| 526 | const origin = craftOrigin(CraftOriginVerb.mixed, characterDisplayName(world, handle)); |
| 527 | const inserted = insertStack(inventory.slots, attachOrigins({ |
| 528 | itemId: ItemId.dye, |
| 529 | quantity: produced, |
| 530 | instance: mixed.instance, |
| 531 | origins: [{ origin, quantity: produced }] |
| 532 | }), dyeDef); |
| 533 | if (!inserted.ok) |
| 534 | { |
| 535 | throw userError("Not enough inventory space for dye."); |
| 536 | } |
| 537 | inventory.slots = inserted.slots; |
| 538 | clearDyeSlots(session); |
| 539 | |
| 540 | return { |
| 541 | events: [], |
| 542 | private: bagView(world.ecs, handle), |
| 543 | cauldron: true, |
| 544 | cauldronState: sessionView(session), |
| 545 | alchemyResult: { |
| 546 | success: true, |
| 547 | reason: null, |
| 548 | produced, |
| 549 | wanted, |
| 550 | lost: Math.max(0, wanted - produced), |
| 551 | potion: { |
| 552 | potionKey: "dye", |
| 553 | displayName: "Dye", |
| 554 | unstable: false, |
| 555 | color: mixed.instance?.color ?? null, |
| 556 | fillLevel: mixed.instance?.fillLevel ?? DEFAULT_DYE_FILL |
| 557 | } |
| 558 | } |
| 559 | }; |
| 560 | } |
| 561 | |
| 562 | function clear (world, sessionId, command) |
| 563 | { |
| 564 | const { handle } = world.requireOwnedEntity(sessionId, command.characterId); |
| 565 | requireNearCauldron(world, handle); |
| 566 | const session = requireSession(world, command.characterId); |
| 567 | clearAlchemy(session); |
| 568 | session.lastSuccessRecipe = null; |
| 569 | return { |
| 570 | events: [], |
| 571 | private: bagView(world.ecs, handle), |
| 572 | cauldron: true, |
| 573 | cauldronState: sessionView(session) |
| 574 | }; |
| 575 | } |
| 576 | |
| 577 | function clearDye (world, sessionId, command) |
| 578 | { |
| 579 | const { handle } = world.requireOwnedEntity(sessionId, command.characterId); |
| 580 | requireNearCauldron(world, handle); |
| 581 | const session = requireSession(world, command.characterId); |
| 582 | clearDyeSlots(session); |
| 583 | return { |
| 584 | events: [], |
| 585 | private: bagView(world.ecs, handle), |
| 586 | cauldron: true, |
| 587 | cauldronState: sessionView(session) |
| 588 | }; |
| 589 | } |
| 590 | |
| 591 | function saveRecipe (world, sessionId, command) |
| 592 | { |
| 593 | const { handle } = world.requireOwnedEntity(sessionId, command.characterId); |
| 594 | requireNearCauldron(world, handle); |
| 595 | const session = requireSession(world, command.characterId); |
| 596 | const recipe = session.lastSuccessRecipe; |
| 597 | if (!recipe) |
| 598 | { throw userError("No successful recipe to save."); } |
| 599 | |
| 600 | const inventory = ensureInventory(world, handle); |
| 601 | const paperIndex = inventory.slots.findIndex((slot) => slot?.itemId === ItemId.paper); |
| 602 | if (paperIndex < 0) |
| 603 | { throw userError("You need paper to write a recipe."); } |
| 604 | const paperSlot = inventory.slots[paperIndex]; |
| 605 | const recipeStack = { |
| 606 | itemId: ItemId.recipePaper, |
| 607 | quantity: 1, |
| 608 | instance: { |
| 609 | kind: "recipe-paper", |
| 610 | recipeId: randomUUID(), |
| 611 | potionKey: recipe.potionKey, |
| 612 | displayName: `${recipe.displayName} recipe`, |
| 613 | minAlchemyLevel: recipe.minAlchemyLevel ?? 1, |
| 614 | body: recipePaperBody(recipe), |
| 615 | writtenAt: Date.now() |
| 616 | } |
| 617 | }; |
| 618 | |
| 619 | if (stackQuantity(paperSlot.quantity) <= 1) |
| 620 | { inventory.slots[paperIndex] = recipeStack; } |
| 621 | else |
| 622 | { |
| 623 | const free = inventory.slots.findIndex((entry) => !entry); |
| 624 | if (free < 0) |
| 625 | { throw userError("Need a free inventory slot for the recipe paper."); } |
| 626 | paperSlot.quantity -= 1; |
| 627 | inventory.slots[free] = recipeStack; |
| 628 | } |
| 629 | |
| 630 | session.lastSuccessRecipe = null; |
| 631 | return { |
| 632 | events: [], |
| 633 | private: bagView(world.ecs, handle), |
| 634 | cauldron: true, |
| 635 | cauldronState: sessionView(session) |
| 636 | }; |
| 637 | } |
| 638 | |
| 639 | function countMatchingInstance (slots, itemId, instance) |
| 640 | { |
| 641 | let total = 0; |
| 642 | const key = JSON.stringify(instance ?? null); |
| 643 | for (const slot of slots ?? []) |
| 644 | { |
| 645 | if (!slot || slot.itemId !== itemId) |
| 646 | { continue; } |
| 647 | if (JSON.stringify(slot.instance ?? null) !== key) |
| 648 | { continue; } |
| 649 | total += stackQuantity(slot.quantity); |
| 650 | } |
| 651 | return total; |
| 652 | } |