@greenborn/mapa-voxel-back 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,299 @@
1
+ // This module contains routines that determine how rooms within a parcel
2
+ // are connected to one another. Originally they lived in
3
+ // generadorCasa.js but have been moved here for clarity.
4
+
5
+ import { computeDoorSpan, stairKeepOutRange, sameLevel } from '../mapType3Utils.js';
6
+
7
+ // -------------------------------------------------------------------------
8
+ // apertura de una puerta entre dos salas adyacentes
9
+ // -------------------------------------------------------------------------
10
+
11
+ /**
12
+ * Abre una puerta entre dos salas que comparten pared y marca los flags
13
+ * `open*`/`open*Range` en las dos. Las cuatro reglas de conexión de este
14
+ * módulo hacían el mismo cálculo duplicado; ahora pasan todas por aquí, que
15
+ * además esquiva el tramo que ocupa una escalera (`stairKeepOut`) y descarta
16
+ * las salas de otra planta, que se superponen en el plano pero no comparten
17
+ * ninguna pared.
18
+ *
19
+ * @returns {boolean} true si quedó abierta una puerta
20
+ */
21
+ export function tryLinkRooms(s, t) {
22
+ if (!sameLevel(s, t)) return false;
23
+
24
+ const sx0 = s.x, sx1 = s.x + s.width;
25
+ const sz0 = s.z, sz1 = s.z + s.depth;
26
+ const tx0 = t.x, tx1 = t.x + t.width;
27
+ const tz0 = t.z, tz1 = t.z + t.depth;
28
+
29
+ // pared vertical (adyacencia en X): la puerta corre a lo largo de Z
30
+ if (sx1 === tx0 || tx1 === sx0) {
31
+ const sWall = (sx1 === tx0) ? sx1 - 1 : sx0;
32
+ const tWall = (sx1 === tx0) ? tx0 : tx1 - 1;
33
+ const door = computeDoorSpan(Math.max(sz0, tz0), Math.min(sz1, tz1), [
34
+ stairKeepOutRange(s, 'z', sWall),
35
+ stairKeepOutRange(t, 'z', tWall)
36
+ ]);
37
+ if (door) {
38
+ const range = { start: door.start, end: door.start + door.width };
39
+ if (sx1 === tx0) {
40
+ s.openEast = true;
41
+ s.openEastRange = range;
42
+ t.openWest = true;
43
+ t.openWestRange = Object.assign({}, range);
44
+ } else {
45
+ s.openWest = true;
46
+ s.openWestRange = range;
47
+ t.openEast = true;
48
+ t.openEastRange = Object.assign({}, range);
49
+ }
50
+ return true;
51
+ }
52
+ return false;
53
+ }
54
+
55
+ // pared horizontal (adyacencia en Z): la puerta corre a lo largo de X
56
+ if (sz1 === tz0 || tz1 === sz0) {
57
+ const sWall = (sz1 === tz0) ? sz1 - 1 : sz0;
58
+ const tWall = (sz1 === tz0) ? tz0 : tz1 - 1;
59
+ const door = computeDoorSpan(Math.max(sx0, tx0), Math.min(sx1, tx1), [
60
+ stairKeepOutRange(s, 'x', sWall),
61
+ stairKeepOutRange(t, 'x', tWall)
62
+ ]);
63
+ if (door) {
64
+ const range = { start: door.start, end: door.start + door.width };
65
+ if (sz1 === tz0) {
66
+ s.openSouth = true;
67
+ s.openSouthRange = range;
68
+ t.openNorth = true;
69
+ t.openNorthRange = Object.assign({}, range);
70
+ } else {
71
+ s.openNorth = true;
72
+ s.openNorthRange = range;
73
+ t.openSouth = true;
74
+ t.openSouthRange = Object.assign({}, range);
75
+ }
76
+ return true;
77
+ }
78
+ return false;
79
+ }
80
+
81
+ return false;
82
+ }
83
+
84
+ // -------------------------------------------------------------------------
85
+ // conexión entre habitaciones y pasillos
86
+ // -------------------------------------------------------------------------
87
+
88
+ export function connectRooms(structures) {
89
+ const len = structures.length;
90
+ for (let i = 0; i < len; i++) {
91
+ const s = structures[i];
92
+ if (s.type !== 'hallway' || !s.parcelId) continue;
93
+ for (let j = i + 1; j < len; j++) {
94
+ const t = structures[j];
95
+ if (t.type !== 'hallway' || s.parcelId !== t.parcelId) continue;
96
+ tryLinkRooms(s, t);
97
+ }
98
+ }
99
+ }
100
+
101
+ // -------------------------------------------------------------------------
102
+ // conexiones adicionales entre habitaciones principales y pasillos
103
+ // -------------------------------------------------------------------------
104
+
105
+ export function connectSpecialRoomsToHallways(structures) {
106
+ const special = new Set(['cocina', 'comedor', 'living']);
107
+ const len = structures.length;
108
+ for (let i = 0; i < len; i++) {
109
+ const s = structures[i];
110
+ if (!special.has(s.type) || !s.parcelId) continue;
111
+ for (let j = 0; j < len; j++) {
112
+ if (i === j) continue;
113
+ const t = structures[j];
114
+ if (t.type !== 'hallway' || s.parcelId !== t.parcelId) continue;
115
+ tryLinkRooms(s, t);
116
+ }
117
+ }
118
+ }
119
+
120
+ // -------------------------------------------------------------------------
121
+ // conexiones de baños
122
+ // -------------------------------------------------------------------------
123
+
124
+ export function connectBathrooms(structures) {
125
+ const len = structures.length;
126
+ for (let i = 0; i < len; i++) {
127
+ const s = structures[i];
128
+ if (s.type !== 'banio' || !s.parcelId) continue;
129
+ if (s.openNorth || s.openSouth || s.openEast || s.openWest) continue;
130
+
131
+ // classify structures by priority
132
+ const halls = [];
133
+ const others = [];
134
+ let mainRoom = null;
135
+ for (let j = 0; j < len; j++) {
136
+ const t = structures[j];
137
+ if (t.parcelId !== s.parcelId || t === s) continue;
138
+ if (!sameLevel(s, t)) continue;
139
+ if (t.type === 'hallway') {
140
+ halls.push(t);
141
+ } else if (t.type !== 'banio') {
142
+ // collect every non-bath non-hall room for later
143
+ others.push(t);
144
+ if (!mainRoom && (t.frontNorth || t.frontSouth || t.frontEast || t.frontWest)) {
145
+ mainRoom = t;
146
+ }
147
+ }
148
+ }
149
+
150
+ const tryList = (list) => {
151
+ for (const t of list) {
152
+ if (tryLinkRooms(s, t)) return true;
153
+ }
154
+ return false;
155
+ };
156
+
157
+ // attempt each priority in turn
158
+ if (tryList(halls)) continue;
159
+ if (mainRoom && tryLinkRooms(s, mainRoom)) continue;
160
+ tryList(others);
161
+ }
162
+ }
163
+
164
+ // -------------------------------------------------------------------------
165
+ // conexiones de dormitorios y de la caja de escalera
166
+ // -------------------------------------------------------------------------
167
+
168
+ // salas con un único acceso, por prioridad pasillo > sala principal > otra.
169
+ // La caja de escalera entra acá porque le alcanza una puerta por planta y
170
+ // porque la cadena de respaldo garantiza que la tenga: si se quedara sin
171
+ // ninguna, las plantas altas quedarían incomunicadas.
172
+ const SINGLE_ACCESS_TYPES = new Set(['dormitorio', 'escalera']);
173
+
174
+ export function connectBedrooms(structures) {
175
+ const len = structures.length;
176
+ for (let i = 0; i < len; i++) {
177
+ const s = structures[i];
178
+ if (!SINGLE_ACCESS_TYPES.has(s.type) || !s.parcelId) continue;
179
+ if (s.openNorth || s.openSouth || s.openEast || s.openWest) continue;
180
+
181
+ const halls = [];
182
+ const others = [];
183
+ let mainRoom = null;
184
+ for (let j = 0; j < len; j++) {
185
+ const t = structures[j];
186
+ if (t.parcelId !== s.parcelId || t === s) continue;
187
+ if (!sameLevel(s, t)) continue;
188
+ if (t.type === 'hallway') {
189
+ halls.push(t);
190
+ } else {
191
+ others.push(t);
192
+ if (!mainRoom && (t.frontNorth || t.frontSouth || t.frontEast || t.frontWest)) {
193
+ mainRoom = t;
194
+ }
195
+ }
196
+ }
197
+
198
+ const abierto = () => s.openNorth || s.openSouth || s.openEast || s.openWest;
199
+ for (const h of halls) {
200
+ if (tryLinkRooms(s, h) && abierto()) break;
201
+ }
202
+ if (abierto()) continue;
203
+ if (mainRoom && tryLinkRooms(s, mainRoom)) continue;
204
+
205
+ // Último recurso: hay distribuciones en las que el dormitorio no toca
206
+ // ni un pasillo ni la sala principal (queda detrás del baño, por
207
+ // ejemplo). Antes se quedaba sin ninguna puerta. Se prueba contra las
208
+ // salas adyacentes que ya tengan acceso, para no abrir una puerta a un
209
+ // cuarto que también está incomunicado; el baño va al final para no
210
+ // convertir el paso obligado en "dormitorio -> baño" si hay otra opción.
211
+ const conectadas = others.filter(t =>
212
+ t.openNorth || t.openSouth || t.openEast || t.openWest
213
+ );
214
+ const preferidas = conectadas.filter(t => t.type !== 'banio')
215
+ .concat(conectadas.filter(t => t.type === 'banio'));
216
+ for (const t of preferidas) {
217
+ if (tryLinkRooms(s, t)) break;
218
+ }
219
+ if (abierto()) continue;
220
+ for (const t of others) {
221
+ if (tryLinkRooms(s, t)) break;
222
+ }
223
+ }
224
+ }
225
+
226
+ // -------------------------------------------------------------------------
227
+ // puerta hacia el patio trasero (funciones auxiliares)
228
+ // -------------------------------------------------------------------------
229
+
230
+ export function connectBackyardDoor(structures, orientation, parcelId) {
231
+ const priorities = ['comedor', 'living', 'cocina'];
232
+
233
+ const score = (room) => {
234
+ switch (orientation) {
235
+ case 'N': return room.z + room.depth; // más al sur
236
+ case 'S': return -room.z; // más al norte
237
+ case 'E': return -room.x; // más al oeste
238
+ case 'O': return room.x + room.width; // más al este
239
+ }
240
+ return 0;
241
+ };
242
+
243
+ for (const type of priorities) {
244
+ const candidates = structures.filter(
245
+ s => s.parcelId === parcelId && s.type === type
246
+ );
247
+ if (candidates.length === 0) continue;
248
+ let best = candidates[0];
249
+ let bestScore = score(best);
250
+ for (let k = 1; k < candidates.length; k++) {
251
+ const sc = score(candidates[k]);
252
+ if (sc > bestScore) {
253
+ bestScore = sc;
254
+ best = candidates[k];
255
+ }
256
+ }
257
+ markRearDoor(best, orientation);
258
+ break;
259
+ }
260
+ }
261
+
262
+ function markRearDoor(room, orientation) {
263
+ // always attempt a 4‑block wide opening; the volume generator will
264
+ // clip it if the room is smaller, but our configured room sizes are
265
+ // large enough that this is never an issue.
266
+ const doorWidth = 4;
267
+ let start;
268
+ switch (orientation) {
269
+ case 'N':
270
+ start = room.x + Math.floor((room.width - doorWidth) / 2);
271
+ room.openSouth = true;
272
+ room.rearDoorSide = 'South';
273
+ // do not provide openSouthRange; leaving the range undefined
274
+ // forces mapType3 to use the default doorWidth, which is always
275
+ // 4. if we gave a range of length 4 the `computeDoor` helper
276
+ // would shrink it to 2 (see issue). the generator already
277
+ // centres the door using its own calculations when no range is
278
+ // present.
279
+ break;
280
+ case 'S':
281
+ start = room.x + Math.floor((room.width - doorWidth) / 2);
282
+ room.openNorth = true;
283
+ room.rearDoorSide = 'North';
284
+ break;
285
+ case 'E':
286
+ start = room.z + Math.floor((room.depth - doorWidth) / 2);
287
+ room.openWest = true;
288
+ room.rearDoorSide = 'West';
289
+ break;
290
+ case 'O':
291
+ start = room.z + Math.floor((room.depth - doorWidth) / 2);
292
+ room.openEast = true;
293
+ room.rearDoorSide = 'East';
294
+ break;
295
+ }
296
+ // door height is always 6; record for clarity (mapType3 already uses
297
+ // hy+6 internally).
298
+ room.doorHeight = 6;
299
+ }