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 { Component } from "../src/ecs/components.js"; import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; function placeBlock (worldDb, tileX, tileY, typeId) { const chunkX = Math.floor(tileX / 16); const chunkY = Math.floor(tileY / 16); const data = createEmptyChunkData(); data.open = true; const localX = tileX - chunkX * 16; const localY = tileY - chunkY * 16; data.blocks[localY * 16 + localX] = { typeId, state: 0 }; worldDb.saveChunkVersion( chunkX, chunkY, worldDb.getActiveRevisionId(), true, encodeChunkBlob(data) ); } 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 ); } function componentsOf (simulation, entityId) { const record = simulation.entityRecord(entityId); const worker = simulation.workers[record.workerId]; const handle = worker.world.ecs.handleOf(entityId); return worker.world.ecs.componentsOf(handle); } function withDeterministicCombat (fn) { const random = Math.random; Math.random = () => 0; try { fn(); } finally { Math.random = random; } } 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), 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), 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", () => { withDeterministicCombat(() => { 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 victimComps = componentsOf(harness.simulation, victimId); victimComps.health.current = 1; const killerComps = componentsOf(harness.simulation, killerId); killerComps.health.current = 1_000_000; killerComps.health.max = 1_000_000; harness.simulation.enqueue(2, { type: "attack", characterId: killer.character_id, targetEntityId: victimId }); let died = null; for (let i = 0; i < 200; i++) { harness.advance(50); const events = harness.simulation.step().events; died = events.find((event) => event.type === "player-died"); if (died) { break; } } assert.ok(died, "victim should die"); assert.equal(harness.stateDb.getCharacterBounty(victim.character_id), 0); assert.equal(countCoins(harness.simulation.bagOf(killerId).vault), 400); } finally { harness.close(); } }); });