import test from "node:test"; import assert from "node:assert/strict"; import { MobId, normalizeSpawnerConfig, mobMaxHealthForLevel, mobLevelDisplayName, mobDefinition } from "../../shared/definitions/mobs.js"; import { mobCanFight, mobSpellCastChancePercent } from "../../shared/definitions/mobCombat.js"; import { SpellId } from "../../shared/definitions/spells.js"; import { SkillId } from "../../shared/definitions/skills.js"; import { mobComponents } from "../src/simulation/spawners.js"; import { Component } from "../src/ecs/components.js"; import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; import { ItemId } from "../../shared/definitions/items.js"; test("spawner config defaults bounty 0 and level 1", () => { const config = normalizeSpawnerConfig({ mobTypeId: MobId.deer }, "0:0:1"); assert.equal(config.bounty, 0); assert.equal(config.level, 1); assert.deepEqual(config.spells, []); }); test("town guard spawner defaults level, bounty, and equipment", () => { const config = normalizeSpawnerConfig({ mobTypeId: MobId.townGuard }, "guard-1"); assert.equal(config.level, 30); assert.equal(config.bounty, 500); assert.equal(config.equipment.head, 100); assert.equal(config.equipment.chest, 97); assert.equal(config.equipment.legs, 92); assert.equal(config.equipment.boots, 99); assert.equal(config.equipment.weapon, 245); }); test("humanoid spawners do not buy from players by default", () => { const config = normalizeSpawnerConfig({ mobTypeId: MobId.humanoid }, "shop-1"); assert.equal(config.buysItems, false); const guard = normalizeSpawnerConfig({ mobTypeId: MobId.townGuard }, "guard-1"); assert.equal(guard.buysItems, false); }); test("spawner config keeps bounty, level, and spells", () => { const config = normalizeSpawnerConfig({ mobTypeId: MobId.rat, level: 5, bounty: 25, spells: [ { spellId: SpellId.firebolt, chance: 75 }, { spellId: SpellId.light, chance: 0 }, { spellId: SpellId.firebolt, chance: 10 } ] }, "s1"); assert.equal(config.level, 5); assert.equal(config.bounty, 25); assert.equal(config.spells.length, 2); assert.equal(config.spells[0].spellId, SpellId.firebolt); assert.equal(config.spells[0].chance, undefined); assert.equal(config.spells[1].spellId, SpellId.light); }); test("spawner config drops retired spark spell without failing", () => { const config = normalizeSpawnerConfig({ mobTypeId: MobId.humanoid, spells: [ { spellId: "spark" }, { spellId: SpellId.light }, { spellId: SpellId.firebolt } ] }, "legacy-spark"); assert.deepEqual( config.spells.map((row) => row.spellId), [SpellId.light, SpellId.firebolt] ); }); test("humanoid aggressive forces bounty to 0", () => { const config = normalizeSpawnerConfig({ mobTypeId: MobId.humanoid, bounty: 50, aggressive: true }, "aggro-1"); assert.equal(config.aggressive, true); assert.equal(config.bounty, 0); }); test("town guard is a fightable humanoid body mob", () => { const def = mobDefinition(MobId.townGuard); assert.ok(def.usePlayerBody); assert.ok(def.townGuard); assert.equal(mobCanFight(def), true); assert.equal(mobCanFight(mobDefinition(MobId.humanoid)), true); assert.equal(mobMaxHealthForLevel(def, 1), 11); assert.equal(mobMaxHealthForLevel(def, 10), 20); assert.equal(mobLevelDisplayName(7, "Town Guard"), "Lv.7 Town Guard"); }); test("mob components use spawner level for skills, HP, bounty origin, and spells", () => { const components = mobComponents({ tileX: 8, tileY: 8, mobTypeId: MobId.townGuard, spawnerId: "0:0:3", originTileX: 8, originTileY: 8, wanderRangeTiles: 6, level: 20, bounty: 12, displayName: "Captain", sex: "male", spells: [ { spellId: SpellId.firebolt }, { spellId: SpellId.light } ] }); assert.equal(components.creature.level, 20); assert.equal(components.health.max, 30); assert.equal(components.health.current, 30); assert.equal(components.skills.levels[SkillId.magic], 20); assert.equal(components.skills.levels[SkillId.unarmed], 20); assert.equal(components.spawnOrigin.bounty, 12); assert.equal(components.spawnOrigin.townGuard, true); assert.ok(components.spells.known.includes(SpellId.firebolt)); assert.ok(components.spells.known.includes(SpellId.light)); assert.equal(components.spells.castChances[SpellId.firebolt], mobSpellCastChancePercent(20)); assert.equal(components.spells.castChances[SpellId.light], mobSpellCastChancePercent(20)); }); test("mobs below spell minMagicLevel do not learn the spell", () => { const components = mobComponents({ tileX: 8, tileY: 8, mobTypeId: MobId.rat, spawnerId: "0:0:4", level: 1, spells: [{ spellId: SpellId.firebolt }] }); assert.equal(components.spells, undefined); }); test("aggressive humanoid stores aggro flag and zero bounty", () => { const components = mobComponents({ tileX: 8, tileY: 8, mobTypeId: MobId.humanoid, spawnerId: "aggro-npc", level: 3, bounty: 99, aggressive: true, displayName: "Bandit", sex: "male" }); assert.equal(components.spawnOrigin.aggressive, true); assert.equal(components.spawnOrigin.bounty, 0); }); test("killing a bounty mob awards player bounty and can wake town guards", () => { const harness = makeSimulation({ workerCount: 1 }); const random = Math.random; Math.random = () => 0; try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const ratId = harness.simulation.spawnEntity(mobComponents({ tileX: 9, tileY: 8, mobTypeId: MobId.rat, spawnerId: "bounty-rat", originTileX: 9, originTileY: 8, wanderRangeTiles: 4, level: 1, bounty: 40 })); const worker = harness.simulation.workers[0]; const ratHealth = worker.world.ecs.get(worker.world.ecs.handleOf(ratId), Component.health); ratHealth.current = 1; ratHealth.max = 1; const guardId = harness.simulation.spawnEntity(mobComponents({ tileX: 10, tileY: 8, mobTypeId: MobId.townGuard, spawnerId: "guard-1", originTileX: 8, originTileY: 8, wanderRangeTiles: 8, level: 3, displayName: "Guard", sex: "male", townGuard: true })); harness.simulation.enqueue(1, { type: "attack", characterId: character.character_id, targetEntityId: ratId }); let killed = false; for (let i = 0; i < 80; i++) { harness.advance(50); const step = harness.simulation.step(); if (step.events.some((event) => event.type === "mob-killed" && event.entityId === ratId)) { killed = true; break; } } assert.ok(killed, "expected rat kill"); assert.equal(harness.stateDb.getCharacterBounty(character.character_id), 40); const guardHandle = worker.world.ecs.handleOf(guardId); const guardCombat = worker.world.ecs.get(guardHandle, Component.combat); assert.equal(guardCombat?.targetEntityId, playerId); } finally { Math.random = random; harness.close(); } }); test("humanoids retaliate when attacked", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const npcId = harness.simulation.spawnEntity(mobComponents({ tileX: 9, tileY: 8, mobTypeId: MobId.humanoid, spawnerId: "shop-1", level: 2, displayName: "Shopkeep", sex: "male", equipment: { weapon: ItemId.stick } })); harness.simulation.enqueue(1, { type: "attack", characterId: character.character_id, targetEntityId: npcId }); let retaliated = false; for (let i = 0; i < 40; i++) { harness.advance(50); harness.simulation.step(); const worker = harness.simulation.workers[0]; const combat = worker.world.ecs.get(worker.world.ecs.handleOf(npcId), Component.combat); if (combat?.targetEntityId === playerId) { retaliated = true; break; } } assert.ok(retaliated, "humanoid should engage the attacker"); } finally { harness.close(); } });