| 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 { SpellId } from "../../shared/definitions/spells.js"; |
| 16 | import { totalXpForLevel } from "../../shared/skills/progression.js"; |
| 17 | import { resolveMeleeSwing, attackProfileOf, hasCombatLineOfSight, queueMeleeApproach, markSeekCover, engageTarget, applyDamageAndKill, markFleeOnHit, stepCombat, chebyshev } from "../src/combat/service.js"; |
| 18 | import { combatDamageRollLevel } from "../../shared/combat/rolls.js"; |
| 19 | import { makeCharacter, makeSimulation, pinEastSplit, stepTimes } from "./helpers/simulationHarness.js"; |
| 20 | import { withDamagingCombatRng, installDamagingCombatRng, installMissDodgeRng, installBlockRng } from "./helpers/combatRng.js"; |
| 21 | |
| 22 | function workerOf (simulation, entityId) |
| 23 | { |
| 24 | const record = simulation.entityRecord(entityId); |
| 25 | assert.ok(record, "entity must be remembered"); |
| 26 | return simulation.workers[record.workerId]; |
| 27 | } |
| 28 | |
| 29 | function componentsOf (simulation, entityId) |
| 30 | { |
| 31 | const worker = workerOf(simulation, entityId); |
| 32 | const handle = worker.world.ecs.handleOf(entityId); |
| 33 | assert.ok(handle != null); |
| 34 | return worker.world.ecs.componentsOf(handle); |
| 35 | } |
| 36 | |
| 37 | function teleportEntity (worker, entityId, tileX, tileY) |
| 38 | { |
| 39 | const world = worker.world; |
| 40 | const handle = world.ecs.handleOf(entityId); |
| 41 | const pos = world.ecs.get(handle, Component.position); |
| 42 | const fromKey = tileKey(pos.tileX, pos.tileY); |
| 43 | const fromSet = world.occupancy.get(fromKey); |
| 44 | if (fromSet) |
| 45 | { |
| 46 | fromSet.delete(entityId); |
| 47 | if (fromSet.size === 0) |
| 48 | { world.occupancy.delete(fromKey); } |
| 49 | } |
| 50 | pos.tileX = tileX; |
| 51 | pos.tileY = tileY; |
| 52 | const toKey = tileKey(tileX, tileY); |
| 53 | let toSet = world.occupancy.get(toKey); |
| 54 | if (!toSet) |
| 55 | { |
| 56 | toSet = new Set(); |
| 57 | world.occupancy.set(toKey, toSet); |
| 58 | } |
| 59 | toSet.add(entityId); |
| 60 | } |
| 61 | |
| 62 | function entityIsAlive (simulation, entityId) |
| 63 | { |
| 64 | const record = simulation.entityRecord(entityId); |
| 65 | if (!record) |
| 66 | { return false; } |
| 67 | const worker = simulation.workers[record.workerId]; |
| 68 | const handle = worker.world.ecs.handleOf(entityId); |
| 69 | if (handle == null) |
| 70 | { return false; } |
| 71 | const health = worker.world.ecs.get(handle, Component.health); |
| 72 | return (health?.current ?? 0) > 0; |
| 73 | } |
| 74 | |
| 75 | function simulateAttackClick (harness, character, targetEntityId) |
| 76 | { |
| 77 | harness.simulation.enqueue(1, { |
| 78 | type: "attack", |
| 79 | characterId: character.character_id, |
| 80 | targetEntityId |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | /** Run ticks until one combatant dies or the step budget is exhausted. */ |
| 85 | function runCombatUntilOutcome (harness, playerId, deerId, maxSteps = 600) |
| 86 | { |
| 87 | let swings = 0; |
| 88 | for (let i = 0; i < maxSteps; i++) |
| 89 | { |
| 90 | harness.advance(50); |
| 91 | const events = harness.simulation.step().events; |
| 92 | swings += events.filter((event) => event.type === "combat-swing").length; |
| 93 | const playerAlive = entityIsAlive(harness.simulation, playerId); |
| 94 | const deerAlive = entityIsAlive(harness.simulation, deerId); |
| 95 | if (!playerAlive || !deerAlive) |
| 96 | { return { playerAlive, deerAlive, swings, steps: i + 1 }; } |
| 97 | } |
| 98 | return { |
| 99 | playerAlive: entityIsAlive(harness.simulation, playerId), |
| 100 | deerAlive: entityIsAlive(harness.simulation, deerId), |
| 101 | swings, |
| 102 | steps: maxSteps |
| 103 | }; |
| 104 | } |
| 105 | |
| 106 | function withDeterministicCombat (fn) |
| 107 | { |
| 108 | withDamagingCombatRng(fn); |
| 109 | } |
| 110 | |
| 111 | test("immortal deer auto-attacks until the player dies", () => |
| 112 | { |
| 113 | withDeterministicCombat(() => |
| 114 | { |
| 115 | const harness = makeSimulation({ workerCount: 1 }); |
| 116 | try |
| 117 | { |
| 118 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 119 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 120 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 121 | tileX: 9, |
| 122 | tileY: 8, |
| 123 | mobTypeId: MobId.deer, |
| 124 | spawnerId: null |
| 125 | })); |
| 126 | const deer = componentsOf(harness.simulation, deerId); |
| 127 | deer.health.current = 1_000_000; |
| 128 | deer.health.max = 1_000_000; |
| 129 | |
| 130 | simulateAttackClick(harness, character, deerId); |
| 131 | harness.simulation.step(); |
| 132 | |
| 133 | const outcome = runCombatUntilOutcome(harness, playerId, deerId, 2_000); |
| 134 | assert.ok(outcome.swings >= 2, `expected sustained auto-attack swings, got ${outcome.swings}`); |
| 135 | assert.equal(outcome.deerAlive, true, "immortal deer should survive"); |
| 136 | assert.equal(outcome.playerAlive, false, "deer should kill the player via auto-attack"); |
| 137 | assert.equal( |
| 138 | componentsOf(harness.simulation, deerId).combat?.targetEntityId ?? null, |
| 139 | null, |
| 140 | "deer should clear engage after the player dies" |
| 141 | ); |
| 142 | } |
| 143 | finally |
| 144 | { harness.close(); } |
| 145 | }); |
| 146 | }); |
| 147 | |
| 148 | test("immortal deer keeps auto-attacking after the player walks away", () => |
| 149 | { |
| 150 | withDeterministicCombat(() => |
| 151 | { |
| 152 | const harness = makeSimulation({ workerCount: 1 }); |
| 153 | try |
| 154 | { |
| 155 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 156 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 157 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 158 | tileX: 9, |
| 159 | tileY: 8, |
| 160 | mobTypeId: MobId.deer, |
| 161 | spawnerId: null |
| 162 | })); |
| 163 | const deer = componentsOf(harness.simulation, deerId); |
| 164 | deer.health.current = 1_000_000; |
| 165 | deer.health.max = 1_000_000; |
| 166 | |
| 167 | simulateAttackClick(harness, character, deerId); |
| 168 | harness.simulation.step(); |
| 169 | |
| 170 | // Let one exchange land, then cancel player engage by walking away (client moveTo). |
| 171 | for (let i = 0; i < 30; i++) |
| 172 | { |
| 173 | harness.advance(50); |
| 174 | const swings = harness.simulation.step().events.filter((event) => event.type === "combat-swing").length; |
| 175 | if (swings > 0) |
| 176 | { break; } |
| 177 | } |
| 178 | harness.simulation.enqueue(1, { |
| 179 | type: "move-to", |
| 180 | characterId: character.character_id, |
| 181 | tileX: 8, |
| 182 | tileY: 14 |
| 183 | }); |
| 184 | harness.simulation.step(); |
| 185 | assert.equal(componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null, null); |
| 186 | assert.equal(componentsOf(harness.simulation, deerId).combat?.targetEntityId, playerId); |
| 187 | |
| 188 | const outcome = runCombatUntilOutcome(harness, playerId, deerId, 2_000); |
| 189 | assert.ok(outcome.swings >= 1, `expected deer pursuit swings, got ${outcome.swings}`); |
| 190 | assert.equal(outcome.deerAlive, true); |
| 191 | assert.equal(outcome.playerAlive, false, "deer should chase and kill the player after walk-away"); |
| 192 | } |
| 193 | finally |
| 194 | { harness.close(); } |
| 195 | }); |
| 196 | }); |
| 197 | |
| 198 | test("cross-worker immortal deer auto-attacks until the player dies", () => |
| 199 | { |
| 200 | withDeterministicCombat(() => |
| 201 | { |
| 202 | const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); |
| 203 | try |
| 204 | { |
| 205 | const character = makeCharacter(harness.stateDb, { tileX: 64, tileY: 8 }); |
| 206 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 207 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 208 | tileX: 63, |
| 209 | tileY: 8, |
| 210 | mobTypeId: MobId.deer, |
| 211 | spawnerId: null |
| 212 | })); |
| 213 | assert.equal(harness.simulation.entityRecord(playerId).workerId, 1); |
| 214 | assert.equal(harness.simulation.entityRecord(deerId).workerId, 0); |
| 215 | |
| 216 | const deerWorker = workerOf(harness.simulation, deerId); |
| 217 | const deerHandle = deerWorker.world.ecs.handleOf(deerId); |
| 218 | const deerHealth = deerWorker.world.ecs.get(deerHandle, Component.health); |
| 219 | deerHealth.current = 1_000_000; |
| 220 | deerHealth.max = 1_000_000; |
| 221 | |
| 222 | simulateAttackClick(harness, character, deerId); |
| 223 | const first = harness.simulation.step(); |
| 224 | assert.equal(first.events.some((event) => event.type === "command-error"), false); |
| 225 | |
| 226 | const outcome = runCombatUntilOutcome(harness, playerId, deerId, 2_000); |
| 227 | assert.ok(outcome.swings >= 2, `expected sustained cross-worker swings, got ${outcome.swings}`); |
| 228 | assert.equal(outcome.deerAlive, true); |
| 229 | assert.equal(outcome.playerAlive, false, "cross-worker deer should kill the player via auto-attack"); |
| 230 | } |
| 231 | finally |
| 232 | { harness.close(); } |
| 233 | }); |
| 234 | }); |
| 235 | |
| 236 | test("click attack from 5 tiles away ends with player or deer dead", () => |
| 237 | { |
| 238 | withDeterministicCombat(() => |
| 239 | { |
| 240 | const harness = makeSimulation({ workerCount: 1 }); |
| 241 | try |
| 242 | { |
| 243 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 244 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 245 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 246 | tileX: 13, |
| 247 | tileY: 8, |
| 248 | mobTypeId: MobId.deer, |
| 249 | spawnerId: null |
| 250 | })); |
| 251 | |
| 252 | simulateAttackClick(harness, character, deerId); |
| 253 | harness.simulation.step(); |
| 254 | |
| 255 | const outcome = runCombatUntilOutcome(harness, playerId, deerId); |
| 256 | assert.ok(outcome.swings >= 1, `expected at least one swing, got ${outcome.swings}`); |
| 257 | assert.ok( |
| 258 | !outcome.playerAlive || !outcome.deerAlive, |
| 259 | `expected a kill within ${outcome.steps} steps (${outcome.swings} swings); both still alive` |
| 260 | ); |
| 261 | } |
| 262 | finally |
| 263 | { harness.close(); } |
| 264 | }); |
| 265 | }); |
| 266 | |
| 267 | test("click attack while adjacent ends with player or deer dead", () => |
| 268 | { |
| 269 | withDeterministicCombat(() => |
| 270 | { |
| 271 | const harness = makeSimulation({ workerCount: 1 }); |
| 272 | try |
| 273 | { |
| 274 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 275 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 276 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 277 | tileX: 9, |
| 278 | tileY: 8, |
| 279 | mobTypeId: MobId.deer, |
| 280 | spawnerId: null |
| 281 | })); |
| 282 | |
| 283 | simulateAttackClick(harness, character, deerId); |
| 284 | harness.simulation.step(); |
| 285 | |
| 286 | const outcome = runCombatUntilOutcome(harness, playerId, deerId); |
| 287 | assert.ok(outcome.swings >= 1, `expected at least one swing, got ${outcome.swings}`); |
| 288 | assert.ok( |
| 289 | !outcome.playerAlive || !outcome.deerAlive, |
| 290 | `expected a kill within ${outcome.steps} steps (${outcome.swings} swings); both still alive` |
| 291 | ); |
| 292 | } |
| 293 | finally |
| 294 | { harness.close(); } |
| 295 | }); |
| 296 | }); |
| 297 | |
| 298 | test("cross-worker click attack from 5 tiles away ends with player or deer dead", () => |
| 299 | { |
| 300 | withDeterministicCombat(() => |
| 301 | { |
| 302 | const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); |
| 303 | try |
| 304 | { |
| 305 | const character = makeCharacter(harness.stateDb, { tileX: 59, tileY: 8 }); |
| 306 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 307 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 308 | tileX: 64, |
| 309 | tileY: 8, |
| 310 | mobTypeId: MobId.deer, |
| 311 | spawnerId: null |
| 312 | })); |
| 313 | assert.equal(harness.simulation.entityRecord(playerId).workerId, 0); |
| 314 | assert.equal(harness.simulation.entityRecord(deerId).workerId, 1); |
| 315 | |
| 316 | simulateAttackClick(harness, character, deerId); |
| 317 | const first = harness.simulation.step(); |
| 318 | assert.equal(first.events.some((event) => event.type === "command-error"), false); |
| 319 | |
| 320 | const outcome = runCombatUntilOutcome(harness, playerId, deerId); |
| 321 | assert.ok(outcome.swings >= 1, `expected at least one swing, got ${outcome.swings}`); |
| 322 | assert.ok( |
| 323 | !outcome.playerAlive || !outcome.deerAlive, |
| 324 | `expected a kill within ${outcome.steps} steps (${outcome.swings} swings); both still alive` |
| 325 | ); |
| 326 | } |
| 327 | finally |
| 328 | { harness.close(); } |
| 329 | }); |
| 330 | }); |
| 331 | |
| 332 | test("cross-worker adjacent click attack ends with player or deer dead", () => |
| 333 | { |
| 334 | withDeterministicCombat(() => |
| 335 | { |
| 336 | const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); |
| 337 | try |
| 338 | { |
| 339 | const character = makeCharacter(harness.stateDb, { tileX: 64, tileY: 8 }); |
| 340 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 341 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 342 | tileX: 63, |
| 343 | tileY: 8, |
| 344 | mobTypeId: MobId.deer, |
| 345 | spawnerId: null |
| 346 | })); |
| 347 | assert.equal(harness.simulation.entityRecord(playerId).workerId, 1); |
| 348 | assert.equal(harness.simulation.entityRecord(deerId).workerId, 0); |
| 349 | |
| 350 | simulateAttackClick(harness, character, deerId); |
| 351 | const first = harness.simulation.step(); |
| 352 | assert.equal(first.events.some((event) => event.type === "command-error"), false); |
| 353 | |
| 354 | const outcome = runCombatUntilOutcome(harness, playerId, deerId); |
| 355 | assert.ok(outcome.swings >= 1, `expected at least one swing, got ${outcome.swings}`); |
| 356 | assert.ok( |
| 357 | !outcome.playerAlive || !outcome.deerAlive, |
| 358 | `expected a kill within ${outcome.steps} steps (${outcome.swings} swings); both still alive` |
| 359 | ); |
| 360 | } |
| 361 | finally |
| 362 | { harness.close(); } |
| 363 | }); |
| 364 | }); |
| 365 | |
| 366 | test("attack engages the attacker; target retaliates on first swing", () => |
| 367 | { |
| 368 | const harness = makeSimulation({ workerCount: 1 }); |
| 369 | try |
| 370 | { |
| 371 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 372 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 373 | // Deer are not aggressive — only retaliate after a real swing. |
| 374 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 375 | tileX: 12, |
| 376 | tileY: 8, |
| 377 | mobTypeId: MobId.deer, |
| 378 | spawnerId: null |
| 379 | })); |
| 380 | |
| 381 | harness.simulation.enqueue(1, { |
| 382 | type: "attack", |
| 383 | characterId: character.character_id, |
| 384 | targetEntityId: deerId |
| 385 | }); |
| 386 | harness.simulation.step(); |
| 387 | |
| 388 | assert.equal(componentsOf(harness.simulation, playerId).combat.targetEntityId, deerId); |
| 389 | assert.equal(componentsOf(harness.simulation, deerId).combat?.targetEntityId ?? null, null); |
| 390 | |
| 391 | let swung = false; |
| 392 | for (let i = 0; i < 80; i++) |
| 393 | { |
| 394 | harness.advance(50); |
| 395 | const events = harness.simulation.step().events; |
| 396 | if (events.some((event) => event.type === "combat-swing")) |
| 397 | { |
| 398 | swung = true; |
| 399 | break; |
| 400 | } |
| 401 | } |
| 402 | assert.ok(swung, "expected a melee swing once adjacent"); |
| 403 | assert.equal(componentsOf(harness.simulation, deerId).combat?.targetEntityId, playerId); |
| 404 | } |
| 405 | finally |
| 406 | { harness.close(); } |
| 407 | }); |
| 408 | |
| 409 | test("melee swing only resolves at Chebyshev distance 1", () => |
| 410 | { |
| 411 | const harness = makeSimulation({ workerCount: 1 }); |
| 412 | try |
| 413 | { |
| 414 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 415 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 416 | const bag = harness.simulation.bagOf(playerId); |
| 417 | const stickSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.stick); |
| 418 | assert.ok(stickSlot >= 0); |
| 419 | harness.simulation.enqueue(1, { |
| 420 | type: "equip-item", |
| 421 | characterId: character.character_id, |
| 422 | slotIndex: stickSlot |
| 423 | }); |
| 424 | harness.simulation.step(); |
| 425 | assert.equal( |
| 426 | harness.simulation.bagOf(playerId).equipment.slots[EQUIPMENT_SLOT.weapon]?.itemId, |
| 427 | ItemId.stick |
| 428 | ); |
| 429 | |
| 430 | const farRat = harness.simulation.spawnEntity(mobComponents({ |
| 431 | tileX: 12, |
| 432 | tileY: 8, |
| 433 | mobTypeId: MobId.rat |
| 434 | })); |
| 435 | harness.simulation.enqueue(1, { |
| 436 | type: "attack", |
| 437 | characterId: character.character_id, |
| 438 | targetEntityId: farRat |
| 439 | }); |
| 440 | const farEvents = harness.simulation.step().events; |
| 441 | assert.equal(farEvents.some((event) => event.type === "combat-swing"), false); |
| 442 | |
| 443 | const nearRat = harness.simulation.spawnEntity(mobComponents({ |
| 444 | tileX: 9, |
| 445 | tileY: 8, |
| 446 | mobTypeId: MobId.rat |
| 447 | })); |
| 448 | harness.simulation.enqueue(1, { |
| 449 | type: "attack", |
| 450 | characterId: character.character_id, |
| 451 | targetEntityId: nearRat |
| 452 | }); |
| 453 | const nearEvents = harness.simulation.step().events; |
| 454 | assert.ok(nearEvents.some((event) => event.type === "combat-swing")); |
| 455 | assert.ok(nearEvents.some((event) => |
| 456 | event.type === "combat-hit" |
| 457 | || event.type === "combat-miss" |
| 458 | || event.type === "combat-block")); |
| 459 | } |
| 460 | finally |
| 461 | { harness.close(); } |
| 462 | }); |
| 463 | |
| 464 | test("pigeons are untargetable free-flight and never get path tiles", () => |
| 465 | { |
| 466 | const harness = makeSimulation({ workerCount: 1 }); |
| 467 | try |
| 468 | { |
| 469 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 470 | harness.simulation.spawnCharacter(1, character); |
| 471 | const pigeonId = harness.simulation.spawnEntity(mobComponents({ |
| 472 | tileX: 10, |
| 473 | tileY: 8, |
| 474 | mobTypeId: MobId.pigeon |
| 475 | })); |
| 476 | |
| 477 | const before = componentsOf(harness.simulation, pigeonId); |
| 478 | assert.equal(before.flight != null, true); |
| 479 | assert.equal(before.combat == null, true); |
| 480 | assert.deepEqual(before.path?.tiles ?? [], []); |
| 481 | |
| 482 | harness.setNow(harness.now() + 500); |
| 483 | stepTimes(harness.simulation, 10); |
| 484 | const after = componentsOf(harness.simulation, pigeonId); |
| 485 | assert.deepEqual(after.path?.tiles ?? [], []); |
| 486 | assert.equal(after.flight != null, true); |
| 487 | |
| 488 | harness.simulation.enqueue(1, { |
| 489 | type: "attack", |
| 490 | characterId: character.character_id, |
| 491 | targetEntityId: pigeonId |
| 492 | }); |
| 493 | const result = harness.simulation.step(); |
| 494 | assert.ok(result.events.some((event) => event.type === "command-error")); |
| 495 | assert.equal(componentsOf(harness.simulation, pigeonId).path?.tiles?.length ?? 0, 0); |
| 496 | } |
| 497 | finally |
| 498 | { harness.close(); } |
| 499 | }); |
| 500 | |
| 501 | test("deer retaliate when attacked", () => |
| 502 | { |
| 503 | const harness = makeSimulation({ workerCount: 1 }); |
| 504 | try |
| 505 | { |
| 506 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 507 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 508 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 509 | tileX: 9, |
| 510 | tileY: 8, |
| 511 | mobTypeId: MobId.deer, |
| 512 | spawnerId: null |
| 513 | })); |
| 514 | |
| 515 | harness.simulation.enqueue(1, { |
| 516 | type: "attack", |
| 517 | characterId: character.character_id, |
| 518 | targetEntityId: deerId |
| 519 | }); |
| 520 | const events = harness.simulation.step().events; |
| 521 | assert.ok(events.some((event) => event.type === "combat-swing")); |
| 522 | |
| 523 | const deer = componentsOf(harness.simulation, deerId); |
| 524 | assert.equal(deer.combat?.targetEntityId, playerId); |
| 525 | assert.ok(deer.skills?.levels?.unarmed >= 1); |
| 526 | } |
| 527 | finally |
| 528 | { harness.close(); } |
| 529 | }); |
| 530 | |
| 531 | test("aggressive rats engage players within aggro range and LOS", () => |
| 532 | { |
| 533 | const harness = makeSimulation({ workerCount: 1 }); |
| 534 | try |
| 535 | { |
| 536 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 537 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 538 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 539 | tileX: 12, |
| 540 | tileY: 8, |
| 541 | mobTypeId: MobId.rat, |
| 542 | spawnerId: null |
| 543 | })); |
| 544 | |
| 545 | harness.simulation.step(); |
| 546 | assert.equal(componentsOf(harness.simulation, ratId).combat?.targetEntityId, playerId); |
| 547 | } |
| 548 | finally |
| 549 | { harness.close(); } |
| 550 | }); |
| 551 | |
| 552 | test("aggressive mobs and town guards aggro each other", () => |
| 553 | { |
| 554 | const harness = makeSimulation({ workerCount: 1 }); |
| 555 | try |
| 556 | { |
| 557 | const character = makeCharacter(harness.stateDb, { tileX: 2, tileY: 2 }); |
| 558 | harness.simulation.spawnCharacter(1, character); |
| 559 | const guardId = harness.simulation.spawnEntity(mobComponents({ |
| 560 | tileX: 10, |
| 561 | tileY: 8, |
| 562 | mobTypeId: MobId.townGuard, |
| 563 | spawnerId: "0:0:guard", |
| 564 | originTileX: 10, |
| 565 | originTileY: 8, |
| 566 | wanderRangeTiles: 4, |
| 567 | level: 10 |
| 568 | })); |
| 569 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 570 | tileX: 12, |
| 571 | tileY: 8, |
| 572 | mobTypeId: MobId.rat, |
| 573 | spawnerId: "0:0:rat", |
| 574 | originTileX: 12, |
| 575 | originTileY: 8, |
| 576 | wanderRangeTiles: 4, |
| 577 | aggressive: true |
| 578 | })); |
| 579 | |
| 580 | harness.simulation.step(); |
| 581 | const ratCombat = componentsOf(harness.simulation, ratId).combat?.targetEntityId; |
| 582 | const guardCombat = componentsOf(harness.simulation, guardId).combat?.targetEntityId; |
| 583 | assert.ok( |
| 584 | ratCombat === guardId || guardCombat === ratId, |
| 585 | `expected mutual aggro; rat→${ratCombat} guard→${guardCombat}` |
| 586 | ); |
| 587 | } |
| 588 | finally |
| 589 | { harness.close(); } |
| 590 | }); |
| 591 | |
| 592 | test("spawner mobs far from home path chunk-step toward the origin", () => |
| 593 | { |
| 594 | const harness = makeSimulation({ workerCount: 1, maxChunkX: 5 }); |
| 595 | try |
| 596 | { |
| 597 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 598 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 599 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 600 | tileX: 10, |
| 601 | tileY: 8, |
| 602 | mobTypeId: MobId.deer, |
| 603 | spawnerId: "0:0:deer", |
| 604 | originTileX: 8, |
| 605 | originTileY: 8, |
| 606 | wanderRangeTiles: 2 |
| 607 | })); |
| 608 | const worker = workerOf(harness.simulation, playerId); |
| 609 | teleportEntity(worker, deerId, 40, 8); |
| 610 | const handle = worker.world.ecs.handleOf(deerId); |
| 611 | worker.world.ecs.get(handle, Component.path).tiles = []; |
| 612 | |
| 613 | let movedCloser = false; |
| 614 | for (let i = 0; i < 40; i++) |
| 615 | { |
| 616 | harness.advance(50); |
| 617 | harness.simulation.step(); |
| 618 | const pos = worker.world.ecs.get(handle, Component.position); |
| 619 | const path = worker.world.ecs.get(handle, Component.path)?.tiles ?? []; |
| 620 | const fromX = pos.tileX; |
| 621 | const fromY = pos.tileY; |
| 622 | const fromDist = Math.max(Math.abs(fromX - 8), Math.abs(fromY - 8)); |
| 623 | if (path.length) |
| 624 | { |
| 625 | const dest = path[path.length - 1]; |
| 626 | const destDist = Math.max(Math.abs(dest.x - 8), Math.abs(dest.y - 8)); |
| 627 | if (destDist < fromDist) |
| 628 | { |
| 629 | movedCloser = true; |
| 630 | break; |
| 631 | } |
| 632 | } |
| 633 | if (fromDist < 32) |
| 634 | { |
| 635 | movedCloser = true; |
| 636 | break; |
| 637 | } |
| 638 | } |
| 639 | assert.ok(movedCloser, "expected a return path closer to the spawner"); |
| 640 | } |
| 641 | finally |
| 642 | { harness.close(); } |
| 643 | }); |
| 644 | |
| 645 | test("players retaliate when a mob swings at them", () => |
| 646 | { |
| 647 | const harness = makeSimulation({ workerCount: 1 }); |
| 648 | try |
| 649 | { |
| 650 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 651 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 652 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 653 | tileX: 9, |
| 654 | tileY: 8, |
| 655 | mobTypeId: MobId.rat, |
| 656 | spawnerId: null |
| 657 | })); |
| 658 | |
| 659 | assert.equal(componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null, null); |
| 660 | |
| 661 | let swung = false; |
| 662 | for (let i = 0; i < 80; i++) |
| 663 | { |
| 664 | harness.advance(50); |
| 665 | const events = harness.simulation.step().events; |
| 666 | if (events.some((event) => |
| 667 | event.type === "combat-swing" && event.attackerId === ratId && event.targetId === playerId |
| 668 | )) |
| 669 | { |
| 670 | swung = true; |
| 671 | break; |
| 672 | } |
| 673 | } |
| 674 | assert.ok(swung, "expected the rat to swing at the player"); |
| 675 | assert.equal( |
| 676 | componentsOf(harness.simulation, playerId).combat?.targetEntityId, |
| 677 | ratId, |
| 678 | "player should auto-retaliate after the mob swing" |
| 679 | ); |
| 680 | } |
| 681 | finally |
| 682 | { harness.close(); } |
| 683 | }); |
| 684 | |
| 685 | test("staff with favorite attack spell retaliates by auto-casting it", () => |
| 686 | { |
| 687 | const harness = makeSimulation({ workerCount: 1 }); |
| 688 | try |
| 689 | { |
| 690 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 691 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 692 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 693 | tileX: 9, |
| 694 | tileY: 8, |
| 695 | mobTypeId: MobId.rat, |
| 696 | spawnerId: null |
| 697 | })); |
| 698 | const worker = workerOf(harness.simulation, playerId); |
| 699 | const handle = worker.world.ecs.handleOf(playerId); |
| 700 | const equipment = worker.world.ecs.get(handle, Component.equipment); |
| 701 | equipment.slots.weapon = { itemId: GemItemId.pineStaff, quantity: 1 }; |
| 702 | const spells = worker.world.ecs.get(handle, Component.spells); |
| 703 | spells.known.push(SpellId.manaBullet); |
| 704 | spells.favorites = [SpellId.light, SpellId.manaBullet]; |
| 705 | spells.autoCast = false; |
| 706 | |
| 707 | const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId)); |
| 708 | resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now()); |
| 709 | |
| 710 | const casting = worker.world.ecs.get(handle, Component.spellcasting); |
| 711 | assert.equal(casting.repeatSpellId, SpellId.manaBullet); |
| 712 | assert.equal(casting.repeatTargetEntityId, ratId); |
| 713 | assert.equal( |
| 714 | componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null, |
| 715 | null, |
| 716 | "staff spell retaliate should not melee-engage" |
| 717 | ); |
| 718 | } |
| 719 | finally |
| 720 | { harness.close(); } |
| 721 | }); |
| 722 | |
| 723 | test("staff without favorite attack spell retaliates with melee", () => |
| 724 | { |
| 725 | const harness = makeSimulation({ workerCount: 1 }); |
| 726 | try |
| 727 | { |
| 728 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 729 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 730 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 731 | tileX: 9, |
| 732 | tileY: 8, |
| 733 | mobTypeId: MobId.rat, |
| 734 | spawnerId: null |
| 735 | })); |
| 736 | const worker = workerOf(harness.simulation, playerId); |
| 737 | const handle = worker.world.ecs.handleOf(playerId); |
| 738 | const equipment = worker.world.ecs.get(handle, Component.equipment); |
| 739 | equipment.slots.weapon = { itemId: GemItemId.pineStaff, quantity: 1 }; |
| 740 | const spells = worker.world.ecs.get(handle, Component.spells); |
| 741 | spells.favorites = [SpellId.light]; |
| 742 | |
| 743 | const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId)); |
| 744 | resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now()); |
| 745 | |
| 746 | assert.equal( |
| 747 | componentsOf(harness.simulation, playerId).combat?.targetEntityId, |
| 748 | ratId, |
| 749 | "staff without attack favorites should melee-retaliate" |
| 750 | ); |
| 751 | assert.equal( |
| 752 | worker.world.ecs.get(handle, Component.spellcasting)?.repeatSpellId ?? null, |
| 753 | null |
| 754 | ); |
| 755 | } |
| 756 | finally |
| 757 | { harness.close(); } |
| 758 | }); |
| 759 | |
| 760 | test("staff click-attack arms favorite spell instead of melee approach", () => |
| 761 | { |
| 762 | const harness = makeSimulation({ workerCount: 1 }); |
| 763 | try |
| 764 | { |
| 765 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 766 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 767 | // Beyond spell range so auto-cast queues a stand-in-range path (not melee adjacency). |
| 768 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 769 | tileX: 28, |
| 770 | tileY: 8, |
| 771 | mobTypeId: MobId.rat, |
| 772 | spawnerId: null |
| 773 | })); |
| 774 | const worker = workerOf(harness.simulation, playerId); |
| 775 | const handle = worker.world.ecs.handleOf(playerId); |
| 776 | const equipment = worker.world.ecs.get(handle, Component.equipment); |
| 777 | equipment.slots.weapon = { itemId: GemItemId.pineStaff, quantity: 1 }; |
| 778 | const spells = worker.world.ecs.get(handle, Component.spells); |
| 779 | spells.known.push(SpellId.manaBullet); |
| 780 | spells.favorites = [SpellId.manaBullet]; |
| 781 | spells.autoCast = false; |
| 782 | |
| 783 | harness.simulation.enqueue(1, { |
| 784 | type: "attack", |
| 785 | characterId: character.character_id, |
| 786 | targetEntityId: ratId |
| 787 | }); |
| 788 | harness.simulation.step(); |
| 789 | |
| 790 | const casting = worker.world.ecs.get(handle, Component.spellcasting); |
| 791 | assert.equal(casting.repeatSpellId, SpellId.manaBullet); |
| 792 | assert.equal(casting.repeatTargetEntityId, ratId); |
| 793 | assert.equal( |
| 794 | componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null, |
| 795 | null, |
| 796 | "staff click-attack should not melee-engage" |
| 797 | ); |
| 798 | |
| 799 | const path = componentsOf(harness.simulation, playerId).path; |
| 800 | const tiles = path?.tiles ?? []; |
| 801 | assert.ok(tiles.length > 0, "should approach into spell range"); |
| 802 | const last = tiles[tiles.length - 1]; |
| 803 | const standDist = chebyshev(last.x, last.y, 28, 8); |
| 804 | assert.ok( |
| 805 | standDist > 1, |
| 806 | `spell approach should stop outside melee adjacency, got dist=${standDist}` |
| 807 | ); |
| 808 | } |
| 809 | finally |
| 810 | { harness.close(); } |
| 811 | }); |
| 812 | |
| 813 | test("staff cast-spell-entity approaches spell range and keeps auto-repeat without autoCast", () => |
| 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: 28, |
| 822 | tileY: 8, |
| 823 | mobTypeId: MobId.rat, |
| 824 | spawnerId: null |
| 825 | })); |
| 826 | const worker = workerOf(harness.simulation, playerId); |
| 827 | const handle = worker.world.ecs.handleOf(playerId); |
| 828 | const equipment = worker.world.ecs.get(handle, Component.equipment); |
| 829 | equipment.slots.weapon = { itemId: GemItemId.pineStaff, quantity: 1 }; |
| 830 | const spells = worker.world.ecs.get(handle, Component.spells); |
| 831 | spells.known.push(SpellId.manaBullet); |
| 832 | spells.favorites = [SpellId.manaBullet]; |
| 833 | spells.autoCast = false; |
| 834 | const skills = worker.world.ecs.get(handle, Component.skills); |
| 835 | skills.xp[SkillId.magic] = totalXpForLevel(20); |
| 836 | |
| 837 | harness.simulation.enqueue(1, { |
| 838 | type: "cast-spell-entity", |
| 839 | characterId: character.character_id, |
| 840 | spellId: SpellId.manaBullet, |
| 841 | targetEntityId: ratId |
| 842 | }); |
| 843 | harness.simulation.step(); |
| 844 | |
| 845 | const casting = worker.world.ecs.get(handle, Component.spellcasting); |
| 846 | assert.equal(casting.repeatSpellId, SpellId.manaBullet); |
| 847 | assert.equal(casting.repeatTargetEntityId, ratId); |
| 848 | assert.equal(casting.pendingCast?.spellId, SpellId.manaBullet); |
| 849 | assert.equal( |
| 850 | componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null, |
| 851 | null |
| 852 | ); |
| 853 | const path = componentsOf(harness.simulation, playerId).path; |
| 854 | const tiles = path?.tiles ?? []; |
| 855 | assert.ok(tiles.length > 0, "should approach into spell range"); |
| 856 | const last = tiles[tiles.length - 1]; |
| 857 | assert.ok(chebyshev(last.x, last.y, 28, 8) > 1); |
| 858 | } |
| 859 | finally |
| 860 | { harness.close(); } |
| 861 | }); |
| 862 | |
| 863 | test("players do not auto-retaliate while walking from a click order", () => |
| 864 | { |
| 865 | const harness = makeSimulation({ workerCount: 1 }); |
| 866 | try |
| 867 | { |
| 868 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 869 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 870 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 871 | tileX: 9, |
| 872 | tileY: 8, |
| 873 | mobTypeId: MobId.rat, |
| 874 | spawnerId: null |
| 875 | })); |
| 876 | |
| 877 | harness.simulation.enqueue(1, { |
| 878 | type: "move-to", |
| 879 | characterId: character.character_id, |
| 880 | tileX: 8, |
| 881 | tileY: 14 |
| 882 | }); |
| 883 | harness.simulation.step(); |
| 884 | const path = componentsOf(harness.simulation, playerId).path; |
| 885 | assert.ok((path?.tiles?.length ?? 0) > 0, "player should be pathing"); |
| 886 | |
| 887 | let swung = false; |
| 888 | for (let i = 0; i < 80; i++) |
| 889 | { |
| 890 | harness.advance(50); |
| 891 | const events = harness.simulation.step().events; |
| 892 | if (events.some((event) => |
| 893 | event.type === "combat-swing" && event.attackerId === ratId && event.targetId === playerId |
| 894 | )) |
| 895 | { |
| 896 | swung = true; |
| 897 | break; |
| 898 | } |
| 899 | } |
| 900 | assert.ok(swung, "expected the rat to swing at the walking player"); |
| 901 | assert.equal( |
| 902 | componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null, |
| 903 | null, |
| 904 | "walking player should not auto-retaliate" |
| 905 | ); |
| 906 | assert.ok( |
| 907 | (componentsOf(harness.simulation, playerId).path?.tiles?.length ?? 0) > 0 |
| 908 | || componentsOf(harness.simulation, playerId).position.tileY !== 8, |
| 909 | "walk order should continue after the hit" |
| 910 | ); |
| 911 | } |
| 912 | finally |
| 913 | { harness.close(); } |
| 914 | }); |
| 915 | |
| 916 | test("players do not auto-retaliate while gathering", () => |
| 917 | { |
| 918 | const harness = makeSimulation({ workerCount: 1 }); |
| 919 | const restore = installDamagingCombatRng(); |
| 920 | try |
| 921 | { |
| 922 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 923 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 924 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 925 | tileX: 9, |
| 926 | tileY: 8, |
| 927 | mobTypeId: MobId.rat, |
| 928 | spawnerId: null |
| 929 | })); |
| 930 | const worker = workerOf(harness.simulation, playerId); |
| 931 | const handle = worker.world.ecs.handleOf(playerId); |
| 932 | worker.world.ecs.add(handle, Component.gather, { |
| 933 | kind: "mining", |
| 934 | tileX: 7, |
| 935 | tileY: 8, |
| 936 | materialId: "copper", |
| 937 | durationMs: 60_000, |
| 938 | startedAt: Date.now(), |
| 939 | finishesAt: Date.now() + 60_000, |
| 940 | nextSwingAt: Date.now() + 60_000 |
| 941 | }); |
| 942 | const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId)); |
| 943 | const events = resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now()); |
| 944 | assert.ok(events.some((event) => event.type === "combat-hit" || event.type === "combat-miss" || event.type === "combat-block")); |
| 945 | assert.equal( |
| 946 | componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null, |
| 947 | null, |
| 948 | "gathering player should not auto-retaliate" |
| 949 | ); |
| 950 | assert.ok( |
| 951 | worker.world.ecs.has(handle, Component.gather), |
| 952 | "gather session should survive the hit" |
| 953 | ); |
| 954 | } |
| 955 | finally |
| 956 | { |
| 957 | restore(); |
| 958 | harness.close(); |
| 959 | } |
| 960 | }); |
| 961 | |
| 962 | test("aggressive rats do not aggro through walls or beyond range", () => |
| 963 | { |
| 964 | const harness = makeSimulation({ workerCount: 1 }); |
| 965 | try |
| 966 | { |
| 967 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 968 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 969 | const blockedRat = harness.simulation.spawnEntity(mobComponents({ |
| 970 | tileX: 12, |
| 971 | tileY: 8, |
| 972 | mobTypeId: MobId.rat, |
| 973 | spawnerId: null |
| 974 | })); |
| 975 | const worker = workerOf(harness.simulation, playerId); |
| 976 | const world = worker.world; |
| 977 | const data = world.chunkData(0, 0); |
| 978 | const wall = { typeId: BlockType.stoneWall, state: null, config: null }; |
| 979 | for (let y = 6; y <= 10; y++) |
| 980 | { data.blocks[y * 16 + 10] = wall; } |
| 981 | |
| 982 | harness.simulation.step(); |
| 983 | assert.equal( |
| 984 | componentsOf(harness.simulation, blockedRat).combat?.targetEntityId ?? null, |
| 985 | null, |
| 986 | "wall blocks LOS aggro" |
| 987 | ); |
| 988 | |
| 989 | const farRat = harness.simulation.spawnEntity(mobComponents({ |
| 990 | tileX: 20, |
| 991 | tileY: 8, |
| 992 | mobTypeId: MobId.rat, |
| 993 | spawnerId: null |
| 994 | })); |
| 995 | harness.simulation.step(); |
| 996 | assert.equal( |
| 997 | componentsOf(harness.simulation, farRat).combat?.targetEntityId ?? null, |
| 998 | null, |
| 999 | "out of range does not aggro" |
| 1000 | ); |
| 1001 | } |
| 1002 | finally |
| 1003 | { harness.close(); } |
| 1004 | }); |
| 1005 | |
| 1006 | test("crafting stations and tables do not block ranged or spell line of sight", () => |
| 1007 | { |
| 1008 | assert.equal(blockBlocksRangedAttacks(BlockType.stoneWall), true); |
| 1009 | assert.equal(blockBlocksRangedAttacks(BlockType.furnace), false); |
| 1010 | assert.equal(blockBlocksRangedAttacks(BlockType.table), false); |
| 1011 | assert.equal(blockBlocksRangedAttacks(null), false); |
| 1012 | |
| 1013 | const harness = makeSimulation({ workerCount: 1 }); |
| 1014 | try |
| 1015 | { |
| 1016 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 1017 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1018 | const worker = workerOf(harness.simulation, playerId); |
| 1019 | const data = worker.world.chunkData(0, 0); |
| 1020 | const blockIndex = (localY, localX) => localY * 16 + localX; |
| 1021 | |
| 1022 | data.blocks[blockIndex(8, 10)] = { typeId: BlockType.furnace, state: null, config: null }; |
| 1023 | assert.equal( |
| 1024 | hasCombatLineOfSight(worker.world, 8, 8, 12, 8), |
| 1025 | true, |
| 1026 | "furnace between caster and target should not block" |
| 1027 | ); |
| 1028 | |
| 1029 | data.blocks[blockIndex(8, 10)] = { typeId: BlockType.table, state: null, config: null }; |
| 1030 | assert.equal( |
| 1031 | hasCombatLineOfSight(worker.world, 8, 8, 12, 8), |
| 1032 | true, |
| 1033 | "table between caster and target should not block" |
| 1034 | ); |
| 1035 | |
| 1036 | data.blocks[blockIndex(8, 10)] = { typeId: BlockType.stoneWall, state: null, config: null }; |
| 1037 | assert.equal( |
| 1038 | hasCombatLineOfSight(worker.world, 8, 8, 12, 8), |
| 1039 | false, |
| 1040 | "stone wall should still block" |
| 1041 | ); |
| 1042 | } |
| 1043 | finally |
| 1044 | { harness.close(); } |
| 1045 | }); |
| 1046 | |
| 1047 | test("attacker keeps pursuing and can swing after the target moves", () => |
| 1048 | { |
| 1049 | const harness = makeSimulation({ workerCount: 1 }); |
| 1050 | try |
| 1051 | { |
| 1052 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 1053 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1054 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 1055 | tileX: 12, |
| 1056 | tileY: 8, |
| 1057 | mobTypeId: MobId.rat, |
| 1058 | spawnerId: null |
| 1059 | })); |
| 1060 | |
| 1061 | harness.simulation.enqueue(1, { |
| 1062 | type: "attack", |
| 1063 | characterId: character.character_id, |
| 1064 | targetEntityId: ratId |
| 1065 | }); |
| 1066 | harness.simulation.step(); |
| 1067 | |
| 1068 | const worker = workerOf(harness.simulation, playerId); |
| 1069 | stepTimes(harness.simulation, 4); |
| 1070 | teleportEntity(worker, ratId, 12, 10); |
| 1071 | |
| 1072 | let swings = 0; |
| 1073 | for (let i = 0; i < 120; i++) |
| 1074 | { |
| 1075 | const events = harness.simulation.step().events; |
| 1076 | swings += events.filter((event) => event.type === "combat-swing").length; |
| 1077 | if (swings > 0) |
| 1078 | { break; } |
| 1079 | } |
| 1080 | |
| 1081 | assert.ok(swings >= 1); |
| 1082 | assert.equal(componentsOf(harness.simulation, playerId).combat?.targetEntityId, ratId); |
| 1083 | } |
| 1084 | finally |
| 1085 | { harness.close(); } |
| 1086 | }); |
| 1087 | |
| 1088 | test("combatants keep auto-attacking while adjacent", () => |
| 1089 | { |
| 1090 | const harness = makeSimulation({ workerCount: 1 }); |
| 1091 | try |
| 1092 | { |
| 1093 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 1094 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1095 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 1096 | tileX: 9, |
| 1097 | tileY: 8, |
| 1098 | mobTypeId: MobId.deer, |
| 1099 | spawnerId: null |
| 1100 | })); |
| 1101 | |
| 1102 | harness.simulation.enqueue(1, { |
| 1103 | type: "attack", |
| 1104 | characterId: character.character_id, |
| 1105 | targetEntityId: deerId |
| 1106 | }); |
| 1107 | harness.simulation.step(); |
| 1108 | |
| 1109 | let swings = 0; |
| 1110 | for (let i = 0; i < 50; i++) |
| 1111 | { |
| 1112 | harness.advance(50); |
| 1113 | const events = harness.simulation.step().events; |
| 1114 | swings += events.filter((event) => event.type === "combat-swing").length; |
| 1115 | const player = componentsOf(harness.simulation, playerId); |
| 1116 | const deer = componentsOf(harness.simulation, deerId); |
| 1117 | if (swings >= 4 |
| 1118 | && player.combat?.targetEntityId === deerId |
| 1119 | && deer.combat?.targetEntityId === playerId |
| 1120 | && (player.health?.current ?? 0) > 0) |
| 1121 | { break; } |
| 1122 | } |
| 1123 | |
| 1124 | assert.ok(swings >= 4, `expected repeated swings, got ${swings}`); |
| 1125 | const player = componentsOf(harness.simulation, playerId); |
| 1126 | const deer = componentsOf(harness.simulation, deerId); |
| 1127 | assert.ok((player.health?.current ?? 0) > 0, "player should still be alive during sustained combat"); |
| 1128 | assert.equal(player.combat?.targetEntityId, deerId); |
| 1129 | assert.equal(deer.combat?.targetEntityId, playerId); |
| 1130 | } |
| 1131 | finally |
| 1132 | { harness.close(); } |
| 1133 | }); |
| 1134 | |
| 1135 | test("cross-worker adjacent combat keeps engaging and swinging", () => |
| 1136 | { |
| 1137 | const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); |
| 1138 | try |
| 1139 | { |
| 1140 | const character = makeCharacter(harness.stateDb, { tileX: 64, tileY: 8 }); |
| 1141 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1142 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 1143 | tileX: 63, |
| 1144 | tileY: 8, |
| 1145 | mobTypeId: MobId.deer, |
| 1146 | spawnerId: null |
| 1147 | })); |
| 1148 | assert.equal(harness.simulation.entityRecord(playerId).workerId, 1); |
| 1149 | assert.equal(harness.simulation.entityRecord(deerId).workerId, 0); |
| 1150 | |
| 1151 | harness.simulation.enqueue(1, { |
| 1152 | type: "attack", |
| 1153 | characterId: character.character_id, |
| 1154 | targetEntityId: deerId |
| 1155 | }); |
| 1156 | const first = harness.simulation.step(); |
| 1157 | assert.equal(first.events.some((event) => event.type === "command-error"), false); |
| 1158 | |
| 1159 | const random = Math.random; |
| 1160 | Math.random = () => 0; |
| 1161 | try |
| 1162 | { |
| 1163 | let swings = 0; |
| 1164 | for (let i = 0; i < 40; i++) |
| 1165 | { |
| 1166 | harness.advance(50); |
| 1167 | swings += harness.simulation.step().events.filter((event) => event.type === "combat-swing").length; |
| 1168 | if (swings >= 4) |
| 1169 | { break; } |
| 1170 | } |
| 1171 | |
| 1172 | assert.ok(swings >= 4, `expected cross-worker sustained swings, got ${swings}`); |
| 1173 | const deerWorker = harness.simulation.workers[0]; |
| 1174 | const playerWorker = harness.simulation.workers[1]; |
| 1175 | const deerHandle = deerWorker.world.ecs.handleOf(deerId); |
| 1176 | const playerHandle = playerWorker.world.ecs.handleOf(playerId); |
| 1177 | const deerCombat = deerWorker.world.ecs.get(deerHandle, Component.combat); |
| 1178 | const playerCombat = playerWorker.world.ecs.get(playerHandle, Component.combat); |
| 1179 | const playerHealth = playerWorker.world.ecs.get(playerHandle, Component.health); |
| 1180 | assert.ok((playerHealth?.current ?? 0) > 0, "player should survive sustained cross-worker combat"); |
| 1181 | assert.equal(deerCombat?.targetEntityId, playerId); |
| 1182 | assert.equal(playerCombat?.targetEntityId, deerId); |
| 1183 | } |
| 1184 | finally |
| 1185 | { Math.random = random; } |
| 1186 | } |
| 1187 | finally |
| 1188 | { harness.close(); } |
| 1189 | }); |
| 1190 | |
| 1191 | test("cancel attack stops the player but the mob keeps pursuing", () => |
| 1192 | { |
| 1193 | const harness = makeSimulation({ workerCount: 1 }); |
| 1194 | try |
| 1195 | { |
| 1196 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 1197 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1198 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 1199 | tileX: 9, |
| 1200 | tileY: 8, |
| 1201 | mobTypeId: MobId.rat, |
| 1202 | spawnerId: null |
| 1203 | })); |
| 1204 | |
| 1205 | harness.simulation.enqueue(1, { |
| 1206 | type: "attack", |
| 1207 | characterId: character.character_id, |
| 1208 | targetEntityId: ratId |
| 1209 | }); |
| 1210 | harness.simulation.step(); |
| 1211 | |
| 1212 | harness.simulation.enqueue(1, { |
| 1213 | type: "cancel-attack", |
| 1214 | characterId: character.character_id |
| 1215 | }); |
| 1216 | harness.simulation.step(); |
| 1217 | |
| 1218 | const player = componentsOf(harness.simulation, playerId); |
| 1219 | const rat = componentsOf(harness.simulation, ratId); |
| 1220 | assert.equal(player.combat?.targetEntityId, null); |
| 1221 | assert.equal(rat.combat?.targetEntityId, playerId); |
| 1222 | } |
| 1223 | finally |
| 1224 | { harness.close(); } |
| 1225 | }); |
| 1226 | |
| 1227 | test("player combat and skills components are present on spawn", () => |
| 1228 | { |
| 1229 | const harness = makeSimulation({ workerCount: 1 }); |
| 1230 | try |
| 1231 | { |
| 1232 | const character = makeCharacter(harness.stateDb); |
| 1233 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1234 | const comps = componentsOf(harness.simulation, playerId); |
| 1235 | assert.ok(comps.combat); |
| 1236 | assert.equal(comps.skills?.xp?.clubfighting, 0); |
| 1237 | assert.ok(comps.health); |
| 1238 | } |
| 1239 | finally |
| 1240 | { harness.close(); } |
| 1241 | }); |
| 1242 | |
| 1243 | test("mobs heal to full when their region unloads", () => |
| 1244 | { |
| 1245 | const harness = makeSimulation({ workerCount: 1 }); |
| 1246 | try |
| 1247 | { |
| 1248 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 1249 | tileX: 8, |
| 1250 | tileY: 8, |
| 1251 | mobTypeId: MobId.rat |
| 1252 | })); |
| 1253 | const worker = workerOf(harness.simulation, ratId); |
| 1254 | const handle = worker.world.ecs.handleOf(ratId); |
| 1255 | worker.world.ecs.get(handle, Component.health).current = 2; |
| 1256 | const result = worker.call({ |
| 1257 | type: WorkerMessage.dropRegion, |
| 1258 | regionX: tileToRegion(8), |
| 1259 | regionY: tileToRegion(8) |
| 1260 | }); |
| 1261 | const record = result.dropped.find((entry) => entry.entityId === ratId); |
| 1262 | assert.ok(record); |
| 1263 | assert.equal( |
| 1264 | record.payload.components.health.current, |
| 1265 | record.payload.components.health.max |
| 1266 | ); |
| 1267 | } |
| 1268 | finally |
| 1269 | { harness.close(); } |
| 1270 | }); |
| 1271 | |
| 1272 | test("taking damage awards Health XP equal to damage dealt", () => |
| 1273 | { |
| 1274 | const harness = makeSimulation({ workerCount: 1 }); |
| 1275 | const restore = installDamagingCombatRng(); |
| 1276 | try |
| 1277 | { |
| 1278 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 1279 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1280 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 1281 | mobTypeId: MobId.rat, |
| 1282 | tileX: 9, |
| 1283 | tileY: 8 |
| 1284 | })); |
| 1285 | const worker = harness.simulation.workers[0]; |
| 1286 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 1287 | const before = worker.world.ecs.get(playerHandle, Component.skills).xp[SkillId.health]; |
| 1288 | const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId)); |
| 1289 | const events = resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now()); |
| 1290 | const hit = events.find((event) => event.type === "combat-hit"); |
| 1291 | assert.ok(hit); |
| 1292 | const updated = events.find((event) => event.type === "skills-updated" && event.entityId === playerId); |
| 1293 | assert.ok(updated); |
| 1294 | assert.equal(updated.stored[SkillId.health] - before, hit.damage); |
| 1295 | } |
| 1296 | finally |
| 1297 | { |
| 1298 | restore(); |
| 1299 | harness.close(); |
| 1300 | } |
| 1301 | }); |
| 1302 | |
| 1303 | test("lethal damage awards no Health XP", () => |
| 1304 | { |
| 1305 | const harness = makeSimulation({ workerCount: 1 }); |
| 1306 | const restore = installDamagingCombatRng(); |
| 1307 | try |
| 1308 | { |
| 1309 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 1310 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1311 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 1312 | mobTypeId: MobId.rat, |
| 1313 | tileX: 9, |
| 1314 | tileY: 8 |
| 1315 | })); |
| 1316 | const worker = harness.simulation.workers[0]; |
| 1317 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 1318 | const health = worker.world.ecs.get(playerHandle, Component.health); |
| 1319 | health.current = 1; |
| 1320 | const before = worker.world.ecs.get(playerHandle, Component.skills).xp[SkillId.health]; |
| 1321 | const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId)); |
| 1322 | const events = resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now()); |
| 1323 | const hit = events.find((event) => event.type === "combat-hit"); |
| 1324 | assert.ok(hit); |
| 1325 | assert.ok(hit.damage >= 1); |
| 1326 | assert.equal(hit.health, 0); |
| 1327 | const updated = events.find((event) => event.type === "skills-updated" && event.entityId === playerId); |
| 1328 | assert.equal(updated, undefined); |
| 1329 | assert.equal(worker.world.ecs.get(playerHandle, Component.skills).xp[SkillId.health], before); |
| 1330 | } |
| 1331 | finally |
| 1332 | { |
| 1333 | restore(); |
| 1334 | harness.close(); |
| 1335 | } |
| 1336 | }); |
| 1337 | |
| 1338 | test("dodging a hit awards Agility XP equal to avoided damage", () => |
| 1339 | { |
| 1340 | const harness = makeSimulation({ workerCount: 1 }); |
| 1341 | const restore = installMissDodgeRng(1); |
| 1342 | try |
| 1343 | { |
| 1344 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 1345 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1346 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 1347 | mobTypeId: MobId.rat, |
| 1348 | tileX: 9, |
| 1349 | tileY: 8 |
| 1350 | })); |
| 1351 | const worker = harness.simulation.workers[0]; |
| 1352 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 1353 | const skills = worker.world.ecs.get(playerHandle, Component.skills); |
| 1354 | skills.xp[SkillId.agility] = totalXpForLevel(10); |
| 1355 | const before = skills.xp[SkillId.agility] | 0; |
| 1356 | const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId)); |
| 1357 | const events = resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now()); |
| 1358 | assert.ok(events.find((event) => event.type === "combat-miss")); |
| 1359 | const updated = events.find((event) => event.type === "skills-updated" && event.entityId === playerId); |
| 1360 | assert.ok(updated); |
| 1361 | assert.equal(updated.stored[SkillId.agility] - before, 1); |
| 1362 | } |
| 1363 | finally |
| 1364 | { |
| 1365 | restore(); |
| 1366 | harness.close(); |
| 1367 | } |
| 1368 | }); |
| 1369 | |
| 1370 | test("blocking a hit awards Defence XP equal to avoided damage", () => |
| 1371 | { |
| 1372 | const harness = makeSimulation({ workerCount: 1 }); |
| 1373 | const restore = installBlockRng(1); |
| 1374 | try |
| 1375 | { |
| 1376 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 1377 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1378 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 1379 | mobTypeId: MobId.rat, |
| 1380 | tileX: 9, |
| 1381 | tileY: 8 |
| 1382 | })); |
| 1383 | const worker = harness.simulation.workers[0]; |
| 1384 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 1385 | const skills = worker.world.ecs.get(playerHandle, Component.skills); |
| 1386 | skills.xp[SkillId.defence] = totalXpForLevel(10); |
| 1387 | const before = skills.xp[SkillId.defence] | 0; |
| 1388 | const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId)); |
| 1389 | const events = resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now()); |
| 1390 | assert.ok(events.find((event) => event.type === "combat-block")); |
| 1391 | const updated = events.find((event) => event.type === "skills-updated" && event.entityId === playerId); |
| 1392 | assert.ok(updated); |
| 1393 | assert.equal(updated.stored[SkillId.defence] - before, 1); |
| 1394 | } |
| 1395 | finally |
| 1396 | { |
| 1397 | restore(); |
| 1398 | harness.close(); |
| 1399 | } |
| 1400 | }); |
| 1401 | |
| 1402 | test("stacked combatants against the same target spread to different tiles", () => |
| 1403 | { |
| 1404 | const harness = makeSimulation({ workerCount: 1 }); |
| 1405 | try |
| 1406 | { |
| 1407 | const character = makeCharacter(harness.stateDb, { tileX: 10, tileY: 8 }); |
| 1408 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1409 | const rat1 = harness.simulation.spawnEntity(mobComponents({ |
| 1410 | tileX: 9, |
| 1411 | tileY: 8, |
| 1412 | mobTypeId: MobId.rat, |
| 1413 | spawnerId: null |
| 1414 | })); |
| 1415 | const rat2 = harness.simulation.spawnEntity(mobComponents({ |
| 1416 | tileX: 9, |
| 1417 | tileY: 7, |
| 1418 | mobTypeId: MobId.rat, |
| 1419 | spawnerId: null |
| 1420 | })); |
| 1421 | const worker = workerOf(harness.simulation, playerId); |
| 1422 | const handle = worker.world.ecs.handleOf(playerId); |
| 1423 | const playerHealth = worker.world.ecs.get(handle, Component.health); |
| 1424 | playerHealth.max = 500; |
| 1425 | playerHealth.current = 500; |
| 1426 | engageTarget(worker.world.ecs, rat1, playerId); |
| 1427 | engageTarget(worker.world.ecs, rat2, playerId); |
| 1428 | assert.equal(componentsOf(harness.simulation, rat1).combat?.targetEntityId, playerId); |
| 1429 | assert.equal(componentsOf(harness.simulation, rat2).combat?.targetEntityId, playerId); |
| 1430 | teleportEntity(worker, rat2, 9, 8); |
| 1431 | |
| 1432 | for (let i = 0; i < 200; i++) |
| 1433 | { |
| 1434 | harness.advance(50); |
| 1435 | harness.simulation.step(); |
| 1436 | } |
| 1437 | |
| 1438 | const pos1 = componentsOf(harness.simulation, rat1).position; |
| 1439 | const pos2 = componentsOf(harness.simulation, rat2).position; |
| 1440 | assert.notEqual( |
| 1441 | `${pos1.tileX},${pos1.tileY}`, |
| 1442 | `${pos2.tileX},${pos2.tileY}`, |
| 1443 | "expected stacked rats to spread apart" |
| 1444 | ); |
| 1445 | } |
| 1446 | finally |
| 1447 | { harness.close(); } |
| 1448 | }); |
| 1449 | |
| 1450 | test("approaching combatants pick different adjacent goals", () => |
| 1451 | { |
| 1452 | const harness = makeSimulation({ workerCount: 1 }); |
| 1453 | try |
| 1454 | { |
| 1455 | const character = makeCharacter(harness.stateDb, { tileX: 10, tileY: 8 }); |
| 1456 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1457 | const rat1 = harness.simulation.spawnEntity(mobComponents({ |
| 1458 | tileX: 6, |
| 1459 | tileY: 8, |
| 1460 | mobTypeId: MobId.rat, |
| 1461 | spawnerId: null |
| 1462 | })); |
| 1463 | const rat2 = harness.simulation.spawnEntity(mobComponents({ |
| 1464 | tileX: 6, |
| 1465 | tileY: 7, |
| 1466 | mobTypeId: MobId.rat, |
| 1467 | spawnerId: null |
| 1468 | })); |
| 1469 | const worker = workerOf(harness.simulation, playerId); |
| 1470 | teleportEntity(worker, rat2, 6, 8); |
| 1471 | |
| 1472 | for (let i = 0; i < 120; i++) |
| 1473 | { |
| 1474 | harness.advance(50); |
| 1475 | harness.simulation.step(); |
| 1476 | } |
| 1477 | |
| 1478 | const path1 = componentsOf(harness.simulation, rat1).path?.tiles ?? []; |
| 1479 | const path2 = componentsOf(harness.simulation, rat2).path?.tiles ?? []; |
| 1480 | const goal1 = path1[path1.length - 1]; |
| 1481 | const goal2 = path2[path2.length - 1]; |
| 1482 | if (goal1 && goal2) |
| 1483 | { |
| 1484 | assert.notEqual( |
| 1485 | `${goal1.x},${goal1.y}`, |
| 1486 | `${goal2.x},${goal2.y}`, |
| 1487 | "expected different approach goals" |
| 1488 | ); |
| 1489 | } |
| 1490 | |
| 1491 | const pos1 = componentsOf(harness.simulation, rat1).position; |
| 1492 | const pos2 = componentsOf(harness.simulation, rat2).position; |
| 1493 | assert.notEqual(`${pos1.tileX},${pos1.tileY}`, `${pos2.tileX},${pos2.tileY}`); |
| 1494 | } |
| 1495 | finally |
| 1496 | { harness.close(); } |
| 1497 | }); |
| 1498 | |
| 1499 | test("humanoid attackers add equipment bonuses on top of spawner level", () => |
| 1500 | { |
| 1501 | const harness = makeSimulation({ workerCount: 1 }); |
| 1502 | const random = Math.random; |
| 1503 | /** Max every skill roll so damage equals effective damage skill. */ |
| 1504 | Math.random = () => 0.999999; |
| 1505 | try |
| 1506 | { |
| 1507 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 1508 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1509 | const player = componentsOf(harness.simulation, playerId); |
| 1510 | player.skills.xp[SkillId.agility] = 0; |
| 1511 | player.skills.xp[SkillId.defence] = 0; |
| 1512 | |
| 1513 | const level = 5; |
| 1514 | const npcId = harness.simulation.spawnEntity(mobComponents({ |
| 1515 | tileX: 9, |
| 1516 | tileY: 8, |
| 1517 | mobTypeId: MobId.humanoid, |
| 1518 | level, |
| 1519 | displayName: "Guard", |
| 1520 | sex: "male", |
| 1521 | equipment: { |
| 1522 | weapon: WoodItemId.oakQuarterstaff |
| 1523 | } |
| 1524 | })); |
| 1525 | const worker = workerOf(harness.simulation, npcId); |
| 1526 | const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(npcId)); |
| 1527 | assert.equal(profile.mobAttackSkill, level); |
| 1528 | assert.equal(profile.damageSkillId, SkillId.strength); |
| 1529 | |
| 1530 | const events = resolveMeleeSwing(worker.world, npcId, playerId, profile, Date.now()); |
| 1531 | const hit = events.find((event) => event.type === "combat-hit"); |
| 1532 | assert.ok(hit, "expected a hit"); |
| 1533 | assert.equal(hit.damage, combatDamageRollLevel(level + tierStatBonus(WoodTier.oak))); |
| 1534 | } |
| 1535 | finally |
| 1536 | { |
| 1537 | Math.random = random; |
| 1538 | harness.close(); |
| 1539 | } |
| 1540 | }); |
| 1541 | |
| 1542 | test("staff melees like a quarterstaff and awards clubs XP", () => |
| 1543 | { |
| 1544 | const harness = makeSimulation({ workerCount: 1 }); |
| 1545 | const random = Math.random; |
| 1546 | Math.random = () => 0.999999; |
| 1547 | try |
| 1548 | { |
| 1549 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 1550 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1551 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 1552 | tileX: 9, |
| 1553 | tileY: 8, |
| 1554 | mobTypeId: MobId.deer |
| 1555 | })); |
| 1556 | const worker = workerOf(harness.simulation, playerId); |
| 1557 | const handle = worker.world.ecs.handleOf(playerId); |
| 1558 | const equipment = worker.world.ecs.get(handle, Component.equipment); |
| 1559 | equipment.slots.weapon = { itemId: GemItemId.pineStaff, quantity: 1 }; |
| 1560 | const skills = worker.world.ecs.get(handle, Component.skills); |
| 1561 | skills.xp[SkillId.clubfighting] = totalXpForLevel(10); |
| 1562 | skills.xp[SkillId.magic] = totalXpForLevel(10); |
| 1563 | const clubsBefore = skills.xp[SkillId.clubfighting]; |
| 1564 | const magicBefore = skills.xp[SkillId.magic]; |
| 1565 | |
| 1566 | const profile = attackProfileOf(worker.world.ecs, handle); |
| 1567 | assert.equal(profile.mode, "melee"); |
| 1568 | assert.equal(profile.hitSkillId, SkillId.clubfighting); |
| 1569 | assert.equal(profile.damageSkillId, SkillId.strength); |
| 1570 | |
| 1571 | const events = resolveMeleeSwing(worker.world, playerId, deerId, profile, Date.now()); |
| 1572 | const hit = events.find((event) => event.type === "combat-hit"); |
| 1573 | assert.ok(hit, "staff should land a melee hit"); |
| 1574 | const updated = events.find((event) => event.type === "skills-updated" && event.entityId === playerId); |
| 1575 | assert.ok(updated); |
| 1576 | assert.ok(updated.stored[SkillId.clubfighting] > clubsBefore, "melee staff awards clubs XP"); |
| 1577 | assert.equal(updated.stored[SkillId.magic], magicBefore, "melee staff does not award magic XP"); |
| 1578 | } |
| 1579 | finally |
| 1580 | { |
| 1581 | Math.random = random; |
| 1582 | harness.close(); |
| 1583 | } |
| 1584 | }); |
| 1585 | |
| 1586 | test("spell damage marks seekCover on a melee mob", () => |
| 1587 | { |
| 1588 | const harness = makeSimulation({ workerCount: 1 }); |
| 1589 | try |
| 1590 | { |
| 1591 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 1592 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1593 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 1594 | tileX: 12, |
| 1595 | tileY: 8, |
| 1596 | mobTypeId: MobId.deer, |
| 1597 | spawnerId: null |
| 1598 | })); |
| 1599 | const worker = workerOf(harness.simulation, playerId); |
| 1600 | applyDamageAndKill(worker.world, playerId, deerId, 1, Date.now()); |
| 1601 | assert.equal(componentsOf(harness.simulation, deerId).combat?.targetEntityId, playerId); |
| 1602 | assert.equal(componentsOf(harness.simulation, deerId).combat?.seekCover, true); |
| 1603 | } |
| 1604 | finally |
| 1605 | { harness.close(); } |
| 1606 | }); |
| 1607 | |
| 1608 | test("melee hit does not mark seekCover on a retaliating mob", () => |
| 1609 | { |
| 1610 | const random = Math.random; |
| 1611 | Math.random = () => 0; |
| 1612 | const harness = makeSimulation({ workerCount: 1 }); |
| 1613 | try |
| 1614 | { |
| 1615 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 1616 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1617 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 1618 | tileX: 9, |
| 1619 | tileY: 8, |
| 1620 | mobTypeId: MobId.deer, |
| 1621 | spawnerId: null |
| 1622 | })); |
| 1623 | const worker = workerOf(harness.simulation, playerId); |
| 1624 | const handle = worker.world.ecs.handleOf(playerId); |
| 1625 | const bag = harness.simulation.bagOf(playerId); |
| 1626 | const stickSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.stick); |
| 1627 | if (stickSlot >= 0) |
| 1628 | { |
| 1629 | harness.simulation.enqueue(1, { |
| 1630 | type: "equip-item", |
| 1631 | characterId: character.character_id, |
| 1632 | slotIndex: stickSlot |
| 1633 | }); |
| 1634 | harness.simulation.step(); |
| 1635 | } |
| 1636 | const profile = attackProfileOf(worker.world.ecs, handle); |
| 1637 | assert.equal(profile.mode, "melee"); |
| 1638 | resolveMeleeSwing(worker.world, playerId, deerId, profile, Date.now()); |
| 1639 | assert.equal(componentsOf(harness.simulation, deerId).combat?.targetEntityId, playerId); |
| 1640 | assert.equal(!!componentsOf(harness.simulation, deerId).combat?.seekCover, false); |
| 1641 | } |
| 1642 | finally |
| 1643 | { |
| 1644 | Math.random = random; |
| 1645 | harness.close(); |
| 1646 | } |
| 1647 | }); |
| 1648 | |
| 1649 | test("seekCover mob flees to a no-LOS tile when melee approach is impossible", () => |
| 1650 | { |
| 1651 | const harness = makeSimulation({ workerCount: 1 }); |
| 1652 | try |
| 1653 | { |
| 1654 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 1655 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1656 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 1657 | tileX: 14, |
| 1658 | tileY: 8, |
| 1659 | mobTypeId: MobId.deer, |
| 1660 | spawnerId: null |
| 1661 | })); |
| 1662 | const worker = workerOf(harness.simulation, playerId); |
| 1663 | const world = worker.world; |
| 1664 | const data = world.chunkData(0, 0); |
| 1665 | const wall = { typeId: BlockType.stoneWall, state: null, config: null }; |
| 1666 | const furnace = { typeId: BlockType.furnace, state: null, config: null }; |
| 1667 | // Impassable stations around the player (do not block LOS) so no melee stand tile exists. |
| 1668 | for (const [dx, dy] of [[1, 0], [-1, 0], [0, 1], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1]]) |
| 1669 | { data.blocks[(8 + dy) * 16 + (8 + dx)] = furnace; } |
| 1670 | // Cover wall east of the approach. |
| 1671 | for (let y = 6; y <= 10; y++) |
| 1672 | { data.blocks[y * 16 + 12] = wall; } |
| 1673 | |
| 1674 | assert.equal(hasCombatLineOfSight(world, 8, 8, 14, 8), false, "wall should block LOS east of the wall"); |
| 1675 | |
| 1676 | const deerHandle = world.ecs.handleOf(deerId); |
| 1677 | engageTarget(world.ecs, deerId, playerId); |
| 1678 | markSeekCover(world.ecs, deerId); |
| 1679 | const playerPos = world.ecs.get(world.ecs.handleOf(playerId), Component.position); |
| 1680 | teleportEntity(worker, deerId, 11, 8); |
| 1681 | assert.equal(hasCombatLineOfSight(world, 8, 8, 11, 8), true); |
| 1682 | queueMeleeApproach(world, deerId, deerHandle, world.ecs.get(deerHandle, Component.position), playerPos); |
| 1683 | const path = world.ecs.get(deerHandle, Component.path)?.tiles ?? []; |
| 1684 | assert.ok(path.length > 0, "expected a flee path into cover"); |
| 1685 | const dest = path[path.length - 1]; |
| 1686 | assert.equal( |
| 1687 | hasCombatLineOfSight(world, playerPos.tileX, playerPos.tileY, dest.x, dest.y), |
| 1688 | false, |
| 1689 | "flee destination must be out of player LOS" |
| 1690 | ); |
| 1691 | } |
| 1692 | finally |
| 1693 | { harness.close(); } |
| 1694 | }); |
| 1695 | |
| 1696 | test("fairy flees to 2 tiles from hit source, then stops until hit again", () => |
| 1697 | { |
| 1698 | const harness = makeSimulation({ workerCount: 1 }); |
| 1699 | try |
| 1700 | { |
| 1701 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 1702 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1703 | const fairyId = harness.simulation.spawnEntity(mobComponents({ |
| 1704 | tileX: 9, |
| 1705 | tileY: 8, |
| 1706 | mobTypeId: MobId.fairy, |
| 1707 | spawnerId: null |
| 1708 | })); |
| 1709 | const worker = workerOf(harness.simulation, fairyId); |
| 1710 | const world = worker.world; |
| 1711 | const fairyHandle = world.ecs.handleOf(fairyId); |
| 1712 | |
| 1713 | applyDamageAndKill(world, playerId, fairyId, 1, Date.now()); |
| 1714 | const combat = world.ecs.get(fairyHandle, Component.combat); |
| 1715 | assert.equal(combat?.fleeFrom?.tileX, 8); |
| 1716 | assert.equal(combat?.fleeFrom?.tileY, 8); |
| 1717 | assert.equal(combat?.fleeFrom?.minDist, 2); |
| 1718 | assert.equal(combat?.targetEntityId, playerId); |
| 1719 | |
| 1720 | stepCombat(world, Date.now()); |
| 1721 | const path = world.ecs.get(fairyHandle, Component.path)?.tiles ?? []; |
| 1722 | assert.ok(path.length > 0, "fairy should path away while fleeing"); |
| 1723 | const dest = path[path.length - 1]; |
| 1724 | assert.ok( |
| 1725 | chebyshev(dest.x, dest.y, 8, 8) >= 2, |
| 1726 | `flee dest should be ≥2 from hit source, got ${dest.x},${dest.y}` |
| 1727 | ); |
| 1728 | |
| 1729 | teleportEntity(worker, fairyId, dest.x, dest.y); |
| 1730 | world.ecs.get(fairyHandle, Component.path).tiles = []; |
| 1731 | stepCombat(world, Date.now()); |
| 1732 | assert.equal(world.ecs.get(fairyHandle, Component.combat)?.fleeFrom, null); |
| 1733 | |
| 1734 | stepCombat(world, Date.now() + 5000); |
| 1735 | assert.equal( |
| 1736 | world.ecs.get(fairyHandle, Component.combat)?.fleeFrom, |
| 1737 | null, |
| 1738 | "no flee until hit again" |
| 1739 | ); |
| 1740 | |
| 1741 | teleportEntity(worker, fairyId, 9, 8); |
| 1742 | applyDamageAndKill(world, playerId, fairyId, 1, Date.now()); |
| 1743 | assert.equal(world.ecs.get(fairyHandle, Component.combat)?.fleeFrom?.minDist, 2); |
| 1744 | |
| 1745 | teleportEntity(worker, fairyId, 12, 8); |
| 1746 | markFleeOnHit(world, fairyId, playerId); |
| 1747 | assert.equal( |
| 1748 | world.ecs.get(fairyHandle, Component.combat)?.fleeFrom, |
| 1749 | null, |
| 1750 | "already ≥2 tiles away does not start a flee" |
| 1751 | ); |
| 1752 | } |
| 1753 | finally |
| 1754 | { harness.close(); } |
| 1755 | }); |