BlueGit

timfa questward

Add Mana Bullet as a tier-1 attack spell with spellbook.

Includes a catalog merge test so colliding item ids (like the Magic Missile/tourmaline clash) fail loudly.

timfa Tim Falken · pushed by timfa

Authored 2026-08-30 · 2 months ago

Hash
0c5ad783221dcb96dcf320178652fd3686e59633
Parent
35c27d8d
5 files changed +153 −10
client/assets/spells/mana-bullet.svg added +4 −0
@@ -0,0 +1,4 @@
1 <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32">
2 <rect width="32" height="32" rx="6" fill="#5a8a78"/>
3 <text x="16" y="21" text-anchor="middle" fill="#f0ece0" font-size="11" font-family="Segoe UI,sans-serif" font-weight="600">MB</text>
4 </svg>
@@ -0,0 +1,4 @@
1<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32">
2<rect width="32" height="32" rx="6" fill="#5a8a78"/>
3<text x="16" y="21" text-anchor="middle" fill="#f0ece0" font-size="11" font-family="Segoe UI,sans-serif" font-weight="600">MB</text>
4</svg>
server/tests/item-ids.test.js added +134 −0
@@ -0,0 +1,134 @@
1 import test from "node:test";
2 import assert from "node:assert/strict";
3 import { ITEM_DEFINITIONS, listAllItemPickerEntries } from "../../shared/definitions/items.js";
4 import { CRAFTING_ITEM_STUBS } from "../../shared/definitions/craftingItems.js";
5 import { LEATHER_ITEM_DEFINITIONS } from "../../shared/definitions/leatherItems.js";
6 import { METAL_ITEM_DEFINITIONS } from "../../shared/definitions/metalItems.js";
7 import { WOOD_ITEM_DEFINITIONS } from "../../shared/definitions/woodItems.js";
8 import { ARMOUR_ITEM_DEFINITIONS } from "../../shared/definitions/armourItems.js";
9 import { CLOTH_ITEM_DEFINITIONS } from "../../shared/definitions/clothItems.js";
10 import { HERB_ITEM_DEFINITIONS } from "../../shared/definitions/herbs.js";
11 import { RANGED_ITEM_DEFINITIONS } from "../../shared/definitions/rangedItems.js";
12 import { SPELLBOOK_ITEM_DEFINITIONS } from "../../shared/definitions/spellItems.js";
13 import { FISH_ITEM_DEFINITIONS } from "../../shared/definitions/fishItems.js";
14 import { GEM_ITEM_DEFINITIONS } from "../../shared/definitions/gemItems.js";
15 import { SWORD_ITEM_DEFINITIONS } from "../../shared/definitions/swordItems.js";
16 import { WAR_AXE_ITEM_DEFINITIONS } from "../../shared/definitions/warAxeItems.js";
17 import { ESSENCE_ITEM_DEFINITIONS } from "../../shared/definitions/essenceItems.js";
18 import { DyeItemId, FLOWER_ITEM_DEFINITION, DYE_ITEM_DEFINITION } from "../../shared/definitions/dyes.js";
19 import { SpellId, spellDefinition, spellMinMagicLevel } from "../../shared/definitions/spells.js";
20
21 /** Inline core item ids in items.js before the category spreads. */
22 const CORE_ITEM_IDS = Object.freeze([1, 6, 2, 275, 3, 4, 7, 8, 26, 27, 28, 29, 32, 33]);
23
24 /**
25 * Maps spread into ITEM_DEFINITIONS. Later spreads overwrite earlier ones on
26 * collision — that is only safe when both sides describe the same item.
27 */
28 const ITEM_DEFINITION_SOURCES = Object.freeze([
29 ["core", Object.fromEntries(CORE_ITEM_IDS.map((id) => [id, ITEM_DEFINITIONS[id]]))],
30 ["crafting", CRAFTING_ITEM_STUBS],
31 ["leather", LEATHER_ITEM_DEFINITIONS],
32 ["metal", METAL_ITEM_DEFINITIONS],
33 ["wood", WOOD_ITEM_DEFINITIONS],
34 ["armour", ARMOUR_ITEM_DEFINITIONS],
35 ["cloth", CLOTH_ITEM_DEFINITIONS],
36 ["herb", HERB_ITEM_DEFINITIONS],
37 ["ranged", RANGED_ITEM_DEFINITIONS],
38 ["spellbook", SPELLBOOK_ITEM_DEFINITIONS],
39 ["fish", FISH_ITEM_DEFINITIONS],
40 ["gem", GEM_ITEM_DEFINITIONS],
41 ["sword", SWORD_ITEM_DEFINITIONS],
42 ["warAxe", WAR_AXE_ITEM_DEFINITIONS],
43 ["essence", ESSENCE_ITEM_DEFINITIONS],
44 ["dye", {
45 [DyeItemId.flower]: FLOWER_ITEM_DEFINITION,
46 [DyeItemId.dye]: DYE_ITEM_DEFINITION
47 }]
48 ]);
49
50 function itemIdentity (def)
51 {
52 if (!def)
53 { return null; }
54 if (def.key)
55 { return `key:${def.key}`; }
56 if (def.consumableSpellId)
57 { return `spell:${def.consumableSpellId}`; }
58 if (def.displayName)
59 { return `name:${def.displayName}`; }
60 return null;
61 }
62
63 test("item definition sources do not collide on the same id", () =>
64 {
65 /** @type {Map<number, { source: string, identity: string, label: string }>} */
66 const owner = new Map();
67 for (const [source, map] of ITEM_DEFINITION_SOURCES)
68 {
69 for (const [key, def] of Object.entries(map))
70 {
71 const id = Number(key);
72 assert.ok(Number.isFinite(id), `${source} key ${key} is not numeric`);
73 if (def?.itemId != null)
74 { assert.equal(def.itemId | 0, id, `${source} id ${id} key/itemId mismatch`); }
75 const identity = itemIdentity(def);
76 assert.ok(identity, `${source} id ${id} has no identity`);
77 const label = def.displayName ?? def.key ?? key;
78 const prev = owner.get(id);
79 if (prev && prev.identity !== identity)
80 {
81 assert.fail(
82 `item id ${id} collision: ${prev.source} (${prev.label}) vs ${source} (${label})`
83 );
84 }
85 owner.set(id, { source, identity, label });
86 }
87 }
88 });
89
90 test("merged ITEM_DEFINITIONS keep each source item identity", () =>
91 {
92 for (const [source, map] of ITEM_DEFINITION_SOURCES)
93 {
94 for (const [key, def] of Object.entries(map))
95 {
96 const id = Number(key);
97 const merged = ITEM_DEFINITIONS[id];
98 assert.ok(merged, `${source} item ${id} missing from merge`);
99 assert.equal(merged.itemId | 0, id);
100 const expected = itemIdentity(def);
101 const actual = itemIdentity(merged);
102 assert.equal(
103 actual,
104 expected,
105 `${source} item ${id} overwritten in merge (${expected} → ${actual})`
106 );
107 }
108 }
109 });
110
111 test("Mana Bullet is a tier-1 learnable attack with a shop-listed spellbook", () =>
112 {
113 const spell = spellDefinition(SpellId.manaBullet);
114 assert.ok(spell);
115 assert.equal(spell.tier, 1);
116 assert.equal(spell.effect, "damage");
117 assert.equal(spell.target, "entity");
118 assert.equal(spellMinMagicLevel(spell), 1);
119 assert.equal(spell.starter, undefined);
120
121 const entry = listAllItemPickerEntries().find((e) => e.displayName === "Spellbook: Mana Bullet");
122 assert.ok(entry, "Mana Bullet spellbook missing from shop stock picker");
123 assert.equal(entry.itemId, 490);
124 assert.equal(ITEM_DEFINITIONS[490]?.consumableSpellId, SpellId.manaBullet);
125 });
126
127 test("Magic Missile spellbook survives the item catalog merge", () =>
128 {
129 const entry = listAllItemPickerEntries().find((e) => e.displayName === "Spellbook: Magic Missile");
130 assert.ok(entry);
131 assert.equal(entry.itemId, 489);
132 assert.equal(ITEM_DEFINITIONS[489]?.consumableSpellId, SpellId.magicMissile);
133 assert.equal(ITEM_DEFINITIONS[402]?.key, "tourmaline-cut");
134 });
@@ -0,0 +1,134 @@
1import test from "node:test";
2import assert from "node:assert/strict";
3import { ITEM_DEFINITIONS, listAllItemPickerEntries } from "../../shared/definitions/items.js";
4import { CRAFTING_ITEM_STUBS } from "../../shared/definitions/craftingItems.js";
5import { LEATHER_ITEM_DEFINITIONS } from "../../shared/definitions/leatherItems.js";
6import { METAL_ITEM_DEFINITIONS } from "../../shared/definitions/metalItems.js";
7import { WOOD_ITEM_DEFINITIONS } from "../../shared/definitions/woodItems.js";
8import { ARMOUR_ITEM_DEFINITIONS } from "../../shared/definitions/armourItems.js";
9import { CLOTH_ITEM_DEFINITIONS } from "../../shared/definitions/clothItems.js";
10import { HERB_ITEM_DEFINITIONS } from "../../shared/definitions/herbs.js";
11import { RANGED_ITEM_DEFINITIONS } from "../../shared/definitions/rangedItems.js";
12import { SPELLBOOK_ITEM_DEFINITIONS } from "../../shared/definitions/spellItems.js";
13import { FISH_ITEM_DEFINITIONS } from "../../shared/definitions/fishItems.js";
14import { GEM_ITEM_DEFINITIONS } from "../../shared/definitions/gemItems.js";
15import { SWORD_ITEM_DEFINITIONS } from "../../shared/definitions/swordItems.js";
16import { WAR_AXE_ITEM_DEFINITIONS } from "../../shared/definitions/warAxeItems.js";
17import { ESSENCE_ITEM_DEFINITIONS } from "../../shared/definitions/essenceItems.js";
18import { DyeItemId, FLOWER_ITEM_DEFINITION, DYE_ITEM_DEFINITION } from "../../shared/definitions/dyes.js";
19import { SpellId, spellDefinition, spellMinMagicLevel } from "../../shared/definitions/spells.js";
20
21/** Inline core item ids in items.js before the category spreads. */
22const CORE_ITEM_IDS = Object.freeze([1, 6, 2, 275, 3, 4, 7, 8, 26, 27, 28, 29, 32, 33]);
23
24/**
25 * Maps spread into ITEM_DEFINITIONS. Later spreads overwrite earlier ones on
26 * collision — that is only safe when both sides describe the same item.
27 */
28const ITEM_DEFINITION_SOURCES = Object.freeze([
29 ["core", Object.fromEntries(CORE_ITEM_IDS.map((id) => [id, ITEM_DEFINITIONS[id]]))],
30 ["crafting", CRAFTING_ITEM_STUBS],
31 ["leather", LEATHER_ITEM_DEFINITIONS],
32 ["metal", METAL_ITEM_DEFINITIONS],
33 ["wood", WOOD_ITEM_DEFINITIONS],
34 ["armour", ARMOUR_ITEM_DEFINITIONS],
35 ["cloth", CLOTH_ITEM_DEFINITIONS],
36 ["herb", HERB_ITEM_DEFINITIONS],
37 ["ranged", RANGED_ITEM_DEFINITIONS],
38 ["spellbook", SPELLBOOK_ITEM_DEFINITIONS],
39 ["fish", FISH_ITEM_DEFINITIONS],
40 ["gem", GEM_ITEM_DEFINITIONS],
41 ["sword", SWORD_ITEM_DEFINITIONS],
42 ["warAxe", WAR_AXE_ITEM_DEFINITIONS],
43 ["essence", ESSENCE_ITEM_DEFINITIONS],
44 ["dye", {
45 [DyeItemId.flower]: FLOWER_ITEM_DEFINITION,
46 [DyeItemId.dye]: DYE_ITEM_DEFINITION
47 }]
48]);
49
50function itemIdentity (def)
51{
52 if (!def)
53 { return null; }
54 if (def.key)
55 { return `key:${def.key}`; }
56 if (def.consumableSpellId)
57 { return `spell:${def.consumableSpellId}`; }
58 if (def.displayName)
59 { return `name:${def.displayName}`; }
60 return null;
61}
62
63test("item definition sources do not collide on the same id", () =>
64{
65 /** @type {Map<number, { source: string, identity: string, label: string }>} */
66 const owner = new Map();
67 for (const [source, map] of ITEM_DEFINITION_SOURCES)
68 {
69 for (const [key, def] of Object.entries(map))
70 {
71 const id = Number(key);
72 assert.ok(Number.isFinite(id), `${source} key ${key} is not numeric`);
73 if (def?.itemId != null)
74 { assert.equal(def.itemId | 0, id, `${source} id ${id} key/itemId mismatch`); }
75 const identity = itemIdentity(def);
76 assert.ok(identity, `${source} id ${id} has no identity`);
77 const label = def.displayName ?? def.key ?? key;
78 const prev = owner.get(id);
79 if (prev && prev.identity !== identity)
80 {
81 assert.fail(
82 `item id ${id} collision: ${prev.source} (${prev.label}) vs ${source} (${label})`
83 );
84 }
85 owner.set(id, { source, identity, label });
86 }
87 }
88});
89
90test("merged ITEM_DEFINITIONS keep each source item identity", () =>
91{
92 for (const [source, map] of ITEM_DEFINITION_SOURCES)
93 {
94 for (const [key, def] of Object.entries(map))
95 {
96 const id = Number(key);
97 const merged = ITEM_DEFINITIONS[id];
98 assert.ok(merged, `${source} item ${id} missing from merge`);
99 assert.equal(merged.itemId | 0, id);
100 const expected = itemIdentity(def);
101 const actual = itemIdentity(merged);
102 assert.equal(
103 actual,
104 expected,
105 `${source} item ${id} overwritten in merge (${expected} → ${actual})`
106 );
107 }
108 }
109});
110
111test("Mana Bullet is a tier-1 learnable attack with a shop-listed spellbook", () =>
112{
113 const spell = spellDefinition(SpellId.manaBullet);
114 assert.ok(spell);
115 assert.equal(spell.tier, 1);
116 assert.equal(spell.effect, "damage");
117 assert.equal(spell.target, "entity");
118 assert.equal(spellMinMagicLevel(spell), 1);
119 assert.equal(spell.starter, undefined);
120
121 const entry = listAllItemPickerEntries().find((e) => e.displayName === "Spellbook: Mana Bullet");
122 assert.ok(entry, "Mana Bullet spellbook missing from shop stock picker");
123 assert.equal(entry.itemId, 490);
124 assert.equal(ITEM_DEFINITIONS[490]?.consumableSpellId, SpellId.manaBullet);
125});
126
127test("Magic Missile spellbook survives the item catalog merge", () =>
128{
129 const entry = listAllItemPickerEntries().find((e) => e.displayName === "Spellbook: Magic Missile");
130 assert.ok(entry);
131 assert.equal(entry.itemId, 489);
132 assert.equal(ITEM_DEFINITIONS[489]?.consumableSpellId, SpellId.magicMissile);
133 assert.equal(ITEM_DEFINITIONS[402]?.key, "tourmaline-cut");
134});
server/tests/shop.test.js +1 −10
@@ -15,7 +15,7 @@ import
15 15 shopRestockDayKey,
16 16 lerp
17 17 } from "../../shared/shop/pricing.js";
18 import { ItemId, itemDefinition, listAllItemPickerEntries } from "../../shared/definitions/items.js";
18 import { ItemId, listAllItemPickerEntries } from "../../shared/definitions/items.js";
19 19 import { CraftingItemId } from "../../shared/definitions/craftingItems.js";
20 20 import { MobId, normalizeSpawnerConfig, isHumanoidMob } from "../../shared/definitions/mobs.js";
21 21 import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
@@ -48,15 +48,6 @@ test("listAllItemPickerEntries includes paper and bronze gather tools", () =>
48 48 assert.equal(canSellInNpcShop(CraftingItemId.bronzeAxe), false);
49 49 });
50 50
51 test("listAllItemPickerEntries includes Magic Missile spellbook", () =>
52 {
53 const entry = listAllItemPickerEntries().find((e) => e.displayName === "Spellbook: Magic Missile");
54 assert.ok(entry);
55 assert.equal(entry.itemId, 489);
56 assert.equal(itemDefinition(489)?.consumableSpellId, "magic-missile");
57 assert.notEqual(itemDefinition(402)?.key, "spellbook-magic-missile");
58 });
59
60 51 test("buy paper from a shopkeeper with explicit catalog", () =>
61 52 {
62 53 const harness = makeSimulation({ workerCount: 1 });
@@ -15,7 +15,7 @@ import
15 shopRestockDayKey,
15 shopRestockDayKey,
16 lerp
16 lerp
17 } from "../../shared/shop/pricing.js";
17 } from "../../shared/shop/pricing.js";
18import { ItemId, itemDefinition, listAllItemPickerEntries } from "../../shared/definitions/items.js";
18import { ItemId, listAllItemPickerEntries } from "../../shared/definitions/items.js";
19import { CraftingItemId } from "../../shared/definitions/craftingItems.js";
19import { CraftingItemId } from "../../shared/definitions/craftingItems.js";
20import { MobId, normalizeSpawnerConfig, isHumanoidMob } from "../../shared/definitions/mobs.js";
20import { MobId, normalizeSpawnerConfig, isHumanoidMob } from "../../shared/definitions/mobs.js";
21import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
21import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
@@ -48,15 +48,6 @@ test("listAllItemPickerEntries includes paper and bronze gather tools", () =>
48 assert.equal(canSellInNpcShop(CraftingItemId.bronzeAxe), false);
48 assert.equal(canSellInNpcShop(CraftingItemId.bronzeAxe), false);
49});
49});
50
50
51test("listAllItemPickerEntries includes Magic Missile spellbook", () =>
52{
53 const entry = listAllItemPickerEntries().find((e) => e.displayName === "Spellbook: Magic Missile");
54 assert.ok(entry);
55 assert.equal(entry.itemId, 489);
56 assert.equal(itemDefinition(489)?.consumableSpellId, "magic-missile");
57 assert.notEqual(itemDefinition(402)?.key, "spellbook-magic-missile");
58});
59
60test("buy paper from a shopkeeper with explicit catalog", () =>
51test("buy paper from a shopkeeper with explicit catalog", () =>
61{
52{
62 const harness = makeSimulation({ workerCount: 1 });
53 const harness = makeSimulation({ workerCount: 1 });
shared/definitions/spellItems.js +1 −0
@@ -6,6 +6,7 @@ import { SpellId, spellDefinition, learnableSpellIds } from "./spells.js";
6 6 * Legacy spark spellbook (134) is retired — id kept unused so saves do not collide.
7 7 */
8 8 const SPELLBOOK_ID_BY_SPELL = Object.freeze({
9 [SpellId.manaBullet]: 490,
9 10 [SpellId.dark]: 391,
10 11 [SpellId.magicMissile]: 489,
11 12 [SpellId.firebolt]: 392,
@@ -6,6 +6,7 @@ import { SpellId, spellDefinition, learnableSpellIds } from "./spells.js";
6 * Legacy spark spellbook (134) is retired — id kept unused so saves do not collide.
6 * Legacy spark spellbook (134) is retired — id kept unused so saves do not collide.
7 */
7 */
8const SPELLBOOK_ID_BY_SPELL = Object.freeze({
8const SPELLBOOK_ID_BY_SPELL = Object.freeze({
9 [SpellId.manaBullet]: 490,
9 [SpellId.dark]: 391,
10 [SpellId.dark]: 391,
10 [SpellId.magicMissile]: 489,
11 [SpellId.magicMissile]: 489,
11 [SpellId.firebolt]: 392,
12 [SpellId.firebolt]: 392,
shared/definitions/spells.js +13 −0
@@ -25,6 +25,7 @@ import { MagicElement } from "./attackTypes.js";
25 25 */
26 26 export const SpellId = Object.freeze({
27 27 light: "light",
28 manaBullet: "mana-bullet",
28 29 dark: "dark",
29 30 magicMissile: "magic-missile",
30 31 // Fire damage tree
@@ -369,6 +370,18 @@ export const SPELL_DEFINITIONS = Object.freeze({
369 370 effect: "light",
370 371 description: "A whisper of radiance that settles on stone or grass, holding back the dark for a while."
371 372 }),
373 [SpellId.manaBullet]: Object.freeze({
374 spellId: SpellId.manaBullet,
375 displayName: "Mana Bullet",
376 tier: 1,
377 element: "none",
378 target: "entity",
379 autoRepeat: true,
380 minMagicLevel: spellMinMagicLevelFromTier(1),
381 projectileSprite: BOLT_PROJECTILE,
382 effect: "damage",
383 description: "A small shot of raw mana, swift and simple, for those just learning to strike."
384 }),
372 385 [SpellId.dark]: Object.freeze({
373 386 spellId: SpellId.dark,
374 387 displayName: "Dark",
@@ -25,6 +25,7 @@ import { MagicElement } from "./attackTypes.js";
25 */
25 */
26export const SpellId = Object.freeze({
26export const SpellId = Object.freeze({
27 light: "light",
27 light: "light",
28 manaBullet: "mana-bullet",
28 dark: "dark",
29 dark: "dark",
29 magicMissile: "magic-missile",
30 magicMissile: "magic-missile",
30 // Fire damage tree
31 // Fire damage tree
@@ -369,6 +370,18 @@ export const SPELL_DEFINITIONS = Object.freeze({
369 effect: "light",
370 effect: "light",
370 description: "A whisper of radiance that settles on stone or grass, holding back the dark for a while."
371 description: "A whisper of radiance that settles on stone or grass, holding back the dark for a while."
371 }),
372 }),
373 [SpellId.manaBullet]: Object.freeze({
374 spellId: SpellId.manaBullet,
375 displayName: "Mana Bullet",
376 tier: 1,
377 element: "none",
378 target: "entity",
379 autoRepeat: true,
380 minMagicLevel: spellMinMagicLevelFromTier(1),
381 projectileSprite: BOLT_PROJECTILE,
382 effect: "damage",
383 description: "A small shot of raw mana, swift and simple, for those just learning to strike."
384 }),
372 [SpellId.dark]: Object.freeze({
385 [SpellId.dark]: Object.freeze({
373 spellId: SpellId.dark,
386 spellId: SpellId.dark,
374 displayName: "Dark",
387 displayName: "Dark",