import { Component } from "../ecs/components.js"; import { JEWELRY_TELEPORT_MS, canTeleportToBank, canTeleportToChurch } from "../../../shared/equipment/jewelryEffects.js"; export const JewelryTeleportDestination = Object.freeze({ church: "church", bank: "bank" }); function ensureSessions (world) { if (!world.jewelryTeleportSessions) { world.jewelryTeleportSessions = new Map(); } return world.jewelryTeleportSessions; } export function cancelJewelryTeleport (world, characterId) { ensureSessions(world).delete(characterId | 0); } function userError (message) { const error = new Error(message); error.userFacing = true; return error; } /** * Begin a 3s jewelry teleport channel. Interrupted by move or damage. */ export function startJewelryTeleport (world, sessionId, characterId, destination, now = Date.now()) { const dest = String(destination || ""); if (dest !== JewelryTeleportDestination.church && dest !== JewelryTeleportDestination.bank) { throw userError("Unknown teleport destination."); } 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 equipment = world.ecs.get(handle, Component.equipment); if (dest === JewelryTeleportDestination.church && !canTeleportToChurch(equipment)) { throw userError("You need an emerald bracelet to teleport to a church."); } if (dest === JewelryTeleportDestination.bank && !canTeleportToBank(equipment)) { throw userError("You need a ruby ring to teleport to a bank."); } const path = world.ecs.get(handle, Component.path); if (path) { path.tiles = []; } ensureSessions(world).set(characterId | 0, { sessionId, entityId, destination: dest, startedAt: now, finishesAt: now + JEWELRY_TELEPORT_MS }); return { events: [] }; } export function handleJewelryTeleportCommand (world, sessionId, command, now = Date.now()) { if (command.type !== "jewelry-teleport") { return null; } return startJewelryTeleport( world, sessionId, command.characterId, command.destination, now ); } /** * Advance jewelry teleport channels. Emits finish events for the simulation to relocate. * @returns {{ events: object[] }} */ export function stepJewelryTeleports (world, now = Date.now()) { const sessions = ensureSessions(world); const events = []; for (const [characterId, session] of [...sessions.entries()]) { const entityId = world.characterEntities.get(characterId) ?? session.entityId; const handle = entityId != null ? world.ecs.handleOf(entityId) : null; if (handle == null || world.ecs.has(handle, Component.dead)) { sessions.delete(characterId); continue; } const path = world.ecs.get(handle, Component.path); if (path?.tiles?.length) { sessions.delete(characterId); continue; } const health = world.ecs.get(handle, Component.health); if (health?.lastDamageAt != null && health.lastDamageAt >= session.startedAt) { sessions.delete(characterId); continue; } const equipment = world.ecs.get(handle, Component.equipment); if (session.destination === JewelryTeleportDestination.church && !canTeleportToChurch(equipment)) { sessions.delete(characterId); continue; } if (session.destination === JewelryTeleportDestination.bank && !canTeleportToBank(equipment)) { sessions.delete(characterId); continue; } if (now < session.finishesAt) { continue; } sessions.delete(characterId); const position = world.ecs.get(handle, Component.position); events.push({ type: "jewelry-teleport-finish", sessionId: session.sessionId, characterId, entityId, destination: session.destination, fromTileX: position?.tileX ?? 0, fromTileY: position?.tileY ?? 0, dimensionId: position?.dimensionId }); } return { events }; }