BlueGit

timfa questward

Files

questward / server / tests / spells.test.js

spells.test.js

spells.test.js 15.1 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { SpellId, spellDefinition } from "../../shared/definitions/spells.js";
4import { SPELL_PROJECTILE_MS, SPELL_COOLDOWN_MS } from "../../shared/spells/constants.js";
5import { mobComponents } from "../src/simulation/spawners.js";
6import { MobId } from "../../shared/definitions/mobs.js";
7import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
8import { Component } from "../src/ecs/components.js";
9import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js";
10import
11 {
12 spellIdFromSpellbookStack,
13 spellbookItemIdForSpell,
14 SPELLBOOK_ITEM_DEFINITIONS
15 } from "../../shared/definitions/spellItems.js";
16import { itemDefinition } from "../../shared/definitions/items.js";
17import { normalizePlayerSpells } from "../../shared/spells/state.js";
18import { createSpellbookStack } from "../src/spells/service.js";
19import { SkillId } from "../../shared/definitions/skills.js";
20import { totalXpForLevel } from "../../shared/skills/progression.js";
21
22function workerOf (simulation, entityId)
23{
24 for (const worker of simulation.workers.values())
25 {
26 if (worker.world.ecs.handleOf(entityId) != null)
27 { return worker; }
28 }
29 return null;
30}
31
32test("cast light resolves without command error and can place a spell light", () =>
33{
34 const harness = makeSimulation({ workerCount: 1 });
35 try
36 {
37 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
38 harness.simulation.spawnCharacter(1, character);
39
40 harness.simulation.enqueue(1, {
41 type: "cast-spell-tile",
42 characterId: character.character_id,
43 spellId: SpellId.light,
44 tileX: 10,
45 tileY: 8
46 });
47 const cast = harness.simulation.step();
48 assert.equal(
49 cast.events.some((event) => event.type === "command-error"),
50 false,
51 JSON.stringify(cast.events)
52 );
53 assert.ok(
54 cast.events.some((event) => event.type === "spell-fizzle" || event.type === "spell-projectile"),
55 "expected fizzle or projectile"
56 );
57
58 if (!cast.events.some((event) => event.type === "spell-projectile"))
59 { return; }
60
61 harness.advance(SPELL_PROJECTILE_MS + 50);
62 const impact = harness.simulation.step();
63 assert.ok(
64 impact.events.some((event) => event.type === "spell-light-appear"),
65 JSON.stringify(impact.events.map((event) => event.type))
66 );
67 }
68 finally
69 { harness.close(); }
70});
71
72test("out-of-range spell cast paths toward the nearest in-range tile", () =>
73{
74 const harness = makeSimulation({ workerCount: 1 });
75 try
76 {
77 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
78 const playerId = harness.simulation.spawnCharacter(1, character);
79 harness.simulation.enqueue(1, {
80 type: "cast-spell-tile",
81 characterId: character.character_id,
82 spellId: SpellId.light,
83 tileX: 30,
84 tileY: 8
85 });
86 const cast = harness.simulation.step();
87 assert.equal(cast.events.some((event) => event.type === "command-error"), false);
88 const start = harness.simulation.entityRecord(playerId);
89 for (let i = 0; i < 80; i++)
90 { harness.simulation.step(); }
91 const moved = harness.simulation.entityRecord(playerId);
92 assert.ok(
93 moved.tileX !== start.tileX || moved.tileY !== start.tileY,
94 `expected movement from (${start.tileX},${start.tileY}), got (${moved.tileX},${moved.tileY})`
95 );
96 }
97 finally
98 { harness.close(); }
99});
100
101test("attack from out of range paths toward the target", () =>
102{
103 const harness = makeSimulation({ workerCount: 1 });
104 try
105 {
106 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
107 const playerId = harness.simulation.spawnCharacter(1, character);
108 const deerId = harness.simulation.spawnEntity(mobComponents({
109 tileX: 24,
110 tileY: 8,
111 mobTypeId: MobId.deer
112 }));
113 harness.simulation.enqueue(1, {
114 type: "attack",
115 characterId: character.character_id,
116 targetEntityId: deerId
117 });
118 harness.simulation.step();
119 const record = harness.simulation.entityRecord(playerId);
120 assert.ok(record.tileX < 24);
121 for (let i = 0; i < 80 && record.tileX < 20; i++)
122 { harness.simulation.step(); }
123 assert.ok(harness.simulation.entityRecord(playerId).tileX > 8);
124 }
125 finally
126 { harness.close(); }
127});
128
129test("spellbook stacks resolve a spell id even without an instance", () =>
130{
131 const itemId = spellbookItemIdForSpell(SpellId.manaBullet);
132 const def = itemDefinition(itemId);
133 assert.ok(def?.defaultInstance?.kind === "spellbook");
134 assert.equal(spellIdFromSpellbookStack({ itemId, quantity: 1 }, def), SpellId.manaBullet);
135 assert.equal(spellIdFromSpellbookStack({ itemId, quantity: 1, instance: null }, def), SpellId.manaBullet);
136 assert.equal(
137 spellIdFromSpellbookStack({ itemId, quantity: 1, instance: def.defaultInstance }, def),
138 SpellId.manaBullet
139 );
140 assert.ok(SPELLBOOK_ITEM_DEFINITIONS[itemId]?.consumableSpellId === SpellId.manaBullet);
141});
142
143test("read-spellbook learns the spell and consumes a catalog-style book", () =>
144{
145 const harness = makeSimulation({ workerCount: 1 });
146 try
147 {
148 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
149 const itemId = spellbookItemIdForSpell(SpellId.manaBullet);
150 const slots = emptySlots();
151 slots[0] = { itemId, quantity: 1, instance: null };
152 harness.stateDb.saveBag(character.character_id, {
153 inventory: slots,
154 equipment: emptyEquipment(),
155 vault: [],
156 vaultReserved: []
157 });
158 const playerId = harness.simulation.spawnCharacter(1, character);
159 const worker = workerOf(harness.simulation, playerId);
160 const handle = worker.world.ecs.handleOf(playerId);
161 const before = normalizePlayerSpells(worker.world.ecs.get(handle, Component.spells));
162 assert.equal(before.known.includes(SpellId.manaBullet), false);
163
164 harness.simulation.enqueue(1, {
165 type: "read-spellbook",
166 characterId: character.character_id,
167 slotIndex: 0
168 });
169 const result = harness.simulation.step();
170 assert.equal(
171 result.events.some((event) => event.type === "command-error"),
172 false,
173 JSON.stringify(result.events)
174 );
175 assert.ok(result.events.some((event) => event.type === "spells"));
176 const updated = result.events.find((event) => event.type === "spells");
177 assert.ok(updated.spells.some((spell) => spell.spellId === SpellId.manaBullet));
178 assert.equal(spellDefinition(SpellId.manaBullet).displayName, "Mana Bullet");
179
180 const inv = worker.world.ecs.get(handle, Component.inventory);
181 assert.equal(inv.slots[0], null);
182 const after = normalizePlayerSpells(worker.world.ecs.get(handle, Component.spells));
183 assert.ok(after.known.includes(SpellId.manaBullet));
184 }
185 finally
186 { harness.close(); }
187});
188
189test("read-spellbook with instance stack also works and rejects duplicates", () =>
190{
191 const harness = makeSimulation({ workerCount: 1 });
192 try
193 {
194 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
195 const stack = createSpellbookStack(SpellId.magicMissile);
196 assert.ok(stack?.instance?.kind === "spellbook");
197 const slots = emptySlots();
198 slots[0] = stack;
199 slots[1] = structuredClone(stack);
200 harness.stateDb.saveBag(character.character_id, {
201 inventory: slots,
202 equipment: emptyEquipment(),
203 vault: [],
204 vaultReserved: []
205 });
206 const playerId = harness.simulation.spawnCharacter(1, character);
207
208 harness.simulation.enqueue(1, {
209 type: "read-spellbook",
210 characterId: character.character_id,
211 slotIndex: 0
212 });
213 const learned = harness.simulation.step();
214 assert.equal(learned.events.some((event) => event.type === "command-error"), false);
215
216 harness.simulation.enqueue(1, {
217 type: "read-spellbook",
218 characterId: character.character_id,
219 slotIndex: 1
220 });
221 const again = harness.simulation.step();
222 assert.ok(again.events.some((event) => event.type === "command-error"));
223 const err = again.events.find((event) => event.type === "command-error");
224 assert.match(err.message ?? "", /already know/i);
225 }
226 finally
227 { harness.close(); }
228});
229
230test("auto-cast repeats an entity damage spell until cancelled by move", () =>
231{
232 const harness = makeSimulation({ workerCount: 1 });
233 const random = Math.random;
234 Math.random = () => 0.999;
235 try
236 {
237 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
238 const playerId = harness.simulation.spawnCharacter(1, character);
239 const deerId = harness.simulation.spawnEntity(mobComponents({
240 tileX: 10,
241 tileY: 8,
242 mobTypeId: MobId.deer,
243 level: 40
244 }));
245 const worker = workerOf(harness.simulation, playerId);
246 const handle = worker.world.ecs.handleOf(playerId);
247 const deerHandle = worker.world.ecs.handleOf(deerId);
248 const deerHealth = worker.world.ecs.get(deerHandle, Component.health);
249 deerHealth.max = 500;
250 deerHealth.current = 500;
251 const spells = worker.world.ecs.get(handle, Component.spells);
252 spells.known.push(SpellId.manaBullet);
253 spells.autoCast = true;
254 const skills = worker.world.ecs.get(handle, Component.skills);
255 skills.xp[SkillId.magic] = totalXpForLevel(20);
256 skills.xp[SkillId.mind] = totalXpForLevel(20);
257
258 harness.simulation.enqueue(1, {
259 type: "cast-spell-entity",
260 characterId: character.character_id,
261 spellId: SpellId.manaBullet,
262 targetEntityId: deerId
263 });
264 const first = harness.simulation.step();
265 assert.ok(first.events.some((e) => e.type === "spell-projectile" || e.type === "spell-fizzle"));
266
267 const casting = worker.world.ecs.get(handle, Component.spellcasting);
268 assert.equal(casting.repeatSpellId, SpellId.manaBullet);
269 assert.equal(casting.repeatTargetEntityId, deerId);
270
271 harness.advance(SPELL_COOLDOWN_MS + 10);
272 const second = harness.simulation.step();
273 assert.ok(
274 second.events.some((e) => e.type === "spell-projectile" || e.type === "spell-fizzle"),
275 `expected auto-cast to fire again after cooldown; got ${second.events.map((e) => e.type).join(",")}`
276 );
277 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
278
279 harness.simulation.enqueue(1, {
280 type: "move-to",
281 characterId: character.character_id,
282 tileX: 6,
283 tileY: 8
284 });
285 harness.simulation.step();
286 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, null);
287
288 harness.advance(SPELL_COOLDOWN_MS + 10);
289 const afterMove = harness.simulation.step();
290 assert.equal(
291 afterMove.events.some((e) => e.type === "spell-projectile" && e.spellId === SpellId.manaBullet),
292 false
293 );
294 }
295 finally
296 {
297 Math.random = random;
298 harness.close();
299 }
300});
301
302test("casting a spell clears the walk path", () =>
303{
304 const harness = makeSimulation({ workerCount: 1 });
305 const random = Math.random;
306 Math.random = () => 0.999;
307 try
308 {
309 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
310 const playerId = harness.simulation.spawnCharacter(1, character);
311 const deerId = harness.simulation.spawnEntity(mobComponents({
312 tileX: 10,
313 tileY: 8,
314 mobTypeId: MobId.deer
315 }));
316 const worker = workerOf(harness.simulation, playerId);
317 const handle = worker.world.ecs.handleOf(playerId);
318 const spells = worker.world.ecs.get(handle, Component.spells);
319 spells.known.push(SpellId.manaBullet);
320 const skills = worker.world.ecs.get(handle, Component.skills);
321 skills.xp[SkillId.magic] = totalXpForLevel(20);
322
323 harness.simulation.enqueue(1, {
324 type: "move-to",
325 characterId: character.character_id,
326 tileX: 14,
327 tileY: 8
328 });
329 harness.simulation.step();
330 assert.ok(worker.world.ecs.get(handle, Component.path)?.tiles?.length > 0);
331
332 harness.simulation.enqueue(1, {
333 type: "cast-spell-entity",
334 characterId: character.character_id,
335 spellId: SpellId.manaBullet,
336 targetEntityId: deerId
337 });
338 harness.simulation.step();
339 assert.equal(worker.world.ecs.get(handle, Component.path)?.tiles?.length ?? 0, 0);
340 }
341 finally
342 {
343 Math.random = random;
344 harness.close();
345 }
346});
347
348test("physical attack cancels spell auto-cast", () =>
349{
350 const harness = makeSimulation({ workerCount: 1 });
351 const random = Math.random;
352 Math.random = () => 0.999;
353 try
354 {
355 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
356 const playerId = harness.simulation.spawnCharacter(1, character);
357 const deerId = harness.simulation.spawnEntity(mobComponents({
358 tileX: 9,
359 tileY: 8,
360 mobTypeId: MobId.deer
361 }));
362 const worker = workerOf(harness.simulation, playerId);
363 const handle = worker.world.ecs.handleOf(playerId);
364 const spells = worker.world.ecs.get(handle, Component.spells);
365 spells.known.push(SpellId.manaBullet);
366 spells.autoCast = true;
367 const skills = worker.world.ecs.get(handle, Component.skills);
368 skills.xp[SkillId.magic] = totalXpForLevel(20);
369
370 harness.simulation.enqueue(1, {
371 type: "cast-spell-entity",
372 characterId: character.character_id,
373 spellId: SpellId.manaBullet,
374 targetEntityId: deerId
375 });
376 harness.simulation.step();
377 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
378
379 harness.simulation.enqueue(1, {
380 type: "attack",
381 characterId: character.character_id,
382 targetEntityId: deerId
383 });
384 harness.simulation.step();
385 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, null);
386 }
387 finally
388 {
389 Math.random = random;
390 harness.close();
391 }
392});
393
394test("auto-retaliate does not engage while auto-casting an attack spell", () =>
395{
396 const harness = makeSimulation({ workerCount: 1 });
397 const random = Math.random;
398 Math.random = () => 0.999;
399 try
400 {
401 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
402 const playerId = harness.simulation.spawnCharacter(1, character);
403 const deerId = harness.simulation.spawnEntity(mobComponents({
404 tileX: 10,
405 tileY: 8,
406 mobTypeId: MobId.deer,
407 level: 40
408 }));
409 const ratId = harness.simulation.spawnEntity(mobComponents({
410 tileX: 9,
411 tileY: 8,
412 mobTypeId: MobId.rat,
413 spawnerId: null
414 }));
415 const worker = workerOf(harness.simulation, playerId);
416 const handle = worker.world.ecs.handleOf(playerId);
417 const deerHandle = worker.world.ecs.handleOf(deerId);
418 const deerHealth = worker.world.ecs.get(deerHandle, Component.health);
419 deerHealth.max = 500;
420 deerHealth.current = 500;
421 const spells = worker.world.ecs.get(handle, Component.spells);
422 spells.known.push(SpellId.manaBullet);
423 spells.autoCast = true;
424 const skills = worker.world.ecs.get(handle, Component.skills);
425 skills.xp[SkillId.magic] = totalXpForLevel(20);
426 skills.xp[SkillId.mind] = totalXpForLevel(20);
427
428 harness.simulation.enqueue(1, {
429 type: "cast-spell-entity",
430 characterId: character.character_id,
431 spellId: SpellId.manaBullet,
432 targetEntityId: deerId
433 });
434 harness.simulation.step();
435 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
436 assert.equal(worker.world.ecs.get(handle, Component.combat)?.targetEntityId ?? null, null);
437
438 let ratSwung = false;
439 for (let i = 0; i < 80; i++)
440 {
441 harness.advance(50);
442 const events = harness.simulation.step().events;
443 if (events.some((event) =>
444 event.type === "combat-swing" && event.attackerId === ratId && event.targetId === playerId
445 ))
446 {
447 ratSwung = true;
448 break;
449 }
450 }
451 assert.ok(ratSwung, "expected the rat to swing at the player");
452 assert.equal(
453 worker.world.ecs.get(handle, Component.combat)?.targetEntityId ?? null,
454 null,
455 "auto-cast is the attack — no melee retaliate engage"
456 );
457 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
458 }
459 finally
460 {
461 Math.random = random;
462 harness.close();
463 }
464});