BlueGit

timfa questward

Files

questward / server / tests / skull.test.js

skull.test.js

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