import test from "node:test"; import assert from "node:assert/strict"; import { skullValue, skullTintColor, decayKillCounter, SKULL_PURE_RED_VALUE, SKULL_DARK_RED_VALUE, SKULL_KILL_BONUS } 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"; 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, red, dark red", () => { assert.equal(skullTintColor(0), null); assert.deepEqual(skullTintColor(1), { r: 1, g: 1, b: 1 }); 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("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(); } });