BlueGit

timfa questward

Files

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

spawner-bounty.test.js

spawner-bounty.test.js 9.7 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import
4 {
5 MobId,
6 normalizeSpawnerConfig,
7 mobMaxHealthForLevel,
8 mobLevelDisplayName,
9 mobDefinition
10 } from "../../shared/definitions/mobs.js";
11import { mobCanFight, mobSpellCastChancePercent } from "../../shared/definitions/mobCombat.js";
12import { SpellId } from "../../shared/definitions/spells.js";
13import { SkillId } from "../../shared/definitions/skills.js";
14import { mobComponents } from "../src/simulation/spawners.js";
15import { Component } from "../src/ecs/components.js";
16import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
17import { ItemId } from "../../shared/definitions/items.js";
18import { attackProfileOf, applyDamageAndKill } from "../src/combat/service.js";
19import { installDamagingCombatRng } from "./helpers/combatRng.js";
20
21test("spawner config defaults bounty 0 and level 1", () =>
22{
23 const config = normalizeSpawnerConfig({ mobTypeId: MobId.deer }, "0:0:1");
24 assert.equal(config.bounty, 0);
25 assert.equal(config.level, 1);
26 assert.deepEqual(config.spells, []);
27});
28
29test("town guard spawner defaults level, bounty, and equipment", () =>
30{
31 const config = normalizeSpawnerConfig({ mobTypeId: MobId.townGuard }, "guard-1");
32 assert.equal(config.level, 30);
33 assert.equal(config.bounty, 500);
34 assert.equal(config.equipment.head, 100);
35 assert.equal(config.equipment.chest, 97);
36 assert.equal(config.equipment.legs, 92);
37 assert.equal(config.equipment.boots, 99);
38 assert.equal(config.equipment.weapon, 245);
39});
40
41test("goblin spawner defaults level 5 without equipment", () =>
42{
43 const config = normalizeSpawnerConfig({ mobTypeId: MobId.goblin }, "goblin-1");
44 assert.equal(config.level, 5);
45 assert.equal(config.bounty, 0);
46 assert.equal(config.equipment.weapon, null);
47});
48
49test("shade spawner defaults level 28", () =>
50{
51 const config = normalizeSpawnerConfig({ mobTypeId: MobId.shade }, "shade-1");
52 assert.equal(config.level, 28);
53});
54
55test("goblin spawns with slashing melee profile from mob definition", () =>
56{
57 const harness = makeSimulation({ workerCount: 1 });
58 try
59 {
60 const config = normalizeSpawnerConfig({ mobTypeId: MobId.goblin }, "goblin-1");
61 const goblinId = harness.simulation.spawnEntity(mobComponents({
62 mobTypeId: MobId.goblin,
63 tileX: 8,
64 tileY: 8,
65 level: config.level
66 }));
67 const worker = harness.simulation.workers[0];
68 const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(goblinId));
69 assert.equal(profile.mode, "melee");
70 assert.equal(profile.attackType, "slashing");
71 assert.equal(profile.hitSkillId, SkillId.swordfighting);
72 assert.equal(profile.mobAttackSkill, 5);
73 }
74 finally
75 { harness.close(); }
76});
77
78test("humanoid spawners do not buy from players by default", () =>
79{
80 const config = normalizeSpawnerConfig({ mobTypeId: MobId.humanoid }, "shop-1");
81 assert.equal(config.buysItems, false);
82 const guard = normalizeSpawnerConfig({ mobTypeId: MobId.townGuard }, "guard-1");
83 assert.equal(guard.buysItems, false);
84});
85
86test("spawner config keeps bounty, level, and spells", () =>
87{
88 const config = normalizeSpawnerConfig({
89 mobTypeId: MobId.rat,
90 level: 5,
91 bounty: 25,
92 spells: [
93 { spellId: SpellId.firebolt, chance: 75 },
94 { spellId: SpellId.light, chance: 0 },
95 { spellId: SpellId.firebolt, chance: 10 }
96 ]
97 }, "s1");
98 assert.equal(config.level, 5);
99 assert.equal(config.bounty, 25);
100 assert.equal(config.spells.length, 2);
101 assert.equal(config.spells[0].spellId, SpellId.firebolt);
102 assert.equal(config.spells[0].chance, undefined);
103 assert.equal(config.spells[1].spellId, SpellId.light);
104});
105
106test("spawner config drops retired spark spell without failing", () =>
107{
108 const config = normalizeSpawnerConfig({
109 mobTypeId: MobId.humanoid,
110 spells: [
111 { spellId: "spark" },
112 { spellId: SpellId.light },
113 { spellId: SpellId.firebolt }
114 ]
115 }, "legacy-spark");
116 assert.deepEqual(
117 config.spells.map((row) => row.spellId),
118 [SpellId.light, SpellId.firebolt]
119 );
120});
121
122test("humanoid aggressive forces bounty to 0", () =>
123{
124 const config = normalizeSpawnerConfig({
125 mobTypeId: MobId.humanoid,
126 bounty: 50,
127 aggressive: true
128 }, "aggro-1");
129 assert.equal(config.aggressive, true);
130 assert.equal(config.bounty, 0);
131});
132
133test("town guard is a fightable humanoid body mob", () =>
134{
135 const def = mobDefinition(MobId.townGuard);
136 assert.ok(def.usePlayerBody);
137 assert.ok(def.townGuard);
138 assert.equal(mobCanFight(def), true);
139 assert.equal(mobCanFight(mobDefinition(MobId.humanoid)), true);
140 assert.equal(mobMaxHealthForLevel(def, 1), 11);
141 assert.equal(mobMaxHealthForLevel(def, 10), 20);
142 assert.equal(mobLevelDisplayName(7, "Town Guard"), "Lv.7 Town Guard");
143});
144
145test("mob components use spawner level for skills, HP, bounty origin, and spells", () =>
146{
147 const components = mobComponents({
148 tileX: 8,
149 tileY: 8,
150 mobTypeId: MobId.townGuard,
151 spawnerId: "0:0:3",
152 originTileX: 8,
153 originTileY: 8,
154 wanderRangeTiles: 6,
155 level: 20,
156 bounty: 12,
157 displayName: "Captain",
158 sex: "male",
159 spells: [
160 { spellId: SpellId.firebolt },
161 { spellId: SpellId.light }
162 ]
163 });
164 assert.equal(components.creature.level, 20);
165 assert.equal(components.health.max, 30);
166 assert.equal(components.health.current, 30);
167 assert.equal(components.skills.levels[SkillId.magic], 20);
168 assert.equal(components.skills.levels[SkillId.unarmed], 20);
169 assert.equal(components.spawnOrigin.bounty, 12);
170 assert.equal(components.spawnOrigin.townGuard, true);
171 assert.ok(components.spells.known.includes(SpellId.firebolt));
172 assert.ok(components.spells.known.includes(SpellId.light));
173 assert.equal(components.spells.castChances[SpellId.firebolt], mobSpellCastChancePercent(20));
174 assert.equal(components.spells.castChances[SpellId.light], mobSpellCastChancePercent(20));
175});
176
177test("mobs below spell minMagicLevel do not learn the spell", () =>
178{
179 const components = mobComponents({
180 tileX: 8,
181 tileY: 8,
182 mobTypeId: MobId.rat,
183 spawnerId: "0:0:4",
184 level: 1,
185 spells: [{ spellId: SpellId.firebolt }]
186 });
187 assert.equal(components.spells, undefined);
188});
189
190test("aggressive humanoid stores aggro flag and zero bounty", () =>
191{
192 const components = mobComponents({
193 tileX: 8,
194 tileY: 8,
195 mobTypeId: MobId.humanoid,
196 spawnerId: "aggro-npc",
197 level: 3,
198 bounty: 99,
199 aggressive: true,
200 displayName: "Bandit",
201 sex: "male"
202 });
203 assert.equal(components.spawnOrigin.aggressive, true);
204 assert.equal(components.spawnOrigin.bounty, 0);
205});
206
207test("killing a bounty mob awards player bounty and can wake town guards", () =>
208{
209 const harness = makeSimulation({ workerCount: 1 });
210 const restore = installDamagingCombatRng();
211 try
212 {
213 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
214 const playerId = harness.simulation.spawnCharacter(1, character);
215 const ratId = harness.simulation.spawnEntity(mobComponents({
216 tileX: 9,
217 tileY: 8,
218 mobTypeId: MobId.rat,
219 spawnerId: "bounty-rat",
220 originTileX: 9,
221 originTileY: 8,
222 wanderRangeTiles: 4,
223 level: 1,
224 bounty: 40
225 }));
226 const worker = harness.simulation.workers[0];
227 const ratHealth = worker.world.ecs.get(worker.world.ecs.handleOf(ratId), Component.health);
228 ratHealth.current = 1;
229 ratHealth.max = 1;
230
231 const guardId = harness.simulation.spawnEntity(mobComponents({
232 tileX: 10,
233 tileY: 8,
234 mobTypeId: MobId.townGuard,
235 spawnerId: "guard-1",
236 originTileX: 8,
237 originTileY: 8,
238 wanderRangeTiles: 8,
239 level: 3,
240 displayName: "Guard",
241 sex: "male",
242 townGuard: true
243 }));
244
245 harness.simulation.enqueue(1, {
246 type: "attack",
247 characterId: character.character_id,
248 targetEntityId: ratId
249 });
250 let killed = false;
251 for (let i = 0; i < 80; i++)
252 {
253 harness.advance(50);
254 const step = harness.simulation.step();
255 if (step.events.some((event) => event.type === "mob-killed" && event.entityId === ratId))
256 {
257 killed = true;
258 break;
259 }
260 }
261 assert.ok(killed, "expected rat kill");
262 assert.equal(harness.stateDb.getCharacterBounty(character.character_id), 40);
263
264 const guardHandle = worker.world.ecs.handleOf(guardId);
265 const guardCombat = worker.world.ecs.get(guardHandle, Component.combat);
266 assert.equal(guardCombat?.targetEntityId, playerId);
267
268 const playerHandle = worker.world.ecs.handleOf(playerId);
269 worker.world.ecs.get(playerHandle, Component.health).current = 1;
270 applyDamageAndKill(worker.world, guardId, playerId, 999, Date.now());
271 assert.ok(worker.world.ecs.has(playerHandle, Component.dead));
272 assert.equal(
273 worker.world.ecs.get(guardHandle, Component.combat)?.targetEntityId ?? null,
274 null,
275 "guard must drop the bounty-killer when they die"
276 );
277 harness.simulation.step();
278 assert.equal(
279 worker.world.ecs.get(guardHandle, Component.combat)?.targetEntityId ?? null,
280 null,
281 "guard must not re-aggro a dead player"
282 );
283 }
284 finally
285 {
286 restore();
287 harness.close();
288 }
289});
290
291test("humanoids retaliate when attacked", () =>
292{
293 const harness = makeSimulation({ workerCount: 1 });
294 try
295 {
296 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
297 const playerId = harness.simulation.spawnCharacter(1, character);
298 const npcId = harness.simulation.spawnEntity(mobComponents({
299 tileX: 9,
300 tileY: 8,
301 mobTypeId: MobId.humanoid,
302 spawnerId: "shop-1",
303 level: 2,
304 displayName: "Shopkeep",
305 sex: "male",
306 equipment: { weapon: ItemId.stick }
307 }));
308 harness.simulation.enqueue(1, {
309 type: "attack",
310 characterId: character.character_id,
311 targetEntityId: npcId
312 });
313 let retaliated = false;
314 for (let i = 0; i < 40; i++)
315 {
316 harness.advance(50);
317 harness.simulation.step();
318 const worker = harness.simulation.workers[0];
319 const combat = worker.world.ecs.get(worker.world.ecs.handleOf(npcId), Component.combat);
320 if (combat?.targetEntityId === playerId)
321 {
322 retaliated = true;
323 break;
324 }
325 }
326 assert.ok(retaliated, "humanoid should engage the attacker");
327 }
328 finally
329 { harness.close(); }
330});