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