BlueGit

timfa questward

Files

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

spawner-bounty.test.js

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