BlueGit

timfa questward

Files

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

spawner-bounty.test.js

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