import test from "node:test"; import assert from "node:assert/strict"; import { NeighborBit, computeNeighborConnectMask, neighborConnectStateName, parseNeighborConnectMask, composeNeighborConnectState, neighborConnectBlocksMatch, parseMaterialNeighborConnectState, worldTileToChunkLocal, neighborConnectRefreshTiles, cardinalConnectionSuffix, composeNamedNeighborConnectState, parseNamedNeighborConnectMask } from "../../shared/world/neighborConnect.js"; import { CENTRAL_EXCHANGE_STAFF, pickExchangeClerk } from "../../shared/market/exchange.js"; import { MobId, normalizeSpawnerConfig, isCentralExchangeSpawner } from "../../shared/definitions/mobs.js"; import { BlockType, BLOCK_DEFINITIONS } from "../../shared/definitions/tiles.js"; import { ItemId } from "../../shared/definitions/items.js"; import { emptyEquipment, emptySlots, addCoinsToSlots } from "../../shared/items/inventory.js"; import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; import { CHUNK_SIZE } from "../../shared/protocol/constants.js"; import { Component } from "../src/ecs/components.js"; test("neighbor connect masks cover all NESW combinations", () => { assert.equal(neighborConnectStateName(0), "0"); assert.equal(neighborConnectStateName(NeighborBit.n | NeighborBit.e | NeighborBit.s | NeighborBit.w), "15"); assert.equal(parseNeighborConnectMask("6"), NeighborBit.e | NeighborBit.s); const mask = computeNeighborConnectMask((dx, dy) => dx === 1 || dy === -1); assert.equal(mask, NeighborBit.n | NeighborBit.e); }); test("named wall connection suffixes follow nesw / we / esw / wne", () => { assert.equal(cardinalConnectionSuffix(0), ""); assert.equal(cardinalConnectionSuffix(NeighborBit.e | NeighborBit.w), "we"); assert.equal(cardinalConnectionSuffix(NeighborBit.n | NeighborBit.s), "ns"); assert.equal( cardinalConnectionSuffix(NeighborBit.e | NeighborBit.s | NeighborBit.w), "esw" ); assert.equal( cardinalConnectionSuffix(NeighborBit.w | NeighborBit.n | NeighborBit.e), "wne" ); assert.equal( cardinalConnectionSuffix(NeighborBit.n | NeighborBit.e | NeighborBit.s | NeighborBit.w), "nesw" ); const def = { key: "thick-stone-wall", neighborConnect: true, neighborConnectNamed: true }; assert.equal(composeNamedNeighborConnectState(def, 0), "thick-stone-wall"); assert.equal( composeNamedNeighborConnectState(def, NeighborBit.e | NeighborBit.s | NeighborBit.w), "thick-stone-wall-esw" ); assert.equal(parseNamedNeighborConnectMask("thick-stone-wall-wne", def), NeighborBit.w | NeighborBit.n | NeighborBit.e); assert.equal(parseNamedNeighborConnectMask("14", def), 14); assert.deepEqual(neighborConnectRefreshTiles(15, 8).length, 5); }); test("legacy diagonal wall masks keep cardinals only (19 → ne, not nesw)", () => { const def = { key: "thin-stone-wall", neighborConnect: true, neighborConnectNamed: true }; assert.equal(parseNamedNeighborConnectMask("19", def), NeighborBit.n | NeighborBit.e); assert.equal(composeNamedNeighborConnectState(def, parseNamedNeighborConnectMask("19", def)), "thin-stone-wall-ne"); assert.equal(parseNamedNeighborConnectMask("38", def), NeighborBit.e | NeighborBit.s); assert.equal(parseNamedNeighborConnectMask("255", def), 15); }); test("material neighbor-connect states keep wood id and only match same material", () => { const def = BLOCK_DEFINITIONS[BlockType.table]; assert.equal(composeNeighborConnectState(def, "oak", 3), "oak-3"); assert.deepEqual(parseMaterialNeighborConnectState("birch-10", def), { material: "birch", mask: 10 }); const pine = { typeId: BlockType.table, state: "pine-0" }; const pineNeighbor = { typeId: BlockType.table, state: "pine-2" }; const oakNeighbor = { typeId: BlockType.table, state: "oak-2" }; assert.equal(neighborConnectBlocksMatch(pine, pineNeighbor, def), true); assert.equal(neighborConnectBlocksMatch(pine, oakNeighbor, def), false); }); test("wooden fences use the same material neighbor-connect as tables", () => { const def = BLOCK_DEFINITIONS[BlockType.fence]; assert.equal(def.neighborConnect, true); assert.equal(def.neighborConnectByMaterial, true); assert.equal(def.passable, false); assert.equal(def.blocksLight, false); assert.equal(composeNeighborConnectState(def, "heartwood", 12), "heartwood-12"); const pine = { typeId: BlockType.fence, state: "pine-0" }; const pineNeighbor = { typeId: BlockType.fence, state: "pine-4" }; const oakNeighbor = { typeId: BlockType.fence, state: "oak-4" }; assert.equal(neighborConnectBlocksMatch(pine, pineNeighbor, def), true); assert.equal(neighborConnectBlocksMatch(pine, oakNeighbor, def), false); }); test("world tiles map to chunk locals across boundaries", () => { assert.deepEqual(worldTileToChunkLocal(15, 8, 16), { chunkX: 0, chunkY: 0, localX: 15, localY: 8 }); assert.deepEqual(worldTileToChunkLocal(16, 8, 16), { chunkX: 1, chunkY: 0, localX: 0, localY: 8 }); assert.deepEqual(worldTileToChunkLocal(-1, 0, 16), { chunkX: -1, chunkY: 0, localX: 15, localY: 0 }); const tiles = neighborConnectRefreshTiles(15, 8); assert.deepEqual(tiles[2], [16, 8]); }); test("CE staff entries include sex labels and pickExchangeClerk returns a name", () => { assert.ok(CENTRAL_EXCHANGE_STAFF.length >= 2); for (const staff of CENTRAL_EXCHANGE_STAFF) { assert.ok(staff.name); assert.ok(staff.sex === "male" || staff.sex === "female"); } const name = pickExchangeClerk(() => 0); assert.equal(name, CENTRAL_EXCHANGE_STAFF[0].name); }); test("central exchange spawner config locks staff count and strips shop options", () => { const config = normalizeSpawnerConfig({ mobTypeId: MobId.centralExchange, maxAlive: 2, sells: [{ itemId: ItemId.apple, quantity: 1 }], buysItems: true, displayName: "ignored" }, "ce:0"); assert.equal(config.centralExchange, true); assert.equal(config.maxAlive, CENTRAL_EXCHANGE_STAFF.length); assert.equal(config.sells.length, 0); assert.equal(config.buysItems, false); assert.equal(isCentralExchangeSpawner(config), true); }); test("central exchange spawner places every clerk with matching names", () => { const harness = makeSimulation({ workerCount: 1 }); try { const data = createEmptyChunkData(); data.open = true; data.blocks[8 * CHUNK_SIZE + 9] = { typeId: BlockType.marketplace, state: "0" }; data.blocks[8 * CHUNK_SIZE + 10] = { typeId: BlockType.bank, state: null }; data.spawners = [{ localX: 8, localY: 8, config: normalizeSpawnerConfig({ mobTypeId: MobId.centralExchange, spawnRangeTiles: 6, maxWanderRangeTiles: 6, equipment: { chest: ItemId.clothShirt } }, "ce-test") }]; harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); harness.simulation.reloadWorld(); harness.advance(50); for (let i = 0; i < 30; i++) { harness.simulation.step(); } const record = harness.simulation.entityRecord( harness.simulation.spawnCharacter(1, makeCharacter(harness.stateDb, { tileX: 7, tileY: 7 })) ); const worker = harness.simulation.workers[record.workerId]; for (let i = 0; i < 40; i++) { harness.simulation.step(); } const clerks = []; for (const entity of worker.world.ecs.query(Component.spawnOrigin, Component.appearance)) { if (!entity.spawnOrigin?.centralExchange) { continue; } clerks.push(entity.appearance.firstName); } assert.equal(clerks.length, CENTRAL_EXCHANGE_STAFF.length); for (const staff of CENTRAL_EXCHANGE_STAFF) { assert.ok(clerks.includes(staff.name), staff.name); } } finally { harness.close(); } }); test("opening the market stations a CE clerk on the opposite side of the counter", () => { const harness = makeSimulation({ workerCount: 1 }); try { const data = createEmptyChunkData(); data.open = true; // Counter at (9,8); player will stand at (8,8); staff station = (10,8). data.blocks[8 * CHUNK_SIZE + 9] = { typeId: BlockType.marketplace, state: "0" }; data.blocks[8 * CHUNK_SIZE + 11] = { typeId: BlockType.bank, state: null }; data.spawners = [{ localX: 12, localY: 8, config: normalizeSpawnerConfig({ mobTypeId: MobId.centralExchange, spawnRangeTiles: 8, maxWanderRangeTiles: 8 }, "ce-station") }]; harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); harness.simulation.reloadWorld(); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const funded = addCoinsToSlots(emptySlots(), 10); harness.stateDb.saveBag(character.character_id, { inventory: funded.slots, equipment: emptyEquipment(), vault: [], vaultReserved: [] }); const playerId = harness.simulation.spawnCharacter(1, character); for (let i = 0; i < 40; i++) { harness.simulation.step(); } harness.simulation.enqueue(1, { type: "open-market", characterId: character.character_id }); harness.simulation.step(); const record = harness.simulation.entityRecord(playerId); const worker = harness.simulation.workers[record.workerId]; let stationed = false; for (const entity of worker.world.ecs.query(Component.spawnOrigin)) { if (!entity.spawnOrigin?.centralExchange) { continue; } if (entity.spawnOrigin.stationTileX === 10 && entity.spawnOrigin.stationTileY === 8) { stationed = true; } } assert.equal(stationed, true); } finally { harness.close(); } }); test("closing the market releases the CE clerk so wander can resume", () => { const harness = makeSimulation({ workerCount: 1 }); try { const data = createEmptyChunkData(); data.open = true; data.blocks[8 * CHUNK_SIZE + 9] = { typeId: BlockType.marketplace, state: "0" }; data.blocks[8 * CHUNK_SIZE + 11] = { typeId: BlockType.bank, state: null }; data.spawners = [{ localX: 12, localY: 8, config: normalizeSpawnerConfig({ mobTypeId: MobId.centralExchange, spawnRangeTiles: 8, maxWanderRangeTiles: 8 }, "ce-release-close") }]; harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); harness.simulation.reloadWorld(); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); for (let i = 0; i < 40; i++) { harness.simulation.step(); } harness.simulation.enqueue(1, { type: "open-market", characterId: character.character_id }); harness.simulation.step(); const record = harness.simulation.entityRecord(playerId); const worker = harness.simulation.workers[record.workerId]; assert.equal(stationedClerkCount(worker, 10, 8), 1); harness.simulation.enqueue(1, { type: "close-market", characterId: character.character_id }); harness.simulation.step(); assert.equal(stationedClerkCount(worker, 10, 8), 0); } finally { harness.close(); } }); test("walking away from the counter releases the clerk; a second player keeps them stationed", () => { const harness = makeSimulation({ workerCount: 1 }); try { const data = createEmptyChunkData(); data.open = true; data.blocks[8 * CHUNK_SIZE + 9] = { typeId: BlockType.marketplace, state: "0" }; data.blocks[8 * CHUNK_SIZE + 11] = { typeId: BlockType.bank, state: null }; data.spawners = [{ localX: 12, localY: 8, config: normalizeSpawnerConfig({ mobTypeId: MobId.centralExchange, spawnRangeTiles: 8, maxWanderRangeTiles: 8 }, "ce-release-prune") }]; harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); harness.simulation.reloadWorld(); const first = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const second = makeCharacter(harness.stateDb, { tileX: 7, tileY: 8 }); const firstId = harness.simulation.spawnCharacter(1, first); const secondId = harness.simulation.spawnCharacter(2, second); for (let i = 0; i < 40; i++) { harness.simulation.step(); } harness.simulation.enqueue(1, { type: "open-market", characterId: first.character_id }); harness.simulation.step(); const record = harness.simulation.entityRecord(firstId); const worker = harness.simulation.workers[record.workerId]; assert.equal(stationedClerkCount(worker, 10, 8), 1); // Share the same approach tile / staff station as the first player. const secondHandle = worker.world.ecs.handleOf(secondId); const secondPos = worker.world.ecs.get(secondHandle, Component.position); secondPos.tileX = 8; secondPos.tileY = 8; const secondDir = harness.simulation.directory.get(secondId); secondDir.tileX = 8; secondDir.tileY = 8; harness.simulation.enqueue(2, { type: "open-market", characterId: second.character_id }); harness.simulation.step(); assert.equal(stationedClerkCount(worker, 10, 8), 1); const firstHandle = worker.world.ecs.handleOf(firstId); const firstPos = worker.world.ecs.get(firstHandle, Component.position); firstPos.tileX = 2; firstPos.tileY = 2; const firstDir = harness.simulation.directory.get(firstId); firstDir.tileX = 2; firstDir.tileY = 2; const prune = harness.simulation.market.step(); assert.ok(prune.some((event) => event.type === "market-closed" && event.characterId === first.character_id)); assert.equal(stationedClerkCount(worker, 10, 8), 1, "second player still keeps the clerk"); secondPos.tileX = 2; secondPos.tileY = 3; secondDir.tileX = 2; secondDir.tileY = 3; harness.simulation.market.step(); assert.equal(stationedClerkCount(worker, 10, 8), 0); } finally { harness.close(); } }); function stationedClerkCount (worker, stationTileX, stationTileY) { let count = 0; for (const entity of worker.world.ecs.query(Component.spawnOrigin)) { if (!entity.spawnOrigin?.centralExchange) { continue; } if ( entity.spawnOrigin.stationTileX === stationTileX && entity.spawnOrigin.stationTileY === stationTileY ) { count += 1; } } return count; }