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