| 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 { WoodItemId } from "../../shared/definitions/woodItems.js"; |
| 7 | import { GemItemId } from "../../shared/definitions/gemItems.js"; |
| 8 | import { tierStatBonus, WoodTier } from "../../shared/definitions/materialTiers.js"; |
| 9 | import { BlockType, blockBlocksRangedAttacks } from "../../shared/definitions/tiles.js"; |
| 10 | import { Component } from "../src/ecs/components.js"; |
| 11 | import { tileKey } from "../src/world/coordinates.js"; |
| 12 | import { WorkerMessage } from "../src/simulation/protocol.js"; |
| 13 | import { tileToRegion } from "../src/world/coordinates.js"; |
| 14 | import { SkillId } from "../../shared/definitions/skills.js"; |
| 15 | import { totalXpForLevel } from "../../shared/skills/progression.js"; |
| 16 | import { resolveMeleeSwing, attackProfileOf, hasCombatLineOfSight, queueMeleeApproach, markSeekCover, engageTarget, applyDamageAndKill } from "../src/combat/service.js"; |
| 17 | import { makeCharacter, makeSimulation, pinEastSplit, stepTimes } from "./helpers/simulationHarness.js"; |
| 18 | |
| 19 | function workerOf (simulation, entityId) |
| 20 | { |
| 21 | const record = simulation.entityRecord(entityId); |
| 22 | assert.ok(record, "entity must be remembered"); |
| 23 | return simulation.workers[record.workerId]; |
| 24 | } |
| 25 | |
| 26 | function componentsOf (simulation, entityId) |
| 27 | { |
| 28 | const worker = workerOf(simulation, entityId); |
| 29 | const handle = worker.world.ecs.handleOf(entityId); |
| 30 | assert.ok(handle != null); |
| 31 | return worker.world.ecs.componentsOf(handle); |
| 32 | } |
| 33 | |
| 34 | function teleportEntity (worker, entityId, tileX, tileY) |
| 35 | { |
| 36 | const world = worker.world; |
| 37 | const handle = world.ecs.handleOf(entityId); |
| 38 | const pos = world.ecs.get(handle, Component.position); |
| 39 | const fromKey = tileKey(pos.tileX, pos.tileY); |
| 40 | const fromSet = world.occupancy.get(fromKey); |
| 41 | if (fromSet) |
| 42 | { |
| 43 | fromSet.delete(entityId); |
| 44 | if (fromSet.size === 0) |
| 45 | { world.occupancy.delete(fromKey); } |
| 46 | } |
| 47 | pos.tileX = tileX; |
| 48 | pos.tileY = tileY; |
| 49 | const toKey = tileKey(tileX, tileY); |
| 50 | let toSet = world.occupancy.get(toKey); |
| 51 | if (!toSet) |
| 52 | { |
| 53 | toSet = new Set(); |
| 54 | world.occupancy.set(toKey, toSet); |
| 55 | } |
| 56 | toSet.add(entityId); |
| 57 | } |
| 58 | |
| 59 | function entityIsAlive (simulation, entityId) |
| 60 | { |
| 61 | const record = simulation.entityRecord(entityId); |
| 62 | if (!record) |
| 63 | { return false; } |
| 64 | const worker = simulation.workers[record.workerId]; |
| 65 | const handle = worker.world.ecs.handleOf(entityId); |
| 66 | if (handle == null) |
| 67 | { return false; } |
| 68 | const health = worker.world.ecs.get(handle, Component.health); |
| 69 | return (health?.current ?? 0) > 0; |
| 70 | } |
| 71 | |
| 72 | function simulateAttackClick (harness, character, targetEntityId) |
| 73 | { |
| 74 | harness.simulation.enqueue(1, { |
| 75 | type: "attack", |
| 76 | characterId: character.character_id, |
| 77 | targetEntityId |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | /** Run ticks until one combatant dies or the step budget is exhausted. */ |
| 82 | function runCombatUntilOutcome (harness, playerId, deerId, maxSteps = 600) |
| 83 | { |
| 84 | let swings = 0; |
| 85 | for (let i = 0; i < maxSteps; i++) |
| 86 | { |
| 87 | harness.advance(50); |
| 88 | const events = harness.simulation.step().events; |
| 89 | swings += events.filter((event) => event.type === "combat-swing").length; |
| 90 | const playerAlive = entityIsAlive(harness.simulation, playerId); |
| 91 | const deerAlive = entityIsAlive(harness.simulation, deerId); |
| 92 | if (!playerAlive || !deerAlive) |
| 93 | { return { playerAlive, deerAlive, swings, steps: i + 1 }; } |
| 94 | } |
| 95 | return { |
| 96 | playerAlive: entityIsAlive(harness.simulation, playerId), |
| 97 | deerAlive: entityIsAlive(harness.simulation, deerId), |
| 98 | swings, |
| 99 | steps: maxSteps |
| 100 | }; |
| 101 | } |
| 102 | |
| 103 | function withDeterministicCombat (fn) |
| 104 | { |
| 105 | const random = Math.random; |
| 106 | Math.random = () => 0; |
| 107 | try |
| 108 | { fn(); } |
| 109 | finally |
| 110 | { Math.random = random; } |
| 111 | } |
| 112 | |
| 113 | test("immortal deer auto-attacks until the player dies", () => |
| 114 | { |
| 115 | withDeterministicCombat(() => |
| 116 | { |
| 117 | const harness = makeSimulation({ workerCount: 1 }); |
| 118 | try |
| 119 | { |
| 120 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 121 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 122 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 123 | tileX: 9, |
| 124 | tileY: 8, |
| 125 | mobTypeId: MobId.deer, |
| 126 | spawnerId: null |
| 127 | })); |
| 128 | const deer = componentsOf(harness.simulation, deerId); |
| 129 | deer.health.current = 1_000_000; |
| 130 | deer.health.max = 1_000_000; |
| 131 | |
| 132 | simulateAttackClick(harness, character, deerId); |
| 133 | harness.simulation.step(); |
| 134 | |
| 135 | const outcome = runCombatUntilOutcome(harness, playerId, deerId, 2_000); |
| 136 | assert.ok(outcome.swings >= 2, `expected sustained auto-attack swings, got ${outcome.swings}`); |
| 137 | assert.equal(outcome.deerAlive, true, "immortal deer should survive"); |
| 138 | assert.equal(outcome.playerAlive, false, "deer should kill the player via auto-attack"); |
| 139 | assert.equal( |
| 140 | componentsOf(harness.simulation, deerId).combat?.targetEntityId ?? null, |
| 141 | null, |
| 142 | "deer should clear engage after the player dies" |
| 143 | ); |
| 144 | } |
| 145 | finally |
| 146 | { harness.close(); } |
| 147 | }); |
| 148 | }); |
| 149 | |
| 150 | test("immortal deer keeps auto-attacking after the player walks away", () => |
| 151 | { |
| 152 | withDeterministicCombat(() => |
| 153 | { |
| 154 | const harness = makeSimulation({ workerCount: 1 }); |
| 155 | try |
| 156 | { |
| 157 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 158 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 159 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 160 | tileX: 9, |
| 161 | tileY: 8, |
| 162 | mobTypeId: MobId.deer, |
| 163 | spawnerId: null |
| 164 | })); |
| 165 | const deer = componentsOf(harness.simulation, deerId); |
| 166 | deer.health.current = 1_000_000; |
| 167 | deer.health.max = 1_000_000; |
| 168 | |
| 169 | simulateAttackClick(harness, character, deerId); |
| 170 | harness.simulation.step(); |
| 171 | |
| 172 | // Let one exchange land, then cancel player engage by walking away (client moveTo). |
| 173 | for (let i = 0; i < 30; i++) |
| 174 | { |
| 175 | harness.advance(50); |
| 176 | const swings = harness.simulation.step().events.filter((event) => event.type === "combat-swing").length; |
| 177 | if (swings > 0) |
| 178 | { break; } |
| 179 | } |
| 180 | harness.simulation.enqueue(1, { |
| 181 | type: "move-to", |
| 182 | characterId: character.character_id, |
| 183 | tileX: 8, |
| 184 | tileY: 14 |
| 185 | }); |
| 186 | harness.simulation.step(); |
| 187 | assert.equal(componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null, null); |
| 188 | assert.equal(componentsOf(harness.simulation, deerId).combat?.targetEntityId, playerId); |
| 189 | |
| 190 | const outcome = runCombatUntilOutcome(harness, playerId, deerId, 2_000); |
| 191 | assert.ok(outcome.swings >= 1, `expected deer pursuit swings, got ${outcome.swings}`); |
| 192 | assert.equal(outcome.deerAlive, true); |
| 193 | assert.equal(outcome.playerAlive, false, "deer should chase and kill the player after walk-away"); |
| 194 | } |
| 195 | finally |
| 196 | { harness.close(); } |
| 197 | }); |
| 198 | }); |
| 199 | |
| 200 | test("cross-worker immortal deer auto-attacks until the player dies", () => |
| 201 | { |
| 202 | withDeterministicCombat(() => |
| 203 | { |
| 204 | const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); |
| 205 | try |
| 206 | { |
| 207 | const character = makeCharacter(harness.stateDb, { tileX: 64, tileY: 8 }); |
| 208 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 209 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 210 | tileX: 63, |
| 211 | tileY: 8, |
| 212 | mobTypeId: MobId.deer, |
| 213 | spawnerId: null |
| 214 | })); |
| 215 | assert.equal(harness.simulation.entityRecord(playerId).workerId, 1); |
| 216 | assert.equal(harness.simulation.entityRecord(deerId).workerId, 0); |
| 217 | |
| 218 | const deerWorker = workerOf(harness.simulation, deerId); |
| 219 | const deerHandle = deerWorker.world.ecs.handleOf(deerId); |
| 220 | const deerHealth = deerWorker.world.ecs.get(deerHandle, Component.health); |
| 221 | deerHealth.current = 1_000_000; |
| 222 | deerHealth.max = 1_000_000; |
| 223 | |
| 224 | simulateAttackClick(harness, character, deerId); |
| 225 | const first = harness.simulation.step(); |
| 226 | assert.equal(first.events.some((event) => event.type === "command-error"), false); |
| 227 | |
| 228 | const outcome = runCombatUntilOutcome(harness, playerId, deerId, 2_000); |
| 229 | assert.ok(outcome.swings >= 2, `expected sustained cross-worker swings, got ${outcome.swings}`); |
| 230 | assert.equal(outcome.deerAlive, true); |
| 231 | assert.equal(outcome.playerAlive, false, "cross-worker deer should kill the player via auto-attack"); |
| 232 | } |
| 233 | finally |
| 234 | { harness.close(); } |
| 235 | }); |
| 236 | }); |
| 237 | |
| 238 | test("click attack from 5 tiles away ends with player or deer dead", () => |
| 239 | { |
| 240 | withDeterministicCombat(() => |
| 241 | { |
| 242 | const harness = makeSimulation({ workerCount: 1 }); |
| 243 | try |
| 244 | { |
| 245 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 246 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 247 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 248 | tileX: 13, |
| 249 | tileY: 8, |
| 250 | mobTypeId: MobId.deer, |
| 251 | spawnerId: null |
| 252 | })); |
| 253 | |
| 254 | simulateAttackClick(harness, character, deerId); |
| 255 | harness.simulation.step(); |
| 256 | |
| 257 | const outcome = runCombatUntilOutcome(harness, playerId, deerId); |
| 258 | assert.ok(outcome.swings >= 1, `expected at least one swing, got ${outcome.swings}`); |
| 259 | assert.ok( |
| 260 | !outcome.playerAlive || !outcome.deerAlive, |
| 261 | `expected a kill within ${outcome.steps} steps (${outcome.swings} swings); both still alive` |
| 262 | ); |
| 263 | } |
| 264 | finally |
| 265 | { harness.close(); } |
| 266 | }); |
| 267 | }); |
| 268 | |
| 269 | test("click attack while adjacent ends with player or deer dead", () => |
| 270 | { |
| 271 | withDeterministicCombat(() => |
| 272 | { |
| 273 | const harness = makeSimulation({ workerCount: 1 }); |
| 274 | try |
| 275 | { |
| 276 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 277 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 278 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 279 | tileX: 9, |
| 280 | tileY: 8, |
| 281 | mobTypeId: MobId.deer, |
| 282 | spawnerId: null |
| 283 | })); |
| 284 | |
| 285 | simulateAttackClick(harness, character, deerId); |
| 286 | harness.simulation.step(); |
| 287 | |
| 288 | const outcome = runCombatUntilOutcome(harness, playerId, deerId); |
| 289 | assert.ok(outcome.swings >= 1, `expected at least one swing, got ${outcome.swings}`); |
| 290 | assert.ok( |
| 291 | !outcome.playerAlive || !outcome.deerAlive, |
| 292 | `expected a kill within ${outcome.steps} steps (${outcome.swings} swings); both still alive` |
| 293 | ); |
| 294 | } |
| 295 | finally |
| 296 | { harness.close(); } |
| 297 | }); |
| 298 | }); |
| 299 | |
| 300 | test("cross-worker click attack from 5 tiles away ends with player or deer dead", () => |
| 301 | { |
| 302 | withDeterministicCombat(() => |
| 303 | { |
| 304 | const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); |
| 305 | try |
| 306 | { |
| 307 | const character = makeCharacter(harness.stateDb, { tileX: 59, tileY: 8 }); |
| 308 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 309 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 310 | tileX: 64, |
| 311 | tileY: 8, |
| 312 | mobTypeId: MobId.deer, |
| 313 | spawnerId: null |
| 314 | })); |
| 315 | assert.equal(harness.simulation.entityRecord(playerId).workerId, 0); |
| 316 | assert.equal(harness.simulation.entityRecord(deerId).workerId, 1); |
| 317 | |
| 318 | simulateAttackClick(harness, character, deerId); |
| 319 | const first = harness.simulation.step(); |
| 320 | assert.equal(first.events.some((event) => event.type === "command-error"), false); |
| 321 | |
| 322 | const outcome = runCombatUntilOutcome(harness, playerId, deerId); |
| 323 | assert.ok(outcome.swings >= 1, `expected at least one swing, got ${outcome.swings}`); |
| 324 | assert.ok( |
| 325 | !outcome.playerAlive || !outcome.deerAlive, |
| 326 | `expected a kill within ${outcome.steps} steps (${outcome.swings} swings); both still alive` |
| 327 | ); |
| 328 | } |
| 329 | finally |
| 330 | { harness.close(); } |
| 331 | }); |
| 332 | }); |
| 333 | |
| 334 | test("cross-worker adjacent click attack ends with player or deer dead", () => |
| 335 | { |
| 336 | withDeterministicCombat(() => |
| 337 | { |
| 338 | const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); |
| 339 | try |
| 340 | { |
| 341 | const character = makeCharacter(harness.stateDb, { tileX: 64, tileY: 8 }); |
| 342 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 343 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 344 | tileX: 63, |
| 345 | tileY: 8, |
| 346 | mobTypeId: MobId.deer, |
| 347 | spawnerId: null |
| 348 | })); |
| 349 | assert.equal(harness.simulation.entityRecord(playerId).workerId, 1); |
| 350 | assert.equal(harness.simulation.entityRecord(deerId).workerId, 0); |
| 351 | |
| 352 | simulateAttackClick(harness, character, deerId); |
| 353 | const first = harness.simulation.step(); |
| 354 | assert.equal(first.events.some((event) => event.type === "command-error"), false); |
| 355 | |
| 356 | const outcome = runCombatUntilOutcome(harness, playerId, deerId); |
| 357 | assert.ok(outcome.swings >= 1, `expected at least one swing, got ${outcome.swings}`); |
| 358 | assert.ok( |
| 359 | !outcome.playerAlive || !outcome.deerAlive, |
| 360 | `expected a kill within ${outcome.steps} steps (${outcome.swings} swings); both still alive` |
| 361 | ); |
| 362 | } |
| 363 | finally |
| 364 | { harness.close(); } |
| 365 | }); |
| 366 | }); |
| 367 | |
| 368 | test("attack engages the attacker; target retaliates on first swing", () => |
| 369 | { |
| 370 | const harness = makeSimulation({ workerCount: 1 }); |
| 371 | try |
| 372 | { |
| 373 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 374 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 375 | // Deer are not aggressive — only retaliate after a real swing. |
| 376 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 377 | tileX: 12, |
| 378 | tileY: 8, |
| 379 | mobTypeId: MobId.deer, |
| 380 | spawnerId: null |
| 381 | })); |
| 382 | |
| 383 | harness.simulation.enqueue(1, { |
| 384 | type: "attack", |
| 385 | characterId: character.character_id, |
| 386 | targetEntityId: deerId |
| 387 | }); |
| 388 | harness.simulation.step(); |
| 389 | |
| 390 | assert.equal(componentsOf(harness.simulation, playerId).combat.targetEntityId, deerId); |
| 391 | assert.equal(componentsOf(harness.simulation, deerId).combat?.targetEntityId ?? null, null); |
| 392 | |
| 393 | let swung = false; |
| 394 | for (let i = 0; i < 80; i++) |
| 395 | { |
| 396 | harness.advance(50); |
| 397 | const events = harness.simulation.step().events; |
| 398 | if (events.some((event) => event.type === "combat-swing")) |
| 399 | { |
| 400 | swung = true; |
| 401 | break; |
| 402 | } |
| 403 | } |
| 404 | assert.ok(swung, "expected a melee swing once adjacent"); |
| 405 | assert.equal(componentsOf(harness.simulation, deerId).combat?.targetEntityId, playerId); |
| 406 | } |
| 407 | finally |
| 408 | { harness.close(); } |
| 409 | }); |
| 410 | |
| 411 | test("melee swing only resolves at Chebyshev distance 1", () => |
| 412 | { |
| 413 | const harness = makeSimulation({ workerCount: 1 }); |
| 414 | try |
| 415 | { |
| 416 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 417 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 418 | const bag = harness.simulation.bagOf(playerId); |
| 419 | const stickSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.stick); |
| 420 | assert.ok(stickSlot >= 0); |
| 421 | harness.simulation.enqueue(1, { |
| 422 | type: "equip-item", |
| 423 | characterId: character.character_id, |
| 424 | slotIndex: stickSlot |
| 425 | }); |
| 426 | harness.simulation.step(); |
| 427 | assert.equal( |
| 428 | harness.simulation.bagOf(playerId).equipment.slots[EQUIPMENT_SLOT.weapon]?.itemId, |
| 429 | ItemId.stick |
| 430 | ); |
| 431 | |
| 432 | const farRat = harness.simulation.spawnEntity(mobComponents({ |
| 433 | tileX: 12, |
| 434 | tileY: 8, |
| 435 | mobTypeId: MobId.rat |
| 436 | })); |
| 437 | harness.simulation.enqueue(1, { |
| 438 | type: "attack", |
| 439 | characterId: character.character_id, |
| 440 | targetEntityId: farRat |
| 441 | }); |
| 442 | const farEvents = harness.simulation.step().events; |
| 443 | assert.equal(farEvents.some((event) => event.type === "combat-swing"), false); |
| 444 | |
| 445 | const nearRat = harness.simulation.spawnEntity(mobComponents({ |
| 446 | tileX: 9, |
| 447 | tileY: 8, |
| 448 | mobTypeId: MobId.rat |
| 449 | })); |
| 450 | harness.simulation.enqueue(1, { |
| 451 | type: "attack", |
| 452 | characterId: character.character_id, |
| 453 | targetEntityId: nearRat |
| 454 | }); |
| 455 | const nearEvents = harness.simulation.step().events; |
| 456 | assert.ok(nearEvents.some((event) => event.type === "combat-swing")); |
| 457 | assert.ok(nearEvents.some((event) => |
| 458 | event.type === "combat-hit" |
| 459 | || event.type === "combat-miss" |
| 460 | || event.type === "combat-block")); |
| 461 | } |
| 462 | finally |
| 463 | { harness.close(); } |
| 464 | }); |
| 465 | |
| 466 | test("pigeons are untargetable free-flight and never get path tiles", () => |
| 467 | { |
| 468 | const harness = makeSimulation({ workerCount: 1 }); |
| 469 | try |
| 470 | { |
| 471 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 472 | harness.simulation.spawnCharacter(1, character); |
| 473 | const pigeonId = harness.simulation.spawnEntity(mobComponents({ |
| 474 | tileX: 10, |
| 475 | tileY: 8, |
| 476 | mobTypeId: MobId.pigeon |
| 477 | })); |
| 478 | |
| 479 | const before = componentsOf(harness.simulation, pigeonId); |
| 480 | assert.equal(before.flight != null, true); |
| 481 | assert.equal(before.combat == null, true); |
| 482 | assert.deepEqual(before.path?.tiles ?? [], []); |
| 483 | |
| 484 | harness.setNow(harness.now() + 500); |
| 485 | stepTimes(harness.simulation, 10); |
| 486 | const after = componentsOf(harness.simulation, pigeonId); |
| 487 | assert.deepEqual(after.path?.tiles ?? [], []); |
| 488 | assert.equal(after.flight != null, true); |
| 489 | |
| 490 | harness.simulation.enqueue(1, { |
| 491 | type: "attack", |
| 492 | characterId: character.character_id, |
| 493 | targetEntityId: pigeonId |
| 494 | }); |
| 495 | const result = harness.simulation.step(); |
| 496 | assert.ok(result.events.some((event) => event.type === "command-error")); |
| 497 | assert.equal(componentsOf(harness.simulation, pigeonId).path?.tiles?.length ?? 0, 0); |
| 498 | } |
| 499 | finally |
| 500 | { harness.close(); } |
| 501 | }); |
| 502 | |
| 503 | test("deer retaliate when attacked", () => |
| 504 | { |
| 505 | const harness = makeSimulation({ workerCount: 1 }); |
| 506 | try |
| 507 | { |
| 508 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 509 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 510 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 511 | tileX: 9, |
| 512 | tileY: 8, |
| 513 | mobTypeId: MobId.deer, |
| 514 | spawnerId: null |
| 515 | })); |
| 516 | |
| 517 | harness.simulation.enqueue(1, { |
| 518 | type: "attack", |
| 519 | characterId: character.character_id, |
| 520 | targetEntityId: deerId |
| 521 | }); |
| 522 | const events = harness.simulation.step().events; |
| 523 | assert.ok(events.some((event) => event.type === "combat-swing")); |
| 524 | |
| 525 | const deer = componentsOf(harness.simulation, deerId); |
| 526 | assert.equal(deer.combat?.targetEntityId, playerId); |
| 527 | assert.ok(deer.skills?.levels?.unarmed >= 1); |
| 528 | } |
| 529 | finally |
| 530 | { harness.close(); } |
| 531 | }); |
| 532 | |
| 533 | test("aggressive rats engage players within aggro range and LOS", () => |
| 534 | { |
| 535 | const harness = makeSimulation({ workerCount: 1 }); |
| 536 | try |
| 537 | { |
| 538 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 539 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 540 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 541 | tileX: 12, |
| 542 | tileY: 8, |
| 543 | mobTypeId: MobId.rat, |
| 544 | spawnerId: null |
| 545 | })); |
| 546 | |
| 547 | harness.simulation.step(); |
| 548 | assert.equal(componentsOf(harness.simulation, ratId).combat?.targetEntityId, playerId); |
| 549 | } |
| 550 | finally |
| 551 | { harness.close(); } |
| 552 | }); |
| 553 | |
| 554 | test("players retaliate when a mob swings at them", () => |
| 555 | { |
| 556 | const harness = makeSimulation({ workerCount: 1 }); |
| 557 | try |
| 558 | { |
| 559 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 560 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 561 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 562 | tileX: 9, |
| 563 | tileY: 8, |
| 564 | mobTypeId: MobId.rat, |
| 565 | spawnerId: null |
| 566 | })); |
| 567 | |
| 568 | assert.equal(componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null, null); |
| 569 | |
| 570 | let swung = false; |
| 571 | for (let i = 0; i < 80; i++) |
| 572 | { |
| 573 | harness.advance(50); |
| 574 | const events = harness.simulation.step().events; |
| 575 | if (events.some((event) => |
| 576 | event.type === "combat-swing" && event.attackerId === ratId && event.targetId === playerId |
| 577 | )) |
| 578 | { |
| 579 | swung = true; |
| 580 | break; |
| 581 | } |
| 582 | } |
| 583 | assert.ok(swung, "expected the rat to swing at the player"); |
| 584 | assert.equal( |
| 585 | componentsOf(harness.simulation, playerId).combat?.targetEntityId, |
| 586 | ratId, |
| 587 | "player should auto-retaliate after the mob swing" |
| 588 | ); |
| 589 | } |
| 590 | finally |
| 591 | { harness.close(); } |
| 592 | }); |
| 593 | |
| 594 | test("aggressive rats do not aggro through walls or beyond range", () => |
| 595 | { |
| 596 | const harness = makeSimulation({ workerCount: 1 }); |
| 597 | try |
| 598 | { |
| 599 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 600 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 601 | const record = harness.simulation.entityRecord(playerId); |
| 602 | const worker = harness.simulation.workers[record.workerId]; |
| 603 | |
| 604 | const blockedRat = harness.simulation.spawnEntity(mobComponents({ |
| 605 | tileX: 12, |
| 606 | tileY: 8, |
| 607 | mobTypeId: MobId.rat, |
| 608 | spawnerId: null |
| 609 | })); |
| 610 | const data = worker.world.chunkData(0, 0); |
| 611 | data.blocks[8 * 16 + 10] = { typeId: BlockType.stoneWall, state: null, config: null }; |
| 612 | |
| 613 | const farRat = harness.simulation.spawnEntity(mobComponents({ |
| 614 | tileX: 14, |
| 615 | tileY: 8, |
| 616 | mobTypeId: MobId.rat, |
| 617 | spawnerId: null |
| 618 | })); |
| 619 | |
| 620 | harness.simulation.step(); |
| 621 | assert.equal(componentsOf(harness.simulation, blockedRat).combat?.targetEntityId ?? null, null); |
| 622 | assert.equal(componentsOf(harness.simulation, farRat).combat?.targetEntityId ?? null, null); |
| 623 | } |
| 624 | finally |
| 625 | { harness.close(); } |
| 626 | }); |
| 627 | |
| 628 | test("crafting stations and tables do not block ranged or spell line of sight", () => |
| 629 | { |
| 630 | assert.equal(blockBlocksRangedAttacks(BlockType.stoneWall), true); |
| 631 | assert.equal(blockBlocksRangedAttacks(BlockType.furnace), false); |
| 632 | assert.equal(blockBlocksRangedAttacks(BlockType.table), false); |
| 633 | assert.equal(blockBlocksRangedAttacks(null), false); |
| 634 | |
| 635 | const harness = makeSimulation({ workerCount: 1 }); |
| 636 | try |
| 637 | { |
| 638 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 639 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 640 | const worker = workerOf(harness.simulation, playerId); |
| 641 | const data = worker.world.chunkData(0, 0); |
| 642 | const blockIndex = (localY, localX) => localY * 16 + localX; |
| 643 | |
| 644 | data.blocks[blockIndex(8, 10)] = { typeId: BlockType.furnace, state: null, config: null }; |
| 645 | assert.equal( |
| 646 | hasCombatLineOfSight(worker.world, 8, 8, 12, 8), |
| 647 | true, |
| 648 | "furnace between caster and target should not block" |
| 649 | ); |
| 650 | |
| 651 | data.blocks[blockIndex(8, 10)] = { typeId: BlockType.table, state: null, config: null }; |
| 652 | assert.equal( |
| 653 | hasCombatLineOfSight(worker.world, 8, 8, 12, 8), |
| 654 | true, |
| 655 | "table between caster and target should not block" |
| 656 | ); |
| 657 | |
| 658 | data.blocks[blockIndex(8, 10)] = { typeId: BlockType.stoneWall, state: null, config: null }; |
| 659 | assert.equal( |
| 660 | hasCombatLineOfSight(worker.world, 8, 8, 12, 8), |
| 661 | false, |
| 662 | "stone wall should still block" |
| 663 | ); |
| 664 | } |
| 665 | finally |
| 666 | { harness.close(); } |
| 667 | }); |
| 668 | |
| 669 | test("attacker keeps pursuing and can swing after the target moves", () => |
| 670 | { |
| 671 | const harness = makeSimulation({ workerCount: 1 }); |
| 672 | try |
| 673 | { |
| 674 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 675 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 676 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 677 | tileX: 12, |
| 678 | tileY: 8, |
| 679 | mobTypeId: MobId.rat, |
| 680 | spawnerId: null |
| 681 | })); |
| 682 | |
| 683 | harness.simulation.enqueue(1, { |
| 684 | type: "attack", |
| 685 | characterId: character.character_id, |
| 686 | targetEntityId: ratId |
| 687 | }); |
| 688 | harness.simulation.step(); |
| 689 | |
| 690 | const worker = workerOf(harness.simulation, playerId); |
| 691 | stepTimes(harness.simulation, 4); |
| 692 | teleportEntity(worker, ratId, 12, 10); |
| 693 | |
| 694 | let swings = 0; |
| 695 | for (let i = 0; i < 120; i++) |
| 696 | { |
| 697 | const events = harness.simulation.step().events; |
| 698 | swings += events.filter((event) => event.type === "combat-swing").length; |
| 699 | if (swings > 0) |
| 700 | { break; } |
| 701 | } |
| 702 | |
| 703 | assert.ok(swings >= 1); |
| 704 | assert.equal(componentsOf(harness.simulation, playerId).combat?.targetEntityId, ratId); |
| 705 | } |
| 706 | finally |
| 707 | { harness.close(); } |
| 708 | }); |
| 709 | |
| 710 | test("combatants keep auto-attacking while adjacent", () => |
| 711 | { |
| 712 | const harness = makeSimulation({ workerCount: 1 }); |
| 713 | try |
| 714 | { |
| 715 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 716 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 717 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 718 | tileX: 9, |
| 719 | tileY: 8, |
| 720 | mobTypeId: MobId.deer, |
| 721 | spawnerId: null |
| 722 | })); |
| 723 | |
| 724 | harness.simulation.enqueue(1, { |
| 725 | type: "attack", |
| 726 | characterId: character.character_id, |
| 727 | targetEntityId: deerId |
| 728 | }); |
| 729 | harness.simulation.step(); |
| 730 | |
| 731 | let swings = 0; |
| 732 | for (let i = 0; i < 50; i++) |
| 733 | { |
| 734 | harness.advance(50); |
| 735 | const events = harness.simulation.step().events; |
| 736 | swings += events.filter((event) => event.type === "combat-swing").length; |
| 737 | const player = componentsOf(harness.simulation, playerId); |
| 738 | const deer = componentsOf(harness.simulation, deerId); |
| 739 | if (swings >= 4 |
| 740 | && player.combat?.targetEntityId === deerId |
| 741 | && deer.combat?.targetEntityId === playerId |
| 742 | && (player.health?.current ?? 0) > 0) |
| 743 | { break; } |
| 744 | } |
| 745 | |
| 746 | assert.ok(swings >= 4, `expected repeated swings, got ${swings}`); |
| 747 | const player = componentsOf(harness.simulation, playerId); |
| 748 | const deer = componentsOf(harness.simulation, deerId); |
| 749 | assert.ok((player.health?.current ?? 0) > 0, "player should still be alive during sustained combat"); |
| 750 | assert.equal(player.combat?.targetEntityId, deerId); |
| 751 | assert.equal(deer.combat?.targetEntityId, playerId); |
| 752 | } |
| 753 | finally |
| 754 | { harness.close(); } |
| 755 | }); |
| 756 | |
| 757 | test("cross-worker adjacent combat keeps engaging and swinging", () => |
| 758 | { |
| 759 | const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); |
| 760 | try |
| 761 | { |
| 762 | const character = makeCharacter(harness.stateDb, { tileX: 64, tileY: 8 }); |
| 763 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 764 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 765 | tileX: 63, |
| 766 | tileY: 8, |
| 767 | mobTypeId: MobId.deer, |
| 768 | spawnerId: null |
| 769 | })); |
| 770 | assert.equal(harness.simulation.entityRecord(playerId).workerId, 1); |
| 771 | assert.equal(harness.simulation.entityRecord(deerId).workerId, 0); |
| 772 | |
| 773 | harness.simulation.enqueue(1, { |
| 774 | type: "attack", |
| 775 | characterId: character.character_id, |
| 776 | targetEntityId: deerId |
| 777 | }); |
| 778 | const first = harness.simulation.step(); |
| 779 | assert.equal(first.events.some((event) => event.type === "command-error"), false); |
| 780 | |
| 781 | const random = Math.random; |
| 782 | Math.random = () => 0; |
| 783 | try |
| 784 | { |
| 785 | let swings = 0; |
| 786 | for (let i = 0; i < 40; i++) |
| 787 | { |
| 788 | harness.advance(50); |
| 789 | swings += harness.simulation.step().events.filter((event) => event.type === "combat-swing").length; |
| 790 | if (swings >= 4) |
| 791 | { break; } |
| 792 | } |
| 793 | |
| 794 | assert.ok(swings >= 4, `expected cross-worker sustained swings, got ${swings}`); |
| 795 | const deerWorker = harness.simulation.workers[0]; |
| 796 | const playerWorker = harness.simulation.workers[1]; |
| 797 | const deerHandle = deerWorker.world.ecs.handleOf(deerId); |
| 798 | const playerHandle = playerWorker.world.ecs.handleOf(playerId); |
| 799 | const deerCombat = deerWorker.world.ecs.get(deerHandle, Component.combat); |
| 800 | const playerCombat = playerWorker.world.ecs.get(playerHandle, Component.combat); |
| 801 | const playerHealth = playerWorker.world.ecs.get(playerHandle, Component.health); |
| 802 | assert.ok((playerHealth?.current ?? 0) > 0, "player should survive sustained cross-worker combat"); |
| 803 | assert.equal(deerCombat?.targetEntityId, playerId); |
| 804 | assert.equal(playerCombat?.targetEntityId, deerId); |
| 805 | } |
| 806 | finally |
| 807 | { Math.random = random; } |
| 808 | } |
| 809 | finally |
| 810 | { harness.close(); } |
| 811 | }); |
| 812 | |
| 813 | test("cancel attack stops the player but the mob keeps pursuing", () => |
| 814 | { |
| 815 | const harness = makeSimulation({ workerCount: 1 }); |
| 816 | try |
| 817 | { |
| 818 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 819 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 820 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 821 | tileX: 9, |
| 822 | tileY: 8, |
| 823 | mobTypeId: MobId.rat, |
| 824 | spawnerId: null |
| 825 | })); |
| 826 | |
| 827 | harness.simulation.enqueue(1, { |
| 828 | type: "attack", |
| 829 | characterId: character.character_id, |
| 830 | targetEntityId: ratId |
| 831 | }); |
| 832 | harness.simulation.step(); |
| 833 | |
| 834 | harness.simulation.enqueue(1, { |
| 835 | type: "cancel-attack", |
| 836 | characterId: character.character_id |
| 837 | }); |
| 838 | harness.simulation.step(); |
| 839 | |
| 840 | const player = componentsOf(harness.simulation, playerId); |
| 841 | const rat = componentsOf(harness.simulation, ratId); |
| 842 | assert.equal(player.combat?.targetEntityId, null); |
| 843 | assert.equal(rat.combat?.targetEntityId, playerId); |
| 844 | } |
| 845 | finally |
| 846 | { harness.close(); } |
| 847 | }); |
| 848 | |
| 849 | test("player combat and skills components are present on spawn", () => |
| 850 | { |
| 851 | const harness = makeSimulation({ workerCount: 1 }); |
| 852 | try |
| 853 | { |
| 854 | const character = makeCharacter(harness.stateDb); |
| 855 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 856 | const comps = componentsOf(harness.simulation, playerId); |
| 857 | assert.ok(comps.combat); |
| 858 | assert.equal(comps.skills?.xp?.clubfighting, 0); |
| 859 | assert.ok(comps.health); |
| 860 | } |
| 861 | finally |
| 862 | { harness.close(); } |
| 863 | }); |
| 864 | |
| 865 | test("mobs heal to full when their region unloads", () => |
| 866 | { |
| 867 | const harness = makeSimulation({ workerCount: 1 }); |
| 868 | try |
| 869 | { |
| 870 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 871 | tileX: 8, |
| 872 | tileY: 8, |
| 873 | mobTypeId: MobId.rat |
| 874 | })); |
| 875 | const worker = workerOf(harness.simulation, ratId); |
| 876 | const handle = worker.world.ecs.handleOf(ratId); |
| 877 | worker.world.ecs.get(handle, Component.health).current = 2; |
| 878 | const result = worker.call({ |
| 879 | type: WorkerMessage.dropRegion, |
| 880 | regionX: tileToRegion(8), |
| 881 | regionY: tileToRegion(8) |
| 882 | }); |
| 883 | const record = result.dropped.find((entry) => entry.entityId === ratId); |
| 884 | assert.ok(record); |
| 885 | assert.equal( |
| 886 | record.payload.components.health.current, |
| 887 | record.payload.components.health.max |
| 888 | ); |
| 889 | } |
| 890 | finally |
| 891 | { harness.close(); } |
| 892 | }); |
| 893 | |
| 894 | test("taking damage awards Health XP equal to damage dealt", () => |
| 895 | { |
| 896 | const harness = makeSimulation({ workerCount: 1 }); |
| 897 | const random = Math.random; |
| 898 | Math.random = () => 0; |
| 899 | try |
| 900 | { |
| 901 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 902 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 903 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 904 | mobTypeId: MobId.rat, |
| 905 | tileX: 9, |
| 906 | tileY: 8 |
| 907 | })); |
| 908 | const worker = harness.simulation.workers[0]; |
| 909 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 910 | const before = worker.world.ecs.get(playerHandle, Component.skills).xp[SkillId.health]; |
| 911 | const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId)); |
| 912 | const events = resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now()); |
| 913 | const hit = events.find((event) => event.type === "combat-hit"); |
| 914 | assert.ok(hit); |
| 915 | const updated = events.find((event) => event.type === "skills-updated" && event.entityId === playerId); |
| 916 | assert.ok(updated); |
| 917 | assert.equal(updated.stored[SkillId.health] - before, hit.damage); |
| 918 | } |
| 919 | finally |
| 920 | { |
| 921 | Math.random = random; |
| 922 | harness.close(); |
| 923 | } |
| 924 | }); |
| 925 | |
| 926 | test("lethal damage awards no Health XP", () => |
| 927 | { |
| 928 | const harness = makeSimulation({ workerCount: 1 }); |
| 929 | const random = Math.random; |
| 930 | Math.random = () => 0; |
| 931 | try |
| 932 | { |
| 933 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 934 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 935 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 936 | mobTypeId: MobId.rat, |
| 937 | tileX: 9, |
| 938 | tileY: 8 |
| 939 | })); |
| 940 | const worker = harness.simulation.workers[0]; |
| 941 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 942 | const health = worker.world.ecs.get(playerHandle, Component.health); |
| 943 | health.current = 1; |
| 944 | const before = worker.world.ecs.get(playerHandle, Component.skills).xp[SkillId.health]; |
| 945 | const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId)); |
| 946 | const events = resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now()); |
| 947 | const hit = events.find((event) => event.type === "combat-hit"); |
| 948 | assert.ok(hit); |
| 949 | assert.ok(hit.damage >= 1); |
| 950 | assert.equal(hit.health, 0); |
| 951 | const updated = events.find((event) => event.type === "skills-updated" && event.entityId === playerId); |
| 952 | assert.equal(updated, undefined); |
| 953 | assert.equal(worker.world.ecs.get(playerHandle, Component.skills).xp[SkillId.health], before); |
| 954 | } |
| 955 | finally |
| 956 | { |
| 957 | Math.random = random; |
| 958 | harness.close(); |
| 959 | } |
| 960 | }); |
| 961 | |
| 962 | test("dodging a hit awards Agility XP equal to avoided damage", () => |
| 963 | { |
| 964 | const harness = makeSimulation({ workerCount: 1 }); |
| 965 | let roll = 0; |
| 966 | const seq = [0, 0.5, 0]; |
| 967 | const random = Math.random; |
| 968 | Math.random = () => seq[roll++ % seq.length]; |
| 969 | try |
| 970 | { |
| 971 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 972 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 973 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 974 | mobTypeId: MobId.rat, |
| 975 | tileX: 9, |
| 976 | tileY: 8 |
| 977 | })); |
| 978 | const worker = harness.simulation.workers[0]; |
| 979 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 980 | const skills = worker.world.ecs.get(playerHandle, Component.skills); |
| 981 | skills.xp[SkillId.agility] = totalXpForLevel(10); |
| 982 | const before = skills.xp[SkillId.agility] | 0; |
| 983 | const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId)); |
| 984 | const events = resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now()); |
| 985 | assert.ok(events.find((event) => event.type === "combat-miss")); |
| 986 | const updated = events.find((event) => event.type === "skills-updated" && event.entityId === playerId); |
| 987 | assert.ok(updated); |
| 988 | assert.equal(updated.stored[SkillId.agility] - before, 1); |
| 989 | } |
| 990 | finally |
| 991 | { |
| 992 | Math.random = random; |
| 993 | harness.close(); |
| 994 | } |
| 995 | }); |
| 996 | |
| 997 | test("blocking a hit awards Defence XP equal to avoided damage", () => |
| 998 | { |
| 999 | const harness = makeSimulation({ workerCount: 1 }); |
| 1000 | let roll = 0; |
| 1001 | const seq = [0.9, 0, 0, 0.9]; |
| 1002 | const random = Math.random; |
| 1003 | Math.random = () => seq[roll++ % seq.length]; |
| 1004 | try |
| 1005 | { |
| 1006 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 1007 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1008 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 1009 | mobTypeId: MobId.rat, |
| 1010 | tileX: 9, |
| 1011 | tileY: 8 |
| 1012 | })); |
| 1013 | const worker = harness.simulation.workers[0]; |
| 1014 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 1015 | const skills = worker.world.ecs.get(playerHandle, Component.skills); |
| 1016 | skills.xp[SkillId.defence] = totalXpForLevel(10); |
| 1017 | const before = skills.xp[SkillId.defence] | 0; |
| 1018 | const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId)); |
| 1019 | const events = resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now()); |
| 1020 | assert.ok(events.find((event) => event.type === "combat-block")); |
| 1021 | const updated = events.find((event) => event.type === "skills-updated" && event.entityId === playerId); |
| 1022 | assert.ok(updated); |
| 1023 | assert.equal(updated.stored[SkillId.defence] - before, 1); |
| 1024 | } |
| 1025 | finally |
| 1026 | { |
| 1027 | Math.random = random; |
| 1028 | harness.close(); |
| 1029 | } |
| 1030 | }); |
| 1031 | |
| 1032 | test("stacked combatants against the same target spread to different tiles", () => |
| 1033 | { |
| 1034 | const harness = makeSimulation({ workerCount: 1 }); |
| 1035 | try |
| 1036 | { |
| 1037 | const character = makeCharacter(harness.stateDb, { tileX: 10, tileY: 8 }); |
| 1038 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1039 | const rat1 = harness.simulation.spawnEntity(mobComponents({ |
| 1040 | tileX: 9, |
| 1041 | tileY: 8, |
| 1042 | mobTypeId: MobId.rat, |
| 1043 | spawnerId: null |
| 1044 | })); |
| 1045 | const rat2 = harness.simulation.spawnEntity(mobComponents({ |
| 1046 | tileX: 9, |
| 1047 | tileY: 7, |
| 1048 | mobTypeId: MobId.rat, |
| 1049 | spawnerId: null |
| 1050 | })); |
| 1051 | const worker = workerOf(harness.simulation, playerId); |
| 1052 | teleportEntity(worker, rat2, 9, 8); |
| 1053 | |
| 1054 | for (let i = 0; i < 200; i++) |
| 1055 | { |
| 1056 | harness.advance(50); |
| 1057 | harness.simulation.step(); |
| 1058 | } |
| 1059 | |
| 1060 | const pos1 = componentsOf(harness.simulation, rat1).position; |
| 1061 | const pos2 = componentsOf(harness.simulation, rat2).position; |
| 1062 | assert.notEqual( |
| 1063 | `${pos1.tileX},${pos1.tileY}`, |
| 1064 | `${pos2.tileX},${pos2.tileY}`, |
| 1065 | "expected stacked rats to spread apart" |
| 1066 | ); |
| 1067 | assert.equal(componentsOf(harness.simulation, rat1).combat?.targetEntityId, playerId); |
| 1068 | assert.equal(componentsOf(harness.simulation, rat2).combat?.targetEntityId, playerId); |
| 1069 | } |
| 1070 | finally |
| 1071 | { harness.close(); } |
| 1072 | }); |
| 1073 | |
| 1074 | test("approaching combatants pick different adjacent goals", () => |
| 1075 | { |
| 1076 | const harness = makeSimulation({ workerCount: 1 }); |
| 1077 | try |
| 1078 | { |
| 1079 | const character = makeCharacter(harness.stateDb, { tileX: 10, tileY: 8 }); |
| 1080 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1081 | const rat1 = harness.simulation.spawnEntity(mobComponents({ |
| 1082 | tileX: 6, |
| 1083 | tileY: 8, |
| 1084 | mobTypeId: MobId.rat, |
| 1085 | spawnerId: null |
| 1086 | })); |
| 1087 | const rat2 = harness.simulation.spawnEntity(mobComponents({ |
| 1088 | tileX: 6, |
| 1089 | tileY: 7, |
| 1090 | mobTypeId: MobId.rat, |
| 1091 | spawnerId: null |
| 1092 | })); |
| 1093 | const worker = workerOf(harness.simulation, playerId); |
| 1094 | teleportEntity(worker, rat2, 6, 8); |
| 1095 | |
| 1096 | for (let i = 0; i < 120; i++) |
| 1097 | { |
| 1098 | harness.advance(50); |
| 1099 | harness.simulation.step(); |
| 1100 | } |
| 1101 | |
| 1102 | const path1 = componentsOf(harness.simulation, rat1).path?.tiles ?? []; |
| 1103 | const path2 = componentsOf(harness.simulation, rat2).path?.tiles ?? []; |
| 1104 | const goal1 = path1[path1.length - 1]; |
| 1105 | const goal2 = path2[path2.length - 1]; |
| 1106 | if (goal1 && goal2) |
| 1107 | { |
| 1108 | assert.notEqual( |
| 1109 | `${goal1.x},${goal1.y}`, |
| 1110 | `${goal2.x},${goal2.y}`, |
| 1111 | "expected different approach goals" |
| 1112 | ); |
| 1113 | } |
| 1114 | |
| 1115 | const pos1 = componentsOf(harness.simulation, rat1).position; |
| 1116 | const pos2 = componentsOf(harness.simulation, rat2).position; |
| 1117 | assert.notEqual(`${pos1.tileX},${pos1.tileY}`, `${pos2.tileX},${pos2.tileY}`); |
| 1118 | } |
| 1119 | finally |
| 1120 | { harness.close(); } |
| 1121 | }); |
| 1122 | |
| 1123 | test("humanoid attackers add equipment bonuses on top of spawner level", () => |
| 1124 | { |
| 1125 | const harness = makeSimulation({ workerCount: 1 }); |
| 1126 | const random = Math.random; |
| 1127 | /** Max every skill roll so damage equals effective damage skill. */ |
| 1128 | Math.random = () => 0.999999; |
| 1129 | try |
| 1130 | { |
| 1131 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 1132 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1133 | const player = componentsOf(harness.simulation, playerId); |
| 1134 | player.skills.xp[SkillId.agility] = 0; |
| 1135 | player.skills.xp[SkillId.defence] = 0; |
| 1136 | |
| 1137 | const level = 5; |
| 1138 | const npcId = harness.simulation.spawnEntity(mobComponents({ |
| 1139 | tileX: 9, |
| 1140 | tileY: 8, |
| 1141 | mobTypeId: MobId.humanoid, |
| 1142 | level, |
| 1143 | displayName: "Guard", |
| 1144 | sex: "male", |
| 1145 | equipment: { |
| 1146 | weapon: WoodItemId.oakQuarterstaff |
| 1147 | } |
| 1148 | })); |
| 1149 | const worker = workerOf(harness.simulation, npcId); |
| 1150 | const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(npcId)); |
| 1151 | assert.equal(profile.mobAttackSkill, level); |
| 1152 | assert.equal(profile.damageSkillId, SkillId.strength); |
| 1153 | |
| 1154 | const events = resolveMeleeSwing(worker.world, npcId, playerId, profile, Date.now()); |
| 1155 | const hit = events.find((event) => event.type === "combat-hit"); |
| 1156 | assert.ok(hit, "expected a hit"); |
| 1157 | assert.equal(hit.damage, level + tierStatBonus(WoodTier.oak)); |
| 1158 | } |
| 1159 | finally |
| 1160 | { |
| 1161 | Math.random = random; |
| 1162 | harness.close(); |
| 1163 | } |
| 1164 | }); |
| 1165 | |
| 1166 | test("staff melees like a quarterstaff and awards clubs XP", () => |
| 1167 | { |
| 1168 | const harness = makeSimulation({ workerCount: 1 }); |
| 1169 | const random = Math.random; |
| 1170 | Math.random = () => 0.999999; |
| 1171 | try |
| 1172 | { |
| 1173 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 1174 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1175 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 1176 | tileX: 9, |
| 1177 | tileY: 8, |
| 1178 | mobTypeId: MobId.deer |
| 1179 | })); |
| 1180 | const worker = workerOf(harness.simulation, playerId); |
| 1181 | const handle = worker.world.ecs.handleOf(playerId); |
| 1182 | const equipment = worker.world.ecs.get(handle, Component.equipment); |
| 1183 | equipment.slots.weapon = { itemId: GemItemId.pineStaff, quantity: 1 }; |
| 1184 | const skills = worker.world.ecs.get(handle, Component.skills); |
| 1185 | skills.xp[SkillId.clubfighting] = totalXpForLevel(10); |
| 1186 | skills.xp[SkillId.magic] = totalXpForLevel(10); |
| 1187 | const clubsBefore = skills.xp[SkillId.clubfighting]; |
| 1188 | const magicBefore = skills.xp[SkillId.magic]; |
| 1189 | |
| 1190 | const profile = attackProfileOf(worker.world.ecs, handle); |
| 1191 | assert.equal(profile.mode, "melee"); |
| 1192 | assert.equal(profile.hitSkillId, SkillId.clubfighting); |
| 1193 | assert.equal(profile.damageSkillId, SkillId.strength); |
| 1194 | |
| 1195 | const events = resolveMeleeSwing(worker.world, playerId, deerId, profile, Date.now()); |
| 1196 | const hit = events.find((event) => event.type === "combat-hit"); |
| 1197 | assert.ok(hit, "staff should land a melee hit"); |
| 1198 | const updated = events.find((event) => event.type === "skills-updated" && event.entityId === playerId); |
| 1199 | assert.ok(updated); |
| 1200 | assert.ok(updated.stored[SkillId.clubfighting] > clubsBefore, "melee staff awards clubs XP"); |
| 1201 | assert.equal(updated.stored[SkillId.magic], magicBefore, "melee staff does not award magic XP"); |
| 1202 | } |
| 1203 | finally |
| 1204 | { |
| 1205 | Math.random = random; |
| 1206 | harness.close(); |
| 1207 | } |
| 1208 | }); |
| 1209 | |
| 1210 | test("spell damage marks seekCover on a melee mob", () => |
| 1211 | { |
| 1212 | const harness = makeSimulation({ workerCount: 1 }); |
| 1213 | try |
| 1214 | { |
| 1215 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 1216 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1217 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 1218 | tileX: 12, |
| 1219 | tileY: 8, |
| 1220 | mobTypeId: MobId.deer, |
| 1221 | spawnerId: null |
| 1222 | })); |
| 1223 | const worker = workerOf(harness.simulation, playerId); |
| 1224 | applyDamageAndKill(worker.world, playerId, deerId, 1, Date.now()); |
| 1225 | assert.equal(componentsOf(harness.simulation, deerId).combat?.targetEntityId, playerId); |
| 1226 | assert.equal(componentsOf(harness.simulation, deerId).combat?.seekCover, true); |
| 1227 | } |
| 1228 | finally |
| 1229 | { harness.close(); } |
| 1230 | }); |
| 1231 | |
| 1232 | test("melee hit does not mark seekCover on a retaliating mob", () => |
| 1233 | { |
| 1234 | const random = Math.random; |
| 1235 | Math.random = () => 0; |
| 1236 | const harness = makeSimulation({ workerCount: 1 }); |
| 1237 | try |
| 1238 | { |
| 1239 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 1240 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1241 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 1242 | tileX: 9, |
| 1243 | tileY: 8, |
| 1244 | mobTypeId: MobId.deer, |
| 1245 | spawnerId: null |
| 1246 | })); |
| 1247 | const worker = workerOf(harness.simulation, playerId); |
| 1248 | const handle = worker.world.ecs.handleOf(playerId); |
| 1249 | const bag = harness.simulation.bagOf(playerId); |
| 1250 | const stickSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.stick); |
| 1251 | if (stickSlot >= 0) |
| 1252 | { |
| 1253 | harness.simulation.enqueue(1, { |
| 1254 | type: "equip-item", |
| 1255 | characterId: character.character_id, |
| 1256 | slotIndex: stickSlot |
| 1257 | }); |
| 1258 | harness.simulation.step(); |
| 1259 | } |
| 1260 | const profile = attackProfileOf(worker.world.ecs, handle); |
| 1261 | assert.equal(profile.mode, "melee"); |
| 1262 | resolveMeleeSwing(worker.world, playerId, deerId, profile, Date.now()); |
| 1263 | assert.equal(componentsOf(harness.simulation, deerId).combat?.targetEntityId, playerId); |
| 1264 | assert.equal(!!componentsOf(harness.simulation, deerId).combat?.seekCover, false); |
| 1265 | } |
| 1266 | finally |
| 1267 | { |
| 1268 | Math.random = random; |
| 1269 | harness.close(); |
| 1270 | } |
| 1271 | }); |
| 1272 | |
| 1273 | test("seekCover mob flees to a no-LOS tile when melee approach is impossible", () => |
| 1274 | { |
| 1275 | const harness = makeSimulation({ workerCount: 1 }); |
| 1276 | try |
| 1277 | { |
| 1278 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 1279 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1280 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 1281 | tileX: 14, |
| 1282 | tileY: 8, |
| 1283 | mobTypeId: MobId.deer, |
| 1284 | spawnerId: null |
| 1285 | })); |
| 1286 | const worker = workerOf(harness.simulation, playerId); |
| 1287 | const world = worker.world; |
| 1288 | const data = world.chunkData(0, 0); |
| 1289 | const wall = { typeId: BlockType.stoneWall, state: null, config: null }; |
| 1290 | const furnace = { typeId: BlockType.furnace, state: null, config: null }; |
| 1291 | // Impassable stations around the player (do not block LOS) so no melee stand tile exists. |
| 1292 | for (const [dx, dy] of [[1, 0], [-1, 0], [0, 1], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1]]) |
| 1293 | { data.blocks[(8 + dy) * 16 + (8 + dx)] = furnace; } |
| 1294 | // Cover wall east of the approach. |
| 1295 | for (let y = 6; y <= 10; y++) |
| 1296 | { data.blocks[y * 16 + 12] = wall; } |
| 1297 | |
| 1298 | assert.equal(hasCombatLineOfSight(world, 8, 8, 14, 8), false, "wall should block LOS east of the wall"); |
| 1299 | |
| 1300 | const deerHandle = world.ecs.handleOf(deerId); |
| 1301 | engageTarget(world.ecs, deerId, playerId); |
| 1302 | markSeekCover(world.ecs, deerId); |
| 1303 | const playerPos = world.ecs.get(world.ecs.handleOf(playerId), Component.position); |
| 1304 | teleportEntity(worker, deerId, 11, 8); |
| 1305 | assert.equal(hasCombatLineOfSight(world, 8, 8, 11, 8), true); |
| 1306 | queueMeleeApproach(world, deerId, deerHandle, world.ecs.get(deerHandle, Component.position), playerPos); |
| 1307 | const path = world.ecs.get(deerHandle, Component.path)?.tiles ?? []; |
| 1308 | assert.ok(path.length > 0, "expected a flee path into cover"); |
| 1309 | const dest = path[path.length - 1]; |
| 1310 | assert.equal( |
| 1311 | hasCombatLineOfSight(world, playerPos.tileX, playerPos.tileY, dest.x, dest.y), |
| 1312 | false, |
| 1313 | "flee destination must be out of player LOS" |
| 1314 | ); |
| 1315 | } |
| 1316 | finally |
| 1317 | { harness.close(); } |
| 1318 | }); |