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