import test from "node:test"; import assert from "node:assert/strict"; import { BlockType } from "../../shared/definitions/tiles.js"; import { ItemId } from "../../shared/definitions/items.js"; import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js"; import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; import { OVERWORLD_ID } from "../../shared/world/dimensions.js"; function placeBlock (worldDb, tileX, tileY, typeId, dimensionId = OVERWORLD_ID) { const chunkX = Math.floor(tileX / 16); const chunkY = Math.floor(tileY / 16); const rev = worldDb.getActiveRevisionId(); const existing = worldDb.getChunkVersion(chunkX, chunkY, rev, dimensionId); const data = createEmptyChunkData(); data.open = true; if (existing?.data_blob) { const parsed = typeof existing.data_blob === "string" ? JSON.parse(existing.data_blob) : existing.data_blob; data.grounds = parsed.grounds ?? data.grounds; data.blocks = parsed.blocks ?? data.blocks; data.open = parsed.open ?? true; } const localX = tileX - chunkX * 16; const localY = tileY - chunkY * 16; data.blocks[localY * 16 + localX] = { typeId, state: 0 }; worldDb.saveChunkVersion( chunkX, chunkY, rev, true, encodeChunkBlob(data), dimensionId ); } function countCoins (slots) { return (slots ?? []) .filter((slot) => slot?.itemId === ItemId.coins) .reduce((sum, slot) => sum + (slot.quantity | 0), 0); } function giveCoins (harness, entityId, amount) { const bag = harness.simulation.bagOf(entityId); bag.inventory = emptySlots(); bag.inventory[0] = { itemId: ItemId.coins, quantity: amount }; bag.equipment = emptyEquipment(); harness.simulation.applyBag(entityId, bag); harness.stateDb.saveBag( harness.simulation.directory.get(entityId).characterId, bag ); } test("raise bounty spends inventory coins and tracks ledger", () => { const harness = makeSimulation({ workerCount: 1 }); try { placeBlock(harness.worldDb, 8, 8, BlockType.bountyOffice); harness.simulation.reloadWorld(); const placer = makeCharacter(harness.stateDb, { firstName: "Place", lastName: "R", tileX: 8, tileY: 9 }); const target = makeCharacter(harness.stateDb, { firstName: "Mark", lastName: "Ed", tileX: 10, tileY: 10 }); const placerId = harness.simulation.spawnCharacter(1, placer); giveCoins(harness, placerId, 500); harness.simulation.enqueue(1, { type: "raise-bounty", characterId: placer.character_id, firstName: "Mark", lastName: "Ed", amountCopper: 250 }); const result = harness.simulation.step(); const snap = result.events.find((event) => event.type === "bounty-snapshot"); assert.ok(snap?.raised); assert.equal(snap.raised.amountCopper, 250); assert.equal(harness.stateDb.getCharacterBounty(target.character_id, harness.now()), 250); assert.equal(countCoins(harness.simulation.bagOf(placerId).inventory), 250); harness.simulation.enqueue(1, { type: "raise-bounty", characterId: placer.character_id, firstName: "Mark", lastName: "Ed", amountCopper: 100 }); harness.simulation.step(); assert.equal(harness.stateDb.getCharacterBounty(target.character_id, harness.now()), 350); assert.equal(countCoins(harness.simulation.bagOf(placerId).inventory), 150); } finally { harness.close(); } }); test("open bounty requires standing next to a bounty office", () => { const harness = makeSimulation({ workerCount: 1 }); try { placeBlock(harness.worldDb, 8, 8, BlockType.bountyOffice); harness.simulation.reloadWorld(); const character = makeCharacter(harness.stateDb, { tileX: 12, tileY: 12 }); harness.simulation.spawnCharacter(1, character); harness.simulation.enqueue(1, { type: "open-bounty", characterId: character.character_id }); const result = harness.simulation.step(); const error = result.events.find((event) => event.type === "command-error"); assert.ok(error); assert.match(error.message, /Bounty Office/i); } finally { harness.close(); } }); test("PvP kill pays bounty into killer vault and clears ledger", () => { const harness = makeSimulation({ workerCount: 1 }); try { placeBlock(harness.worldDb, 8, 8, BlockType.bountyOffice); placeBlock(harness.worldDb, 7, 8, BlockType.bank); harness.simulation.reloadWorld(); const victim = makeCharacter(harness.stateDb, { firstName: "Vic", lastName: "Tim", tileX: 9, tileY: 9 }); const killer = makeCharacter(harness.stateDb, { firstName: "Kill", lastName: "Er", tileX: 10, tileY: 9 }); harness.stateDb.addCharacterBounty(victim.character_id, 400); const victimId = harness.simulation.spawnCharacter(1, victim); const killerId = harness.simulation.spawnCharacter(2, killer); const settled = harness.simulation.bounty.settleKill( victim.character_id, killerId, 9, 9 ); assert.equal(settled?.paidCopper, 400); assert.equal(harness.stateDb.getCharacterBounty(victim.character_id, harness.now()), 0); assert.equal(countCoins(harness.simulation.bagOf(killerId).vault), 400); harness.stateDb.addKillCounter(killer.character_id, 500, harness.now()); harness.simulation.refreshCharacterSkull(killer.character_id); assert.equal(harness.stateDb.peekKillCounter(killer.character_id, harness.now()), 500); assert.equal(harness.simulation.directory.get(killerId).skullValue, 500); assert.equal(harness.simulation.directory.get(victimId).skullValue, 0); } finally { harness.close(); } }); test("friend PvP kill does not pay or clear bounty", () => { const harness = makeSimulation({ workerCount: 1 }); try { placeBlock(harness.worldDb, 8, 8, BlockType.bountyOffice); placeBlock(harness.worldDb, 7, 8, BlockType.bank); harness.simulation.reloadWorld(); const victim = makeCharacter(harness.stateDb, { firstName: "Vic", lastName: "Friend", tileX: 9, tileY: 9 }); const killer = makeCharacter(harness.stateDb, { firstName: "Kill", lastName: "Buddy", tileX: 10, tileY: 9 }); harness.stateDb.addCharacterBounty(victim.character_id, 400); harness.stateDb.insertAccountFriendship(victim.account_id, killer.account_id); const victimId = harness.simulation.spawnCharacter(1, victim); const killerId = harness.simulation.spawnCharacter(2, killer); const settled = harness.simulation.bounty.settleKill( victim.character_id, killerId, 9, 9 ); assert.equal(settled, null); assert.equal(harness.stateDb.getCharacterBounty(victim.character_id, harness.now()), 400); assert.equal(countCoins(harness.simulation.bagOf(killerId).vault), 0); assert.equal(harness.simulation.directory.get(victimId).skullValue, 400); } finally { harness.close(); } }); test("pay off own bounty spends 10 coins per 1 bounty cleared", () => { const harness = makeSimulation({ workerCount: 1 }); try { placeBlock(harness.worldDb, 8, 8, BlockType.bountyOffice); harness.simulation.reloadWorld(); const character = makeCharacter(harness.stateDb, { firstName: "Self", lastName: "Pay", tileX: 8, tileY: 9 }); harness.stateDb.addCharacterBounty(character.character_id, 25); const entityId = harness.simulation.spawnCharacter(1, character); giveCoins(harness, entityId, 100); harness.simulation.enqueue(1, { type: "pay-bounty", characterId: character.character_id, amountCopper: 100 }); const result = harness.simulation.step(); const snap = result.events.find((event) => event.type === "bounty-snapshot"); assert.ok(snap?.paidOff); assert.equal(snap.paidOff.clearedCopper, 10); assert.equal(snap.paidOff.spentCopper, 100); assert.equal(snap.myAmountCopper, 15); assert.equal(harness.stateDb.getCharacterBounty(character.character_id, harness.now()), 15); assert.equal(countCoins(harness.simulation.bagOf(entityId).inventory), 0); assert.equal(harness.simulation.directory.get(entityId).skullValue, 15); } finally { harness.close(); } }); test("pay off caps at remaining bounty and spends only matching tens", () => { const harness = makeSimulation({ workerCount: 1 }); try { placeBlock(harness.worldDb, 8, 8, BlockType.bountyOffice); harness.simulation.reloadWorld(); const character = makeCharacter(harness.stateDb, { firstName: "Cap", lastName: "Pay", tileX: 8, tileY: 9 }); harness.stateDb.addCharacterBounty(character.character_id, 3); const entityId = harness.simulation.spawnCharacter(1, character); giveCoins(harness, entityId, 100); harness.simulation.enqueue(1, { type: "pay-bounty", characterId: character.character_id, amountCopper: 100 }); harness.simulation.step(); assert.equal(harness.stateDb.getCharacterBounty(character.character_id, harness.now()), 0); assert.equal(countCoins(harness.simulation.bagOf(entityId).inventory), 70); } finally { harness.close(); } }); test("bounty decays one coin per calendar day", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { firstName: "Decay", lastName: "Ing", tileX: 5, tileY: 5 }); const dayA = Date.parse("2026-03-01T12:00:00"); const dayC = Date.parse("2026-03-03T12:00:00"); harness.stateDb.addCharacterBounty(character.character_id, 10, dayA); assert.equal(harness.stateDb.getCharacterBounty(character.character_id, dayA), 10); assert.equal(harness.stateDb.getCharacterBounty(character.character_id, dayC), 8); } finally { harness.close(); } }); test("bounty payout letter prefers office in death dimension", () => { const harness = makeSimulation({ workerCount: 1 }); const CAVE_ID = "bounty-cave"; try { const rev = harness.worldDb.getActiveRevisionId(); harness.worldDb.saveDimension(rev, { dimensionId: CAVE_ID, displayName: "Bounty Cave", lightingMode: "alwaysLit", seasonMode: "summer", defaultGroundTypeId: 1 }); const cave = createEmptyChunkData(); cave.open = true; harness.worldDb.saveChunkVersion(0, 0, rev, true, encodeChunkBlob(cave), CAVE_ID); // Overworld office farther from death tile than cave office placeBlock(harness.worldDb, 2, 2, BlockType.bountyOffice, OVERWORLD_ID); placeBlock(harness.worldDb, 1, 2, BlockType.bank, OVERWORLD_ID); placeBlock(harness.worldDb, 9, 9, BlockType.bountyOffice, CAVE_ID); placeBlock(harness.worldDb, 8, 9, BlockType.bank, CAVE_ID); harness.simulation.reloadWorld(); const victim = makeCharacter(harness.stateDb, { firstName: "Vic", lastName: "DimVictim", tileX: 10, tileY: 9 }); const killer = makeCharacter(harness.stateDb, { firstName: "Kill", lastName: "DimKiller", tileX: 11, tileY: 9 }); harness.stateDb.addCharacterBounty(victim.character_id, 400); const killerId = harness.simulation.spawnCharacter(2, killer); const settled = harness.simulation.bounty.settleKill( victim.character_id, killerId, 10, 9, CAVE_ID ); assert.equal(settled?.paidCopper, 400); const office = harness.simulation.courier.nearestBountyOffice(10, 9, CAVE_ID); assert.equal(office.dimensionId, CAVE_ID); assert.equal(office.tileX, 9); assert.equal(office.tileY, 9); const open = harness.stateDb.listActiveCourierTransits(); assert.ok(open.length >= 1); const row = open[open.length - 1]; assert.equal(row.originDimensionId, CAVE_ID); assert.equal(Math.floor(row.originX), 9); assert.equal(Math.floor(row.originY), 9); } finally { harness.close(); } });