import test from "node:test"; import assert from "node:assert/strict"; import { fileURLToPath } from "node:url"; import { join } from "node:path"; import { BlockType, isBlockPassable, isChunkAtlasBakedBlock, blockLightOcclusionForBlock, blockBlocksRangedAttacks, LightOcclusion } from "../../shared/definitions/tiles.js"; import { WOOD_TIER_ORDER } from "../../shared/definitions/materialTiers.js"; import { MobId } from "../../shared/definitions/mobs.js"; import { ItemId } from "../../shared/definitions/items.js"; import { ConditionKind } from "../../shared/quests/conditions.js"; import { composeDoorState, doorSortSubOrder, flipDoorState, isDoorTraversable, listAllDoorStates, parseDoorState } from "../../shared/world/doors.js"; import { doorHasRequirements, doorRequirementsMet, normalizeDoorConfig } from "../../shared/world/doorRequirements.js"; import { buildSpriteCatalog } from "../src/assets/spriteCatalog.js"; import { Component } from "../src/ecs/components.js"; import { mobComponents } from "../src/simulation/spawners.js"; import { entityCanOpenDoors, maybeOpenDoorOnApproach, stepDoors } from "../src/doors/service.js"; import { makeCharacter, makeSimulation, walkTicks } from "./helpers/simulationHarness.js"; const ASSET_ROOT = join(fileURLToPath(new URL("../../client/assets", import.meta.url))); function workerOf (simulation, entityId) { const record = simulation.entityRecord(entityId); assert.ok(record); return simulation.workers[record.workerId]; } function placeDoor (worker, tileX, tileY, state, config = null) { const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16)); const localX = ((tileX % 16) + 16) % 16; const localY = ((tileY % 16) + 16) % 16; data.blocks[localY * 16 + localX] = { typeId: BlockType.door, state, config }; } function placeWall (worker, tileX, tileY) { const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16)); const localX = ((tileX % 16) + 16) % 16; const localY = ((tileY % 16) + 16) % 16; data.blocks[localY * 16 + localX] = { typeId: BlockType.stoneWall, state: null, config: null }; } /** One-tile corridor through (8,8)-(9,8)-(10,8) so pathing cannot detour. */ function placeGatedCorridor (worker) { for (let x = 7; x <= 11; x++) { placeWall(worker, x, 7); placeWall(worker, x, 9); } placeWall(worker, 7, 8); placeWall(worker, 11, 8); } function doorAt (worker, tileX, tileY) { const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16)); const localX = ((tileX % 16) + 16) % 16; const localY = ((tileY % 16) + 16) % 16; return data.blocks[localY * 16 + localX]; } test("door states cover every wood facing and openness", () => { const states = listAllDoorStates(); assert.equal(states.length, WOOD_TIER_ORDER.length * 4); assert.deepEqual(parseDoorState("oak-n-open"), { wood: "oak", facing: "n", open: true }); assert.equal(flipDoorState("pine-s-closed"), "pine-s-open"); assert.equal(flipDoorState("birch-n-open"), "birch-n-closed"); assert.equal(composeDoorState("elder", "n", false), "elder-n-closed"); }); test("doors at resting block unless opener; passage means flipped from resting", () => { assert.equal(isBlockPassable(BlockType.door, "pine-s-closed"), false); assert.equal(isBlockPassable(BlockType.door, "pine-s-open"), false); assert.equal(isDoorTraversable({ state: "pine-s-closed" }, { canOpenDoors: false }), false); assert.equal(isDoorTraversable({ state: "pine-s-closed" }, { canOpenDoors: true }), true); assert.equal(isDoorTraversable({ state: "pine-s-open" }, { canOpenDoors: false }), false); assert.equal(isDoorTraversable({ state: "pine-s-closed", config: { restingState: "pine-s-open" } }, { canOpenDoors: false }), true); }); test("light and ranged LoS follow passage-open (flipped from resting), not the open suffix", () => { assert.equal(blockLightOcclusionForBlock({ typeId: BlockType.door, state: "pine-s-closed" }), LightOcclusion.solid); assert.equal(blockLightOcclusionForBlock({ typeId: BlockType.door, state: "pine-s-open" }), LightOcclusion.solid); assert.equal(blockLightOcclusionForBlock({ typeId: BlockType.door, state: "pine-s-closed", config: { restingState: "pine-s-open" } }), LightOcclusion.clear); assert.equal(blockBlocksRangedAttacks(BlockType.door, "pine-s-closed"), true); assert.equal(blockBlocksRangedAttacks(BlockType.door, "pine-s-open", { typeId: BlockType.door, state: "pine-s-closed", config: { restingState: "pine-s-open" } }), false); }); test("door sort sub-order: only S-closed is in front", () => { assert.equal(doorSortSubOrder("pine-s-closed", 3), 12); assert.equal(doorSortSubOrder("pine-s-open", 3), 1); assert.equal(doorSortSubOrder("pine-n-closed", 3), 1); assert.equal(doorSortSubOrder("pine-n-open", 3), 1); }); test("doors are per-tile sprites with tinted catalog states", () => { assert.equal(isChunkAtlasBakedBlock({ typeId: BlockType.door, state: "pine-s-closed" }), false); const catalog = buildSpriteCatalog(ASSET_ROOT); assert.equal(catalog.blocks.door.defaultState, "pine-s-closed"); assert.equal(catalog.blocks.door.states.length, WOOD_TIER_ORDER.length * 4); assert.ok(catalog.images.some((image) => image.name === "block:door:oak-n-open")); const tinted = catalog.images.find((image) => image.name === "block:door:pine-s-closed"); assert.ok(tinted?.tint); }); test("players and humanoids can open doors; rats cannot unless already open", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); harness.simulation.spawnCharacter(1, character); const humanoidId = harness.simulation.spawnEntity(mobComponents({ tileX: 10, tileY: 8, mobTypeId: MobId.humanoid })); const ratId = harness.simulation.spawnEntity(mobComponents({ tileX: 11, tileY: 8, mobTypeId: MobId.rat })); harness.simulation.step(); const worker = workerOf(harness.simulation, character.character_id); assert.equal(entityCanOpenDoors(worker.world.ecs, character.character_id), true); assert.equal(entityCanOpenDoors(worker.world.ecs, humanoidId), true); assert.equal(entityCanOpenDoors(worker.world.ecs, ratId), false); placeDoor(worker, 9, 8, "pine-s-closed"); assert.equal(worker.world.walkableFor(character.character_id, 9, 8), true); assert.equal(worker.world.walkableFor(ratId, 9, 8), false); /** Resting s-open (≈ W-closed) still blocks rats until flipped. */ placeDoor(worker, 9, 8, "pine-s-open"); assert.equal(worker.world.walkableFor(ratId, 9, 8), false); assert.equal(worker.world.walkableFor(character.character_id, 9, 8), true); } finally { harness.close(); } }); test("approaching a closed door opens it and restores when vicinity clears", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); harness.simulation.spawnCharacter(1, character); harness.simulation.step(); const worker = workerOf(harness.simulation, character.character_id); placeDoor(worker, 9, 8, "oak-n-closed"); const opened = maybeOpenDoorOnApproach(worker.world, character.character_id, 9, 8); assert.ok(opened); assert.equal(opened.block.state, "oak-n-open"); assert.equal(doorAt(worker, 9, 8).state, "oak-n-open"); assert.equal(doorAt(worker, 9, 8).config?.restingState, "oak-n-closed"); const stillHeld = stepDoors(worker.world); assert.equal(stillHeld.length, 0); assert.equal(doorAt(worker, 9, 8).state, "oak-n-open"); const handle = worker.world.ecs.handleOf(character.character_id); const position = worker.world.ecs.get(handle, Component.position); position.tileX = 12; position.tileY = 8; const restored = stepDoors(worker.world); assert.equal(restored.length, 1); assert.equal(restored[0].block.state, "oak-n-closed"); assert.equal(doorAt(worker, 9, 8).state, "oak-n-closed"); } finally { harness.close(); } }); test("player walking through a door flips it open via movement", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); harness.simulation.spawnCharacter(1, character); harness.simulation.step(); const worker = workerOf(harness.simulation, character.character_id); placeDoor(worker, 9, 8, "pine-s-closed"); const handle = worker.world.ecs.handleOf(character.character_id); const path = worker.world.ecs.get(handle, Component.path) ?? worker.world.ecs.add(handle, Component.path, { tiles: [] }); path.tiles = [{ x: 9, y: 8 }, { x: 10, y: 8 }]; for (let i = 0; i < walkTicks(2); i++) { harness.advance(50); harness.simulation.step(); } assert.equal(doorAt(worker, 9, 8).state, "pine-s-open"); const position = worker.world.ecs.get(handle, Component.position); assert.ok(position.tileX >= 9); } finally { harness.close(); } }); test("resting open (≈ W-closed) flips to closed for passage, then restores", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); harness.simulation.spawnCharacter(1, character); const ratId = harness.simulation.spawnEntity(mobComponents({ tileX: 11, tileY: 8, mobTypeId: MobId.rat })); harness.simulation.step(); const worker = workerOf(harness.simulation, character.character_id); placeDoor(worker, 9, 8, "pine-s-open"); assert.equal(worker.world.walkableFor(ratId, 9, 8), false); const opened = maybeOpenDoorOnApproach(worker.world, character.character_id, 9, 8); assert.ok(opened); assert.equal(opened.block.state, "pine-s-closed"); assert.equal(doorAt(worker, 9, 8).config?.restingState, "pine-s-open"); assert.equal(worker.world.walkableFor(ratId, 9, 8), true); const handle = worker.world.ecs.handleOf(character.character_id); const position = worker.world.ecs.get(handle, Component.position); position.tileX = 12; position.tileY = 8; const restored = stepDoors(worker.world); assert.equal(restored.length, 1); assert.equal(doorAt(worker, 9, 8).state, "pine-s-open"); } finally { harness.close(); } }); test("door requirements normalize and fail closed without context", () => { assert.equal(doorHasRequirements({ config: null }), false); const gated = { config: normalizeDoorConfig({ when: { all: [{ condition: { kind: ConditionKind.hasItem, itemId: ItemId.apple, quantity: 1 } }] }, blockedMessage: "Need an apple." }) }; assert.equal(doorHasRequirements(gated), true); assert.equal(doorRequirementsMet(gated, null), false); assert.equal(isDoorTraversable(gated, { canOpenDoors: true, requirementsAllow: false }), false); assert.equal(isDoorTraversable({ state: "pine-s-closed", config: { restingState: "pine-s-open", when: gated.config.when } }, { canOpenDoors: false, requirementsAllow: false }), false); }); test("gated doors stay impassable for players who fail requirements even when held open", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const entityId = harness.simulation.spawnCharacter(1, character); harness.simulation.step(); const worker = workerOf(harness.simulation, entityId); const gate = normalizeDoorConfig({ when: { all: [{ condition: { kind: ConditionKind.hasItem, itemId: ItemId.bones, quantity: 1 } }] }, blockedMessage: "The door ignores you." }); placeDoor(worker, 9, 8, "pine-s-closed", gate); assert.equal(worker.world.walkableFor(entityId, 9, 8), false); assert.equal(worker.world.pathWalkableFor(entityId, 9, 8), true); assert.equal(maybeOpenDoorOnApproach(worker.world, entityId, 9, 8), null); /** Another player holds the door open — still blocked for the gated character. */ placeDoor(worker, 9, 8, "pine-s-open", { ...gate, restingState: "pine-s-closed" }); assert.equal(worker.world.walkableFor(entityId, 9, 8), false); assert.equal(worker.world.pathWalkableFor(entityId, 9, 8), true); const bag = harness.simulation.bagOf(entityId); bag.inventory[0] = { itemId: ItemId.bones, quantity: 1 }; harness.simulation.applyBag(entityId, bag); assert.equal(worker.world.walkableFor(entityId, 9, 8), true); assert.equal(worker.world.pathWalkableFor(entityId, 9, 8), true); } finally { harness.close(); } }); test("walking into a gated door emits a blocked toast and shove", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const entityId = harness.simulation.spawnCharacter(1, character); harness.simulation.step(); const worker = workerOf(harness.simulation, entityId); const gate = normalizeDoorConfig({ when: { all: [{ condition: { kind: ConditionKind.hasItem, itemId: ItemId.bones, quantity: 1 } }] }, blockedMessage: "Members only." }); placeGatedCorridor(worker); placeDoor(worker, 9, 8, "pine-s-closed", gate); harness.simulation.enqueue(1, { type: "move-to", characterId: character.character_id, tileX: 10, tileY: 8 }); harness.simulation.step(); const handle = worker.world.ecs.handleOf(entityId); const planned = worker.world.ecs.get(handle, Component.path); assert.ok(planned.tiles.some((t) => t.x === 9 && t.y === 8), "soft-path includes gated door"); let toast = null; let shove = null; for (let i = 0; i < walkTicks(4); i++) { harness.advance(50); const result = harness.simulation.step(); toast ??= (result.events ?? []).find((e) => e.type === "toast" && e.message === "Members only."); shove ??= (result.events ?? []).find((e) => e.type === "tile-shove" && e.tileX === 9 && e.tileY === 8); } assert.ok(toast); assert.equal(toast.characterId, character.character_id); assert.ok(shove); assert.equal(shove.attackerId, entityId); const position = worker.world.ecs.get(handle, Component.position); assert.equal(position.tileX, 8); assert.equal(position.tileY, 8); assert.deepEqual(planned.tiles, []); } finally { harness.close(); } }); test("gated door path cost prefers an open detour when available", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const entityId = harness.simulation.spawnCharacter(1, character); harness.simulation.step(); const worker = workerOf(harness.simulation, entityId); placeDoor(worker, 9, 8, "pine-s-closed", normalizeDoorConfig({ when: { all: [{ condition: { kind: ConditionKind.hasItem, itemId: ItemId.bones, quantity: 1 } }] }, blockedMessage: "Members only." })); harness.simulation.enqueue(1, { type: "move-to", characterId: character.character_id, tileX: 10, tileY: 8 }); harness.simulation.step(); const handle = worker.world.ecs.handleOf(entityId); const path = worker.world.ecs.get(handle, Component.path); assert.ok(path.tiles.length > 0); assert.equal( path.tiles.some((t) => t.x === 9 && t.y === 8), false, "should detour around gated door when cheaper" ); assert.equal(path.tiles[path.tiles.length - 1].x, 10); assert.equal(path.tiles[path.tiles.length - 1].y, 8); } finally { harness.close(); } }); test("quest-gated doors unlock after conditionAccess sync", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const entityId = harness.simulation.spawnCharacter(1, character); harness.simulation.step(); const worker = workerOf(harness.simulation, entityId); placeDoor(worker, 9, 8, "oak-n-closed", normalizeDoorConfig({ when: { all: [{ condition: { kind: ConditionKind.questState, questId: "door-key-quest", state: "completed" } }] }, blockedMessage: "Finish the quest first." })); assert.equal(worker.world.walkableFor(entityId, 9, 8), false); harness.stateDb.saveQuestState(character.character_id, "door-key-quest", "completed", {}, Date.now(), Date.now()); harness.simulation.syncCharacterConditionAccess(character.character_id); assert.equal(worker.world.walkableFor(entityId, 9, 8), true); } finally { harness.close(); } });