forked from Bitcoindefi/OpenAO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexportEditableMaps.ts
More file actions
454 lines (369 loc) · 14.6 KB
/
Copy pathexportEditableMaps.ts
File metadata and controls
454 lines (369 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
import fs from "node:fs";
import path from "node:path";
type JsonValue = string | number | boolean | null | JsonObject | JsonArray;
type JsonObject = { [key: string]: JsonValue };
type JsonArray = JsonValue[];
type RawTile = Record<string, unknown>;
type TileExit = {
map: number;
x: number;
y: number;
};
type TileExitConfig = TileExit | { destinations: TileExit[] };
type ObjectInfo = {
objIndex: number;
amount: number;
};
type TerrainTile = {
blocked?: true;
graphics: number | Array<number | null>;
};
type MapMetadata = {
id: number;
name: string;
musicNum: number;
magiaSinEfecto: number;
noEncriptarMp: number;
terreno: string;
zona: string;
restringir: string | number;
minLevel: number;
maxLevel: number;
backup: number;
pk: number;
};
type EditableTerrain = {
id: number;
width: number;
height: number;
palette: Record<string, TerrainTile>;
rows: number[][];
};
type EditableSpecials = {
id: number;
exits: Record<string, TileExitConfig>;
objects: Record<string, ObjectInfo>;
npcs: Record<string, number>;
triggers: Record<string, number>;
};
const DEFAULT_MAPS_DIR = path.resolve(__dirname, "../../mapas");
const DEFAULT_DATS_DIR = path.resolve(DEFAULT_MAPS_DIR, "dats");
const DEFAULT_OUTPUT_DIR = path.resolve(__dirname, "../../mapas_source");
const MAP_FILE_PATTERN = /^mapa_(\d+)\.json$/i;
function parseCliArgs(argv: string[]) {
const mapIds = new Set<number>();
let inputDir = DEFAULT_MAPS_DIR;
let datsDir = DEFAULT_DATS_DIR;
let outputDir = DEFAULT_OUTPUT_DIR;
let pretty = false;
for (const arg of argv) {
if (arg.startsWith("--input-dir=")) {
inputDir = path.resolve(process.cwd(), arg.slice("--input-dir=".length));
continue;
}
if (arg.startsWith("--dats-dir=")) {
datsDir = path.resolve(process.cwd(), arg.slice("--dats-dir=".length));
continue;
}
if (arg.startsWith("--output-dir=")) {
outputDir = path.resolve(process.cwd(), arg.slice("--output-dir=".length));
continue;
}
if (arg.startsWith("--maps=")) {
for (const rawId of arg.slice("--maps=".length).split(",")) {
const mapId = Number.parseInt(rawId.trim(), 10);
if (Number.isInteger(mapId) && mapId > 0) {
mapIds.add(mapId);
}
}
continue;
}
if (arg === "--pretty") {
pretty = true;
continue;
}
const mapId = Number.parseInt(arg, 10);
if (Number.isInteger(mapId) && mapId > 0) {
mapIds.add(mapId);
}
}
return {
inputDir,
datsDir,
outputDir,
pretty,
mapIds: [...mapIds].sort((left, right) => left - right),
};
}
function readJsonFile(filePath: string): unknown {
return JSON.parse(fs.readFileSync(filePath, "utf8")) as unknown;
}
function getAvailableMapIds(inputDir: string, datsDir: string): number[] {
const mapFiles = fs.readdirSync(inputDir);
const mapIds: number[] = [];
for (const fileName of mapFiles) {
const match = fileName.match(MAP_FILE_PATTERN);
if (!match) {
continue;
}
const mapId = Number.parseInt(match[1], 10);
const datFilePath = path.join(datsDir, fileName);
if (Number.isInteger(mapId) && mapId > 0 && fs.existsSync(datFilePath)) {
mapIds.push(mapId);
}
}
return mapIds.sort((left, right) => left - right);
}
function toFiniteNumber(value: unknown): number | undefined {
if (typeof value === "number" && Number.isFinite(value)) {
return value;
}
if (typeof value === "string" && value.trim()) {
const parsed = Number(value);
return Number.isFinite(parsed) ? parsed : undefined;
}
return undefined;
}
function normalizeTileExitDestinations(tileExit: Record<string, unknown> | undefined): TileExit[] {
const rawDestinations = Array.isArray(tileExit?.destinations) ? tileExit.destinations : tileExit ? [tileExit] : [];
const destinations: TileExit[] = [];
for (const destination of rawDestinations) {
const destinationRecord = destination as Record<string, unknown>;
const map = toFiniteNumber(destinationRecord?.map);
const x = toFiniteNumber(destinationRecord?.x);
const y = toFiniteNumber(destinationRecord?.y);
if (map === undefined || x === undefined || y === undefined) {
continue;
}
destinations.push({ map, x, y });
}
return destinations;
}
function normalizeGraphics(value: unknown): number | Array<number | null> | undefined {
if (!value || typeof value !== "object" || Array.isArray(value)) {
return undefined;
}
const graphicsRecord = value as Record<string, unknown>;
const numericEntries: Array<readonly [number, number]> = Object.entries(graphicsRecord)
.map(([layer, layerValue]) => {
const parsedLayer = Number.parseInt(layer, 10);
const parsedValue = toFiniteNumber(layerValue);
if (!Number.isInteger(parsedLayer) || parsedLayer <= 0 || parsedValue === undefined) {
return null;
}
return [parsedLayer, parsedValue] as const;
})
.filter((entry): entry is readonly [number, number] => entry !== null)
.sort((left, right) => left[0] - right[0]);
if (numericEntries.length === 0) {
return undefined;
}
if (numericEntries.length === 1 && numericEntries[0][0] === 1) {
return numericEntries[0][1];
}
const lastLayer = numericEntries[numericEntries.length - 1][0];
const result: Array<number | null> = Array.from({ length: lastLayer }, () => null);
for (const [layer, layerValue] of numericEntries) {
result[layer - 1] = layerValue;
}
return result;
}
function sortJsonValue(value: JsonValue): JsonValue {
if (Array.isArray(value)) {
return value.map((entry) => sortJsonValue(entry));
}
if (!value || typeof value !== "object") {
return value;
}
const sortedEntries = Object.entries(value)
.sort(([leftKey], [rightKey]) => leftKey.localeCompare(rightKey))
.map(([key, entryValue]) => [key, sortJsonValue(entryValue)] as const);
return Object.fromEntries(sortedEntries);
}
function stableStringify(value: JsonValue): string {
return JSON.stringify(sortJsonValue(value));
}
function buildCoordinateKey(x: number, y: number): string {
return `${x},${y}`;
}
function buildMetadata(mapId: number, rawMetadata: unknown): MapMetadata {
const metadata = (
rawMetadata && typeof rawMetadata === "object" && !Array.isArray(rawMetadata) ? rawMetadata : {}
) as Record<string, unknown>;
return {
id: mapId,
name: String(metadata.name ?? ""),
musicNum: toFiniteNumber(metadata.musicNum) ?? 0,
magiaSinEfecto: toFiniteNumber(metadata.magiaSinEfecto) ?? 0,
noEncriptarMp: toFiniteNumber(metadata.noEncriptarMp) ?? 0,
terreno: String(metadata.terreno ?? ""),
zona: String(metadata.zona ?? ""),
restringir: typeof metadata.restringir === "number" ? metadata.restringir : String(metadata.restringir ?? "No"),
minLevel: toFiniteNumber(metadata.minLevel) ?? 0,
maxLevel: toFiniteNumber(metadata.maxLevel) ?? 0,
backup: toFiniteNumber(metadata.backup) ?? 0,
pk: toFiniteNumber(metadata.pk) ?? 0,
};
}
function buildEditableMap(
mapId: number,
rawMapJson: unknown,
): {
terrain: EditableTerrain;
specials: EditableSpecials;
} {
if (!rawMapJson || typeof rawMapJson !== "object" || Array.isArray(rawMapJson)) {
throw new Error(`Mapa ${mapId}: formato JSON inválido.`);
}
const root = rawMapJson as Record<string, unknown>;
const mapNode = root[String(mapId)];
if (!mapNode || typeof mapNode !== "object" || Array.isArray(mapNode)) {
throw new Error(`Mapa ${mapId}: no se encontró el nodo principal ${mapId}.`);
}
const rowKeys = Object.keys(mapNode)
.map((value) => Number.parseInt(value, 10))
.filter((value) => Number.isInteger(value) && value > 0)
.sort((left, right) => left - right);
const height = rowKeys.at(-1) ?? 0;
const width = rowKeys.reduce((maxWidth, rowKey) => {
const row = (mapNode as Record<string, unknown>)[String(rowKey)];
if (!row || typeof row !== "object" || Array.isArray(row)) {
return maxWidth;
}
const rowWidth = Object.keys(row)
.map((value) => Number.parseInt(value, 10))
.filter((value) => Number.isInteger(value) && value > 0)
.reduce((maxX, x) => Math.max(maxX, x), 0);
return Math.max(maxWidth, rowWidth);
}, 0);
const terrainPaletteBySignature = new Map<string, TerrainTile>();
const terrainRows: Array<Array<TerrainTile | null>> = [];
const specials: EditableSpecials = {
id: mapId,
exits: {},
objects: {},
npcs: {},
triggers: {},
};
for (let y = 1; y <= height; y++) {
const row = (mapNode as Record<string, unknown>)[String(y)] as Record<string, unknown> | undefined;
const terrainRow: Array<TerrainTile | null> = [];
for (let x = 1; x <= width; x++) {
const rawTile = row?.[String(x)];
const tile =
rawTile && typeof rawTile === "object" && !Array.isArray(rawTile) ? (rawTile as RawTile) : null;
const coordinateKey = buildCoordinateKey(x, y);
const graphics = normalizeGraphics(tile?.graphics);
const blocked = toFiniteNumber(tile?.blocked) === 1;
if (graphics !== undefined) {
const terrainTile: TerrainTile = {
graphics,
};
if (blocked) {
terrainTile.blocked = true;
}
const signature = stableStringify(terrainTile as JsonValue);
terrainPaletteBySignature.set(signature, terrainTile);
terrainRow.push(terrainTile);
} else {
terrainRow.push(null);
}
const tileExit = tile?.tileExit as Record<string, unknown> | undefined;
const tileExitDestinations = normalizeTileExitDestinations(tileExit);
if (tileExitDestinations.length === 1) {
specials.exits[coordinateKey] = tileExitDestinations[0];
} else if (tileExitDestinations.length > 1) {
specials.exits[coordinateKey] = {
destinations: tileExitDestinations,
};
}
const objInfo = tile?.objInfo as Record<string, unknown> | undefined;
const objIndex = toFiniteNumber(objInfo?.objIndex);
const amount = toFiniteNumber(objInfo?.amount);
if (objIndex !== undefined && amount !== undefined) {
specials.objects[coordinateKey] = {
objIndex,
amount,
};
}
const npcIndex = toFiniteNumber(tile?.npcIndex);
if (npcIndex !== undefined) {
specials.npcs[coordinateKey] = npcIndex;
}
const trigger = toFiniteNumber(tile?.trigger);
if (trigger !== undefined) {
specials.triggers[coordinateKey] = trigger;
}
}
terrainRows.push(terrainRow);
}
const sortedTerrainEntries = [...terrainPaletteBySignature.entries()].sort(([leftKey], [rightKey]) =>
leftKey.localeCompare(rightKey),
);
const palette: Record<string, TerrainTile> = {};
const terrainIdBySignature = new Map<string, number>();
for (let index = 0; index < sortedTerrainEntries.length; index++) {
const [signature, terrainTile] = sortedTerrainEntries[index];
const terrainId = index + 1;
terrainIdBySignature.set(signature, terrainId);
palette[String(terrainId)] = terrainTile;
}
const rows = terrainRows.map((row) =>
row.map((terrainTile) => {
if (!terrainTile) {
return 0;
}
const signature = stableStringify(terrainTile as JsonValue);
return terrainIdBySignature.get(signature) ?? 0;
}),
);
return {
terrain: {
id: mapId,
width,
height,
palette,
rows,
},
specials,
};
}
function writeJsonFile(filePath: string, value: unknown, pretty: boolean): void {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
const output = pretty ? `${JSON.stringify(value, null, 4)}\n` : JSON.stringify(value);
fs.writeFileSync(filePath, output, "utf8");
}
function exportMap(mapId: number, inputDir: string, datsDir: string, outputDir: string, pretty: boolean): void {
const mapFilePath = path.join(inputDir, `mapa_${mapId}.json`);
const datFilePath = path.join(datsDir, `mapa_${mapId}.json`);
if (!fs.existsSync(mapFilePath)) {
throw new Error(`Mapa ${mapId}: no existe ${mapFilePath}`);
}
if (!fs.existsSync(datFilePath)) {
throw new Error(`Mapa ${mapId}: no existe ${datFilePath}`);
}
const { terrain, specials } = buildEditableMap(mapId, readJsonFile(mapFilePath));
const metadata = buildMetadata(mapId, readJsonFile(datFilePath));
const mapOutputDir = path.join(outputDir, `mapa_${mapId}`);
writeJsonFile(path.join(mapOutputDir, "meta.json"), metadata, pretty);
writeJsonFile(path.join(mapOutputDir, "terrain.json"), terrain, pretty);
writeJsonFile(path.join(mapOutputDir, "specials.json"), specials, pretty);
console.log(
`Exportado mapa ${mapId} -> ${path.relative(process.cwd(), mapOutputDir)} ` +
`(terrainPalette=${Object.keys(terrain.palette).length}, exits=${Object.keys(specials.exits).length}, ` +
`objects=${Object.keys(specials.objects).length}, npcs=${Object.keys(specials.npcs).length}, ` +
`triggers=${Object.keys(specials.triggers).length})`,
);
}
function main(): void {
const { inputDir, datsDir, outputDir, pretty, mapIds } = parseCliArgs(process.argv.slice(2));
const resolvedMapIds = mapIds.length > 0 ? mapIds : getAvailableMapIds(inputDir, datsDir);
if (resolvedMapIds.length === 0) {
throw new Error("No se encontraron mapas para exportar.");
}
for (const mapId of resolvedMapIds) {
exportMap(mapId, inputDir, datsDir, outputDir, pretty);
}
console.log(`Listo. Mapas exportados: ${resolvedMapIds.length}`);
}
main();