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