| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import |
| 4 | { |
| 5 | MobId, |
| 6 | normalizeSpawnerConfig, |
| 7 | mobMaxHealthForLevel, |
| 8 | mobLevelDisplayName, |
| 9 | mobDefinition |
| 10 | } from "../../shared/definitions/mobs.js"; |
| 11 | import { mobCanFight, mobSpellCastChancePercent } from "../../shared/definitions/mobCombat.js"; |
| 12 | import { SpellId } from "../../shared/definitions/spells.js"; |
| 13 | import { SkillId } from "../../shared/definitions/skills.js"; |
| 14 | import { mobComponents } from "../src/simulation/spawners.js"; |
| 15 | import { Component } from "../src/ecs/components.js"; |
| 16 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 17 | import { ItemId } from "../../shared/definitions/items.js"; |
| 18 | import { attackProfileOf } from "../src/combat/service.js"; |
| 19 | import { installDamagingCombatRng } from "./helpers/combatRng.js"; |
| 20 | |
| 21 | test("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 | |
| 29 | test("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 | |
| 41 | test("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 | |
| 49 | test("shade spawner defaults level 28", () => |
| 50 | { |
| 51 | const config = normalizeSpawnerConfig({ mobTypeId: MobId.shade }, "shade-1"); |
| 52 | assert.equal(config.level, 28); |
| 53 | }); |
| 54 | |
| 55 | test("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 | |
| 78 | test("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 | |
| 86 | test("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 | |
| 106 | test("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 | |
| 122 | test("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 | |
| 133 | test("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 | |
| 145 | test("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 | |
| 177 | test("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 | |
| 190 | test("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 | |
| 207 | test("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 | finally |
| 269 | { |
| 270 | restore(); |
| 271 | harness.close(); |
| 272 | } |
| 273 | }); |
| 274 | |
| 275 | test("humanoids retaliate when attacked", () => |
| 276 | { |
| 277 | const harness = makeSimulation({ workerCount: 1 }); |
| 278 | try |
| 279 | { |
| 280 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 281 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 282 | const npcId = harness.simulation.spawnEntity(mobComponents({ |
| 283 | tileX: 9, |
| 284 | tileY: 8, |
| 285 | mobTypeId: MobId.humanoid, |
| 286 | spawnerId: "shop-1", |
| 287 | level: 2, |
| 288 | displayName: "Shopkeep", |
| 289 | sex: "male", |
| 290 | equipment: { weapon: ItemId.stick } |
| 291 | })); |
| 292 | harness.simulation.enqueue(1, { |
| 293 | type: "attack", |
| 294 | characterId: character.character_id, |
| 295 | targetEntityId: npcId |
| 296 | }); |
| 297 | let retaliated = false; |
| 298 | for (let i = 0; i < 40; i++) |
| 299 | { |
| 300 | harness.advance(50); |
| 301 | harness.simulation.step(); |
| 302 | const worker = harness.simulation.workers[0]; |
| 303 | const combat = worker.world.ecs.get(worker.world.ecs.handleOf(npcId), Component.combat); |
| 304 | if (combat?.targetEntityId === playerId) |
| 305 | { |
| 306 | retaliated = true; |
| 307 | break; |
| 308 | } |
| 309 | } |
| 310 | assert.ok(retaliated, "humanoid should engage the attacker"); |
| 311 | } |
| 312 | finally |
| 313 | { harness.close(); } |
| 314 | }); |