| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { fileURLToPath } from "node:url"; |
| 4 | import { join } from "node:path"; |
| 5 | import { BlockType, isBlockPassable, isChunkAtlasBakedBlock, blockLightOcclusionForBlock, blockBlocksRangedAttacks, LightOcclusion } from "../../shared/definitions/tiles.js"; |
| 6 | import { WOOD_TIER_ORDER } from "../../shared/definitions/materialTiers.js"; |
| 7 | import { MobId } from "../../shared/definitions/mobs.js"; |
| 8 | import { ItemId } from "../../shared/definitions/items.js"; |
| 9 | import { ConditionKind } from "../../shared/quests/conditions.js"; |
| 10 | import { |
| 11 | composeDoorState, |
| 12 | doorSortSubOrder, |
| 13 | flipDoorState, |
| 14 | isDoorTraversable, |
| 15 | listAllDoorStates, |
| 16 | parseDoorState |
| 17 | } from "../../shared/world/doors.js"; |
| 18 | import { |
| 19 | doorHasRequirements, |
| 20 | doorRequirementsMet, |
| 21 | normalizeDoorConfig |
| 22 | } from "../../shared/world/doorRequirements.js"; |
| 23 | import { buildSpriteCatalog } from "../src/assets/spriteCatalog.js"; |
| 24 | import { Component } from "../src/ecs/components.js"; |
| 25 | import { mobComponents } from "../src/simulation/spawners.js"; |
| 26 | import { |
| 27 | entityCanOpenDoors, |
| 28 | maybeOpenDoorOnApproach, |
| 29 | stepDoors |
| 30 | } from "../src/doors/service.js"; |
| 31 | import { makeCharacter, makeSimulation, walkTicks } from "./helpers/simulationHarness.js"; |
| 32 | |
| 33 | const ASSET_ROOT = join(fileURLToPath(new URL("../../client/assets", import.meta.url))); |
| 34 | |
| 35 | function workerOf (simulation, entityId) |
| 36 | { |
| 37 | const record = simulation.entityRecord(entityId); |
| 38 | assert.ok(record); |
| 39 | return simulation.workers[record.workerId]; |
| 40 | } |
| 41 | |
| 42 | function placeDoor (worker, tileX, tileY, state, config = null) |
| 43 | { |
| 44 | const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16)); |
| 45 | const localX = ((tileX % 16) + 16) % 16; |
| 46 | const localY = ((tileY % 16) + 16) % 16; |
| 47 | data.blocks[localY * 16 + localX] = { |
| 48 | typeId: BlockType.door, |
| 49 | state, |
| 50 | config |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | function placeWall (worker, tileX, tileY) |
| 55 | { |
| 56 | const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16)); |
| 57 | const localX = ((tileX % 16) + 16) % 16; |
| 58 | const localY = ((tileY % 16) + 16) % 16; |
| 59 | data.blocks[localY * 16 + localX] = { |
| 60 | typeId: BlockType.stoneWall, |
| 61 | state: null, |
| 62 | config: null |
| 63 | }; |
| 64 | } |
| 65 | |
| 66 | /** One-tile corridor through (8,8)-(9,8)-(10,8) so pathing cannot detour. */ |
| 67 | function placeGatedCorridor (worker) |
| 68 | { |
| 69 | for (let x = 7; x <= 11; x++) |
| 70 | { |
| 71 | placeWall(worker, x, 7); |
| 72 | placeWall(worker, x, 9); |
| 73 | } |
| 74 | placeWall(worker, 7, 8); |
| 75 | placeWall(worker, 11, 8); |
| 76 | } |
| 77 | |
| 78 | function doorAt (worker, tileX, tileY) |
| 79 | { |
| 80 | const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16)); |
| 81 | const localX = ((tileX % 16) + 16) % 16; |
| 82 | const localY = ((tileY % 16) + 16) % 16; |
| 83 | return data.blocks[localY * 16 + localX]; |
| 84 | } |
| 85 | |
| 86 | test("door states cover every wood facing and openness", () => |
| 87 | { |
| 88 | const states = listAllDoorStates(); |
| 89 | assert.equal(states.length, WOOD_TIER_ORDER.length * 4); |
| 90 | assert.deepEqual(parseDoorState("oak-n-open"), { wood: "oak", facing: "n", open: true }); |
| 91 | assert.equal(flipDoorState("pine-s-closed"), "pine-s-open"); |
| 92 | assert.equal(flipDoorState("birch-n-open"), "birch-n-closed"); |
| 93 | assert.equal(composeDoorState("elder", "n", false), "elder-n-closed"); |
| 94 | }); |
| 95 | |
| 96 | test("doors at resting block unless opener; passage means flipped from resting", () => |
| 97 | { |
| 98 | assert.equal(isBlockPassable(BlockType.door, "pine-s-closed"), false); |
| 99 | assert.equal(isBlockPassable(BlockType.door, "pine-s-open"), false); |
| 100 | assert.equal(isDoorTraversable({ state: "pine-s-closed" }, { canOpenDoors: false }), false); |
| 101 | assert.equal(isDoorTraversable({ state: "pine-s-closed" }, { canOpenDoors: true }), true); |
| 102 | assert.equal(isDoorTraversable({ state: "pine-s-open" }, { canOpenDoors: false }), false); |
| 103 | assert.equal(isDoorTraversable({ |
| 104 | state: "pine-s-closed", |
| 105 | config: { restingState: "pine-s-open" } |
| 106 | }, { canOpenDoors: false }), true); |
| 107 | }); |
| 108 | |
| 109 | test("light and ranged LoS follow passage-open (flipped from resting), not the open suffix", () => |
| 110 | { |
| 111 | assert.equal(blockLightOcclusionForBlock({ typeId: BlockType.door, state: "pine-s-closed" }), LightOcclusion.solid); |
| 112 | assert.equal(blockLightOcclusionForBlock({ typeId: BlockType.door, state: "pine-s-open" }), LightOcclusion.solid); |
| 113 | assert.equal(blockLightOcclusionForBlock({ |
| 114 | typeId: BlockType.door, |
| 115 | state: "pine-s-closed", |
| 116 | config: { restingState: "pine-s-open" } |
| 117 | }), LightOcclusion.clear); |
| 118 | assert.equal(blockBlocksRangedAttacks(BlockType.door, "pine-s-closed"), true); |
| 119 | assert.equal(blockBlocksRangedAttacks(BlockType.door, "pine-s-open", { |
| 120 | typeId: BlockType.door, |
| 121 | state: "pine-s-closed", |
| 122 | config: { restingState: "pine-s-open" } |
| 123 | }), false); |
| 124 | }); |
| 125 | |
| 126 | test("door sort sub-order: only S-closed is in front", () => |
| 127 | { |
| 128 | assert.equal(doorSortSubOrder("pine-s-closed", 3), 12); |
| 129 | assert.equal(doorSortSubOrder("pine-s-open", 3), 1); |
| 130 | assert.equal(doorSortSubOrder("pine-n-closed", 3), 1); |
| 131 | assert.equal(doorSortSubOrder("pine-n-open", 3), 1); |
| 132 | }); |
| 133 | |
| 134 | test("doors are per-tile sprites with tinted catalog states", () => |
| 135 | { |
| 136 | assert.equal(isChunkAtlasBakedBlock({ typeId: BlockType.door, state: "pine-s-closed" }), false); |
| 137 | const catalog = buildSpriteCatalog(ASSET_ROOT); |
| 138 | assert.equal(catalog.blocks.door.defaultState, "pine-s-closed"); |
| 139 | assert.equal(catalog.blocks.door.states.length, WOOD_TIER_ORDER.length * 4); |
| 140 | assert.ok(catalog.images.some((image) => image.name === "block:door:oak-n-open")); |
| 141 | const tinted = catalog.images.find((image) => image.name === "block:door:pine-s-closed"); |
| 142 | assert.ok(tinted?.tint); |
| 143 | }); |
| 144 | |
| 145 | test("players and humanoids can open doors; rats cannot unless already open", () => |
| 146 | { |
| 147 | const harness = makeSimulation({ workerCount: 1 }); |
| 148 | try |
| 149 | { |
| 150 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 151 | harness.simulation.spawnCharacter(1, character); |
| 152 | const humanoidId = harness.simulation.spawnEntity(mobComponents({ |
| 153 | tileX: 10, |
| 154 | tileY: 8, |
| 155 | mobTypeId: MobId.humanoid |
| 156 | })); |
| 157 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 158 | tileX: 11, |
| 159 | tileY: 8, |
| 160 | mobTypeId: MobId.rat |
| 161 | })); |
| 162 | harness.simulation.step(); |
| 163 | |
| 164 | const worker = workerOf(harness.simulation, character.character_id); |
| 165 | assert.equal(entityCanOpenDoors(worker.world.ecs, character.character_id), true); |
| 166 | assert.equal(entityCanOpenDoors(worker.world.ecs, humanoidId), true); |
| 167 | assert.equal(entityCanOpenDoors(worker.world.ecs, ratId), false); |
| 168 | |
| 169 | placeDoor(worker, 9, 8, "pine-s-closed"); |
| 170 | assert.equal(worker.world.walkableFor(character.character_id, 9, 8), true); |
| 171 | assert.equal(worker.world.walkableFor(ratId, 9, 8), false); |
| 172 | |
| 173 | /** Resting s-open (≈ W-closed) still blocks rats until flipped. */ |
| 174 | placeDoor(worker, 9, 8, "pine-s-open"); |
| 175 | assert.equal(worker.world.walkableFor(ratId, 9, 8), false); |
| 176 | assert.equal(worker.world.walkableFor(character.character_id, 9, 8), true); |
| 177 | } |
| 178 | finally |
| 179 | { harness.close(); } |
| 180 | }); |
| 181 | |
| 182 | test("approaching a closed door opens it and restores when vicinity clears", () => |
| 183 | { |
| 184 | const harness = makeSimulation({ workerCount: 1 }); |
| 185 | try |
| 186 | { |
| 187 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 188 | harness.simulation.spawnCharacter(1, character); |
| 189 | harness.simulation.step(); |
| 190 | const worker = workerOf(harness.simulation, character.character_id); |
| 191 | placeDoor(worker, 9, 8, "oak-n-closed"); |
| 192 | |
| 193 | const opened = maybeOpenDoorOnApproach(worker.world, character.character_id, 9, 8); |
| 194 | assert.ok(opened); |
| 195 | assert.equal(opened.block.state, "oak-n-open"); |
| 196 | assert.equal(doorAt(worker, 9, 8).state, "oak-n-open"); |
| 197 | assert.equal(doorAt(worker, 9, 8).config?.restingState, "oak-n-closed"); |
| 198 | |
| 199 | const stillHeld = stepDoors(worker.world); |
| 200 | assert.equal(stillHeld.length, 0); |
| 201 | assert.equal(doorAt(worker, 9, 8).state, "oak-n-open"); |
| 202 | |
| 203 | const handle = worker.world.ecs.handleOf(character.character_id); |
| 204 | const position = worker.world.ecs.get(handle, Component.position); |
| 205 | position.tileX = 12; |
| 206 | position.tileY = 8; |
| 207 | |
| 208 | const restored = stepDoors(worker.world); |
| 209 | assert.equal(restored.length, 1); |
| 210 | assert.equal(restored[0].block.state, "oak-n-closed"); |
| 211 | assert.equal(doorAt(worker, 9, 8).state, "oak-n-closed"); |
| 212 | } |
| 213 | finally |
| 214 | { harness.close(); } |
| 215 | }); |
| 216 | |
| 217 | test("player walking through a door flips it open via movement", () => |
| 218 | { |
| 219 | const harness = makeSimulation({ workerCount: 1 }); |
| 220 | try |
| 221 | { |
| 222 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 223 | harness.simulation.spawnCharacter(1, character); |
| 224 | harness.simulation.step(); |
| 225 | const worker = workerOf(harness.simulation, character.character_id); |
| 226 | placeDoor(worker, 9, 8, "pine-s-closed"); |
| 227 | |
| 228 | const handle = worker.world.ecs.handleOf(character.character_id); |
| 229 | const path = worker.world.ecs.get(handle, Component.path) |
| 230 | ?? worker.world.ecs.add(handle, Component.path, { tiles: [] }); |
| 231 | path.tiles = [{ x: 9, y: 8 }, { x: 10, y: 8 }]; |
| 232 | |
| 233 | for (let i = 0; i < walkTicks(2); i++) |
| 234 | { |
| 235 | harness.advance(50); |
| 236 | harness.simulation.step(); |
| 237 | } |
| 238 | |
| 239 | assert.equal(doorAt(worker, 9, 8).state, "pine-s-open"); |
| 240 | const position = worker.world.ecs.get(handle, Component.position); |
| 241 | assert.ok(position.tileX >= 9); |
| 242 | } |
| 243 | finally |
| 244 | { harness.close(); } |
| 245 | }); |
| 246 | |
| 247 | test("resting open (≈ W-closed) flips to closed for passage, then restores", () => |
| 248 | { |
| 249 | const harness = makeSimulation({ workerCount: 1 }); |
| 250 | try |
| 251 | { |
| 252 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 253 | harness.simulation.spawnCharacter(1, character); |
| 254 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 255 | tileX: 11, |
| 256 | tileY: 8, |
| 257 | mobTypeId: MobId.rat |
| 258 | })); |
| 259 | harness.simulation.step(); |
| 260 | const worker = workerOf(harness.simulation, character.character_id); |
| 261 | placeDoor(worker, 9, 8, "pine-s-open"); |
| 262 | |
| 263 | assert.equal(worker.world.walkableFor(ratId, 9, 8), false); |
| 264 | |
| 265 | const opened = maybeOpenDoorOnApproach(worker.world, character.character_id, 9, 8); |
| 266 | assert.ok(opened); |
| 267 | assert.equal(opened.block.state, "pine-s-closed"); |
| 268 | assert.equal(doorAt(worker, 9, 8).config?.restingState, "pine-s-open"); |
| 269 | assert.equal(worker.world.walkableFor(ratId, 9, 8), true); |
| 270 | |
| 271 | const handle = worker.world.ecs.handleOf(character.character_id); |
| 272 | const position = worker.world.ecs.get(handle, Component.position); |
| 273 | position.tileX = 12; |
| 274 | position.tileY = 8; |
| 275 | |
| 276 | const restored = stepDoors(worker.world); |
| 277 | assert.equal(restored.length, 1); |
| 278 | assert.equal(doorAt(worker, 9, 8).state, "pine-s-open"); |
| 279 | } |
| 280 | finally |
| 281 | { harness.close(); } |
| 282 | }); |
| 283 | |
| 284 | test("door requirements normalize and fail closed without context", () => |
| 285 | { |
| 286 | assert.equal(doorHasRequirements({ config: null }), false); |
| 287 | const gated = { |
| 288 | config: normalizeDoorConfig({ |
| 289 | when: { all: [{ condition: { kind: ConditionKind.hasItem, itemId: ItemId.apple, quantity: 1 } }] }, |
| 290 | blockedMessage: "Need an apple." |
| 291 | }) |
| 292 | }; |
| 293 | assert.equal(doorHasRequirements(gated), true); |
| 294 | assert.equal(doorRequirementsMet(gated, null), false); |
| 295 | assert.equal(isDoorTraversable(gated, { canOpenDoors: true, requirementsAllow: false }), false); |
| 296 | assert.equal(isDoorTraversable({ |
| 297 | state: "pine-s-closed", |
| 298 | config: { restingState: "pine-s-open", when: gated.config.when } |
| 299 | }, { canOpenDoors: false, requirementsAllow: false }), false); |
| 300 | }); |
| 301 | |
| 302 | test("gated doors stay impassable for players who fail requirements even when held open", () => |
| 303 | { |
| 304 | const harness = makeSimulation({ workerCount: 1 }); |
| 305 | try |
| 306 | { |
| 307 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 308 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 309 | harness.simulation.step(); |
| 310 | const worker = workerOf(harness.simulation, entityId); |
| 311 | const gate = normalizeDoorConfig({ |
| 312 | when: { all: [{ condition: { kind: ConditionKind.hasItem, itemId: ItemId.bones, quantity: 1 } }] }, |
| 313 | blockedMessage: "The door ignores you." |
| 314 | }); |
| 315 | placeDoor(worker, 9, 8, "pine-s-closed", gate); |
| 316 | |
| 317 | assert.equal(worker.world.walkableFor(entityId, 9, 8), false); |
| 318 | assert.equal(worker.world.pathWalkableFor(entityId, 9, 8), true); |
| 319 | assert.equal(maybeOpenDoorOnApproach(worker.world, entityId, 9, 8), null); |
| 320 | |
| 321 | /** Another player holds the door open — still blocked for the gated character. */ |
| 322 | placeDoor(worker, 9, 8, "pine-s-open", { |
| 323 | ...gate, |
| 324 | restingState: "pine-s-closed" |
| 325 | }); |
| 326 | assert.equal(worker.world.walkableFor(entityId, 9, 8), false); |
| 327 | assert.equal(worker.world.pathWalkableFor(entityId, 9, 8), true); |
| 328 | |
| 329 | const bag = harness.simulation.bagOf(entityId); |
| 330 | bag.inventory[0] = { itemId: ItemId.bones, quantity: 1 }; |
| 331 | harness.simulation.applyBag(entityId, bag); |
| 332 | assert.equal(worker.world.walkableFor(entityId, 9, 8), true); |
| 333 | assert.equal(worker.world.pathWalkableFor(entityId, 9, 8), true); |
| 334 | } |
| 335 | finally |
| 336 | { harness.close(); } |
| 337 | }); |
| 338 | |
| 339 | test("walking into a gated door emits a blocked toast and shove", () => |
| 340 | { |
| 341 | const harness = makeSimulation({ workerCount: 1 }); |
| 342 | try |
| 343 | { |
| 344 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 345 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 346 | harness.simulation.step(); |
| 347 | const worker = workerOf(harness.simulation, entityId); |
| 348 | const gate = normalizeDoorConfig({ |
| 349 | when: { all: [{ condition: { kind: ConditionKind.hasItem, itemId: ItemId.bones, quantity: 1 } }] }, |
| 350 | blockedMessage: "Members only." |
| 351 | }); |
| 352 | placeGatedCorridor(worker); |
| 353 | placeDoor(worker, 9, 8, "pine-s-closed", gate); |
| 354 | |
| 355 | harness.simulation.enqueue(1, { |
| 356 | type: "move-to", |
| 357 | characterId: character.character_id, |
| 358 | tileX: 10, |
| 359 | tileY: 8 |
| 360 | }); |
| 361 | harness.simulation.step(); |
| 362 | const handle = worker.world.ecs.handleOf(entityId); |
| 363 | const planned = worker.world.ecs.get(handle, Component.path); |
| 364 | assert.ok(planned.tiles.some((t) => t.x === 9 && t.y === 8), "soft-path includes gated door"); |
| 365 | |
| 366 | let toast = null; |
| 367 | let shove = null; |
| 368 | for (let i = 0; i < walkTicks(4); i++) |
| 369 | { |
| 370 | harness.advance(50); |
| 371 | const result = harness.simulation.step(); |
| 372 | toast ??= (result.events ?? []).find((e) => e.type === "toast" && e.message === "Members only."); |
| 373 | shove ??= (result.events ?? []).find((e) => e.type === "tile-shove" && e.tileX === 9 && e.tileY === 8); |
| 374 | } |
| 375 | assert.ok(toast); |
| 376 | assert.equal(toast.characterId, character.character_id); |
| 377 | assert.ok(shove); |
| 378 | assert.equal(shove.attackerId, entityId); |
| 379 | const position = worker.world.ecs.get(handle, Component.position); |
| 380 | assert.equal(position.tileX, 8); |
| 381 | assert.equal(position.tileY, 8); |
| 382 | assert.deepEqual(planned.tiles, []); |
| 383 | } |
| 384 | finally |
| 385 | { harness.close(); } |
| 386 | }); |
| 387 | |
| 388 | test("gated door path cost prefers an open detour when available", () => |
| 389 | { |
| 390 | const harness = makeSimulation({ workerCount: 1 }); |
| 391 | try |
| 392 | { |
| 393 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 394 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 395 | harness.simulation.step(); |
| 396 | const worker = workerOf(harness.simulation, entityId); |
| 397 | placeDoor(worker, 9, 8, "pine-s-closed", normalizeDoorConfig({ |
| 398 | when: { all: [{ condition: { kind: ConditionKind.hasItem, itemId: ItemId.bones, quantity: 1 } }] }, |
| 399 | blockedMessage: "Members only." |
| 400 | })); |
| 401 | |
| 402 | harness.simulation.enqueue(1, { |
| 403 | type: "move-to", |
| 404 | characterId: character.character_id, |
| 405 | tileX: 10, |
| 406 | tileY: 8 |
| 407 | }); |
| 408 | harness.simulation.step(); |
| 409 | const handle = worker.world.ecs.handleOf(entityId); |
| 410 | const path = worker.world.ecs.get(handle, Component.path); |
| 411 | assert.ok(path.tiles.length > 0); |
| 412 | assert.equal( |
| 413 | path.tiles.some((t) => t.x === 9 && t.y === 8), |
| 414 | false, |
| 415 | "should detour around gated door when cheaper" |
| 416 | ); |
| 417 | assert.equal(path.tiles[path.tiles.length - 1].x, 10); |
| 418 | assert.equal(path.tiles[path.tiles.length - 1].y, 8); |
| 419 | } |
| 420 | finally |
| 421 | { harness.close(); } |
| 422 | }); |
| 423 | |
| 424 | test("quest-gated doors unlock after conditionAccess sync", () => |
| 425 | { |
| 426 | const harness = makeSimulation({ workerCount: 1 }); |
| 427 | try |
| 428 | { |
| 429 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 430 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 431 | harness.simulation.step(); |
| 432 | const worker = workerOf(harness.simulation, entityId); |
| 433 | placeDoor(worker, 9, 8, "oak-n-closed", normalizeDoorConfig({ |
| 434 | when: { |
| 435 | all: [{ |
| 436 | condition: { kind: ConditionKind.questState, questId: "door-key-quest", state: "completed" } |
| 437 | }] |
| 438 | }, |
| 439 | blockedMessage: "Finish the quest first." |
| 440 | })); |
| 441 | assert.equal(worker.world.walkableFor(entityId, 9, 8), false); |
| 442 | |
| 443 | harness.stateDb.saveQuestState(character.character_id, "door-key-quest", "completed", {}, Date.now(), Date.now()); |
| 444 | harness.simulation.syncCharacterConditionAccess(character.character_id); |
| 445 | assert.equal(worker.world.walkableFor(entityId, 9, 8), true); |
| 446 | } |
| 447 | finally |
| 448 | { harness.close(); } |
| 449 | }); |