| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { |
| 4 | combatLevelFromSkills, |
| 5 | mobAggressionLevel, |
| 6 | playerOutlevelsMobAggression |
| 7 | } from "../../shared/combat/combatLevel.js"; |
| 8 | import { CHUNK_AGGRO_PEACE_MS, chunkPeaceAggressionImmunity } from "../../shared/navigation/chunkPresence.js"; |
| 9 | import { normalizePlayerSkills } from "../../shared/skills/state.js"; |
| 10 | import { SkillId } from "../../shared/definitions/skills.js"; |
| 11 | import { totalXpForLevel } from "../../shared/skills/progression.js"; |
| 12 | import { mobComponents } from "../src/simulation/spawners.js"; |
| 13 | import { MobId } from "../../shared/definitions/mobs.js"; |
| 14 | import { Component } from "../src/ecs/components.js"; |
| 15 | import { CHUNK_SIZE } from "../../shared/protocol/constants.js"; |
| 16 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 17 | |
| 18 | function componentsOf (simulation, entityId) |
| 19 | { |
| 20 | const record = simulation.entityRecord(entityId); |
| 21 | const worker = simulation.workers[record.workerId]; |
| 22 | const handle = worker.world.ecs.handleOf(entityId); |
| 23 | const out = {}; |
| 24 | for (const key of Object.values(Component)) |
| 25 | { |
| 26 | const value = worker.world.ecs.get(handle, key); |
| 27 | if (value != null) |
| 28 | { out[key] = value; } |
| 29 | } |
| 30 | return out; |
| 31 | } |
| 32 | |
| 33 | const COMBAT_SKILL_IDS = [ |
| 34 | SkillId.health, |
| 35 | SkillId.defence, |
| 36 | SkillId.agility, |
| 37 | SkillId.strength, |
| 38 | SkillId.swordfighting, |
| 39 | SkillId.axefighting, |
| 40 | SkillId.unarmed, |
| 41 | SkillId.clubfighting, |
| 42 | SkillId.archery, |
| 43 | SkillId.dexterity, |
| 44 | SkillId.magic, |
| 45 | SkillId.mind |
| 46 | ]; |
| 47 | |
| 48 | function skillsAtLevel (level) |
| 49 | { |
| 50 | const xp = {}; |
| 51 | for (const skillId of COMBAT_SKILL_IDS) |
| 52 | { xp[skillId] = totalXpForLevel(level); } |
| 53 | return normalizePlayerSkills(xp); |
| 54 | } |
| 55 | |
| 56 | test("combatLevelFromSkills applies D + max(M, R, S)", () => |
| 57 | { |
| 58 | const starter = defaultPlayerSkills(); |
| 59 | assert.equal(combatLevelFromSkills(starter), 2); |
| 60 | |
| 61 | const focused = skillsAtLevel(30); |
| 62 | assert.equal(combatLevelFromSkills(focused), 40); |
| 63 | }); |
| 64 | |
| 65 | test("playerOutlevelsMobAggression requires combat level above 2× mob + 1", () => |
| 66 | { |
| 67 | assert.equal(playerOutlevelsMobAggression(3, 1), false); |
| 68 | assert.equal(playerOutlevelsMobAggression(4, 1), true); |
| 69 | assert.equal(playerOutlevelsMobAggression(10, 4), true); |
| 70 | assert.equal(playerOutlevelsMobAggression(9, 4), false); |
| 71 | }); |
| 72 | |
| 73 | test("mobAggressionLevel prefers creature and spawn origin levels", () => |
| 74 | { |
| 75 | assert.equal(mobAggressionLevel({ level: 7 }, null, { attackSkill: 1 }), 7); |
| 76 | assert.equal(mobAggressionLevel({}, { level: 5 }, { attackSkill: 1 }), 5); |
| 77 | assert.equal(mobAggressionLevel({}, null, { attackSkill: 2 }), 2); |
| 78 | }); |
| 79 | |
| 80 | test("chunkPeaceAggressionImmunity after fifteen minutes in the same chunk", () => |
| 81 | { |
| 82 | const tileToChunk = (tile) => Math.floor(tile / CHUNK_SIZE); |
| 83 | const presence = { chunkX: 0, chunkY: 0, dimensionId: "overworld", sinceMs: 1_000 }; |
| 84 | assert.equal( |
| 85 | chunkPeaceAggressionImmunity(presence, 8, 8, "overworld", 1_000 + CHUNK_AGGRO_PEACE_MS - 1, tileToChunk), |
| 86 | false |
| 87 | ); |
| 88 | assert.equal( |
| 89 | chunkPeaceAggressionImmunity(presence, 8, 8, "overworld", 1_000 + CHUNK_AGGRO_PEACE_MS, tileToChunk), |
| 90 | true |
| 91 | ); |
| 92 | assert.equal( |
| 93 | chunkPeaceAggressionImmunity(presence, 256, 8, "overworld", 1_000 + CHUNK_AGGRO_PEACE_MS, tileToChunk), |
| 94 | false |
| 95 | ); |
| 96 | }); |
| 97 | |
| 98 | test("high combat level players are ignored by aggressive rats", () => |
| 99 | { |
| 100 | const harness = makeSimulation({ workerCount: 1 }); |
| 101 | try |
| 102 | { |
| 103 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 104 | harness.stateDb.saveSkills(character.character_id, skillsAtLevel(30)); |
| 105 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 106 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 107 | tileX: 12, |
| 108 | tileY: 8, |
| 109 | mobTypeId: MobId.rat, |
| 110 | spawnerId: null |
| 111 | })); |
| 112 | |
| 113 | harness.simulation.step(); |
| 114 | assert.notEqual(componentsOf(harness.simulation, ratId).combat?.targetEntityId, playerId); |
| 115 | } |
| 116 | finally |
| 117 | { harness.close(); } |
| 118 | }); |
| 119 | |
| 120 | test("chunk peace prevents aggressive rats from auto-aggroing", () => |
| 121 | { |
| 122 | const harness = makeSimulation({ workerCount: 1 }); |
| 123 | try |
| 124 | { |
| 125 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 126 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 127 | harness.advance(CHUNK_AGGRO_PEACE_MS + 1); |
| 128 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 129 | tileX: 12, |
| 130 | tileY: 8, |
| 131 | mobTypeId: MobId.rat, |
| 132 | spawnerId: null |
| 133 | })); |
| 134 | |
| 135 | harness.simulation.step(); |
| 136 | assert.notEqual(componentsOf(harness.simulation, ratId).combat?.targetEntityId, playerId); |
| 137 | } |
| 138 | finally |
| 139 | { harness.close(); } |
| 140 | }); |
| 141 | |
| 142 | function defaultPlayerSkills () |
| 143 | { |
| 144 | return normalizePlayerSkills({ |
| 145 | [SkillId.health]: totalXpForLevel(5), |
| 146 | [SkillId.stamina]: totalXpForLevel(5) |
| 147 | }); |
| 148 | } |