BlueGit

timfa questward

Files

questward / server / tests / spells.test.js

spells.test.js

spells.test.js 22.4 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("cancel-arm after a cast does not clear auto-repeat", () =>
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 worker = workerOf(harness.simulation, playerId);
410 const handle = worker.world.ecs.handleOf(playerId);
411 const deerHandle = worker.world.ecs.handleOf(deerId);
412 worker.world.ecs.get(deerHandle, Component.health).max = 500;
413 worker.world.ecs.get(deerHandle, Component.health).current = 500;
414 const spells = worker.world.ecs.get(handle, Component.spells);
415 spells.known.push(SpellId.manaBullet);
416 spells.autoCast = true;
417 const skills = worker.world.ecs.get(handle, Component.skills);
418 skills.xp[SkillId.magic] = totalXpForLevel(20);
419 skills.xp[SkillId.mind] = totalXpForLevel(20);
420
421 harness.simulation.enqueue(1, {
422 type: "cast-spell-entity",
423 characterId: character.character_id,
424 spellId: SpellId.manaBullet,
425 targetEntityId: deerId
426 });
427 harness.simulation.step();
428 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
429
430 /** Late right-click cancel while the client still thought the spell was armed. */
431 harness.simulation.enqueue(1, {
432 type: "cancel-arm-spell",
433 characterId: character.character_id
434 });
435 harness.simulation.step();
436 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
437
438 harness.advance(SPELL_COOLDOWN_MS + 10);
439 const again = harness.simulation.step();
440 assert.ok(
441 again.events.some((e) => e.type === "spell-projectile" || e.type === "spell-fizzle"),
442 "auto-cast must continue after cancel-arm"
443 );
444 }
445 finally
446 {
447 Math.random = random;
448 harness.close();
449 }
450});
451
452test("re-arming the same auto-cast spell keeps repeating", () =>
453{
454 const harness = makeSimulation({ workerCount: 1 });
455 const random = Math.random;
456 Math.random = () => 0.999;
457 try
458 {
459 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
460 const playerId = harness.simulation.spawnCharacter(1, character);
461 const deerId = harness.simulation.spawnEntity(mobComponents({
462 tileX: 10,
463 tileY: 8,
464 mobTypeId: MobId.deer,
465 level: 40
466 }));
467 const worker = workerOf(harness.simulation, playerId);
468 const handle = worker.world.ecs.handleOf(playerId);
469 const deerHandle = worker.world.ecs.handleOf(deerId);
470 worker.world.ecs.get(deerHandle, Component.health).max = 500;
471 worker.world.ecs.get(deerHandle, Component.health).current = 500;
472 const spells = worker.world.ecs.get(handle, Component.spells);
473 spells.known.push(SpellId.manaBullet);
474 spells.autoCast = true;
475 const skills = worker.world.ecs.get(handle, Component.skills);
476 skills.xp[SkillId.magic] = totalXpForLevel(20);
477 skills.xp[SkillId.mind] = totalXpForLevel(20);
478
479 harness.simulation.enqueue(1, {
480 type: "cast-spell-entity",
481 characterId: character.character_id,
482 spellId: SpellId.manaBullet,
483 targetEntityId: deerId
484 });
485 harness.simulation.step();
486 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
487
488 harness.simulation.enqueue(1, {
489 type: "arm-spell",
490 characterId: character.character_id,
491 spellId: SpellId.manaBullet
492 });
493 harness.simulation.step();
494 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
495 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).armedSpellId, SpellId.manaBullet);
496 }
497 finally
498 {
499 Math.random = random;
500 harness.close();
501 }
502});
503
504test("auto-retaliate does not engage while auto-casting an attack spell", () =>
505{
506 const harness = makeSimulation({ workerCount: 1 });
507 const random = Math.random;
508 Math.random = () => 0.999;
509 try
510 {
511 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
512 const playerId = harness.simulation.spawnCharacter(1, character);
513 const deerId = harness.simulation.spawnEntity(mobComponents({
514 tileX: 10,
515 tileY: 8,
516 mobTypeId: MobId.deer,
517 level: 40
518 }));
519 const ratId = harness.simulation.spawnEntity(mobComponents({
520 tileX: 9,
521 tileY: 8,
522 mobTypeId: MobId.rat,
523 spawnerId: null
524 }));
525 const worker = workerOf(harness.simulation, playerId);
526 const handle = worker.world.ecs.handleOf(playerId);
527 const deerHandle = worker.world.ecs.handleOf(deerId);
528 const deerHealth = worker.world.ecs.get(deerHandle, Component.health);
529 deerHealth.max = 500;
530 deerHealth.current = 500;
531 const spells = worker.world.ecs.get(handle, Component.spells);
532 spells.known.push(SpellId.manaBullet);
533 spells.autoCast = true;
534 const skills = worker.world.ecs.get(handle, Component.skills);
535 skills.xp[SkillId.magic] = totalXpForLevel(20);
536 skills.xp[SkillId.mind] = totalXpForLevel(20);
537
538 harness.simulation.enqueue(1, {
539 type: "cast-spell-entity",
540 characterId: character.character_id,
541 spellId: SpellId.manaBullet,
542 targetEntityId: deerId
543 });
544 harness.simulation.step();
545 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
546 assert.equal(worker.world.ecs.get(handle, Component.combat)?.targetEntityId ?? null, null);
547
548 let ratSwung = false;
549 for (let i = 0; i < 80; i++)
550 {
551 harness.advance(50);
552 const events = harness.simulation.step().events;
553 if (events.some((event) =>
554 event.type === "combat-swing" && event.attackerId === ratId && event.targetId === playerId
555 ))
556 {
557 ratSwung = true;
558 break;
559 }
560 }
561 assert.ok(ratSwung, "expected the rat to swing at the player");
562 assert.equal(
563 worker.world.ecs.get(handle, Component.combat)?.targetEntityId ?? null,
564 null,
565 "auto-cast is the attack — no melee retaliate engage"
566 );
567 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
568 }
569 finally
570 {
571 Math.random = random;
572 harness.close();
573 }
574});
575
576test("death clears active auto-repeat but keeps auto-cast for respawn", () =>
577{
578 const harness = makeSimulation({ workerCount: 1 });
579 const random = Math.random;
580 Math.random = () => 0.999;
581 try
582 {
583 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
584 const playerId = harness.simulation.spawnCharacter(1, character);
585 const deerId = harness.simulation.spawnEntity(mobComponents({
586 tileX: 10,
587 tileY: 8,
588 mobTypeId: MobId.deer,
589 level: 40
590 }));
591 const worker = workerOf(harness.simulation, playerId);
592 const handle = worker.world.ecs.handleOf(playerId);
593 const deerHandle = worker.world.ecs.handleOf(deerId);
594 worker.world.ecs.get(deerHandle, Component.health).max = 500;
595 worker.world.ecs.get(deerHandle, Component.health).current = 500;
596 const spells = worker.world.ecs.get(handle, Component.spells);
597 spells.known.push(SpellId.manaBullet);
598 spells.autoCast = true;
599 const skills = worker.world.ecs.get(handle, Component.skills);
600 skills.xp[SkillId.magic] = totalXpForLevel(20);
601 skills.xp[SkillId.mind] = totalXpForLevel(20);
602
603 harness.simulation.enqueue(1, {
604 type: "cast-spell-entity",
605 characterId: character.character_id,
606 spellId: SpellId.manaBullet,
607 targetEntityId: deerId
608 });
609 harness.simulation.step();
610 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
611
612 const killerId = harness.simulation.spawnEntity(mobComponents({
613 tileX: 9,
614 tileY: 8,
615 mobTypeId: MobId.deer,
616 spawnerId: null
617 }));
618 worker.world.ecs.get(worker.world.ecs.handleOf(killerId), Component.health).current = 1_000_000;
619 worker.world.ecs.get(worker.world.ecs.handleOf(killerId), Component.health).max = 1_000_000;
620 worker.world.ecs.get(handle, Component.health).current = 1;
621 harness.simulation.enqueue(1, {
622 type: "attack",
623 characterId: character.character_id,
624 targetEntityId: killerId
625 });
626 let died = false;
627 for (let i = 0; i < 100; i++)
628 {
629 harness.advance(1000);
630 const events = harness.simulation.step().events;
631 if (events.some((event) => event.type === "player-died" && event.entityId === playerId))
632 {
633 died = true;
634 break;
635 }
636 }
637 assert.ok(died, "player should die from retaliation");
638 assert.equal(worker.world.ecs.get(handle, Component.spells).autoCast, true);
639 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, null);
640 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).armedSpellId, null);
641
642 harness.simulation.enqueue(1, { type: "respawn", characterId: character.character_id });
643 harness.simulation.step();
644 assert.equal(worker.world.ecs.has(handle, Component.dead), false);
645 assert.equal(worker.world.ecs.get(handle, Component.spells).autoCast, true);
646
647 const casting = worker.world.ecs.get(handle, Component.spellcasting);
648 casting.nextCastAt = 0;
649 const pos = worker.world.ecs.get(handle, Component.position);
650 pos.tileX = 8;
651 pos.tileY = 8;
652 worker.world.ecs.get(deerHandle, Component.health).current = 500;
653
654 harness.simulation.enqueue(1, {
655 type: "cast-spell-entity",
656 characterId: character.character_id,
657 spellId: SpellId.manaBullet,
658 targetEntityId: deerId
659 });
660 const castResult = harness.simulation.step();
661 assert.equal(
662 castResult.events.some((event) => event.type === "command-error"),
663 false,
664 castResult.events.find((event) => event.type === "command-error")?.message
665 ?? "cast after respawn should succeed"
666 );
667 assert.equal(
668 worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId,
669 SpellId.manaBullet,
670 "auto-cast preference still arms repeat after respawn"
671 );
672 }
673 finally
674 {
675 Math.random = random;
676 harness.close();
677 }
678});