BlueGit

timfa questward

Files

questward / server / tests / spawner-bounty.test.js

spawner-bounty.test.js

spawner-bounty.test.js 6.0 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import {
4 MobId,
5 normalizeSpawnerConfig,
6 mobMaxHealthForLevel,
7 mobLevelDisplayName,
8 mobDefinition
9} from "../../shared/definitions/mobs.js";
10import { mobCanFight } from "../../shared/definitions/mobCombat.js";
11import { SpellId } from "../../shared/definitions/spells.js";
12import { SkillId } from "../../shared/definitions/skills.js";
13import { mobComponents } from "../src/simulation/spawners.js";
14import { Component } from "../src/ecs/components.js";
15import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
16import { ItemId } from "../../shared/definitions/items.js";
17
18test("spawner config defaults bounty 0 and level 1", () =>
19{
20 const config = normalizeSpawnerConfig({ mobTypeId: MobId.deer }, "0:0:1");
21 assert.equal(config.bounty, 0);
22 assert.equal(config.level, 1);
23 assert.deepEqual(config.spells, []);
24});
25
26test("spawner config keeps bounty, level, and spells", () =>
27{
28 const config = normalizeSpawnerConfig({
29 mobTypeId: MobId.rat,
30 level: 5,
31 bounty: 25,
32 spells: [
33 { spellId: SpellId.spark, chance: 75 },
34 { spellId: SpellId.light, chance: 0 },
35 { spellId: SpellId.spark, chance: 10 }
36 ]
37 }, "s1");
38 assert.equal(config.level, 5);
39 assert.equal(config.bounty, 25);
40 assert.equal(config.spells.length, 2);
41 assert.equal(config.spells[0].spellId, SpellId.spark);
42 assert.equal(config.spells[0].chance, 75);
43 assert.equal(config.spells[1].chance, 0);
44});
45
46test("town guard is a fightable humanoid body mob", () =>
47{
48 const def = mobDefinition(MobId.townGuard);
49 assert.ok(def.usePlayerBody);
50 assert.ok(def.townGuard);
51 assert.equal(mobCanFight(def), true);
52 assert.equal(mobCanFight(mobDefinition(MobId.humanoid)), true);
53 assert.equal(mobMaxHealthForLevel(def, 1), 11);
54 assert.equal(mobMaxHealthForLevel(def, 10), 20);
55 assert.equal(mobLevelDisplayName(7, "Town Guard"), "Lv.7 Town Guard");
56});
57
58test("mob components use spawner level for skills, HP, bounty origin, and spells", () =>
59{
60 const components = mobComponents({
61 tileX: 8,
62 tileY: 8,
63 mobTypeId: MobId.townGuard,
64 spawnerId: "0:0:3",
65 originTileX: 8,
66 originTileY: 8,
67 wanderRangeTiles: 6,
68 level: 4,
69 bounty: 12,
70 displayName: "Captain",
71 sex: "male",
72 spells: [
73 { spellId: SpellId.spark, chance: 50 },
74 { spellId: SpellId.light, chance: 100 }
75 ]
76 });
77 assert.equal(components.creature.level, 4);
78 assert.equal(components.health.max, 14);
79 assert.equal(components.health.current, 14);
80 assert.equal(components.skills.levels[SkillId.magic], 4);
81 assert.equal(components.skills.levels[SkillId.unarmed], 4);
82 assert.equal(components.spawnOrigin.bounty, 12);
83 assert.equal(components.spawnOrigin.townGuard, true);
84 assert.ok(components.spells.known.includes(SpellId.spark));
85 assert.ok(components.spells.known.includes(SpellId.light));
86 assert.equal(components.spells.castChances[SpellId.spark], 50);
87});
88
89test("mobs below spell minMagicLevel do not learn the spell", () =>
90{
91 const components = mobComponents({
92 tileX: 8,
93 tileY: 8,
94 mobTypeId: MobId.rat,
95 spawnerId: "0:0:4",
96 level: 1,
97 spells: [{ spellId: SpellId.spark, chance: 100 }]
98 });
99 assert.equal(components.spells, undefined);
100});
101
102test("killing a bounty mob awards player bounty and can wake town guards", () =>
103{
104 const harness = makeSimulation({ workerCount: 1 });
105 const random = Math.random;
106 Math.random = () => 0;
107 try
108 {
109 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
110 const playerId = harness.simulation.spawnCharacter(1, character);
111 const ratId = harness.simulation.spawnEntity(mobComponents({
112 tileX: 9,
113 tileY: 8,
114 mobTypeId: MobId.rat,
115 spawnerId: "bounty-rat",
116 originTileX: 9,
117 originTileY: 8,
118 wanderRangeTiles: 4,
119 level: 1,
120 bounty: 40
121 }));
122 const worker = harness.simulation.workers[0];
123 const ratHealth = worker.world.ecs.get(worker.world.ecs.handleOf(ratId), Component.health);
124 ratHealth.current = 1;
125 ratHealth.max = 1;
126
127 const guardId = harness.simulation.spawnEntity(mobComponents({
128 tileX: 10,
129 tileY: 8,
130 mobTypeId: MobId.townGuard,
131 spawnerId: "guard-1",
132 originTileX: 8,
133 originTileY: 8,
134 wanderRangeTiles: 8,
135 level: 3,
136 displayName: "Guard",
137 sex: "male",
138 townGuard: true
139 }));
140
141 harness.simulation.enqueue(1, {
142 type: "attack",
143 characterId: character.character_id,
144 targetEntityId: ratId
145 });
146 let killed = false;
147 for (let i = 0; i < 80; i++)
148 {
149 harness.advance(50);
150 const step = harness.simulation.step();
151 if (step.events.some((event) => event.type === "mob-killed" && event.entityId === ratId))
152 {
153 killed = true;
154 break;
155 }
156 }
157 assert.ok(killed, "expected rat kill");
158 assert.equal(harness.stateDb.getCharacterBounty(character.character_id), 40);
159
160 const guardHandle = worker.world.ecs.handleOf(guardId);
161 const guardCombat = worker.world.ecs.get(guardHandle, Component.combat);
162 assert.equal(guardCombat?.targetEntityId, playerId);
163 }
164 finally
165 {
166 Math.random = random;
167 harness.close();
168 }
169});
170
171test("humanoids retaliate when attacked", () =>
172{
173 const harness = makeSimulation({ workerCount: 1 });
174 try
175 {
176 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
177 const playerId = harness.simulation.spawnCharacter(1, character);
178 const npcId = harness.simulation.spawnEntity(mobComponents({
179 tileX: 9,
180 tileY: 8,
181 mobTypeId: MobId.humanoid,
182 spawnerId: "shop-1",
183 level: 2,
184 displayName: "Shopkeep",
185 sex: "male",
186 equipment: { weapon: ItemId.stick }
187 }));
188 harness.simulation.enqueue(1, {
189 type: "attack",
190 characterId: character.character_id,
191 targetEntityId: npcId
192 });
193 let retaliated = false;
194 for (let i = 0; i < 40; i++)
195 {
196 harness.advance(50);
197 harness.simulation.step();
198 const worker = harness.simulation.workers[0];
199 const combat = worker.world.ecs.get(worker.world.ecs.handleOf(npcId), Component.combat);
200 if (combat?.targetEntityId === playerId)
201 {
202 retaliated = true;
203 break;
204 }
205 }
206 assert.ok(retaliated, "humanoid should engage the attacker");
207 }
208 finally
209 { harness.close(); }
210});