BlueGit

timfa questward

Files

questward / shared / definitions / spellItems.js

spellItems.js

spellItems.js 3.8 KiB Download Raw
1import { ItemCategory } from "./itemCategories.js";
2import { SpellId, spellDefinition, learnableSpellIds } from "./spells.js";
3
4/**
5 * Stable spellbook item ids.
6 * Legacy spark spellbook (134) is retired — id kept unused so saves do not collide.
7 */
8const SPELLBOOK_ID_BY_SPELL = Object.freeze({
9 [SpellId.manaBullet]: 490,
10 [SpellId.dark]: 391,
11 [SpellId.magicMissile]: 489,
12 [SpellId.firebolt]: 392,
13 [SpellId.fireball]: 393,
14 [SpellId.blaze]: 394,
15 [SpellId.inferno]: 395,
16 [SpellId.hellfire]: 396,
17 [SpellId.poison]: 397,
18 [SpellId.drain]: 398,
19 [SpellId.tinders]: 399,
20 [SpellId.shade]: 400,
21 [SpellId.revitalize]: 401,
22 // Water tree
23 [SpellId.waterbolt]: 431,
24 [SpellId.waterspout]: 432,
25 [SpellId.torrent]: 433,
26 [SpellId.deluge]: 434,
27 [SpellId.maelstrom]: 435,
28 // Air tree
29 [SpellId.airbolt]: 436,
30 [SpellId.gale]: 437,
31 [SpellId.cyclone]: 438,
32 [SpellId.tempest]: 439,
33 [SpellId.hurricane]: 440,
34 // Earth tree
35 [SpellId.earthbolt]: 441,
36 [SpellId.boulder]: 442,
37 [SpellId.quake]: 443,
38 [SpellId.landslide]: 444,
39 [SpellId.cataclysm]: 445,
40 // Light tree
41 [SpellId.lightbolt]: 446,
42 [SpellId.radiance]: 447,
43 [SpellId.flare]: 448,
44 [SpellId.sunbeam]: 449,
45 [SpellId.judgment]: 450,
46 // Dark tree
47 [SpellId.darkbolt]: 451,
48 [SpellId.gloom]: 452,
49 [SpellId.voidlance]: 453,
50 [SpellId.eclipse]: 454,
51 [SpellId.oblivion]: 455,
52 // Poison tree
53 [SpellId.poisonbolt]: 456,
54 [SpellId.toxinburst]: 457,
55 [SpellId.venomcloud]: 458,
56 [SpellId.plague]: 459,
57 [SpellId.blight]: 460
58});
59
60export const SpellbookItemId = Object.freeze(
61 Object.fromEntries(
62 Object.entries(SPELLBOOK_ID_BY_SPELL).map(([spellId, itemId]) =>
63 {
64 const key = `${spellId}Spellbook`;
65 return [key, itemId];
66 })
67 )
68);
69
70export function spellbookInstance (spellId)
71{
72 const def = spellDefinition(spellId);
73 if (!def)
74 { return null; }
75 return Object.freeze({
76 kind: "spellbook",
77 spellId,
78 displayName: `${def.displayName} spellbook`
79 });
80}
81
82/**
83 * Resolve which spell a bag/shop stack teaches.
84 * Prefer instance data; fall back to the item def / stable spellbook id map
85 * so catalog stock without an instance still works.
86 */
87export function spellIdFromSpellbookStack (stack, itemDef = null)
88{
89 if (stack?.instance?.kind === "spellbook" && typeof stack.instance.spellId === "string")
90 {
91 const id = stack.instance.spellId;
92 if (spellDefinition(id))
93 { return id; }
94 }
95 if (itemDef?.consumableSpellId && spellDefinition(itemDef.consumableSpellId))
96 { return itemDef.consumableSpellId; }
97 const itemId = stack?.itemId | 0;
98 if (!itemId)
99 { return null; }
100 for (const [spellId, bookId] of Object.entries(SPELLBOOK_ID_BY_SPELL))
101 {
102 if ((bookId | 0) === itemId && spellDefinition(spellId))
103 { return spellId; }
104 }
105 return null;
106}
107
108function buildSpellbookDefinitions ()
109{
110 const out = {};
111 for (const spellId of learnableSpellIds())
112 {
113 const itemId = SPELLBOOK_ID_BY_SPELL[spellId];
114 const spell = spellDefinition(spellId);
115 if (itemId == null || !spell)
116 { continue; }
117 out[itemId] = Object.freeze({
118 itemId,
119 key: `spellbook-${spellId}`,
120 displayName: `Spellbook: ${spell.displayName}`,
121 description: `A worn volume of sigils. Study it carefully to learn ${spell.displayName}.`,
122 seedValue: 20 + (spell.tier | 0) * 10,
123 stackable: false,
124 maxStackSize: 1,
125 uniqueInstance: true,
126 category: ItemCategory.finished,
127 iconKey: "spellbook",
128 defaultState: "spark",
129 consumableSpellId: spellId,
130 defaultInstance: spellbookInstance(spellId)
131 });
132 }
133 return out;
134}
135
136/** @type {Record<number, object>} */
137export const SPELLBOOK_ITEM_DEFINITIONS = Object.freeze(buildSpellbookDefinitions());
138
139export function spellbookItemForSpell (spellId)
140{
141 for (const def of Object.values(SPELLBOOK_ITEM_DEFINITIONS))
142 {
143 if (def.consumableSpellId === spellId)
144 { return def; }
145 }
146 return null;
147}
148
149export function spellbookItemIdForSpell (spellId)
150{
151 return SPELLBOOK_ID_BY_SPELL[spellId] ?? null;
152}