@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.
- package/LICENSE +21 -0
- package/README.md +107 -2
- package/package.json +36 -4
- package/src/formato/chunk_compacto.js +263 -0
- package/src/generador/cartel_tienda.js +118 -0
- package/src/generador/construirMapa.js +447 -0
- package/src/generador/excavar_refugios.js +32 -0
- package/src/generador/genera_casa/bathroom_impl.js +87 -0
- package/src/generador/genera_casa/bathrooms_insert.js +104 -0
- package/src/generador/genera_casa/generadorCasa.js +470 -0
- package/src/generador/genera_casa/generadorCasaConexiones.js +299 -0
- package/src/generador/genera_casa/generadorCasaPisos.js +816 -0
- package/src/generador/genera_casa/generadorCasaVentanas.js +361 -0
- package/src/generador/genera_casa/ubicacionSalaCasa.js +486 -0
- package/src/generador/genera_nidos.js +74 -0
- package/src/generador/genera_refugios.js +301 -0
- package/src/generador/genera_tiendas.js +200 -0
- package/src/generador/mapConfig.js +549 -0
- package/src/generador/mapType3.js +635 -0
- package/src/generador/mapType3Block.js +476 -0
- package/src/generador/mapType3Center.js +137 -0
- package/src/generador/mapType3Parcel.js +63 -0
- package/src/generador/mapType3Utils.js +299 -0
- package/src/generador/volumeBuffer.js +210 -0
- package/src/index.js +23 -0
- package/src/plugins/datos.js +111 -0
- package/src/plugins/registro.js +48 -0
|
@@ -0,0 +1,549 @@
|
|
|
1
|
+
// Configuraciones comunes para generación de mapas.
|
|
2
|
+
//
|
|
3
|
+
// Estado de configuración del paquete (ver `configurarMundo`):
|
|
4
|
+
// - factorSuperficie: fracción de la superficie del mapa base (1 = mapa
|
|
5
|
+
// completo). El lado se escala con su raíz cuadrada, así que la superficie
|
|
6
|
+
// queda proporcional al factor.
|
|
7
|
+
// - directorioMinimapas: carpeta donde se escriben los PNG de los planos.
|
|
8
|
+
// Si no se configura, no se escribe ningún archivo.
|
|
9
|
+
import fs from 'fs';
|
|
10
|
+
import path from 'path';
|
|
11
|
+
import { PNG } from 'pngjs';
|
|
12
|
+
|
|
13
|
+
let factorSuperficie = 1;
|
|
14
|
+
let directorioMinimapas = null;
|
|
15
|
+
|
|
16
|
+
// Una línea extra de manzanas de cada lado del mapa (ver más abajo); se
|
|
17
|
+
// declara antes de usarse en `tamanoMundoPara`.
|
|
18
|
+
const LADO_MANZANA = 196;
|
|
19
|
+
const MANZANAS_EXTRA_POR_EJE = 2 * LADO_MANZANA;
|
|
20
|
+
|
|
21
|
+
/** Lado del mapa (en bloques) para un factor de superficie dado. */
|
|
22
|
+
function tamanoMundoPara(factor) {
|
|
23
|
+
return Math.round(1000 * Math.sqrt(factor)) + MANZANAS_EXTRA_POR_EJE;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const BLOCK_TYPES = {
|
|
27
|
+
AIR: 0,
|
|
28
|
+
GREEN: 1,
|
|
29
|
+
BROWN: 2,
|
|
30
|
+
GRAY: 3,
|
|
31
|
+
WHITE: 4,
|
|
32
|
+
BRICK: 5,
|
|
33
|
+
BRICK_2: 9, // alternate brick texture/variant
|
|
34
|
+
BRICK_3: 10, // third brick variant
|
|
35
|
+
ASPHALT: 6,
|
|
36
|
+
VEREDA: 7,
|
|
37
|
+
WINDOW: 8,
|
|
38
|
+
WOOD_1: 11, // correspondencia con textura madera
|
|
39
|
+
BALDOSAS_1: 12, // baldosas cerámicas
|
|
40
|
+
BALDOSAS_2: 13, // segunda variante de baldosas
|
|
41
|
+
LAMPARA: 14 // luz puntual física en el techo de cada habitación con luz
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// Properties associated with each block type. We need to know whether a
|
|
45
|
+
// type may be tinted when drawing the minimap and/or in the 3D render.
|
|
46
|
+
// Generators will consult these flags before calling tinting helpers.
|
|
47
|
+
const BLOCK_PROPERTIES = {
|
|
48
|
+
// `transparent` indicates the block does not cast a shadow on the front
|
|
49
|
+
// face; light may pass through. generators and the renderer will consult
|
|
50
|
+
// this flag when building lighting/occlusion maps.
|
|
51
|
+
[BLOCK_TYPES.AIR]: { tintableMinimap: false, tintableRender: false, transparent: true },
|
|
52
|
+
[BLOCK_TYPES.GREEN]: { tintableMinimap: true, tintableRender: true, transparent: false },
|
|
53
|
+
[BLOCK_TYPES.BROWN]: { tintableMinimap: true, tintableRender: true, transparent: false },
|
|
54
|
+
[BLOCK_TYPES.GRAY]: { tintableMinimap: true, tintableRender: false, transparent: false },
|
|
55
|
+
[BLOCK_TYPES.WHITE]: { tintableMinimap: true, tintableRender: true, transparent: false },
|
|
56
|
+
[BLOCK_TYPES.BRICK]: { tintableMinimap: true, tintableRender: true, transparent: false },
|
|
57
|
+
[BLOCK_TYPES.BRICK_2]: { tintableMinimap: true, tintableRender: true, transparent: false },
|
|
58
|
+
[BLOCK_TYPES.BRICK_3]: { tintableMinimap: true, tintableRender: false, transparent: false },
|
|
59
|
+
[BLOCK_TYPES.ASPHALT]: { tintableMinimap: true, tintableRender: true, transparent: false },
|
|
60
|
+
[BLOCK_TYPES.VEREDA]: { tintableMinimap: true, tintableRender: false, transparent: false },
|
|
61
|
+
[BLOCK_TYPES.WINDOW]: { tintableMinimap: true, tintableRender: false, transparent: true },
|
|
62
|
+
[BLOCK_TYPES.WOOD_1]: { tintableMinimap: true, tintableRender: true, transparent: false },
|
|
63
|
+
[BLOCK_TYPES.BALDOSAS_1]: { tintableMinimap: false, tintableRender: false, transparent: false },
|
|
64
|
+
[BLOCK_TYPES.BALDOSAS_2]: { tintableMinimap: false, tintableRender: false, transparent: false },
|
|
65
|
+
[BLOCK_TYPES.LAMPARA]: { tintableMinimap: false, tintableRender: false, transparent: false }
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Determina si un tipo de bloque admite tintado en el minimapa.
|
|
70
|
+
* @param {number} type - constante numérica de BLOCK_TYPES
|
|
71
|
+
* @returns {boolean} true si el tipo puede recibir tintes en el minimapa
|
|
72
|
+
*/
|
|
73
|
+
function isTintableMinimap(type) {
|
|
74
|
+
const info = BLOCK_PROPERTIES[type];
|
|
75
|
+
return info ? info.tintableMinimap : false;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Determina si un tipo de bloque admite tintado en el render 3D.
|
|
80
|
+
* @param {number} type - constante numérica de BLOCK_TYPES
|
|
81
|
+
* @returns {boolean} true si el tipo puede recibir tintes en el render
|
|
82
|
+
*/
|
|
83
|
+
function isTintableRender(type) {
|
|
84
|
+
const info = BLOCK_PROPERTIES[type];
|
|
85
|
+
return info ? info.tintableRender : false;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Determina si un tipo de bloque es transparente para el render frontal.
|
|
90
|
+
* Un bloque transparente no proyecta sombras y deja pasar la luz.
|
|
91
|
+
* @param {number} type - constante numérica de BLOCK_TYPES
|
|
92
|
+
* @returns {boolean} true si el bloque es transparente
|
|
93
|
+
*/
|
|
94
|
+
function isTransparent(type) {
|
|
95
|
+
const info = BLOCK_PROPERTIES[type];
|
|
96
|
+
return info ? !!info.transparent : false;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// ruta al último minimapa generado (incluye sufijo único)
|
|
100
|
+
let lastMinimapFile = null;
|
|
101
|
+
|
|
102
|
+
function getLastMinimapFile() {
|
|
103
|
+
return lastMinimapFile;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// El mapa de cada ronda nueva se genera en un worker_thread aparte (ver
|
|
107
|
+
// `preparador_mapa.js`), que tiene su propia copia de este módulo con su
|
|
108
|
+
// propio `lastMinimapFile`: ese valor nunca llega solo al hilo principal, que
|
|
109
|
+
// es el que de verdad atiende `/minimap`. `instalarMapa` (mapGenerator.js)
|
|
110
|
+
// llama a esto con el nombre que vino en el mapa recibido del worker para que
|
|
111
|
+
// el hilo principal sirva el PNG correcto en vez de uno que el worker ya borró.
|
|
112
|
+
function setLastMinimapFile(file) {
|
|
113
|
+
if (file) lastMinimapFile = file;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const CONFIG_GENERATION = {
|
|
117
|
+
baseNoiseFreq: 0.01,
|
|
118
|
+
detailNoiseFreq: 0.05,
|
|
119
|
+
detailNoiseAmp: 0.1,
|
|
120
|
+
randomAmp: 0.05,
|
|
121
|
+
heightMultiplier: 16, // Altura entre 0 y 32 (aumentada para más resolución)
|
|
122
|
+
caveChance: 0.2 // 80% de bloques sólidos
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
// Una línea extra de manzanas de cada lado del mapa: cada manzana (cuadra + calle)
|
|
126
|
+
// mide 196 bloques (`patternSize` en mapType3Block.js; no se importa acá para no
|
|
127
|
+
// crear un ciclo entre módulos). Son dos líneas en total, una por borde.
|
|
128
|
+
// (LADO_MANZANA y MANZANAS_EXTRA_POR_EJE se declaran arriba del archivo.)
|
|
129
|
+
|
|
130
|
+
// Objeto mutable: `configurarMundo` actualiza width/height in place, así todos
|
|
131
|
+
// los módulos que lo importaron ven el tamaño ya escalado.
|
|
132
|
+
const WORLD_CONFIG = {
|
|
133
|
+
width: tamanoMundoPara(1),
|
|
134
|
+
height: tamanoMundoPara(1),
|
|
135
|
+
volumeHeight: 76, // alto del mundo: el suelo queda en la mitad y las casas de 4 plantas necesitan ~38 bloques sobre él
|
|
136
|
+
blockSize: 1,
|
|
137
|
+
chunkSize: 128,
|
|
138
|
+
renderDistance: 256,
|
|
139
|
+
// Distancia mínima (en bloques) entre árboles en veredas. Si dos posiciones quedan
|
|
140
|
+
// más cerca que este valor, sólo se colocará uno de los árboles.
|
|
141
|
+
treeMinDistance: 8,
|
|
142
|
+
updateThreshold: 0.5
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
// Configuraciones de colores del terreno por altura
|
|
146
|
+
const TERRAIN_CONFIG = {
|
|
147
|
+
subdivisions: 4, // Aumentado para mejor resolución del terreno
|
|
148
|
+
colors: [
|
|
149
|
+
{ maxHeight: 3, color: 0x1E90FF, name: 'agua' },
|
|
150
|
+
{ maxHeight: 6, color: 0xF4A460, name: 'arena' },
|
|
151
|
+
{ maxHeight: 10, color: 0x228B22, name: 'pasto' },
|
|
152
|
+
{ maxHeight: 14, color: 0x8B7355, name: 'montaña' },
|
|
153
|
+
{ maxHeight: Infinity, color: 0xE0E0E0, name: 'nieve' }
|
|
154
|
+
]
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
// Función para colocar una estructura simple (edificio)
|
|
158
|
+
function placeStructure(maxHeightMap, structures, x, z, width, height, depth) {
|
|
159
|
+
structures.push({ x, z, width, height, depth });
|
|
160
|
+
for (let dx = 0; dx < width; dx++) {
|
|
161
|
+
for (let dz = 0; dz < depth; dz++) {
|
|
162
|
+
const nx = x + dx;
|
|
163
|
+
const nz = z + dz;
|
|
164
|
+
if (nx >= 0 && nx < maxHeightMap.length && nz >= 0 && nz < maxHeightMap[0].length) {
|
|
165
|
+
const baseHeight = maxHeightMap[nx][nz];
|
|
166
|
+
maxHeightMap[nx][nz] = Math.max(baseHeight, height);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Configura el mundo que generan los módulos de este archivo.
|
|
174
|
+
*
|
|
175
|
+
* - `factorSuperficie`: fracción de la superficie base (1 = mapa completo).
|
|
176
|
+
* Actualiza `WORLD_CONFIG.width/height` in place. Pensado para correr en un
|
|
177
|
+
* worker por sala: cada worker tiene su propia copia de este módulo.
|
|
178
|
+
* - `directorioMinimapas`: carpeta para los PNG de los planos (ver
|
|
179
|
+
* `generateMapPNGs`). `null` desactiva la escritura de archivos.
|
|
180
|
+
*/
|
|
181
|
+
function configurarMundo({ factorSuperficie: factor = 1, directorioMinimapas: dir = null } = {}) {
|
|
182
|
+
if (!(factor > 0)) throw new RangeError('factorSuperficie debe ser mayor que 0');
|
|
183
|
+
factorSuperficie = factor;
|
|
184
|
+
directorioMinimapas = dir ? path.resolve(dir) : null;
|
|
185
|
+
WORLD_CONFIG.width = tamanoMundoPara(factor);
|
|
186
|
+
WORLD_CONFIG.height = tamanoMundoPara(factor);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Factor de superficie vigente (ver `configurarMundo`). */
|
|
190
|
+
function getFactorSuperficie() {
|
|
191
|
+
return factorSuperficie;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Función para generar PNGs de los planos del mapa
|
|
195
|
+
function generateMapPNGs(volume, width, height, depth, mapType, structures = [], zonasEspeciales = null) {
|
|
196
|
+
|
|
197
|
+
// Sin carpeta configurada no hay PNG que escribir (ver `configurarMundo`).
|
|
198
|
+
if (!directorioMinimapas) return null;
|
|
199
|
+
const mapsDir = directorioMinimapas;
|
|
200
|
+
|
|
201
|
+
// No se borra nada acá: este código puede correr en el worker de la ronda
|
|
202
|
+
// siguiente mientras el minimapa vigente todavía se sirve. Los viejos se
|
|
203
|
+
// limpian al instalar el mapa nuevo (ver `limpiarMinimapasViejos`).
|
|
204
|
+
fs.mkdirSync(mapsDir, { recursive: true });
|
|
205
|
+
|
|
206
|
+
// Generar un único minimapa combinando todas las capas
|
|
207
|
+
// La contribución de cada bloque se acumula y al final se obtiene el color
|
|
208
|
+
// promedio de todas las capas ocupadas. Esto asegura un resultado "acumulativo".
|
|
209
|
+
const minimap = new PNG({ width: width, height: depth });
|
|
210
|
+
// arrays auxiliares para suma de canales y conteo de capas
|
|
211
|
+
const pixelCount = width * depth;
|
|
212
|
+
const sumR = new Uint32Array(pixelCount);
|
|
213
|
+
const sumG = new Uint32Array(pixelCount);
|
|
214
|
+
const sumB = new Uint32Array(pixelCount);
|
|
215
|
+
const count = new Uint16Array(pixelCount);
|
|
216
|
+
|
|
217
|
+
// comenzar en altura media menos dos, como indicó el usuario
|
|
218
|
+
const startY = Math.max(0, Math.floor(height/2) - 2);
|
|
219
|
+
for (let y = startY; y < height; y++) {
|
|
220
|
+
for (let x = 0; x < width; x++) {
|
|
221
|
+
for (let z = 0; z < depth; z++) {
|
|
222
|
+
const idx4 = (z * width + x) * 4;
|
|
223
|
+
const idx = z * width + x;
|
|
224
|
+
// el volumen ahora puede ser un VolumeBuffer o contener objetos con propiedades { type, tint }
|
|
225
|
+
let block = (volume && typeof volume.get === 'function') ? volume.get(x, y, z) : volume[x][y][z];
|
|
226
|
+
if (block == null) continue;
|
|
227
|
+
|
|
228
|
+
// obtener el tipo de bloque incluso si el valor es un objeto
|
|
229
|
+
const blockType = (typeof block === 'object') ? block.type : block;
|
|
230
|
+
if (blockType === BLOCK_TYPES.AIR) continue; // no pintar aire
|
|
231
|
+
|
|
232
|
+
// generar color según material base
|
|
233
|
+
let r2 = 255, g2 = 255, b2 = 255;
|
|
234
|
+
switch (blockType) {
|
|
235
|
+
case BLOCK_TYPES.GREEN:
|
|
236
|
+
r2 = 0; g2 = 200; b2 = 0; break;
|
|
237
|
+
case BLOCK_TYPES.BROWN:
|
|
238
|
+
r2 = 139; g2 = 69; b2 = 19; break;
|
|
239
|
+
case BLOCK_TYPES.GRAY:
|
|
240
|
+
r2 = 128; g2 = 128; b2 = 128; break;
|
|
241
|
+
case BLOCK_TYPES.WHITE:
|
|
242
|
+
r2 = 240; g2 = 240; b2 = 240; break;
|
|
243
|
+
case BLOCK_TYPES.BRICK:
|
|
244
|
+
r2 = 178; g2 = 34; b2 = 34; break;
|
|
245
|
+
case BLOCK_TYPES.BRICK_2:
|
|
246
|
+
// use same minimap colour as brick for simplicity
|
|
247
|
+
r2 = 178; g2 = 34; b2 = 34; break;
|
|
248
|
+
case BLOCK_TYPES.BRICK_3:
|
|
249
|
+
// third brick variant shares colour too
|
|
250
|
+
r2 = 178; g2 = 34; b2 = 34; break;
|
|
251
|
+
case BLOCK_TYPES.ASPHALT:
|
|
252
|
+
r2 = 30; g2 = 30; b2 = 30; break;
|
|
253
|
+
case BLOCK_TYPES.VEREDA:
|
|
254
|
+
r2 = 200; g2 = 200; b2 = 0; break;
|
|
255
|
+
case BLOCK_TYPES.WINDOW:
|
|
256
|
+
// ventanas con matiz verdoso en el minimapa
|
|
257
|
+
r2 = 180; g2 = 220; b2 = 180; break;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// aplicar tintado minimapa si existe (mezcla lineal)
|
|
261
|
+
// primero comprobamos el campo nuevo, luego el legacy `tint`.
|
|
262
|
+
const tintSource = (typeof block === 'object') ?
|
|
263
|
+
(block.tintMinimap || block.tint) : null;
|
|
264
|
+
if (tintSource) {
|
|
265
|
+
const t = tintSource;
|
|
266
|
+
if (typeof t.color === 'number' && typeof t.opacity === 'number') {
|
|
267
|
+
// descomponer el color hexadecimal en rgb
|
|
268
|
+
const tintColor = t.color;
|
|
269
|
+
const tr = (tintColor >> 16) & 0xFF;
|
|
270
|
+
const tg = (tintColor >> 8) & 0xFF;
|
|
271
|
+
const tb = tintColor & 0xFF;
|
|
272
|
+
const alpha = Math.max(0, Math.min(1, t.opacity));
|
|
273
|
+
// mezclar
|
|
274
|
+
r2 = Math.floor(r2 * (1 - alpha) + tr * alpha);
|
|
275
|
+
g2 = Math.floor(g2 * (1 - alpha) + tg * alpha);
|
|
276
|
+
b2 = Math.floor(b2 * (1 - alpha) + tb * alpha);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
sumR[idx] += r2;
|
|
281
|
+
sumG[idx] += g2;
|
|
282
|
+
sumB[idx] += b2;
|
|
283
|
+
count[idx]++;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// construir pixels finales promediando y ajustando por cobertura
|
|
289
|
+
// columnas con pocas capas se oscurecerán para evidenciar huecos
|
|
290
|
+
const maxLayers = height - startY;
|
|
291
|
+
for (let z = 0; z < depth; z++) {
|
|
292
|
+
for (let x = 0; x < width; x++) {
|
|
293
|
+
const idx = z * width + x;
|
|
294
|
+
const idx4 = idx * 4;
|
|
295
|
+
if (count[idx] === 0) {
|
|
296
|
+
// antes se dejaba transparente, lo que en muchos visores se pinta de
|
|
297
|
+
// negro y hacía que el mapa entero pareciera "lleno de ollín".
|
|
298
|
+
// En su lugar rellenamos con un gris claro para indicar ausencia de
|
|
299
|
+
// bloques por encima del plano de corte.
|
|
300
|
+
minimap.data[idx4] = 200;
|
|
301
|
+
minimap.data[idx4 + 1] = 200;
|
|
302
|
+
minimap.data[idx4 + 2] = 200;
|
|
303
|
+
minimap.data[idx4 + 3] = 255;
|
|
304
|
+
} else {
|
|
305
|
+
let r = Math.floor(sumR[idx] / count[idx]);
|
|
306
|
+
let g = Math.floor(sumG[idx] / count[idx]);
|
|
307
|
+
let b = Math.floor(sumB[idx] / count[idx]);
|
|
308
|
+
// Ya no oscurecemos según la proporción de capas, el mapa se ve demasiado
|
|
309
|
+
// oscuro cuando casi todos los valores son pequeños. Si se requiere indicar
|
|
310
|
+
// profundidad podemos aplicar otras técnicas más adelante.
|
|
311
|
+
minimap.data[idx4] = r;
|
|
312
|
+
minimap.data[idx4 + 1] = g;
|
|
313
|
+
minimap.data[idx4 + 2] = b;
|
|
314
|
+
minimap.data[idx4 + 3] = 255;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// superponer los frentes de parcelas en rojo chillón
|
|
320
|
+
overlayParcelFronts(minimap, width, depth, structures);
|
|
321
|
+
|
|
322
|
+
// modo zombies: marcar las dos bases (0 = humana, 1 = zombie, ver
|
|
323
|
+
// `elegirBasesEnEdificios` en construirMapa.js) con un disco + cruz bien
|
|
324
|
+
// grandes, en colores distintos, para ubicarlas de un vistazo en el mapa.
|
|
325
|
+
if (zonasEspeciales && zonasEspeciales[0]) {
|
|
326
|
+
overlayBase(minimap, width, depth, zonasEspeciales[0], zonasEspeciales[0].color || 0x2f9dff);
|
|
327
|
+
}
|
|
328
|
+
if (zonasEspeciales && zonasEspeciales[1]) {
|
|
329
|
+
overlayBase(minimap, width, depth, zonasEspeciales[1], zonasEspeciales[1].color || 0xff00ff);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// nombrar el minimapa con un sufijo de marca de tiempo para evitar caches
|
|
333
|
+
const fileName = `${mapsDir}/map_${mapType}_minimap_${Date.now()}.png`;
|
|
334
|
+
// síncrono: así el archivo está completo antes de que el mapa se instale y
|
|
335
|
+
// `/minimap` nunca sirve un PNG a medio escribir
|
|
336
|
+
fs.writeFileSync(fileName, PNG.sync.write(minimap));
|
|
337
|
+
console.log(`PNG generado: ${fileName}`);
|
|
338
|
+
// mantener referencia global al último archivo generado
|
|
339
|
+
lastMinimapFile = fileName;
|
|
340
|
+
return fileName;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// Borra los PNG de minimapas que ya no están en uso. Se llama al instalar un
|
|
344
|
+
// mapa, cuando el nuevo ya quedó como el vigente: antes de eso el anterior
|
|
345
|
+
// sigue sirviéndose.
|
|
346
|
+
//
|
|
347
|
+
// OJO: cada sala corre en su propio worker_thread pero TODAS comparten este
|
|
348
|
+
// directorio. Si se borrara todo lo que no sea el PNG propio, la sala que
|
|
349
|
+
// instala su mapa más tarde le borraría el minimapa vigente a la otra (su
|
|
350
|
+
// `lastMinimapFile` seguiría apuntando a un archivo inexistente y el pedido
|
|
351
|
+
// fallaba con ENOENT). Por eso sólo se borran los PNG con más de una hora de
|
|
352
|
+
// antigüedad: ninguna sala conserva un minimapa activo tanto tiempo (las
|
|
353
|
+
// rondas duran 15 minutos y cada una genera el suyo).
|
|
354
|
+
const MINIMAP_RETENCION_MS = 60 * 60 * 1000;
|
|
355
|
+
|
|
356
|
+
function limpiarMinimapasViejos(conservar) {
|
|
357
|
+
if (!directorioMinimapas) return;
|
|
358
|
+
const mapsDir = directorioMinimapas;
|
|
359
|
+
const mantener = conservar ? path.resolve(conservar) : null;
|
|
360
|
+
const ahora = Date.now();
|
|
361
|
+
let files = [];
|
|
362
|
+
try {
|
|
363
|
+
files = fs.readdirSync(mapsDir);
|
|
364
|
+
} catch (err) {
|
|
365
|
+
return; // el directorio todavía no existe: nada que limpiar
|
|
366
|
+
}
|
|
367
|
+
for (const file of files) {
|
|
368
|
+
const ruta = path.resolve(mapsDir, file);
|
|
369
|
+
if (!file.endsWith('.png') || ruta === mantener) continue;
|
|
370
|
+
try {
|
|
371
|
+
const info = fs.statSync(ruta);
|
|
372
|
+
if (ahora - info.mtimeMs < MINIMAP_RETENCION_MS) continue;
|
|
373
|
+
fs.unlinkSync(ruta);
|
|
374
|
+
} catch (err) {
|
|
375
|
+
console.error(`[mapa] no se pudo borrar minimapa viejo ${file}:`, err.message);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// dibuja líneas rojas un píxel fuera del terreno en el frente de cada
|
|
381
|
+
// parcela identificada por `parcelId` en la lista `structures`.
|
|
382
|
+
function overlayParcelFronts(minimap, width, depth, structures) {
|
|
383
|
+
// agrupar por parcelId
|
|
384
|
+
const parcels = {};
|
|
385
|
+
for (const s of structures) {
|
|
386
|
+
if (!s.parcelId) continue;
|
|
387
|
+
let p = parcels[s.parcelId];
|
|
388
|
+
if (!p) {
|
|
389
|
+
p = {minX: Infinity, maxX: -Infinity, minZ: Infinity, maxZ: -Infinity, orientation: null};
|
|
390
|
+
parcels[s.parcelId] = p;
|
|
391
|
+
}
|
|
392
|
+
p.minX = Math.min(p.minX, s.x);
|
|
393
|
+
p.maxX = Math.max(p.maxX, s.x + s.width);
|
|
394
|
+
p.minZ = Math.min(p.minZ, s.z);
|
|
395
|
+
p.maxZ = Math.max(p.maxZ, s.z + s.depth);
|
|
396
|
+
if (s.hasFrontDoor) {
|
|
397
|
+
if (s.frontNorth) p.orientation = 'N';
|
|
398
|
+
else if (s.frontSouth) p.orientation = 'S';
|
|
399
|
+
else if (s.frontEast) p.orientation = 'E';
|
|
400
|
+
else if (s.frontWest) p.orientation = 'O';
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const r = 255, g = 0, b = 0, a = 255;
|
|
405
|
+
for (const p of Object.values(parcels)) {
|
|
406
|
+
if (!p.orientation) continue;
|
|
407
|
+
if (p.orientation === 'N') {
|
|
408
|
+
const z = p.minZ - 1;
|
|
409
|
+
for (let x = p.minX; x < p.maxX; x++) {
|
|
410
|
+
if (x >= 0 && x < width && z >= 0 && z < depth) {
|
|
411
|
+
const idx4 = (z * width + x) * 4;
|
|
412
|
+
minimap.data[idx4] = r;
|
|
413
|
+
minimap.data[idx4 + 1] = g;
|
|
414
|
+
minimap.data[idx4 + 2] = b;
|
|
415
|
+
minimap.data[idx4 + 3] = a;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
} else if (p.orientation === 'S') {
|
|
419
|
+
const z = p.maxZ;
|
|
420
|
+
for (let x = p.minX; x < p.maxX; x++) {
|
|
421
|
+
if (x >= 0 && x < width && z >= 0 && z < depth) {
|
|
422
|
+
const idx4 = (z * width + x) * 4;
|
|
423
|
+
minimap.data[idx4] = r;
|
|
424
|
+
minimap.data[idx4 + 1] = g;
|
|
425
|
+
minimap.data[idx4 + 2] = b;
|
|
426
|
+
minimap.data[idx4 + 3] = a;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
} else if (p.orientation === 'E') {
|
|
430
|
+
const x = p.maxX;
|
|
431
|
+
for (let z = p.minZ; z < p.maxZ; z++) {
|
|
432
|
+
if (x >= 0 && x < width && z >= 0 && z < depth) {
|
|
433
|
+
const idx4 = (z * width + x) * 4;
|
|
434
|
+
minimap.data[idx4] = r;
|
|
435
|
+
minimap.data[idx4 + 1] = g;
|
|
436
|
+
minimap.data[idx4 + 2] = b;
|
|
437
|
+
minimap.data[idx4 + 3] = a;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
} else if (p.orientation === 'O') {
|
|
441
|
+
const x = p.minX - 1;
|
|
442
|
+
for (let z = p.minZ; z < p.maxZ; z++) {
|
|
443
|
+
if (x >= 0 && x < width && z >= 0 && z < depth) {
|
|
444
|
+
const idx4 = (z * width + x) * 4;
|
|
445
|
+
minimap.data[idx4] = r;
|
|
446
|
+
minimap.data[idx4 + 1] = g;
|
|
447
|
+
minimap.data[idx4 + 2] = b;
|
|
448
|
+
minimap.data[idx4 + 3] = a;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Marca bien visible sobre el centro de `zona`: un disco relleno con una
|
|
457
|
+
* cruz más larga encima. El mapa es ~1000x1000 bloques y el minimapa es 1:1
|
|
458
|
+
* con él, así que un marcador de pocos bloques de lado (como la cruz de 5
|
|
459
|
+
* que había antes) queda en unos pocos píxeles y no se ve una vez que el
|
|
460
|
+
* <img> lo achica para mostrarlo en pantalla — de ahí que no se notara.
|
|
461
|
+
*/
|
|
462
|
+
function overlayBase(minimap, width, depth, zona, color) {
|
|
463
|
+
const cx = Math.round(zona.x), cz = Math.round(zona.z);
|
|
464
|
+
const r = (color >> 16) & 0xFF, g = (color >> 8) & 0xFF, b = color & 0xFF, a = 255;
|
|
465
|
+
const pintar = (x, z) => {
|
|
466
|
+
if (x < 0 || x >= width || z < 0 || z >= depth) return;
|
|
467
|
+
const idx4 = (z * width + x) * 4;
|
|
468
|
+
minimap.data[idx4] = r;
|
|
469
|
+
minimap.data[idx4 + 1] = g;
|
|
470
|
+
minimap.data[idx4 + 2] = b;
|
|
471
|
+
minimap.data[idx4 + 3] = a;
|
|
472
|
+
};
|
|
473
|
+
const RADIO_DISCO = 14;
|
|
474
|
+
for (let dz = -RADIO_DISCO; dz <= RADIO_DISCO; dz++) {
|
|
475
|
+
for (let dx = -RADIO_DISCO; dx <= RADIO_DISCO; dx++) {
|
|
476
|
+
if (dx * dx + dz * dz <= RADIO_DISCO * RADIO_DISCO) pintar(cx + dx, cz + dz);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
const BRAZO = 24;
|
|
480
|
+
const ESPESOR = 2;
|
|
481
|
+
for (let d = -BRAZO; d <= BRAZO; d++) {
|
|
482
|
+
for (let e = -ESPESOR; e <= ESPESOR; e++) {
|
|
483
|
+
pintar(cx + d, cz + e);
|
|
484
|
+
pintar(cx + e, cz + d);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
// helper para aplicar tintado a un bloque dentro de un volumen
|
|
490
|
+
// el juego ahora diferencia entre dos tintes distintos:
|
|
491
|
+
// • tintMinimap: usado únicamente por el minimapa (y opcionalmente por
|
|
492
|
+
// el render cuando el usuario aprieta la tecla “n”).
|
|
493
|
+
// • tintRender: aplicado siempre en el render 3D del cliente.
|
|
494
|
+
// el campo legacy `tint` se mantiene sólo por compatibilidad y se tratará
|
|
495
|
+
// como `tintMinimap` si aparece.
|
|
496
|
+
function tintBlockMinimap(volume, x, y, z, color, opacity) {
|
|
497
|
+
if (volume && typeof volume.setTintMinimap === 'function') {
|
|
498
|
+
volume.setTintMinimap(x, y, z, color, opacity);
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
if (!volume[x] || !volume[x][y] || volume[x][y][z] == null) return;
|
|
502
|
+
let cell = volume[x][y][z];
|
|
503
|
+
if (typeof cell !== 'object') {
|
|
504
|
+
cell = { type: cell };
|
|
505
|
+
volume[x][y][z] = cell;
|
|
506
|
+
}
|
|
507
|
+
cell.tintMinimap = { color, opacity };
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
function tintBlockRender(volume, x, y, z, color, opacity) {
|
|
511
|
+
if (volume && typeof volume.setTintRender === 'function') {
|
|
512
|
+
volume.setTintRender(x, y, z, color, opacity);
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
if (!volume[x] || !volume[x][y] || volume[x][y][z] == null) return;
|
|
516
|
+
let cell = volume[x][y][z];
|
|
517
|
+
if (typeof cell !== 'object') {
|
|
518
|
+
cell = { type: cell };
|
|
519
|
+
volume[x][y][z] = cell;
|
|
520
|
+
}
|
|
521
|
+
cell.tintRender = { color, opacity };
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
// para no romper código existente exportamos tintBlock como alias a
|
|
525
|
+
// tintBlockMinimap, que reproduce el comportamiento previo.
|
|
526
|
+
function tintBlock(volume, x, y, z, color, opacity) {
|
|
527
|
+
return tintBlockMinimap(volume, x, y, z, color, opacity);
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
export {
|
|
531
|
+
configurarMundo,
|
|
532
|
+
getFactorSuperficie,
|
|
533
|
+
CONFIG_GENERATION,
|
|
534
|
+
WORLD_CONFIG,
|
|
535
|
+
TERRAIN_CONFIG,
|
|
536
|
+
BLOCK_TYPES,
|
|
537
|
+
BLOCK_PROPERTIES,
|
|
538
|
+
isTintableMinimap,
|
|
539
|
+
isTintableRender,
|
|
540
|
+
isTransparent,
|
|
541
|
+
placeStructure,
|
|
542
|
+
generateMapPNGs,
|
|
543
|
+
limpiarMinimapasViejos,
|
|
544
|
+
tintBlock,
|
|
545
|
+
tintBlockMinimap,
|
|
546
|
+
tintBlockRender,
|
|
547
|
+
getLastMinimapFile,
|
|
548
|
+
setLastMinimapFile
|
|
549
|
+
};
|