import test from "node:test"; import assert from "node:assert/strict"; import { combatLevelFromSkills, mobAggressionLevel, playerOutlevelsMobAggression } from "../../shared/combat/combatLevel.js"; import { CHUNK_AGGRO_PEACE_MS, chunkPeaceAggressionImmunity } from "../../shared/navigation/chunkPresence.js"; import { normalizePlayerSkills } from "../../shared/skills/state.js"; import { SkillId } from "../../shared/definitions/skills.js"; import { totalXpForLevel } from "../../shared/skills/progression.js"; import { mobComponents } from "../src/simulation/spawners.js"; import { MobId } from "../../shared/definitions/mobs.js"; import { Component } from "../src/ecs/components.js"; import { CHUNK_SIZE } from "../../shared/protocol/constants.js"; import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; function componentsOf (simulation, entityId) { const record = simulation.entityRecord(entityId); const worker = simulation.workers[record.workerId]; const handle = worker.world.ecs.handleOf(entityId); const out = {}; for (const key of Object.values(Component)) { const value = worker.world.ecs.get(handle, key); if (value != null) { out[key] = value; } } return out; } const COMBAT_SKILL_IDS = [ SkillId.health, SkillId.defence, SkillId.agility, SkillId.strength, SkillId.swordfighting, SkillId.axefighting, SkillId.unarmed, SkillId.clubfighting, SkillId.archery, SkillId.dexterity, SkillId.magic, SkillId.mind ]; function skillsAtLevel (level) { const xp = {}; for (const skillId of COMBAT_SKILL_IDS) { xp[skillId] = totalXpForLevel(level); } return normalizePlayerSkills(xp); } test("combatLevelFromSkills applies D + max(M, R, S)", () => { const starter = defaultPlayerSkills(); assert.equal(combatLevelFromSkills(starter), 2); const focused = skillsAtLevel(30); assert.equal(combatLevelFromSkills(focused), 40); }); test("playerOutlevelsMobAggression requires combat level above 2× mob + 1", () => { assert.equal(playerOutlevelsMobAggression(3, 1), false); assert.equal(playerOutlevelsMobAggression(4, 1), true); assert.equal(playerOutlevelsMobAggression(10, 4), true); assert.equal(playerOutlevelsMobAggression(9, 4), false); }); test("mobAggressionLevel prefers creature and spawn origin levels", () => { assert.equal(mobAggressionLevel({ level: 7 }, null, { attackSkill: 1 }), 7); assert.equal(mobAggressionLevel({}, { level: 5 }, { attackSkill: 1 }), 5); assert.equal(mobAggressionLevel({}, null, { attackSkill: 2 }), 2); }); test("chunkPeaceAggressionImmunity after fifteen minutes in the same chunk", () => { const tileToChunk = (tile) => Math.floor(tile / CHUNK_SIZE); const presence = { chunkX: 0, chunkY: 0, dimensionId: "overworld", sinceMs: 1_000 }; assert.equal( chunkPeaceAggressionImmunity(presence, 8, 8, "overworld", 1_000 + CHUNK_AGGRO_PEACE_MS - 1, tileToChunk), false ); assert.equal( chunkPeaceAggressionImmunity(presence, 8, 8, "overworld", 1_000 + CHUNK_AGGRO_PEACE_MS, tileToChunk), true ); assert.equal( chunkPeaceAggressionImmunity(presence, 256, 8, "overworld", 1_000 + CHUNK_AGGRO_PEACE_MS, tileToChunk), false ); }); test("high combat level players are ignored by aggressive rats", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); harness.stateDb.saveSkills(character.character_id, skillsAtLevel(30)); const playerId = harness.simulation.spawnCharacter(1, character); const ratId = harness.simulation.spawnEntity(mobComponents({ tileX: 12, tileY: 8, mobTypeId: MobId.rat, spawnerId: null })); harness.simulation.step(); assert.notEqual(componentsOf(harness.simulation, ratId).combat?.targetEntityId, playerId); } finally { harness.close(); } }); test("chunk peace prevents aggressive rats from auto-aggroing", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); harness.advance(CHUNK_AGGRO_PEACE_MS + 1); const ratId = harness.simulation.spawnEntity(mobComponents({ tileX: 12, tileY: 8, mobTypeId: MobId.rat, spawnerId: null })); harness.simulation.step(); assert.notEqual(componentsOf(harness.simulation, ratId).combat?.targetEntityId, playerId); } finally { harness.close(); } }); function defaultPlayerSkills () { return normalizePlayerSkills({ [SkillId.health]: totalXpForLevel(5), [SkillId.stamina]: totalXpForLevel(5) }); }