import { Component } from "../ecs/components.js"; import { barkItemIdForWood, debarkedLogInstance, isLogDebarked } from "../../../shared/definitions/woodItems.js"; import { ItemFamilyId } from "../../../shared/definitions/tierFamilies.js"; import { itemDefinition } from "../../../shared/definitions/items.js"; import { resolveWoodcuttingAxe } from "../../../shared/definitions/gathering.js"; import { bagView, addGroundItem, publicGroundItem } from "../items/bags.js"; import { isAdjacentTile } from "../../../shared/navigation/adjacency.js"; import { attachOrigins, craftOrigin, CraftOriginVerb } from "../../../shared/items/origin.js"; import { stackQuantity } from "../../../shared/items/inventory.js"; function userError (message) { const error = new Error(message); error.userFacing = true; return error; } function characterDisplayName (world, handle) { const appearance = world.ecs.get(handle, Component.appearance); const name = [appearance?.firstName, appearance?.lastName].filter(Boolean).join(" ").trim(); return name || "someone"; } function woodTierFromLogItemId (itemId) { const def = itemDefinition(itemId); if (!def || def.familyId !== ItemFamilyId.log) { return null; } return def.tier ?? null; } function axeForWood (world, handle, woodTier) { const inventory = world.ecs.get(handle, Component.inventory); const equipment = world.ecs.get(handle, Component.equipment); const skills = world.ecs.get(handle, Component.skills); return resolveWoodcuttingAxe({ inventorySlots: inventory?.slots, equipmentSlots: equipment?.slots, skills, woodTier }); } function publishGround (item) { return { type: "ground-item-appear", ...publicGroundItem(item), stack: item.stack, droppedAt: item.droppedAt, source: item.source, droppedByCharacterId: item.droppedByCharacterId, expiresAt: item.expiresAt }; } /** * Strip bark from a ground wood log. Requires an axe of the same tier gate as chopping * that wood. Leaves a debarked log (same itemId + instance flag) and bark. */ export function debarkLog (world, sessionId, characterId, groundItemId, now = Date.now()) { const { handle } = world.requireOwnedEntity(sessionId, characterId); const item = world.groundItems.get(groundItemId); if (!item?.stack) { throw userError("That item is no longer on the ground."); } const woodTier = woodTierFromLogItemId(item.stack.itemId); if (!woodTier) { throw userError("That is not a log."); } if (isLogDebarked(item.stack)) { throw userError("That log is already stripped of bark."); } const position = world.ecs.get(handle, Component.position); if (!isAdjacentTile(position.tileX, position.tileY, item.tileX, item.tileY) && !(position.tileX === item.tileX && position.tileY === item.tileY)) { throw userError("Stand next to the log."); } const axe = axeForWood(world, handle, woodTier); if (!axe.ok) { throw userError(axe.reason || "You need an axe to strip this bark."); } const barkId = barkItemIdForWood(woodTier); if (barkId == null) { throw userError("That bark cannot be stripped."); } const qty = stackQuantity(item.stack.quantity); if (qty <= 0) { throw userError("That item is no longer on the ground."); } const events = []; const despawnMs = world.settings.groundItemDespawnDurationMs ?? 600_000; const origin = craftOrigin(CraftOriginVerb.made, characterDisplayName(world, handle)); const debarkedStack = attachOrigins({ itemId: item.stack.itemId, quantity: 1, instance: debarkedLogInstance(), origins: item.stack.origins ?? [] }); if (qty === 1) { item.stack = debarkedStack; events.push(publishGround(item)); } else { item.stack = { ...item.stack, quantity: qty - 1 }; events.push(publishGround(item)); const debarkedItem = { groundItemId: (world.nextGroundItemId = (world.nextGroundItemId ?? 1) + 1), tileX: item.tileX, tileY: item.tileY, stack: debarkedStack, droppedAt: now, expiresAt: now + despawnMs, source: "debark", droppedByCharacterId: characterId }; addGroundItem(world, debarkedItem); events.push(publishGround(debarkedItem)); } const barkItem = { groundItemId: (world.nextGroundItemId = (world.nextGroundItemId ?? 1) + 1), tileX: item.tileX, tileY: item.tileY, stack: attachOrigins({ itemId: barkId, quantity: 1, origins: [{ origin, quantity: 1 }] }), droppedAt: now, expiresAt: now + despawnMs, source: "debark", droppedByCharacterId: characterId }; addGroundItem(world, barkItem); events.push(publishGround(barkItem)); return { events, private: bagView(world.ecs, handle) }; } export function handleDebarkCommand (world, sessionId, command, now = Date.now()) { if (command.type !== "debark-log") { return null; } return debarkLog(world, sessionId, command.characterId, command.groundItemId, now); }