import test from "node:test"; import assert from "node:assert/strict"; import { skullValue, skullTintColor, skullAttractsTownGuards, resolveSkullThresholds, decayKillCounter, SKULL_PURE_RED_VALUE, SKULL_DARK_RED_VALUE, SKULL_KILL_BONUS, SKULL_BLACK_MAX_DEFAULT, SKULL_RED_MAX_DEFAULT } from "../../shared/combat/skull.js"; import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; import { Component } from "../src/ecs/components.js"; import { totalXpForLevel } from "../../shared/skills/progression.js"; import { SkillId } from "../../shared/definitions/skills.js"; import { WorkerMessage } from "../src/simulation/protocol.js"; import { BlockType } from "../../shared/definitions/tiles.js"; import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; function placeBlock (worldDb, tileX, tileY, typeId) { const chunkX = Math.floor(tileX / 16); const chunkY = Math.floor(tileY / 16); const data = createEmptyChunkData(); data.open = true; const localX = tileX - chunkX * 16; const localY = tileY - chunkY * 16; data.blocks[localY * 16 + localX] = { typeId, state: 0 }; worldDb.saveChunkVersion( chunkX, chunkY, worldDb.getActiveRevisionId(), true, encodeChunkBlob(data) ); } function componentsOf (simulation, entityId) { const record = simulation.entityRecord(entityId); const worker = simulation.workers[record.workerId]; const handle = worker.world.ecs.handleOf(entityId); return worker.world.ecs.componentsOf(handle); } test("skull value is bounty plus kill counter", () => { assert.equal(skullValue(100, 400), 500); assert.equal(skullValue(0, 0), 0); assert.equal(SKULL_KILL_BONUS, 500); }); test("kill counter decays one point per second", () => { assert.deepEqual(decayKillCounter(10, 3500), { killCounter: 7, consumedMs: 3000 }); assert.deepEqual(decayKillCounter(2, 10_000), { killCounter: 0, consumedMs: 2000 }); }); test("skull tint: invisible, white→black, black→red, dark red", () => { assert.equal(skullTintColor(0), null); const nearWhite = skullTintColor(1); assert.ok(nearWhite.r > 0.99 && nearWhite.g > 0.99 && nearWhite.b > 0.99); assert.deepEqual(skullTintColor(SKULL_BLACK_MAX_DEFAULT), { r: 0, g: 0, b: 0 }); assert.deepEqual(skullTintColor(SKULL_PURE_RED_VALUE), { r: 1, g: 0, b: 0 }); const dark = skullTintColor(SKULL_DARK_RED_VALUE); assert.ok(Math.abs(dark.r - 128 / 255) < 1e-9); assert.equal(dark.g, 0); assert.equal(dark.b, 0); }); test("skull thresholds honour world settings", () => { const custom = { skullBlackMax: 100, skullRedMax: 1000 }; assert.deepEqual(resolveSkullThresholds(custom), { blackMax: 100, redMax: 1000, darkRedMax: 2000 }); assert.deepEqual(skullTintColor(100, custom), { r: 0, g: 0, b: 0 }); assert.deepEqual(skullTintColor(1000, custom), { r: 1, g: 0, b: 0 }); assert.equal(skullAttractsTownGuards(100, custom), false); assert.equal(skullAttractsTownGuards(101, custom), true); assert.equal(SKULL_RED_MAX_DEFAULT, SKULL_PURE_RED_VALUE); }); test("skull attracts town guards once past black max (red lerp)", () => { assert.equal(skullAttractsTownGuards(0), false); assert.equal(skullAttractsTownGuards(SKULL_BLACK_MAX_DEFAULT), false); assert.equal(skullAttractsTownGuards(SKULL_BLACK_MAX_DEFAULT + 1), true); assert.equal(skullAttractsTownGuards(SKULL_KILL_BONUS), false); }); test("PvP kill awards temporary skull points that replicate", () => { const random = Math.random; Math.random = () => 0.5; const harness = makeSimulation({ workerCount: 1 }); try { const victim = makeCharacter(harness.stateDb, { firstName: "Vic", lastName: "Victim", tileX: 9, tileY: 8 }); const killer = makeCharacter(harness.stateDb, { firstName: "Kill", lastName: "Attacker", tileX: 8, tileY: 8 }); const victimId = harness.simulation.spawnCharacter(1, victim); const killerId = harness.simulation.spawnCharacter(2, killer); const killerComps = componentsOf(harness.simulation, killerId); killerComps.skills.xp[SkillId.unarmed] = totalXpForLevel(20); killerComps.skills.xp[SkillId.strength] = totalXpForLevel(20); componentsOf(harness.simulation, victimId).health.current = 1; harness.simulation.enqueue(2, { type: "attack", characterId: killer.character_id, targetEntityId: victimId }); let skullEvent = null; for (let i = 0; i < 40; i++) { harness.advance(200); const result = harness.simulation.step(); skullEvent = result.events.find((event) => event.type === "entity-skull" && event.entityId === killerId ) ?? result.replication?.find?.((event) => event.type === "entity-skull" && event.entityId === killerId ) ?? skullEvent; if (componentsOf(harness.simulation, victimId)[Component.dead]) { break; } } assert.ok(componentsOf(harness.simulation, victimId)[Component.dead], "victim should die"); assert.equal(harness.stateDb.peekKillCounter(killer.character_id, harness.now()), SKULL_KILL_BONUS); assert.equal(harness.simulation.directory.get(killerId).skullValue, SKULL_KILL_BONUS); assert.ok(skullEvent, "entity-skull must replicate to clients"); assert.equal(skullEvent.skullValue, SKULL_KILL_BONUS); // Directory skull survives a remember overwrite from public entity view. const worker = harness.simulation.workers[ harness.simulation.entityRecord(killerId).workerId ]; const view = worker.call({ type: WorkerMessage.inspect, entityId: killerId }); harness.simulation.directory.set(killerId, { ...view, workerId: 0 }); harness.simulation.refreshCharacterSkull(killer.character_id, { force: true }); assert.equal(harness.simulation.directory.get(killerId).skullValue, SKULL_KILL_BONUS); } finally { Math.random = random; harness.close(); } }); test("snapshots include skullValue for other players", () => { const harness = makeSimulation({ workerCount: 1 }); try { const a = makeCharacter(harness.stateDb, { firstName: "A", lastName: "One", tileX: 8, tileY: 8 }); const b = makeCharacter(harness.stateDb, { firstName: "B", lastName: "Two", tileX: 9, tileY: 8 }); const aId = harness.simulation.spawnCharacter(1, a); const bId = harness.simulation.spawnCharacter(2, b); harness.stateDb.addKillCounter(a.character_id, 500, harness.now()); harness.simulation.refreshCharacterSkull(a.character_id, { force: true }); const snaps = harness.simulation.snapshotsForView(bId, 2); const entities = snaps.flatMap((snap) => snap.entities ?? []); const seen = entities.find((entity) => entity.entityId === aId); assert.ok(seen, "viewer should see killer in snapshot"); assert.equal(seen.skullValue, 500); } finally { harness.close(); } }); test("PvP kill clears bounty from skull immediately; kill counter remains", () => { const random = Math.random; Math.random = () => 0.5; const harness = makeSimulation({ workerCount: 1 }); try { placeBlock(harness.worldDb, 8, 8, BlockType.bountyOffice); placeBlock(harness.worldDb, 7, 8, BlockType.bank); harness.simulation.reloadWorld(); const victim = makeCharacter(harness.stateDb, { firstName: "Vic", lastName: "Bounty", tileX: 9, tileY: 8 }); const killer = makeCharacter(harness.stateDb, { firstName: "Kill", lastName: "Hunter", tileX: 8, tileY: 8 }); harness.stateDb.addCharacterBounty(victim.character_id, 12_000); harness.stateDb.addKillCounter(victim.character_id, 250, harness.now()); const victimId = harness.simulation.spawnCharacter(1, victim); const killerId = harness.simulation.spawnCharacter(2, killer); harness.simulation.refreshCharacterSkull(victim.character_id, { force: true }); assert.equal(harness.simulation.directory.get(victimId).skullValue, 12_250); const killerComps = componentsOf(harness.simulation, killerId); killerComps.skills.xp[SkillId.unarmed] = totalXpForLevel(20); killerComps.skills.xp[SkillId.strength] = totalXpForLevel(20); componentsOf(harness.simulation, victimId).health.current = 1; harness.simulation.enqueue(2, { type: "attack", characterId: killer.character_id, targetEntityId: victimId }); let victimSkullEvent = null; for (let i = 0; i < 40; i++) { harness.advance(200); const result = harness.simulation.step(); victimSkullEvent = result.events.find((event) => event.type === "entity-skull" && event.entityId === victimId ) ?? result.replication?.find?.((event) => event.type === "entity-skull" && event.entityId === victimId ) ?? victimSkullEvent; if (componentsOf(harness.simulation, victimId)[Component.dead]) { break; } } assert.ok(componentsOf(harness.simulation, victimId)[Component.dead], "victim should die"); assert.equal(harness.stateDb.getCharacterBounty(victim.character_id), 0); assert.equal(harness.stateDb.peekKillCounter(victim.character_id, harness.now()), 250); assert.equal(harness.simulation.directory.get(victimId).skullValue, 250); assert.ok(victimSkullEvent, "victim entity-skull must replicate after bounty clear"); assert.equal(victimSkullEvent.skullValue, 250); assert.equal(harness.simulation.directory.get(killerId).skullValue, SKULL_KILL_BONUS); } finally { Math.random = random; harness.close(); } });