BlueGit

timfa questward

Files

questward / server / tests / spells.test.js

spells.test.js

spells.test.js 3.2 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { SpellId } from "../../shared/definitions/spells.js";
4import { SPELL_PROJECTILE_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";
8
9test("cast light resolves without command error and can place a spell light", () =>
10{
11 const harness = makeSimulation({ workerCount: 1 });
12 try
13 {
14 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
15 harness.simulation.spawnCharacter(1, character);
16
17 harness.simulation.enqueue(1, {
18 type: "cast-spell-tile",
19 characterId: character.character_id,
20 spellId: SpellId.light,
21 tileX: 10,
22 tileY: 8
23 });
24 const cast = harness.simulation.step();
25 assert.equal(
26 cast.events.some((event) => event.type === "command-error"),
27 false,
28 JSON.stringify(cast.events)
29 );
30 assert.ok(
31 cast.events.some((event) => event.type === "spell-fizzle" || event.type === "spell-projectile"),
32 "expected fizzle or projectile"
33 );
34
35 if (!cast.events.some((event) => event.type === "spell-projectile"))
36 { return; }
37
38 harness.advance(SPELL_PROJECTILE_MS + 50);
39 const impact = harness.simulation.step();
40 assert.ok(
41 impact.events.some((event) => event.type === "spell-light-appear"),
42 JSON.stringify(impact.events.map((event) => event.type))
43 );
44 }
45 finally
46 { harness.close(); }
47});
48
49test("out-of-range spell cast paths toward the nearest in-range tile", () =>
50{
51 const harness = makeSimulation({ workerCount: 1 });
52 try
53 {
54 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
55 const playerId = harness.simulation.spawnCharacter(1, character);
56 harness.simulation.enqueue(1, {
57 type: "cast-spell-tile",
58 characterId: character.character_id,
59 spellId: SpellId.light,
60 tileX: 30,
61 tileY: 8
62 });
63 const cast = harness.simulation.step();
64 assert.equal(cast.events.some((event) => event.type === "command-error"), false);
65 const start = harness.simulation.entityRecord(playerId);
66 for (let i = 0; i < 80; i++)
67 { harness.simulation.step(); }
68 const moved = harness.simulation.entityRecord(playerId);
69 assert.ok(
70 moved.tileX !== start.tileX || moved.tileY !== start.tileY,
71 `expected movement from (${start.tileX},${start.tileY}), got (${moved.tileX},${moved.tileY})`
72 );
73 }
74 finally
75 { harness.close(); }
76});
77
78test("attack from out of range paths toward the target", () =>
79{
80 const harness = makeSimulation({ workerCount: 1 });
81 try
82 {
83 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
84 const playerId = harness.simulation.spawnCharacter(1, character);
85 const deerId = harness.simulation.spawnEntity(mobComponents({
86 tileX: 24,
87 tileY: 8,
88 mobTypeId: MobId.deer
89 }));
90 harness.simulation.enqueue(1, {
91 type: "attack",
92 characterId: character.character_id,
93 targetEntityId: deerId
94 });
95 harness.simulation.step();
96 const record = harness.simulation.entityRecord(playerId);
97 assert.ok(record.tileX < 24);
98 for (let i = 0; i < 80 && record.tileX < 20; i++)
99 { harness.simulation.step(); }
100 assert.ok(harness.simulation.entityRecord(playerId).tileX > 8);
101 }
102 finally
103 { harness.close(); }
104});