import { Component } from "../ecs/components.js"; import { BlockType } from "../../../shared/definitions/tiles.js"; import { itemDefinition } from "../../../shared/definitions/items.js"; import { EquipmentKind } from "../../../shared/definitions/equipmentKinds.js"; import { SkillId, skillLevel } from "../../../shared/definitions/skills.js"; import { materialFromOreBoulderState, canMineMaterial, oreItemIdForMaterial, miningDurationMs, miningXpForMaterial, oreExhaustDurationMs, oreBoulderState, EXHAUSTED_ORE_BOULDER_STATE } from "../../../shared/definitions/gathering.js"; import { insertStack } from "../../../shared/items/inventory.js"; import { addSkillXp, publicSkillsView, storedSkillXp } from "../../../shared/skills/state.js"; import { bagView } from "../items/bags.js"; import { chebyshev, clearEngage } from "../combat/service.js"; import { tileKey, tileToChunk, tileToLocal } from "../world/coordinates.js"; import { CHUNK_SIZE } from "../../../shared/protocol/constants.js"; const GATHER_SWING_INTERVAL_MS = 900; function userError (message) { const error = new Error(message); error.userFacing = true; return error; } function ensureOreRespawns (world) { if (!world.oreRespawns) { world.oreRespawns = new Map(); } return world.oreRespawns; } function blockAt (world, tileX, tileY) { const data = world.chunkData(tileToChunk(tileX), tileToChunk(tileY)); return data?.blocks?.[tileToLocal(tileY) * CHUNK_SIZE + tileToLocal(tileX)] ?? null; } function setBoulderState (world, tileX, tileY, state) { const chunkX = tileToChunk(tileX); const chunkY = tileToChunk(tileY); const localX = tileToLocal(tileX); const localY = tileToLocal(tileY); const data = world.chunkData(chunkX, chunkY); const index = localY * CHUNK_SIZE + localX; const prev = data?.blocks?.[index]; if (!prev || prev.typeId !== BlockType.boulder) { return null; } const block = { typeId: prev.typeId, state, config: prev.config ?? null }; data.blocks[index] = block; return { type: "chunk-delta", chunkX, chunkY, tileX, tileY, localX, localY, block }; } function equippedPickaxe (ecs, handle) { const equipment = ecs.get(handle, Component.equipment); const stack = equipment?.slots?.weapon; if (!stack?.itemId) { return null; } const def = itemDefinition(stack.itemId); if (def?.equipment?.kind !== EquipmentKind.pickaxeTool) { return null; } return { stack, def, tier: def.equipment.tier }; } function clearGather (ecs, handle) { if (ecs.has(handle, Component.gather)) { ecs.remove(handle, Component.gather); } } export function cancelGather (ecs, handle) { clearGather(ecs, handle); } /** Drop scheduled ore respawns when authored chunk data is replaced or unloaded. */ export function clearOreRespawnsForChunk (world, chunkX, chunkY) { const respawns = world.oreRespawns; if (!respawns?.size) { return; } for (const key of [...respawns.keys()]) { const [tileX, tileY] = key.split(",").map(Number); if (tileToChunk(tileX) === chunkX && tileToChunk(tileY) === chunkY) { respawns.delete(key); } } } /** * Restore exhausted ore boulders whose timers have elapsed. */ export function stepOreRespawns (world, now) { const respawns = world.oreRespawns; if (!respawns?.size) { return { events: [] }; } const events = []; for (const [key, entry] of [...respawns.entries()]) { if (now < entry.respawnsAt) { continue; } respawns.delete(key); const delta = setBoulderState(world, entry.tileX, entry.tileY, oreBoulderState(entry.materialId)); if (delta) { events.push(delta); } } return { events }; } /** * Start mining an ore boulder. Returns events / private bag updates when complete via stepGather. */ export function startMine (world, sessionId, characterId, tileX, tileY, now) { const entityId = world.characterEntities.get(characterId); if (entityId == null) { throw userError("Character is not in the world."); } const handle = world.ecs.handleOf(entityId); const identity = world.ecs.get(handle, Component.networkIdentity); if (!identity || identity.sessionId !== sessionId) { throw userError("Character is not in the world."); } if (world.ecs.has(handle, Component.dead)) { throw userError("You are dead."); } const position = world.ecs.get(handle, Component.position); if (chebyshev(position.tileX, position.tileY, tileX, tileY) > 1) { throw userError("You need to stand next to the rock."); } const block = blockAt(world, tileX, tileY); if (!block || block.typeId !== BlockType.boulder) { throw userError("There is nothing to mine there."); } const materialId = materialFromOreBoulderState(block.state); if (!materialId) { throw userError(block.state === EXHAUSTED_ORE_BOULDER_STATE ? "That rock is exhausted." : "That rock has no ore."); } const pickaxe = equippedPickaxe(world.ecs, handle); if (!pickaxe) { throw userError("Equip a pickaxe to mine."); } const skills = world.ecs.get(handle, Component.skills); const miningLevel = skillLevel(skills, SkillId.mining); const gate = canMineMaterial(miningLevel, pickaxe.tier, materialId); if (!gate.ok) { throw userError(gate.reason); } const durationMs = miningDurationMs(materialId, pickaxe.tier); clearEngage(world.ecs, entityId); const path = world.ecs.get(handle, Component.path); if (path) { path.tiles = []; } world.ecs.add(handle, Component.gather, { kind: "mining", tileX, tileY, materialId, startedAt: now, finishesAt: now + durationMs, nextSwingAt: now }); return { events: [{ type: "gather-swing", attackerId: entityId, tileX, tileY, fromTileX: position.tileX, fromTileY: position.tileY, swipe: false }] }; } export function stepGather (world, now) { const events = []; const privates = []; const respawnEvents = stepOreRespawns(world, now); events.push(...respawnEvents.events); for (const entity of world.ecs.query(Component.gather, Component.position, Component.character)) { const { handle, gather, position, character, entityId } = entity; if (world.ecs.has(handle, Component.dead)) { clearGather(world.ecs, handle); continue; } if (chebyshev(position.tileX, position.tileY, gather.tileX, gather.tileY) > 1) { clearGather(world.ecs, handle); continue; } const block = blockAt(world, gather.tileX, gather.tileY); if (!block || block.typeId !== BlockType.boulder || materialFromOreBoulderState(block.state) !== gather.materialId) { clearGather(world.ecs, handle); continue; } if (now >= gather.nextSwingAt && now < gather.finishesAt) { gather.nextSwingAt = now + GATHER_SWING_INTERVAL_MS; events.push({ type: "gather-swing", attackerId: entityId, tileX: gather.tileX, tileY: gather.tileY, fromTileX: position.tileX, fromTileY: position.tileY, swipe: false }); } if (now < gather.finishesAt) { continue; } const oreItemId = oreItemIdForMaterial(gather.materialId); const def = itemDefinition(oreItemId); const inventory = world.ecs.get(handle, Component.inventory); if (!oreItemId || !def || !inventory) { clearGather(world.ecs, handle); continue; } const inserted = insertStack(inventory.slots, { itemId: oreItemId, quantity: 1 }, def); if (!inserted.ok || inserted.remainder > 0) { clearGather(world.ecs, handle); events.push({ type: "command-error", sessionId: world.ecs.get(handle, Component.networkIdentity)?.sessionId, message: "Your inventory is full." }); continue; } inventory.slots = inserted.slots; const skills = world.ecs.get(handle, Component.skills); let skillsChanged = false; if (skills?.xp && addSkillXp(skills, SkillId.mining, miningXpForMaterial(gather.materialId))) { skillsChanged = true; } const exhausted = setBoulderState( world, gather.tileX, gather.tileY, EXHAUSTED_ORE_BOULDER_STATE ); if (exhausted) { events.push(exhausted); ensureOreRespawns(world).set(tileKey(gather.tileX, gather.tileY), { tileX: gather.tileX, tileY: gather.tileY, materialId: gather.materialId, respawnsAt: now + oreExhaustDurationMs(gather.materialId) }); } clearGather(world.ecs, handle); privates.push({ characterId: character.characterId, bag: bagView(world.ecs, handle) }); if (skillsChanged) { events.push({ type: "skills-updated", characterId: character.characterId, skills: publicSkillsView(skills), stored: storedSkillXp(skills) }); } events.push({ type: "gather-complete", characterId: character.characterId, kind: "mining", materialId: gather.materialId, itemId: oreItemId }); } return { events, privates }; } export function handleGatherCommand (world, sessionId, command, now) { switch (command.type) { case "mine": return startMine(world, sessionId, command.characterId, command.tileX | 0, command.tileY | 0, now); case "cancel-gather": { const entityId = world.characterEntities.get(command.characterId); if (entityId == null) { return { events: [] }; } const handle = world.ecs.handleOf(entityId); clearGather(world.ecs, handle); return { events: [] }; } default: return null; } }