BlueGit

timfa questward

Files

questward / server / tests / spells.test.js

spells.test.js

60aff71a History
spells.test.js 25.7 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { SpellId, spellDefinition, spellDifficulty } 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, spellCastXpFromDifficulty } from "../../shared/skills/progression.js";
21import { engageTarget, ensureMobCombat } from "../src/combat/service.js";
22
23function workerOf (simulation, entityId)
24{
25 for (const worker of simulation.workers.values())
26 {
27 if (worker.world.ecs.handleOf(entityId) != null)
28 { return worker; }
29 }
30 return null;
31}
32
33test("cast light resolves without command error and can place a spell light", () =>
34{
35 const harness = makeSimulation({ workerCount: 1 });
36 try
37 {
38 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
39 harness.simulation.spawnCharacter(1, character);
40
41 harness.simulation.enqueue(1, {
42 type: "cast-spell-tile",
43 characterId: character.character_id,
44 spellId: SpellId.light,
45 tileX: 10,
46 tileY: 8
47 });
48 const cast = harness.simulation.step();
49 assert.equal(
50 cast.events.some((event) => event.type === "command-error"),
51 false,
52 JSON.stringify(cast.events)
53 );
54 assert.ok(
55 cast.events.some((event) => event.type === "spell-fizzle" || event.type === "spell-projectile"),
56 "expected fizzle or projectile"
57 );
58
59 if (!cast.events.some((event) => event.type === "spell-projectile"))
60 { return; }
61
62 harness.advance(SPELL_PROJECTILE_MS + 50);
63 const impact = harness.simulation.step();
64 assert.ok(
65 impact.events.some((event) => event.type === "spell-light-appear"),
66 JSON.stringify(impact.events.map((event) => event.type))
67 );
68 }
69 finally
70 { harness.close(); }
71});
72
73test("out-of-range spell cast paths toward the nearest in-range tile", () =>
74{
75 const harness = makeSimulation({ workerCount: 1 });
76 try
77 {
78 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
79 const playerId = harness.simulation.spawnCharacter(1, character);
80 harness.simulation.enqueue(1, {
81 type: "cast-spell-tile",
82 characterId: character.character_id,
83 spellId: SpellId.light,
84 tileX: 30,
85 tileY: 8
86 });
87 const cast = harness.simulation.step();
88 assert.equal(cast.events.some((event) => event.type === "command-error"), false);
89 const start = harness.simulation.entityRecord(playerId);
90 for (let i = 0; i < 80; i++)
91 { harness.simulation.step(); }
92 const moved = harness.simulation.entityRecord(playerId);
93 assert.ok(
94 moved.tileX !== start.tileX || moved.tileY !== start.tileY,
95 `expected movement from (${start.tileX},${start.tileY}), got (${moved.tileX},${moved.tileY})`
96 );
97 }
98 finally
99 { harness.close(); }
100});
101
102test("attack from out of range paths toward the target", () =>
103{
104 const harness = makeSimulation({ workerCount: 1 });
105 try
106 {
107 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
108 const playerId = harness.simulation.spawnCharacter(1, character);
109 const deerId = harness.simulation.spawnEntity(mobComponents({
110 tileX: 24,
111 tileY: 8,
112 mobTypeId: MobId.deer
113 }));
114 harness.simulation.enqueue(1, {
115 type: "attack",
116 characterId: character.character_id,
117 targetEntityId: deerId
118 });
119 harness.simulation.step();
120 const record = harness.simulation.entityRecord(playerId);
121 assert.ok(record.tileX < 24);
122 for (let i = 0; i < 80 && record.tileX < 20; i++)
123 { harness.simulation.step(); }
124 assert.ok(harness.simulation.entityRecord(playerId).tileX > 8);
125 }
126 finally
127 { harness.close(); }
128});
129
130test("spellbook stacks resolve a spell id even without an instance", () =>
131{
132 const itemId = spellbookItemIdForSpell(SpellId.manaBullet);
133 const def = itemDefinition(itemId);
134 assert.ok(def?.defaultInstance?.kind === "spellbook");
135 assert.equal(spellIdFromSpellbookStack({ itemId, quantity: 1 }, def), SpellId.manaBullet);
136 assert.equal(spellIdFromSpellbookStack({ itemId, quantity: 1, instance: null }, def), SpellId.manaBullet);
137 assert.equal(
138 spellIdFromSpellbookStack({ itemId, quantity: 1, instance: def.defaultInstance }, def),
139 SpellId.manaBullet
140 );
141 assert.ok(SPELLBOOK_ITEM_DEFINITIONS[itemId]?.consumableSpellId === SpellId.manaBullet);
142});
143
144test("read-spellbook learns the spell and consumes a catalog-style book", () =>
145{
146 const harness = makeSimulation({ workerCount: 1 });
147 try
148 {
149 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
150 const itemId = spellbookItemIdForSpell(SpellId.manaBullet);
151 const slots = emptySlots();
152 slots[0] = { itemId, quantity: 1, instance: null };
153 harness.stateDb.saveBag(character.character_id, {
154 inventory: slots,
155 equipment: emptyEquipment(),
156 vault: [],
157 vaultReserved: []
158 });
159 const playerId = harness.simulation.spawnCharacter(1, character);
160 const worker = workerOf(harness.simulation, playerId);
161 const handle = worker.world.ecs.handleOf(playerId);
162 const before = normalizePlayerSpells(worker.world.ecs.get(handle, Component.spells));
163 assert.equal(before.known.includes(SpellId.manaBullet), false);
164
165 harness.simulation.enqueue(1, {
166 type: "read-spellbook",
167 characterId: character.character_id,
168 slotIndex: 0
169 });
170 const result = harness.simulation.step();
171 assert.equal(
172 result.events.some((event) => event.type === "command-error"),
173 false,
174 JSON.stringify(result.events)
175 );
176 assert.ok(result.events.some((event) => event.type === "spells"));
177 const updated = result.events.find((event) => event.type === "spells");
178 assert.ok(updated.spells.some((spell) => spell.spellId === SpellId.manaBullet));
179 assert.equal(spellDefinition(SpellId.manaBullet).displayName, "Mana Bullet");
180
181 const inv = worker.world.ecs.get(handle, Component.inventory);
182 assert.equal(inv.slots[0], null);
183 const after = normalizePlayerSpells(worker.world.ecs.get(handle, Component.spells));
184 assert.ok(after.known.includes(SpellId.manaBullet));
185 }
186 finally
187 { harness.close(); }
188});
189
190test("read-spellbook with instance stack also works and rejects duplicates", () =>
191{
192 const harness = makeSimulation({ workerCount: 1 });
193 try
194 {
195 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
196 const stack = createSpellbookStack(SpellId.magicMissile);
197 assert.ok(stack?.instance?.kind === "spellbook");
198 const slots = emptySlots();
199 slots[0] = stack;
200 slots[1] = structuredClone(stack);
201 harness.stateDb.saveBag(character.character_id, {
202 inventory: slots,
203 equipment: emptyEquipment(),
204 vault: [],
205 vaultReserved: []
206 });
207 const playerId = harness.simulation.spawnCharacter(1, character);
208
209 harness.simulation.enqueue(1, {
210 type: "read-spellbook",
211 characterId: character.character_id,
212 slotIndex: 0
213 });
214 const learned = harness.simulation.step();
215 assert.equal(learned.events.some((event) => event.type === "command-error"), false);
216
217 harness.simulation.enqueue(1, {
218 type: "read-spellbook",
219 characterId: character.character_id,
220 slotIndex: 1
221 });
222 const again = harness.simulation.step();
223 assert.ok(again.events.some((event) => event.type === "command-error"));
224 const err = again.events.find((event) => event.type === "command-error");
225 assert.match(err.message ?? "", /already know/i);
226 }
227 finally
228 { harness.close(); }
229});
230
231test("auto-cast repeats an entity damage spell until cancelled by move", () =>
232{
233 const harness = makeSimulation({ workerCount: 1 });
234 const random = Math.random;
235 Math.random = () => 0.999;
236 try
237 {
238 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
239 const playerId = harness.simulation.spawnCharacter(1, character);
240 const deerId = harness.simulation.spawnEntity(mobComponents({
241 tileX: 10,
242 tileY: 8,
243 mobTypeId: MobId.deer,
244 level: 40
245 }));
246 const worker = workerOf(harness.simulation, playerId);
247 const handle = worker.world.ecs.handleOf(playerId);
248 const deerHandle = worker.world.ecs.handleOf(deerId);
249 const deerHealth = worker.world.ecs.get(deerHandle, Component.health);
250 deerHealth.max = 500;
251 deerHealth.current = 500;
252 const spells = worker.world.ecs.get(handle, Component.spells);
253 spells.known.push(SpellId.manaBullet);
254 spells.autoCast = true;
255 const skills = worker.world.ecs.get(handle, Component.skills);
256 skills.xp[SkillId.magic] = totalXpForLevel(20);
257 skills.xp[SkillId.mind] = totalXpForLevel(20);
258
259 harness.simulation.enqueue(1, {
260 type: "cast-spell-entity",
261 characterId: character.character_id,
262 spellId: SpellId.manaBullet,
263 targetEntityId: deerId
264 });
265 const first = harness.simulation.step();
266 assert.ok(first.events.some((e) => e.type === "spell-projectile" || e.type === "spell-fizzle"));
267
268 const casting = worker.world.ecs.get(handle, Component.spellcasting);
269 assert.equal(casting.repeatSpellId, SpellId.manaBullet);
270 assert.equal(casting.repeatTargetEntityId, deerId);
271
272 harness.advance(SPELL_COOLDOWN_MS + 10);
273 const second = harness.simulation.step();
274 assert.ok(
275 second.events.some((e) => e.type === "spell-projectile" || e.type === "spell-fizzle"),
276 `expected auto-cast to fire again after cooldown; got ${second.events.map((e) => e.type).join(",")}`
277 );
278 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
279
280 harness.simulation.enqueue(1, {
281 type: "move-to",
282 characterId: character.character_id,
283 tileX: 6,
284 tileY: 8
285 });
286 harness.simulation.step();
287 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, null);
288
289 harness.advance(SPELL_COOLDOWN_MS + 10);
290 const afterMove = harness.simulation.step();
291 assert.equal(
292 afterMove.events.some((e) => e.type === "spell-projectile" && e.spellId === SpellId.manaBullet),
293 false
294 );
295 }
296 finally
297 {
298 Math.random = random;
299 harness.close();
300 }
301});
302
303test("casting a spell clears the walk path", () =>
304{
305 const harness = makeSimulation({ workerCount: 1 });
306 const random = Math.random;
307 Math.random = () => 0.999;
308 try
309 {
310 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
311 const playerId = harness.simulation.spawnCharacter(1, character);
312 const deerId = harness.simulation.spawnEntity(mobComponents({
313 tileX: 10,
314 tileY: 8,
315 mobTypeId: MobId.deer
316 }));
317 const worker = workerOf(harness.simulation, playerId);
318 const handle = worker.world.ecs.handleOf(playerId);
319 const spells = worker.world.ecs.get(handle, Component.spells);
320 spells.known.push(SpellId.manaBullet);
321 const skills = worker.world.ecs.get(handle, Component.skills);
322 skills.xp[SkillId.magic] = totalXpForLevel(20);
323
324 harness.simulation.enqueue(1, {
325 type: "move-to",
326 characterId: character.character_id,
327 tileX: 14,
328 tileY: 8
329 });
330 harness.simulation.step();
331 assert.ok(worker.world.ecs.get(handle, Component.path)?.tiles?.length > 0);
332
333 harness.simulation.enqueue(1, {
334 type: "cast-spell-entity",
335 characterId: character.character_id,
336 spellId: SpellId.manaBullet,
337 targetEntityId: deerId
338 });
339 harness.simulation.step();
340 assert.equal(worker.world.ecs.get(handle, Component.path)?.tiles?.length ?? 0, 0);
341 }
342 finally
343 {
344 Math.random = random;
345 harness.close();
346 }
347});
348
349test("physical attack cancels spell auto-cast", () =>
350{
351 const harness = makeSimulation({ workerCount: 1 });
352 const random = Math.random;
353 Math.random = () => 0.999;
354 try
355 {
356 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
357 const playerId = harness.simulation.spawnCharacter(1, character);
358 const deerId = harness.simulation.spawnEntity(mobComponents({
359 tileX: 9,
360 tileY: 8,
361 mobTypeId: MobId.deer
362 }));
363 const worker = workerOf(harness.simulation, playerId);
364 const handle = worker.world.ecs.handleOf(playerId);
365 const spells = worker.world.ecs.get(handle, Component.spells);
366 spells.known.push(SpellId.manaBullet);
367 spells.autoCast = true;
368 const skills = worker.world.ecs.get(handle, Component.skills);
369 skills.xp[SkillId.magic] = totalXpForLevel(20);
370
371 harness.simulation.enqueue(1, {
372 type: "cast-spell-entity",
373 characterId: character.character_id,
374 spellId: SpellId.manaBullet,
375 targetEntityId: deerId
376 });
377 harness.simulation.step();
378 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
379
380 harness.simulation.enqueue(1, {
381 type: "attack",
382 characterId: character.character_id,
383 targetEntityId: deerId
384 });
385 harness.simulation.step();
386 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, null);
387 }
388 finally
389 {
390 Math.random = random;
391 harness.close();
392 }
393});
394
395test("cancel-arm after a cast does not clear auto-repeat", () =>
396{
397 const harness = makeSimulation({ workerCount: 1 });
398 const random = Math.random;
399 Math.random = () => 0.999;
400 try
401 {
402 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
403 const playerId = harness.simulation.spawnCharacter(1, character);
404 const deerId = harness.simulation.spawnEntity(mobComponents({
405 tileX: 10,
406 tileY: 8,
407 mobTypeId: MobId.deer,
408 level: 40
409 }));
410 const worker = workerOf(harness.simulation, playerId);
411 const handle = worker.world.ecs.handleOf(playerId);
412 const deerHandle = worker.world.ecs.handleOf(deerId);
413 worker.world.ecs.get(deerHandle, Component.health).max = 500;
414 worker.world.ecs.get(deerHandle, Component.health).current = 500;
415 const spells = worker.world.ecs.get(handle, Component.spells);
416 spells.known.push(SpellId.manaBullet);
417 spells.autoCast = true;
418 const skills = worker.world.ecs.get(handle, Component.skills);
419 skills.xp[SkillId.magic] = totalXpForLevel(20);
420 skills.xp[SkillId.mind] = totalXpForLevel(20);
421
422 harness.simulation.enqueue(1, {
423 type: "cast-spell-entity",
424 characterId: character.character_id,
425 spellId: SpellId.manaBullet,
426 targetEntityId: deerId
427 });
428 harness.simulation.step();
429 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
430
431 /** Late right-click cancel while the client still thought the spell was armed. */
432 harness.simulation.enqueue(1, {
433 type: "cancel-arm-spell",
434 characterId: character.character_id
435 });
436 harness.simulation.step();
437 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
438
439 harness.advance(SPELL_COOLDOWN_MS + 10);
440 const again = harness.simulation.step();
441 assert.ok(
442 again.events.some((e) => e.type === "spell-projectile" || e.type === "spell-fizzle"),
443 "auto-cast must continue after cancel-arm"
444 );
445 }
446 finally
447 {
448 Math.random = random;
449 harness.close();
450 }
451});
452
453test("re-arming the same auto-cast spell keeps repeating", () =>
454{
455 const harness = makeSimulation({ workerCount: 1 });
456 const random = Math.random;
457 Math.random = () => 0.999;
458 try
459 {
460 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
461 const playerId = harness.simulation.spawnCharacter(1, character);
462 const deerId = harness.simulation.spawnEntity(mobComponents({
463 tileX: 10,
464 tileY: 8,
465 mobTypeId: MobId.deer,
466 level: 40
467 }));
468 const worker = workerOf(harness.simulation, playerId);
469 const handle = worker.world.ecs.handleOf(playerId);
470 const deerHandle = worker.world.ecs.handleOf(deerId);
471 worker.world.ecs.get(deerHandle, Component.health).max = 500;
472 worker.world.ecs.get(deerHandle, Component.health).current = 500;
473 const spells = worker.world.ecs.get(handle, Component.spells);
474 spells.known.push(SpellId.manaBullet);
475 spells.autoCast = true;
476 const skills = worker.world.ecs.get(handle, Component.skills);
477 skills.xp[SkillId.magic] = totalXpForLevel(20);
478 skills.xp[SkillId.mind] = totalXpForLevel(20);
479
480 harness.simulation.enqueue(1, {
481 type: "cast-spell-entity",
482 characterId: character.character_id,
483 spellId: SpellId.manaBullet,
484 targetEntityId: deerId
485 });
486 harness.simulation.step();
487 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
488
489 harness.simulation.enqueue(1, {
490 type: "arm-spell",
491 characterId: character.character_id,
492 spellId: SpellId.manaBullet
493 });
494 harness.simulation.step();
495 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
496 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).armedSpellId, SpellId.manaBullet);
497 }
498 finally
499 {
500 Math.random = random;
501 harness.close();
502 }
503});
504
505test("auto-retaliate does not engage while auto-casting an attack spell", () =>
506{
507 const harness = makeSimulation({ workerCount: 1 });
508 const random = Math.random;
509 Math.random = () => 0.999;
510 try
511 {
512 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
513 const playerId = harness.simulation.spawnCharacter(1, character);
514 const deerId = harness.simulation.spawnEntity(mobComponents({
515 tileX: 10,
516 tileY: 8,
517 mobTypeId: MobId.deer,
518 level: 40
519 }));
520 const ratId = harness.simulation.spawnEntity(mobComponents({
521 tileX: 9,
522 tileY: 8,
523 mobTypeId: MobId.rat,
524 spawnerId: null
525 }));
526 const worker = workerOf(harness.simulation, playerId);
527 const handle = worker.world.ecs.handleOf(playerId);
528 const deerHandle = worker.world.ecs.handleOf(deerId);
529 const deerHealth = worker.world.ecs.get(deerHandle, Component.health);
530 deerHealth.max = 500_000;
531 deerHealth.current = 500_000;
532 const spells = worker.world.ecs.get(handle, Component.spells);
533 spells.known.push(SpellId.manaBullet);
534 spells.autoCast = true;
535 const skills = worker.world.ecs.get(handle, Component.skills);
536 skills.xp[SkillId.magic] = totalXpForLevel(20);
537 skills.xp[SkillId.mind] = totalXpForLevel(20);
538
539 harness.simulation.enqueue(1, {
540 type: "cast-spell-entity",
541 characterId: character.character_id,
542 spellId: SpellId.manaBullet,
543 targetEntityId: deerId
544 });
545 harness.simulation.step();
546 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
547 assert.equal(worker.world.ecs.get(handle, Component.combat)?.targetEntityId ?? null, null);
548 assert.ok(ensureMobCombat(worker.world.ecs, ratId));
549 engageTarget(worker.world.ecs, ratId, playerId, harness.now());
550
551 let ratSwung = false;
552 for (let i = 0; i < 80; i++)
553 {
554 harness.advance(50);
555 const events = harness.simulation.step().events;
556 if (events.some((event) =>
557 event.type === "combat-swing" && event.attackerId === ratId && event.targetId === playerId
558 ))
559 {
560 ratSwung = true;
561 break;
562 }
563 }
564 assert.ok(ratSwung, "expected the rat to swing at the player");
565 assert.equal(
566 worker.world.ecs.get(handle, Component.combat)?.targetEntityId ?? null,
567 null,
568 "auto-cast is the attack — no melee retaliate engage"
569 );
570 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
571 }
572 finally
573 {
574 Math.random = random;
575 harness.close();
576 }
577});
578
579test("death clears active auto-repeat but keeps auto-cast for respawn", () =>
580{
581 const harness = makeSimulation({ workerCount: 1 });
582 const random = Math.random;
583 Math.random = () => 0.999;
584 try
585 {
586 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
587 const playerId = harness.simulation.spawnCharacter(1, character);
588 const deerId = harness.simulation.spawnEntity(mobComponents({
589 tileX: 10,
590 tileY: 8,
591 mobTypeId: MobId.deer,
592 level: 40
593 }));
594 const worker = workerOf(harness.simulation, playerId);
595 const handle = worker.world.ecs.handleOf(playerId);
596 const deerHandle = worker.world.ecs.handleOf(deerId);
597 worker.world.ecs.get(deerHandle, Component.health).max = 500;
598 worker.world.ecs.get(deerHandle, Component.health).current = 500;
599 const spells = worker.world.ecs.get(handle, Component.spells);
600 spells.known.push(SpellId.manaBullet);
601 spells.autoCast = true;
602 const skills = worker.world.ecs.get(handle, Component.skills);
603 skills.xp[SkillId.magic] = totalXpForLevel(20);
604 skills.xp[SkillId.mind] = totalXpForLevel(20);
605
606 harness.simulation.enqueue(1, {
607 type: "cast-spell-entity",
608 characterId: character.character_id,
609 spellId: SpellId.manaBullet,
610 targetEntityId: deerId
611 });
612 harness.simulation.step();
613 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, SpellId.manaBullet);
614
615 const killerId = harness.simulation.spawnEntity(mobComponents({
616 tileX: 9,
617 tileY: 8,
618 mobTypeId: MobId.deer,
619 spawnerId: null
620 }));
621 worker.world.ecs.get(worker.world.ecs.handleOf(killerId), Component.health).current = 1_000_000;
622 worker.world.ecs.get(worker.world.ecs.handleOf(killerId), Component.health).max = 1_000_000;
623 worker.world.ecs.get(handle, Component.health).current = 1;
624 harness.simulation.enqueue(1, {
625 type: "attack",
626 characterId: character.character_id,
627 targetEntityId: killerId
628 });
629 let died = false;
630 for (let i = 0; i < 100; i++)
631 {
632 harness.advance(1000);
633 const events = harness.simulation.step().events;
634 if (events.some((event) => event.type === "player-died" && event.entityId === playerId))
635 {
636 died = true;
637 break;
638 }
639 }
640 assert.ok(died, "player should die from retaliation");
641 assert.equal(worker.world.ecs.get(handle, Component.spells).autoCast, true);
642 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId, null);
643 assert.equal(worker.world.ecs.get(handle, Component.spellcasting).armedSpellId, null);
644
645 harness.simulation.enqueue(1, { type: "respawn", characterId: character.character_id });
646 harness.simulation.step();
647 assert.equal(worker.world.ecs.has(handle, Component.dead), false);
648 assert.equal(worker.world.ecs.get(handle, Component.spells).autoCast, true);
649
650 const casting = worker.world.ecs.get(handle, Component.spellcasting);
651 casting.nextCastAt = 0;
652 const pos = worker.world.ecs.get(handle, Component.position);
653 pos.tileX = 8;
654 pos.tileY = 8;
655 worker.world.ecs.get(deerHandle, Component.health).current = 500;
656
657 harness.simulation.enqueue(1, {
658 type: "cast-spell-entity",
659 characterId: character.character_id,
660 spellId: SpellId.manaBullet,
661 targetEntityId: deerId
662 });
663 const castResult = harness.simulation.step();
664 assert.equal(
665 castResult.events.some((event) => event.type === "command-error"),
666 false,
667 castResult.events.find((event) => event.type === "command-error")?.message
668 ?? "cast after respawn should succeed"
669 );
670 assert.equal(
671 worker.world.ecs.get(handle, Component.spellcasting).repeatSpellId,
672 SpellId.manaBullet,
673 "auto-cast preference still arms repeat after respawn"
674 );
675 }
676 finally
677 {
678 Math.random = random;
679 harness.close();
680 }
681});
682
683test("reorder-spell-favorites swaps favourite order", () =>
684{
685 const harness = makeSimulation({ workerCount: 1 });
686 try
687 {
688 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
689 const playerId = harness.simulation.spawnCharacter(1, character);
690 const worker = workerOf(harness.simulation, playerId);
691 const handle = worker.world.ecs.handleOf(playerId);
692 const spells = worker.world.ecs.get(handle, Component.spells);
693 spells.known.push(SpellId.manaBullet);
694 spells.favorites = [SpellId.light, SpellId.manaBullet];
695 const skills = worker.world.ecs.get(handle, Component.skills);
696 skills.xp[SkillId.magic] = totalXpForLevel(20);
697
698 harness.simulation.enqueue(1, {
699 type: "reorder-spell-favorites",
700 characterId: character.character_id,
701 favoriteSpellIds: [SpellId.manaBullet, SpellId.light]
702 });
703 harness.simulation.step();
704 assert.deepEqual(
705 worker.world.ecs.get(handle, Component.spells).favorites,
706 [SpellId.manaBullet, SpellId.light]
707 );
708 }
709 finally
710 { harness.close(); }
711});
712
713test("spell hit awards mind XP but not extra magic XP", () =>
714{
715 const harness = makeSimulation({ workerCount: 1 });
716 const random = Math.random;
717 Math.random = () => 0.999;
718 try
719 {
720 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
721 const playerId = harness.simulation.spawnCharacter(1, character);
722 const deerId = harness.simulation.spawnEntity(mobComponents({
723 tileX: 10,
724 tileY: 8,
725 mobTypeId: MobId.deer,
726 level: 1
727 }));
728 const worker = workerOf(harness.simulation, playerId);
729 const handle = worker.world.ecs.handleOf(playerId);
730 const deerHandle = worker.world.ecs.handleOf(deerId);
731 worker.world.ecs.get(deerHandle, Component.health).max = 500;
732 worker.world.ecs.get(deerHandle, Component.health).current = 500;
733 const spells = worker.world.ecs.get(handle, Component.spells);
734 spells.known.push(SpellId.manaBullet);
735 const skills = worker.world.ecs.get(handle, Component.skills);
736 const magicBefore = totalXpForLevel(20);
737 const mindBefore = totalXpForLevel(20);
738 skills.xp[SkillId.magic] = magicBefore;
739 skills.xp[SkillId.mind] = mindBefore;
740 const castXp = spellCastXpFromDifficulty(spellDifficulty(spellDefinition(SpellId.manaBullet)));
741
742 harness.simulation.enqueue(1, {
743 type: "cast-spell-entity",
744 characterId: character.character_id,
745 spellId: SpellId.manaBullet,
746 targetEntityId: deerId
747 });
748 const cast = harness.simulation.step();
749 assert.ok(
750 cast.events.some((event) => event.type === "spell-projectile"),
751 JSON.stringify(cast.events.map((e) => e.type))
752 );
753 assert.equal(skills.xp[SkillId.magic], magicBefore + castXp);
754 assert.equal(skills.xp[SkillId.mind], mindBefore);
755
756 harness.advance(SPELL_PROJECTILE_MS + 50);
757 const impact = harness.simulation.step();
758 assert.ok(
759 impact.events.some((event) => event.type === "combat-hit"),
760 JSON.stringify(impact.events.map((e) => e.type))
761 );
762 assert.equal(
763 skills.xp[SkillId.magic],
764 magicBefore + castXp,
765 "hitting must not grant additional Magic XP"
766 );
767 assert.ok(
768 skills.xp[SkillId.mind] > mindBefore,
769 "hitting should grant Mind XP"
770 );
771 }
772 finally
773 {
774 Math.random = random;
775 harness.close();
776 }
777});