| 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, pinEastSplit, 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 | function entityIsAlive (simulation, entityId) |
| 41 | { |
| 42 | const record = simulation.entityRecord(entityId); |
| 43 | if (!record) |
| 44 | { return false; } |
| 45 | const worker = simulation.workers[record.workerId]; |
| 46 | const handle = worker.world.ecs.handleOf(entityId); |
| 47 | if (handle == null) |
| 48 | { return false; } |
| 49 | const health = worker.world.ecs.get(handle, Component.health); |
| 50 | return (health?.current ?? 0) > 0; |
| 51 | } |
| 52 | |
| 53 | function simulateAttackClick (harness, character, targetEntityId) |
| 54 | { |
| 55 | harness.simulation.enqueue(1, { |
| 56 | type: "attack", |
| 57 | characterId: character.character_id, |
| 58 | targetEntityId |
| 59 | }); |
| 60 | } |
| 61 | |
| 62 | /** Run ticks until one combatant dies or the step budget is exhausted. */ |
| 63 | function runCombatUntilOutcome (harness, playerId, deerId, maxSteps = 600) |
| 64 | { |
| 65 | let swings = 0; |
| 66 | for (let i = 0; i < maxSteps; i++) |
| 67 | { |
| 68 | harness.advance(50); |
| 69 | const events = harness.simulation.step().events; |
| 70 | swings += events.filter((event) => event.type === "combat-swing").length; |
| 71 | const playerAlive = entityIsAlive(harness.simulation, playerId); |
| 72 | const deerAlive = entityIsAlive(harness.simulation, deerId); |
| 73 | if (!playerAlive || !deerAlive) |
| 74 | { return { playerAlive, deerAlive, swings, steps: i + 1 }; } |
| 75 | } |
| 76 | return { |
| 77 | playerAlive: entityIsAlive(harness.simulation, playerId), |
| 78 | deerAlive: entityIsAlive(harness.simulation, deerId), |
| 79 | swings, |
| 80 | steps: maxSteps |
| 81 | }; |
| 82 | } |
| 83 | |
| 84 | function withDeterministicCombat (fn) |
| 85 | { |
| 86 | const random = Math.random; |
| 87 | Math.random = () => 0; |
| 88 | try |
| 89 | { fn(); } |
| 90 | finally |
| 91 | { Math.random = random; } |
| 92 | } |
| 93 | |
| 94 | test("immortal deer auto-attacks until the player dies", () => |
| 95 | { |
| 96 | withDeterministicCombat(() => |
| 97 | { |
| 98 | const harness = makeSimulation({ workerCount: 1 }); |
| 99 | try |
| 100 | { |
| 101 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 102 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 103 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 104 | tileX: 9, |
| 105 | tileY: 8, |
| 106 | mobTypeId: MobId.deer, |
| 107 | spawnerId: null |
| 108 | })); |
| 109 | const deer = componentsOf(harness.simulation, deerId); |
| 110 | deer.health.current = 1_000_000; |
| 111 | deer.health.max = 1_000_000; |
| 112 | |
| 113 | simulateAttackClick(harness, character, deerId); |
| 114 | harness.simulation.step(); |
| 115 | |
| 116 | const outcome = runCombatUntilOutcome(harness, playerId, deerId, 2_000); |
| 117 | assert.ok(outcome.swings >= 2, `expected sustained auto-attack swings, got ${outcome.swings}`); |
| 118 | assert.equal(outcome.deerAlive, true, "immortal deer should survive"); |
| 119 | assert.equal(outcome.playerAlive, false, "deer should kill the player via auto-attack"); |
| 120 | assert.equal( |
| 121 | componentsOf(harness.simulation, deerId).combat?.targetEntityId ?? null, |
| 122 | null, |
| 123 | "deer should clear engage after the player dies" |
| 124 | ); |
| 125 | } |
| 126 | finally |
| 127 | { harness.close(); } |
| 128 | }); |
| 129 | }); |
| 130 | |
| 131 | test("immortal deer keeps auto-attacking after the player walks away", () => |
| 132 | { |
| 133 | withDeterministicCombat(() => |
| 134 | { |
| 135 | const harness = makeSimulation({ workerCount: 1 }); |
| 136 | try |
| 137 | { |
| 138 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 139 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 140 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 141 | tileX: 9, |
| 142 | tileY: 8, |
| 143 | mobTypeId: MobId.deer, |
| 144 | spawnerId: null |
| 145 | })); |
| 146 | const deer = componentsOf(harness.simulation, deerId); |
| 147 | deer.health.current = 1_000_000; |
| 148 | deer.health.max = 1_000_000; |
| 149 | |
| 150 | simulateAttackClick(harness, character, deerId); |
| 151 | harness.simulation.step(); |
| 152 | |
| 153 | // Let one exchange land, then cancel player engage by walking away (client moveTo). |
| 154 | for (let i = 0; i < 30; i++) |
| 155 | { |
| 156 | harness.advance(50); |
| 157 | const swings = harness.simulation.step().events.filter((event) => event.type === "combat-swing").length; |
| 158 | if (swings > 0) |
| 159 | { break; } |
| 160 | } |
| 161 | harness.simulation.enqueue(1, { |
| 162 | type: "move-to", |
| 163 | characterId: character.character_id, |
| 164 | tileX: 8, |
| 165 | tileY: 14 |
| 166 | }); |
| 167 | harness.simulation.step(); |
| 168 | assert.equal(componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null, null); |
| 169 | assert.equal(componentsOf(harness.simulation, deerId).combat?.targetEntityId, playerId); |
| 170 | |
| 171 | const outcome = runCombatUntilOutcome(harness, playerId, deerId, 2_000); |
| 172 | assert.ok(outcome.swings >= 1, `expected deer pursuit swings, got ${outcome.swings}`); |
| 173 | assert.equal(outcome.deerAlive, true); |
| 174 | assert.equal(outcome.playerAlive, false, "deer should chase and kill the player after walk-away"); |
| 175 | } |
| 176 | finally |
| 177 | { harness.close(); } |
| 178 | }); |
| 179 | }); |
| 180 | |
| 181 | test("cross-worker immortal deer auto-attacks until the player dies", () => |
| 182 | { |
| 183 | withDeterministicCombat(() => |
| 184 | { |
| 185 | const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); |
| 186 | try |
| 187 | { |
| 188 | const character = makeCharacter(harness.stateDb, { tileX: 64, tileY: 8 }); |
| 189 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 190 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 191 | tileX: 63, |
| 192 | tileY: 8, |
| 193 | mobTypeId: MobId.deer, |
| 194 | spawnerId: null |
| 195 | })); |
| 196 | assert.equal(harness.simulation.entityRecord(playerId).workerId, 1); |
| 197 | assert.equal(harness.simulation.entityRecord(deerId).workerId, 0); |
| 198 | |
| 199 | const deerWorker = workerOf(harness.simulation, deerId); |
| 200 | const deerHandle = deerWorker.world.ecs.handleOf(deerId); |
| 201 | const deerHealth = deerWorker.world.ecs.get(deerHandle, Component.health); |
| 202 | deerHealth.current = 1_000_000; |
| 203 | deerHealth.max = 1_000_000; |
| 204 | |
| 205 | simulateAttackClick(harness, character, deerId); |
| 206 | const first = harness.simulation.step(); |
| 207 | assert.equal(first.events.some((event) => event.type === "command-error"), false); |
| 208 | |
| 209 | const outcome = runCombatUntilOutcome(harness, playerId, deerId, 2_000); |
| 210 | assert.ok(outcome.swings >= 2, `expected sustained cross-worker swings, got ${outcome.swings}`); |
| 211 | assert.equal(outcome.deerAlive, true); |
| 212 | assert.equal(outcome.playerAlive, false, "cross-worker deer should kill the player via auto-attack"); |
| 213 | } |
| 214 | finally |
| 215 | { harness.close(); } |
| 216 | }); |
| 217 | }); |
| 218 | |
| 219 | test("click attack from 5 tiles away ends with player or deer dead", () => |
| 220 | { |
| 221 | withDeterministicCombat(() => |
| 222 | { |
| 223 | const harness = makeSimulation({ workerCount: 1 }); |
| 224 | try |
| 225 | { |
| 226 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 227 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 228 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 229 | tileX: 13, |
| 230 | tileY: 8, |
| 231 | mobTypeId: MobId.deer, |
| 232 | spawnerId: null |
| 233 | })); |
| 234 | |
| 235 | simulateAttackClick(harness, character, deerId); |
| 236 | harness.simulation.step(); |
| 237 | |
| 238 | const outcome = runCombatUntilOutcome(harness, playerId, deerId); |
| 239 | assert.ok(outcome.swings >= 1, `expected at least one swing, got ${outcome.swings}`); |
| 240 | assert.ok( |
| 241 | !outcome.playerAlive || !outcome.deerAlive, |
| 242 | `expected a kill within ${outcome.steps} steps (${outcome.swings} swings); both still alive` |
| 243 | ); |
| 244 | } |
| 245 | finally |
| 246 | { harness.close(); } |
| 247 | }); |
| 248 | }); |
| 249 | |
| 250 | test("click attack while adjacent ends with player or deer dead", () => |
| 251 | { |
| 252 | withDeterministicCombat(() => |
| 253 | { |
| 254 | const harness = makeSimulation({ workerCount: 1 }); |
| 255 | try |
| 256 | { |
| 257 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 258 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 259 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 260 | tileX: 9, |
| 261 | tileY: 8, |
| 262 | mobTypeId: MobId.deer, |
| 263 | spawnerId: null |
| 264 | })); |
| 265 | |
| 266 | simulateAttackClick(harness, character, deerId); |
| 267 | harness.simulation.step(); |
| 268 | |
| 269 | const outcome = runCombatUntilOutcome(harness, playerId, deerId); |
| 270 | assert.ok(outcome.swings >= 1, `expected at least one swing, got ${outcome.swings}`); |
| 271 | assert.ok( |
| 272 | !outcome.playerAlive || !outcome.deerAlive, |
| 273 | `expected a kill within ${outcome.steps} steps (${outcome.swings} swings); both still alive` |
| 274 | ); |
| 275 | } |
| 276 | finally |
| 277 | { harness.close(); } |
| 278 | }); |
| 279 | }); |
| 280 | |
| 281 | test("cross-worker click attack from 5 tiles away ends with player or deer dead", () => |
| 282 | { |
| 283 | withDeterministicCombat(() => |
| 284 | { |
| 285 | const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); |
| 286 | try |
| 287 | { |
| 288 | const character = makeCharacter(harness.stateDb, { tileX: 59, tileY: 8 }); |
| 289 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 290 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 291 | tileX: 64, |
| 292 | tileY: 8, |
| 293 | mobTypeId: MobId.deer, |
| 294 | spawnerId: null |
| 295 | })); |
| 296 | assert.equal(harness.simulation.entityRecord(playerId).workerId, 0); |
| 297 | assert.equal(harness.simulation.entityRecord(deerId).workerId, 1); |
| 298 | |
| 299 | simulateAttackClick(harness, character, deerId); |
| 300 | const first = harness.simulation.step(); |
| 301 | assert.equal(first.events.some((event) => event.type === "command-error"), false); |
| 302 | |
| 303 | const outcome = runCombatUntilOutcome(harness, playerId, deerId); |
| 304 | assert.ok(outcome.swings >= 1, `expected at least one swing, got ${outcome.swings}`); |
| 305 | assert.ok( |
| 306 | !outcome.playerAlive || !outcome.deerAlive, |
| 307 | `expected a kill within ${outcome.steps} steps (${outcome.swings} swings); both still alive` |
| 308 | ); |
| 309 | } |
| 310 | finally |
| 311 | { harness.close(); } |
| 312 | }); |
| 313 | }); |
| 314 | |
| 315 | test("cross-worker adjacent click attack ends with player or deer dead", () => |
| 316 | { |
| 317 | withDeterministicCombat(() => |
| 318 | { |
| 319 | const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); |
| 320 | try |
| 321 | { |
| 322 | const character = makeCharacter(harness.stateDb, { tileX: 64, tileY: 8 }); |
| 323 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 324 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 325 | tileX: 63, |
| 326 | tileY: 8, |
| 327 | mobTypeId: MobId.deer, |
| 328 | spawnerId: null |
| 329 | })); |
| 330 | assert.equal(harness.simulation.entityRecord(playerId).workerId, 1); |
| 331 | assert.equal(harness.simulation.entityRecord(deerId).workerId, 0); |
| 332 | |
| 333 | simulateAttackClick(harness, character, deerId); |
| 334 | const first = harness.simulation.step(); |
| 335 | assert.equal(first.events.some((event) => event.type === "command-error"), false); |
| 336 | |
| 337 | const outcome = runCombatUntilOutcome(harness, playerId, deerId); |
| 338 | assert.ok(outcome.swings >= 1, `expected at least one swing, got ${outcome.swings}`); |
| 339 | assert.ok( |
| 340 | !outcome.playerAlive || !outcome.deerAlive, |
| 341 | `expected a kill within ${outcome.steps} steps (${outcome.swings} swings); both still alive` |
| 342 | ); |
| 343 | } |
| 344 | finally |
| 345 | { harness.close(); } |
| 346 | }); |
| 347 | }); |
| 348 | |
| 349 | test("attack engages both combatants and retaliates", () => |
| 350 | { |
| 351 | const harness = makeSimulation({ workerCount: 1 }); |
| 352 | try |
| 353 | { |
| 354 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 355 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 356 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 357 | tileX: 9, |
| 358 | tileY: 8, |
| 359 | mobTypeId: MobId.rat, |
| 360 | spawnerId: null |
| 361 | })); |
| 362 | |
| 363 | harness.simulation.enqueue(1, { |
| 364 | type: "attack", |
| 365 | characterId: character.character_id, |
| 366 | targetEntityId: ratId |
| 367 | }); |
| 368 | harness.simulation.step(); |
| 369 | |
| 370 | const player = componentsOf(harness.simulation, playerId); |
| 371 | const rat = componentsOf(harness.simulation, ratId); |
| 372 | assert.equal(player.combat.targetEntityId, ratId); |
| 373 | assert.equal(rat.combat.targetEntityId, playerId); |
| 374 | } |
| 375 | finally |
| 376 | { harness.close(); } |
| 377 | }); |
| 378 | |
| 379 | test("melee swing only resolves at Chebyshev distance 1", () => |
| 380 | { |
| 381 | const harness = makeSimulation({ workerCount: 1 }); |
| 382 | try |
| 383 | { |
| 384 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 385 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 386 | const bag = harness.simulation.bagOf(playerId); |
| 387 | const stickSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.stick); |
| 388 | assert.ok(stickSlot >= 0); |
| 389 | harness.simulation.enqueue(1, { |
| 390 | type: "equip-item", |
| 391 | characterId: character.character_id, |
| 392 | slotIndex: stickSlot |
| 393 | }); |
| 394 | harness.simulation.step(); |
| 395 | assert.equal( |
| 396 | harness.simulation.bagOf(playerId).equipment.slots[EQUIPMENT_SLOT.weapon]?.itemId, |
| 397 | ItemId.stick |
| 398 | ); |
| 399 | |
| 400 | const farRat = harness.simulation.spawnEntity(mobComponents({ |
| 401 | tileX: 12, |
| 402 | tileY: 8, |
| 403 | mobTypeId: MobId.rat |
| 404 | })); |
| 405 | harness.simulation.enqueue(1, { |
| 406 | type: "attack", |
| 407 | characterId: character.character_id, |
| 408 | targetEntityId: farRat |
| 409 | }); |
| 410 | const farEvents = harness.simulation.step().events; |
| 411 | assert.equal(farEvents.some((event) => event.type === "combat-swing"), false); |
| 412 | |
| 413 | const nearRat = harness.simulation.spawnEntity(mobComponents({ |
| 414 | tileX: 9, |
| 415 | tileY: 8, |
| 416 | mobTypeId: MobId.rat |
| 417 | })); |
| 418 | harness.simulation.enqueue(1, { |
| 419 | type: "attack", |
| 420 | characterId: character.character_id, |
| 421 | targetEntityId: nearRat |
| 422 | }); |
| 423 | const nearEvents = harness.simulation.step().events; |
| 424 | assert.ok(nearEvents.some((event) => event.type === "combat-swing")); |
| 425 | assert.ok(nearEvents.some((event) => |
| 426 | event.type === "combat-hit" |
| 427 | || event.type === "combat-miss" |
| 428 | || event.type === "combat-block")); |
| 429 | } |
| 430 | finally |
| 431 | { harness.close(); } |
| 432 | }); |
| 433 | |
| 434 | test("pigeons are untargetable free-flight and never get path tiles", () => |
| 435 | { |
| 436 | const harness = makeSimulation({ workerCount: 1 }); |
| 437 | try |
| 438 | { |
| 439 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 440 | harness.simulation.spawnCharacter(1, character); |
| 441 | const pigeonId = harness.simulation.spawnEntity(mobComponents({ |
| 442 | tileX: 10, |
| 443 | tileY: 8, |
| 444 | mobTypeId: MobId.pigeon |
| 445 | })); |
| 446 | |
| 447 | const before = componentsOf(harness.simulation, pigeonId); |
| 448 | assert.equal(before.flight != null, true); |
| 449 | assert.equal(before.combat == null, true); |
| 450 | assert.deepEqual(before.path?.tiles ?? [], []); |
| 451 | |
| 452 | harness.setNow(harness.now() + 500); |
| 453 | stepTimes(harness.simulation, 10); |
| 454 | const after = componentsOf(harness.simulation, pigeonId); |
| 455 | assert.deepEqual(after.path?.tiles ?? [], []); |
| 456 | assert.equal(after.flight != null, true); |
| 457 | |
| 458 | harness.simulation.enqueue(1, { |
| 459 | type: "attack", |
| 460 | characterId: character.character_id, |
| 461 | targetEntityId: pigeonId |
| 462 | }); |
| 463 | const result = harness.simulation.step(); |
| 464 | assert.ok(result.events.some((event) => event.type === "command-error")); |
| 465 | assert.equal(componentsOf(harness.simulation, pigeonId).path?.tiles?.length ?? 0, 0); |
| 466 | } |
| 467 | finally |
| 468 | { harness.close(); } |
| 469 | }); |
| 470 | |
| 471 | test("deer retaliate when attacked", () => |
| 472 | { |
| 473 | const harness = makeSimulation({ workerCount: 1 }); |
| 474 | try |
| 475 | { |
| 476 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 477 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 478 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 479 | tileX: 9, |
| 480 | tileY: 8, |
| 481 | mobTypeId: MobId.deer, |
| 482 | spawnerId: null |
| 483 | })); |
| 484 | |
| 485 | harness.simulation.enqueue(1, { |
| 486 | type: "attack", |
| 487 | characterId: character.character_id, |
| 488 | targetEntityId: deerId |
| 489 | }); |
| 490 | harness.simulation.step(); |
| 491 | |
| 492 | const deer = componentsOf(harness.simulation, deerId); |
| 493 | assert.equal(deer.combat?.targetEntityId, playerId); |
| 494 | assert.ok(deer.skills?.levels?.unarmed >= 1); |
| 495 | } |
| 496 | finally |
| 497 | { harness.close(); } |
| 498 | }); |
| 499 | |
| 500 | test("attacker keeps pursuing and can swing after the target moves", () => |
| 501 | { |
| 502 | const harness = makeSimulation({ workerCount: 1 }); |
| 503 | try |
| 504 | { |
| 505 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 506 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 507 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 508 | tileX: 12, |
| 509 | tileY: 8, |
| 510 | mobTypeId: MobId.rat, |
| 511 | spawnerId: null |
| 512 | })); |
| 513 | |
| 514 | harness.simulation.enqueue(1, { |
| 515 | type: "attack", |
| 516 | characterId: character.character_id, |
| 517 | targetEntityId: ratId |
| 518 | }); |
| 519 | harness.simulation.step(); |
| 520 | |
| 521 | const worker = workerOf(harness.simulation, playerId); |
| 522 | stepTimes(harness.simulation, 4); |
| 523 | teleportEntity(worker, ratId, 12, 10); |
| 524 | |
| 525 | let swings = 0; |
| 526 | for (let i = 0; i < 120; i++) |
| 527 | { |
| 528 | const events = harness.simulation.step().events; |
| 529 | swings += events.filter((event) => event.type === "combat-swing").length; |
| 530 | if (swings > 0) |
| 531 | { break; } |
| 532 | } |
| 533 | |
| 534 | assert.ok(swings >= 1); |
| 535 | assert.equal(componentsOf(harness.simulation, playerId).combat?.targetEntityId, ratId); |
| 536 | } |
| 537 | finally |
| 538 | { harness.close(); } |
| 539 | }); |
| 540 | |
| 541 | test("combatants keep auto-attacking while adjacent", () => |
| 542 | { |
| 543 | const harness = makeSimulation({ workerCount: 1 }); |
| 544 | try |
| 545 | { |
| 546 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 547 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 548 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 549 | tileX: 9, |
| 550 | tileY: 8, |
| 551 | mobTypeId: MobId.deer, |
| 552 | spawnerId: null |
| 553 | })); |
| 554 | |
| 555 | harness.simulation.enqueue(1, { |
| 556 | type: "attack", |
| 557 | characterId: character.character_id, |
| 558 | targetEntityId: deerId |
| 559 | }); |
| 560 | harness.simulation.step(); |
| 561 | |
| 562 | let swings = 0; |
| 563 | for (let i = 0; i < 50; i++) |
| 564 | { |
| 565 | harness.advance(50); |
| 566 | swings += harness.simulation.step().events.filter((event) => event.type === "combat-swing").length; |
| 567 | } |
| 568 | |
| 569 | assert.ok(swings >= 4, `expected repeated swings, got ${swings}`); |
| 570 | const player = componentsOf(harness.simulation, playerId); |
| 571 | const deer = componentsOf(harness.simulation, deerId); |
| 572 | assert.ok((player.health?.current ?? 0) > 0, "player should still be alive during sustained combat"); |
| 573 | assert.equal(player.combat?.targetEntityId, deerId); |
| 574 | assert.equal(deer.combat?.targetEntityId, playerId); |
| 575 | } |
| 576 | finally |
| 577 | { harness.close(); } |
| 578 | }); |
| 579 | |
| 580 | test("cross-worker adjacent combat keeps engaging and swinging", () => |
| 581 | { |
| 582 | const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); |
| 583 | try |
| 584 | { |
| 585 | const character = makeCharacter(harness.stateDb, { tileX: 64, tileY: 8 }); |
| 586 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 587 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 588 | tileX: 63, |
| 589 | tileY: 8, |
| 590 | mobTypeId: MobId.deer, |
| 591 | spawnerId: null |
| 592 | })); |
| 593 | assert.equal(harness.simulation.entityRecord(playerId).workerId, 1); |
| 594 | assert.equal(harness.simulation.entityRecord(deerId).workerId, 0); |
| 595 | |
| 596 | harness.simulation.enqueue(1, { |
| 597 | type: "attack", |
| 598 | characterId: character.character_id, |
| 599 | targetEntityId: deerId |
| 600 | }); |
| 601 | const first = harness.simulation.step(); |
| 602 | assert.equal(first.events.some((event) => event.type === "command-error"), false); |
| 603 | |
| 604 | const random = Math.random; |
| 605 | Math.random = () => 0; |
| 606 | try |
| 607 | { |
| 608 | let swings = 0; |
| 609 | for (let i = 0; i < 5; i++) |
| 610 | { |
| 611 | harness.advance(50); |
| 612 | swings += harness.simulation.step().events.filter((event) => event.type === "combat-swing").length; |
| 613 | } |
| 614 | |
| 615 | assert.ok(swings >= 4, `expected cross-worker sustained swings, got ${swings}`); |
| 616 | const deerWorker = harness.simulation.workers[0]; |
| 617 | const playerWorker = harness.simulation.workers[1]; |
| 618 | const deerHandle = deerWorker.world.ecs.handleOf(deerId); |
| 619 | const playerHandle = playerWorker.world.ecs.handleOf(playerId); |
| 620 | const deerCombat = deerWorker.world.ecs.get(deerHandle, Component.combat); |
| 621 | const playerCombat = playerWorker.world.ecs.get(playerHandle, Component.combat); |
| 622 | const playerHealth = playerWorker.world.ecs.get(playerHandle, Component.health); |
| 623 | assert.ok((playerHealth?.current ?? 0) > 0, "player should survive sustained cross-worker combat"); |
| 624 | assert.equal(deerCombat?.targetEntityId, playerId); |
| 625 | assert.equal(playerCombat?.targetEntityId, deerId); |
| 626 | } |
| 627 | finally |
| 628 | { Math.random = random; } |
| 629 | } |
| 630 | finally |
| 631 | { harness.close(); } |
| 632 | }); |
| 633 | |
| 634 | test("cancel attack stops the player but the mob keeps pursuing", () => |
| 635 | { |
| 636 | const harness = makeSimulation({ workerCount: 1 }); |
| 637 | try |
| 638 | { |
| 639 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 640 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 641 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 642 | tileX: 9, |
| 643 | tileY: 8, |
| 644 | mobTypeId: MobId.rat, |
| 645 | spawnerId: null |
| 646 | })); |
| 647 | |
| 648 | harness.simulation.enqueue(1, { |
| 649 | type: "attack", |
| 650 | characterId: character.character_id, |
| 651 | targetEntityId: ratId |
| 652 | }); |
| 653 | harness.simulation.step(); |
| 654 | |
| 655 | harness.simulation.enqueue(1, { |
| 656 | type: "cancel-attack", |
| 657 | characterId: character.character_id |
| 658 | }); |
| 659 | harness.simulation.step(); |
| 660 | |
| 661 | const player = componentsOf(harness.simulation, playerId); |
| 662 | const rat = componentsOf(harness.simulation, ratId); |
| 663 | assert.equal(player.combat?.targetEntityId, null); |
| 664 | assert.equal(rat.combat?.targetEntityId, playerId); |
| 665 | } |
| 666 | finally |
| 667 | { harness.close(); } |
| 668 | }); |
| 669 | |
| 670 | test("player combat and skills components are present on spawn", () => |
| 671 | { |
| 672 | const harness = makeSimulation({ workerCount: 1 }); |
| 673 | try |
| 674 | { |
| 675 | const character = makeCharacter(harness.stateDb); |
| 676 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 677 | const comps = componentsOf(harness.simulation, playerId); |
| 678 | assert.ok(comps.combat); |
| 679 | assert.equal(comps.skills?.xp?.clubfighting, 0); |
| 680 | assert.ok(comps.health); |
| 681 | } |
| 682 | finally |
| 683 | { harness.close(); } |
| 684 | }); |
| 685 | |
| 686 | test("mobs heal to full when their region unloads", () => |
| 687 | { |
| 688 | const harness = makeSimulation({ workerCount: 1 }); |
| 689 | try |
| 690 | { |
| 691 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 692 | tileX: 8, |
| 693 | tileY: 8, |
| 694 | mobTypeId: MobId.rat |
| 695 | })); |
| 696 | const worker = workerOf(harness.simulation, ratId); |
| 697 | const handle = worker.world.ecs.handleOf(ratId); |
| 698 | worker.world.ecs.get(handle, Component.health).current = 2; |
| 699 | const result = worker.call({ |
| 700 | type: WorkerMessage.dropRegion, |
| 701 | regionX: tileToRegion(8), |
| 702 | regionY: tileToRegion(8) |
| 703 | }); |
| 704 | const record = result.dropped.find((entry) => entry.entityId === ratId); |
| 705 | assert.ok(record); |
| 706 | assert.equal( |
| 707 | record.payload.components.health.current, |
| 708 | record.payload.components.health.max |
| 709 | ); |
| 710 | } |
| 711 | finally |
| 712 | { harness.close(); } |
| 713 | }); |
| 714 | |
| 715 | test("taking damage awards Health XP equal to damage dealt", () => |
| 716 | { |
| 717 | const harness = makeSimulation({ workerCount: 1 }); |
| 718 | const random = Math.random; |
| 719 | Math.random = () => 0; |
| 720 | try |
| 721 | { |
| 722 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 723 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 724 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 725 | mobTypeId: MobId.rat, |
| 726 | tileX: 9, |
| 727 | tileY: 8 |
| 728 | })); |
| 729 | const worker = harness.simulation.workers[0]; |
| 730 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 731 | const before = worker.world.ecs.get(playerHandle, Component.skills).xp[SkillId.health]; |
| 732 | const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId)); |
| 733 | const events = resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now()); |
| 734 | const hit = events.find((event) => event.type === "combat-hit"); |
| 735 | assert.ok(hit); |
| 736 | const updated = events.find((event) => event.type === "skills-updated" && event.entityId === playerId); |
| 737 | assert.ok(updated); |
| 738 | assert.equal(updated.stored[SkillId.health] - before, hit.damage); |
| 739 | } |
| 740 | finally |
| 741 | { |
| 742 | Math.random = random; |
| 743 | harness.close(); |
| 744 | } |
| 745 | }); |