BlueGit

timfa questward

Add arrow stack equip, craft batch UI, and ranged client FX.

Crafting benches support Craft x/all batches; arrows equip as full offhand stacks; projectiles lerp with darkness-aware sprites and an out-of-arrows toast.

timfa Tim Falken · pushed by timfa

Authored 2026-08-26 · 2 months ago

Hash
794083453944b4fd7f7afee2f4244002e3bec935
Parent
ac50f31d
8 files changed +270 −40
client/assets/fx/projectile/arrow.png added Binary file +0 −0

The contents of this binary file are not shown.

client/index.html +5 −1
@@ -207,7 +207,11 @@
207 207 <div id="craft-progress" class="craft-progress" hidden>
208 208 <div id="craft-progress-fill" class="craft-progress-fill"></div>
209 209 </div>
210 <button id="craft-button" type="button" disabled>Craft</button>
210 <div id="craft-actions" class="craft-actions">
211 <button id="craft-button" type="button" disabled>Craft</button>
212 <button id="craft-batch-button" type="button" hidden disabled>Craft x</button>
213 <button id="craft-batch-all-button" type="button" hidden disabled>Craft all</button>
214 </div>
211 215 </div>
212 216 </div>
213 217 </div>
@@ -207,7 +207,11 @@
207 <div id="craft-progress" class="craft-progress" hidden>
207 <div id="craft-progress" class="craft-progress" hidden>
208 <div id="craft-progress-fill" class="craft-progress-fill"></div>
208 <div id="craft-progress-fill" class="craft-progress-fill"></div>
209 </div>
209 </div>
210 <button id="craft-button" type="button" disabled>Craft</button>
210 <div id="craft-actions" class="craft-actions">
211 <button id="craft-button" type="button" disabled>Craft</button>
212 <button id="craft-batch-button" type="button" hidden disabled>Craft x</button>
213 <button id="craft-batch-all-button" type="button" hidden disabled>Craft all</button>
214 </div>
211 </div>
215 </div>
212 </div>
216 </div>
213 </div>
217 </div>
client/src/play/crafting.js +75 −2
@@ -53,6 +53,8 @@ export function initCrafting ({
53 53 const progressWrap = document.getElementById("craft-progress");
54 54 const progressFill = document.getElementById("craft-progress-fill");
55 55 const craftButton = document.getElementById("craft-button");
56 const craftBatchButton = document.getElementById("craft-batch-button");
57 const craftBatchAllButton = document.getElementById("craft-batch-all-button");
56 58 const dyeBackdrop = document.getElementById("dye-picker-backdrop");
57 59 const dyePanel = document.getElementById("dye-picker-panel");
58 60 const dyeGrid = document.getElementById("dye-picker-grid");
@@ -72,6 +74,8 @@ export function initCrafting ({
72 74 document.getElementById("craft-close").addEventListener("click", () => close());
73 75 backdrop.addEventListener("click", () => close());
74 76 craftButton.addEventListener("click", () => startCraft());
77 craftBatchButton?.addEventListener("click", () => startBatchCraft());
78 craftBatchAllButton?.addEventListener("click", () => startCraft({ batchAll: true }));
75 79 materialTierSelect?.addEventListener("change", () =>
76 80 {
77 81 selectedMaterial = materialTierSelect.value || selectedMaterial;
@@ -104,6 +108,11 @@ export function initCrafting ({
104 108 return stationId === CraftingStationId.woodworkingBench;
105 109 }
106 110
111 function isCraftingBenchStation ()
112 {
113 return stationId === CraftingStationId.craftingBench;
114 }
115
107 116 function usesMaterialDropdown ()
108 117 {
109 118 return isTailorStation() || isAnvilStation() || isWoodworkingStation();
@@ -380,9 +389,19 @@ export function initCrafting ({
380 389
381 390 function updateCraftState (slots, recipe)
382 391 {
392 const batchVisible = isCraftingBenchStation();
393 if (craftBatchButton)
394 { craftBatchButton.hidden = !batchVisible; }
395 if (craftBatchAllButton)
396 { craftBatchAllButton.hidden = !batchVisible; }
397
383 398 if (crafting)
384 399 {
385 400 craftButton.disabled = true;
401 if (craftBatchButton)
402 { craftBatchButton.disabled = true; }
403 if (craftBatchAllButton)
404 { craftBatchAllButton.disabled = true; }
386 405 return;
387 406 }
388 407 const skill = stationSkillLevel();
@@ -391,6 +410,10 @@ export function initCrafting ({
391 410 {
392 411 statusEl.textContent = `Requires skill level ${minLevel} (you have ${skill}).`;
393 412 craftButton.disabled = true;
413 if (craftBatchButton)
414 { craftBatchButton.disabled = true; }
415 if (craftBatchAllButton)
416 { craftBatchAllButton.disabled = true; }
394 417 return;
395 418 }
396 419 const slotsSnapshot = slots ?? getInventorySlots?.() ?? [];
@@ -399,6 +422,10 @@ export function initCrafting ({
399 422 {
400 423 statusEl.textContent = "";
401 424 craftButton.disabled = false;
425 if (craftBatchButton)
426 { craftBatchButton.disabled = !batchVisible; }
427 if (craftBatchAllButton)
428 { craftBatchAllButton.disabled = !batchVisible; }
402 429 return;
403 430 }
404 431 if (!hasRecipeInputs(slotsSnapshot, recipe))
@@ -414,6 +441,10 @@ export function initCrafting ({
414 441 statusEl.textContent = "Not enough bag space for the result.";
415 442 }
416 443 craftButton.disabled = true;
444 if (craftBatchButton)
445 { craftBatchButton.disabled = true; }
446 if (craftBatchAllButton)
447 { craftBatchAllButton.disabled = true; }
417 448 }
418 449
419 450 function openDyePicker ()
@@ -459,7 +490,18 @@ export function initCrafting ({
459 490 dyeBackdrop.hidden = true;
460 491 }
461 492
462 function startCraft ()
493 function startBatchCraft ()
494 {
495 const raw = window.prompt("How many?", "1");
496 if (raw == null)
497 { return; }
498 const count = Number.parseInt(raw, 10);
499 if (!Number.isFinite(count) || count < 1)
500 { return; }
501 startCraft({ batchCount: count });
502 }
503
504 function startCraft (options = {})
463 505 {
464 506 const recipe = activeRecipe();
465 507 if (!recipe || crafting)
@@ -472,9 +514,17 @@ export function initCrafting ({
472 514 }
473 515 crafting = true;
474 516 craftButton.disabled = true;
517 if (craftBatchButton)
518 { craftBatchButton.disabled = true; }
519 if (craftBatchAllButton)
520 { craftBatchAllButton.disabled = true; }
475 521 progressWrap.hidden = false;
476 522 progressFill.style.width = "0%";
477 statusEl.textContent = "Crafting…";
523 statusEl.textContent = options.batchAll
524 ? "Crafting all…"
525 : options.batchCount > 1
526 ? `Crafting ${options.batchCount}…`
527 : "Crafting…";
478 528 craftStartedAt = performance.now();
479 529 const durationMs = craftDurationMs(recipe, CRAFT_DURATION_MS);
480 530 const payload = {
@@ -482,6 +532,10 @@ export function initCrafting ({
482 532 stationId,
483 533 recipeId: selectedRecipeId
484 534 };
535 if (options.batchCount != null)
536 { payload.batchCount = options.batchCount | 0; }
537 if (options.batchAll)
538 { payload.batchAll = true; }
485 539 if (isTailorStation())
486 540 {
487 541 payload.clothTier = selectedMaterial;
@@ -587,6 +641,25 @@ export function initCrafting ({
587 641 {
588 642 if (!open || message.recipeId !== selectedRecipeId)
589 643 { return; }
644 if (message.ok && message.continuing)
645 {
646 const recipe = activeRecipe();
647 if (!recipe)
648 {
649 stopCraftAnimation();
650 renderDetail();
651 return;
652 }
653 statusEl.textContent = "Crafting…";
654 craftStartedAt = performance.now();
655 const durationMs = craftDurationMs(recipe, CRAFT_DURATION_MS);
656 if (craftTimer != null)
657 { clearTimeout(craftTimer); }
658 animateProgress(durationMs);
659 craftTimer = window.setTimeout(() => finishCraft(true), durationMs);
660 renderDetail();
661 return;
662 }
590 663 stopCraftAnimation();
591 664 if (message.ok)
592 665 {
@@ -53,6 +53,8 @@ export function initCrafting ({
53 const progressWrap = document.getElementById("craft-progress");
53 const progressWrap = document.getElementById("craft-progress");
54 const progressFill = document.getElementById("craft-progress-fill");
54 const progressFill = document.getElementById("craft-progress-fill");
55 const craftButton = document.getElementById("craft-button");
55 const craftButton = document.getElementById("craft-button");
56 const craftBatchButton = document.getElementById("craft-batch-button");
57 const craftBatchAllButton = document.getElementById("craft-batch-all-button");
56 const dyeBackdrop = document.getElementById("dye-picker-backdrop");
58 const dyeBackdrop = document.getElementById("dye-picker-backdrop");
57 const dyePanel = document.getElementById("dye-picker-panel");
59 const dyePanel = document.getElementById("dye-picker-panel");
58 const dyeGrid = document.getElementById("dye-picker-grid");
60 const dyeGrid = document.getElementById("dye-picker-grid");
@@ -72,6 +74,8 @@ export function initCrafting ({
72 document.getElementById("craft-close").addEventListener("click", () => close());
74 document.getElementById("craft-close").addEventListener("click", () => close());
73 backdrop.addEventListener("click", () => close());
75 backdrop.addEventListener("click", () => close());
74 craftButton.addEventListener("click", () => startCraft());
76 craftButton.addEventListener("click", () => startCraft());
77 craftBatchButton?.addEventListener("click", () => startBatchCraft());
78 craftBatchAllButton?.addEventListener("click", () => startCraft({ batchAll: true }));
75 materialTierSelect?.addEventListener("change", () =>
79 materialTierSelect?.addEventListener("change", () =>
76 {
80 {
77 selectedMaterial = materialTierSelect.value || selectedMaterial;
81 selectedMaterial = materialTierSelect.value || selectedMaterial;
@@ -104,6 +108,11 @@ export function initCrafting ({
104 return stationId === CraftingStationId.woodworkingBench;
108 return stationId === CraftingStationId.woodworkingBench;
105 }
109 }
106
110
111 function isCraftingBenchStation ()
112 {
113 return stationId === CraftingStationId.craftingBench;
114 }
115
107 function usesMaterialDropdown ()
116 function usesMaterialDropdown ()
108 {
117 {
109 return isTailorStation() || isAnvilStation() || isWoodworkingStation();
118 return isTailorStation() || isAnvilStation() || isWoodworkingStation();
@@ -380,9 +389,19 @@ export function initCrafting ({
380
389
381 function updateCraftState (slots, recipe)
390 function updateCraftState (slots, recipe)
382 {
391 {
392 const batchVisible = isCraftingBenchStation();
393 if (craftBatchButton)
394 { craftBatchButton.hidden = !batchVisible; }
395 if (craftBatchAllButton)
396 { craftBatchAllButton.hidden = !batchVisible; }
397
383 if (crafting)
398 if (crafting)
384 {
399 {
385 craftButton.disabled = true;
400 craftButton.disabled = true;
401 if (craftBatchButton)
402 { craftBatchButton.disabled = true; }
403 if (craftBatchAllButton)
404 { craftBatchAllButton.disabled = true; }
386 return;
405 return;
387 }
406 }
388 const skill = stationSkillLevel();
407 const skill = stationSkillLevel();
@@ -391,6 +410,10 @@ export function initCrafting ({
391 {
410 {
392 statusEl.textContent = `Requires skill level ${minLevel} (you have ${skill}).`;
411 statusEl.textContent = `Requires skill level ${minLevel} (you have ${skill}).`;
393 craftButton.disabled = true;
412 craftButton.disabled = true;
413 if (craftBatchButton)
414 { craftBatchButton.disabled = true; }
415 if (craftBatchAllButton)
416 { craftBatchAllButton.disabled = true; }
394 return;
417 return;
395 }
418 }
396 const slotsSnapshot = slots ?? getInventorySlots?.() ?? [];
419 const slotsSnapshot = slots ?? getInventorySlots?.() ?? [];
@@ -399,6 +422,10 @@ export function initCrafting ({
399 {
422 {
400 statusEl.textContent = "";
423 statusEl.textContent = "";
401 craftButton.disabled = false;
424 craftButton.disabled = false;
425 if (craftBatchButton)
426 { craftBatchButton.disabled = !batchVisible; }
427 if (craftBatchAllButton)
428 { craftBatchAllButton.disabled = !batchVisible; }
402 return;
429 return;
403 }
430 }
404 if (!hasRecipeInputs(slotsSnapshot, recipe))
431 if (!hasRecipeInputs(slotsSnapshot, recipe))
@@ -414,6 +441,10 @@ export function initCrafting ({
414 statusEl.textContent = "Not enough bag space for the result.";
441 statusEl.textContent = "Not enough bag space for the result.";
415 }
442 }
416 craftButton.disabled = true;
443 craftButton.disabled = true;
444 if (craftBatchButton)
445 { craftBatchButton.disabled = true; }
446 if (craftBatchAllButton)
447 { craftBatchAllButton.disabled = true; }
417 }
448 }
418
449
419 function openDyePicker ()
450 function openDyePicker ()
@@ -459,7 +490,18 @@ export function initCrafting ({
459 dyeBackdrop.hidden = true;
490 dyeBackdrop.hidden = true;
460 }
491 }
461
492
462 function startCraft ()
493 function startBatchCraft ()
494 {
495 const raw = window.prompt("How many?", "1");
496 if (raw == null)
497 { return; }
498 const count = Number.parseInt(raw, 10);
499 if (!Number.isFinite(count) || count < 1)
500 { return; }
501 startCraft({ batchCount: count });
502 }
503
504 function startCraft (options = {})
463 {
505 {
464 const recipe = activeRecipe();
506 const recipe = activeRecipe();
465 if (!recipe || crafting)
507 if (!recipe || crafting)
@@ -472,9 +514,17 @@ export function initCrafting ({
472 }
514 }
473 crafting = true;
515 crafting = true;
474 craftButton.disabled = true;
516 craftButton.disabled = true;
517 if (craftBatchButton)
518 { craftBatchButton.disabled = true; }
519 if (craftBatchAllButton)
520 { craftBatchAllButton.disabled = true; }
475 progressWrap.hidden = false;
521 progressWrap.hidden = false;
476 progressFill.style.width = "0%";
522 progressFill.style.width = "0%";
477 statusEl.textContent = "Crafting…";
523 statusEl.textContent = options.batchAll
524 ? "Crafting all…"
525 : options.batchCount > 1
526 ? `Crafting ${options.batchCount}…`
527 : "Crafting…";
478 craftStartedAt = performance.now();
528 craftStartedAt = performance.now();
479 const durationMs = craftDurationMs(recipe, CRAFT_DURATION_MS);
529 const durationMs = craftDurationMs(recipe, CRAFT_DURATION_MS);
480 const payload = {
530 const payload = {
@@ -482,6 +532,10 @@ export function initCrafting ({
482 stationId,
532 stationId,
483 recipeId: selectedRecipeId
533 recipeId: selectedRecipeId
484 };
534 };
535 if (options.batchCount != null)
536 { payload.batchCount = options.batchCount | 0; }
537 if (options.batchAll)
538 { payload.batchAll = true; }
485 if (isTailorStation())
539 if (isTailorStation())
486 {
540 {
487 payload.clothTier = selectedMaterial;
541 payload.clothTier = selectedMaterial;
@@ -587,6 +641,25 @@ export function initCrafting ({
587 {
641 {
588 if (!open || message.recipeId !== selectedRecipeId)
642 if (!open || message.recipeId !== selectedRecipeId)
589 { return; }
643 { return; }
644 if (message.ok && message.continuing)
645 {
646 const recipe = activeRecipe();
647 if (!recipe)
648 {
649 stopCraftAnimation();
650 renderDetail();
651 return;
652 }
653 statusEl.textContent = "Crafting…";
654 craftStartedAt = performance.now();
655 const durationMs = craftDurationMs(recipe, CRAFT_DURATION_MS);
656 if (craftTimer != null)
657 { clearTimeout(craftTimer); }
658 animateProgress(durationMs);
659 craftTimer = window.setTimeout(() => finishCraft(true), durationMs);
660 renderDetail();
661 return;
662 }
590 stopCraftAnimation();
663 stopCraftAnimation();
591 if (message.ok)
664 if (message.ok)
592 {
665 {
client/src/play/main.js +9 −0
@@ -1148,6 +1148,15 @@ async function main ()
1148 1148 case MessageType.combatSwing:
1149 1149 view?.spawnCombatSwing(message);
1150 1150 break;
1151 case MessageType.combatProjectile:
1152 view?.spawnPhysicalProjectileFx({
1153 ...message,
1154 sprite: "fx:projectile:arrow"
1155 });
1156 break;
1157 case MessageType.combatNoArrows:
1158 toasts?.show("Out of arrows!");
1159 break;
1151 1160 case MessageType.gatherSwing:
1152 1161 case MessageType.tileShove:
1153 1162 view?.spawnGatherSwing(message);
@@ -1148,6 +1148,15 @@ async function main ()
1148 case MessageType.combatSwing:
1148 case MessageType.combatSwing:
1149 view?.spawnCombatSwing(message);
1149 view?.spawnCombatSwing(message);
1150 break;
1150 break;
1151 case MessageType.combatProjectile:
1152 view?.spawnPhysicalProjectileFx({
1153 ...message,
1154 sprite: "fx:projectile:arrow"
1155 });
1156 break;
1157 case MessageType.combatNoArrows:
1158 toasts?.show("Out of arrows!");
1159 break;
1151 case MessageType.gatherSwing:
1160 case MessageType.gatherSwing:
1152 case MessageType.tileShove:
1161 case MessageType.tileShove:
1153 view?.spawnGatherSwing(message);
1162 view?.spawnGatherSwing(message);
client/src/render/worldView.js +41 −15
@@ -856,21 +856,19 @@ export class WorldView
856 856 /** Physical combat FX (melee swipes, ranged projectiles). Magic spells stay unshaded. */
857 857 spawnPhysicalProjectileFx (payload)
858 858 {
859 const spriteName = payload.sprite ?? "fx:projectile:projectile";
859 const spriteName = payload.sprite ?? "fx:projectile:arrow";
860 860 const image = this.game.images[spriteName];
861 861 if (!image)
862 862 { return; }
863 863 const fromPivot = tilePivot(payload.fromTileX, payload.fromTileY, this.tileSize());
864 864 const toPivot = tilePivot(payload.tileX, payload.tileY, this.tileSize());
865 const midX = (fromPivot.x + toPivot.x) / 2;
866 const midY = (fromPivot.y + toPivot.y) / 2;
867 865 const sortY = Math.max(fromPivot.y, toPivot.y) + 0.02;
868 866 const angle = Math.atan2(toPivot.y - fromPivot.y, toPivot.x - fromPivot.x) * (180 / Math.PI);
869 867 const object = new GameObject(0, 0, image, SORT_FX);
870 868 if (object.transform)
871 869 { object.transform.rotation = angle; }
872 870 this.scene.addObject(object);
873 this.#placePivoted(object, midX, midY, sortY, SORT_FX);
871 this.#placePivoted(object, fromPivot.x, fromPivot.y, sortY, SORT_FX);
874 872 this.#applyPhysicalCombatFxDarkness(
875 873 object,
876 874 payload.fromTileX,
@@ -878,9 +876,14 @@ export class WorldView
878 876 payload.tileX,
879 877 payload.tileY
880 878 );
879 const started = performance.now();
880 const durationMs = payload.durationMs ?? 400;
881 881 this.swipes.push({
882 882 object,
883 until: performance.now() + (payload.durationMs ?? 500),
883 until: started + durationMs,
884 started,
885 durationMs,
886 lerp: true,
884 887 fromTileX: payload.fromTileX,
885 888 fromTileY: payload.fromTileY,
886 889 tileX: payload.tileX,
@@ -2299,20 +2302,43 @@ export class WorldView
2299 2302 {
2300 2303 this.swipes = this.swipes.filter((swipe) =>
2301 2304 {
2302 if (swipe.until > now)
2305 if (swipe.lerp && swipe.started != null)
2303 2306 {
2304 if (swipe.fromTileX != null)
2307 const durationMs = swipe.durationMs ?? 400;
2308 const t = Math.min(1, (now - swipe.started) / durationMs);
2309 const fromPivot = tilePivot(swipe.fromTileX, swipe.fromTileY, this.tileSize());
2310 const toPivot = tilePivot(swipe.tileX, swipe.tileY, this.tileSize());
2311 const x = fromPivot.x + (toPivot.x - fromPivot.x) * t;
2312 const y = fromPivot.y + (toPivot.y - fromPivot.y) * t;
2313 const sortY = Math.max(fromPivot.y, toPivot.y) + 0.02;
2314 if (swipe.object?.transform)
2305 2315 {
2306 this.#applyPhysicalCombatFxDarkness(
2307 swipe.object,
2308 swipe.fromTileX,
2309 swipe.fromTileY,
2310 swipe.tileX,
2311 swipe.tileY
2312 );
2316 swipe.object.transform.rotation = Math.atan2(
2317 toPivot.y - fromPivot.y,
2318 toPivot.x - fromPivot.x
2319 ) * (180 / Math.PI);
2313 2320 }
2314 return true;
2321 this.#placePivoted(swipe.object, x, y, sortY, SORT_FX);
2322 this.#applyPhysicalCombatFxDarkness(
2323 swipe.object,
2324 swipe.fromTileX,
2325 swipe.fromTileY,
2326 swipe.tileX,
2327 swipe.tileY
2328 );
2329 }
2330 else if (swipe.fromTileX != null)
2331 {
2332 this.#applyPhysicalCombatFxDarkness(
2333 swipe.object,
2334 swipe.fromTileX,
2335 swipe.fromTileY,
2336 swipe.tileX,
2337 swipe.tileY
2338 );
2315 2339 }
2340 if (swipe.until > now)
2341 { return true; }
2316 2342 swipe.object?.destroy();
2317 2343 return false;
2318 2344 });
@@ -856,21 +856,19 @@ export class WorldView
856 /** Physical combat FX (melee swipes, ranged projectiles). Magic spells stay unshaded. */
856 /** Physical combat FX (melee swipes, ranged projectiles). Magic spells stay unshaded. */
857 spawnPhysicalProjectileFx (payload)
857 spawnPhysicalProjectileFx (payload)
858 {
858 {
859 const spriteName = payload.sprite ?? "fx:projectile:projectile";
859 const spriteName = payload.sprite ?? "fx:projectile:arrow";
860 const image = this.game.images[spriteName];
860 const image = this.game.images[spriteName];
861 if (!image)
861 if (!image)
862 { return; }
862 { return; }
863 const fromPivot = tilePivot(payload.fromTileX, payload.fromTileY, this.tileSize());
863 const fromPivot = tilePivot(payload.fromTileX, payload.fromTileY, this.tileSize());
864 const toPivot = tilePivot(payload.tileX, payload.tileY, this.tileSize());
864 const toPivot = tilePivot(payload.tileX, payload.tileY, this.tileSize());
865 const midX = (fromPivot.x + toPivot.x) / 2;
866 const midY = (fromPivot.y + toPivot.y) / 2;
867 const sortY = Math.max(fromPivot.y, toPivot.y) + 0.02;
865 const sortY = Math.max(fromPivot.y, toPivot.y) + 0.02;
868 const angle = Math.atan2(toPivot.y - fromPivot.y, toPivot.x - fromPivot.x) * (180 / Math.PI);
866 const angle = Math.atan2(toPivot.y - fromPivot.y, toPivot.x - fromPivot.x) * (180 / Math.PI);
869 const object = new GameObject(0, 0, image, SORT_FX);
867 const object = new GameObject(0, 0, image, SORT_FX);
870 if (object.transform)
868 if (object.transform)
871 { object.transform.rotation = angle; }
869 { object.transform.rotation = angle; }
872 this.scene.addObject(object);
870 this.scene.addObject(object);
873 this.#placePivoted(object, midX, midY, sortY, SORT_FX);
871 this.#placePivoted(object, fromPivot.x, fromPivot.y, sortY, SORT_FX);
874 this.#applyPhysicalCombatFxDarkness(
872 this.#applyPhysicalCombatFxDarkness(
875 object,
873 object,
876 payload.fromTileX,
874 payload.fromTileX,
@@ -878,9 +876,14 @@ export class WorldView
878 payload.tileX,
876 payload.tileX,
879 payload.tileY
877 payload.tileY
880 );
878 );
879 const started = performance.now();
880 const durationMs = payload.durationMs ?? 400;
881 this.swipes.push({
881 this.swipes.push({
882 object,
882 object,
883 until: performance.now() + (payload.durationMs ?? 500),
883 until: started + durationMs,
884 started,
885 durationMs,
886 lerp: true,
884 fromTileX: payload.fromTileX,
887 fromTileX: payload.fromTileX,
885 fromTileY: payload.fromTileY,
888 fromTileY: payload.fromTileY,
886 tileX: payload.tileX,
889 tileX: payload.tileX,
@@ -2299,20 +2302,43 @@ export class WorldView
2299 {
2302 {
2300 this.swipes = this.swipes.filter((swipe) =>
2303 this.swipes = this.swipes.filter((swipe) =>
2301 {
2304 {
2302 if (swipe.until > now)
2305 if (swipe.lerp && swipe.started != null)
2303 {
2306 {
2304 if (swipe.fromTileX != null)
2307 const durationMs = swipe.durationMs ?? 400;
2308 const t = Math.min(1, (now - swipe.started) / durationMs);
2309 const fromPivot = tilePivot(swipe.fromTileX, swipe.fromTileY, this.tileSize());
2310 const toPivot = tilePivot(swipe.tileX, swipe.tileY, this.tileSize());
2311 const x = fromPivot.x + (toPivot.x - fromPivot.x) * t;
2312 const y = fromPivot.y + (toPivot.y - fromPivot.y) * t;
2313 const sortY = Math.max(fromPivot.y, toPivot.y) + 0.02;
2314 if (swipe.object?.transform)
2305 {
2315 {
2306 this.#applyPhysicalCombatFxDarkness(
2316 swipe.object.transform.rotation = Math.atan2(
2307 swipe.object,
2317 toPivot.y - fromPivot.y,
2308 swipe.fromTileX,
2318 toPivot.x - fromPivot.x
2309 swipe.fromTileY,
2319 ) * (180 / Math.PI);
2310 swipe.tileX,
2311 swipe.tileY
2312 );
2313 }
2320 }
2314 return true;
2321 this.#placePivoted(swipe.object, x, y, sortY, SORT_FX);
2322 this.#applyPhysicalCombatFxDarkness(
2323 swipe.object,
2324 swipe.fromTileX,
2325 swipe.fromTileY,
2326 swipe.tileX,
2327 swipe.tileY
2328 );
2329 }
2330 else if (swipe.fromTileX != null)
2331 {
2332 this.#applyPhysicalCombatFxDarkness(
2333 swipe.object,
2334 swipe.fromTileX,
2335 swipe.fromTileY,
2336 swipe.tileX,
2337 swipe.tileY
2338 );
2315 }
2339 }
2340 if (swipe.until > now)
2341 { return true; }
2316 swipe.object?.destroy();
2342 swipe.object?.destroy();
2317 return false;
2343 return false;
2318 });
2344 });
server/src/crafting/service.js +46 −2
@@ -188,12 +188,24 @@ export function startCraft (world, sessionId, characterId, stationId, recipeId,
188 188
189 189 const sessions = ensureCraftSessions(world);
190 190 const durationMs = craftDurationMs(recipe, CRAFT_DURATION_MS);
191 let batchRemaining = 1;
192 if (stationId === CraftingStationId.craftingBench)
193 {
194 if (options.batchAll)
195 { batchRemaining = Infinity; }
196 else if (options.batchCount != null)
197 {
198 batchRemaining = Math.max(1, options.batchCount | 0);
199 }
200 }
191 201 sessions.set(characterId, {
192 202 sessionId,
193 203 stationId,
194 204 recipeId,
195 205 recipe,
196 206 dyeSlotIndex,
207 batchRemaining,
208 durationMs,
197 209 finishesAt: now + durationMs
198 210 });
199 211 const events = [];
@@ -403,11 +415,41 @@ export function stepCrafting (world, now)
403 415 levelUps
404 416 });
405 417 }
418
419 let continuing = false;
420 const batchRemaining = session.batchRemaining ?? 1;
421 const nextRemaining = batchRemaining === Infinity
422 ? Infinity
423 : Math.max(0, (batchRemaining | 0) - 1);
424 if (nextRemaining !== 0 && canStartCraft(
425 inventory.slots,
426 recipe,
427 level,
428 { dyeSlotIndex: session.dyeSlotIndex }
429 ))
430 {
431 const durationMs = session.durationMs ?? craftDurationMs(recipe, CRAFT_DURATION_MS);
432 sessions.set(characterId, {
433 ...session,
434 recipe,
435 batchRemaining: nextRemaining,
436 durationMs,
437 finishesAt: now + durationMs
438 });
439 continuing = true;
440 events.push({
441 type: "item-bubble",
442 entityId,
443 itemId: recipe.output.itemId,
444 quantity: Math.max(1, recipe.output.quantity | 0 || 1)
445 });
446 }
406 447 events.push({
407 448 type: "craft-result",
408 449 sessionId: session.sessionId,
409 450 recipeId: session.recipeId,
410 ok: true
451 ok: true,
452 continuing
411 453 });
412 454 }
413 455 return { events, privates };
@@ -429,7 +471,9 @@ export function handleCraftCommand (world, sessionId, command, now)
429 471 now,
430 472 {
431 473 clothTier: command.clothTier ?? null,
432 dyeSlotIndex: command.dyeSlotIndex ?? null
474 dyeSlotIndex: command.dyeSlotIndex ?? null,
475 batchCount: command.batchCount ?? null,
476 batchAll: !!command.batchAll
433 477 }
434 478 );
435 479 case "cancel-craft":
@@ -188,12 +188,24 @@ export function startCraft (world, sessionId, characterId, stationId, recipeId,
188
188
189 const sessions = ensureCraftSessions(world);
189 const sessions = ensureCraftSessions(world);
190 const durationMs = craftDurationMs(recipe, CRAFT_DURATION_MS);
190 const durationMs = craftDurationMs(recipe, CRAFT_DURATION_MS);
191 let batchRemaining = 1;
192 if (stationId === CraftingStationId.craftingBench)
193 {
194 if (options.batchAll)
195 { batchRemaining = Infinity; }
196 else if (options.batchCount != null)
197 {
198 batchRemaining = Math.max(1, options.batchCount | 0);
199 }
200 }
191 sessions.set(characterId, {
201 sessions.set(characterId, {
192 sessionId,
202 sessionId,
193 stationId,
203 stationId,
194 recipeId,
204 recipeId,
195 recipe,
205 recipe,
196 dyeSlotIndex,
206 dyeSlotIndex,
207 batchRemaining,
208 durationMs,
197 finishesAt: now + durationMs
209 finishesAt: now + durationMs
198 });
210 });
199 const events = [];
211 const events = [];
@@ -403,11 +415,41 @@ export function stepCrafting (world, now)
403 levelUps
415 levelUps
404 });
416 });
405 }
417 }
418
419 let continuing = false;
420 const batchRemaining = session.batchRemaining ?? 1;
421 const nextRemaining = batchRemaining === Infinity
422 ? Infinity
423 : Math.max(0, (batchRemaining | 0) - 1);
424 if (nextRemaining !== 0 && canStartCraft(
425 inventory.slots,
426 recipe,
427 level,
428 { dyeSlotIndex: session.dyeSlotIndex }
429 ))
430 {
431 const durationMs = session.durationMs ?? craftDurationMs(recipe, CRAFT_DURATION_MS);
432 sessions.set(characterId, {
433 ...session,
434 recipe,
435 batchRemaining: nextRemaining,
436 durationMs,
437 finishesAt: now + durationMs
438 });
439 continuing = true;
440 events.push({
441 type: "item-bubble",
442 entityId,
443 itemId: recipe.output.itemId,
444 quantity: Math.max(1, recipe.output.quantity | 0 || 1)
445 });
446 }
406 events.push({
447 events.push({
407 type: "craft-result",
448 type: "craft-result",
408 sessionId: session.sessionId,
449 sessionId: session.sessionId,
409 recipeId: session.recipeId,
450 recipeId: session.recipeId,
410 ok: true
451 ok: true,
452 continuing
411 });
453 });
412 }
454 }
413 return { events, privates };
455 return { events, privates };
@@ -429,7 +471,9 @@ export function handleCraftCommand (world, sessionId, command, now)
429 now,
471 now,
430 {
472 {
431 clothTier: command.clothTier ?? null,
473 clothTier: command.clothTier ?? null,
432 dyeSlotIndex: command.dyeSlotIndex ?? null
474 dyeSlotIndex: command.dyeSlotIndex ?? null,
475 batchCount: command.batchCount ?? null,
476 batchAll: !!command.batchAll
433 }
477 }
434 );
478 );
435 case "cancel-craft":
479 case "cancel-craft":
server/src/gateway/websocket.js +29 −3
@@ -344,7 +344,9 @@ export function attachGateway ({ httpServer, auth, stateDb, characters, friends,
344 344 stationId: message.stationId,
345 345 recipeId: message.recipeId,
346 346 clothTier: message.clothTier ?? null,
347 dyeSlotIndex: message.dyeSlotIndex ?? null
347 dyeSlotIndex: message.dyeSlotIndex ?? null,
348 batchCount: message.batchCount ?? null,
349 batchAll: !!message.batchAll
348 350 });
349 351 break;
350 352 case MessageType.cancelCraft:
@@ -1051,6 +1053,13 @@ export function attachGateway ({ httpServer, auth, stateDb, characters, friends,
1051 1053 }
1052 1054 continue;
1053 1055 }
1056 if (event.type === "combat-no-arrows")
1057 {
1058 const session = sessions.get(event.sessionId);
1059 if (session)
1060 { send(session, { type: MessageType.combatNoArrows }); }
1061 continue;
1062 }
1054 1063 if (event.type === "craft-result")
1055 1064 {
1056 1065 const session = sessions.get(event.sessionId);
@@ -1061,7 +1070,8 @@ export function attachGateway ({ httpServer, auth, stateDb, characters, friends,
1061 1070 recipeId: event.recipeId,
1062 1071 ok: !!event.ok,
1063 1072 error: event.error ?? null,
1064 burned: !!event.burned
1073 burned: !!event.burned,
1074 continuing: !!event.continuing
1065 1075 });
1066 1076 }
1067 1077 continue;
@@ -1241,7 +1251,7 @@ export function attachGateway ({ httpServer, auth, stateDb, characters, friends,
1241 1251 }
1242 1252 continue;
1243 1253 }
1244 if (event.type === "entity-move" || event.type === "ground-item-appear" || event.type === "ground-item-despawn" || event.type === "entity-appearance" || event.type === "entity-despawn" || event.type === "entity-spawn" || event.type === "combat-swing" || event.type === "combat-hit" || event.type === "combat-miss" || event.type === "combat-block" || event.type === "entity-health" || event.type === "gather-swing" || event.type === "tile-shove" || event.type === "chunk-delta" || event.type === "skill-level-up")
1254 if (event.type === "entity-move" || event.type === "ground-item-appear" || event.type === "ground-item-despawn" || event.type === "entity-appearance" || event.type === "entity-despawn" || event.type === "entity-spawn" || event.type === "combat-swing" || event.type === "combat-hit" || event.type === "combat-miss" || event.type === "combat-block" || event.type === "combat-projectile" || event.type === "entity-health" || event.type === "gather-swing" || event.type === "tile-shove" || event.type === "chunk-delta" || event.type === "skill-level-up")
1245 1255 { publishEntity(event); }
1246 1256 }
1247 1257 if (simulation.tickIndex % simulation.settings.simulationTickHz === 0)
@@ -1333,6 +1343,22 @@ export function attachGateway ({ httpServer, auth, stateDb, characters, friends,
1333 1343 });
1334 1344 continue;
1335 1345 }
1346 if (event.type === "combat-projectile")
1347 {
1348 if (!seesTo && !seesFrom)
1349 { continue; }
1350 send(session, {
1351 type: MessageType.combatProjectile,
1352 attackerId: event.attackerId,
1353 targetId: event.targetId,
1354 fromTileX: event.fromTileX,
1355 fromTileY: event.fromTileY,
1356 tileX: event.tileX,
1357 tileY: event.tileY,
1358 durationMs: event.durationMs ?? 400
1359 });
1360 continue;
1361 }
1336 1362 if (event.type === "entity-health")
1337 1363 {
1338 1364 if (!seesTo && !seesFrom)
@@ -344,7 +344,9 @@ export function attachGateway ({ httpServer, auth, stateDb, characters, friends,
344 stationId: message.stationId,
344 stationId: message.stationId,
345 recipeId: message.recipeId,
345 recipeId: message.recipeId,
346 clothTier: message.clothTier ?? null,
346 clothTier: message.clothTier ?? null,
347 dyeSlotIndex: message.dyeSlotIndex ?? null
347 dyeSlotIndex: message.dyeSlotIndex ?? null,
348 batchCount: message.batchCount ?? null,
349 batchAll: !!message.batchAll
348 });
350 });
349 break;
351 break;
350 case MessageType.cancelCraft:
352 case MessageType.cancelCraft:
@@ -1051,6 +1053,13 @@ export function attachGateway ({ httpServer, auth, stateDb, characters, friends,
1051 }
1053 }
1052 continue;
1054 continue;
1053 }
1055 }
1056 if (event.type === "combat-no-arrows")
1057 {
1058 const session = sessions.get(event.sessionId);
1059 if (session)
1060 { send(session, { type: MessageType.combatNoArrows }); }
1061 continue;
1062 }
1054 if (event.type === "craft-result")
1063 if (event.type === "craft-result")
1055 {
1064 {
1056 const session = sessions.get(event.sessionId);
1065 const session = sessions.get(event.sessionId);
@@ -1061,7 +1070,8 @@ export function attachGateway ({ httpServer, auth, stateDb, characters, friends,
1061 recipeId: event.recipeId,
1070 recipeId: event.recipeId,
1062 ok: !!event.ok,
1071 ok: !!event.ok,
1063 error: event.error ?? null,
1072 error: event.error ?? null,
1064 burned: !!event.burned
1073 burned: !!event.burned,
1074 continuing: !!event.continuing
1065 });
1075 });
1066 }
1076 }
1067 continue;
1077 continue;
@@ -1241,7 +1251,7 @@ export function attachGateway ({ httpServer, auth, stateDb, characters, friends,
1241 }
1251 }
1242 continue;
1252 continue;
1243 }
1253 }
1244 if (event.type === "entity-move" || event.type === "ground-item-appear" || event.type === "ground-item-despawn" || event.type === "entity-appearance" || event.type === "entity-despawn" || event.type === "entity-spawn" || event.type === "combat-swing" || event.type === "combat-hit" || event.type === "combat-miss" || event.type === "combat-block" || event.type === "entity-health" || event.type === "gather-swing" || event.type === "tile-shove" || event.type === "chunk-delta" || event.type === "skill-level-up")
1254 if (event.type === "entity-move" || event.type === "ground-item-appear" || event.type === "ground-item-despawn" || event.type === "entity-appearance" || event.type === "entity-despawn" || event.type === "entity-spawn" || event.type === "combat-swing" || event.type === "combat-hit" || event.type === "combat-miss" || event.type === "combat-block" || event.type === "combat-projectile" || event.type === "entity-health" || event.type === "gather-swing" || event.type === "tile-shove" || event.type === "chunk-delta" || event.type === "skill-level-up")
1245 { publishEntity(event); }
1255 { publishEntity(event); }
1246 }
1256 }
1247 if (simulation.tickIndex % simulation.settings.simulationTickHz === 0)
1257 if (simulation.tickIndex % simulation.settings.simulationTickHz === 0)
@@ -1333,6 +1343,22 @@ export function attachGateway ({ httpServer, auth, stateDb, characters, friends,
1333 });
1343 });
1334 continue;
1344 continue;
1335 }
1345 }
1346 if (event.type === "combat-projectile")
1347 {
1348 if (!seesTo && !seesFrom)
1349 { continue; }
1350 send(session, {
1351 type: MessageType.combatProjectile,
1352 attackerId: event.attackerId,
1353 targetId: event.targetId,
1354 fromTileX: event.fromTileX,
1355 fromTileY: event.fromTileY,
1356 tileX: event.tileX,
1357 tileY: event.tileY,
1358 durationMs: event.durationMs ?? 400
1359 });
1360 continue;
1361 }
1336 if (event.type === "entity-health")
1362 if (event.type === "entity-health")
1337 {
1363 {
1338 if (!seesTo && !seesFrom)
1364 if (!seesTo && !seesFrom)
server/src/items/bags.js +65 −17
@@ -1,6 +1,8 @@
1 1 import { Component } from "../ecs/components.js";
2 2 import { syncPlayerHealth } from "../skills/service.js";
3 3 import { canEquipItem } from "../../../shared/equipment/stats.js";
4 import { EquipmentKind } from "../../../shared/definitions/equipmentKinds.js";
5 import { stackRulesFor } from "../../../shared/items/stackRules.js";
4 6 import { BlockType } from "../../../shared/definitions/tiles.js";
5 7 import { ItemId, itemDefinition, itemSpriteName, stackDisplayLabel, LETTER_MAX_BODY_CHARS, BLANK_CHEQUE_CARRY_CAP } from "../../../shared/definitions/items.js";
6 8 import {
@@ -273,28 +275,71 @@ function equipItem (world, sessionId, command)
273 275 const source = inventory.slots[slotIndex];
274 276 if (!source || stackQuantity(source.quantity) <= 0)
275 277 { throw userError("That inventory slot is empty."); }
276 const taken = takeFromSlot(inventory.slots, slotIndex, 1);
277 if (!taken.ok || stackQuantity(taken.taken?.quantity) !== 1)
278 const def = itemDefinition(source.itemId);
279 const slot = def?.equipment?.slot;
280 if (!slot)
281 { throw userError("That item cannot be equipped."); }
282 const skills = world.ecs.get(handle, Component.skills);
283 const equipCheck = canEquipItem(skills, def);
284 if (!equipCheck.ok)
285 { throw userError(equipCheck.reason); }
286
287 const isArrowAmmo = def.equipment?.kind === EquipmentKind.arrowAmmo;
288 const taken = takeFromSlot(inventory.slots, slotIndex, isArrowAmmo ? null : 1);
289 if (!taken.ok || stackQuantity(taken.taken?.quantity) <= 0)
278 290 { throw userError("That inventory slot is empty."); }
279 291 if (taken.taken.itemId === ItemId.coins)
280 292 {
281 293 inventory.slots = insertStack(taken.slots, taken.taken, itemDefinition(ItemId.coins)).slots;
282 294 throw userError("Coins cannot be equipped.");
283 295 }
284 const def = itemDefinition(taken.taken.itemId);
285 const slot = def?.equipment?.slot;
286 if (!slot)
287 {
288 inventory.slots = insertStack(taken.slots, taken.taken, def).slots;
289 throw userError("That item cannot be equipped.");
290 }
291 const skills = world.ecs.get(handle, Component.skills);
292 const equipCheck = canEquipItem(skills, def);
293 if (!equipCheck.ok)
296
297 if (isArrowAmmo)
294 298 {
295 inventory.slots = insertStack(taken.slots, taken.taken, def).slots;
296 throw userError(equipCheck.reason);
299 let slots = taken.slots;
300 const previous = equipment.slots[slot];
301 if (previous)
302 {
303 const previousDef = itemDefinition(previous.itemId);
304 if (previous.itemId === taken.taken.itemId)
305 {
306 const rules = stackRulesFor(def);
307 const space = rules.maxStackSize - stackQuantity(previous.quantity);
308 const add = Math.min(space, stackQuantity(taken.taken.quantity));
309 if (add <= 0)
310 {
311 inventory.slots = insertStack(slots, taken.taken, def).slots;
312 throw userError("Your arrow stack is full.");
313 }
314 previous.quantity = stackQuantity(previous.quantity) + add;
315 const leftover = stackQuantity(taken.taken.quantity) - add;
316 if (leftover > 0)
317 {
318 const back = insertStack(slots, { ...taken.taken, quantity: leftover }, def);
319 if (!back.ok)
320 {
321 previous.quantity = stackQuantity(previous.quantity) - add;
322 inventory.slots = insertStack(slots, taken.taken, def).slots;
323 throw userError("No inventory space for leftover arrows.");
324 }
325 slots = back.slots;
326 }
327 inventory.slots = slots;
328 return { events: [], private: bagView(world.ecs, handle), appearanceChanged: false };
329 }
330 const back = insertStack(slots, previous, previousDef);
331 if (!back.ok)
332 {
333 inventory.slots = insertStack(slots, taken.taken, def).slots;
334 throw userError("No inventory space to unequip the current item.");
335 }
336 slots = back.slots;
337 }
338 inventory.slots = slots;
339 equipment.slots[slot] = taken.taken;
340 return { events: [], private: bagView(world.ecs, handle), appearanceChanged: false };
297 341 }
342
298 343 const toEquip = singleItemFromStack(taken.taken);
299 344 const previous = equipment.slots[slot];
300 345 if (previous)
@@ -323,13 +368,16 @@ function unequipItem (world, sessionId, command)
323 368 if (!worn)
324 369 { throw userError("Nothing is equipped there."); }
325 370 const def = itemDefinition(worn.itemId);
326 const single = singleItemFromStack(worn);
327 const inserted = insertStack(inventory.slots, single, def);
371 const toStore = def?.equipment?.kind === EquipmentKind.arrowAmmo
372 ? cloneStack(worn)
373 : singleItemFromStack(worn);
374 const inserted = insertStack(inventory.slots, toStore, def);
328 375 if (!inserted.ok)
329 376 { throw userError("Your inventory cannot hold that."); }
330 377 inventory.slots = inserted.slots;
331 378 equipment.slots[command.equipSlot] = null;
332 return { events: [], private: bagView(world.ecs, handle), appearanceChanged: true };
379 const appearanceChanged = def?.equipment?.kind !== EquipmentKind.arrowAmmo;
380 return { events: [], private: bagView(world.ecs, handle), appearanceChanged };
333 381 }
334 382
335 383 function eatItem (world, sessionId, command)
@@ -1,6 +1,8 @@
1import { Component } from "../ecs/components.js";
1import { Component } from "../ecs/components.js";
2import { syncPlayerHealth } from "../skills/service.js";
2import { syncPlayerHealth } from "../skills/service.js";
3import { canEquipItem } from "../../../shared/equipment/stats.js";
3import { canEquipItem } from "../../../shared/equipment/stats.js";
4import { EquipmentKind } from "../../../shared/definitions/equipmentKinds.js";
5import { stackRulesFor } from "../../../shared/items/stackRules.js";
4import { BlockType } from "../../../shared/definitions/tiles.js";
6import { BlockType } from "../../../shared/definitions/tiles.js";
5import { ItemId, itemDefinition, itemSpriteName, stackDisplayLabel, LETTER_MAX_BODY_CHARS, BLANK_CHEQUE_CARRY_CAP } from "../../../shared/definitions/items.js";
7import { ItemId, itemDefinition, itemSpriteName, stackDisplayLabel, LETTER_MAX_BODY_CHARS, BLANK_CHEQUE_CARRY_CAP } from "../../../shared/definitions/items.js";
6import {
8import {
@@ -273,28 +275,71 @@ function equipItem (world, sessionId, command)
273 const source = inventory.slots[slotIndex];
275 const source = inventory.slots[slotIndex];
274 if (!source || stackQuantity(source.quantity) <= 0)
276 if (!source || stackQuantity(source.quantity) <= 0)
275 { throw userError("That inventory slot is empty."); }
277 { throw userError("That inventory slot is empty."); }
276 const taken = takeFromSlot(inventory.slots, slotIndex, 1);
278 const def = itemDefinition(source.itemId);
277 if (!taken.ok || stackQuantity(taken.taken?.quantity) !== 1)
279 const slot = def?.equipment?.slot;
280 if (!slot)
281 { throw userError("That item cannot be equipped."); }
282 const skills = world.ecs.get(handle, Component.skills);
283 const equipCheck = canEquipItem(skills, def);
284 if (!equipCheck.ok)
285 { throw userError(equipCheck.reason); }
286
287 const isArrowAmmo = def.equipment?.kind === EquipmentKind.arrowAmmo;
288 const taken = takeFromSlot(inventory.slots, slotIndex, isArrowAmmo ? null : 1);
289 if (!taken.ok || stackQuantity(taken.taken?.quantity) <= 0)
278 { throw userError("That inventory slot is empty."); }
290 { throw userError("That inventory slot is empty."); }
279 if (taken.taken.itemId === ItemId.coins)
291 if (taken.taken.itemId === ItemId.coins)
280 {
292 {
281 inventory.slots = insertStack(taken.slots, taken.taken, itemDefinition(ItemId.coins)).slots;
293 inventory.slots = insertStack(taken.slots, taken.taken, itemDefinition(ItemId.coins)).slots;
282 throw userError("Coins cannot be equipped.");
294 throw userError("Coins cannot be equipped.");
283 }
295 }
284 const def = itemDefinition(taken.taken.itemId);
296
285 const slot = def?.equipment?.slot;
297 if (isArrowAmmo)
286 if (!slot)
287 {
288 inventory.slots = insertStack(taken.slots, taken.taken, def).slots;
289 throw userError("That item cannot be equipped.");
290 }
291 const skills = world.ecs.get(handle, Component.skills);
292 const equipCheck = canEquipItem(skills, def);
293 if (!equipCheck.ok)
294 {
298 {
295 inventory.slots = insertStack(taken.slots, taken.taken, def).slots;
299 let slots = taken.slots;
296 throw userError(equipCheck.reason);
300 const previous = equipment.slots[slot];
301 if (previous)
302 {
303 const previousDef = itemDefinition(previous.itemId);
304 if (previous.itemId === taken.taken.itemId)
305 {
306 const rules = stackRulesFor(def);
307 const space = rules.maxStackSize - stackQuantity(previous.quantity);
308 const add = Math.min(space, stackQuantity(taken.taken.quantity));
309 if (add <= 0)
310 {
311 inventory.slots = insertStack(slots, taken.taken, def).slots;
312 throw userError("Your arrow stack is full.");
313 }
314 previous.quantity = stackQuantity(previous.quantity) + add;
315 const leftover = stackQuantity(taken.taken.quantity) - add;
316 if (leftover > 0)
317 {
318 const back = insertStack(slots, { ...taken.taken, quantity: leftover }, def);
319 if (!back.ok)
320 {
321 previous.quantity = stackQuantity(previous.quantity) - add;
322 inventory.slots = insertStack(slots, taken.taken, def).slots;
323 throw userError("No inventory space for leftover arrows.");
324 }
325 slots = back.slots;
326 }
327 inventory.slots = slots;
328 return { events: [], private: bagView(world.ecs, handle), appearanceChanged: false };
329 }
330 const back = insertStack(slots, previous, previousDef);
331 if (!back.ok)
332 {
333 inventory.slots = insertStack(slots, taken.taken, def).slots;
334 throw userError("No inventory space to unequip the current item.");
335 }
336 slots = back.slots;
337 }
338 inventory.slots = slots;
339 equipment.slots[slot] = taken.taken;
340 return { events: [], private: bagView(world.ecs, handle), appearanceChanged: false };
297 }
341 }
342
298 const toEquip = singleItemFromStack(taken.taken);
343 const toEquip = singleItemFromStack(taken.taken);
299 const previous = equipment.slots[slot];
344 const previous = equipment.slots[slot];
300 if (previous)
345 if (previous)
@@ -323,13 +368,16 @@ function unequipItem (world, sessionId, command)
323 if (!worn)
368 if (!worn)
324 { throw userError("Nothing is equipped there."); }
369 { throw userError("Nothing is equipped there."); }
325 const def = itemDefinition(worn.itemId);
370 const def = itemDefinition(worn.itemId);
326 const single = singleItemFromStack(worn);
371 const toStore = def?.equipment?.kind === EquipmentKind.arrowAmmo
327 const inserted = insertStack(inventory.slots, single, def);
372 ? cloneStack(worn)
373 : singleItemFromStack(worn);
374 const inserted = insertStack(inventory.slots, toStore, def);
328 if (!inserted.ok)
375 if (!inserted.ok)
329 { throw userError("Your inventory cannot hold that."); }
376 { throw userError("Your inventory cannot hold that."); }
330 inventory.slots = inserted.slots;
377 inventory.slots = inserted.slots;
331 equipment.slots[command.equipSlot] = null;
378 equipment.slots[command.equipSlot] = null;
332 return { events: [], private: bagView(world.ecs, handle), appearanceChanged: true };
379 const appearanceChanged = def?.equipment?.kind !== EquipmentKind.arrowAmmo;
380 return { events: [], private: bagView(world.ecs, handle), appearanceChanged };
333}
381}
334
382
335function eatItem (world, sessionId, command)
383function eatItem (world, sessionId, command)