BlueGit

timfa questward

Files

questward / server / src / doors / service.js

service.js

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