| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { BlockType } from "../../shared/definitions/tiles.js"; |
| 4 | import { ItemId } from "../../shared/definitions/items.js"; |
| 5 | import { Component } from "../src/ecs/components.js"; |
| 6 | import { MobId } from "../../shared/definitions/mobs.js"; |
| 7 | import { mobComponents } from "../src/simulation/spawners.js"; |
| 8 | import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; |
| 9 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 10 | import { emptyEquipment, emptySlots, addCoinsToSlots, countCoinsInSlots } from "../../shared/items/inventory.js"; |
| 11 | import { applyDamageAndKill } from "../src/combat/service.js"; |
| 12 | |
| 13 | function componentsOf (simulation, entityId) |
| 14 | { |
| 15 | const record = simulation.entityRecord(entityId); |
| 16 | const worker = simulation.workers[record.workerId]; |
| 17 | const handle = worker.world.ecs.handleOf(entityId); |
| 18 | return worker.world.ecs.componentsOf(handle); |
| 19 | } |
| 20 | |
| 21 | function placeAltar (worldDb, tileX, tileY) |
| 22 | { |
| 23 | const chunkX = Math.floor(tileX / 16); |
| 24 | const chunkY = Math.floor(tileY / 16); |
| 25 | const data = createEmptyChunkData(); |
| 26 | data.open = true; |
| 27 | data.blocks[(tileY - chunkY * 16) * 16 + (tileX - chunkX * 16)] = { |
| 28 | typeId: BlockType.churchAltar, |
| 29 | state: 0 |
| 30 | }; |
| 31 | worldDb.saveChunkVersion( |
| 32 | chunkX, |
| 33 | chunkY, |
| 34 | worldDb.getActiveRevisionId(), |
| 35 | true, |
| 36 | encodeChunkBlob(data) |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | function killPlayer (harness, character, playerId) |
| 41 | { |
| 42 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 43 | tileX: 9, |
| 44 | tileY: 8, |
| 45 | mobTypeId: MobId.deer, |
| 46 | spawnerId: null |
| 47 | })); |
| 48 | const deer = componentsOf(harness.simulation, deerId); |
| 49 | deer.health.current = 1_000_000; |
| 50 | deer.health.max = 1_000_000; |
| 51 | const player = componentsOf(harness.simulation, playerId); |
| 52 | player.health.current = 1; |
| 53 | harness.simulation.enqueue(1, { |
| 54 | type: "attack", |
| 55 | characterId: character.character_id, |
| 56 | targetEntityId: deerId |
| 57 | }); |
| 58 | const first = harness.simulation.step(); |
| 59 | const immediate = first.events.find((event) => event.type === "player-died"); |
| 60 | if (immediate) |
| 61 | { return { died: immediate, events: first.events }; } |
| 62 | for (let i = 0; i < 100; i++) |
| 63 | { |
| 64 | harness.advance(1000); |
| 65 | const result = harness.simulation.step(); |
| 66 | const died = result.events.find((event) => event.type === "player-died"); |
| 67 | if (died) |
| 68 | { return { died, events: result.events }; } |
| 69 | } |
| 70 | throw new Error("Player did not die."); |
| 71 | } |
| 72 | |
| 73 | function withDeterministicCombat (fn) |
| 74 | { |
| 75 | const random = Math.random; |
| 76 | Math.random = () => 0.5; |
| 77 | try |
| 78 | { fn(); } |
| 79 | finally |
| 80 | { Math.random = random; } |
| 81 | } |
| 82 | |
| 83 | test("player death marks dead and drops bones and skull", () => |
| 84 | { |
| 85 | withDeterministicCombat(() => |
| 86 | { |
| 87 | const harness = makeSimulation({ workerCount: 1 }); |
| 88 | try |
| 89 | { |
| 90 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 91 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 92 | const result = killPlayer(harness, character, playerId); |
| 93 | assert.equal(result.died.sessionId, 1); |
| 94 | assert.ok(componentsOf(harness.simulation, playerId)[Component.dead]); |
| 95 | assert.equal(harness.stateDb.getCharacter(character.character_id).is_dead, 1); |
| 96 | const drops = harness.stateDb.listGroundItemsInRegion(0, 0, 63, 63); |
| 97 | const itemIds = drops.map((item) => item.stack.itemId); |
| 98 | assert.ok(itemIds.includes(ItemId.bones)); |
| 99 | assert.ok(itemIds.includes(ItemId.skull)); |
| 100 | const bag = harness.simulation.bagOf(playerId); |
| 101 | assert.equal(countCoinsInSlots(bag.inventory), 0); |
| 102 | } |
| 103 | finally |
| 104 | { harness.close(); } |
| 105 | }); |
| 106 | }); |
| 107 | |
| 108 | test("player death drops all carried coins for anyone to pick up", () => |
| 109 | { |
| 110 | withDeterministicCombat(() => |
| 111 | { |
| 112 | const harness = makeSimulation({ workerCount: 1 }); |
| 113 | try |
| 114 | { |
| 115 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 116 | const funded = addCoinsToSlots(emptySlots(), 250); |
| 117 | assert.equal(funded.ok, true); |
| 118 | harness.stateDb.saveBag(character.character_id, { |
| 119 | inventory: funded.slots, |
| 120 | equipment: emptyEquipment(), |
| 121 | vault: [], |
| 122 | vaultReserved: [] |
| 123 | }); |
| 124 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 125 | assert.equal(countCoinsInSlots(harness.simulation.bagOf(playerId).inventory), 250); |
| 126 | killPlayer(harness, character, playerId); |
| 127 | const bag = harness.simulation.bagOf(playerId); |
| 128 | assert.equal(countCoinsInSlots(bag.inventory), 0); |
| 129 | const drops = harness.stateDb.listGroundItemsInRegion(0, 0, 63, 63); |
| 130 | const coinDrop = drops.find((item) => item.stack.itemId === ItemId.coins); |
| 131 | assert.ok(coinDrop); |
| 132 | assert.equal(coinDrop.stack.quantity, 250); |
| 133 | assert.equal(coinDrop.source, "player-death"); |
| 134 | assert.deepEqual( |
| 135 | drops.map((item) => item.stack.itemId).sort((a, b) => a - b), |
| 136 | [ItemId.coins, ItemId.bones, ItemId.skull].sort((a, b) => a - b) |
| 137 | ); |
| 138 | } |
| 139 | finally |
| 140 | { harness.close(); } |
| 141 | }); |
| 142 | }); |
| 143 | |
| 144 | test("respawn moves beside nearest church altar with full vitals", () => |
| 145 | { |
| 146 | withDeterministicCombat(() => |
| 147 | { |
| 148 | const harness = makeSimulation({ workerCount: 1 }); |
| 149 | try |
| 150 | { |
| 151 | placeAltar(harness.worldDb, 20, 8); |
| 152 | harness.simulation.reloadWorld(); |
| 153 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 154 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 155 | killPlayer(harness, character, playerId); |
| 156 | harness.simulation.enqueue(1, { type: "respawn", characterId: character.character_id }); |
| 157 | const result = harness.simulation.step(); |
| 158 | assert.ok(result.events.some((event) => event.type === "player-respawned")); |
| 159 | const player = componentsOf(harness.simulation, playerId); |
| 160 | assert.equal(player[Component.dead], undefined); |
| 161 | assert.equal(player.health.current, player.health.max); |
| 162 | assert.equal(player.stamina.current, player.stamina.max); |
| 163 | assert.equal( |
| 164 | Math.max(Math.abs(player.position.tileX - 20), Math.abs(player.position.tileY - 8)), |
| 165 | 1 |
| 166 | ); |
| 167 | assert.equal(harness.stateDb.getCharacter(character.character_id).is_dead, 0); |
| 168 | } |
| 169 | finally |
| 170 | { harness.close(); } |
| 171 | }); |
| 172 | }); |
| 173 | |
| 174 | test("respawn without an altar falls back to the death tile", () => |
| 175 | { |
| 176 | withDeterministicCombat(() => |
| 177 | { |
| 178 | const harness = makeSimulation({ workerCount: 1 }); |
| 179 | try |
| 180 | { |
| 181 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 182 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 183 | const { died } = killPlayer(harness, character, playerId); |
| 184 | harness.simulation.enqueue(1, { type: "respawn", characterId: character.character_id }); |
| 185 | const result = harness.simulation.step(); |
| 186 | assert.equal(result.events.some((event) => event.type === "command-error"), false); |
| 187 | const player = componentsOf(harness.simulation, playerId); |
| 188 | assert.equal(player.position.tileX, died.tileX); |
| 189 | assert.equal(player.position.tileY, died.tileY); |
| 190 | assert.equal(player.health.current, player.health.max); |
| 191 | assert.equal(player[Component.dead], undefined); |
| 192 | } |
| 193 | finally |
| 194 | { harness.close(); } |
| 195 | }); |
| 196 | }); |
| 197 | |
| 198 | test("dead players stop combat and active auto-repeat and cannot be targeted", () => |
| 199 | { |
| 200 | withDeterministicCombat(() => |
| 201 | { |
| 202 | const harness = makeSimulation({ workerCount: 1 }); |
| 203 | try |
| 204 | { |
| 205 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 206 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 207 | const worker = harness.simulation.workers[ |
| 208 | harness.simulation.entityRecord(playerId).workerId |
| 209 | ]; |
| 210 | const handle = worker.world.ecs.handleOf(playerId); |
| 211 | worker.world.ecs.add(handle, Component.combat, { |
| 212 | targetEntityId: 99, |
| 213 | nextSwingAt: 0, |
| 214 | seekCover: false |
| 215 | }); |
| 216 | worker.world.ecs.add(handle, Component.spellcasting, { |
| 217 | armedSpellId: "firebolt", |
| 218 | repeatSpellId: "firebolt", |
| 219 | repeatTargetEntityId: 99, |
| 220 | repeatTileX: null, |
| 221 | repeatTileY: null, |
| 222 | pendingCast: { spellId: "firebolt", tileX: 9, tileY: 8, targetEntityId: 99 }, |
| 223 | nextCastAt: 0 |
| 224 | }); |
| 225 | worker.world.ecs.add(handle, Component.spells, { |
| 226 | known: ["firebolt"], |
| 227 | favorites: [], |
| 228 | autoCast: true, |
| 229 | castChances: {} |
| 230 | }); |
| 231 | const path = worker.world.ecs.get(handle, Component.path) |
| 232 | ?? worker.world.ecs.add(handle, Component.path, { tiles: [] }); |
| 233 | path.tiles = [{ x: 9, y: 8 }]; |
| 234 | |
| 235 | killPlayer(harness, character, playerId); |
| 236 | const player = componentsOf(harness.simulation, playerId); |
| 237 | assert.ok(player[Component.dead]); |
| 238 | assert.equal(player.combat?.targetEntityId ?? null, null); |
| 239 | assert.equal(player.spells?.autoCast, true, "auto-cast preference survives death"); |
| 240 | assert.equal(player.spellcasting?.armedSpellId ?? null, null); |
| 241 | assert.equal(player.spellcasting?.repeatSpellId ?? null, null); |
| 242 | assert.equal(player.spellcasting?.pendingCast ?? null, null); |
| 243 | assert.deepEqual(player.path?.tiles ?? [], []); |
| 244 | |
| 245 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 246 | tileX: 10, |
| 247 | tileY: 8, |
| 248 | mobTypeId: MobId.deer, |
| 249 | spawnerId: null |
| 250 | })); |
| 251 | harness.simulation.enqueue(1, { |
| 252 | type: "attack", |
| 253 | characterId: character.character_id, |
| 254 | targetEntityId: deerId |
| 255 | }); |
| 256 | harness.simulation.enqueue(1, { |
| 257 | type: "drop-item", |
| 258 | characterId: character.character_id, |
| 259 | slotIndex: 0, |
| 260 | quantity: 1 |
| 261 | }); |
| 262 | const result = harness.simulation.step(); |
| 263 | assert.equal( |
| 264 | result.events.some((event) => event.type === "command-error"), |
| 265 | false, |
| 266 | "dead player intents are ignored silently" |
| 267 | ); |
| 268 | assert.equal( |
| 269 | componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null, |
| 270 | null, |
| 271 | "dead player cannot start new combat" |
| 272 | ); |
| 273 | } |
| 274 | finally |
| 275 | { harness.close(); } |
| 276 | }); |
| 277 | }); |
| 278 | |
| 279 | test("attackers drop melee and spell auto-targets when the victim dies", () => |
| 280 | { |
| 281 | withDeterministicCombat(() => |
| 282 | { |
| 283 | const harness = makeSimulation({ workerCount: 1 }); |
| 284 | try |
| 285 | { |
| 286 | const victim = makeCharacter(harness.stateDb, { |
| 287 | tileX: 8, |
| 288 | tileY: 8, |
| 289 | firstName: "Vic", |
| 290 | lastName: "Victim" |
| 291 | }); |
| 292 | const caster = makeCharacter(harness.stateDb, { |
| 293 | tileX: 10, |
| 294 | tileY: 8, |
| 295 | firstName: "Cas", |
| 296 | lastName: "Caster" |
| 297 | }); |
| 298 | const victimId = harness.simulation.spawnCharacter(1, victim); |
| 299 | const casterId = harness.simulation.spawnCharacter(2, caster); |
| 300 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 301 | tileX: 9, |
| 302 | tileY: 8, |
| 303 | mobTypeId: MobId.deer, |
| 304 | spawnerId: null |
| 305 | })); |
| 306 | const worker = harness.simulation.workers[ |
| 307 | harness.simulation.entityRecord(victimId).workerId |
| 308 | ]; |
| 309 | const ecs = worker.world.ecs; |
| 310 | const deerHandle = ecs.handleOf(deerId); |
| 311 | const casterHandle = ecs.handleOf(casterId); |
| 312 | const victimHandle = ecs.handleOf(victimId); |
| 313 | ecs.get(deerHandle, Component.health).current = 1_000_000; |
| 314 | ecs.get(deerHandle, Component.health).max = 1_000_000; |
| 315 | ecs.get(victimHandle, Component.health).current = 1; |
| 316 | ecs.add(deerHandle, Component.combat, { |
| 317 | targetEntityId: victimId, |
| 318 | nextSwingAt: 0, |
| 319 | seekCover: false, |
| 320 | fleeFrom: null |
| 321 | }); |
| 322 | ecs.add(casterHandle, Component.spellcasting, { |
| 323 | armedSpellId: null, |
| 324 | repeatSpellId: "firebolt", |
| 325 | repeatTargetEntityId: victimId, |
| 326 | repeatTileX: null, |
| 327 | repeatTileY: null, |
| 328 | pendingCast: { spellId: "firebolt", tileX: 8, tileY: 8, targetEntityId: victimId }, |
| 329 | nextCastAt: 0 |
| 330 | }); |
| 331 | const casterPath = ecs.get(casterHandle, Component.path) |
| 332 | ?? ecs.add(casterHandle, Component.path, { tiles: [] }); |
| 333 | casterPath.tiles = [{ x: 8, y: 8 }]; |
| 334 | |
| 335 | applyDamageAndKill(worker.world, deerId, victimId, 999, Date.now()); |
| 336 | |
| 337 | assert.ok(ecs.has(victimHandle, Component.dead)); |
| 338 | assert.equal( |
| 339 | ecs.get(deerHandle, Component.combat)?.targetEntityId ?? null, |
| 340 | null, |
| 341 | "melee auto-attack must stop on death" |
| 342 | ); |
| 343 | const casting = ecs.get(casterHandle, Component.spellcasting); |
| 344 | assert.equal(casting?.repeatTargetEntityId ?? null, null); |
| 345 | assert.equal(casting?.pendingCast ?? null, null); |
| 346 | assert.deepEqual(ecs.get(casterHandle, Component.path)?.tiles ?? [], []); |
| 347 | } |
| 348 | finally |
| 349 | { harness.close(); } |
| 350 | }); |
| 351 | }); |
| 352 | |
| 353 | test("player death emits entity-despawn for other viewers", () => |
| 354 | { |
| 355 | withDeterministicCombat(() => |
| 356 | { |
| 357 | const harness = makeSimulation({ workerCount: 1 }); |
| 358 | try |
| 359 | { |
| 360 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 361 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 362 | const result = killPlayer(harness, character, playerId); |
| 363 | assert.ok(result.events.some((event) => |
| 364 | event.type === "entity-despawn" |
| 365 | && event.entityId === playerId |
| 366 | && event.cause === "death" |
| 367 | )); |
| 368 | } |
| 369 | finally |
| 370 | { harness.close(); } |
| 371 | }); |
| 372 | }); |
| 373 | |
| 374 | test("mob kill entity-despawn is marked as combat death", () => |
| 375 | { |
| 376 | const harness = makeSimulation({ workerCount: 1 }); |
| 377 | try |
| 378 | { |
| 379 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 380 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 381 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 382 | tileX: 9, |
| 383 | tileY: 8, |
| 384 | mobTypeId: MobId.rat, |
| 385 | spawnerId: null |
| 386 | })); |
| 387 | const worker = harness.simulation.workers[ |
| 388 | harness.simulation.entityRecord(ratId).workerId |
| 389 | ]; |
| 390 | const handle = worker.world.ecs.handleOf(ratId); |
| 391 | const health = worker.world.ecs.get(handle, Component.health); |
| 392 | health.current = 1; |
| 393 | const events = applyDamageAndKill(worker.world, playerId, ratId, 1, harness.now()); |
| 394 | assert.ok(events.some((event) => |
| 395 | event.type === "entity-despawn" |
| 396 | && event.entityId === ratId |
| 397 | && event.cause === "death" |
| 398 | )); |
| 399 | } |
| 400 | finally |
| 401 | { harness.close(); } |
| 402 | }); |