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