| 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 { |
| 9 | composeDoorState, |
| 10 | doorSortSubOrder, |
| 11 | flipDoorState, |
| 12 | isDoorTraversable, |
| 13 | listAllDoorStates, |
| 14 | parseDoorState |
| 15 | } from "../../shared/world/doors.js"; |
| 16 | import { buildSpriteCatalog } from "../src/assets/spriteCatalog.js"; |
| 17 | import { Component } from "../src/ecs/components.js"; |
| 18 | import { mobComponents } from "../src/simulation/spawners.js"; |
| 19 | import { |
| 20 | entityCanOpenDoors, |
| 21 | maybeOpenDoorOnApproach, |
| 22 | stepDoors |
| 23 | } from "../src/doors/service.js"; |
| 24 | import { makeCharacter, makeSimulation, walkTicks } from "./helpers/simulationHarness.js"; |
| 25 | |
| 26 | const ASSET_ROOT = join(fileURLToPath(new URL("../../client/assets", import.meta.url))); |
| 27 | |
| 28 | function workerOf (simulation, entityId) |
| 29 | { |
| 30 | const record = simulation.entityRecord(entityId); |
| 31 | assert.ok(record); |
| 32 | return simulation.workers[record.workerId]; |
| 33 | } |
| 34 | |
| 35 | function placeDoor (worker, tileX, tileY, state) |
| 36 | { |
| 37 | const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16)); |
| 38 | const localX = ((tileX % 16) + 16) % 16; |
| 39 | const localY = ((tileY % 16) + 16) % 16; |
| 40 | data.blocks[localY * 16 + localX] = { |
| 41 | typeId: BlockType.door, |
| 42 | state, |
| 43 | config: null |
| 44 | }; |
| 45 | } |
| 46 | |
| 47 | function doorAt (worker, tileX, tileY) |
| 48 | { |
| 49 | const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16)); |
| 50 | const localX = ((tileX % 16) + 16) % 16; |
| 51 | const localY = ((tileY % 16) + 16) % 16; |
| 52 | return data.blocks[localY * 16 + localX]; |
| 53 | } |
| 54 | |
| 55 | test("door states cover every wood facing and openness", () => |
| 56 | { |
| 57 | const states = listAllDoorStates(); |
| 58 | assert.equal(states.length, WOOD_TIER_ORDER.length * 4); |
| 59 | assert.deepEqual(parseDoorState("oak-n-open"), { wood: "oak", facing: "n", open: true }); |
| 60 | assert.equal(flipDoorState("pine-s-closed"), "pine-s-open"); |
| 61 | assert.equal(flipDoorState("birch-n-open"), "birch-n-closed"); |
| 62 | assert.equal(composeDoorState("elder", "n", false), "elder-n-closed"); |
| 63 | }); |
| 64 | |
| 65 | test("doors at resting block unless opener; passage means flipped from resting", () => |
| 66 | { |
| 67 | assert.equal(isBlockPassable(BlockType.door, "pine-s-closed"), false); |
| 68 | assert.equal(isBlockPassable(BlockType.door, "pine-s-open"), false); |
| 69 | assert.equal(isDoorTraversable({ state: "pine-s-closed" }, { canOpenDoors: false }), false); |
| 70 | assert.equal(isDoorTraversable({ state: "pine-s-closed" }, { canOpenDoors: true }), true); |
| 71 | assert.equal(isDoorTraversable({ state: "pine-s-open" }, { canOpenDoors: false }), false); |
| 72 | assert.equal(isDoorTraversable({ |
| 73 | state: "pine-s-closed", |
| 74 | config: { restingState: "pine-s-open" } |
| 75 | }, { canOpenDoors: false }), true); |
| 76 | }); |
| 77 | |
| 78 | test("light and ranged LoS follow passage-open (flipped from resting), not the open suffix", () => |
| 79 | { |
| 80 | assert.equal(blockLightOcclusionForBlock({ typeId: BlockType.door, state: "pine-s-closed" }), LightOcclusion.solid); |
| 81 | assert.equal(blockLightOcclusionForBlock({ typeId: BlockType.door, state: "pine-s-open" }), LightOcclusion.solid); |
| 82 | assert.equal(blockLightOcclusionForBlock({ |
| 83 | typeId: BlockType.door, |
| 84 | state: "pine-s-closed", |
| 85 | config: { restingState: "pine-s-open" } |
| 86 | }), LightOcclusion.clear); |
| 87 | assert.equal(blockBlocksRangedAttacks(BlockType.door, "pine-s-closed"), true); |
| 88 | assert.equal(blockBlocksRangedAttacks(BlockType.door, "pine-s-open", { |
| 89 | typeId: BlockType.door, |
| 90 | state: "pine-s-closed", |
| 91 | config: { restingState: "pine-s-open" } |
| 92 | }), false); |
| 93 | }); |
| 94 | |
| 95 | test("door sort sub-order: only S-closed is in front", () => |
| 96 | { |
| 97 | assert.equal(doorSortSubOrder("pine-s-closed", 3), 12); |
| 98 | assert.equal(doorSortSubOrder("pine-s-open", 3), 1); |
| 99 | assert.equal(doorSortSubOrder("pine-n-closed", 3), 1); |
| 100 | assert.equal(doorSortSubOrder("pine-n-open", 3), 1); |
| 101 | }); |
| 102 | |
| 103 | test("doors are per-tile sprites with tinted catalog states", () => |
| 104 | { |
| 105 | assert.equal(isChunkAtlasBakedBlock({ typeId: BlockType.door, state: "pine-s-closed" }), false); |
| 106 | const catalog = buildSpriteCatalog(ASSET_ROOT); |
| 107 | assert.equal(catalog.blocks.door.defaultState, "pine-s-closed"); |
| 108 | assert.equal(catalog.blocks.door.states.length, WOOD_TIER_ORDER.length * 4); |
| 109 | assert.ok(catalog.images.some((image) => image.name === "block:door:oak-n-open")); |
| 110 | const tinted = catalog.images.find((image) => image.name === "block:door:pine-s-closed"); |
| 111 | assert.ok(tinted?.tint); |
| 112 | }); |
| 113 | |
| 114 | test("players and humanoids can open doors; rats cannot unless already open", () => |
| 115 | { |
| 116 | const harness = makeSimulation({ workerCount: 1 }); |
| 117 | try |
| 118 | { |
| 119 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 120 | harness.simulation.spawnCharacter(1, character); |
| 121 | const humanoidId = harness.simulation.spawnEntity(mobComponents({ |
| 122 | tileX: 10, |
| 123 | tileY: 8, |
| 124 | mobTypeId: MobId.humanoid |
| 125 | })); |
| 126 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 127 | tileX: 11, |
| 128 | tileY: 8, |
| 129 | mobTypeId: MobId.rat |
| 130 | })); |
| 131 | harness.simulation.step(); |
| 132 | |
| 133 | const worker = workerOf(harness.simulation, character.character_id); |
| 134 | assert.equal(entityCanOpenDoors(worker.world.ecs, character.character_id), true); |
| 135 | assert.equal(entityCanOpenDoors(worker.world.ecs, humanoidId), true); |
| 136 | assert.equal(entityCanOpenDoors(worker.world.ecs, ratId), false); |
| 137 | |
| 138 | placeDoor(worker, 9, 8, "pine-s-closed"); |
| 139 | assert.equal(worker.world.walkableFor(character.character_id, 9, 8), true); |
| 140 | assert.equal(worker.world.walkableFor(ratId, 9, 8), false); |
| 141 | |
| 142 | /** Resting s-open (≈ W-closed) still blocks rats until flipped. */ |
| 143 | placeDoor(worker, 9, 8, "pine-s-open"); |
| 144 | assert.equal(worker.world.walkableFor(ratId, 9, 8), false); |
| 145 | assert.equal(worker.world.walkableFor(character.character_id, 9, 8), true); |
| 146 | } |
| 147 | finally |
| 148 | { harness.close(); } |
| 149 | }); |
| 150 | |
| 151 | test("approaching a closed door opens it and restores when vicinity clears", () => |
| 152 | { |
| 153 | const harness = makeSimulation({ workerCount: 1 }); |
| 154 | try |
| 155 | { |
| 156 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 157 | harness.simulation.spawnCharacter(1, character); |
| 158 | harness.simulation.step(); |
| 159 | const worker = workerOf(harness.simulation, character.character_id); |
| 160 | placeDoor(worker, 9, 8, "oak-n-closed"); |
| 161 | |
| 162 | const opened = maybeOpenDoorOnApproach(worker.world, character.character_id, 9, 8); |
| 163 | assert.ok(opened); |
| 164 | assert.equal(opened.block.state, "oak-n-open"); |
| 165 | assert.equal(doorAt(worker, 9, 8).state, "oak-n-open"); |
| 166 | assert.equal(doorAt(worker, 9, 8).config?.restingState, "oak-n-closed"); |
| 167 | |
| 168 | const stillHeld = stepDoors(worker.world); |
| 169 | assert.equal(stillHeld.length, 0); |
| 170 | assert.equal(doorAt(worker, 9, 8).state, "oak-n-open"); |
| 171 | |
| 172 | const handle = worker.world.ecs.handleOf(character.character_id); |
| 173 | const position = worker.world.ecs.get(handle, Component.position); |
| 174 | position.tileX = 12; |
| 175 | position.tileY = 8; |
| 176 | |
| 177 | const restored = stepDoors(worker.world); |
| 178 | assert.equal(restored.length, 1); |
| 179 | assert.equal(restored[0].block.state, "oak-n-closed"); |
| 180 | assert.equal(doorAt(worker, 9, 8).state, "oak-n-closed"); |
| 181 | } |
| 182 | finally |
| 183 | { harness.close(); } |
| 184 | }); |
| 185 | |
| 186 | test("player walking through a door flips it open via movement", () => |
| 187 | { |
| 188 | const harness = makeSimulation({ workerCount: 1 }); |
| 189 | try |
| 190 | { |
| 191 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 192 | harness.simulation.spawnCharacter(1, character); |
| 193 | harness.simulation.step(); |
| 194 | const worker = workerOf(harness.simulation, character.character_id); |
| 195 | placeDoor(worker, 9, 8, "pine-s-closed"); |
| 196 | |
| 197 | const handle = worker.world.ecs.handleOf(character.character_id); |
| 198 | const path = worker.world.ecs.get(handle, Component.path) |
| 199 | ?? worker.world.ecs.add(handle, Component.path, { tiles: [] }); |
| 200 | path.tiles = [{ x: 9, y: 8 }, { x: 10, y: 8 }]; |
| 201 | |
| 202 | for (let i = 0; i < walkTicks(2); i++) |
| 203 | { |
| 204 | harness.advance(50); |
| 205 | harness.simulation.step(); |
| 206 | } |
| 207 | |
| 208 | assert.equal(doorAt(worker, 9, 8).state, "pine-s-open"); |
| 209 | const position = worker.world.ecs.get(handle, Component.position); |
| 210 | assert.ok(position.tileX >= 9); |
| 211 | } |
| 212 | finally |
| 213 | { harness.close(); } |
| 214 | }); |
| 215 | |
| 216 | test("resting open (≈ W-closed) flips to closed for passage, then restores", () => |
| 217 | { |
| 218 | const harness = makeSimulation({ workerCount: 1 }); |
| 219 | try |
| 220 | { |
| 221 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 222 | harness.simulation.spawnCharacter(1, character); |
| 223 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 224 | tileX: 11, |
| 225 | tileY: 8, |
| 226 | mobTypeId: MobId.rat |
| 227 | })); |
| 228 | harness.simulation.step(); |
| 229 | const worker = workerOf(harness.simulation, character.character_id); |
| 230 | placeDoor(worker, 9, 8, "pine-s-open"); |
| 231 | |
| 232 | assert.equal(worker.world.walkableFor(ratId, 9, 8), false); |
| 233 | |
| 234 | const opened = maybeOpenDoorOnApproach(worker.world, character.character_id, 9, 8); |
| 235 | assert.ok(opened); |
| 236 | assert.equal(opened.block.state, "pine-s-closed"); |
| 237 | assert.equal(doorAt(worker, 9, 8).config?.restingState, "pine-s-open"); |
| 238 | assert.equal(worker.world.walkableFor(ratId, 9, 8), true); |
| 239 | |
| 240 | const handle = worker.world.ecs.handleOf(character.character_id); |
| 241 | const position = worker.world.ecs.get(handle, Component.position); |
| 242 | position.tileX = 12; |
| 243 | position.tileY = 8; |
| 244 | |
| 245 | const restored = stepDoors(worker.world); |
| 246 | assert.equal(restored.length, 1); |
| 247 | assert.equal(doorAt(worker, 9, 8).state, "pine-s-open"); |
| 248 | } |
| 249 | finally |
| 250 | { harness.close(); } |
| 251 | }); |