| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import |
| 4 | { |
| 5 | skullValue, |
| 6 | skullTintColor, |
| 7 | decayKillCounter, |
| 8 | SKULL_PURE_RED_VALUE, |
| 9 | SKULL_DARK_RED_VALUE, |
| 10 | SKULL_KILL_BONUS |
| 11 | } from "../../shared/combat/skull.js"; |
| 12 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 13 | import { Component } from "../src/ecs/components.js"; |
| 14 | import { totalXpForLevel } from "../../shared/skills/progression.js"; |
| 15 | import { SkillId } from "../../shared/definitions/skills.js"; |
| 16 | import { WorkerMessage } from "../src/simulation/protocol.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 | return worker.world.ecs.componentsOf(handle); |
| 24 | } |
| 25 | |
| 26 | test("skull value is bounty plus kill counter", () => |
| 27 | { |
| 28 | assert.equal(skullValue(100, 400), 500); |
| 29 | assert.equal(skullValue(0, 0), 0); |
| 30 | assert.equal(SKULL_KILL_BONUS, 500); |
| 31 | }); |
| 32 | |
| 33 | test("kill counter decays one point per second", () => |
| 34 | { |
| 35 | assert.deepEqual(decayKillCounter(10, 3500), { killCounter: 7, consumedMs: 3000 }); |
| 36 | assert.deepEqual(decayKillCounter(2, 10_000), { killCounter: 0, consumedMs: 2000 }); |
| 37 | }); |
| 38 | |
| 39 | test("skull tint: invisible, white, red, dark red", () => |
| 40 | { |
| 41 | assert.equal(skullTintColor(0), null); |
| 42 | assert.deepEqual(skullTintColor(1), { r: 1, g: 1, b: 1 }); |
| 43 | assert.deepEqual(skullTintColor(SKULL_PURE_RED_VALUE), { r: 1, g: 0, b: 0 }); |
| 44 | const dark = skullTintColor(SKULL_DARK_RED_VALUE); |
| 45 | assert.ok(Math.abs(dark.r - 128 / 255) < 1e-9); |
| 46 | assert.equal(dark.g, 0); |
| 47 | assert.equal(dark.b, 0); |
| 48 | }); |
| 49 | |
| 50 | test("PvP kill awards temporary skull points that replicate", () => |
| 51 | { |
| 52 | const random = Math.random; |
| 53 | Math.random = () => 0.5; |
| 54 | const harness = makeSimulation({ workerCount: 1 }); |
| 55 | try |
| 56 | { |
| 57 | const victim = makeCharacter(harness.stateDb, { |
| 58 | firstName: "Vic", |
| 59 | lastName: "Victim", |
| 60 | tileX: 9, |
| 61 | tileY: 8 |
| 62 | }); |
| 63 | const killer = makeCharacter(harness.stateDb, { |
| 64 | firstName: "Kill", |
| 65 | lastName: "Attacker", |
| 66 | tileX: 8, |
| 67 | tileY: 8 |
| 68 | }); |
| 69 | const victimId = harness.simulation.spawnCharacter(1, victim); |
| 70 | const killerId = harness.simulation.spawnCharacter(2, killer); |
| 71 | |
| 72 | const killerComps = componentsOf(harness.simulation, killerId); |
| 73 | killerComps.skills.xp[SkillId.unarmed] = totalXpForLevel(20); |
| 74 | killerComps.skills.xp[SkillId.strength] = totalXpForLevel(20); |
| 75 | componentsOf(harness.simulation, victimId).health.current = 1; |
| 76 | |
| 77 | harness.simulation.enqueue(2, { |
| 78 | type: "attack", |
| 79 | characterId: killer.character_id, |
| 80 | targetEntityId: victimId |
| 81 | }); |
| 82 | let skullEvent = null; |
| 83 | for (let i = 0; i < 40; i++) |
| 84 | { |
| 85 | harness.advance(200); |
| 86 | const result = harness.simulation.step(); |
| 87 | skullEvent = result.events.find((event) => |
| 88 | event.type === "entity-skull" && event.entityId === killerId |
| 89 | ) ?? result.replication?.find?.((event) => |
| 90 | event.type === "entity-skull" && event.entityId === killerId |
| 91 | ) ?? skullEvent; |
| 92 | if (componentsOf(harness.simulation, victimId)[Component.dead]) |
| 93 | { break; } |
| 94 | } |
| 95 | assert.ok(componentsOf(harness.simulation, victimId)[Component.dead], "victim should die"); |
| 96 | assert.equal(harness.stateDb.peekKillCounter(killer.character_id, harness.now()), SKULL_KILL_BONUS); |
| 97 | assert.equal(harness.simulation.directory.get(killerId).skullValue, SKULL_KILL_BONUS); |
| 98 | assert.ok(skullEvent, "entity-skull must replicate to clients"); |
| 99 | assert.equal(skullEvent.skullValue, SKULL_KILL_BONUS); |
| 100 | |
| 101 | // Directory skull survives a remember overwrite from public entity view. |
| 102 | const worker = harness.simulation.workers[ |
| 103 | harness.simulation.entityRecord(killerId).workerId |
| 104 | ]; |
| 105 | const view = worker.call({ type: WorkerMessage.inspect, entityId: killerId }); |
| 106 | harness.simulation.directory.set(killerId, { ...view, workerId: 0 }); |
| 107 | harness.simulation.refreshCharacterSkull(killer.character_id, { force: true }); |
| 108 | assert.equal(harness.simulation.directory.get(killerId).skullValue, SKULL_KILL_BONUS); |
| 109 | } |
| 110 | finally |
| 111 | { |
| 112 | Math.random = random; |
| 113 | harness.close(); |
| 114 | } |
| 115 | }); |
| 116 | |
| 117 | test("snapshots include skullValue for other players", () => |
| 118 | { |
| 119 | const harness = makeSimulation({ workerCount: 1 }); |
| 120 | try |
| 121 | { |
| 122 | const a = makeCharacter(harness.stateDb, { firstName: "A", lastName: "One", tileX: 8, tileY: 8 }); |
| 123 | const b = makeCharacter(harness.stateDb, { firstName: "B", lastName: "Two", tileX: 9, tileY: 8 }); |
| 124 | const aId = harness.simulation.spawnCharacter(1, a); |
| 125 | const bId = harness.simulation.spawnCharacter(2, b); |
| 126 | harness.stateDb.addKillCounter(a.character_id, 500, harness.now()); |
| 127 | harness.simulation.refreshCharacterSkull(a.character_id, { force: true }); |
| 128 | const snaps = harness.simulation.snapshotsForView(bId, 2); |
| 129 | const entities = snaps.flatMap((snap) => snap.entities ?? []); |
| 130 | const seen = entities.find((entity) => entity.entityId === aId); |
| 131 | assert.ok(seen, "viewer should see killer in snapshot"); |
| 132 | assert.equal(seen.skullValue, 500); |
| 133 | } |
| 134 | finally |
| 135 | { harness.close(); } |
| 136 | }); |