| 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 { BlockType, blockBlocksRangedAttacks } from "../../shared/definitions/tiles.js"; |
| 7 | import { Component } from "../src/ecs/components.js"; |
| 8 | import { tileKey } from "../src/world/coordinates.js"; |
| 9 | import { WorkerMessage } from "../src/simulation/protocol.js"; |
| 10 | import { tileToRegion } from "../src/world/coordinates.js"; |
| 11 | import { SkillId } from "../../shared/definitions/skills.js"; |
| 12 | import { totalXpForLevel } from "../../shared/skills/progression.js"; |
| 13 | import { resolveMeleeSwing, attackProfileOf, hasCombatLineOfSight } from "../src/combat/service.js"; |
| 14 | import { makeCharacter, makeSimulation, pinEastSplit, stepTimes } from "./helpers/simulationHarness.js"; |
| 15 | |
| 16 | function workerOf (simulation, entityId) |
| 17 | { |
| 18 | const record = simulation.entityRecord(entityId); |
| 19 | assert.ok(record, "entity must be remembered"); |
| 20 | return simulation.workers[record.workerId]; |
| 21 | } |
| 22 | |
| 23 | function componentsOf (simulation, entityId) |
| 24 | { |
| 25 | const worker = workerOf(simulation, entityId); |
| 26 | const handle = worker.world.ecs.handleOf(entityId); |
| 27 | assert.ok(handle != null); |
| 28 | return worker.world.ecs.componentsOf(handle); |
| 29 | } |
| 30 | |
| 31 | function teleportEntity (worker, entityId, tileX, tileY) |
| 32 | { |
| 33 | const world = worker.world; |
| 34 | const handle = world.ecs.handleOf(entityId); |
| 35 | const pos = world.ecs.get(handle, Component.position); |
| 36 | const fromKey = tileKey(pos.tileX, pos.tileY); |
| 37 | const fromSet = world.occupancy.get(fromKey); |
| 38 | if (fromSet) |
| 39 | { |
| 40 | fromSet.delete(entityId); |
| 41 | if (fromSet.size === 0) |
| 42 | { world.occupancy.delete(fromKey); } |
| 43 | } |
| 44 | pos.tileX = tileX; |
| 45 | pos.tileY = tileY; |
| 46 | const toKey = tileKey(tileX, tileY); |
| 47 | let toSet = world.occupancy.get(toKey); |
| 48 | if (!toSet) |
| 49 | { |
| 50 | toSet = new Set(); |
| 51 | world.occupancy.set(toKey, toSet); |
| 52 | } |
| 53 | toSet.add(entityId); |
| 54 | } |
| 55 | |
| 56 | function entityIsAlive (simulation, entityId) |
| 57 | { |
| 58 | const record = simulation.entityRecord(entityId); |
| 59 | if (!record) |
| 60 | { return false; } |
| 61 | const worker = simulation.workers[record.workerId]; |
| 62 | const handle = worker.world.ecs.handleOf(entityId); |
| 63 | if (handle == null) |
| 64 | { return false; } |
| 65 | const health = worker.world.ecs.get(handle, Component.health); |
| 66 | return (health?.current ?? 0) > 0; |
| 67 | } |
| 68 | |
| 69 | function simulateAttackClick (harness, character, targetEntityId) |
| 70 | { |
| 71 | harness.simulation.enqueue(1, { |
| 72 | type: "attack", |
| 73 | characterId: character.character_id, |
| 74 | targetEntityId |
| 75 | }); |
| 76 | } |
| 77 | |
| 78 | /** Run ticks until one combatant dies or the step budget is exhausted. */ |
| 79 | function runCombatUntilOutcome (harness, playerId, deerId, maxSteps = 600) |
| 80 | { |
| 81 | let swings = 0; |
| 82 | for (let i = 0; i < maxSteps; i++) |
| 83 | { |
| 84 | harness.advance(50); |
| 85 | const events = harness.simulation.step().events; |
| 86 | swings += events.filter((event) => event.type === "combat-swing").length; |
| 87 | const playerAlive = entityIsAlive(harness.simulation, playerId); |
| 88 | const deerAlive = entityIsAlive(harness.simulation, deerId); |
| 89 | if (!playerAlive || !deerAlive) |
| 90 | { return { playerAlive, deerAlive, swings, steps: i + 1 }; } |
| 91 | } |
| 92 | return { |
| 93 | playerAlive: entityIsAlive(harness.simulation, playerId), |
| 94 | deerAlive: entityIsAlive(harness.simulation, deerId), |
| 95 | swings, |
| 96 | steps: maxSteps |
| 97 | }; |
| 98 | } |
| 99 | |
| 100 | function withDeterministicCombat (fn) |
| 101 | { |
| 102 | const random = Math.random; |
| 103 | Math.random = () => 0; |
| 104 | try |
| 105 | { fn(); } |
| 106 | finally |
| 107 | { Math.random = random; } |
| 108 | } |
| 109 | |
| 110 | test("immortal deer auto-attacks until the player dies", () => |
| 111 | { |
| 112 | withDeterministicCombat(() => |
| 113 | { |
| 114 | const harness = makeSimulation({ workerCount: 1 }); |
| 115 | try |
| 116 | { |
| 117 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 118 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 119 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 120 | tileX: 9, |
| 121 | tileY: 8, |
| 122 | mobTypeId: MobId.deer, |
| 123 | spawnerId: null |
| 124 | })); |
| 125 | const deer = componentsOf(harness.simulation, deerId); |
| 126 | deer.health.current = 1_000_000; |
| 127 | deer.health.max = 1_000_000; |
| 128 | |
| 129 | simulateAttackClick(harness, character, deerId); |
| 130 | harness.simulation.step(); |
| 131 | |
| 132 | const outcome = runCombatUntilOutcome(harness, playerId, deerId, 2_000); |
| 133 | assert.ok(outcome.swings >= 2, `expected sustained auto-attack swings, got ${outcome.swings}`); |
| 134 | assert.equal(outcome.deerAlive, true, "immortal deer should survive"); |
| 135 | assert.equal(outcome.playerAlive, false, "deer should kill the player via auto-attack"); |
| 136 | assert.equal( |
| 137 | componentsOf(harness.simulation, deerId).combat?.targetEntityId ?? null, |
| 138 | null, |
| 139 | "deer should clear engage after the player dies" |
| 140 | ); |
| 141 | } |
| 142 | finally |
| 143 | { harness.close(); } |
| 144 | }); |
| 145 | }); |
| 146 | |
| 147 | test("immortal deer keeps auto-attacking after the player walks away", () => |
| 148 | { |
| 149 | withDeterministicCombat(() => |
| 150 | { |
| 151 | const harness = makeSimulation({ workerCount: 1 }); |
| 152 | try |
| 153 | { |
| 154 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 155 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 156 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 157 | tileX: 9, |
| 158 | tileY: 8, |
| 159 | mobTypeId: MobId.deer, |
| 160 | spawnerId: null |
| 161 | })); |
| 162 | const deer = componentsOf(harness.simulation, deerId); |
| 163 | deer.health.current = 1_000_000; |
| 164 | deer.health.max = 1_000_000; |
| 165 | |
| 166 | simulateAttackClick(harness, character, deerId); |
| 167 | harness.simulation.step(); |
| 168 | |
| 169 | // Let one exchange land, then cancel player engage by walking away (client moveTo). |
| 170 | for (let i = 0; i < 30; i++) |
| 171 | { |
| 172 | harness.advance(50); |
| 173 | const swings = harness.simulation.step().events.filter((event) => event.type === "combat-swing").length; |
| 174 | if (swings > 0) |
| 175 | { break; } |
| 176 | } |
| 177 | harness.simulation.enqueue(1, { |
| 178 | type: "move-to", |
| 179 | characterId: character.character_id, |
| 180 | tileX: 8, |
| 181 | tileY: 14 |
| 182 | }); |
| 183 | harness.simulation.step(); |
| 184 | assert.equal(componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null, null); |
| 185 | assert.equal(componentsOf(harness.simulation, deerId).combat?.targetEntityId, playerId); |
| 186 | |
| 187 | const outcome = runCombatUntilOutcome(harness, playerId, deerId, 2_000); |
| 188 | assert.ok(outcome.swings >= 1, `expected deer pursuit swings, got ${outcome.swings}`); |
| 189 | assert.equal(outcome.deerAlive, true); |
| 190 | assert.equal(outcome.playerAlive, false, "deer should chase and kill the player after walk-away"); |
| 191 | } |
| 192 | finally |
| 193 | { harness.close(); } |
| 194 | }); |
| 195 | }); |
| 196 | |
| 197 | test("cross-worker immortal deer auto-attacks until the player dies", () => |
| 198 | { |
| 199 | withDeterministicCombat(() => |
| 200 | { |
| 201 | const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); |
| 202 | try |
| 203 | { |
| 204 | const character = makeCharacter(harness.stateDb, { tileX: 64, tileY: 8 }); |
| 205 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 206 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 207 | tileX: 63, |
| 208 | tileY: 8, |
| 209 | mobTypeId: MobId.deer, |
| 210 | spawnerId: null |
| 211 | })); |
| 212 | assert.equal(harness.simulation.entityRecord(playerId).workerId, 1); |
| 213 | assert.equal(harness.simulation.entityRecord(deerId).workerId, 0); |
| 214 | |
| 215 | const deerWorker = workerOf(harness.simulation, deerId); |
| 216 | const deerHandle = deerWorker.world.ecs.handleOf(deerId); |
| 217 | const deerHealth = deerWorker.world.ecs.get(deerHandle, Component.health); |
| 218 | deerHealth.current = 1_000_000; |
| 219 | deerHealth.max = 1_000_000; |
| 220 | |
| 221 | simulateAttackClick(harness, character, deerId); |
| 222 | const first = harness.simulation.step(); |
| 223 | assert.equal(first.events.some((event) => event.type === "command-error"), false); |
| 224 | |
| 225 | const outcome = runCombatUntilOutcome(harness, playerId, deerId, 2_000); |
| 226 | assert.ok(outcome.swings >= 2, `expected sustained cross-worker swings, got ${outcome.swings}`); |
| 227 | assert.equal(outcome.deerAlive, true); |
| 228 | assert.equal(outcome.playerAlive, false, "cross-worker deer should kill the player via auto-attack"); |
| 229 | } |
| 230 | finally |
| 231 | { harness.close(); } |
| 232 | }); |
| 233 | }); |
| 234 | |
| 235 | test("click attack from 5 tiles away ends with player or deer dead", () => |
| 236 | { |
| 237 | withDeterministicCombat(() => |
| 238 | { |
| 239 | const harness = makeSimulation({ workerCount: 1 }); |
| 240 | try |
| 241 | { |
| 242 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 243 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 244 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 245 | tileX: 13, |
| 246 | tileY: 8, |
| 247 | mobTypeId: MobId.deer, |
| 248 | spawnerId: null |
| 249 | })); |
| 250 | |
| 251 | simulateAttackClick(harness, character, deerId); |
| 252 | harness.simulation.step(); |
| 253 | |
| 254 | const outcome = runCombatUntilOutcome(harness, playerId, deerId); |
| 255 | assert.ok(outcome.swings >= 1, `expected at least one swing, got ${outcome.swings}`); |
| 256 | assert.ok( |
| 257 | !outcome.playerAlive || !outcome.deerAlive, |
| 258 | `expected a kill within ${outcome.steps} steps (${outcome.swings} swings); both still alive` |
| 259 | ); |
| 260 | } |
| 261 | finally |
| 262 | { harness.close(); } |
| 263 | }); |
| 264 | }); |
| 265 | |
| 266 | test("click attack while adjacent ends with player or deer dead", () => |
| 267 | { |
| 268 | withDeterministicCombat(() => |
| 269 | { |
| 270 | const harness = makeSimulation({ workerCount: 1 }); |
| 271 | try |
| 272 | { |
| 273 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 274 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 275 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 276 | tileX: 9, |
| 277 | tileY: 8, |
| 278 | mobTypeId: MobId.deer, |
| 279 | spawnerId: null |
| 280 | })); |
| 281 | |
| 282 | simulateAttackClick(harness, character, deerId); |
| 283 | harness.simulation.step(); |
| 284 | |
| 285 | const outcome = runCombatUntilOutcome(harness, playerId, deerId); |
| 286 | assert.ok(outcome.swings >= 1, `expected at least one swing, got ${outcome.swings}`); |
| 287 | assert.ok( |
| 288 | !outcome.playerAlive || !outcome.deerAlive, |
| 289 | `expected a kill within ${outcome.steps} steps (${outcome.swings} swings); both still alive` |
| 290 | ); |
| 291 | } |
| 292 | finally |
| 293 | { harness.close(); } |
| 294 | }); |
| 295 | }); |
| 296 | |
| 297 | test("cross-worker click attack from 5 tiles away ends with player or deer dead", () => |
| 298 | { |
| 299 | withDeterministicCombat(() => |
| 300 | { |
| 301 | const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); |
| 302 | try |
| 303 | { |
| 304 | const character = makeCharacter(harness.stateDb, { tileX: 59, tileY: 8 }); |
| 305 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 306 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 307 | tileX: 64, |
| 308 | tileY: 8, |
| 309 | mobTypeId: MobId.deer, |
| 310 | spawnerId: null |
| 311 | })); |
| 312 | assert.equal(harness.simulation.entityRecord(playerId).workerId, 0); |
| 313 | assert.equal(harness.simulation.entityRecord(deerId).workerId, 1); |
| 314 | |
| 315 | simulateAttackClick(harness, character, deerId); |
| 316 | const first = harness.simulation.step(); |
| 317 | assert.equal(first.events.some((event) => event.type === "command-error"), false); |
| 318 | |
| 319 | const outcome = runCombatUntilOutcome(harness, playerId, deerId); |
| 320 | assert.ok(outcome.swings >= 1, `expected at least one swing, got ${outcome.swings}`); |
| 321 | assert.ok( |
| 322 | !outcome.playerAlive || !outcome.deerAlive, |
| 323 | `expected a kill within ${outcome.steps} steps (${outcome.swings} swings); both still alive` |
| 324 | ); |
| 325 | } |
| 326 | finally |
| 327 | { harness.close(); } |
| 328 | }); |
| 329 | }); |
| 330 | |
| 331 | test("cross-worker adjacent click attack ends with player or deer dead", () => |
| 332 | { |
| 333 | withDeterministicCombat(() => |
| 334 | { |
| 335 | const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); |
| 336 | try |
| 337 | { |
| 338 | const character = makeCharacter(harness.stateDb, { tileX: 64, tileY: 8 }); |
| 339 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 340 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 341 | tileX: 63, |
| 342 | tileY: 8, |
| 343 | mobTypeId: MobId.deer, |
| 344 | spawnerId: null |
| 345 | })); |
| 346 | assert.equal(harness.simulation.entityRecord(playerId).workerId, 1); |
| 347 | assert.equal(harness.simulation.entityRecord(deerId).workerId, 0); |
| 348 | |
| 349 | simulateAttackClick(harness, character, deerId); |
| 350 | const first = harness.simulation.step(); |
| 351 | assert.equal(first.events.some((event) => event.type === "command-error"), false); |
| 352 | |
| 353 | const outcome = runCombatUntilOutcome(harness, playerId, deerId); |
| 354 | assert.ok(outcome.swings >= 1, `expected at least one swing, got ${outcome.swings}`); |
| 355 | assert.ok( |
| 356 | !outcome.playerAlive || !outcome.deerAlive, |
| 357 | `expected a kill within ${outcome.steps} steps (${outcome.swings} swings); both still alive` |
| 358 | ); |
| 359 | } |
| 360 | finally |
| 361 | { harness.close(); } |
| 362 | }); |
| 363 | }); |
| 364 | |
| 365 | test("attack engages the attacker; target retaliates on first swing", () => |
| 366 | { |
| 367 | const harness = makeSimulation({ workerCount: 1 }); |
| 368 | try |
| 369 | { |
| 370 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 371 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 372 | // Deer are not aggressive — only retaliate after a real swing. |
| 373 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 374 | tileX: 12, |
| 375 | tileY: 8, |
| 376 | mobTypeId: MobId.deer, |
| 377 | spawnerId: null |
| 378 | })); |
| 379 | |
| 380 | harness.simulation.enqueue(1, { |
| 381 | type: "attack", |
| 382 | characterId: character.character_id, |
| 383 | targetEntityId: deerId |
| 384 | }); |
| 385 | harness.simulation.step(); |
| 386 | |
| 387 | assert.equal(componentsOf(harness.simulation, playerId).combat.targetEntityId, deerId); |
| 388 | assert.equal(componentsOf(harness.simulation, deerId).combat?.targetEntityId ?? null, null); |
| 389 | |
| 390 | let swung = false; |
| 391 | for (let i = 0; i < 80; i++) |
| 392 | { |
| 393 | harness.advance(50); |
| 394 | const events = harness.simulation.step().events; |
| 395 | if (events.some((event) => event.type === "combat-swing")) |
| 396 | { |
| 397 | swung = true; |
| 398 | break; |
| 399 | } |
| 400 | } |
| 401 | assert.ok(swung, "expected a melee swing once adjacent"); |
| 402 | assert.equal(componentsOf(harness.simulation, deerId).combat?.targetEntityId, playerId); |
| 403 | } |
| 404 | finally |
| 405 | { harness.close(); } |
| 406 | }); |
| 407 | |
| 408 | test("melee swing only resolves at Chebyshev distance 1", () => |
| 409 | { |
| 410 | const harness = makeSimulation({ workerCount: 1 }); |
| 411 | try |
| 412 | { |
| 413 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 414 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 415 | const bag = harness.simulation.bagOf(playerId); |
| 416 | const stickSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.stick); |
| 417 | assert.ok(stickSlot >= 0); |
| 418 | harness.simulation.enqueue(1, { |
| 419 | type: "equip-item", |
| 420 | characterId: character.character_id, |
| 421 | slotIndex: stickSlot |
| 422 | }); |
| 423 | harness.simulation.step(); |
| 424 | assert.equal( |
| 425 | harness.simulation.bagOf(playerId).equipment.slots[EQUIPMENT_SLOT.weapon]?.itemId, |
| 426 | ItemId.stick |
| 427 | ); |
| 428 | |
| 429 | const farRat = harness.simulation.spawnEntity(mobComponents({ |
| 430 | tileX: 12, |
| 431 | tileY: 8, |
| 432 | mobTypeId: MobId.rat |
| 433 | })); |
| 434 | harness.simulation.enqueue(1, { |
| 435 | type: "attack", |
| 436 | characterId: character.character_id, |
| 437 | targetEntityId: farRat |
| 438 | }); |
| 439 | const farEvents = harness.simulation.step().events; |
| 440 | assert.equal(farEvents.some((event) => event.type === "combat-swing"), false); |
| 441 | |
| 442 | const nearRat = harness.simulation.spawnEntity(mobComponents({ |
| 443 | tileX: 9, |
| 444 | tileY: 8, |
| 445 | mobTypeId: MobId.rat |
| 446 | })); |
| 447 | harness.simulation.enqueue(1, { |
| 448 | type: "attack", |
| 449 | characterId: character.character_id, |
| 450 | targetEntityId: nearRat |
| 451 | }); |
| 452 | const nearEvents = harness.simulation.step().events; |
| 453 | assert.ok(nearEvents.some((event) => event.type === "combat-swing")); |
| 454 | assert.ok(nearEvents.some((event) => |
| 455 | event.type === "combat-hit" |
| 456 | || event.type === "combat-miss" |
| 457 | || event.type === "combat-block")); |
| 458 | } |
| 459 | finally |
| 460 | { harness.close(); } |
| 461 | }); |
| 462 | |
| 463 | test("pigeons are untargetable free-flight and never get path tiles", () => |
| 464 | { |
| 465 | const harness = makeSimulation({ workerCount: 1 }); |
| 466 | try |
| 467 | { |
| 468 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 469 | harness.simulation.spawnCharacter(1, character); |
| 470 | const pigeonId = harness.simulation.spawnEntity(mobComponents({ |
| 471 | tileX: 10, |
| 472 | tileY: 8, |
| 473 | mobTypeId: MobId.pigeon |
| 474 | })); |
| 475 | |
| 476 | const before = componentsOf(harness.simulation, pigeonId); |
| 477 | assert.equal(before.flight != null, true); |
| 478 | assert.equal(before.combat == null, true); |
| 479 | assert.deepEqual(before.path?.tiles ?? [], []); |
| 480 | |
| 481 | harness.setNow(harness.now() + 500); |
| 482 | stepTimes(harness.simulation, 10); |
| 483 | const after = componentsOf(harness.simulation, pigeonId); |
| 484 | assert.deepEqual(after.path?.tiles ?? [], []); |
| 485 | assert.equal(after.flight != null, true); |
| 486 | |
| 487 | harness.simulation.enqueue(1, { |
| 488 | type: "attack", |
| 489 | characterId: character.character_id, |
| 490 | targetEntityId: pigeonId |
| 491 | }); |
| 492 | const result = harness.simulation.step(); |
| 493 | assert.ok(result.events.some((event) => event.type === "command-error")); |
| 494 | assert.equal(componentsOf(harness.simulation, pigeonId).path?.tiles?.length ?? 0, 0); |
| 495 | } |
| 496 | finally |
| 497 | { harness.close(); } |
| 498 | }); |
| 499 | |
| 500 | test("deer retaliate when attacked", () => |
| 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 deerId = harness.simulation.spawnEntity(mobComponents({ |
| 508 | tileX: 9, |
| 509 | tileY: 8, |
| 510 | mobTypeId: MobId.deer, |
| 511 | spawnerId: null |
| 512 | })); |
| 513 | |
| 514 | harness.simulation.enqueue(1, { |
| 515 | type: "attack", |
| 516 | characterId: character.character_id, |
| 517 | targetEntityId: deerId |
| 518 | }); |
| 519 | const events = harness.simulation.step().events; |
| 520 | assert.ok(events.some((event) => event.type === "combat-swing")); |
| 521 | |
| 522 | const deer = componentsOf(harness.simulation, deerId); |
| 523 | assert.equal(deer.combat?.targetEntityId, playerId); |
| 524 | assert.ok(deer.skills?.levels?.unarmed >= 1); |
| 525 | } |
| 526 | finally |
| 527 | { harness.close(); } |
| 528 | }); |
| 529 | |
| 530 | test("aggressive rats engage players within aggro range and LOS", () => |
| 531 | { |
| 532 | const harness = makeSimulation({ workerCount: 1 }); |
| 533 | try |
| 534 | { |
| 535 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 536 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 537 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 538 | tileX: 12, |
| 539 | tileY: 8, |
| 540 | mobTypeId: MobId.rat, |
| 541 | spawnerId: null |
| 542 | })); |
| 543 | |
| 544 | harness.simulation.step(); |
| 545 | assert.equal(componentsOf(harness.simulation, ratId).combat?.targetEntityId, playerId); |
| 546 | } |
| 547 | finally |
| 548 | { harness.close(); } |
| 549 | }); |
| 550 | |
| 551 | test("players retaliate when a mob swings at them", () => |
| 552 | { |
| 553 | const harness = makeSimulation({ workerCount: 1 }); |
| 554 | try |
| 555 | { |
| 556 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 557 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 558 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 559 | tileX: 9, |
| 560 | tileY: 8, |
| 561 | mobTypeId: MobId.rat, |
| 562 | spawnerId: null |
| 563 | })); |
| 564 | |
| 565 | assert.equal(componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null, null); |
| 566 | |
| 567 | let swung = false; |
| 568 | for (let i = 0; i < 80; i++) |
| 569 | { |
| 570 | harness.advance(50); |
| 571 | const events = harness.simulation.step().events; |
| 572 | if (events.some((event) => |
| 573 | event.type === "combat-swing" && event.attackerId === ratId && event.targetId === playerId |
| 574 | )) |
| 575 | { |
| 576 | swung = true; |
| 577 | break; |
| 578 | } |
| 579 | } |
| 580 | assert.ok(swung, "expected the rat to swing at the player"); |
| 581 | assert.equal( |
| 582 | componentsOf(harness.simulation, playerId).combat?.targetEntityId, |
| 583 | ratId, |
| 584 | "player should auto-retaliate after the mob swing" |
| 585 | ); |
| 586 | } |
| 587 | finally |
| 588 | { harness.close(); } |
| 589 | }); |
| 590 | |
| 591 | test("aggressive rats do not aggro through walls or beyond range", () => |
| 592 | { |
| 593 | const harness = makeSimulation({ workerCount: 1 }); |
| 594 | try |
| 595 | { |
| 596 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 597 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 598 | const record = harness.simulation.entityRecord(playerId); |
| 599 | const worker = harness.simulation.workers[record.workerId]; |
| 600 | |
| 601 | const blockedRat = harness.simulation.spawnEntity(mobComponents({ |
| 602 | tileX: 12, |
| 603 | tileY: 8, |
| 604 | mobTypeId: MobId.rat, |
| 605 | spawnerId: null |
| 606 | })); |
| 607 | const data = worker.world.chunkData(0, 0); |
| 608 | data.blocks[8 * 16 + 10] = { typeId: BlockType.stoneWall, state: null, config: null }; |
| 609 | |
| 610 | const farRat = harness.simulation.spawnEntity(mobComponents({ |
| 611 | tileX: 14, |
| 612 | tileY: 8, |
| 613 | mobTypeId: MobId.rat, |
| 614 | spawnerId: null |
| 615 | })); |
| 616 | |
| 617 | harness.simulation.step(); |
| 618 | assert.equal(componentsOf(harness.simulation, blockedRat).combat?.targetEntityId ?? null, null); |
| 619 | assert.equal(componentsOf(harness.simulation, farRat).combat?.targetEntityId ?? null, null); |
| 620 | } |
| 621 | finally |
| 622 | { harness.close(); } |
| 623 | }); |
| 624 | |
| 625 | test("crafting stations and tables do not block ranged or spell line of sight", () => |
| 626 | { |
| 627 | assert.equal(blockBlocksRangedAttacks(BlockType.stoneWall), true); |
| 628 | assert.equal(blockBlocksRangedAttacks(BlockType.furnace), false); |
| 629 | assert.equal(blockBlocksRangedAttacks(BlockType.table), false); |
| 630 | assert.equal(blockBlocksRangedAttacks(null), false); |
| 631 | |
| 632 | const harness = makeSimulation({ workerCount: 1 }); |
| 633 | try |
| 634 | { |
| 635 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 636 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 637 | const worker = workerOf(harness.simulation, playerId); |
| 638 | const data = worker.world.chunkData(0, 0); |
| 639 | const blockIndex = (localY, localX) => localY * 16 + localX; |
| 640 | |
| 641 | data.blocks[blockIndex(8, 10)] = { typeId: BlockType.furnace, state: null, config: null }; |
| 642 | assert.equal( |
| 643 | hasCombatLineOfSight(worker.world, 8, 8, 12, 8), |
| 644 | true, |
| 645 | "furnace between caster and target should not block" |
| 646 | ); |
| 647 | |
| 648 | data.blocks[blockIndex(8, 10)] = { typeId: BlockType.table, state: null, config: null }; |
| 649 | assert.equal( |
| 650 | hasCombatLineOfSight(worker.world, 8, 8, 12, 8), |
| 651 | true, |
| 652 | "table between caster and target should not block" |
| 653 | ); |
| 654 | |
| 655 | data.blocks[blockIndex(8, 10)] = { typeId: BlockType.stoneWall, state: null, config: null }; |
| 656 | assert.equal( |
| 657 | hasCombatLineOfSight(worker.world, 8, 8, 12, 8), |
| 658 | false, |
| 659 | "stone wall should still block" |
| 660 | ); |
| 661 | } |
| 662 | finally |
| 663 | { harness.close(); } |
| 664 | }); |
| 665 | |
| 666 | test("attacker keeps pursuing and can swing after the target moves", () => |
| 667 | { |
| 668 | const harness = makeSimulation({ workerCount: 1 }); |
| 669 | try |
| 670 | { |
| 671 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 672 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 673 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 674 | tileX: 12, |
| 675 | tileY: 8, |
| 676 | mobTypeId: MobId.rat, |
| 677 | spawnerId: null |
| 678 | })); |
| 679 | |
| 680 | harness.simulation.enqueue(1, { |
| 681 | type: "attack", |
| 682 | characterId: character.character_id, |
| 683 | targetEntityId: ratId |
| 684 | }); |
| 685 | harness.simulation.step(); |
| 686 | |
| 687 | const worker = workerOf(harness.simulation, playerId); |
| 688 | stepTimes(harness.simulation, 4); |
| 689 | teleportEntity(worker, ratId, 12, 10); |
| 690 | |
| 691 | let swings = 0; |
| 692 | for (let i = 0; i < 120; i++) |
| 693 | { |
| 694 | const events = harness.simulation.step().events; |
| 695 | swings += events.filter((event) => event.type === "combat-swing").length; |
| 696 | if (swings > 0) |
| 697 | { break; } |
| 698 | } |
| 699 | |
| 700 | assert.ok(swings >= 1); |
| 701 | assert.equal(componentsOf(harness.simulation, playerId).combat?.targetEntityId, ratId); |
| 702 | } |
| 703 | finally |
| 704 | { harness.close(); } |
| 705 | }); |
| 706 | |
| 707 | test("combatants keep auto-attacking while adjacent", () => |
| 708 | { |
| 709 | const harness = makeSimulation({ workerCount: 1 }); |
| 710 | try |
| 711 | { |
| 712 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 713 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 714 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 715 | tileX: 9, |
| 716 | tileY: 8, |
| 717 | mobTypeId: MobId.deer, |
| 718 | spawnerId: null |
| 719 | })); |
| 720 | |
| 721 | harness.simulation.enqueue(1, { |
| 722 | type: "attack", |
| 723 | characterId: character.character_id, |
| 724 | targetEntityId: deerId |
| 725 | }); |
| 726 | harness.simulation.step(); |
| 727 | |
| 728 | let swings = 0; |
| 729 | for (let i = 0; i < 50; i++) |
| 730 | { |
| 731 | harness.advance(50); |
| 732 | const events = harness.simulation.step().events; |
| 733 | swings += events.filter((event) => event.type === "combat-swing").length; |
| 734 | const player = componentsOf(harness.simulation, playerId); |
| 735 | const deer = componentsOf(harness.simulation, deerId); |
| 736 | if (swings >= 4 |
| 737 | && player.combat?.targetEntityId === deerId |
| 738 | && deer.combat?.targetEntityId === playerId |
| 739 | && (player.health?.current ?? 0) > 0) |
| 740 | { break; } |
| 741 | } |
| 742 | |
| 743 | assert.ok(swings >= 4, `expected repeated swings, got ${swings}`); |
| 744 | const player = componentsOf(harness.simulation, playerId); |
| 745 | const deer = componentsOf(harness.simulation, deerId); |
| 746 | assert.ok((player.health?.current ?? 0) > 0, "player should still be alive during sustained combat"); |
| 747 | assert.equal(player.combat?.targetEntityId, deerId); |
| 748 | assert.equal(deer.combat?.targetEntityId, playerId); |
| 749 | } |
| 750 | finally |
| 751 | { harness.close(); } |
| 752 | }); |
| 753 | |
| 754 | test("cross-worker adjacent combat keeps engaging and swinging", () => |
| 755 | { |
| 756 | const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); |
| 757 | try |
| 758 | { |
| 759 | const character = makeCharacter(harness.stateDb, { tileX: 64, tileY: 8 }); |
| 760 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 761 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 762 | tileX: 63, |
| 763 | tileY: 8, |
| 764 | mobTypeId: MobId.deer, |
| 765 | spawnerId: null |
| 766 | })); |
| 767 | assert.equal(harness.simulation.entityRecord(playerId).workerId, 1); |
| 768 | assert.equal(harness.simulation.entityRecord(deerId).workerId, 0); |
| 769 | |
| 770 | harness.simulation.enqueue(1, { |
| 771 | type: "attack", |
| 772 | characterId: character.character_id, |
| 773 | targetEntityId: deerId |
| 774 | }); |
| 775 | const first = harness.simulation.step(); |
| 776 | assert.equal(first.events.some((event) => event.type === "command-error"), false); |
| 777 | |
| 778 | const random = Math.random; |
| 779 | Math.random = () => 0; |
| 780 | try |
| 781 | { |
| 782 | let swings = 0; |
| 783 | for (let i = 0; i < 40; i++) |
| 784 | { |
| 785 | harness.advance(50); |
| 786 | swings += harness.simulation.step().events.filter((event) => event.type === "combat-swing").length; |
| 787 | if (swings >= 4) |
| 788 | { break; } |
| 789 | } |
| 790 | |
| 791 | assert.ok(swings >= 4, `expected cross-worker sustained swings, got ${swings}`); |
| 792 | const deerWorker = harness.simulation.workers[0]; |
| 793 | const playerWorker = harness.simulation.workers[1]; |
| 794 | const deerHandle = deerWorker.world.ecs.handleOf(deerId); |
| 795 | const playerHandle = playerWorker.world.ecs.handleOf(playerId); |
| 796 | const deerCombat = deerWorker.world.ecs.get(deerHandle, Component.combat); |
| 797 | const playerCombat = playerWorker.world.ecs.get(playerHandle, Component.combat); |
| 798 | const playerHealth = playerWorker.world.ecs.get(playerHandle, Component.health); |
| 799 | assert.ok((playerHealth?.current ?? 0) > 0, "player should survive sustained cross-worker combat"); |
| 800 | assert.equal(deerCombat?.targetEntityId, playerId); |
| 801 | assert.equal(playerCombat?.targetEntityId, deerId); |
| 802 | } |
| 803 | finally |
| 804 | { Math.random = random; } |
| 805 | } |
| 806 | finally |
| 807 | { harness.close(); } |
| 808 | }); |
| 809 | |
| 810 | test("cancel attack stops the player but the mob keeps pursuing", () => |
| 811 | { |
| 812 | const harness = makeSimulation({ workerCount: 1 }); |
| 813 | try |
| 814 | { |
| 815 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 816 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 817 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 818 | tileX: 9, |
| 819 | tileY: 8, |
| 820 | mobTypeId: MobId.rat, |
| 821 | spawnerId: null |
| 822 | })); |
| 823 | |
| 824 | harness.simulation.enqueue(1, { |
| 825 | type: "attack", |
| 826 | characterId: character.character_id, |
| 827 | targetEntityId: ratId |
| 828 | }); |
| 829 | harness.simulation.step(); |
| 830 | |
| 831 | harness.simulation.enqueue(1, { |
| 832 | type: "cancel-attack", |
| 833 | characterId: character.character_id |
| 834 | }); |
| 835 | harness.simulation.step(); |
| 836 | |
| 837 | const player = componentsOf(harness.simulation, playerId); |
| 838 | const rat = componentsOf(harness.simulation, ratId); |
| 839 | assert.equal(player.combat?.targetEntityId, null); |
| 840 | assert.equal(rat.combat?.targetEntityId, playerId); |
| 841 | } |
| 842 | finally |
| 843 | { harness.close(); } |
| 844 | }); |
| 845 | |
| 846 | test("player combat and skills components are present on spawn", () => |
| 847 | { |
| 848 | const harness = makeSimulation({ workerCount: 1 }); |
| 849 | try |
| 850 | { |
| 851 | const character = makeCharacter(harness.stateDb); |
| 852 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 853 | const comps = componentsOf(harness.simulation, playerId); |
| 854 | assert.ok(comps.combat); |
| 855 | assert.equal(comps.skills?.xp?.clubfighting, 0); |
| 856 | assert.ok(comps.health); |
| 857 | } |
| 858 | finally |
| 859 | { harness.close(); } |
| 860 | }); |
| 861 | |
| 862 | test("mobs heal to full when their region unloads", () => |
| 863 | { |
| 864 | const harness = makeSimulation({ workerCount: 1 }); |
| 865 | try |
| 866 | { |
| 867 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 868 | tileX: 8, |
| 869 | tileY: 8, |
| 870 | mobTypeId: MobId.rat |
| 871 | })); |
| 872 | const worker = workerOf(harness.simulation, ratId); |
| 873 | const handle = worker.world.ecs.handleOf(ratId); |
| 874 | worker.world.ecs.get(handle, Component.health).current = 2; |
| 875 | const result = worker.call({ |
| 876 | type: WorkerMessage.dropRegion, |
| 877 | regionX: tileToRegion(8), |
| 878 | regionY: tileToRegion(8) |
| 879 | }); |
| 880 | const record = result.dropped.find((entry) => entry.entityId === ratId); |
| 881 | assert.ok(record); |
| 882 | assert.equal( |
| 883 | record.payload.components.health.current, |
| 884 | record.payload.components.health.max |
| 885 | ); |
| 886 | } |
| 887 | finally |
| 888 | { harness.close(); } |
| 889 | }); |
| 890 | |
| 891 | test("taking damage awards Health XP equal to damage dealt", () => |
| 892 | { |
| 893 | const harness = makeSimulation({ workerCount: 1 }); |
| 894 | const random = Math.random; |
| 895 | Math.random = () => 0; |
| 896 | try |
| 897 | { |
| 898 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 899 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 900 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 901 | mobTypeId: MobId.rat, |
| 902 | tileX: 9, |
| 903 | tileY: 8 |
| 904 | })); |
| 905 | const worker = harness.simulation.workers[0]; |
| 906 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 907 | const before = worker.world.ecs.get(playerHandle, Component.skills).xp[SkillId.health]; |
| 908 | const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId)); |
| 909 | const events = resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now()); |
| 910 | const hit = events.find((event) => event.type === "combat-hit"); |
| 911 | assert.ok(hit); |
| 912 | const updated = events.find((event) => event.type === "skills-updated" && event.entityId === playerId); |
| 913 | assert.ok(updated); |
| 914 | assert.equal(updated.stored[SkillId.health] - before, hit.damage); |
| 915 | } |
| 916 | finally |
| 917 | { |
| 918 | Math.random = random; |
| 919 | harness.close(); |
| 920 | } |
| 921 | }); |
| 922 | |
| 923 | test("lethal damage awards no Health XP", () => |
| 924 | { |
| 925 | const harness = makeSimulation({ workerCount: 1 }); |
| 926 | const random = Math.random; |
| 927 | Math.random = () => 0; |
| 928 | try |
| 929 | { |
| 930 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 931 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 932 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 933 | mobTypeId: MobId.rat, |
| 934 | tileX: 9, |
| 935 | tileY: 8 |
| 936 | })); |
| 937 | const worker = harness.simulation.workers[0]; |
| 938 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 939 | const health = worker.world.ecs.get(playerHandle, Component.health); |
| 940 | health.current = 1; |
| 941 | const before = worker.world.ecs.get(playerHandle, Component.skills).xp[SkillId.health]; |
| 942 | const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId)); |
| 943 | const events = resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now()); |
| 944 | const hit = events.find((event) => event.type === "combat-hit"); |
| 945 | assert.ok(hit); |
| 946 | assert.ok(hit.damage >= 1); |
| 947 | assert.equal(hit.health, 0); |
| 948 | const updated = events.find((event) => event.type === "skills-updated" && event.entityId === playerId); |
| 949 | assert.equal(updated, undefined); |
| 950 | assert.equal(worker.world.ecs.get(playerHandle, Component.skills).xp[SkillId.health], before); |
| 951 | } |
| 952 | finally |
| 953 | { |
| 954 | Math.random = random; |
| 955 | harness.close(); |
| 956 | } |
| 957 | }); |
| 958 | |
| 959 | test("dodging a hit awards Agility XP equal to avoided damage", () => |
| 960 | { |
| 961 | const harness = makeSimulation({ workerCount: 1 }); |
| 962 | let roll = 0; |
| 963 | const seq = [0, 0.5, 0]; |
| 964 | const random = Math.random; |
| 965 | Math.random = () => seq[roll++ % seq.length]; |
| 966 | try |
| 967 | { |
| 968 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 969 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 970 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 971 | mobTypeId: MobId.rat, |
| 972 | tileX: 9, |
| 973 | tileY: 8 |
| 974 | })); |
| 975 | const worker = harness.simulation.workers[0]; |
| 976 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 977 | const skills = worker.world.ecs.get(playerHandle, Component.skills); |
| 978 | skills.xp[SkillId.agility] = totalXpForLevel(10); |
| 979 | const before = skills.xp[SkillId.agility] | 0; |
| 980 | const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId)); |
| 981 | const events = resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now()); |
| 982 | assert.ok(events.find((event) => event.type === "combat-miss")); |
| 983 | const updated = events.find((event) => event.type === "skills-updated" && event.entityId === playerId); |
| 984 | assert.ok(updated); |
| 985 | assert.equal(updated.stored[SkillId.agility] - before, 1); |
| 986 | } |
| 987 | finally |
| 988 | { |
| 989 | Math.random = random; |
| 990 | harness.close(); |
| 991 | } |
| 992 | }); |
| 993 | |
| 994 | test("blocking a hit awards Defence XP equal to avoided damage", () => |
| 995 | { |
| 996 | const harness = makeSimulation({ workerCount: 1 }); |
| 997 | let roll = 0; |
| 998 | const seq = [0.9, 0, 0, 0.9]; |
| 999 | const random = Math.random; |
| 1000 | Math.random = () => seq[roll++ % seq.length]; |
| 1001 | try |
| 1002 | { |
| 1003 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 1004 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1005 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 1006 | mobTypeId: MobId.rat, |
| 1007 | tileX: 9, |
| 1008 | tileY: 8 |
| 1009 | })); |
| 1010 | const worker = harness.simulation.workers[0]; |
| 1011 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 1012 | const skills = worker.world.ecs.get(playerHandle, Component.skills); |
| 1013 | skills.xp[SkillId.defence] = totalXpForLevel(10); |
| 1014 | const before = skills.xp[SkillId.defence] | 0; |
| 1015 | const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId)); |
| 1016 | const events = resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now()); |
| 1017 | assert.ok(events.find((event) => event.type === "combat-block")); |
| 1018 | const updated = events.find((event) => event.type === "skills-updated" && event.entityId === playerId); |
| 1019 | assert.ok(updated); |
| 1020 | assert.equal(updated.stored[SkillId.defence] - before, 1); |
| 1021 | } |
| 1022 | finally |
| 1023 | { |
| 1024 | Math.random = random; |
| 1025 | harness.close(); |
| 1026 | } |
| 1027 | }); |