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