BlueGit

timfa questward

Treat door passage as invert of resting state.

Authored open/closed is the resting visual; opening flips to the opposite until vicinity clears, so W-facing resting s-open actually opens.

timfa Tim Falken · pushed by timfa

Authored 2026-09-02 · 2 months ago

Hash
9134982ab057a6f9689745ee928365661be5a18e
Parent
33ee6492
5 files changed +97 −34
server/src/doors/service.js +12 −11
@@ -3,7 +3,8 @@ import { mobDefinition, isHumanoidMob } from "../../../shared/definitions/mobs.j
3 3 import { BlockType } from "../../../shared/definitions/tiles.js";
4 4 import {
5 5 flipDoorState,
6 isDoorOpenState,
6 isDoorPassageOpen,
7 doorRestingState,
7 8 parseDoorState
8 9 } from "../../../shared/world/doors.js";
9 10 import { tileToChunk, tileToLocal, tileKey, parseTileKey } from "../world/coordinates.js";
@@ -91,8 +92,8 @@ function writeDoorState (world, tileX, tileY, state, config, dimensionId = OVERW
91 92 }
92 93
93 94 /**
94 * Flip a closed door open (or ensure held) so a canOpenDoors entity can pass.
95 * Resting authored state is remembered until the vicinity clears.
95 * Flip the door away from its resting (authored) state so a canOpenDoors entity
96 * can pass. Resting is remembered until the vicinity clears.
96 97 */
97 98 export function holdDoorForPassage (world, tileX, tileY, dimensionId = OVERWORLD_ID)
98 99 {
@@ -105,18 +106,18 @@ export function holdDoorForPassage (world, tileX, tileY, dimensionId = OVERWORLD
105 106 const held = ensureHeldDoors(world);
106 107 const key = tileKey(tileX, tileY, dimensionId);
107 108 let restingState = held.get(key)?.restingState
108 ?? block.config?.restingState
109 ?? block.state;
109 ?? doorRestingState(block);
110 110 if (!parseDoorState(restingState))
111 111 { restingState = block.state; }
112 112
113 113 if (!held.has(key))
114 114 { held.set(key, { restingState, dimensionId }); }
115 115
116 if (parsed.open)
116 /** Already flipped for passage — keep held, no visual change. */
117 if (block.state !== restingState)
117 118 { return null; }
118 119
119 const next = flipDoorState(block.state);
120 const next = flipDoorState(restingState);
120 121 if (!next || next === block.state)
121 122 { return null; }
122 123 const config = {
@@ -175,7 +176,7 @@ export function stepDoors (world)
175 176 held.delete(key);
176 177 continue;
177 178 }
178 const resting = entry.restingState ?? block.config?.restingState ?? block.state;
179 const resting = entry.restingState ?? doorRestingState(block) ?? block.state;
179 180 if (block.state !== resting)
180 181 {
181 182 const config = {
@@ -192,7 +193,7 @@ export function stepDoors (world)
192 193 }
193 194
194 195 /**
195 * If the next path tile is a closed door and this entity can open doors, flip it.
196 * If the next path tile is a door still at resting, flip it for passage.
196 197 */
197 198 export function maybeOpenDoorOnApproach (world, entityId, nextTileX, nextTileY)
198 199 {
@@ -202,7 +203,7 @@ export function maybeOpenDoorOnApproach (world, entityId, nextTileX, nextTileY)
202 203 const position = handle != null ? world.ecs.get(handle, Component.position) : null;
203 204 const dimensionId = position?.dimensionId ?? OVERWORLD_ID;
204 205 const block = readBlock(world, nextTileX, nextTileY, dimensionId);
205 if (!isDoorBlock(block) || isDoorOpenState(block.state))
206 if (!isDoorBlock(block) || isDoorPassageOpen(block))
206 207 { return null; }
207 208 return holdDoorForPassage(world, nextTileX, nextTileY, dimensionId);
208 209 }
@@ -211,5 +212,5 @@ export function doorBlocksRangedAttacks (block)
211 212 {
212 213 if (!isDoorBlock(block))
213 214 { return null; }
214 return !isDoorOpenState(block.state);
215 return !isDoorPassageOpen(block);
215 216 }
@@ -3,7 +3,8 @@ import { mobDefinition, isHumanoidMob } from "../../../shared/definitions/mobs.j
3import { BlockType } from "../../../shared/definitions/tiles.js";
3import { BlockType } from "../../../shared/definitions/tiles.js";
4import {
4import {
5 flipDoorState,
5 flipDoorState,
6 isDoorOpenState,
6 isDoorPassageOpen,
7 doorRestingState,
7 parseDoorState
8 parseDoorState
8} from "../../../shared/world/doors.js";
9} from "../../../shared/world/doors.js";
9import { tileToChunk, tileToLocal, tileKey, parseTileKey } from "../world/coordinates.js";
10import { tileToChunk, tileToLocal, tileKey, parseTileKey } from "../world/coordinates.js";
@@ -91,8 +92,8 @@ function writeDoorState (world, tileX, tileY, state, config, dimensionId = OVERW
91}
92}
92
93
93/**
94/**
94 * Flip a closed door open (or ensure held) so a canOpenDoors entity can pass.
95 * Flip the door away from its resting (authored) state so a canOpenDoors entity
95 * Resting authored state is remembered until the vicinity clears.
96 * can pass. Resting is remembered until the vicinity clears.
96 */
97 */
97export function holdDoorForPassage (world, tileX, tileY, dimensionId = OVERWORLD_ID)
98export function holdDoorForPassage (world, tileX, tileY, dimensionId = OVERWORLD_ID)
98{
99{
@@ -105,18 +106,18 @@ export function holdDoorForPassage (world, tileX, tileY, dimensionId = OVERWORLD
105 const held = ensureHeldDoors(world);
106 const held = ensureHeldDoors(world);
106 const key = tileKey(tileX, tileY, dimensionId);
107 const key = tileKey(tileX, tileY, dimensionId);
107 let restingState = held.get(key)?.restingState
108 let restingState = held.get(key)?.restingState
108 ?? block.config?.restingState
109 ?? doorRestingState(block);
109 ?? block.state;
110 if (!parseDoorState(restingState))
110 if (!parseDoorState(restingState))
111 { restingState = block.state; }
111 { restingState = block.state; }
112
112
113 if (!held.has(key))
113 if (!held.has(key))
114 { held.set(key, { restingState, dimensionId }); }
114 { held.set(key, { restingState, dimensionId }); }
115
115
116 if (parsed.open)
116 /** Already flipped for passage — keep held, no visual change. */
117 if (block.state !== restingState)
117 { return null; }
118 { return null; }
118
119
119 const next = flipDoorState(block.state);
120 const next = flipDoorState(restingState);
120 if (!next || next === block.state)
121 if (!next || next === block.state)
121 { return null; }
122 { return null; }
122 const config = {
123 const config = {
@@ -175,7 +176,7 @@ export function stepDoors (world)
175 held.delete(key);
176 held.delete(key);
176 continue;
177 continue;
177 }
178 }
178 const resting = entry.restingState ?? block.config?.restingState ?? block.state;
179 const resting = entry.restingState ?? doorRestingState(block) ?? block.state;
179 if (block.state !== resting)
180 if (block.state !== resting)
180 {
181 {
181 const config = {
182 const config = {
@@ -192,7 +193,7 @@ export function stepDoors (world)
192}
193}
193
194
194/**
195/**
195 * If the next path tile is a closed door and this entity can open doors, flip it.
196 * If the next path tile is a door still at resting, flip it for passage.
196 */
197 */
197export function maybeOpenDoorOnApproach (world, entityId, nextTileX, nextTileY)
198export function maybeOpenDoorOnApproach (world, entityId, nextTileX, nextTileY)
198{
199{
@@ -202,7 +203,7 @@ export function maybeOpenDoorOnApproach (world, entityId, nextTileX, nextTileY)
202 const position = handle != null ? world.ecs.get(handle, Component.position) : null;
203 const position = handle != null ? world.ecs.get(handle, Component.position) : null;
203 const dimensionId = position?.dimensionId ?? OVERWORLD_ID;
204 const dimensionId = position?.dimensionId ?? OVERWORLD_ID;
204 const block = readBlock(world, nextTileX, nextTileY, dimensionId);
205 const block = readBlock(world, nextTileX, nextTileY, dimensionId);
205 if (!isDoorBlock(block) || isDoorOpenState(block.state))
206 if (!isDoorBlock(block) || isDoorPassageOpen(block))
206 { return null; }
207 { return null; }
207 return holdDoorForPassage(world, nextTileX, nextTileY, dimensionId);
208 return holdDoorForPassage(world, nextTileX, nextTileY, dimensionId);
208}
209}
@@ -211,5 +212,5 @@ export function doorBlocksRangedAttacks (block)
211{
212{
212 if (!isDoorBlock(block))
213 if (!isDoorBlock(block))
213 { return null; }
214 { return null; }
214 return !isDoorOpenState(block.state);
215 return !isDoorPassageOpen(block);
215}
216}
server/src/world/chunkData.js +1 −1
@@ -206,7 +206,7 @@ export function isChunkTileClearForRangedAttacks (data, localX, localY)
206 206 if (!isGroundPassable(ground.typeId))
207 207 { return false; }
208 208 const block = data.blocks[index];
209 return !blockBlocksRangedAttacks(block?.typeId ?? null, block?.state ?? null);
209 return !blockBlocksRangedAttacks(block?.typeId ?? null, block?.state ?? null, block);
210 210 }
211 211
212 212 export function collectLightSources (chunkX, chunkY, data)
@@ -206,7 +206,7 @@ export function isChunkTileClearForRangedAttacks (data, localX, localY)
206 if (!isGroundPassable(ground.typeId))
206 if (!isGroundPassable(ground.typeId))
207 { return false; }
207 { return false; }
208 const block = data.blocks[index];
208 const block = data.blocks[index];
209 return !blockBlocksRangedAttacks(block?.typeId ?? null, block?.state ?? null);
209 return !blockBlocksRangedAttacks(block?.typeId ?? null, block?.state ?? null, block);
210}
210}
211
211
212export function collectLightSources (chunkX, chunkY, data)
212export function collectLightSources (chunkX, chunkY, data)
server/tests/doors.test.js +43 −11
@@ -18,7 +18,6 @@ import { Component } from "../src/ecs/components.js";
18 18 import { mobComponents } from "../src/simulation/spawners.js";
19 19 import {
20 20 entityCanOpenDoors,
21 holdDoorForPassage,
22 21 maybeOpenDoorOnApproach,
23 22 stepDoors
24 23 } from "../src/doors/service.js";
@@ -63,21 +62,34 @@ test("door states cover every wood facing and openness", () =>
63 62 assert.equal(composeDoorState("elder", "n", false), "elder-n-closed");
64 63 });
65 64
66 test("closed doors block passability unless opener; open doors do not", () =>
65 test("doors at resting block unless opener; passage means flipped from resting", () =>
67 66 {
68 67 assert.equal(isBlockPassable(BlockType.door, "pine-s-closed"), false);
69 assert.equal(isBlockPassable(BlockType.door, "pine-s-open"), true);
68 assert.equal(isBlockPassable(BlockType.door, "pine-s-open"), false);
70 69 assert.equal(isDoorTraversable({ state: "pine-s-closed" }, { canOpenDoors: false }), false);
71 70 assert.equal(isDoorTraversable({ state: "pine-s-closed" }, { canOpenDoors: true }), true);
72 assert.equal(isDoorTraversable({ state: "pine-s-open" }, { canOpenDoors: false }), 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);
73 76 });
74 77
75 test("closed doors block light and ranged LoS; open doors do not", () =>
78 test("light and ranged LoS follow passage-open (flipped from resting), not the open suffix", () =>
76 79 {
77 80 assert.equal(blockLightOcclusionForBlock({ typeId: BlockType.door, state: "pine-s-closed" }), LightOcclusion.solid);
78 assert.equal(blockLightOcclusionForBlock({ typeId: BlockType.door, state: "pine-s-open" }), LightOcclusion.clear);
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);
79 87 assert.equal(blockBlocksRangedAttacks(BlockType.door, "pine-s-closed"), true);
80 assert.equal(blockBlocksRangedAttacks(BlockType.door, "pine-s-open"), false);
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);
81 93 });
82 94
83 95 test("door sort sub-order puts N-closed behind and S-closed in front", () =>
@@ -127,8 +139,10 @@ test("players and humanoids can open doors; rats cannot unless already open", ()
127 139 assert.equal(worker.world.walkableFor(character.character_id, 9, 8), true);
128 140 assert.equal(worker.world.walkableFor(ratId, 9, 8), false);
129 141
142 /** Resting s-open (≈ W-closed) still blocks rats until flipped. */
130 143 placeDoor(worker, 9, 8, "pine-s-open");
131 assert.equal(worker.world.walkableFor(ratId, 9, 8), true);
144 assert.equal(worker.world.walkableFor(ratId, 9, 8), false);
145 assert.equal(worker.world.walkableFor(character.character_id, 9, 8), true);
132 146 }
133 147 finally
134 148 { harness.close(); }
@@ -199,19 +213,37 @@ test("player walking through a door flips it open via movement", () =>
199 213 { harness.close(); }
200 214 });
201 215
202 test("holdDoorForPassage remembers resting open without flipping again", () =>
216 test("resting open (≈ W-closed) flips to closed for passage, then restores", () =>
203 217 {
204 218 const harness = makeSimulation({ workerCount: 1 });
205 219 try
206 220 {
207 221 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
208 222 harness.simulation.spawnCharacter(1, character);
223 const ratId = harness.simulation.spawnEntity(mobComponents({
224 tileX: 11,
225 tileY: 8,
226 mobTypeId: MobId.rat
227 }));
209 228 harness.simulation.step();
210 229 const worker = workerOf(harness.simulation, character.character_id);
211 230 placeDoor(worker, 9, 8, "pine-s-open");
212 231
213 assert.equal(holdDoorForPassage(worker.world, 9, 8), null);
214 assert.ok(worker.world.heldDoors?.size > 0);
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);
215 247 assert.equal(doorAt(worker, 9, 8).state, "pine-s-open");
216 248 }
217 249 finally
@@ -18,7 +18,6 @@ import { Component } from "../src/ecs/components.js";
18import { mobComponents } from "../src/simulation/spawners.js";
18import { mobComponents } from "../src/simulation/spawners.js";
19import {
19import {
20 entityCanOpenDoors,
20 entityCanOpenDoors,
21 holdDoorForPassage,
22 maybeOpenDoorOnApproach,
21 maybeOpenDoorOnApproach,
23 stepDoors
22 stepDoors
24} from "../src/doors/service.js";
23} from "../src/doors/service.js";
@@ -63,21 +62,34 @@ test("door states cover every wood facing and openness", () =>
63 assert.equal(composeDoorState("elder", "n", false), "elder-n-closed");
62 assert.equal(composeDoorState("elder", "n", false), "elder-n-closed");
64});
63});
65
64
66test("closed doors block passability unless opener; open doors do not", () =>
65test("doors at resting block unless opener; passage means flipped from resting", () =>
67{
66{
68 assert.equal(isBlockPassable(BlockType.door, "pine-s-closed"), false);
67 assert.equal(isBlockPassable(BlockType.door, "pine-s-closed"), false);
69 assert.equal(isBlockPassable(BlockType.door, "pine-s-open"), true);
68 assert.equal(isBlockPassable(BlockType.door, "pine-s-open"), false);
70 assert.equal(isDoorTraversable({ state: "pine-s-closed" }, { canOpenDoors: false }), false);
69 assert.equal(isDoorTraversable({ state: "pine-s-closed" }, { canOpenDoors: false }), false);
71 assert.equal(isDoorTraversable({ state: "pine-s-closed" }, { canOpenDoors: true }), true);
70 assert.equal(isDoorTraversable({ state: "pine-s-closed" }, { canOpenDoors: true }), true);
72 assert.equal(isDoorTraversable({ state: "pine-s-open" }, { canOpenDoors: false }), 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);
73});
76});
74
77
75test("closed doors block light and ranged LoS; open doors do not", () =>
78test("light and ranged LoS follow passage-open (flipped from resting), not the open suffix", () =>
76{
79{
77 assert.equal(blockLightOcclusionForBlock({ typeId: BlockType.door, state: "pine-s-closed" }), LightOcclusion.solid);
80 assert.equal(blockLightOcclusionForBlock({ typeId: BlockType.door, state: "pine-s-closed" }), LightOcclusion.solid);
78 assert.equal(blockLightOcclusionForBlock({ typeId: BlockType.door, state: "pine-s-open" }), LightOcclusion.clear);
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);
79 assert.equal(blockBlocksRangedAttacks(BlockType.door, "pine-s-closed"), true);
87 assert.equal(blockBlocksRangedAttacks(BlockType.door, "pine-s-closed"), true);
80 assert.equal(blockBlocksRangedAttacks(BlockType.door, "pine-s-open"), false);
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);
81});
93});
82
94
83test("door sort sub-order puts N-closed behind and S-closed in front", () =>
95test("door sort sub-order puts N-closed behind and S-closed in front", () =>
@@ -127,8 +139,10 @@ test("players and humanoids can open doors; rats cannot unless already open", ()
127 assert.equal(worker.world.walkableFor(character.character_id, 9, 8), true);
139 assert.equal(worker.world.walkableFor(character.character_id, 9, 8), true);
128 assert.equal(worker.world.walkableFor(ratId, 9, 8), false);
140 assert.equal(worker.world.walkableFor(ratId, 9, 8), false);
129
141
142 /** Resting s-open (≈ W-closed) still blocks rats until flipped. */
130 placeDoor(worker, 9, 8, "pine-s-open");
143 placeDoor(worker, 9, 8, "pine-s-open");
131 assert.equal(worker.world.walkableFor(ratId, 9, 8), true);
144 assert.equal(worker.world.walkableFor(ratId, 9, 8), false);
145 assert.equal(worker.world.walkableFor(character.character_id, 9, 8), true);
132 }
146 }
133 finally
147 finally
134 { harness.close(); }
148 { harness.close(); }
@@ -199,19 +213,37 @@ test("player walking through a door flips it open via movement", () =>
199 { harness.close(); }
213 { harness.close(); }
200});
214});
201
215
202test("holdDoorForPassage remembers resting open without flipping again", () =>
216test("resting open (≈ W-closed) flips to closed for passage, then restores", () =>
203{
217{
204 const harness = makeSimulation({ workerCount: 1 });
218 const harness = makeSimulation({ workerCount: 1 });
205 try
219 try
206 {
220 {
207 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
221 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
208 harness.simulation.spawnCharacter(1, character);
222 harness.simulation.spawnCharacter(1, character);
223 const ratId = harness.simulation.spawnEntity(mobComponents({
224 tileX: 11,
225 tileY: 8,
226 mobTypeId: MobId.rat
227 }));
209 harness.simulation.step();
228 harness.simulation.step();
210 const worker = workerOf(harness.simulation, character.character_id);
229 const worker = workerOf(harness.simulation, character.character_id);
211 placeDoor(worker, 9, 8, "pine-s-open");
230 placeDoor(worker, 9, 8, "pine-s-open");
212
231
213 assert.equal(holdDoorForPassage(worker.world, 9, 8), null);
232 assert.equal(worker.world.walkableFor(ratId, 9, 8), false);
214 assert.ok(worker.world.heldDoors?.size > 0);
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);
215 assert.equal(doorAt(worker, 9, 8).state, "pine-s-open");
247 assert.equal(doorAt(worker, 9, 8).state, "pine-s-open");
216 }
248 }
217 finally
249 finally
shared/definitions/tiles.js +15 −7
@@ -32,7 +32,7 @@ import
32 32 import {
33 33 composeDoorState,
34 34 doorPaletteDisplayName,
35 isDoorOpenState
35 isDoorPassageOpen
36 36 } from "../world/doors.js";
37 37
38 38 export const GroundType = Object.freeze({
@@ -734,10 +734,10 @@ export const BLOCK_DEFINITIONS = Object.freeze({
734 734 id: BlockType.door,
735 735 key: "door",
736 736 displayName: "Door",
737 /** Closed by default; open state is passable (see isBlockPassable). */
737 /** Resting by default; passage when flipped from resting (see isDoorPassageOpen). */
738 738 passable: false,
739 739 /**
740 * Light/LoS follow open vs closed via {@link blockLightOcclusionForBlock} /
740 * Light/LoS follow passage-open via {@link blockLightOcclusionForBlock} /
741 741 * {@link blockBlocksRangedAttacks} — not this static flag.
742 742 */
743 743 blocksLight: true,
@@ -1070,7 +1070,10 @@ export function isBlockPassable (typeId, state = null)
1070 1070 if (!def)
1071 1071 { return false; }
1072 1072 if (def.door)
1073 { return isDoorOpenState(state); }
1073 {
1074 /** Without config, authored state is resting → not yet held for passage. */
1075 return isDoorPassageOpen({ typeId, state, config: null });
1076 }
1074 1077 return def.passable;
1075 1078 }
1076 1079
@@ -1079,7 +1082,7 @@ export function isBlockPassable (typeId, state = null)
1079 1082 * `blocksRangedAttacks`: explicit override; `craftingStationId` defaults to clear;
1080 1083 * otherwise impassable blocks block and passable blocks do not.
1081 1084 */
1082 export function blockBlocksRangedAttacks (typeId, state = null)
1085 export function blockBlocksRangedAttacks (typeId, state = null, block = null)
1083 1086 {
1084 1087 if (typeId == null)
1085 1088 { return false; }
@@ -1087,7 +1090,12 @@ export function blockBlocksRangedAttacks (typeId, state = null)
1087 1090 if (!def)
1088 1091 { return true; }
1089 1092 if (def.door)
1090 { return !isDoorOpenState(state); }
1093 {
1094 const entry = block && typeof block === "object"
1095 ? block
1096 : { typeId, state, config: null };
1097 return !isDoorPassageOpen(entry);
1098 }
1091 1099 if (def.blocksRangedAttacks === false)
1092 1100 { return false; }
1093 1101 if (def.blocksRangedAttacks === true)
@@ -1121,7 +1129,7 @@ export function blockLightOcclusionForBlock (block)
1121 1129 { return LightOcclusion.clear; }
1122 1130 const def = blockDefinition(block.typeId ?? null);
1123 1131 if (def?.door)
1124 { return isDoorOpenState(block.state) ? LightOcclusion.clear : LightOcclusion.solid; }
1132 { return isDoorPassageOpen(block) ? LightOcclusion.clear : LightOcclusion.solid; }
1125 1133 return blockLightOcclusion(block.typeId ?? null);
1126 1134 }
1127 1135
@@ -32,7 +32,7 @@ import
32import {
32import {
33 composeDoorState,
33 composeDoorState,
34 doorPaletteDisplayName,
34 doorPaletteDisplayName,
35 isDoorOpenState
35 isDoorPassageOpen
36} from "../world/doors.js";
36} from "../world/doors.js";
37
37
38export const GroundType = Object.freeze({
38export const GroundType = Object.freeze({
@@ -734,10 +734,10 @@ export const BLOCK_DEFINITIONS = Object.freeze({
734 id: BlockType.door,
734 id: BlockType.door,
735 key: "door",
735 key: "door",
736 displayName: "Door",
736 displayName: "Door",
737 /** Closed by default; open state is passable (see isBlockPassable). */
737 /** Resting by default; passage when flipped from resting (see isDoorPassageOpen). */
738 passable: false,
738 passable: false,
739 /**
739 /**
740 * Light/LoS follow open vs closed via {@link blockLightOcclusionForBlock} /
740 * Light/LoS follow passage-open via {@link blockLightOcclusionForBlock} /
741 * {@link blockBlocksRangedAttacks} — not this static flag.
741 * {@link blockBlocksRangedAttacks} — not this static flag.
742 */
742 */
743 blocksLight: true,
743 blocksLight: true,
@@ -1070,7 +1070,10 @@ export function isBlockPassable (typeId, state = null)
1070 if (!def)
1070 if (!def)
1071 { return false; }
1071 { return false; }
1072 if (def.door)
1072 if (def.door)
1073 { return isDoorOpenState(state); }
1073 {
1074 /** Without config, authored state is resting → not yet held for passage. */
1075 return isDoorPassageOpen({ typeId, state, config: null });
1076 }
1074 return def.passable;
1077 return def.passable;
1075}
1078}
1076
1079
@@ -1079,7 +1082,7 @@ export function isBlockPassable (typeId, state = null)
1079 * `blocksRangedAttacks`: explicit override; `craftingStationId` defaults to clear;
1082 * `blocksRangedAttacks`: explicit override; `craftingStationId` defaults to clear;
1080 * otherwise impassable blocks block and passable blocks do not.
1083 * otherwise impassable blocks block and passable blocks do not.
1081 */
1084 */
1082export function blockBlocksRangedAttacks (typeId, state = null)
1085export function blockBlocksRangedAttacks (typeId, state = null, block = null)
1083{
1086{
1084 if (typeId == null)
1087 if (typeId == null)
1085 { return false; }
1088 { return false; }
@@ -1087,7 +1090,12 @@ export function blockBlocksRangedAttacks (typeId, state = null)
1087 if (!def)
1090 if (!def)
1088 { return true; }
1091 { return true; }
1089 if (def.door)
1092 if (def.door)
1090 { return !isDoorOpenState(state); }
1093 {
1094 const entry = block && typeof block === "object"
1095 ? block
1096 : { typeId, state, config: null };
1097 return !isDoorPassageOpen(entry);
1098 }
1091 if (def.blocksRangedAttacks === false)
1099 if (def.blocksRangedAttacks === false)
1092 { return false; }
1100 { return false; }
1093 if (def.blocksRangedAttacks === true)
1101 if (def.blocksRangedAttacks === true)
@@ -1121,7 +1129,7 @@ export function blockLightOcclusionForBlock (block)
1121 { return LightOcclusion.clear; }
1129 { return LightOcclusion.clear; }
1122 const def = blockDefinition(block.typeId ?? null);
1130 const def = blockDefinition(block.typeId ?? null);
1123 if (def?.door)
1131 if (def?.door)
1124 { return isDoorOpenState(block.state) ? LightOcclusion.clear : LightOcclusion.solid; }
1132 { return isDoorPassageOpen(block) ? LightOcclusion.clear : LightOcclusion.solid; }
1125 return blockLightOcclusion(block.typeId ?? null);
1133 return blockLightOcclusion(block.typeId ?? null);
1126}
1134}
1127
1135
shared/world/doors.js +26 −4
@@ -69,6 +69,28 @@ export function flipDoorState (state)
69 69 return composeDoorState(parsed.wood, parsed.facing, !parsed.open);
70 70 }
71 71
72 /**
73 * Authored resting state (world placement). Passage "open" is the flip of this,
74 * not the open/closed suffix — e.g. resting `s-open` (≈ W-closed) opens to `s-closed`.
75 */
76 export function doorRestingState (block)
77 {
78 const resting = block?.config?.restingState;
79 if (parseDoorState(resting))
80 { return resting; }
81 return block?.state ?? null;
82 }
83
84 /** True when the live state is flipped away from resting (someone held it for passage). */
85 export function isDoorPassageOpen (block)
86 {
87 const current = block?.state;
88 const resting = doorRestingState(block);
89 if (!parseDoorState(current) || !parseDoorState(resting))
90 { return false; }
91 return current !== resting;
92 }
93
72 94 export function isDoorOpenState (state)
73 95 {
74 96 return !!parseDoorState(state)?.open;
@@ -78,7 +100,7 @@ export function isDoorOpenState (state)
78 100 * Y-sort sub-order within the door's tile (see `ySortLayer`).
79 101 * N-closed draws behind blocks/entities; S-closed in front of them;
80 102 * still shares the tile's sortY so the row above stays behind.
81 * Open doors use the default entity sub-order.
103 * Visual open sprites use the default entity sub-order.
82 104 */
83 105 export function doorSortSubOrder (state, defaultSubOrder = 3)
84 106 {
@@ -91,12 +113,12 @@ export function doorSortSubOrder (state, defaultSubOrder = 3)
91 113 }
92 114
93 115 /**
94 * Passability for a door block. Closed doors are walkable only when the mover
95 * can open them (they auto-open on approach).
116 * Passability: traversable when held open for passage (flipped from resting),
117 * or when the mover can open doors (they flip on approach).
96 118 */
97 119 export function isDoorTraversable (block, { canOpenDoors = false } = {})
98 120 {
99 if (isDoorOpenState(block?.state))
121 if (isDoorPassageOpen(block))
100 122 { return true; }
101 123 return !!canOpenDoors;
102 124 }
@@ -69,6 +69,28 @@ export function flipDoorState (state)
69 return composeDoorState(parsed.wood, parsed.facing, !parsed.open);
69 return composeDoorState(parsed.wood, parsed.facing, !parsed.open);
70}
70}
71
71
72/**
73 * Authored resting state (world placement). Passage "open" is the flip of this,
74 * not the open/closed suffix — e.g. resting `s-open` (≈ W-closed) opens to `s-closed`.
75 */
76export function doorRestingState (block)
77{
78 const resting = block?.config?.restingState;
79 if (parseDoorState(resting))
80 { return resting; }
81 return block?.state ?? null;
82}
83
84/** True when the live state is flipped away from resting (someone held it for passage). */
85export function isDoorPassageOpen (block)
86{
87 const current = block?.state;
88 const resting = doorRestingState(block);
89 if (!parseDoorState(current) || !parseDoorState(resting))
90 { return false; }
91 return current !== resting;
92}
93
72export function isDoorOpenState (state)
94export function isDoorOpenState (state)
73{
95{
74 return !!parseDoorState(state)?.open;
96 return !!parseDoorState(state)?.open;
@@ -78,7 +100,7 @@ export function isDoorOpenState (state)
78 * Y-sort sub-order within the door's tile (see `ySortLayer`).
100 * Y-sort sub-order within the door's tile (see `ySortLayer`).
79 * N-closed draws behind blocks/entities; S-closed in front of them;
101 * N-closed draws behind blocks/entities; S-closed in front of them;
80 * still shares the tile's sortY so the row above stays behind.
102 * still shares the tile's sortY so the row above stays behind.
81 * Open doors use the default entity sub-order.
103 * Visual open sprites use the default entity sub-order.
82 */
104 */
83export function doorSortSubOrder (state, defaultSubOrder = 3)
105export function doorSortSubOrder (state, defaultSubOrder = 3)
84{
106{
@@ -91,12 +113,12 @@ export function doorSortSubOrder (state, defaultSubOrder = 3)
91}
113}
92
114
93/**
115/**
94 * Passability for a door block. Closed doors are walkable only when the mover
116 * Passability: traversable when held open for passage (flipped from resting),
95 * can open them (they auto-open on approach).
117 * or when the mover can open doors (they flip on approach).
96 */
118 */
97export function isDoorTraversable (block, { canOpenDoors = false } = {})
119export function isDoorTraversable (block, { canOpenDoors = false } = {})
98{
120{
99 if (isDoorOpenState(block?.state))
121 if (isDoorPassageOpen(block))
100 { return true; }
122 { return true; }
101 return !!canOpenDoors;
123 return !!canOpenDoors;
102}
124}