| 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, applyDamageAndKill } 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("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 | |
| 78 | test("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 | |
| 90 | test("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 | |
| 113 | test("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 | |
| 121 | test("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 | |
| 141 | test("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 | |
| 157 | test("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 | |
| 168 | test("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 | |
| 180 | test("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 | |
| 212 | test("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 | |
| 225 | test("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 | assert.equal(components.spawnOrigin.passive, false); |
| 241 | }); |
| 242 | |
| 243 | test("passive humanoid stores passive flag when not aggressive", () => |
| 244 | { |
| 245 | const config = normalizeSpawnerConfig({ |
| 246 | mobTypeId: MobId.humanoid, |
| 247 | aggressive: false, |
| 248 | passive: true, |
| 249 | bounty: 10 |
| 250 | }, "passive-1"); |
| 251 | assert.equal(config.aggressive, false); |
| 252 | assert.equal(config.passive, true); |
| 253 | assert.equal(config.bounty, 10); |
| 254 | |
| 255 | const cleared = normalizeSpawnerConfig({ |
| 256 | mobTypeId: MobId.humanoid, |
| 257 | aggressive: true, |
| 258 | passive: true, |
| 259 | bounty: 10 |
| 260 | }, "passive-cleared"); |
| 261 | assert.equal(cleared.aggressive, true); |
| 262 | assert.equal(cleared.passive, false); |
| 263 | assert.equal(cleared.bounty, 0); |
| 264 | |
| 265 | const components = mobComponents({ |
| 266 | tileX: 8, |
| 267 | tileY: 8, |
| 268 | mobTypeId: MobId.humanoid, |
| 269 | spawnerId: "passive-npc", |
| 270 | level: 2, |
| 271 | bounty: 5, |
| 272 | aggressive: false, |
| 273 | passive: true, |
| 274 | displayName: "Pacifist", |
| 275 | sex: "female" |
| 276 | }); |
| 277 | assert.equal(components.spawnOrigin.aggressive, false); |
| 278 | assert.equal(components.spawnOrigin.passive, true); |
| 279 | assert.equal(components.spawnOrigin.bounty, 5); |
| 280 | }); |
| 281 | |
| 282 | test("wildlife passive stores on spawn origin; aggressive wildlife clears it", () => |
| 283 | { |
| 284 | const deer = normalizeSpawnerConfig({ |
| 285 | mobTypeId: MobId.deer, |
| 286 | passive: true |
| 287 | }, "deer-passive"); |
| 288 | assert.equal(deer.passive, true); |
| 289 | |
| 290 | const rat = normalizeSpawnerConfig({ |
| 291 | mobTypeId: MobId.rat, |
| 292 | passive: true |
| 293 | }, "rat-passive"); |
| 294 | assert.equal(rat.passive, false); |
| 295 | |
| 296 | const components = mobComponents({ |
| 297 | tileX: 4, |
| 298 | tileY: 4, |
| 299 | mobTypeId: MobId.deer, |
| 300 | spawnerId: "deer-p", |
| 301 | passive: true |
| 302 | }); |
| 303 | assert.equal(components.spawnOrigin.passive, true); |
| 304 | assert.equal(components.spawnOrigin.aggressive, false); |
| 305 | }); |
| 306 | |
| 307 | test("killing a bounty mob awards player bounty and can wake town guards", () => |
| 308 | { |
| 309 | const harness = makeSimulation({ workerCount: 1 }); |
| 310 | const restore = installDamagingCombatRng(); |
| 311 | try |
| 312 | { |
| 313 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 314 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 315 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 316 | tileX: 9, |
| 317 | tileY: 8, |
| 318 | mobTypeId: MobId.rat, |
| 319 | spawnerId: "bounty-rat", |
| 320 | originTileX: 9, |
| 321 | originTileY: 8, |
| 322 | wanderRangeTiles: 4, |
| 323 | level: 1, |
| 324 | bounty: 40 |
| 325 | })); |
| 326 | const worker = harness.simulation.workers[0]; |
| 327 | const ratHealth = worker.world.ecs.get(worker.world.ecs.handleOf(ratId), Component.health); |
| 328 | ratHealth.current = 1; |
| 329 | ratHealth.max = 1; |
| 330 | |
| 331 | const guardId = harness.simulation.spawnEntity(mobComponents({ |
| 332 | tileX: 10, |
| 333 | tileY: 8, |
| 334 | mobTypeId: MobId.townGuard, |
| 335 | spawnerId: "guard-1", |
| 336 | originTileX: 8, |
| 337 | originTileY: 8, |
| 338 | wanderRangeTiles: 8, |
| 339 | level: 3, |
| 340 | displayName: "Guard", |
| 341 | sex: "male", |
| 342 | townGuard: true |
| 343 | })); |
| 344 | |
| 345 | harness.simulation.enqueue(1, { |
| 346 | type: "attack", |
| 347 | characterId: character.character_id, |
| 348 | targetEntityId: ratId |
| 349 | }); |
| 350 | let killed = false; |
| 351 | for (let i = 0; i < 80; i++) |
| 352 | { |
| 353 | harness.advance(50); |
| 354 | const step = harness.simulation.step(); |
| 355 | if (step.events.some((event) => event.type === "mob-killed" && event.entityId === ratId)) |
| 356 | { |
| 357 | killed = true; |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | assert.ok(killed, "expected rat kill"); |
| 362 | assert.equal(harness.stateDb.getCharacterBounty(character.character_id), 40); |
| 363 | |
| 364 | const guardHandle = worker.world.ecs.handleOf(guardId); |
| 365 | const guardCombat = worker.world.ecs.get(guardHandle, Component.combat); |
| 366 | assert.equal(guardCombat?.targetEntityId, playerId); |
| 367 | |
| 368 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 369 | worker.world.ecs.get(playerHandle, Component.health).current = 1; |
| 370 | applyDamageAndKill(worker.world, guardId, playerId, 999, Date.now()); |
| 371 | assert.ok(worker.world.ecs.has(playerHandle, Component.dead)); |
| 372 | assert.equal( |
| 373 | worker.world.ecs.get(guardHandle, Component.combat)?.targetEntityId ?? null, |
| 374 | null, |
| 375 | "guard must drop the bounty-killer when they die" |
| 376 | ); |
| 377 | harness.simulation.step(); |
| 378 | assert.equal( |
| 379 | worker.world.ecs.get(guardHandle, Component.combat)?.targetEntityId ?? null, |
| 380 | null, |
| 381 | "guard must not re-aggro a dead player" |
| 382 | ); |
| 383 | } |
| 384 | finally |
| 385 | { |
| 386 | restore(); |
| 387 | harness.close(); |
| 388 | } |
| 389 | }); |
| 390 | |
| 391 | test("humanoids retaliate when attacked", () => |
| 392 | { |
| 393 | const harness = makeSimulation({ workerCount: 1 }); |
| 394 | try |
| 395 | { |
| 396 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 397 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 398 | const npcId = harness.simulation.spawnEntity(mobComponents({ |
| 399 | tileX: 9, |
| 400 | tileY: 8, |
| 401 | mobTypeId: MobId.humanoid, |
| 402 | spawnerId: "shop-1", |
| 403 | level: 2, |
| 404 | displayName: "Shopkeep", |
| 405 | sex: "male", |
| 406 | equipment: { weapon: ItemId.stick } |
| 407 | })); |
| 408 | harness.simulation.enqueue(1, { |
| 409 | type: "attack", |
| 410 | characterId: character.character_id, |
| 411 | targetEntityId: npcId |
| 412 | }); |
| 413 | let retaliated = false; |
| 414 | for (let i = 0; i < 40; i++) |
| 415 | { |
| 416 | harness.advance(50); |
| 417 | harness.simulation.step(); |
| 418 | const worker = harness.simulation.workers[0]; |
| 419 | const combat = worker.world.ecs.get(worker.world.ecs.handleOf(npcId), Component.combat); |
| 420 | if (combat?.targetEntityId === playerId) |
| 421 | { |
| 422 | retaliated = true; |
| 423 | break; |
| 424 | } |
| 425 | } |
| 426 | assert.ok(retaliated, "humanoid should engage the attacker"); |
| 427 | } |
| 428 | finally |
| 429 | { harness.close(); } |
| 430 | }); |