| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { mobComponents } from "../src/simulation/spawners.js"; |
| 4 | import { MobId } from "../../shared/definitions/mobs.js"; |
| 5 | import { ItemId, EQUIPMENT_SLOT } from "../../shared/definitions/items.js"; |
| 6 | import { makeCharacter, makeSimulation, stepTimes } from "./helpers/simulationHarness.js"; |
| 7 | |
| 8 | function workerOf (simulation, entityId) |
| 9 | { |
| 10 | const record = simulation.entityRecord(entityId); |
| 11 | assert.ok(record, "entity must be remembered"); |
| 12 | return simulation.workers[record.workerId]; |
| 13 | } |
| 14 | |
| 15 | function componentsOf (simulation, entityId) |
| 16 | { |
| 17 | const worker = workerOf(simulation, entityId); |
| 18 | const handle = worker.world.ecs.handleOf(entityId); |
| 19 | assert.ok(handle != null); |
| 20 | return worker.world.ecs.componentsOf(handle); |
| 21 | } |
| 22 | |
| 23 | test("attack engages both combatants and retaliates", () => |
| 24 | { |
| 25 | const harness = makeSimulation({ workerCount: 1 }); |
| 26 | try |
| 27 | { |
| 28 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 29 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 30 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 31 | tileX: 9, |
| 32 | tileY: 8, |
| 33 | mobTypeId: MobId.rat, |
| 34 | spawnerId: null |
| 35 | })); |
| 36 | |
| 37 | harness.simulation.enqueue(1, { |
| 38 | type: "attack", |
| 39 | characterId: character.character_id, |
| 40 | targetEntityId: ratId |
| 41 | }); |
| 42 | harness.simulation.step(); |
| 43 | |
| 44 | const player = componentsOf(harness.simulation, playerId); |
| 45 | const rat = componentsOf(harness.simulation, ratId); |
| 46 | assert.equal(player.combat.targetEntityId, ratId); |
| 47 | assert.equal(rat.combat.targetEntityId, playerId); |
| 48 | } |
| 49 | finally |
| 50 | { harness.close(); } |
| 51 | }); |
| 52 | |
| 53 | test("melee swing only resolves at Chebyshev distance 1", () => |
| 54 | { |
| 55 | const harness = makeSimulation({ workerCount: 1 }); |
| 56 | try |
| 57 | { |
| 58 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 59 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 60 | const bag = harness.simulation.bagOf(playerId); |
| 61 | const stickSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.stick); |
| 62 | assert.ok(stickSlot >= 0); |
| 63 | harness.simulation.enqueue(1, { |
| 64 | type: "equip-item", |
| 65 | characterId: character.character_id, |
| 66 | slotIndex: stickSlot |
| 67 | }); |
| 68 | harness.simulation.step(); |
| 69 | assert.equal( |
| 70 | harness.simulation.bagOf(playerId).equipment.slots[EQUIPMENT_SLOT.weapon]?.itemId, |
| 71 | ItemId.stick |
| 72 | ); |
| 73 | |
| 74 | const farRat = harness.simulation.spawnEntity(mobComponents({ |
| 75 | tileX: 12, |
| 76 | tileY: 8, |
| 77 | mobTypeId: MobId.rat |
| 78 | })); |
| 79 | harness.simulation.enqueue(1, { |
| 80 | type: "attack", |
| 81 | characterId: character.character_id, |
| 82 | targetEntityId: farRat |
| 83 | }); |
| 84 | const farEvents = harness.simulation.step().events; |
| 85 | assert.equal(farEvents.some((event) => event.type === "combat-swing"), false); |
| 86 | |
| 87 | const nearRat = harness.simulation.spawnEntity(mobComponents({ |
| 88 | tileX: 9, |
| 89 | tileY: 8, |
| 90 | mobTypeId: MobId.rat |
| 91 | })); |
| 92 | harness.simulation.enqueue(1, { |
| 93 | type: "attack", |
| 94 | characterId: character.character_id, |
| 95 | targetEntityId: nearRat |
| 96 | }); |
| 97 | const nearEvents = harness.simulation.step().events; |
| 98 | assert.ok(nearEvents.some((event) => event.type === "combat-swing")); |
| 99 | assert.ok(nearEvents.some((event) => event.type === "combat-hit" || event.type === "combat-miss")); |
| 100 | } |
| 101 | finally |
| 102 | { harness.close(); } |
| 103 | }); |
| 104 | |
| 105 | test("pigeons are untargetable free-flight and never get path tiles", () => |
| 106 | { |
| 107 | const harness = makeSimulation({ workerCount: 1 }); |
| 108 | try |
| 109 | { |
| 110 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 111 | harness.simulation.spawnCharacter(1, character); |
| 112 | const pigeonId = harness.simulation.spawnEntity(mobComponents({ |
| 113 | tileX: 10, |
| 114 | tileY: 8, |
| 115 | mobTypeId: MobId.pigeon |
| 116 | })); |
| 117 | |
| 118 | const before = componentsOf(harness.simulation, pigeonId); |
| 119 | assert.equal(before.flight != null, true); |
| 120 | assert.equal(before.combat == null, true); |
| 121 | assert.deepEqual(before.path?.tiles ?? [], []); |
| 122 | |
| 123 | harness.setNow(harness.now() + 500); |
| 124 | stepTimes(harness.simulation, 10); |
| 125 | const after = componentsOf(harness.simulation, pigeonId); |
| 126 | assert.deepEqual(after.path?.tiles ?? [], []); |
| 127 | assert.equal(after.flight != null, true); |
| 128 | |
| 129 | harness.simulation.enqueue(1, { |
| 130 | type: "attack", |
| 131 | characterId: character.character_id, |
| 132 | targetEntityId: pigeonId |
| 133 | }); |
| 134 | const result = harness.simulation.step(); |
| 135 | assert.ok(result.events.some((event) => event.type === "command-error")); |
| 136 | assert.equal(componentsOf(harness.simulation, pigeonId).path?.tiles?.length ?? 0, 0); |
| 137 | } |
| 138 | finally |
| 139 | { harness.close(); } |
| 140 | }); |
| 141 | |
| 142 | test("player combat and skills components are present on spawn", () => |
| 143 | { |
| 144 | const harness = makeSimulation({ workerCount: 1 }); |
| 145 | try |
| 146 | { |
| 147 | const character = makeCharacter(harness.stateDb); |
| 148 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 149 | const comps = componentsOf(harness.simulation, playerId); |
| 150 | assert.ok(comps.combat); |
| 151 | assert.ok(comps.skills?.levels?.clubfighting); |
| 152 | assert.ok(comps.health); |
| 153 | } |
| 154 | finally |
| 155 | { harness.close(); } |
| 156 | }); |