BlueGit

timfa questward

Drop bounty from skull value immediately on PvP payout.

Force-replicate the victim skull after settleKill, and refresh skull on respawn so only the decaying kill counter remains visible.

timfa Tim Falken · pushed by timfa

Authored 2026-08-31 · 2 months ago

Hash
64b245dcae2f7fa3ae7618caf99c4524090d22f3
Parent
7e99a71c
5 files changed +108 −8
client/src/play/main.js +2 −0
@@ -2030,6 +2030,8 @@ async function main ()
2030 2030 // Drop deferred killing-blow health=0 that may still be queued on a projectile.
2031 2031 view?.cancelDeferredCombatHits?.(message.entityId);
2032 2032 view?.updateEntityHealth(message.entityId, message.health, message.maxHealth);
2033 if (message.skullValue != null)
2034 { view?.updateEntitySkull(message.entityId, message.skullValue); }
2033 2035 if (message.entityId === view?.localEntityId)
2034 2036 {
2035 2037 healthUi?.apply({
@@ -2030,6 +2030,8 @@ async function main ()
2030 // Drop deferred killing-blow health=0 that may still be queued on a projectile.
2030 // Drop deferred killing-blow health=0 that may still be queued on a projectile.
2031 view?.cancelDeferredCombatHits?.(message.entityId);
2031 view?.cancelDeferredCombatHits?.(message.entityId);
2032 view?.updateEntityHealth(message.entityId, message.health, message.maxHealth);
2032 view?.updateEntityHealth(message.entityId, message.health, message.maxHealth);
2033 if (message.skullValue != null)
2034 { view?.updateEntitySkull(message.entityId, message.skullValue); }
2033 if (message.entityId === view?.localEntityId)
2035 if (message.entityId === view?.localEntityId)
2034 {
2036 {
2035 healthUi?.apply({
2037 healthUi?.apply({
server/src/bounty/service.js +2 −2
@@ -142,7 +142,7 @@ export class BountyService
142 142 this.#persistBag(placerCharacterId, bag);
143 143
144 144 const nextAmount = this.stateDb.addCharacterBounty(targetId, copper);
145 this.simulation.refreshCharacterSkull?.(targetId);
145 this.simulation.refreshCharacterSkull?.(targetId, { force: true });
146 146 this.simulation.pendingPrivate.push({
147 147 type: "toast",
148 148 characterId: targetId,
@@ -229,7 +229,7 @@ export class BountyService
229 229 message: `Bounty collected: ${formatBountyCopper(paidCopper)} for defeating ${victimName} (sent to vault).`
230 230 });
231 231
232 this.simulation.refreshCharacterSkull?.(victimCharacterId);
232 this.simulation.refreshCharacterSkull?.(victimCharacterId, { force: true });
233 233
234 234 return { paidCopper, victimName };
235 235 }
@@ -142,7 +142,7 @@ export class BountyService
142 this.#persistBag(placerCharacterId, bag);
142 this.#persistBag(placerCharacterId, bag);
143
143
144 const nextAmount = this.stateDb.addCharacterBounty(targetId, copper);
144 const nextAmount = this.stateDb.addCharacterBounty(targetId, copper);
145 this.simulation.refreshCharacterSkull?.(targetId);
145 this.simulation.refreshCharacterSkull?.(targetId, { force: true });
146 this.simulation.pendingPrivate.push({
146 this.simulation.pendingPrivate.push({
147 type: "toast",
147 type: "toast",
148 characterId: targetId,
148 characterId: targetId,
@@ -229,7 +229,7 @@ export class BountyService
229 message: `Bounty collected: ${formatBountyCopper(paidCopper)} for defeating ${victimName} (sent to vault).`
229 message: `Bounty collected: ${formatBountyCopper(paidCopper)} for defeating ${victimName} (sent to vault).`
230 });
230 });
231
231
232 this.simulation.refreshCharacterSkull?.(victimCharacterId);
232 this.simulation.refreshCharacterSkull?.(victimCharacterId, { force: true });
233
233
234 return { paidCopper, victimName };
234 return { paidCopper, victimName };
235 }
235 }
server/src/gateway/websocket.js +2 −1
@@ -1634,7 +1634,8 @@ export function attachGateway ({ httpServer, auth, stateDb, characters, friends,
1634 1634 health: event.health,
1635 1635 maxHealth: event.maxHealth,
1636 1636 stamina: event.stamina,
1637 maxStamina: event.maxStamina
1637 maxStamina: event.maxStamina,
1638 skullValue: event.skullValue | 0
1638 1639 });
1639 1640 }
1640 1641 continue;
@@ -1634,7 +1634,8 @@ export function attachGateway ({ httpServer, auth, stateDb, characters, friends,
1634 health: event.health,
1634 health: event.health,
1635 maxHealth: event.maxHealth,
1635 maxHealth: event.maxHealth,
1636 stamina: event.stamina,
1636 stamina: event.stamina,
1637 maxStamina: event.maxStamina
1637 maxStamina: event.maxStamina,
1638 skullValue: event.skullValue | 0
1638 });
1639 });
1639 }
1640 }
1640 continue;
1641 continue;
server/src/simulation/simulation.js +14 −5
@@ -521,7 +521,7 @@ export class Simulation
521 521 if (!record)
522 522 { return null; }
523 523 if (!force && (record.skullValue | 0) === value)
524 { return value; }
524 { return null; }
525 525 record.skullValue = value;
526 526 const event = {
527 527 type: "entity-skull",
@@ -1739,7 +1739,11 @@ export class Simulation
1739 1739 const killerCharacterId = Number(attackerRecord?.characterId) || 0;
1740 1740 if (killerCharacterId && killerCharacterId !== (event.characterId | 0))
1741 1741 { this.stateDb.addKillCounter(killerCharacterId, SKULL_KILL_BONUS, this.now); }
1742 this.refreshCharacterSkull(event.characterId, { force: true });
1742 const events = [event];
1743 // Bounty is cleared instantly on non-friend PvP death; only kill-counter decays over time.
1744 const victimSkull = this.refreshCharacterSkull(event.characterId, { force: true });
1745 if (victimSkull)
1746 { events.push(victimSkull); }
1743 1747 for (const worker of this.workers)
1744 1748 {
1745 1749 worker.call({
@@ -1780,7 +1784,6 @@ export class Simulation
1780 1784 }
1781 1785 ]
1782 1786 });
1783 const events = [event];
1784 1787 if (killerCharacterId && killerCharacterId !== (event.characterId | 0))
1785 1788 {
1786 1789 const skullEvent = this.refreshCharacterSkull(killerCharacterId, { force: true });
@@ -2014,14 +2017,19 @@ export class Simulation
2014 2017 );
2015 2018 this.#recalculateActivation(this.now);
2016 2019 this.#syncGhosts();
2020 const skullEvent = this.refreshCharacterSkull(characterId, { force: true });
2021 const skullPoints = this.directory.get(entityId)?.skullValue | 0;
2017 2022 const spawnView = this.workers[record.workerId].call({
2018 2023 type: WorkerMessage.inspect,
2019 2024 entityId
2020 2025 });
2021 const spawn = spawnView ? replicationSpawn(spawnView) : null;
2026 const spawn = spawnView
2027 ? replicationSpawn({ ...spawnView, skullValue: skullPoints })
2028 : null;
2022 2029 return [
2023 2030 ...moveEvents,
2024 2031 ...(spawn ? [spawn] : []),
2032 ...(skullEvent ? [skullEvent] : []),
2025 2033 {
2026 2034 type: "player-respawned",
2027 2035 entityId,
@@ -2032,7 +2040,8 @@ export class Simulation
2032 2040 health: playerMaxHealth(skills),
2033 2041 maxHealth: playerMaxHealth(skills),
2034 2042 stamina: playerMaxStamina(skills),
2035 maxStamina: playerMaxStamina(skills)
2043 maxStamina: playerMaxStamina(skills),
2044 skullValue: skullPoints
2036 2045 }
2037 2046 ];
2038 2047 }
@@ -521,7 +521,7 @@ export class Simulation
521 if (!record)
521 if (!record)
522 { return null; }
522 { return null; }
523 if (!force && (record.skullValue | 0) === value)
523 if (!force && (record.skullValue | 0) === value)
524 { return value; }
524 { return null; }
525 record.skullValue = value;
525 record.skullValue = value;
526 const event = {
526 const event = {
527 type: "entity-skull",
527 type: "entity-skull",
@@ -1739,7 +1739,11 @@ export class Simulation
1739 const killerCharacterId = Number(attackerRecord?.characterId) || 0;
1739 const killerCharacterId = Number(attackerRecord?.characterId) || 0;
1740 if (killerCharacterId && killerCharacterId !== (event.characterId | 0))
1740 if (killerCharacterId && killerCharacterId !== (event.characterId | 0))
1741 { this.stateDb.addKillCounter(killerCharacterId, SKULL_KILL_BONUS, this.now); }
1741 { this.stateDb.addKillCounter(killerCharacterId, SKULL_KILL_BONUS, this.now); }
1742 this.refreshCharacterSkull(event.characterId, { force: true });
1742 const events = [event];
1743 // Bounty is cleared instantly on non-friend PvP death; only kill-counter decays over time.
1744 const victimSkull = this.refreshCharacterSkull(event.characterId, { force: true });
1745 if (victimSkull)
1746 { events.push(victimSkull); }
1743 for (const worker of this.workers)
1747 for (const worker of this.workers)
1744 {
1748 {
1745 worker.call({
1749 worker.call({
@@ -1780,7 +1784,6 @@ export class Simulation
1780 }
1784 }
1781 ]
1785 ]
1782 });
1786 });
1783 const events = [event];
1784 if (killerCharacterId && killerCharacterId !== (event.characterId | 0))
1787 if (killerCharacterId && killerCharacterId !== (event.characterId | 0))
1785 {
1788 {
1786 const skullEvent = this.refreshCharacterSkull(killerCharacterId, { force: true });
1789 const skullEvent = this.refreshCharacterSkull(killerCharacterId, { force: true });
@@ -2014,14 +2017,19 @@ export class Simulation
2014 );
2017 );
2015 this.#recalculateActivation(this.now);
2018 this.#recalculateActivation(this.now);
2016 this.#syncGhosts();
2019 this.#syncGhosts();
2020 const skullEvent = this.refreshCharacterSkull(characterId, { force: true });
2021 const skullPoints = this.directory.get(entityId)?.skullValue | 0;
2017 const spawnView = this.workers[record.workerId].call({
2022 const spawnView = this.workers[record.workerId].call({
2018 type: WorkerMessage.inspect,
2023 type: WorkerMessage.inspect,
2019 entityId
2024 entityId
2020 });
2025 });
2021 const spawn = spawnView ? replicationSpawn(spawnView) : null;
2026 const spawn = spawnView
2027 ? replicationSpawn({ ...spawnView, skullValue: skullPoints })
2028 : null;
2022 return [
2029 return [
2023 ...moveEvents,
2030 ...moveEvents,
2024 ...(spawn ? [spawn] : []),
2031 ...(spawn ? [spawn] : []),
2032 ...(skullEvent ? [skullEvent] : []),
2025 {
2033 {
2026 type: "player-respawned",
2034 type: "player-respawned",
2027 entityId,
2035 entityId,
@@ -2032,7 +2040,8 @@ export class Simulation
2032 health: playerMaxHealth(skills),
2040 health: playerMaxHealth(skills),
2033 maxHealth: playerMaxHealth(skills),
2041 maxHealth: playerMaxHealth(skills),
2034 stamina: playerMaxStamina(skills),
2042 stamina: playerMaxStamina(skills),
2035 maxStamina: playerMaxStamina(skills)
2043 maxStamina: playerMaxStamina(skills),
2044 skullValue: skullPoints
2036 }
2045 }
2037 ];
2046 ];
2038 }
2047 }
server/tests/skull.test.js +88 −0
@@ -14,6 +14,26 @@ import { Component } from "../src/ecs/components.js";
14 14 import { totalXpForLevel } from "../../shared/skills/progression.js";
15 15 import { SkillId } from "../../shared/definitions/skills.js";
16 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 }
17 37
18 38 function componentsOf (simulation, entityId)
19 39 {
@@ -134,3 +154,71 @@ test("snapshots include skullValue for other players", () =>
134 154 finally
135 155 { harness.close(); }
136 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 });
@@ -14,6 +14,26 @@ import { Component } from "../src/ecs/components.js";
14import { totalXpForLevel } from "../../shared/skills/progression.js";
14import { totalXpForLevel } from "../../shared/skills/progression.js";
15import { SkillId } from "../../shared/definitions/skills.js";
15import { SkillId } from "../../shared/definitions/skills.js";
16import { WorkerMessage } from "../src/simulation/protocol.js";
16import { WorkerMessage } from "../src/simulation/protocol.js";
17import { BlockType } from "../../shared/definitions/tiles.js";
18import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";
19
20function 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}
17
37
18function componentsOf (simulation, entityId)
38function componentsOf (simulation, entityId)
19{
39{
@@ -134,3 +154,71 @@ test("snapshots include skullValue for other players", () =>
134 finally
154 finally
135 { harness.close(); }
155 { harness.close(); }
136});
156});
157
158test("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});