/** * Play spells panel: known spells, favourites bar, arm / cast intents. */ import { spellDefinition, spellMindPowerFromTier, spellMinMagicLevel } from "/shared/definitions/spells.js"; import { SkillId } from "/shared/definitions/skills.js"; import { spellMaxHitDisplay, isMagicStaffEquipped, firstFavoriteAttackSpellId } from "/shared/combat/maxHit.js"; export function initSpells ({ socket, MessageType, getSkillLevel, getSkillLevels = null, getEquipmentSlots = null, getSettings = null } = {}) { const panel = document.getElementById("spells-panel"); const list = document.getElementById("spells-list"); const bar = document.getElementById("spell-favorites-bar"); const autoCastToggle = document.getElementById("spell-autocast-toggle"); let spells = []; /** Server favourite order (spell ids). */ let favoriteOrder = []; let autoCast = false; let armedSpellId = null; let dragFromSpellId = null; let suppressNextClick = false; document.getElementById("spells-toggle")?.addEventListener("click", () => { panel.hidden = !panel.hidden; if (!panel.hidden) { refreshCastGates(); } }); autoCastToggle?.addEventListener("change", () => { socket.send({ type: MessageType.setSpellAutoCast, enabled: autoCastToggle.checked }); }); /** Re-evaluate Magic gates from the latest client skill levels (e.g. after level-up). */ function refreshCastGates () { if (!spells.length) { return; } const magicLevel = Math.max(1, getSkillLevel?.(SkillId.magic) | 0 || 1); let changed = false; for (const spell of spells) { const need = spellMinMagicLevel(spellDefinition(spell.spellId)); const ok = magicLevel >= need; if (spell.canCast !== ok || spell.canFavorite !== ok) { spell.canCast = ok; spell.canFavorite = ok; changed = true; } } if (changed || !panel.hidden) { renderList(); } /** Favourites sit outside the panel — always sync so they show after login. */ renderBar(); } function refreshMaxHitDisplay () { if (!panel.hidden) { renderList(); } renderBar(); } function disarmLocal () { if (!armedSpellId) { return; } armedSpellId = null; renderList(); renderBar(); } function syncFavoriteOrder (payload) { if (Array.isArray(payload?.favorites)) { favoriteOrder = payload.favorites.filter((id) => typeof id === "string" || typeof id === "number"); return; } favoriteOrder = spells.filter((entry) => entry.favorite).map((entry) => entry.spellId); } function render (payload) { spells = payload?.spells ?? []; autoCast = !!payload?.autoCast; if (payload?.armedSpellId !== undefined) { armedSpellId = payload.armedSpellId; } if (autoCastToggle) { autoCastToggle.checked = autoCast; } syncFavoriteOrder(payload); refreshCastGates(); } function renderList () { list.replaceChildren(); for (const spell of spells) { list.append(renderSpellRow(spell)); } } function renderSpellRow (spell) { const row = document.createElement("div"); row.className = "spell-row"; if (spell.spellId === armedSpellId) { row.classList.add("armed"); } const icon = document.createElement("img"); icon.className = "spell-icon"; icon.src = spell.icon; icon.alt = ""; icon.loading = "lazy"; const body = document.createElement("div"); body.className = "spell-body"; const title = document.createElement("div"); title.className = "spell-title-row"; const name = document.createElement("span"); name.className = "spell-title"; name.textContent = spell.displayName; const meta = document.createElement("span"); meta.className = "spell-meta"; const level = spell.tier | 0 || 1; const power = spell.mindPower != null ? spell.mindPower | 0 : spellMindPowerFromTier(level); meta.textContent = `Lv. ${level} Pwr: ${power}`; title.append(name, meta); const desc = document.createElement("div"); desc.className = "spell-desc hint"; desc.textContent = spell.description ?? ""; const maxHit = spellMaxHitDisplay({ spellId: spell.spellId, skillLevels: getSkillLevels?.() ?? {}, equipmentSlots: getEquipmentSlots?.() ?? {}, settings: getSettings?.() ?? null }); const maxHitEl = maxHit != null ? (() => { const line = document.createElement("div"); line.className = "spell-max-hit hint"; line.textContent = `Max hit: ${maxHit}`; return line; })() : null; const actions = document.createElement("div"); actions.className = "spell-actions"; const favorite = document.createElement("button"); favorite.type = "button"; favorite.className = "spell-favorite-btn"; favorite.textContent = spell.favorite ? "★" : "☆"; favorite.title = spell.canFavorite ? (spell.favorite ? "Remove from favourites" : "Add to favourites") : "Magic level too low to favourite"; favorite.disabled = !spell.canFavorite; favorite.addEventListener("click", () => { socket.send({ type: MessageType.setSpellFavorite, spellId: spell.spellId }); }); const arm = document.createElement("button"); arm.type = "button"; arm.className = "spell-arm-btn"; arm.textContent = spell.spellId === armedSpellId ? "Armed" : "Arm"; arm.disabled = !spell.canCast; arm.addEventListener("click", () => { if (spell.spellId === armedSpellId) { cancelTargeting(); } else { socket.send({ type: MessageType.armSpell, spellId: spell.spellId }); } }); actions.append(favorite, arm); body.append(title, desc); if (maxHitEl) { body.append(maxHitEl); } body.append(actions); row.append(icon, body); return row; } function orderedFavoriteSpells () { const byId = new Map(spells.map((entry) => [entry.spellId, entry])); const ordered = []; for (const spellId of favoriteOrder) { const spell = byId.get(spellId); if (spell?.favorite) { ordered.push(spell); } } for (const spell of spells) { if (spell.favorite && !ordered.some((entry) => entry.spellId === spell.spellId)) { ordered.push(spell); } } return ordered; } function staffAttackFavoriteId () { if (!isMagicStaffEquipped(getEquipmentSlots?.() ?? {})) { return null; } return firstFavoriteAttackSpellId({ favorites: favoriteOrder }); } function swapFavorites (fromSpellId, toSpellId) { if (fromSpellId == null || toSpellId == null || fromSpellId === toSpellId) { return; } const order = orderedFavoriteSpells().map((entry) => entry.spellId); const fromIndex = order.indexOf(fromSpellId); const toIndex = order.indexOf(toSpellId); if (fromIndex < 0 || toIndex < 0) { return; } const next = [...order]; const tmp = next[fromIndex]; next[fromIndex] = next[toIndex]; next[toIndex] = tmp; favoriteOrder = next; renderBar(); socket.send({ type: MessageType.reorderSpellFavorites, favoriteSpellIds: next }); } function renderBar () { const favorites = orderedFavoriteSpells(); bar.replaceChildren(); bar.hidden = favorites.length === 0; const attackId = staffAttackFavoriteId(); for (const spell of favorites) { const btn = document.createElement("button"); btn.type = "button"; btn.className = "spell-favorite-slot"; btn.draggable = true; btn.dataset.spellId = String(spell.spellId); if (spell.spellId === armedSpellId) { btn.classList.add("armed"); } if (spell.spellId === attackId) { btn.classList.add("staff-attack"); } btn.title = spell.spellId === attackId ? `${spell.displayName} (staff auto-attack)` : spell.displayName; btn.disabled = !spell.canCast; const icon = document.createElement("img"); icon.src = spell.icon; icon.alt = ""; icon.draggable = false; btn.append(icon); btn.addEventListener("click", () => { if (suppressNextClick) { suppressNextClick = false; return; } if (spell.spellId === armedSpellId) { return; } socket.send({ type: MessageType.armSpell, spellId: spell.spellId }); }); btn.addEventListener("contextmenu", (event) => { event.preventDefault(); socket.send({ type: MessageType.setSpellFavorite, spellId: spell.spellId }); }); btn.addEventListener("dragstart", (event) => { dragFromSpellId = spell.spellId; btn.classList.add("dragging"); event.dataTransfer?.setData("text/plain", String(spell.spellId)); if (event.dataTransfer) { event.dataTransfer.effectAllowed = "move"; } }); btn.addEventListener("dragend", () => { btn.classList.remove("dragging"); dragFromSpellId = null; for (const el of bar.querySelectorAll(".spell-favorite-slot.drag-over")) { el.classList.remove("drag-over"); } }); btn.addEventListener("dragover", (event) => { event.preventDefault(); if (event.dataTransfer) { event.dataTransfer.dropEffect = "move"; } btn.classList.add("drag-over"); }); btn.addEventListener("dragleave", () => { btn.classList.remove("drag-over"); }); btn.addEventListener("drop", (event) => { event.preventDefault(); btn.classList.remove("drag-over"); const rawFrom = dragFromSpellId ?? event.dataTransfer?.getData("text/plain"); if (rawFrom == null || rawFrom === "") { return; } suppressNextClick = true; const fromId = favoriteOrder.find((id) => String(id) === String(rawFrom)) ?? rawFrom; swapFavorites(fromId, spell.spellId); }); bar.append(btn); } } function getArmedSpell () { if (!armedSpellId) { return null; } return spells.find((entry) => entry.spellId === armedSpellId) ?? null; } function tryCastAtTile (tileX, tileY) { const armed = getArmedSpell(); if (!armed || armed.target !== "tile" || !armed.canCast) { return false; } socket.send({ type: MessageType.castSpellTile, spellId: armed.spellId, tileX, tileY }); return true; } function tryCastAtEntity (targetEntityId) { const armed = getArmedSpell(); if (!armed || armed.target !== "entity" || !armed.canCast) { return false; } socket.send({ type: MessageType.castSpellEntity, spellId: armed.spellId, targetEntityId }); return true; } function cancelTargeting () { if (!armedSpellId) { return; } socket.send({ type: MessageType.cancelArmSpell }); disarmLocal(); } return { render, refreshCastGates, getArmedSpell, tryCastAtTile, tryCastAtEntity, cancelTargeting, isBlocking () { return panel != null && !panel.hidden; }, isTargeting () { return armedSpellId != null; }, handleSpellArmed (message) { armedSpellId = message.spellId ?? null; renderList(); renderBar(); }, knowsSpell (spellId) { return spells.some((spell) => spell.spellId === spellId); }, getKnownSpellIds () { return spells.map((spell) => spell.spellId); }, refreshMaxHitDisplay, handleSpellDisarmed () { disarmLocal(); }, hide () { panel.hidden = true; spells = []; favoriteOrder = []; autoCast = false; armedSpellId = null; if (autoCastToggle) { autoCastToggle.checked = false; } list.replaceChildren(); bar.replaceChildren(); bar.hidden = true; } }; }