-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitmap.c
More file actions
488 lines (430 loc) · 13.5 KB
/
splitmap.c
File metadata and controls
488 lines (430 loc) · 13.5 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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <libgen.h>
#include "zx0.h"
#define MAX_OFFSET_ZX0 32640
#define MAX_OFFSET_ZX7 2176
#define MAX_ITEMS 256
typedef struct item_data
{
struct item_data *next;
int id;
unsigned char frame;
} item_data_t;
typedef struct
{
char *tableName;
int count;
item_data_t *data;
} item_t;
item_t items[MAX_ITEMS];
item_t *addItem(char *name, int id, unsigned char frame)
{
int n;
item_t *foundEntry = NULL;
item_data_t *itemData = NULL;
// Find the item with the same name
for (n = 0; n < MAX_ITEMS; n++)
{
if (items[n].tableName == NULL)
{
foundEntry = &items[n];
foundEntry->tableName = name;
break;
}
else if (strcmp(items[n].tableName, name) == 0)
{
foundEntry = &items[n];
break;
}
}
if (n == MAX_ITEMS)
{
fprintf(stderr, "No space to add item (%s)\n", name);
exit(-1);
}
if ((itemData = malloc(sizeof(item_data_t))) == NULL)
{
perror("Could not allocate memory for item");
exit(-1);
}
itemData->id = id;
itemData->frame = frame;
// Add to list
itemData->next = foundEntry->data;
foundEntry->data = itemData;
return (foundEntry);
}
void doCompression(char *fileName, unsigned char *input_data, int input_size)
{
FILE *ofp = NULL;
char *output_name = NULL;
int output_size;
int skip = 0;
unsigned char *output_data;
int delta;
int quick_mode = FALSE;
int backwards_mode = FALSE;
int classic_mode = TRUE;
void *blockStart = NULL;
output_name = (char *)malloc(strlen(fileName) + 5);
strcpy(output_name, fileName);
strcat(output_name, ".zx0");
/* create output file */
ofp = fopen(output_name, "wb");
if (!ofp)
{
fprintf(stderr, "Error: Cannot create output file %s\n", output_name);
free(output_name);
exit(1);
}
output_data = compress(optimize(input_data, input_size, skip, quick_mode ? MAX_OFFSET_ZX7 : MAX_OFFSET_ZX0, &blockStart),
input_data, input_size, skip, backwards_mode, !classic_mode && !backwards_mode, &output_size, &delta);
free(blockStart);
/* write output file */
if (fwrite(output_data, sizeof(char), output_size, ofp) != output_size)
{
fprintf(stderr, "Error: Cannot write output file %s\n", output_name);
fclose(ofp);
free(output_name);
free(output_data);
exit(1);
}
/* close output file */
fclose(ofp);
free(output_name);
free(output_data);
}
int main(int argc, char *argv[])
{
char *outputFileName = NULL;
char *fileName = NULL;
char *ext = NULL;
char *tablesSection = NULL;
char *dataSection = NULL;
char *roDataSection = NULL;
unsigned char *inputData = NULL;
unsigned char *compressData = NULL;
FILE *inFile = NULL;
FILE *cFile = NULL;
int mapWidth = 0;
int mapHeight = 0;
int levelWidth = 0;
int levelHeight = 0;
int compressWidth = -1;
int compressHeight = -1;
int itemsOnly = 0;
int inputSize = 0;
int compressSize = 0;
int blank = 0;
memset(items, 0, sizeof(items));
if (argc < 2)
{
printf(
"%s --map <filename> --map-size <WIDTHxHEIGHT> --level-size <WIDTHxHEIGHT> [--blank <tile ID>] [--tablessection <section name>] [--[ro]datasection <section name>] [--items-only] [--item <name>,<ID>[,<FRAME>] [...]]\n",
argv[0]);
return 0;
}
for (int param = 1; param < argc; param++)
{
if (!strcmp(argv[param], "--items-only"))
{
itemsOnly = 1;
}
else if (!strcmp(argv[param], "--map-size"))
{
param++;
mapWidth = atoi(strtok(argv[param], "x"));
mapHeight = atoi(strtok(NULL, "x"));
}
else if (!strcmp(argv[param], "--level-size"))
{
param++;
levelWidth = atoi(strtok(argv[param], "x"));
levelHeight = atoi(strtok(NULL, "x"));
}
else if (!strcmp(argv[param], "--output-size"))
{
param++;
compressWidth = atoi(strtok(argv[param], "x"));
compressHeight = atoi(strtok(NULL, "x"));
}
else if (!strcmp(argv[param], "--map"))
{
param++;
fileName = argv[param];
}
else if (!strcmp(argv[param], "--datasection"))
{
param++;
dataSection = argv[param];
}
else if (!strcmp(argv[param], "--rodatasection"))
{
param++;
roDataSection = argv[param];
}
else if (!strcmp(argv[param], "--tablessection"))
{
param++;
tablesSection = argv[param];
}
else if (!strcmp(argv[param], "--blank"))
{
param++;
blank = atoi(argv[param]);
}
else if (!strcmp(argv[param], "--item"))
{
char *tmp;
char *name;
int id;
unsigned char frame;
param++;
// Items are represented in a comma separated list
// of <itemname>,<itemID>
name = strtok(argv[param], ",");
id = atoi(strtok(NULL, ","));
if ((tmp = strtok(NULL, ",")) != NULL)
{
frame = atoi(tmp);
}
else
{
frame = 0;
}
addItem(name, id, frame);
}
}
if (!mapWidth || !mapHeight || !levelWidth || !levelHeight || !fileName)
{
fprintf(stderr, "Invalid parameter!\n");
exit(-1);
}
if (compressWidth == -1)
{
compressWidth = levelWidth;
}
if (compressHeight == -1)
{
compressHeight = levelHeight;
}
if ((inFile = fopen(fileName, "r+b")) == NULL)
{
fprintf(stderr, "Could not open in file\n");
exit(-1);
}
ext = strrchr(fileName, '.');
*ext++ = 0;
if (!(outputFileName = malloc(strlen(fileName) + 128)))
{
fprintf(stderr, "Error allocating memory for output filename\n");
fclose(inFile);
exit(-1);
}
inputSize = levelHeight * levelWidth;
if (!(inputData = malloc(inputSize)))
{
free(outputFileName);
fclose(inFile);
fprintf(stderr, "Error allocating input buffer\n");
exit(-1);
}
compressSize = compressHeight * compressWidth;
if (!(compressData = malloc(compressSize)))
{
free(outputFileName);
free(inputData);
fclose(inFile);
fprintf(stderr, "Error allocating compressed data buffer\n");
exit(-1);
}
sprintf(outputFileName, "%s.inc", fileName);
if ((cFile = fopen(outputFileName, "w")) == NULL)
{
fprintf(stderr, "Could not open in file\n");
free(outputFileName);
free(inputData);
free(compressData);
exit(-1);
}
fprintf(cFile, ";\n");
fprintf(cFile, "; Auto-generated by splitmap do not modify.\n");
fprintf(cFile, ";\n");
if (roDataSection)
{
fprintf(cFile, " section %s", roDataSection);
fprintf(cFile, "\n");
fprintf(cFile, "eot:\n");
fprintf(cFile, " db $ff\n\n");
}
if (tablesSection)
{
fprintf(cFile, " section %s", tablesSection);
fprintf(cFile, "\n");
}
for (int n = 0; items[n].tableName != NULL; n++)
{
fprintf(cFile, " public _%sTables\n\n", items[n].tableName);
fprintf(cFile, "_%sTables:\n", items[n].tableName);
for (int levels = 0; levels < (mapHeight / levelHeight) * (mapWidth / levelWidth); levels++)
{
fprintf(cFile, " dw level%d%s\n", levels, items[n].tableName);
}
fprintf(cFile, "\n");
}
if (dataSection)
{
fprintf(cFile, " section %s", dataSection);
fprintf(cFile, "\n");
}
for (int y = 0; y < (mapHeight / levelHeight); y++)
{
for (int x = 0; x < (mapWidth / levelWidth); x++)
{
for (int row = 0; row < levelHeight; row++)
{
fseek(inFile, (y * levelHeight * mapWidth) + (x * levelWidth) + (row * mapWidth), SEEK_SET);
if ((fread(&inputData[row * levelWidth], levelWidth, 1, inFile)) != 1)
{
fprintf(stderr, "Error reading input file\n");
free(outputFileName);
free(inputData);
free(compressData);
fclose(inFile);
exit(-1);
}
}
for (int n = 0; items[n].tableName != NULL; n++)
{
bool oneShot = FALSE;
for (item_data_t *data = items[n].data; data != NULL; data = data->next)
{
// Find items
for (int map = 0; map < levelHeight * levelWidth; map++)
{
if (inputData[map] == data->id)
{
// Items in tables don't need to be in
// the map, blank them out.
inputData[map] = blank;
if (!oneShot)
{
oneShot = TRUE;
fprintf(cFile, "level%d%s:\n", (y * (mapWidth / levelWidth)) + x, items[n].tableName);
}
fprintf(cFile, " db $01, $%02x, $%02x, $%02x\n", (map % levelWidth) * 8,
(map / levelWidth) * 8, data->frame);
items[n].count++;
}
}
}
if (!oneShot)
{
fprintf(cFile, "level%d%s equ eot\n\n", (y * (mapWidth / levelWidth)) + x, items[n].tableName);
}
else
fprintf(cFile, " db $ff\n\n");
}
// Write out updated map
for (int row = 0; row < levelHeight; row++)
{
fseek(inFile, (y * levelHeight * mapWidth) + (x * levelWidth) + (row * mapWidth), SEEK_SET);
if ((fwrite(&inputData[row * levelWidth], levelWidth, 1, inFile)) != 1)
{
fprintf(stderr, "Error writing updated map\n");
free(outputFileName);
free(inputData);
free(compressData);
fclose(inFile);
exit(-1);
}
}
}
}
// See if compressed maps need to be written
if (itemsOnly == 0)
{
rewind(inFile);
for (int y = 0; y < (mapHeight / compressHeight); y++)
{
for (int x = 0; x < (mapWidth / compressWidth); x++)
{
sprintf(outputFileName, "%s_%02d%02d.%s", fileName, x, y, ext);
for (int row = 0; row < compressHeight; row++)
{
fseek(inFile, (y * compressHeight * mapWidth) + (x * compressWidth) + (row * mapWidth), SEEK_SET);
if ((fread(&compressData[row * compressWidth], compressWidth, 1, inFile)) != 1)
{
fprintf(stderr, "Error reading input file\n");
free(outputFileName);
free(inputData);
free(compressData);
fclose(inFile);
exit(-1);
}
}
fprintf(stderr, "%s ", outputFileName);
doCompression(outputFileName, compressData, compressSize);
}
}
}
fclose(inFile);
free(outputFileName);
free(inputData);
free(compressData);
for (int n = 0; items[n].tableName != NULL; n++)
{
if (items[n].tableName)
{
printf("\t#define\t%s_COUNT %d\n", items[n].tableName, items[n].count);
}
}
//
// Write out the level table and the compressed tilemap data table
//
if (itemsOnly == 0)
{
fprintf(cFile, "\n");
if (roDataSection)
{
fprintf(cFile, " section %s\n", roDataSection);
fprintf(cFile, "\n");
}
fprintf(cFile, " public _levelTable\n");
fprintf(cFile, "\n");
fprintf(cFile, "_levelTable:\n");
char *fname = basename(fileName);
for (int y = 0; y < (mapHeight / compressHeight); y++)
{
for (int x = 0; x < (mapWidth / compressWidth); x++)
{
fprintf(cFile, " dw %s_%02d%02d\n", fname, x, y);
}
}
fprintf(cFile, "\n");
for (int y = 0; y < (mapHeight / compressHeight); y++)
{
for (int x = 0; x < (mapWidth / compressWidth); x++)
{
fprintf(cFile, "%s_%02d%02d:\n", fname, x, y);
fprintf(cFile, " binary \"%s_%02d%02d.nxm.zx0\"\n", fname, x, y);
}
}
}
fclose(cFile);
// Free allocated memory
for (int n = 0; items[n].tableName != NULL; n++)
{
while (items[n].data)
{
item_data_t *tmp = items[n].data;
items[n].data = items[n].data->next;
free(tmp);
}
}
return 0;
}