| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { dirname, join } from "node:path"; |
| 4 | import { fileURLToPath } from "node:url"; |
| 5 | import { MobId, mobDefinition, mobSpriteName } from "../../shared/definitions/mobs.js"; |
| 6 | import { mobIsAggressive } from "../../shared/definitions/mobCombat.js"; |
| 7 | import { EssenceItemId } from "../../shared/definitions/essenceItems.js"; |
| 8 | import { MagicElement } from "../../shared/definitions/attackTypes.js"; |
| 9 | import { buildSpriteCatalog } from "../src/assets/spriteCatalog.js"; |
| 10 | import { mobComponents } from "../src/simulation/spawners.js"; |
| 11 | import { shadeSummonComponents, stepSummons } from "../src/spells/summons.js"; |
| 12 | import { applyDamageAndKill, stepAggression } from "../src/combat/service.js"; |
| 13 | import { Component } from "../src/ecs/components.js"; |
| 14 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 15 | |
| 16 | const ASSET_ROOT = join(dirname(fileURLToPath(import.meta.url)), "../../client/assets"); |
| 17 | |
| 18 | function workerOf (simulation, entityId) |
| 19 | { |
| 20 | const record = simulation.entityRecord(entityId); |
| 21 | assert.ok(record, "entity must be remembered"); |
| 22 | return simulation.workers[record.workerId]; |
| 23 | } |
| 24 | |
| 25 | test("shade uses own static body and animated bloom sprites", () => |
| 26 | { |
| 27 | const shade = mobDefinition(MobId.shade); |
| 28 | assert.equal(shade.spriteKey, undefined); |
| 29 | assert.equal(mobSpriteName(MobId.shade), "mob:shade:shade"); |
| 30 | assert.equal(shade.bloomBody, true); |
| 31 | assert.equal(shade.frames, 1); |
| 32 | assert.equal(shade.bloomFrames, 2); |
| 33 | assert.equal(shade.bloomAnimationSpeed, 0.5); |
| 34 | assert.equal(shade.aggressive, true); |
| 35 | assert.equal(mobIsAggressive(shade), true); |
| 36 | assert.equal(shade.drops.length, 1); |
| 37 | assert.equal(shade.drops[0].itemId, EssenceItemId[MagicElement.dark]); |
| 38 | assert.equal(shade.drops[0].chance, 1); |
| 39 | |
| 40 | const catalog = buildSpriteCatalog(ASSET_ROOT); |
| 41 | const body = catalog.images.find((entry) => entry.name === "mob:shade:shade"); |
| 42 | const bloom = catalog.images.find((entry) => entry.name === "mob:shade:shade:bloom"); |
| 43 | assert.ok(body, "shade body catalog entry"); |
| 44 | assert.ok(bloom, "shade bloom catalog entry"); |
| 45 | assert.equal(body.subImgTotal, 1); |
| 46 | assert.equal(body.animationSpeed, 0); |
| 47 | assert.equal(bloom.subImgTotal, 2); |
| 48 | assert.equal(bloom.perRow, 2); |
| 49 | assert.equal(bloom.animationSpeed, 0.5); |
| 50 | |
| 51 | const components = mobComponents({ |
| 52 | tileX: 8, |
| 53 | tileY: 8, |
| 54 | mobTypeId: MobId.shade, |
| 55 | spawnerId: "0:0:1", |
| 56 | originTileX: 8, |
| 57 | originTileY: 8 |
| 58 | }); |
| 59 | assert.equal(components.appearance.bloomBody, true); |
| 60 | assert.equal(components.appearance.tint, undefined); |
| 61 | assert.equal(components.appearance.dualTint, undefined); |
| 62 | assert.equal(components.spawnOrigin.aggressive, true); |
| 63 | assert.equal(components.summon, undefined); |
| 64 | }); |
| 65 | |
| 66 | test("summoned shade uses companion targeting, not player aggro", () => |
| 67 | { |
| 68 | const harness = makeSimulation({ workerCount: 1 }); |
| 69 | try |
| 70 | { |
| 71 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 72 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 73 | const components = shadeSummonComponents({ |
| 74 | tileX: 10, |
| 75 | tileY: 8, |
| 76 | level: 10, |
| 77 | ownerEntityId: playerId, |
| 78 | expiresAt: Date.now() + 60_000 |
| 79 | }); |
| 80 | assert.ok(components.summon); |
| 81 | assert.equal(components.spawnOrigin, undefined); |
| 82 | assert.equal(components.appearance.bloomBody, true); |
| 83 | |
| 84 | const shadeId = harness.simulation.spawnEntity(components); |
| 85 | const worker = workerOf(harness.simulation, shadeId); |
| 86 | const world = worker.world; |
| 87 | const shadeHandle = world.ecs.handleOf(shadeId); |
| 88 | |
| 89 | stepAggression(world, Date.now()); |
| 90 | assert.equal( |
| 91 | world.ecs.get(shadeHandle, Component.combat)?.targetEntityId ?? null, |
| 92 | null, |
| 93 | "summoned shade must not auto-aggro the owner/player" |
| 94 | ); |
| 95 | |
| 96 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 97 | tileX: 12, |
| 98 | tileY: 8, |
| 99 | mobTypeId: MobId.rat, |
| 100 | spawnerId: "0:0:2", |
| 101 | originTileX: 12, |
| 102 | originTileY: 8, |
| 103 | aggressive: true |
| 104 | })); |
| 105 | stepSummons(world, Date.now()); |
| 106 | assert.equal( |
| 107 | world.ecs.get(shadeHandle, Component.combat)?.targetEntityId, |
| 108 | ratId, |
| 109 | "summoned shade should pick nearby aggressive hostiles" |
| 110 | ); |
| 111 | } |
| 112 | finally |
| 113 | { harness.close(); } |
| 114 | }); |
| 115 | |
| 116 | test("wild shade drops dark essence; summoned shade does not", () => |
| 117 | { |
| 118 | const harness = makeSimulation({ workerCount: 1 }); |
| 119 | try |
| 120 | { |
| 121 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 122 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 123 | |
| 124 | const wildId = harness.simulation.spawnEntity(mobComponents({ |
| 125 | tileX: 10, |
| 126 | tileY: 8, |
| 127 | mobTypeId: MobId.shade, |
| 128 | spawnerId: "0:0:shade", |
| 129 | originTileX: 10, |
| 130 | originTileY: 8, |
| 131 | level: 1 |
| 132 | })); |
| 133 | const worker = workerOf(harness.simulation, wildId); |
| 134 | const world = worker.world; |
| 135 | const wildHandle = world.ecs.handleOf(wildId); |
| 136 | const wildHp = world.ecs.get(wildHandle, Component.health)?.max ?? 10; |
| 137 | const wildEvents = applyDamageAndKill(world, playerId, wildId, wildHp, Date.now()); |
| 138 | const wildDrop = wildEvents.find((event) => |
| 139 | event.type === "ground-item-appear" |
| 140 | && event.stack?.itemId === EssenceItemId[MagicElement.dark] |
| 141 | ); |
| 142 | assert.ok(wildDrop, "spawner shade must drop dark essence"); |
| 143 | assert.equal(wildDrop.stack.quantity, 1); |
| 144 | |
| 145 | const summonId = harness.simulation.spawnEntity(shadeSummonComponents({ |
| 146 | tileX: 12, |
| 147 | tileY: 8, |
| 148 | level: 10, |
| 149 | ownerEntityId: playerId, |
| 150 | expiresAt: Date.now() + 60_000 |
| 151 | })); |
| 152 | const summonHandle = world.ecs.handleOf(summonId); |
| 153 | const summonHp = world.ecs.get(summonHandle, Component.health)?.max ?? 10; |
| 154 | const summonEvents = applyDamageAndKill(world, playerId, summonId, summonHp, Date.now()); |
| 155 | assert.equal( |
| 156 | summonEvents.some((event) => event.type === "ground-item-appear"), |
| 157 | false, |
| 158 | "summoned shade must not drop loot" |
| 159 | ); |
| 160 | } |
| 161 | finally |
| 162 | { harness.close(); } |
| 163 | }); |