BlueGit

timfa questward

Files

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

spawner-bounty.test.js

spawner-bounty.test.js 7.4 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, mobSpellCastChancePercent } 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("town guard spawner defaults level, bounty, and equipment", () =>
27{
28 const config = normalizeSpawnerConfig({ mobTypeId: MobId.townGuard }, "guard-1");
29 assert.equal(config.level, 30);
30 assert.equal(config.bounty, 500);
31 assert.equal(config.equipment.head, 100);
32 assert.equal(config.equipment.chest, 97);
33 assert.equal(config.equipment.legs, 92);
34 assert.equal(config.equipment.boots, 99);
35 assert.equal(config.equipment.weapon, 245);
36});
37
38test("humanoid spawners do not buy from players by default", () =>
39{
40 const config = normalizeSpawnerConfig({ mobTypeId: MobId.humanoid }, "shop-1");
41 assert.equal(config.buysItems, false);
42 const guard = normalizeSpawnerConfig({ mobTypeId: MobId.townGuard }, "guard-1");
43 assert.equal(guard.buysItems, false);
44});
45
46test("spawner config keeps bounty, level, and spells", () =>
47{
48 const config = normalizeSpawnerConfig({
49 mobTypeId: MobId.rat,
50 level: 5,
51 bounty: 25,
52 spells: [
53 { spellId: SpellId.spark, chance: 75 },
54 { spellId: SpellId.light, chance: 0 },
55 { spellId: SpellId.spark, chance: 10 }
56 ]
57 }, "s1");
58 assert.equal(config.level, 5);
59 assert.equal(config.bounty, 25);
60 assert.equal(config.spells.length, 2);
61 assert.equal(config.spells[0].spellId, SpellId.spark);
62 assert.equal(config.spells[0].chance, undefined);
63 assert.equal(config.spells[1].spellId, SpellId.light);
64});
65
66test("humanoid aggressive forces bounty to 0", () =>
67{
68 const config = normalizeSpawnerConfig({
69 mobTypeId: MobId.humanoid,
70 bounty: 50,
71 aggressive: true
72 }, "aggro-1");
73 assert.equal(config.aggressive, true);
74 assert.equal(config.bounty, 0);
75});
76
77test("town guard is a fightable humanoid body mob", () =>
78{
79 const def = mobDefinition(MobId.townGuard);
80 assert.ok(def.usePlayerBody);
81 assert.ok(def.townGuard);
82 assert.equal(mobCanFight(def), true);
83 assert.equal(mobCanFight(mobDefinition(MobId.humanoid)), true);
84 assert.equal(mobMaxHealthForLevel(def, 1), 11);
85 assert.equal(mobMaxHealthForLevel(def, 10), 20);
86 assert.equal(mobLevelDisplayName(7, "Town Guard"), "Lv.7 Town Guard");
87});
88
89test("mob components use spawner level for skills, HP, bounty origin, and spells", () =>
90{
91 const components = mobComponents({
92 tileX: 8,
93 tileY: 8,
94 mobTypeId: MobId.townGuard,
95 spawnerId: "0:0:3",
96 originTileX: 8,
97 originTileY: 8,
98 wanderRangeTiles: 6,
99 level: 4,
100 bounty: 12,
101 displayName: "Captain",
102 sex: "male",
103 spells: [
104 { spellId: SpellId.spark },
105 { spellId: SpellId.light }
106 ]
107 });
108 assert.equal(components.creature.level, 4);
109 assert.equal(components.health.max, 14);
110 assert.equal(components.health.current, 14);
111 assert.equal(components.skills.levels[SkillId.magic], 4);
112 assert.equal(components.skills.levels[SkillId.unarmed], 4);
113 assert.equal(components.spawnOrigin.bounty, 12);
114 assert.equal(components.spawnOrigin.townGuard, true);
115 assert.ok(components.spells.known.includes(SpellId.spark));
116 assert.ok(components.spells.known.includes(SpellId.light));
117 assert.equal(components.spells.castChances[SpellId.spark], mobSpellCastChancePercent(4));
118 assert.equal(components.spells.castChances[SpellId.light], mobSpellCastChancePercent(4));
119});
120
121test("mobs below spell minMagicLevel do not learn the spell", () =>
122{
123 const components = mobComponents({
124 tileX: 8,
125 tileY: 8,
126 mobTypeId: MobId.rat,
127 spawnerId: "0:0:4",
128 level: 1,
129 spells: [{ spellId: SpellId.spark }]
130 });
131 assert.equal(components.spells, undefined);
132});
133
134test("aggressive humanoid stores aggro flag and zero bounty", () =>
135{
136 const components = mobComponents({
137 tileX: 8,
138 tileY: 8,
139 mobTypeId: MobId.humanoid,
140 spawnerId: "aggro-npc",
141 level: 3,
142 bounty: 99,
143 aggressive: true,
144 displayName: "Bandit",
145 sex: "male"
146 });
147 assert.equal(components.spawnOrigin.aggressive, true);
148 assert.equal(components.spawnOrigin.bounty, 0);
149});
150
151test("killing a bounty mob awards player bounty and can wake town guards", () =>
152{
153 const harness = makeSimulation({ workerCount: 1 });
154 const random = Math.random;
155 Math.random = () => 0;
156 try
157 {
158 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
159 const playerId = harness.simulation.spawnCharacter(1, character);
160 const ratId = harness.simulation.spawnEntity(mobComponents({
161 tileX: 9,
162 tileY: 8,
163 mobTypeId: MobId.rat,
164 spawnerId: "bounty-rat",
165 originTileX: 9,
166 originTileY: 8,
167 wanderRangeTiles: 4,
168 level: 1,
169 bounty: 40
170 }));
171 const worker = harness.simulation.workers[0];
172 const ratHealth = worker.world.ecs.get(worker.world.ecs.handleOf(ratId), Component.health);
173 ratHealth.current = 1;
174 ratHealth.max = 1;
175
176 const guardId = harness.simulation.spawnEntity(mobComponents({
177 tileX: 10,
178 tileY: 8,
179 mobTypeId: MobId.townGuard,
180 spawnerId: "guard-1",
181 originTileX: 8,
182 originTileY: 8,
183 wanderRangeTiles: 8,
184 level: 3,
185 displayName: "Guard",
186 sex: "male",
187 townGuard: true
188 }));
189
190 harness.simulation.enqueue(1, {
191 type: "attack",
192 characterId: character.character_id,
193 targetEntityId: ratId
194 });
195 let killed = false;
196 for (let i = 0; i < 80; i++)
197 {
198 harness.advance(50);
199 const step = harness.simulation.step();
200 if (step.events.some((event) => event.type === "mob-killed" && event.entityId === ratId))
201 {
202 killed = true;
203 break;
204 }
205 }
206 assert.ok(killed, "expected rat kill");
207 assert.equal(harness.stateDb.getCharacterBounty(character.character_id), 40);
208
209 const guardHandle = worker.world.ecs.handleOf(guardId);
210 const guardCombat = worker.world.ecs.get(guardHandle, Component.combat);
211 assert.equal(guardCombat?.targetEntityId, playerId);
212 }
213 finally
214 {
215 Math.random = random;
216 harness.close();
217 }
218});
219
220test("humanoids retaliate when attacked", () =>
221{
222 const harness = makeSimulation({ workerCount: 1 });
223 try
224 {
225 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
226 const playerId = harness.simulation.spawnCharacter(1, character);
227 const npcId = harness.simulation.spawnEntity(mobComponents({
228 tileX: 9,
229 tileY: 8,
230 mobTypeId: MobId.humanoid,
231 spawnerId: "shop-1",
232 level: 2,
233 displayName: "Shopkeep",
234 sex: "male",
235 equipment: { weapon: ItemId.stick }
236 }));
237 harness.simulation.enqueue(1, {
238 type: "attack",
239 characterId: character.character_id,
240 targetEntityId: npcId
241 });
242 let retaliated = false;
243 for (let i = 0; i < 40; i++)
244 {
245 harness.advance(50);
246 harness.simulation.step();
247 const worker = harness.simulation.workers[0];
248 const combat = worker.world.ecs.get(worker.world.ecs.handleOf(npcId), Component.combat);
249 if (combat?.targetEntityId === playerId)
250 {
251 retaliated = true;
252 break;
253 }
254 }
255 assert.ok(retaliated, "humanoid should engage the attacker");
256 }
257 finally
258 { harness.close(); }
259});