import { ItemCategory } from "./itemCategories.js"; import { SpellId, spellDefinition, learnableSpellIds } from "./spells.js"; /** * Stable spellbook item ids. * Legacy spark spellbook (134) is retired — id kept unused so saves do not collide. */ const SPELLBOOK_ID_BY_SPELL = Object.freeze({ [SpellId.manaBullet]: 490, [SpellId.dark]: 391, [SpellId.magicMissile]: 489, [SpellId.firebolt]: 392, [SpellId.fireball]: 393, [SpellId.blaze]: 394, [SpellId.inferno]: 395, [SpellId.hellfire]: 396, [SpellId.poison]: 397, [SpellId.drain]: 398, [SpellId.tinders]: 399, [SpellId.shade]: 400, [SpellId.revitalize]: 401, // Water tree [SpellId.waterbolt]: 431, [SpellId.waterspout]: 432, [SpellId.torrent]: 433, [SpellId.deluge]: 434, [SpellId.maelstrom]: 435, // Air tree [SpellId.airbolt]: 436, [SpellId.gale]: 437, [SpellId.cyclone]: 438, [SpellId.tempest]: 439, [SpellId.hurricane]: 440, // Earth tree [SpellId.earthbolt]: 441, [SpellId.boulder]: 442, [SpellId.quake]: 443, [SpellId.landslide]: 444, [SpellId.cataclysm]: 445, // Light tree [SpellId.lightbolt]: 446, [SpellId.radiance]: 447, [SpellId.flare]: 448, [SpellId.sunbeam]: 449, [SpellId.judgment]: 450, // Dark tree [SpellId.darkbolt]: 451, [SpellId.gloom]: 452, [SpellId.voidlance]: 453, [SpellId.eclipse]: 454, [SpellId.oblivion]: 455, // Poison tree [SpellId.poisonbolt]: 456, [SpellId.toxinburst]: 457, [SpellId.venomcloud]: 458, [SpellId.plague]: 459, [SpellId.blight]: 460 }); export const SpellbookItemId = Object.freeze( Object.fromEntries( Object.entries(SPELLBOOK_ID_BY_SPELL).map(([spellId, itemId]) => { const key = `${spellId}Spellbook`; return [key, itemId]; }) ) ); export function spellbookInstance (spellId) { const def = spellDefinition(spellId); if (!def) { return null; } return Object.freeze({ kind: "spellbook", spellId, displayName: `${def.displayName} spellbook` }); } /** * Resolve which spell a bag/shop stack teaches. * Prefer instance data; fall back to the item def / stable spellbook id map * so catalog stock without an instance still works. */ export function spellIdFromSpellbookStack (stack, itemDef = null) { if (stack?.instance?.kind === "spellbook" && typeof stack.instance.spellId === "string") { const id = stack.instance.spellId; if (spellDefinition(id)) { return id; } } if (itemDef?.consumableSpellId && spellDefinition(itemDef.consumableSpellId)) { return itemDef.consumableSpellId; } const itemId = stack?.itemId | 0; if (!itemId) { return null; } for (const [spellId, bookId] of Object.entries(SPELLBOOK_ID_BY_SPELL)) { if ((bookId | 0) === itemId && spellDefinition(spellId)) { return spellId; } } return null; } function buildSpellbookDefinitions () { const out = {}; for (const spellId of learnableSpellIds()) { const itemId = SPELLBOOK_ID_BY_SPELL[spellId]; const spell = spellDefinition(spellId); if (itemId == null || !spell) { continue; } out[itemId] = Object.freeze({ itemId, key: `spellbook-${spellId}`, displayName: `Spellbook: ${spell.displayName}`, description: `A worn volume of sigils. Study it carefully to learn ${spell.displayName}.`, seedValue: 20 + (spell.tier | 0) * 10, stackable: false, maxStackSize: 1, uniqueInstance: true, category: ItemCategory.finished, iconKey: "spellbook", defaultState: "spark", consumableSpellId: spellId, defaultInstance: spellbookInstance(spellId) }); } return out; } /** @type {Record} */ export const SPELLBOOK_ITEM_DEFINITIONS = Object.freeze(buildSpellbookDefinitions()); export function spellbookItemForSpell (spellId) { for (const def of Object.values(SPELLBOOK_ITEM_DEFINITIONS)) { if (def.consumableSpellId === spellId) { return def; } } return null; } export function spellbookItemIdForSpell (spellId) { return SPELLBOOK_ID_BY_SPELL[spellId] ?? null; }