import test from "node:test"; import assert from "node:assert/strict"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import { MobId, mobDefinition, mobSpriteName } from "../../shared/definitions/mobs.js"; import { mobIsAggressive } from "../../shared/definitions/mobCombat.js"; import { EssenceItemId } from "../../shared/definitions/essenceItems.js"; import { MagicElement } from "../../shared/definitions/attackTypes.js"; import { buildSpriteCatalog } from "../src/assets/spriteCatalog.js"; import { mobComponents } from "../src/simulation/spawners.js"; import { shadeSummonComponents, stepSummons } from "../src/spells/summons.js"; import { applyDamageAndKill, stepAggression } from "../src/combat/service.js"; import { Component } from "../src/ecs/components.js"; import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; const ASSET_ROOT = join(dirname(fileURLToPath(import.meta.url)), "../../client/assets"); function workerOf (simulation, entityId) { const record = simulation.entityRecord(entityId); assert.ok(record, "entity must be remembered"); return simulation.workers[record.workerId]; } test("shade uses own static body and animated bloom sprites", () => { const shade = mobDefinition(MobId.shade); assert.equal(shade.spriteKey, undefined); assert.equal(mobSpriteName(MobId.shade), "mob:shade:shade"); assert.equal(shade.bloomBody, true); assert.equal(shade.frames, 1); assert.equal(shade.bloomFrames, 2); assert.equal(shade.bloomAnimationSpeed, 0.5); assert.equal(shade.aggressive, true); assert.equal(mobIsAggressive(shade), true); assert.equal(shade.drops.length, 1); assert.equal(shade.drops[0].itemId, EssenceItemId[MagicElement.dark]); assert.equal(shade.drops[0].chance, 1); const catalog = buildSpriteCatalog(ASSET_ROOT); const body = catalog.images.find((entry) => entry.name === "mob:shade:shade"); const bloom = catalog.images.find((entry) => entry.name === "mob:shade:shade:bloom"); assert.ok(body, "shade body catalog entry"); assert.ok(bloom, "shade bloom catalog entry"); assert.equal(body.subImgTotal, 1); assert.equal(body.animationSpeed, 0); assert.equal(bloom.subImgTotal, 2); assert.equal(bloom.perRow, 2); assert.equal(bloom.animationSpeed, 0.5); const components = mobComponents({ tileX: 8, tileY: 8, mobTypeId: MobId.shade, spawnerId: "0:0:1", originTileX: 8, originTileY: 8 }); assert.equal(components.appearance.bloomBody, true); assert.equal(components.appearance.tint, undefined); assert.equal(components.appearance.dualTint, undefined); assert.equal(components.spawnOrigin.aggressive, true); assert.equal(components.summon, undefined); }); test("summoned shade uses companion targeting, not player aggro", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const components = shadeSummonComponents({ tileX: 10, tileY: 8, level: 10, ownerEntityId: playerId, expiresAt: Date.now() + 60_000 }); assert.ok(components.summon); assert.equal(components.spawnOrigin, undefined); assert.equal(components.appearance.bloomBody, true); const shadeId = harness.simulation.spawnEntity(components); const worker = workerOf(harness.simulation, shadeId); const world = worker.world; const shadeHandle = world.ecs.handleOf(shadeId); stepAggression(world, Date.now()); assert.equal( world.ecs.get(shadeHandle, Component.combat)?.targetEntityId ?? null, null, "summoned shade must not auto-aggro the owner/player" ); const ratId = harness.simulation.spawnEntity(mobComponents({ tileX: 12, tileY: 8, mobTypeId: MobId.rat, spawnerId: "0:0:2", originTileX: 12, originTileY: 8, aggressive: true })); stepSummons(world, Date.now()); assert.equal( world.ecs.get(shadeHandle, Component.combat)?.targetEntityId, ratId, "summoned shade should pick nearby aggressive hostiles" ); } finally { harness.close(); } }); test("wild shade drops dark essence; summoned shade does not", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const wildId = harness.simulation.spawnEntity(mobComponents({ tileX: 10, tileY: 8, mobTypeId: MobId.shade, spawnerId: "0:0:shade", originTileX: 10, originTileY: 8, level: 1 })); const worker = workerOf(harness.simulation, wildId); const world = worker.world; const wildHandle = world.ecs.handleOf(wildId); const wildHp = world.ecs.get(wildHandle, Component.health)?.max ?? 10; const wildEvents = applyDamageAndKill(world, playerId, wildId, wildHp, Date.now()); const wildDrop = wildEvents.find((event) => event.type === "ground-item-appear" && event.stack?.itemId === EssenceItemId[MagicElement.dark] ); assert.ok(wildDrop, "spawner shade must drop dark essence"); assert.equal(wildDrop.stack.quantity, 1); const summonId = harness.simulation.spawnEntity(shadeSummonComponents({ tileX: 12, tileY: 8, level: 10, ownerEntityId: playerId, expiresAt: Date.now() + 60_000 })); const summonHandle = world.ecs.handleOf(summonId); const summonHp = world.ecs.get(summonHandle, Component.health)?.max ?? 10; const summonEvents = applyDamageAndKill(world, playerId, summonId, summonHp, Date.now()); assert.equal( summonEvents.some((event) => event.type === "ground-item-appear"), false, "summoned shade must not drop loot" ); } finally { harness.close(); } });