BlueGit

timfa questward

Files

questward / server / tests / doors.test.js

doors.test.js

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