| 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 doorAt (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 | return data.blocks[localY * 16 + localX]; |
| 60 | } |
| 61 | |
| 62 | test("door states cover every wood facing and openness", () => |
| 63 | { |
| 64 | const states = listAllDoorStates(); |
| 65 | assert.equal(states.length, WOOD_TIER_ORDER.length * 4); |
| 66 | assert.deepEqual(parseDoorState("oak-n-open"), { wood: "oak", facing: "n", open: true }); |
| 67 | assert.equal(flipDoorState("pine-s-closed"), "pine-s-open"); |
| 68 | assert.equal(flipDoorState("birch-n-open"), "birch-n-closed"); |
| 69 | assert.equal(composeDoorState("elder", "n", false), "elder-n-closed"); |
| 70 | }); |
| 71 | |
| 72 | test("doors at resting block unless opener; passage means flipped from resting", () => |
| 73 | { |
| 74 | assert.equal(isBlockPassable(BlockType.door, "pine-s-closed"), false); |
| 75 | assert.equal(isBlockPassable(BlockType.door, "pine-s-open"), false); |
| 76 | assert.equal(isDoorTraversable({ state: "pine-s-closed" }, { canOpenDoors: false }), false); |
| 77 | assert.equal(isDoorTraversable({ state: "pine-s-closed" }, { canOpenDoors: true }), true); |
| 78 | assert.equal(isDoorTraversable({ state: "pine-s-open" }, { canOpenDoors: false }), false); |
| 79 | assert.equal(isDoorTraversable({ |
| 80 | state: "pine-s-closed", |
| 81 | config: { restingState: "pine-s-open" } |
| 82 | }, { canOpenDoors: false }), true); |
| 83 | }); |
| 84 | |
| 85 | test("light and ranged LoS follow passage-open (flipped from resting), not the open suffix", () => |
| 86 | { |
| 87 | assert.equal(blockLightOcclusionForBlock({ typeId: BlockType.door, state: "pine-s-closed" }), LightOcclusion.solid); |
| 88 | assert.equal(blockLightOcclusionForBlock({ typeId: BlockType.door, state: "pine-s-open" }), LightOcclusion.solid); |
| 89 | assert.equal(blockLightOcclusionForBlock({ |
| 90 | typeId: BlockType.door, |
| 91 | state: "pine-s-closed", |
| 92 | config: { restingState: "pine-s-open" } |
| 93 | }), LightOcclusion.clear); |
| 94 | assert.equal(blockBlocksRangedAttacks(BlockType.door, "pine-s-closed"), true); |
| 95 | assert.equal(blockBlocksRangedAttacks(BlockType.door, "pine-s-open", { |
| 96 | typeId: BlockType.door, |
| 97 | state: "pine-s-closed", |
| 98 | config: { restingState: "pine-s-open" } |
| 99 | }), false); |
| 100 | }); |
| 101 | |
| 102 | test("door sort sub-order: only S-closed is in front", () => |
| 103 | { |
| 104 | assert.equal(doorSortSubOrder("pine-s-closed", 3), 12); |
| 105 | assert.equal(doorSortSubOrder("pine-s-open", 3), 1); |
| 106 | assert.equal(doorSortSubOrder("pine-n-closed", 3), 1); |
| 107 | assert.equal(doorSortSubOrder("pine-n-open", 3), 1); |
| 108 | }); |
| 109 | |
| 110 | test("doors are per-tile sprites with tinted catalog states", () => |
| 111 | { |
| 112 | assert.equal(isChunkAtlasBakedBlock({ typeId: BlockType.door, state: "pine-s-closed" }), false); |
| 113 | const catalog = buildSpriteCatalog(ASSET_ROOT); |
| 114 | assert.equal(catalog.blocks.door.defaultState, "pine-s-closed"); |
| 115 | assert.equal(catalog.blocks.door.states.length, WOOD_TIER_ORDER.length * 4); |
| 116 | assert.ok(catalog.images.some((image) => image.name === "block:door:oak-n-open")); |
| 117 | const tinted = catalog.images.find((image) => image.name === "block:door:pine-s-closed"); |
| 118 | assert.ok(tinted?.tint); |
| 119 | }); |
| 120 | |
| 121 | test("players and humanoids can open doors; rats cannot unless already open", () => |
| 122 | { |
| 123 | const harness = makeSimulation({ workerCount: 1 }); |
| 124 | try |
| 125 | { |
| 126 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 127 | harness.simulation.spawnCharacter(1, character); |
| 128 | const humanoidId = harness.simulation.spawnEntity(mobComponents({ |
| 129 | tileX: 10, |
| 130 | tileY: 8, |
| 131 | mobTypeId: MobId.humanoid |
| 132 | })); |
| 133 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 134 | tileX: 11, |
| 135 | tileY: 8, |
| 136 | mobTypeId: MobId.rat |
| 137 | })); |
| 138 | harness.simulation.step(); |
| 139 | |
| 140 | const worker = workerOf(harness.simulation, character.character_id); |
| 141 | assert.equal(entityCanOpenDoors(worker.world.ecs, character.character_id), true); |
| 142 | assert.equal(entityCanOpenDoors(worker.world.ecs, humanoidId), true); |
| 143 | assert.equal(entityCanOpenDoors(worker.world.ecs, ratId), false); |
| 144 | |
| 145 | placeDoor(worker, 9, 8, "pine-s-closed"); |
| 146 | assert.equal(worker.world.walkableFor(character.character_id, 9, 8), true); |
| 147 | assert.equal(worker.world.walkableFor(ratId, 9, 8), false); |
| 148 | |
| 149 | /** Resting s-open (≈ W-closed) still blocks rats until flipped. */ |
| 150 | placeDoor(worker, 9, 8, "pine-s-open"); |
| 151 | assert.equal(worker.world.walkableFor(ratId, 9, 8), false); |
| 152 | assert.equal(worker.world.walkableFor(character.character_id, 9, 8), true); |
| 153 | } |
| 154 | finally |
| 155 | { harness.close(); } |
| 156 | }); |
| 157 | |
| 158 | test("approaching a closed door opens it and restores when vicinity clears", () => |
| 159 | { |
| 160 | const harness = makeSimulation({ workerCount: 1 }); |
| 161 | try |
| 162 | { |
| 163 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 164 | harness.simulation.spawnCharacter(1, character); |
| 165 | harness.simulation.step(); |
| 166 | const worker = workerOf(harness.simulation, character.character_id); |
| 167 | placeDoor(worker, 9, 8, "oak-n-closed"); |
| 168 | |
| 169 | const opened = maybeOpenDoorOnApproach(worker.world, character.character_id, 9, 8); |
| 170 | assert.ok(opened); |
| 171 | assert.equal(opened.block.state, "oak-n-open"); |
| 172 | assert.equal(doorAt(worker, 9, 8).state, "oak-n-open"); |
| 173 | assert.equal(doorAt(worker, 9, 8).config?.restingState, "oak-n-closed"); |
| 174 | |
| 175 | const stillHeld = stepDoors(worker.world); |
| 176 | assert.equal(stillHeld.length, 0); |
| 177 | assert.equal(doorAt(worker, 9, 8).state, "oak-n-open"); |
| 178 | |
| 179 | const handle = worker.world.ecs.handleOf(character.character_id); |
| 180 | const position = worker.world.ecs.get(handle, Component.position); |
| 181 | position.tileX = 12; |
| 182 | position.tileY = 8; |
| 183 | |
| 184 | const restored = stepDoors(worker.world); |
| 185 | assert.equal(restored.length, 1); |
| 186 | assert.equal(restored[0].block.state, "oak-n-closed"); |
| 187 | assert.equal(doorAt(worker, 9, 8).state, "oak-n-closed"); |
| 188 | } |
| 189 | finally |
| 190 | { harness.close(); } |
| 191 | }); |
| 192 | |
| 193 | test("player walking through a door flips it open via movement", () => |
| 194 | { |
| 195 | const harness = makeSimulation({ workerCount: 1 }); |
| 196 | try |
| 197 | { |
| 198 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 199 | harness.simulation.spawnCharacter(1, character); |
| 200 | harness.simulation.step(); |
| 201 | const worker = workerOf(harness.simulation, character.character_id); |
| 202 | placeDoor(worker, 9, 8, "pine-s-closed"); |
| 203 | |
| 204 | const handle = worker.world.ecs.handleOf(character.character_id); |
| 205 | const path = worker.world.ecs.get(handle, Component.path) |
| 206 | ?? worker.world.ecs.add(handle, Component.path, { tiles: [] }); |
| 207 | path.tiles = [{ x: 9, y: 8 }, { x: 10, y: 8 }]; |
| 208 | |
| 209 | for (let i = 0; i < walkTicks(2); i++) |
| 210 | { |
| 211 | harness.advance(50); |
| 212 | harness.simulation.step(); |
| 213 | } |
| 214 | |
| 215 | assert.equal(doorAt(worker, 9, 8).state, "pine-s-open"); |
| 216 | const position = worker.world.ecs.get(handle, Component.position); |
| 217 | assert.ok(position.tileX >= 9); |
| 218 | } |
| 219 | finally |
| 220 | { harness.close(); } |
| 221 | }); |
| 222 | |
| 223 | test("resting open (≈ W-closed) flips to closed for passage, then restores", () => |
| 224 | { |
| 225 | const harness = makeSimulation({ workerCount: 1 }); |
| 226 | try |
| 227 | { |
| 228 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 229 | harness.simulation.spawnCharacter(1, character); |
| 230 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 231 | tileX: 11, |
| 232 | tileY: 8, |
| 233 | mobTypeId: MobId.rat |
| 234 | })); |
| 235 | harness.simulation.step(); |
| 236 | const worker = workerOf(harness.simulation, character.character_id); |
| 237 | placeDoor(worker, 9, 8, "pine-s-open"); |
| 238 | |
| 239 | assert.equal(worker.world.walkableFor(ratId, 9, 8), false); |
| 240 | |
| 241 | const opened = maybeOpenDoorOnApproach(worker.world, character.character_id, 9, 8); |
| 242 | assert.ok(opened); |
| 243 | assert.equal(opened.block.state, "pine-s-closed"); |
| 244 | assert.equal(doorAt(worker, 9, 8).config?.restingState, "pine-s-open"); |
| 245 | assert.equal(worker.world.walkableFor(ratId, 9, 8), true); |
| 246 | |
| 247 | const handle = worker.world.ecs.handleOf(character.character_id); |
| 248 | const position = worker.world.ecs.get(handle, Component.position); |
| 249 | position.tileX = 12; |
| 250 | position.tileY = 8; |
| 251 | |
| 252 | const restored = stepDoors(worker.world); |
| 253 | assert.equal(restored.length, 1); |
| 254 | assert.equal(doorAt(worker, 9, 8).state, "pine-s-open"); |
| 255 | } |
| 256 | finally |
| 257 | { harness.close(); } |
| 258 | }); |
| 259 | |
| 260 | test("door requirements normalize and fail closed without context", () => |
| 261 | { |
| 262 | assert.equal(doorHasRequirements({ config: null }), false); |
| 263 | const gated = { |
| 264 | config: normalizeDoorConfig({ |
| 265 | when: { all: [{ condition: { kind: ConditionKind.hasItem, itemId: ItemId.apple, quantity: 1 } }] }, |
| 266 | blockedMessage: "Need an apple." |
| 267 | }) |
| 268 | }; |
| 269 | assert.equal(doorHasRequirements(gated), true); |
| 270 | assert.equal(doorRequirementsMet(gated, null), false); |
| 271 | assert.equal(isDoorTraversable(gated, { canOpenDoors: true, requirementsAllow: false }), false); |
| 272 | assert.equal(isDoorTraversable({ |
| 273 | state: "pine-s-closed", |
| 274 | config: { restingState: "pine-s-open", when: gated.config.when } |
| 275 | }, { canOpenDoors: false, requirementsAllow: false }), false); |
| 276 | }); |
| 277 | |
| 278 | test("gated doors stay impassable for players who fail requirements even when held open", () => |
| 279 | { |
| 280 | const harness = makeSimulation({ workerCount: 1 }); |
| 281 | try |
| 282 | { |
| 283 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 284 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 285 | harness.simulation.step(); |
| 286 | const worker = workerOf(harness.simulation, entityId); |
| 287 | const gate = normalizeDoorConfig({ |
| 288 | when: { all: [{ condition: { kind: ConditionKind.hasItem, itemId: ItemId.bones, quantity: 1 } }] }, |
| 289 | blockedMessage: "The door ignores you." |
| 290 | }); |
| 291 | placeDoor(worker, 9, 8, "pine-s-closed", gate); |
| 292 | |
| 293 | assert.equal(worker.world.walkableFor(entityId, 9, 8), false); |
| 294 | assert.equal(maybeOpenDoorOnApproach(worker.world, entityId, 9, 8), null); |
| 295 | |
| 296 | /** Another player holds the door open — still blocked for the gated character. */ |
| 297 | placeDoor(worker, 9, 8, "pine-s-open", { |
| 298 | ...gate, |
| 299 | restingState: "pine-s-closed" |
| 300 | }); |
| 301 | assert.equal(worker.world.walkableFor(entityId, 9, 8), false); |
| 302 | |
| 303 | const bag = harness.simulation.bagOf(entityId); |
| 304 | bag.inventory[0] = { itemId: ItemId.bones, quantity: 1 }; |
| 305 | harness.simulation.applyBag(entityId, bag); |
| 306 | assert.equal(worker.world.walkableFor(entityId, 9, 8), true); |
| 307 | } |
| 308 | finally |
| 309 | { harness.close(); } |
| 310 | }); |
| 311 | |
| 312 | test("walking into a gated door emits a blocked toast", () => |
| 313 | { |
| 314 | const harness = makeSimulation({ workerCount: 1 }); |
| 315 | try |
| 316 | { |
| 317 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 318 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 319 | harness.simulation.step(); |
| 320 | const worker = workerOf(harness.simulation, entityId); |
| 321 | const gate = normalizeDoorConfig({ |
| 322 | when: { all: [{ condition: { kind: ConditionKind.hasItem, itemId: ItemId.bones, quantity: 1 } }] }, |
| 323 | blockedMessage: "Members only." |
| 324 | }); |
| 325 | placeDoor(worker, 9, 8, "pine-s-closed", gate); |
| 326 | |
| 327 | const handle = worker.world.ecs.handleOf(entityId); |
| 328 | const path = worker.world.ecs.get(handle, Component.path); |
| 329 | path.tiles = [{ x: 9, y: 8 }, { x: 10, y: 8 }]; |
| 330 | |
| 331 | let toast = null; |
| 332 | for (let i = 0; i < walkTicks(2); i++) |
| 333 | { |
| 334 | harness.advance(50); |
| 335 | const result = harness.simulation.step(); |
| 336 | toast ??= (result.events ?? []).find((e) => e.type === "toast" && e.message === "Members only."); |
| 337 | } |
| 338 | assert.ok(toast); |
| 339 | assert.equal(toast.characterId, character.character_id); |
| 340 | const position = worker.world.ecs.get(handle, Component.position); |
| 341 | assert.equal(position.tileX, 8); |
| 342 | assert.equal(position.tileY, 8); |
| 343 | } |
| 344 | finally |
| 345 | { harness.close(); } |
| 346 | }); |
| 347 | |
| 348 | test("quest-gated doors unlock after conditionAccess sync", () => |
| 349 | { |
| 350 | const harness = makeSimulation({ workerCount: 1 }); |
| 351 | try |
| 352 | { |
| 353 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 354 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 355 | harness.simulation.step(); |
| 356 | const worker = workerOf(harness.simulation, entityId); |
| 357 | placeDoor(worker, 9, 8, "oak-n-closed", normalizeDoorConfig({ |
| 358 | when: { |
| 359 | all: [{ |
| 360 | condition: { kind: ConditionKind.questState, questId: "door-key-quest", state: "completed" } |
| 361 | }] |
| 362 | }, |
| 363 | blockedMessage: "Finish the quest first." |
| 364 | })); |
| 365 | assert.equal(worker.world.walkableFor(entityId, 9, 8), false); |
| 366 | |
| 367 | harness.stateDb.saveQuestState(character.character_id, "door-key-quest", "completed", {}, Date.now(), Date.now()); |
| 368 | harness.simulation.syncCharacterConditionAccess(character.character_id); |
| 369 | assert.equal(worker.world.walkableFor(entityId, 9, 8), true); |
| 370 | } |
| 371 | finally |
| 372 | { harness.close(); } |
| 373 | }); |