BlueGit

timfa questward

Files

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

spawner-bounty.test.js

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