| 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 | import { BlockType } from "../../shared/definitions/tiles.js"; |
| 18 | import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; |
| 19 | |
| 20 | function placeBlock (worldDb, tileX, tileY, typeId) |
| 21 | { |
| 22 | const chunkX = Math.floor(tileX / 16); |
| 23 | const chunkY = Math.floor(tileY / 16); |
| 24 | const data = createEmptyChunkData(); |
| 25 | data.open = true; |
| 26 | const localX = tileX - chunkX * 16; |
| 27 | const localY = tileY - chunkY * 16; |
| 28 | data.blocks[localY * 16 + localX] = { typeId, state: 0 }; |
| 29 | worldDb.saveChunkVersion( |
| 30 | chunkX, |
| 31 | chunkY, |
| 32 | worldDb.getActiveRevisionId(), |
| 33 | true, |
| 34 | encodeChunkBlob(data) |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | function componentsOf (simulation, entityId) |
| 39 | { |
| 40 | const record = simulation.entityRecord(entityId); |
| 41 | const worker = simulation.workers[record.workerId]; |
| 42 | const handle = worker.world.ecs.handleOf(entityId); |
| 43 | return worker.world.ecs.componentsOf(handle); |
| 44 | } |
| 45 | |
| 46 | test("skull value is bounty plus kill counter", () => |
| 47 | { |
| 48 | assert.equal(skullValue(100, 400), 500); |
| 49 | assert.equal(skullValue(0, 0), 0); |
| 50 | assert.equal(SKULL_KILL_BONUS, 500); |
| 51 | }); |
| 52 | |
| 53 | test("kill counter decays one point per second", () => |
| 54 | { |
| 55 | assert.deepEqual(decayKillCounter(10, 3500), { killCounter: 7, consumedMs: 3000 }); |
| 56 | assert.deepEqual(decayKillCounter(2, 10_000), { killCounter: 0, consumedMs: 2000 }); |
| 57 | }); |
| 58 | |
| 59 | test("skull tint: invisible, white, red, dark red", () => |
| 60 | { |
| 61 | assert.equal(skullTintColor(0), null); |
| 62 | assert.deepEqual(skullTintColor(1), { r: 1, g: 1, b: 1 }); |
| 63 | assert.deepEqual(skullTintColor(SKULL_PURE_RED_VALUE), { r: 1, g: 0, b: 0 }); |
| 64 | const dark = skullTintColor(SKULL_DARK_RED_VALUE); |
| 65 | assert.ok(Math.abs(dark.r - 128 / 255) < 1e-9); |
| 66 | assert.equal(dark.g, 0); |
| 67 | assert.equal(dark.b, 0); |
| 68 | }); |
| 69 | |
| 70 | test("PvP kill awards temporary skull points that replicate", () => |
| 71 | { |
| 72 | const random = Math.random; |
| 73 | Math.random = () => 0.5; |
| 74 | const harness = makeSimulation({ workerCount: 1 }); |
| 75 | try |
| 76 | { |
| 77 | const victim = makeCharacter(harness.stateDb, { |
| 78 | firstName: "Vic", |
| 79 | lastName: "Victim", |
| 80 | tileX: 9, |
| 81 | tileY: 8 |
| 82 | }); |
| 83 | const killer = makeCharacter(harness.stateDb, { |
| 84 | firstName: "Kill", |
| 85 | lastName: "Attacker", |
| 86 | tileX: 8, |
| 87 | tileY: 8 |
| 88 | }); |
| 89 | const victimId = harness.simulation.spawnCharacter(1, victim); |
| 90 | const killerId = harness.simulation.spawnCharacter(2, killer); |
| 91 | |
| 92 | const killerComps = componentsOf(harness.simulation, killerId); |
| 93 | killerComps.skills.xp[SkillId.unarmed] = totalXpForLevel(20); |
| 94 | killerComps.skills.xp[SkillId.strength] = totalXpForLevel(20); |
| 95 | componentsOf(harness.simulation, victimId).health.current = 1; |
| 96 | |
| 97 | harness.simulation.enqueue(2, { |
| 98 | type: "attack", |
| 99 | characterId: killer.character_id, |
| 100 | targetEntityId: victimId |
| 101 | }); |
| 102 | let skullEvent = null; |
| 103 | for (let i = 0; i < 40; i++) |
| 104 | { |
| 105 | harness.advance(200); |
| 106 | const result = harness.simulation.step(); |
| 107 | skullEvent = result.events.find((event) => |
| 108 | event.type === "entity-skull" && event.entityId === killerId |
| 109 | ) ?? result.replication?.find?.((event) => |
| 110 | event.type === "entity-skull" && event.entityId === killerId |
| 111 | ) ?? skullEvent; |
| 112 | if (componentsOf(harness.simulation, victimId)[Component.dead]) |
| 113 | { break; } |
| 114 | } |
| 115 | assert.ok(componentsOf(harness.simulation, victimId)[Component.dead], "victim should die"); |
| 116 | assert.equal(harness.stateDb.peekKillCounter(killer.character_id, harness.now()), SKULL_KILL_BONUS); |
| 117 | assert.equal(harness.simulation.directory.get(killerId).skullValue, SKULL_KILL_BONUS); |
| 118 | assert.ok(skullEvent, "entity-skull must replicate to clients"); |
| 119 | assert.equal(skullEvent.skullValue, SKULL_KILL_BONUS); |
| 120 | |
| 121 | // Directory skull survives a remember overwrite from public entity view. |
| 122 | const worker = harness.simulation.workers[ |
| 123 | harness.simulation.entityRecord(killerId).workerId |
| 124 | ]; |
| 125 | const view = worker.call({ type: WorkerMessage.inspect, entityId: killerId }); |
| 126 | harness.simulation.directory.set(killerId, { ...view, workerId: 0 }); |
| 127 | harness.simulation.refreshCharacterSkull(killer.character_id, { force: true }); |
| 128 | assert.equal(harness.simulation.directory.get(killerId).skullValue, SKULL_KILL_BONUS); |
| 129 | } |
| 130 | finally |
| 131 | { |
| 132 | Math.random = random; |
| 133 | harness.close(); |
| 134 | } |
| 135 | }); |
| 136 | |
| 137 | test("snapshots include skullValue for other players", () => |
| 138 | { |
| 139 | const harness = makeSimulation({ workerCount: 1 }); |
| 140 | try |
| 141 | { |
| 142 | const a = makeCharacter(harness.stateDb, { firstName: "A", lastName: "One", tileX: 8, tileY: 8 }); |
| 143 | const b = makeCharacter(harness.stateDb, { firstName: "B", lastName: "Two", tileX: 9, tileY: 8 }); |
| 144 | const aId = harness.simulation.spawnCharacter(1, a); |
| 145 | const bId = harness.simulation.spawnCharacter(2, b); |
| 146 | harness.stateDb.addKillCounter(a.character_id, 500, harness.now()); |
| 147 | harness.simulation.refreshCharacterSkull(a.character_id, { force: true }); |
| 148 | const snaps = harness.simulation.snapshotsForView(bId, 2); |
| 149 | const entities = snaps.flatMap((snap) => snap.entities ?? []); |
| 150 | const seen = entities.find((entity) => entity.entityId === aId); |
| 151 | assert.ok(seen, "viewer should see killer in snapshot"); |
| 152 | assert.equal(seen.skullValue, 500); |
| 153 | } |
| 154 | finally |
| 155 | { harness.close(); } |
| 156 | }); |
| 157 | |
| 158 | test("PvP kill clears bounty from skull immediately; kill counter remains", () => |
| 159 | { |
| 160 | const random = Math.random; |
| 161 | Math.random = () => 0.5; |
| 162 | const harness = makeSimulation({ workerCount: 1 }); |
| 163 | try |
| 164 | { |
| 165 | placeBlock(harness.worldDb, 8, 8, BlockType.bountyOffice); |
| 166 | placeBlock(harness.worldDb, 7, 8, BlockType.bank); |
| 167 | harness.simulation.reloadWorld(); |
| 168 | |
| 169 | const victim = makeCharacter(harness.stateDb, { |
| 170 | firstName: "Vic", |
| 171 | lastName: "Bounty", |
| 172 | tileX: 9, |
| 173 | tileY: 8 |
| 174 | }); |
| 175 | const killer = makeCharacter(harness.stateDb, { |
| 176 | firstName: "Kill", |
| 177 | lastName: "Hunter", |
| 178 | tileX: 8, |
| 179 | tileY: 8 |
| 180 | }); |
| 181 | harness.stateDb.addCharacterBounty(victim.character_id, 12_000); |
| 182 | harness.stateDb.addKillCounter(victim.character_id, 250, harness.now()); |
| 183 | const victimId = harness.simulation.spawnCharacter(1, victim); |
| 184 | const killerId = harness.simulation.spawnCharacter(2, killer); |
| 185 | harness.simulation.refreshCharacterSkull(victim.character_id, { force: true }); |
| 186 | assert.equal(harness.simulation.directory.get(victimId).skullValue, 12_250); |
| 187 | |
| 188 | const killerComps = componentsOf(harness.simulation, killerId); |
| 189 | killerComps.skills.xp[SkillId.unarmed] = totalXpForLevel(20); |
| 190 | killerComps.skills.xp[SkillId.strength] = totalXpForLevel(20); |
| 191 | componentsOf(harness.simulation, victimId).health.current = 1; |
| 192 | |
| 193 | harness.simulation.enqueue(2, { |
| 194 | type: "attack", |
| 195 | characterId: killer.character_id, |
| 196 | targetEntityId: victimId |
| 197 | }); |
| 198 | let victimSkullEvent = null; |
| 199 | for (let i = 0; i < 40; i++) |
| 200 | { |
| 201 | harness.advance(200); |
| 202 | const result = harness.simulation.step(); |
| 203 | victimSkullEvent = result.events.find((event) => |
| 204 | event.type === "entity-skull" && event.entityId === victimId |
| 205 | ) ?? result.replication?.find?.((event) => |
| 206 | event.type === "entity-skull" && event.entityId === victimId |
| 207 | ) ?? victimSkullEvent; |
| 208 | if (componentsOf(harness.simulation, victimId)[Component.dead]) |
| 209 | { break; } |
| 210 | } |
| 211 | assert.ok(componentsOf(harness.simulation, victimId)[Component.dead], "victim should die"); |
| 212 | assert.equal(harness.stateDb.getCharacterBounty(victim.character_id), 0); |
| 213 | assert.equal(harness.stateDb.peekKillCounter(victim.character_id, harness.now()), 250); |
| 214 | assert.equal(harness.simulation.directory.get(victimId).skullValue, 250); |
| 215 | assert.ok(victimSkullEvent, "victim entity-skull must replicate after bounty clear"); |
| 216 | assert.equal(victimSkullEvent.skullValue, 250); |
| 217 | assert.equal(harness.simulation.directory.get(killerId).skullValue, SKULL_KILL_BONUS); |
| 218 | } |
| 219 | finally |
| 220 | { |
| 221 | Math.random = random; |
| 222 | harness.close(); |
| 223 | } |
| 224 | }); |