BlueGit

timfa questward

Files

questward / server / src / doors / service.js

service.js

service.js 6.4 KiB Download Raw
1import { Component } from "../ecs/components.js";
2import { mobDefinition, isHumanoidMob } from "../../../shared/definitions/mobs.js";
3import { BlockType } from "../../../shared/definitions/tiles.js";
4import {
5 flipDoorState,
6 isDoorPassageOpen,
7 doorRestingState,
8 parseDoorState
9} from "../../../shared/world/doors.js";
10import { tileToChunk, tileToLocal, tileKey, parseTileKey } from "../world/coordinates.js";
11import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";
12import { OVERWORLD_ID } from "../../../shared/world/dimensions.js";
13import { tileLightOcclusionAt } from "../../../shared/lighting/chunkOcclusion.js";
14import { entityDoorRequirementsAllow } from "./conditionContext.js";
15
16export function isDoorTypeId (typeId)
17{
18 return (typeId | 0) === BlockType.door;
19}
20
21export function isDoorBlock (block)
22{
23 return isDoorTypeId(block?.typeId);
24}
25
26/**
27 * Players always open doors; humanoid bodies by default;
28 * set `canOpenDoors: true|false` on a mob def to override.
29 */
30export function entityCanOpenDoors (ecs, entityId)
31{
32 const handle = ecs.handleOf(entityId);
33 if (handle == null)
34 { return false; }
35 if (ecs.get(handle, Component.character))
36 { return true; }
37 const creature = ecs.get(handle, Component.creature);
38 if (!creature)
39 { return false; }
40 const def = mobDefinition(creature.mobTypeId);
41 if (!def)
42 { return false; }
43 if (def.canOpenDoors === true)
44 { return true; }
45 if (def.canOpenDoors === false)
46 { return false; }
47 return isHumanoidMob(creature.mobTypeId) || !!def.usePlayerBody;
48}
49
50function ensureHeldDoors (world)
51{
52 if (!world.heldDoors)
53 { world.heldDoors = new Map(); }
54 return world.heldDoors;
55}
56
57function readBlock (world, tileX, tileY, dimensionId = OVERWORLD_ID)
58{
59 const data = world.chunkData?.(tileToChunk(tileX), tileToChunk(tileY), dimensionId);
60 return data?.blocks?.[tileToLocal(tileY) * CHUNK_SIZE + tileToLocal(tileX)] ?? null;
61}
62
63function writeDoorState (world, tileX, tileY, state, config, dimensionId = OVERWORLD_ID)
64{
65 const chunkX = tileToChunk(tileX);
66 const chunkY = tileToChunk(tileY);
67 const localX = tileToLocal(tileX);
68 const localY = tileToLocal(tileY);
69 const data = world.chunkData?.(chunkX, chunkY, dimensionId);
70 const index = localY * CHUNK_SIZE + localX;
71 const prev = data?.blocks?.[index];
72 if (!prev || !isDoorTypeId(prev.typeId))
73 { return null; }
74 const block = {
75 typeId: BlockType.door,
76 state,
77 config: config ?? prev.config ?? null
78 };
79 data.blocks[index] = block;
80 if (Array.isArray(data.occlusion) && data.occlusion.length > index)
81 { data.occlusion[index] = tileLightOcclusionAt(data.grounds, data.blocks, localX, localY); }
82 return {
83 type: "chunk-delta",
84 chunkX,
85 chunkY,
86 tileX,
87 tileY,
88 localX,
89 localY,
90 dimensionId,
91 block
92 };
93}
94
95/**
96 * Flip the door away from its resting (authored) state so a canOpenDoors entity
97 * can pass. Resting is remembered until the vicinity clears.
98 */
99export function holdDoorForPassage (world, tileX, tileY, dimensionId = OVERWORLD_ID)
100{
101 const block = readBlock(world, tileX, tileY, dimensionId);
102 if (!isDoorBlock(block))
103 { return null; }
104 const parsed = parseDoorState(block.state);
105 if (!parsed)
106 { return null; }
107 const held = ensureHeldDoors(world);
108 const key = tileKey(tileX, tileY, dimensionId);
109 let restingState = held.get(key)?.restingState
110 ?? doorRestingState(block);
111 if (!parseDoorState(restingState))
112 { restingState = block.state; }
113
114 if (!held.has(key))
115 { held.set(key, { restingState, dimensionId }); }
116
117 /** Already flipped for passage — keep held, no visual change. */
118 if (block.state !== restingState)
119 { return null; }
120
121 const next = flipDoorState(restingState);
122 if (!next || next === block.state)
123 { return null; }
124 const config = {
125 ...(block.config && typeof block.config === "object" ? block.config : {}),
126 restingState
127 // Reserved for later: keyItemId, unlockNpcId, tollCoins, forcedPassage
128 };
129 return writeDoorState(world, tileX, tileY, next, config, dimensionId);
130}
131
132function chebyshev (ax, ay, bx, by)
133{
134 return Math.max(Math.abs(ax - bx), Math.abs(ay - by));
135}
136
137/** Any living body on the door or Chebyshev-adjacent keeps the held state. */
138function doorVicinityOccupied (world, tileX, tileY, dimensionId = OVERWORLD_ID)
139{
140 for (const entity of world.ecs.query(Component.position))
141 {
142 if (world.ecs.has(entity.handle, Component.dead))
143 { continue; }
144 const pos = entity.position;
145 const posDim = pos.dimensionId ?? OVERWORLD_ID;
146 if (posDim !== dimensionId)
147 { continue; }
148 if (chebyshev(pos.tileX, pos.tileY, tileX, tileY) <= 1)
149 { return true; }
150 }
151 return false;
152}
153
154/** Restore doors whose holders left the tile and its neighbours. */
155export function stepDoors (world)
156{
157 const held = world.heldDoors;
158 if (!held?.size)
159 { return []; }
160 const events = [];
161 for (const [key, entry] of [...held.entries()])
162 {
163 const parsed = parseTileKey(key);
164 const tileX = parsed.tileX;
165 const tileY = parsed.tileY;
166 const dimensionId = entry.dimensionId ?? parsed.dimensionId ?? OVERWORLD_ID;
167 if (!Number.isFinite(tileX) || !Number.isFinite(tileY))
168 {
169 held.delete(key);
170 continue;
171 }
172 if (doorVicinityOccupied(world, tileX, tileY, dimensionId))
173 { continue; }
174 const block = readBlock(world, tileX, tileY, dimensionId);
175 if (!isDoorBlock(block))
176 {
177 held.delete(key);
178 continue;
179 }
180 const resting = entry.restingState ?? doorRestingState(block) ?? block.state;
181 if (block.state !== resting)
182 {
183 const config = {
184 ...(block.config && typeof block.config === "object" ? block.config : {}),
185 restingState: resting
186 };
187 const delta = writeDoorState(world, tileX, tileY, resting, config, dimensionId);
188 if (delta)
189 { events.push(delta); }
190 }
191 held.delete(key);
192 }
193 return events;
194}
195
196/**
197 * If the next path tile is a door still at resting, flip it for passage.
198 */
199export function maybeOpenDoorOnApproach (world, entityId, nextTileX, nextTileY)
200{
201 if (!entityCanOpenDoors(world.ecs, entityId))
202 { return null; }
203 if (!entityDoorRequirementsAllow(world, entityId, nextTileX, nextTileY))
204 { return null; }
205 const handle = world.ecs.handleOf(entityId);
206 const position = handle != null ? world.ecs.get(handle, Component.position) : null;
207 const dimensionId = position?.dimensionId ?? OVERWORLD_ID;
208 const block = readBlock(world, nextTileX, nextTileY, dimensionId);
209 if (!isDoorBlock(block) || isDoorPassageOpen(block))
210 { return null; }
211 return holdDoorForPassage(world, nextTileX, nextTileY, dimensionId);
212}
213
214export function doorBlocksRangedAttacks (block)
215{
216 if (!isDoorBlock(block))
217 { return null; }
218 return !isDoorPassageOpen(block);
219}