| 1 | import { Component } from "../ecs/components.js"; |
| 2 | import |
| 3 | { |
| 4 | JEWELRY_TELEPORT_MS, |
| 5 | canTeleportToBank, |
| 6 | canTeleportToChurch |
| 7 | } from "../../../shared/equipment/jewelryEffects.js"; |
| 8 | |
| 9 | export const JewelryTeleportDestination = Object.freeze({ |
| 10 | church: "church", |
| 11 | bank: "bank" |
| 12 | }); |
| 13 | |
| 14 | function ensureSessions (world) |
| 15 | { |
| 16 | if (!world.jewelryTeleportSessions) |
| 17 | { world.jewelryTeleportSessions = new Map(); } |
| 18 | return world.jewelryTeleportSessions; |
| 19 | } |
| 20 | |
| 21 | export function cancelJewelryTeleport (world, characterId) |
| 22 | { |
| 23 | ensureSessions(world).delete(characterId | 0); |
| 24 | } |
| 25 | |
| 26 | function userError (message) |
| 27 | { |
| 28 | const error = new Error(message); |
| 29 | error.userFacing = true; |
| 30 | return error; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Begin a 3s jewelry teleport channel. Interrupted by move or damage. |
| 35 | */ |
| 36 | export function startJewelryTeleport (world, sessionId, characterId, destination, now = Date.now()) |
| 37 | { |
| 38 | const dest = String(destination || ""); |
| 39 | if (dest !== JewelryTeleportDestination.church && dest !== JewelryTeleportDestination.bank) |
| 40 | { throw userError("Unknown teleport destination."); } |
| 41 | |
| 42 | const entityId = world.characterEntities.get(characterId); |
| 43 | if (entityId == null) |
| 44 | { throw userError("Character is not in the world."); } |
| 45 | const handle = world.ecs.handleOf(entityId); |
| 46 | const identity = world.ecs.get(handle, Component.networkIdentity); |
| 47 | if (!identity || identity.sessionId !== sessionId) |
| 48 | { throw userError("Character is not in the world."); } |
| 49 | if (world.ecs.has(handle, Component.dead)) |
| 50 | { throw userError("You are dead."); } |
| 51 | |
| 52 | const equipment = world.ecs.get(handle, Component.equipment); |
| 53 | if (dest === JewelryTeleportDestination.church && !canTeleportToChurch(equipment)) |
| 54 | { throw userError("You need an emerald bracelet to teleport to a church."); } |
| 55 | if (dest === JewelryTeleportDestination.bank && !canTeleportToBank(equipment)) |
| 56 | { throw userError("You need a ruby ring to teleport to a bank."); } |
| 57 | |
| 58 | const path = world.ecs.get(handle, Component.path); |
| 59 | if (path) |
| 60 | { path.tiles = []; } |
| 61 | |
| 62 | ensureSessions(world).set(characterId | 0, { |
| 63 | sessionId, |
| 64 | entityId, |
| 65 | destination: dest, |
| 66 | startedAt: now, |
| 67 | finishesAt: now + JEWELRY_TELEPORT_MS |
| 68 | }); |
| 69 | return { events: [] }; |
| 70 | } |
| 71 | |
| 72 | export function handleJewelryTeleportCommand (world, sessionId, command, now = Date.now()) |
| 73 | { |
| 74 | if (command.type !== "jewelry-teleport") |
| 75 | { return null; } |
| 76 | return startJewelryTeleport( |
| 77 | world, |
| 78 | sessionId, |
| 79 | command.characterId, |
| 80 | command.destination, |
| 81 | now |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Advance jewelry teleport channels. Emits finish events for the simulation to relocate. |
| 87 | * @returns {{ events: object[] }} |
| 88 | */ |
| 89 | export function stepJewelryTeleports (world, now = Date.now()) |
| 90 | { |
| 91 | const sessions = ensureSessions(world); |
| 92 | const events = []; |
| 93 | for (const [characterId, session] of [...sessions.entries()]) |
| 94 | { |
| 95 | const entityId = world.characterEntities.get(characterId) ?? session.entityId; |
| 96 | const handle = entityId != null ? world.ecs.handleOf(entityId) : null; |
| 97 | if (handle == null || world.ecs.has(handle, Component.dead)) |
| 98 | { |
| 99 | sessions.delete(characterId); |
| 100 | continue; |
| 101 | } |
| 102 | |
| 103 | const path = world.ecs.get(handle, Component.path); |
| 104 | if (path?.tiles?.length) |
| 105 | { |
| 106 | sessions.delete(characterId); |
| 107 | continue; |
| 108 | } |
| 109 | |
| 110 | const health = world.ecs.get(handle, Component.health); |
| 111 | if (health?.lastDamageAt != null && health.lastDamageAt >= session.startedAt) |
| 112 | { |
| 113 | sessions.delete(characterId); |
| 114 | continue; |
| 115 | } |
| 116 | |
| 117 | const equipment = world.ecs.get(handle, Component.equipment); |
| 118 | if (session.destination === JewelryTeleportDestination.church && !canTeleportToChurch(equipment)) |
| 119 | { |
| 120 | sessions.delete(characterId); |
| 121 | continue; |
| 122 | } |
| 123 | if (session.destination === JewelryTeleportDestination.bank && !canTeleportToBank(equipment)) |
| 124 | { |
| 125 | sessions.delete(characterId); |
| 126 | continue; |
| 127 | } |
| 128 | |
| 129 | if (now < session.finishesAt) |
| 130 | { continue; } |
| 131 | |
| 132 | sessions.delete(characterId); |
| 133 | const position = world.ecs.get(handle, Component.position); |
| 134 | events.push({ |
| 135 | type: "jewelry-teleport-finish", |
| 136 | sessionId: session.sessionId, |
| 137 | characterId, |
| 138 | entityId, |
| 139 | destination: session.destination, |
| 140 | fromTileX: position?.tileX ?? 0, |
| 141 | fromTileY: position?.tileY ?? 0, |
| 142 | dimensionId: position?.dimensionId |
| 143 | }); |
| 144 | } |
| 145 | return { events }; |
| 146 | } |