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.
@@ -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.j3
import { BlockType } from "../../../shared/definitions/tiles.js";3
import { BlockType } from "../../../shared/definitions/tiles.js";4
import {4
import {5
flipDoorState,5
flipDoorState,6
isDoorOpenState,6
isDoorPassageOpen,7
doorRestingState,7
parseDoorState8
parseDoorState8
} from "../../../shared/world/doors.js";9
} from "../../../shared/world/doors.js";9
import { tileToChunk, tileToLocal, tileKey, parseTileKey } from "../world/coordinates.js";10
import { tileToChunk, tileToLocal, tileKey, parseTileKey } from "../world/coordinates.js";@@ -91,8 +92,8 @@ function writeDoorState (world, tileX, tileY, state, config, dimensionId = OVERW91
}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 entity95
* Resting authored state is remembered until the vicinity clears.96
* can pass. Resting is remembered until the vicinity clears.96
*/97
*/97
export function holdDoorForPassage (world, tileX, tileY, dimensionId = OVERWORLD_ID)98
export function holdDoorForPassage (world, tileX, tileY, dimensionId = OVERWORLD_ID)98
{99
{@@ -105,18 +106,18 @@ export function holdDoorForPassage (world, tileX, tileY, dimensionId = OVERWORLD105
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)?.restingState108
let restingState = held.get(key)?.restingState108
?? block.config?.restingState109
?? 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
*/197
export function maybeOpenDoorOnApproach (world, entityId, nextTileX, nextTileY)198
export 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
}@@ -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
212
export function collectLightSources (chunkX, chunkY, data)212
export function collectLightSources (chunkX, chunkY, data)@@ -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";18
import { mobComponents } from "../src/simulation/spawners.js";18
import { mobComponents } from "../src/simulation/spawners.js";19
import {19
import {20
entityCanOpenDoors,20
entityCanOpenDoors,21
holdDoorForPassage,22
maybeOpenDoorOnApproach,21
maybeOpenDoorOnApproach,23
stepDoors22
stepDoors24
} 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
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
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
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
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
83
test("door sort sub-order puts N-closed behind and S-closed in front", () =>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
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
finally147
finally134
{ 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
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
const harness = makeSimulation({ workerCount: 1 });218
const harness = makeSimulation({ workerCount: 1 });205
try219
try206
{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.rat227
}));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
finally249
finally