| 1 | import { Component } from "../ecs/components.js"; |
| 2 | import { BlockType } from "../../../shared/definitions/tiles.js"; |
| 3 | import { CraftingStationId, CRAFT_DURATION_MS, COOK_BURN_CHANCE_OVEN, COOK_BURN_CHANCE_CAMPFIRE } from "../../../shared/definitions/craftingStations.js"; |
| 4 | import { ItemId, itemDefinition } from "../../../shared/definitions/items.js"; |
| 5 | import { SkillId, skillLevel } from "../../../shared/definitions/skills.js"; |
| 6 | import { craftingStationDefinition } from "../../../shared/definitions/craftingStations.js"; |
| 7 | import |
| 8 | { |
| 9 | recipeDefinition, |
| 10 | canStartCraft, |
| 11 | canCraftFitInventory, |
| 12 | hasRecipeInputs |
| 13 | } from "../../../shared/crafting/recipes.js"; |
| 14 | import { resolveTailorCraft } from "../../../shared/crafting/tailor.js"; |
| 15 | import { pickEmptiestDyeSlot } from "../../../shared/crafting/dye.js"; |
| 16 | import { pickEmptiestPotionSlot } from "../../../shared/crafting/potionCharge.js"; |
| 17 | import { dyeColorKey as dyeInstanceColorKey } from "../../../shared/definitions/dyes.js"; |
| 18 | import { craftDurationMs, craftXpForConsumedInputs } from "../../../shared/definitions/gathering.js"; |
| 19 | import { takeFromSlot, insertStack, stackQuantity, consumeFillCharge } from "../../../shared/items/inventory.js"; |
| 20 | import |
| 21 | { |
| 22 | attachOrigins, |
| 23 | craftOrigin, |
| 24 | craftOriginVerbForStation, |
| 25 | CraftOriginVerb |
| 26 | } from "../../../shared/items/origin.js"; |
| 27 | import { applySkillXp, publicSkillsPayload, storedSkillXp } from "../../../shared/skills/state.js"; |
| 28 | import { craftDurationMultiplier } from "../../../shared/equipment/jewelryEffects.js"; |
| 29 | import { bagView } from "../items/bags.js"; |
| 30 | import { tileToChunk, tileToLocal } from "../world/coordinates.js"; |
| 31 | import { CHUNK_SIZE } from "../../../shared/protocol/constants.js"; |
| 32 | import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js"; |
| 33 | import { tileShoveTowardBlock } from "../simulation/tileInteract.js"; |
| 34 | |
| 35 | function userError (message) |
| 36 | { |
| 37 | const error = new Error(message); |
| 38 | error.userFacing = true; |
| 39 | return error; |
| 40 | } |
| 41 | |
| 42 | function characterDisplayName (world, handle) |
| 43 | { |
| 44 | const appearance = world.ecs.get(handle, Component.appearance); |
| 45 | const name = [appearance?.firstName, appearance?.lastName].filter(Boolean).join(" ").trim(); |
| 46 | return name || "someone"; |
| 47 | } |
| 48 | |
| 49 | function ensureCraftSessions (world) |
| 50 | { |
| 51 | if (!world.craftSessions) |
| 52 | { world.craftSessions = new Map(); } |
| 53 | return world.craftSessions; |
| 54 | } |
| 55 | |
| 56 | function nearStationBlock (world, tileX, tileY, blockType, dimensionId = OVERWORLD_ID) |
| 57 | { |
| 58 | const dim = normalizeDimensionId(dimensionId); |
| 59 | for (let dy = -1; dy <= 1; dy++) |
| 60 | { |
| 61 | for (let dx = -1; dx <= 1; dx++) |
| 62 | { |
| 63 | if (dx === 0 && dy === 0) |
| 64 | { continue; } |
| 65 | const data = world.chunkData(tileToChunk(tileX + dx), tileToChunk(tileY + dy), dim); |
| 66 | const index = tileToLocal(tileY + dy) * CHUNK_SIZE + tileToLocal(tileX + dx); |
| 67 | if (data.blocks[index]?.typeId === blockType) |
| 68 | { return true; } |
| 69 | } |
| 70 | } |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | function stationBlockType (stationId) |
| 75 | { |
| 76 | if (stationId === CraftingStationId.furnace) |
| 77 | { return BlockType.furnace; } |
| 78 | if (stationId === CraftingStationId.anvil) |
| 79 | { return BlockType.anvil; } |
| 80 | if (stationId === CraftingStationId.jeweleringStation) |
| 81 | { return BlockType.jeweleringStation; } |
| 82 | if (stationId === CraftingStationId.woodworkingBench) |
| 83 | { return BlockType.woodworkingBench; } |
| 84 | if (stationId === CraftingStationId.craftingBench) |
| 85 | { return BlockType.craftingBench; } |
| 86 | if (stationId === CraftingStationId.spinningWheel) |
| 87 | { return BlockType.spinningWheel; } |
| 88 | if (stationId === CraftingStationId.loom) |
| 89 | { return BlockType.loom; } |
| 90 | if (stationId === CraftingStationId.tailorBench) |
| 91 | { return BlockType.tailorBench; } |
| 92 | if (stationId === CraftingStationId.leatherworkingStation) |
| 93 | { return BlockType.leatherworkingTable; } |
| 94 | if (stationId === CraftingStationId.oven) |
| 95 | { return BlockType.oven; } |
| 96 | if (stationId === CraftingStationId.fireplace) |
| 97 | { return BlockType.campfire; } |
| 98 | return null; |
| 99 | } |
| 100 | |
| 101 | function cookBurnChance (stationId) |
| 102 | { |
| 103 | if (stationId === CraftingStationId.fireplace) |
| 104 | { return COOK_BURN_CHANCE_CAMPFIRE; } |
| 105 | if (stationId === CraftingStationId.oven) |
| 106 | { return COOK_BURN_CHANCE_OVEN; } |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | function takeInputs (slots, inputs) |
| 111 | { |
| 112 | let next = slots.map((slot) => (slot ? structuredClone(slot) : null)); |
| 113 | for (const input of inputs ?? []) |
| 114 | { |
| 115 | let remaining = Math.max(0, stackQuantity(input.quantity)); |
| 116 | if (!remaining) |
| 117 | { continue; } |
| 118 | for (let i = 0; i < next.length && remaining > 0; i++) |
| 119 | { |
| 120 | if (!next[i] || next[i].itemId !== input.itemId) |
| 121 | { continue; } |
| 122 | const taken = takeFromSlot(next, i, remaining); |
| 123 | if (!taken.ok) |
| 124 | { continue; } |
| 125 | next = taken.slots; |
| 126 | remaining -= stackQuantity(taken.taken?.quantity); |
| 127 | } |
| 128 | if (remaining > 0) |
| 129 | { return { ok: false, slots }; } |
| 130 | } |
| 131 | return { ok: true, slots: next }; |
| 132 | } |
| 133 | |
| 134 | /** Spend one fill level of a dye bottle (remove at 0). Never loses the leftover. */ |
| 135 | function consumeDyeUse (slots, slotIndex) |
| 136 | { |
| 137 | return consumeFillCharge(slots, slotIndex, { |
| 138 | kind: "dye", |
| 139 | itemId: ItemId.dye |
| 140 | }); |
| 141 | } |
| 142 | |
| 143 | /** Spend one fill of a potion bottle; exhausted bottles become empty glass. */ |
| 144 | function consumePotionUse (slots, slotIndex) |
| 145 | { |
| 146 | return consumeFillCharge(slots, slotIndex, { |
| 147 | kind: "potion", |
| 148 | itemId: ItemId.potion, |
| 149 | returnEmptyItemId: ItemId.glassBottle |
| 150 | }); |
| 151 | } |
| 152 | |
| 153 | export function startCraft (world, sessionId, characterId, stationId, recipeId, now, options = {}) |
| 154 | { |
| 155 | const entityId = world.characterEntities.get(characterId); |
| 156 | if (entityId == null) |
| 157 | { throw userError("Character is not in the world."); } |
| 158 | const handle = world.ecs.handleOf(entityId); |
| 159 | const identity = world.ecs.get(handle, Component.networkIdentity); |
| 160 | if (!identity || identity.sessionId !== sessionId) |
| 161 | { throw userError("Character is not in the world."); } |
| 162 | if (world.ecs.has(handle, Component.dead)) |
| 163 | { throw userError("You are dead."); } |
| 164 | |
| 165 | const stationDef = craftingStationDefinition(stationId); |
| 166 | if (!stationDef) |
| 167 | { throw userError("Unknown crafting station."); } |
| 168 | let recipe = recipeDefinition(recipeId); |
| 169 | if (!recipe) |
| 170 | { throw userError("Unknown recipe."); } |
| 171 | const stationIds = Array.isArray(recipe.stationIds) |
| 172 | ? recipe.stationIds |
| 173 | : [recipe.stationId]; |
| 174 | if (!stationIds.includes(stationId)) |
| 175 | { throw userError("That recipe cannot be made here."); } |
| 176 | |
| 177 | const position = world.ecs.get(handle, Component.position); |
| 178 | const dim = normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID); |
| 179 | const blockType = stationBlockType(stationId); |
| 180 | if (blockType && !nearStationBlock(world, position.tileX, position.tileY, blockType, dim)) |
| 181 | { throw userError("You need to stand next to the station."); } |
| 182 | |
| 183 | const inventory = world.ecs.get(handle, Component.inventory); |
| 184 | const skills = world.ecs.get(handle, Component.skills); |
| 185 | const level = skillLevel(skills, stationDef.skillId ?? SkillId.smithing); |
| 186 | |
| 187 | let dyeColorKey = null; |
| 188 | if (recipe.tailorTemplate) |
| 189 | { |
| 190 | if (stationId !== CraftingStationId.tailorBench) |
| 191 | { throw userError("That garment can only be made at a tailor's bench."); } |
| 192 | const dyeIdx = options.dyeSlotIndex; |
| 193 | let dyeStack = null; |
| 194 | if (dyeIdx != null && dyeIdx >= 0) |
| 195 | { |
| 196 | dyeStack = inventory?.slots?.[dyeIdx | 0] ?? null; |
| 197 | dyeColorKey = dyeStack ? dyeInstanceColorKey(dyeStack.instance) : null; |
| 198 | } |
| 199 | const resolved = resolveTailorCraft({ |
| 200 | templateId: recipe.tailorTemplate, |
| 201 | clothTier: options.clothTier, |
| 202 | dyeStack |
| 203 | }); |
| 204 | if (!resolved.ok) |
| 205 | { throw userError(resolved.reason); } |
| 206 | recipe = resolved.recipe; |
| 207 | } |
| 208 | else if (options.dyeSlotIndex != null) |
| 209 | { |
| 210 | // Dye is tailor-only; ignore client-supplied dye on other stations. |
| 211 | dyeColorKey = null; |
| 212 | } |
| 213 | |
| 214 | if (!canStartCraft(inventory?.slots ?? [], recipe, level, { dyeColorKey, dyeSlotIndex: options.dyeSlotIndex ?? null })) |
| 215 | { |
| 216 | if ((level | 0) < (recipe.minSkillLevel | 0 || 1)) |
| 217 | { throw userError(`Requires ${stationDef.skillId} ${recipe.minSkillLevel}.`); } |
| 218 | if (!hasRecipeInputs(inventory?.slots ?? [], recipe)) |
| 219 | { throw userError("Missing materials."); } |
| 220 | if (recipe.consumeDye && dyeColorKey != null) |
| 221 | { |
| 222 | const probeSlot = pickEmptiestDyeSlot(inventory?.slots ?? [], dyeColorKey); |
| 223 | const dyeProbe = consumeDyeUse(inventory?.slots ?? [], probeSlot); |
| 224 | if (!dyeProbe.ok) |
| 225 | { throw userError("Need bag space to separate the partially used dye."); } |
| 226 | } |
| 227 | if (recipe.consumePotionKey) |
| 228 | { |
| 229 | const probeSlot = pickEmptiestPotionSlot(inventory?.slots ?? [], recipe.consumePotionKey); |
| 230 | const potionProbe = consumePotionUse(inventory?.slots ?? [], probeSlot); |
| 231 | if (!potionProbe.ok) |
| 232 | { throw userError("Need bag space to separate the partially used brew."); } |
| 233 | } |
| 234 | throw userError("Not enough bag space for the result."); |
| 235 | } |
| 236 | if (recipe.consumeDye && dyeColorKey != null) |
| 237 | { |
| 238 | const probeSlot = pickEmptiestDyeSlot(inventory?.slots ?? [], dyeColorKey); |
| 239 | const dye = inventory?.slots?.[probeSlot]; |
| 240 | if (!dye || dye.itemId !== ItemId.dye) |
| 241 | { throw userError("Selected dye is missing."); } |
| 242 | } |
| 243 | if (recipe.consumePotionKey) |
| 244 | { |
| 245 | const probeSlot = pickEmptiestPotionSlot(inventory?.slots ?? [], recipe.consumePotionKey); |
| 246 | if (probeSlot == null) |
| 247 | { throw userError("Missing materials."); } |
| 248 | } |
| 249 | |
| 250 | const sessions = ensureCraftSessions(world); |
| 251 | const equipment = world.ecs.get(handle, Component.equipment); |
| 252 | const durationMs = Math.max( |
| 253 | 1, |
| 254 | Math.floor(craftDurationMs(recipe, CRAFT_DURATION_MS) * craftDurationMultiplier(equipment)) |
| 255 | ); |
| 256 | let batchRemaining = 1; |
| 257 | if (options.batchAll) |
| 258 | { batchRemaining = Infinity; } |
| 259 | else if (options.batchCount != null) |
| 260 | { batchRemaining = Math.max(1, options.batchCount | 0); } |
| 261 | sessions.set(characterId, { |
| 262 | sessionId, |
| 263 | stationId, |
| 264 | recipeId, |
| 265 | recipe, |
| 266 | dyeColorKey, |
| 267 | batchRemaining, |
| 268 | durationMs, |
| 269 | finishesAt: now + durationMs |
| 270 | }); |
| 271 | const events = []; |
| 272 | if (blockType) |
| 273 | { |
| 274 | const shove = tileShoveTowardBlock(world, handle, entityId, blockType); |
| 275 | if (shove) |
| 276 | { events.push(shove); } |
| 277 | } |
| 278 | events.push({ |
| 279 | type: "item-bubble", |
| 280 | entityId, |
| 281 | itemId: recipe.output.itemId, |
| 282 | quantity: Math.max(1, recipe.output.quantity | 0 || 1) |
| 283 | }); |
| 284 | events.push({ |
| 285 | type: "craft-started", |
| 286 | sessionId, |
| 287 | characterId, |
| 288 | recipeId, |
| 289 | durationMs |
| 290 | }); |
| 291 | return { events }; |
| 292 | } |
| 293 | |
| 294 | export function openStation (world, sessionId, characterId, stationId) |
| 295 | { |
| 296 | const entityId = world.characterEntities.get(characterId); |
| 297 | if (entityId == null) |
| 298 | { throw userError("Character is not in the world."); } |
| 299 | const handle = world.ecs.handleOf(entityId); |
| 300 | const identity = world.ecs.get(handle, Component.networkIdentity); |
| 301 | if (!identity || identity.sessionId !== sessionId) |
| 302 | { throw userError("Character is not in the world."); } |
| 303 | if (world.ecs.has(handle, Component.dead)) |
| 304 | { throw userError("You are dead."); } |
| 305 | |
| 306 | const stationDef = craftingStationDefinition(stationId); |
| 307 | if (!stationDef) |
| 308 | { throw userError("Unknown crafting station."); } |
| 309 | const blockType = stationBlockType(stationId); |
| 310 | const position = world.ecs.get(handle, Component.position); |
| 311 | const dim = normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID); |
| 312 | if (blockType && !nearStationBlock(world, position.tileX, position.tileY, blockType, dim)) |
| 313 | { throw userError("You need to stand next to the station."); } |
| 314 | |
| 315 | const events = []; |
| 316 | if (blockType) |
| 317 | { |
| 318 | const shove = tileShoveTowardBlock(world, handle, entityId, blockType); |
| 319 | if (shove) |
| 320 | { events.push(shove); } |
| 321 | } |
| 322 | events.push({ |
| 323 | type: "station-open", |
| 324 | sessionId, |
| 325 | characterId, |
| 326 | stationId |
| 327 | }); |
| 328 | return { events }; |
| 329 | } |
| 330 | |
| 331 | export function cancelCraft (world, characterId) |
| 332 | { |
| 333 | ensureCraftSessions(world).delete(characterId); |
| 334 | return { events: [] }; |
| 335 | } |
| 336 | |
| 337 | export function stepCrafting (world, now) |
| 338 | { |
| 339 | const sessions = ensureCraftSessions(world); |
| 340 | const events = []; |
| 341 | const privates = []; |
| 342 | for (const [characterId, session] of [...sessions.entries()]) |
| 343 | { |
| 344 | if (now < session.finishesAt) |
| 345 | { continue; } |
| 346 | sessions.delete(characterId); |
| 347 | const entityId = world.characterEntities.get(characterId); |
| 348 | if (entityId == null) |
| 349 | { |
| 350 | events.push({ |
| 351 | type: "craft-result", |
| 352 | sessionId: session.sessionId, |
| 353 | recipeId: session.recipeId, |
| 354 | ok: false, |
| 355 | error: "Character left the world." |
| 356 | }); |
| 357 | continue; |
| 358 | } |
| 359 | const handle = world.ecs.handleOf(entityId); |
| 360 | if (world.ecs.has(handle, Component.dead)) |
| 361 | { |
| 362 | events.push({ |
| 363 | type: "craft-result", |
| 364 | sessionId: session.sessionId, |
| 365 | recipeId: session.recipeId, |
| 366 | ok: false, |
| 367 | error: "You are dead." |
| 368 | }); |
| 369 | continue; |
| 370 | } |
| 371 | |
| 372 | const recipe = session.recipe ?? recipeDefinition(session.recipeId); |
| 373 | const stationDef = craftingStationDefinition(session.stationId); |
| 374 | const inventory = world.ecs.get(handle, Component.inventory); |
| 375 | const skills = world.ecs.get(handle, Component.skills); |
| 376 | const position = world.ecs.get(handle, Component.position); |
| 377 | const dim = normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID); |
| 378 | const blockType = stationBlockType(session.stationId); |
| 379 | const level = skillLevel(skills, stationDef?.skillId ?? SkillId.smithing); |
| 380 | |
| 381 | if (!recipe || !stationDef || !inventory |
| 382 | || (blockType && !nearStationBlock(world, position.tileX, position.tileY, blockType, dim)) |
| 383 | || !canCraftFitInventory(inventory.slots, recipe, { dyeColorKey: session.dyeColorKey }) |
| 384 | || (level | 0) < (recipe.minSkillLevel | 0 || 1)) |
| 385 | { |
| 386 | events.push({ |
| 387 | type: "craft-result", |
| 388 | sessionId: session.sessionId, |
| 389 | recipeId: session.recipeId, |
| 390 | ok: false, |
| 391 | error: "Could not finish crafting." |
| 392 | }); |
| 393 | continue; |
| 394 | } |
| 395 | |
| 396 | const taken = takeInputs(inventory.slots, recipe.inputs); |
| 397 | if (!taken.ok) |
| 398 | { |
| 399 | events.push({ |
| 400 | type: "craft-result", |
| 401 | sessionId: session.sessionId, |
| 402 | recipeId: session.recipeId, |
| 403 | ok: false, |
| 404 | error: "Missing materials." |
| 405 | }); |
| 406 | continue; |
| 407 | } |
| 408 | let slotsAfter = taken.slots; |
| 409 | if (recipe.consumeDye && session.dyeColorKey != null) |
| 410 | { |
| 411 | const consumeSlot = pickEmptiestDyeSlot(slotsAfter, session.dyeColorKey); |
| 412 | const dyeTaken = consumeDyeUse(slotsAfter, consumeSlot); |
| 413 | if (!dyeTaken.ok) |
| 414 | { |
| 415 | events.push({ |
| 416 | type: "craft-result", |
| 417 | sessionId: session.sessionId, |
| 418 | recipeId: session.recipeId, |
| 419 | ok: false, |
| 420 | error: dyeTaken.reason === "space" |
| 421 | ? "Need bag space to separate the partially used dye." |
| 422 | : "Selected dye is missing." |
| 423 | }); |
| 424 | continue; |
| 425 | } |
| 426 | slotsAfter = dyeTaken.slots; |
| 427 | } |
| 428 | if (recipe.consumePotionKey) |
| 429 | { |
| 430 | const consumeSlot = pickEmptiestPotionSlot(slotsAfter, recipe.consumePotionKey); |
| 431 | const potionTaken = consumePotionUse(slotsAfter, consumeSlot); |
| 432 | if (!potionTaken.ok) |
| 433 | { |
| 434 | events.push({ |
| 435 | type: "craft-result", |
| 436 | sessionId: session.sessionId, |
| 437 | recipeId: session.recipeId, |
| 438 | ok: false, |
| 439 | error: potionTaken.reason === "space" |
| 440 | ? "Need bag space to separate the partially used brew." |
| 441 | : "Missing materials." |
| 442 | }); |
| 443 | continue; |
| 444 | } |
| 445 | slotsAfter = potionTaken.slots; |
| 446 | } |
| 447 | |
| 448 | const burnChance = cookBurnChance(session.stationId); |
| 449 | const burned = burnChance > 0 |
| 450 | && recipe.burntOutput?.itemId |
| 451 | && ((world.rng?.() ?? Math.random()) < burnChance); |
| 452 | const outSpec = burned ? recipe.burntOutput : recipe.output; |
| 453 | const outDef = itemDefinition(outSpec.itemId); |
| 454 | const outQty = Math.max(1, outSpec.quantity | 0 || 1); |
| 455 | const origin = craftOrigin( |
| 456 | burned ? CraftOriginVerb.ruined : craftOriginVerbForStation(session.stationId), |
| 457 | characterDisplayName(world, handle) |
| 458 | ); |
| 459 | const outStack = { |
| 460 | itemId: outSpec.itemId, |
| 461 | quantity: outQty, |
| 462 | origins: [{ origin, quantity: outQty }], |
| 463 | ...(recipe.dyeColor && !burned ? { instance: { kind: "clothing", dyeColor: recipe.dyeColor } } : {}) |
| 464 | }; |
| 465 | const added = insertStack(slotsAfter, attachOrigins(outStack, origin), outDef); |
| 466 | if (!added.ok || added.remainder > 0) |
| 467 | { |
| 468 | events.push({ |
| 469 | type: "craft-result", |
| 470 | sessionId: session.sessionId, |
| 471 | recipeId: session.recipeId, |
| 472 | ok: false, |
| 473 | error: "Not enough bag space for the result." |
| 474 | }); |
| 475 | continue; |
| 476 | } |
| 477 | inventory.slots = added.slots; |
| 478 | |
| 479 | let skillsChanged = false; |
| 480 | const levelUps = []; |
| 481 | if (!burned && skills?.xp && stationDef?.skillId) |
| 482 | { |
| 483 | const result = applySkillXp( |
| 484 | skills, |
| 485 | stationDef.skillId, |
| 486 | craftXpForConsumedInputs(recipe.inputs, stationDef.skillId), |
| 487 | world.ecs.get(handle, Component.equipment) |
| 488 | ); |
| 489 | if (result.changed) |
| 490 | { skillsChanged = true; } |
| 491 | if (result.levelUp) |
| 492 | { levelUps.push(result.levelUp); } |
| 493 | } |
| 494 | |
| 495 | privates.push({ characterId, bag: bagView(world.ecs, handle) }); |
| 496 | if (skillsChanged) |
| 497 | { |
| 498 | events.push({ |
| 499 | type: "skills-updated", |
| 500 | entityId, |
| 501 | characterId, |
| 502 | ...publicSkillsPayload(skills), |
| 503 | stored: storedSkillXp(skills), |
| 504 | levelUps |
| 505 | }); |
| 506 | } |
| 507 | |
| 508 | if (burned) |
| 509 | { |
| 510 | events.push({ |
| 511 | type: "command-error", |
| 512 | sessionId: session.sessionId, |
| 513 | message: "You burn the food." |
| 514 | }); |
| 515 | } |
| 516 | |
| 517 | let continuing = false; |
| 518 | const batchRemaining = session.batchRemaining ?? 1; |
| 519 | const nextRemaining = batchRemaining === Infinity |
| 520 | ? Infinity |
| 521 | : Math.max(0, (batchRemaining | 0) - 1); |
| 522 | if (nextRemaining !== 0 && canStartCraft( |
| 523 | inventory.slots, |
| 524 | recipe, |
| 525 | level, |
| 526 | { dyeColorKey: session.dyeColorKey, dyeSlotIndex: null } |
| 527 | )) |
| 528 | { |
| 529 | const equipment = world.ecs.get(handle, Component.equipment); |
| 530 | const durationMs = session.durationMs ?? Math.max( |
| 531 | 1, |
| 532 | Math.floor(craftDurationMs(recipe, CRAFT_DURATION_MS) * craftDurationMultiplier(equipment)) |
| 533 | ); |
| 534 | sessions.set(characterId, { |
| 535 | ...session, |
| 536 | recipe, |
| 537 | batchRemaining: nextRemaining, |
| 538 | durationMs, |
| 539 | finishesAt: now + durationMs |
| 540 | }); |
| 541 | continuing = true; |
| 542 | if (blockType) |
| 543 | { |
| 544 | const shove = tileShoveTowardBlock(world, handle, entityId, blockType); |
| 545 | if (shove) |
| 546 | { events.push(shove); } |
| 547 | } |
| 548 | events.push({ |
| 549 | type: "item-bubble", |
| 550 | entityId, |
| 551 | itemId: outSpec.itemId, |
| 552 | quantity: outQty |
| 553 | }); |
| 554 | events.push({ |
| 555 | type: "craft-started", |
| 556 | sessionId: session.sessionId, |
| 557 | characterId, |
| 558 | recipeId: session.recipeId, |
| 559 | durationMs |
| 560 | }); |
| 561 | } |
| 562 | events.push({ |
| 563 | type: "craft-result", |
| 564 | sessionId: session.sessionId, |
| 565 | recipeId: session.recipeId, |
| 566 | ok: true, |
| 567 | continuing |
| 568 | }); |
| 569 | } |
| 570 | return { events, privates }; |
| 571 | } |
| 572 | |
| 573 | export function handleCraftCommand (world, sessionId, command, now) |
| 574 | { |
| 575 | switch (command.type) |
| 576 | { |
| 577 | case "open-station": |
| 578 | return openStation(world, sessionId, command.characterId, command.stationId); |
| 579 | case "start-craft": |
| 580 | return startCraft( |
| 581 | world, |
| 582 | sessionId, |
| 583 | command.characterId, |
| 584 | command.stationId, |
| 585 | command.recipeId, |
| 586 | now, |
| 587 | { |
| 588 | clothTier: command.clothTier ?? null, |
| 589 | dyeSlotIndex: command.dyeSlotIndex ?? null, |
| 590 | batchCount: command.batchCount ?? null, |
| 591 | batchAll: !!command.batchAll |
| 592 | } |
| 593 | ); |
| 594 | case "cancel-craft": |
| 595 | return cancelCraft(world, command.characterId); |
| 596 | default: |
| 597 | return null; |
| 598 | } |
| 599 | } |