| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { |
| 4 | NeighborBit, |
| 5 | computeNeighborConnectMask, |
| 6 | neighborConnectStateName, |
| 7 | parseNeighborConnectMask, |
| 8 | composeNeighborConnectState, |
| 9 | neighborConnectBlocksMatch, |
| 10 | parseMaterialNeighborConnectState |
| 11 | } from "../../shared/world/neighborConnect.js"; |
| 12 | import { CENTRAL_EXCHANGE_STAFF, pickExchangeClerk } from "../../shared/market/exchange.js"; |
| 13 | import { MobId, normalizeSpawnerConfig, isCentralExchangeSpawner } from "../../shared/definitions/mobs.js"; |
| 14 | import { BlockType, BLOCK_DEFINITIONS } from "../../shared/definitions/tiles.js"; |
| 15 | import { ItemId } from "../../shared/definitions/items.js"; |
| 16 | import { emptyEquipment, emptySlots, addCoinsToSlots } from "../../shared/items/inventory.js"; |
| 17 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 18 | import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; |
| 19 | import { CHUNK_SIZE } from "../../shared/protocol/constants.js"; |
| 20 | import { Component } from "../src/ecs/components.js"; |
| 21 | |
| 22 | test("neighbor connect masks cover all NESW combinations", () => |
| 23 | { |
| 24 | assert.equal(neighborConnectStateName(0), "0"); |
| 25 | assert.equal(neighborConnectStateName(NeighborBit.n | NeighborBit.e | NeighborBit.s | NeighborBit.w), "15"); |
| 26 | assert.equal(parseNeighborConnectMask("6"), NeighborBit.e | NeighborBit.s); |
| 27 | const mask = computeNeighborConnectMask((dx, dy) => dx === 1 || dy === -1); |
| 28 | assert.equal(mask, NeighborBit.n | NeighborBit.e); |
| 29 | }); |
| 30 | |
| 31 | test("material neighbor-connect states keep wood id and only match same material", () => |
| 32 | { |
| 33 | const def = BLOCK_DEFINITIONS[BlockType.table]; |
| 34 | assert.equal(composeNeighborConnectState(def, "oak", 3), "oak-3"); |
| 35 | assert.deepEqual(parseMaterialNeighborConnectState("birch-10", def), { material: "birch", mask: 10 }); |
| 36 | const pine = { typeId: BlockType.table, state: "pine-0" }; |
| 37 | const pineNeighbor = { typeId: BlockType.table, state: "pine-2" }; |
| 38 | const oakNeighbor = { typeId: BlockType.table, state: "oak-2" }; |
| 39 | assert.equal(neighborConnectBlocksMatch(pine, pineNeighbor, def), true); |
| 40 | assert.equal(neighborConnectBlocksMatch(pine, oakNeighbor, def), false); |
| 41 | }); |
| 42 | |
| 43 | test("wooden fences use the same material neighbor-connect as tables", () => |
| 44 | { |
| 45 | const def = BLOCK_DEFINITIONS[BlockType.fence]; |
| 46 | assert.equal(def.neighborConnect, true); |
| 47 | assert.equal(def.neighborConnectByMaterial, true); |
| 48 | assert.equal(def.passable, false); |
| 49 | assert.equal(def.blocksLight, false); |
| 50 | assert.equal(composeNeighborConnectState(def, "heartwood", 12), "heartwood-12"); |
| 51 | const pine = { typeId: BlockType.fence, state: "pine-0" }; |
| 52 | const pineNeighbor = { typeId: BlockType.fence, state: "pine-4" }; |
| 53 | const oakNeighbor = { typeId: BlockType.fence, state: "oak-4" }; |
| 54 | assert.equal(neighborConnectBlocksMatch(pine, pineNeighbor, def), true); |
| 55 | assert.equal(neighborConnectBlocksMatch(pine, oakNeighbor, def), false); |
| 56 | }); |
| 57 | |
| 58 | test("CE staff entries include sex labels and pickExchangeClerk returns a name", () => |
| 59 | { |
| 60 | assert.ok(CENTRAL_EXCHANGE_STAFF.length >= 2); |
| 61 | for (const staff of CENTRAL_EXCHANGE_STAFF) |
| 62 | { |
| 63 | assert.ok(staff.name); |
| 64 | assert.ok(staff.sex === "male" || staff.sex === "female"); |
| 65 | } |
| 66 | const name = pickExchangeClerk(() => 0); |
| 67 | assert.equal(name, CENTRAL_EXCHANGE_STAFF[0].name); |
| 68 | }); |
| 69 | |
| 70 | test("central exchange spawner config locks staff count and strips shop options", () => |
| 71 | { |
| 72 | const config = normalizeSpawnerConfig({ |
| 73 | mobTypeId: MobId.centralExchange, |
| 74 | maxAlive: 2, |
| 75 | sells: [{ itemId: ItemId.apple, quantity: 1 }], |
| 76 | buysItems: true, |
| 77 | displayName: "ignored" |
| 78 | }, "ce:0"); |
| 79 | assert.equal(config.centralExchange, true); |
| 80 | assert.equal(config.maxAlive, CENTRAL_EXCHANGE_STAFF.length); |
| 81 | assert.equal(config.sells.length, 0); |
| 82 | assert.equal(config.buysItems, false); |
| 83 | assert.equal(isCentralExchangeSpawner(config), true); |
| 84 | }); |
| 85 | |
| 86 | test("central exchange spawner places every clerk with matching names", () => |
| 87 | { |
| 88 | const harness = makeSimulation({ workerCount: 1 }); |
| 89 | try |
| 90 | { |
| 91 | const data = createEmptyChunkData(); |
| 92 | data.open = true; |
| 93 | data.blocks[8 * CHUNK_SIZE + 9] = { typeId: BlockType.marketplace, state: "0" }; |
| 94 | data.blocks[8 * CHUNK_SIZE + 10] = { typeId: BlockType.bank, state: null }; |
| 95 | data.spawners = [{ |
| 96 | localX: 8, |
| 97 | localY: 8, |
| 98 | config: normalizeSpawnerConfig({ |
| 99 | mobTypeId: MobId.centralExchange, |
| 100 | spawnRangeTiles: 6, |
| 101 | maxWanderRangeTiles: 6, |
| 102 | equipment: { chest: ItemId.clothShirt } |
| 103 | }, "ce-test") |
| 104 | }]; |
| 105 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 106 | harness.simulation.reloadWorld(); |
| 107 | harness.advance(50); |
| 108 | for (let i = 0; i < 30; i++) |
| 109 | { harness.simulation.step(); } |
| 110 | |
| 111 | const record = harness.simulation.entityRecord( |
| 112 | harness.simulation.spawnCharacter(1, makeCharacter(harness.stateDb, { tileX: 7, tileY: 7 })) |
| 113 | ); |
| 114 | const worker = harness.simulation.workers[record.workerId]; |
| 115 | for (let i = 0; i < 40; i++) |
| 116 | { harness.simulation.step(); } |
| 117 | |
| 118 | const clerks = []; |
| 119 | for (const entity of worker.world.ecs.query(Component.spawnOrigin, Component.appearance)) |
| 120 | { |
| 121 | if (!entity.spawnOrigin?.centralExchange) |
| 122 | { continue; } |
| 123 | clerks.push(entity.appearance.firstName); |
| 124 | } |
| 125 | assert.equal(clerks.length, CENTRAL_EXCHANGE_STAFF.length); |
| 126 | for (const staff of CENTRAL_EXCHANGE_STAFF) |
| 127 | { assert.ok(clerks.includes(staff.name), staff.name); } |
| 128 | } |
| 129 | finally |
| 130 | { harness.close(); } |
| 131 | }); |
| 132 | |
| 133 | test("opening the market stations a CE clerk on the opposite side of the counter", () => |
| 134 | { |
| 135 | const harness = makeSimulation({ workerCount: 1 }); |
| 136 | try |
| 137 | { |
| 138 | const data = createEmptyChunkData(); |
| 139 | data.open = true; |
| 140 | // Counter at (9,8); player will stand at (8,8); staff station = (10,8). |
| 141 | data.blocks[8 * CHUNK_SIZE + 9] = { typeId: BlockType.marketplace, state: "0" }; |
| 142 | data.blocks[8 * CHUNK_SIZE + 11] = { typeId: BlockType.bank, state: null }; |
| 143 | data.spawners = [{ |
| 144 | localX: 12, |
| 145 | localY: 8, |
| 146 | config: normalizeSpawnerConfig({ |
| 147 | mobTypeId: MobId.centralExchange, |
| 148 | spawnRangeTiles: 8, |
| 149 | maxWanderRangeTiles: 8 |
| 150 | }, "ce-station") |
| 151 | }]; |
| 152 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 153 | harness.simulation.reloadWorld(); |
| 154 | |
| 155 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 156 | const funded = addCoinsToSlots(emptySlots(), 10); |
| 157 | harness.stateDb.saveBag(character.character_id, { |
| 158 | inventory: funded.slots, |
| 159 | equipment: emptyEquipment(), |
| 160 | vault: [], |
| 161 | vaultReserved: [] |
| 162 | }); |
| 163 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 164 | for (let i = 0; i < 40; i++) |
| 165 | { harness.simulation.step(); } |
| 166 | |
| 167 | harness.simulation.enqueue(1, { |
| 168 | type: "open-market", |
| 169 | characterId: character.character_id |
| 170 | }); |
| 171 | harness.simulation.step(); |
| 172 | |
| 173 | const record = harness.simulation.entityRecord(playerId); |
| 174 | const worker = harness.simulation.workers[record.workerId]; |
| 175 | let stationed = false; |
| 176 | for (const entity of worker.world.ecs.query(Component.spawnOrigin)) |
| 177 | { |
| 178 | if (!entity.spawnOrigin?.centralExchange) |
| 179 | { continue; } |
| 180 | if (entity.spawnOrigin.stationTileX === 10 && entity.spawnOrigin.stationTileY === 8) |
| 181 | { stationed = true; } |
| 182 | } |
| 183 | assert.equal(stationed, true); |
| 184 | } |
| 185 | finally |
| 186 | { harness.close(); } |
| 187 | }); |
| 188 | |
| 189 | test("closing the market releases the CE clerk so wander can resume", () => |
| 190 | { |
| 191 | const harness = makeSimulation({ workerCount: 1 }); |
| 192 | try |
| 193 | { |
| 194 | const data = createEmptyChunkData(); |
| 195 | data.open = true; |
| 196 | data.blocks[8 * CHUNK_SIZE + 9] = { typeId: BlockType.marketplace, state: "0" }; |
| 197 | data.blocks[8 * CHUNK_SIZE + 11] = { typeId: BlockType.bank, state: null }; |
| 198 | data.spawners = [{ |
| 199 | localX: 12, |
| 200 | localY: 8, |
| 201 | config: normalizeSpawnerConfig({ |
| 202 | mobTypeId: MobId.centralExchange, |
| 203 | spawnRangeTiles: 8, |
| 204 | maxWanderRangeTiles: 8 |
| 205 | }, "ce-release-close") |
| 206 | }]; |
| 207 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 208 | harness.simulation.reloadWorld(); |
| 209 | |
| 210 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 211 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 212 | for (let i = 0; i < 40; i++) |
| 213 | { harness.simulation.step(); } |
| 214 | |
| 215 | harness.simulation.enqueue(1, { |
| 216 | type: "open-market", |
| 217 | characterId: character.character_id |
| 218 | }); |
| 219 | harness.simulation.step(); |
| 220 | |
| 221 | const record = harness.simulation.entityRecord(playerId); |
| 222 | const worker = harness.simulation.workers[record.workerId]; |
| 223 | assert.equal(stationedClerkCount(worker, 10, 8), 1); |
| 224 | |
| 225 | harness.simulation.enqueue(1, { |
| 226 | type: "close-market", |
| 227 | characterId: character.character_id |
| 228 | }); |
| 229 | harness.simulation.step(); |
| 230 | assert.equal(stationedClerkCount(worker, 10, 8), 0); |
| 231 | } |
| 232 | finally |
| 233 | { harness.close(); } |
| 234 | }); |
| 235 | |
| 236 | test("walking away from the counter releases the clerk; a second player keeps them stationed", () => |
| 237 | { |
| 238 | const harness = makeSimulation({ workerCount: 1 }); |
| 239 | try |
| 240 | { |
| 241 | const data = createEmptyChunkData(); |
| 242 | data.open = true; |
| 243 | data.blocks[8 * CHUNK_SIZE + 9] = { typeId: BlockType.marketplace, state: "0" }; |
| 244 | data.blocks[8 * CHUNK_SIZE + 11] = { typeId: BlockType.bank, state: null }; |
| 245 | data.spawners = [{ |
| 246 | localX: 12, |
| 247 | localY: 8, |
| 248 | config: normalizeSpawnerConfig({ |
| 249 | mobTypeId: MobId.centralExchange, |
| 250 | spawnRangeTiles: 8, |
| 251 | maxWanderRangeTiles: 8 |
| 252 | }, "ce-release-prune") |
| 253 | }]; |
| 254 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 255 | harness.simulation.reloadWorld(); |
| 256 | |
| 257 | const first = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 258 | const second = makeCharacter(harness.stateDb, { tileX: 7, tileY: 8 }); |
| 259 | const firstId = harness.simulation.spawnCharacter(1, first); |
| 260 | const secondId = harness.simulation.spawnCharacter(2, second); |
| 261 | for (let i = 0; i < 40; i++) |
| 262 | { harness.simulation.step(); } |
| 263 | |
| 264 | harness.simulation.enqueue(1, { type: "open-market", characterId: first.character_id }); |
| 265 | harness.simulation.step(); |
| 266 | |
| 267 | const record = harness.simulation.entityRecord(firstId); |
| 268 | const worker = harness.simulation.workers[record.workerId]; |
| 269 | assert.equal(stationedClerkCount(worker, 10, 8), 1); |
| 270 | |
| 271 | // Share the same approach tile / staff station as the first player. |
| 272 | const secondHandle = worker.world.ecs.handleOf(secondId); |
| 273 | const secondPos = worker.world.ecs.get(secondHandle, Component.position); |
| 274 | secondPos.tileX = 8; |
| 275 | secondPos.tileY = 8; |
| 276 | const secondDir = harness.simulation.directory.get(secondId); |
| 277 | secondDir.tileX = 8; |
| 278 | secondDir.tileY = 8; |
| 279 | harness.simulation.enqueue(2, { type: "open-market", characterId: second.character_id }); |
| 280 | harness.simulation.step(); |
| 281 | assert.equal(stationedClerkCount(worker, 10, 8), 1); |
| 282 | |
| 283 | const firstHandle = worker.world.ecs.handleOf(firstId); |
| 284 | const firstPos = worker.world.ecs.get(firstHandle, Component.position); |
| 285 | firstPos.tileX = 2; |
| 286 | firstPos.tileY = 2; |
| 287 | const firstDir = harness.simulation.directory.get(firstId); |
| 288 | firstDir.tileX = 2; |
| 289 | firstDir.tileY = 2; |
| 290 | const prune = harness.simulation.market.step(); |
| 291 | assert.ok(prune.some((event) => event.type === "market-closed" && event.characterId === first.character_id)); |
| 292 | assert.equal(stationedClerkCount(worker, 10, 8), 1, "second player still keeps the clerk"); |
| 293 | |
| 294 | secondPos.tileX = 2; |
| 295 | secondPos.tileY = 3; |
| 296 | secondDir.tileX = 2; |
| 297 | secondDir.tileY = 3; |
| 298 | harness.simulation.market.step(); |
| 299 | assert.equal(stationedClerkCount(worker, 10, 8), 0); |
| 300 | } |
| 301 | finally |
| 302 | { harness.close(); } |
| 303 | }); |
| 304 | |
| 305 | function stationedClerkCount (worker, stationTileX, stationTileY) |
| 306 | { |
| 307 | let count = 0; |
| 308 | for (const entity of worker.world.ecs.query(Component.spawnOrigin)) |
| 309 | { |
| 310 | if (!entity.spawnOrigin?.centralExchange) |
| 311 | { continue; } |
| 312 | if ( |
| 313 | entity.spawnOrigin.stationTileX === stationTileX |
| 314 | && entity.spawnOrigin.stationTileY === stationTileY |
| 315 | ) |
| 316 | { count += 1; } |
| 317 | } |
| 318 | return count; |
| 319 | } |