| 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 { Component } from "../src/ecs/components.js"; |
| 7 | import { tileKey } from "../src/world/coordinates.js"; |
| 8 | import { WorkerMessage } from "../src/simulation/protocol.js"; |
| 9 | import { tileToRegion } from "../src/world/coordinates.js"; |
| 10 | import { SkillId } from "../../shared/definitions/skills.js"; |
| 11 | import { resolveMeleeSwing, attackProfileOf } from "../src/combat/service.js"; |
| 12 | import { makeCharacter, makeSimulation, stepTimes } from "./helpers/simulationHarness.js"; |
| 13 | |
| 14 | function workerOf (simulation, entityId) |
| 15 | { |
| 16 | const record = simulation.entityRecord(entityId); |
| 17 | assert.ok(record, "entity must be remembered"); |
| 18 | return simulation.workers[record.workerId]; |
| 19 | } |
| 20 | |
| 21 | function componentsOf (simulation, entityId) |
| 22 | { |
| 23 | const worker = workerOf(simulation, entityId); |
| 24 | const handle = worker.world.ecs.handleOf(entityId); |
| 25 | assert.ok(handle != null); |
| 26 | return worker.world.ecs.componentsOf(handle); |
| 27 | } |
| 28 | |
| 29 | function teleportEntity (worker, entityId, tileX, tileY) |
| 30 | { |
| 31 | const world = worker.world; |
| 32 | const handle = world.ecs.handleOf(entityId); |
| 33 | const pos = world.ecs.get(handle, Component.position); |
| 34 | world.occupancy.delete(tileKey(pos.tileX, pos.tileY)); |
| 35 | pos.tileX = tileX; |
| 36 | pos.tileY = tileY; |
| 37 | world.occupancy.set(tileKey(tileX, tileY), entityId); |
| 38 | } |
| 39 | |
| 40 | test("attack engages both combatants and retaliates", () => |
| 41 | { |
| 42 | const harness = makeSimulation({ workerCount: 1 }); |
| 43 | try |
| 44 | { |
| 45 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 46 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 47 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 48 | tileX: 9, |
| 49 | tileY: 8, |
| 50 | mobTypeId: MobId.rat, |
| 51 | spawnerId: null |
| 52 | })); |
| 53 | |
| 54 | harness.simulation.enqueue(1, { |
| 55 | type: "attack", |
| 56 | characterId: character.character_id, |
| 57 | targetEntityId: ratId |
| 58 | }); |
| 59 | harness.simulation.step(); |
| 60 | |
| 61 | const player = componentsOf(harness.simulation, playerId); |
| 62 | const rat = componentsOf(harness.simulation, ratId); |
| 63 | assert.equal(player.combat.targetEntityId, ratId); |
| 64 | assert.equal(rat.combat.targetEntityId, playerId); |
| 65 | } |
| 66 | finally |
| 67 | { harness.close(); } |
| 68 | }); |
| 69 | |
| 70 | test("melee swing only resolves at Chebyshev distance 1", () => |
| 71 | { |
| 72 | const harness = makeSimulation({ workerCount: 1 }); |
| 73 | try |
| 74 | { |
| 75 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 76 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 77 | const bag = harness.simulation.bagOf(playerId); |
| 78 | const stickSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.stick); |
| 79 | assert.ok(stickSlot >= 0); |
| 80 | harness.simulation.enqueue(1, { |
| 81 | type: "equip-item", |
| 82 | characterId: character.character_id, |
| 83 | slotIndex: stickSlot |
| 84 | }); |
| 85 | harness.simulation.step(); |
| 86 | assert.equal( |
| 87 | harness.simulation.bagOf(playerId).equipment.slots[EQUIPMENT_SLOT.weapon]?.itemId, |
| 88 | ItemId.stick |
| 89 | ); |
| 90 | |
| 91 | const farRat = harness.simulation.spawnEntity(mobComponents({ |
| 92 | tileX: 12, |
| 93 | tileY: 8, |
| 94 | mobTypeId: MobId.rat |
| 95 | })); |
| 96 | harness.simulation.enqueue(1, { |
| 97 | type: "attack", |
| 98 | characterId: character.character_id, |
| 99 | targetEntityId: farRat |
| 100 | }); |
| 101 | const farEvents = harness.simulation.step().events; |
| 102 | assert.equal(farEvents.some((event) => event.type === "combat-swing"), false); |
| 103 | |
| 104 | const nearRat = harness.simulation.spawnEntity(mobComponents({ |
| 105 | tileX: 9, |
| 106 | tileY: 8, |
| 107 | mobTypeId: MobId.rat |
| 108 | })); |
| 109 | harness.simulation.enqueue(1, { |
| 110 | type: "attack", |
| 111 | characterId: character.character_id, |
| 112 | targetEntityId: nearRat |
| 113 | }); |
| 114 | const nearEvents = harness.simulation.step().events; |
| 115 | assert.ok(nearEvents.some((event) => event.type === "combat-swing")); |
| 116 | assert.ok(nearEvents.some((event) => |
| 117 | event.type === "combat-hit" |
| 118 | || event.type === "combat-miss" |
| 119 | || event.type === "combat-block")); |
| 120 | } |
| 121 | finally |
| 122 | { harness.close(); } |
| 123 | }); |
| 124 | |
| 125 | test("pigeons are untargetable free-flight and never get path tiles", () => |
| 126 | { |
| 127 | const harness = makeSimulation({ workerCount: 1 }); |
| 128 | try |
| 129 | { |
| 130 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 131 | harness.simulation.spawnCharacter(1, character); |
| 132 | const pigeonId = harness.simulation.spawnEntity(mobComponents({ |
| 133 | tileX: 10, |
| 134 | tileY: 8, |
| 135 | mobTypeId: MobId.pigeon |
| 136 | })); |
| 137 | |
| 138 | const before = componentsOf(harness.simulation, pigeonId); |
| 139 | assert.equal(before.flight != null, true); |
| 140 | assert.equal(before.combat == null, true); |
| 141 | assert.deepEqual(before.path?.tiles ?? [], []); |
| 142 | |
| 143 | harness.setNow(harness.now() + 500); |
| 144 | stepTimes(harness.simulation, 10); |
| 145 | const after = componentsOf(harness.simulation, pigeonId); |
| 146 | assert.deepEqual(after.path?.tiles ?? [], []); |
| 147 | assert.equal(after.flight != null, true); |
| 148 | |
| 149 | harness.simulation.enqueue(1, { |
| 150 | type: "attack", |
| 151 | characterId: character.character_id, |
| 152 | targetEntityId: pigeonId |
| 153 | }); |
| 154 | const result = harness.simulation.step(); |
| 155 | assert.ok(result.events.some((event) => event.type === "command-error")); |
| 156 | assert.equal(componentsOf(harness.simulation, pigeonId).path?.tiles?.length ?? 0, 0); |
| 157 | } |
| 158 | finally |
| 159 | { harness.close(); } |
| 160 | }); |
| 161 | |
| 162 | test("deer retaliate when attacked", () => |
| 163 | { |
| 164 | const harness = makeSimulation({ workerCount: 1 }); |
| 165 | try |
| 166 | { |
| 167 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 168 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 169 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 170 | tileX: 9, |
| 171 | tileY: 8, |
| 172 | mobTypeId: MobId.deer, |
| 173 | spawnerId: null |
| 174 | })); |
| 175 | |
| 176 | harness.simulation.enqueue(1, { |
| 177 | type: "attack", |
| 178 | characterId: character.character_id, |
| 179 | targetEntityId: deerId |
| 180 | }); |
| 181 | harness.simulation.step(); |
| 182 | |
| 183 | const deer = componentsOf(harness.simulation, deerId); |
| 184 | assert.equal(deer.combat?.targetEntityId, playerId); |
| 185 | assert.ok(deer.skills?.levels?.unarmed >= 1); |
| 186 | } |
| 187 | finally |
| 188 | { harness.close(); } |
| 189 | }); |
| 190 | |
| 191 | test("attacker keeps pursuing and can swing after the target moves", () => |
| 192 | { |
| 193 | const harness = makeSimulation({ workerCount: 1 }); |
| 194 | try |
| 195 | { |
| 196 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 197 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 198 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 199 | tileX: 12, |
| 200 | tileY: 8, |
| 201 | mobTypeId: MobId.rat, |
| 202 | spawnerId: null |
| 203 | })); |
| 204 | |
| 205 | harness.simulation.enqueue(1, { |
| 206 | type: "attack", |
| 207 | characterId: character.character_id, |
| 208 | targetEntityId: ratId |
| 209 | }); |
| 210 | harness.simulation.step(); |
| 211 | |
| 212 | const worker = workerOf(harness.simulation, playerId); |
| 213 | stepTimes(harness.simulation, 4); |
| 214 | teleportEntity(worker, ratId, 12, 10); |
| 215 | |
| 216 | let swings = 0; |
| 217 | for (let i = 0; i < 120; i++) |
| 218 | { |
| 219 | const events = harness.simulation.step().events; |
| 220 | swings += events.filter((event) => event.type === "combat-swing").length; |
| 221 | if (swings > 0) |
| 222 | { break; } |
| 223 | } |
| 224 | |
| 225 | assert.ok(swings >= 1); |
| 226 | assert.equal(componentsOf(harness.simulation, playerId).combat?.targetEntityId, ratId); |
| 227 | } |
| 228 | finally |
| 229 | { harness.close(); } |
| 230 | }); |
| 231 | |
| 232 | test("cancel attack stops the player but the mob keeps pursuing", () => |
| 233 | { |
| 234 | const harness = makeSimulation({ workerCount: 1 }); |
| 235 | try |
| 236 | { |
| 237 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 238 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 239 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 240 | tileX: 9, |
| 241 | tileY: 8, |
| 242 | mobTypeId: MobId.rat, |
| 243 | spawnerId: null |
| 244 | })); |
| 245 | |
| 246 | harness.simulation.enqueue(1, { |
| 247 | type: "attack", |
| 248 | characterId: character.character_id, |
| 249 | targetEntityId: ratId |
| 250 | }); |
| 251 | harness.simulation.step(); |
| 252 | |
| 253 | harness.simulation.enqueue(1, { |
| 254 | type: "cancel-attack", |
| 255 | characterId: character.character_id |
| 256 | }); |
| 257 | harness.simulation.step(); |
| 258 | |
| 259 | const player = componentsOf(harness.simulation, playerId); |
| 260 | const rat = componentsOf(harness.simulation, ratId); |
| 261 | assert.equal(player.combat?.targetEntityId, null); |
| 262 | assert.equal(rat.combat?.targetEntityId, playerId); |
| 263 | } |
| 264 | finally |
| 265 | { harness.close(); } |
| 266 | }); |
| 267 | |
| 268 | test("player combat and skills components are present on spawn", () => |
| 269 | { |
| 270 | const harness = makeSimulation({ workerCount: 1 }); |
| 271 | try |
| 272 | { |
| 273 | const character = makeCharacter(harness.stateDb); |
| 274 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 275 | const comps = componentsOf(harness.simulation, playerId); |
| 276 | assert.ok(comps.combat); |
| 277 | assert.equal(comps.skills?.xp?.clubfighting, 0); |
| 278 | assert.ok(comps.health); |
| 279 | } |
| 280 | finally |
| 281 | { harness.close(); } |
| 282 | }); |
| 283 | |
| 284 | test("mobs heal to full when their region unloads", () => |
| 285 | { |
| 286 | const harness = makeSimulation({ workerCount: 1 }); |
| 287 | try |
| 288 | { |
| 289 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 290 | tileX: 8, |
| 291 | tileY: 8, |
| 292 | mobTypeId: MobId.rat |
| 293 | })); |
| 294 | const worker = workerOf(harness.simulation, ratId); |
| 295 | const handle = worker.world.ecs.handleOf(ratId); |
| 296 | worker.world.ecs.get(handle, Component.health).current = 2; |
| 297 | const result = worker.call({ |
| 298 | type: WorkerMessage.dropRegion, |
| 299 | regionX: tileToRegion(8), |
| 300 | regionY: tileToRegion(8) |
| 301 | }); |
| 302 | const record = result.dropped.find((entry) => entry.entityId === ratId); |
| 303 | assert.ok(record); |
| 304 | assert.equal( |
| 305 | record.payload.components.health.current, |
| 306 | record.payload.components.health.max |
| 307 | ); |
| 308 | } |
| 309 | finally |
| 310 | { harness.close(); } |
| 311 | }); |
| 312 | |
| 313 | test("taking damage awards Health XP equal to damage dealt", () => |
| 314 | { |
| 315 | const harness = makeSimulation({ workerCount: 1 }); |
| 316 | const random = Math.random; |
| 317 | Math.random = () => 0; |
| 318 | try |
| 319 | { |
| 320 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 321 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 322 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 323 | mobTypeId: MobId.rat, |
| 324 | tileX: 9, |
| 325 | tileY: 8 |
| 326 | })); |
| 327 | const worker = harness.simulation.workers[0]; |
| 328 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 329 | const before = worker.world.ecs.get(playerHandle, Component.skills).xp[SkillId.health]; |
| 330 | const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId)); |
| 331 | const events = resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now()); |
| 332 | const hit = events.find((event) => event.type === "combat-hit"); |
| 333 | assert.ok(hit); |
| 334 | const updated = events.find((event) => event.type === "skills-updated" && event.entityId === playerId); |
| 335 | assert.ok(updated); |
| 336 | assert.equal(updated.stored[SkillId.health] - before, hit.damage); |
| 337 | } |
| 338 | finally |
| 339 | { |
| 340 | Math.random = random; |
| 341 | harness.close(); |
| 342 | } |
| 343 | }); |