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