File: tile2bin.c

package info (click to toggle)
nethack 3.6.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,468 kB
  • sloc: ansic: 266,495; cpp: 13,652; yacc: 2,903; perl: 1,426; lex: 581; sh: 535; xml: 372; awk: 98; makefile: 68; fortran: 51; sed: 11
file content (368 lines) | stat: -rw-r--r-- 10,186 bytes parent folder | download | duplicates (5)
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
/* NetHack 3.6	tile2bin.c	$NHDT-Date: 1457207041 2016/03/05 19:44:01 $  $NHDT-Branch: chasonr $:$NHDT-Revision: 1.9 $ */
/*   Copyright (c) NetHack PC Development Team 1993, 1994, 1995     */
/*   NetHack may be freely redistributed.  See license for details. */

/*
 * Edit History:
 *
 *	Initial Creation			M.Allison	93/10/21
 *	ifndef MONITOR_HEAP for heaputil.c	P.Winner	94/03/12
 *      added Borland C _stklen variable	Y.Sapir		94/05/01
 *	fixed to use text tiles from win/share	M.Allison	95/01/31
 *
 */

#include "hack.h"
#include "pcvideo.h"
#include "tile.h"
#include "pctiles.h"

#include <dos.h>
#ifndef MONITOR_HEAP
#include <stdlib.h>
#endif
#include <time.h>

#ifdef __GO32__
#include <unistd.h>
#endif

#if defined(_MSC_VER) && _MSC_VER >= 700
#pragma warning(disable : 4309) /* initializing */
#pragma warning(disable : 4018) /* signed/unsigned mismatch */
#pragma warning(disable : 4131) /* old style declarator */
#pragma warning(disable : 4127) /* conditional express. is constant */
#endif

#ifdef __BORLANDC__
extern unsigned _stklen = STKSIZ;
#endif

/* Produce only a planar file if building the overview file; with packed
   files, we can make overview-size tiles on the fly */
#if defined(OVERVIEW_FILE) && defined(PACKED_FILE)
#undef PACKED_FILE
#endif

extern char *FDECL(tilename, (int, int));

#ifdef PLANAR_FILE
char masktable[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
char charcolors[MAXCOLORMAPSIZE];
#ifdef OVERVIEW_FILE
struct overview_planar_cell_struct planetile;
#else
struct planar_cell_struct planetile;
#endif
FILE *tibfile1;
#endif

#ifdef PACKED_FILE
char packtile[TILE_Y][TILE_X];
FILE *tibfile2;
#endif

int num_colors;
pixel pixels[TILE_Y][TILE_X];
struct tibhdr_struct tibheader;

static void FDECL(write_tibtile, (int));
static void FDECL(write_tibheader, (FILE *, struct tibhdr_struct *));
static void FDECL(build_tibtile, (pixel(*) [TILE_X], BOOLEAN_P));
static void NDECL(remap_colors);

#ifndef OVERVIEW_FILE
char *tilefiles[] = { "../win/share/monsters.txt", "../win/share/objects.txt",
                      "../win/share/other.txt" };
#else
char *tilefiles[] = { "../win/share/monthin.txt", "../win/share/objthin.txt",
                      "../win/share/oththin.txt" };
#endif

int tilecount;
int filenum;
int paletteflag;

int
main(argc, argv)
int argc;
char *argv[];
{
    int i;
    struct tm *newtime;
    time_t aclock;
    char *paletteptr;
    unsigned num_monsters = 0;

    if (argc != 1) {
        Fprintf(stderr, "usage: tile2bin (from the util directory)\n");
        exit(EXIT_FAILURE);
    }

#ifdef PLANAR_FILE
#ifndef OVERVIEW_FILE
    tibfile1 = fopen(NETHACK_PLANAR_TILEFILE, WRBMODE);
#else
    tibfile1 = fopen(NETHACK_OVERVIEW_TILEFILE, WRBMODE);
#endif
    if (tibfile1 == (FILE *) 0) {
        Fprintf(stderr, "Unable to open output file %s\n",
#ifndef OVERVIEW_FILE
                NETHACK_PLANAR_TILEFILE);
#else
                NETHACK_OVERVIEW_TILEFILE);
#endif
        exit(EXIT_FAILURE);
    }
#endif

#ifdef PACKED_FILE
    tibfile2 = fopen(NETHACK_PACKED_TILEFILE, WRBMODE);
    if (tibfile2 == (FILE *) 0) {
        Fprintf(stderr, "Unable to open output file %s\n",
                NETHACK_PACKED_TILEFILE);
        exit(EXIT_FAILURE);
    }
#endif
    time(&aclock);
    newtime = localtime(&aclock);

    tilecount = 0;
    paletteflag = 0;
    filenum = 0;
    while (filenum < 3) {
        if (!fopen_text_file(tilefiles[filenum], RDTMODE)) {
            Fprintf(stderr,
                    "usage: tile2bin (from the util or src directory)\n");
            exit(EXIT_FAILURE);
        }
        num_colors = colorsinmap;
        if (num_colors > 62) {
            Fprintf(stderr, "too many colors (%d)\n", num_colors);
            exit(EXIT_FAILURE);
        }

        if (!paletteflag) {
            remap_colors();
            paletteptr = tibheader.palette;
            for (i = 0; i < num_colors; i++) {
                *paletteptr++ = ColorMap[CM_RED][i],
                *paletteptr++ = ColorMap[CM_GREEN][i],
                *paletteptr++ = ColorMap[CM_BLUE][i];
            }
            paletteflag++;
        }

        while (read_text_tile(pixels)) {
            build_tibtile(pixels, FALSE);
            write_tibtile(tilecount);
            tilecount++;
        }

        (void) fclose_text_file();
        if (filenum == 0) {
            num_monsters = tilecount;
        }
        ++filenum;
    }

    /* Build the statue glyphs */
    if (!fopen_text_file(tilefiles[0], RDTMODE)) {
        Fprintf(stderr,
                "usage: tile2bin (from the util or src directory)\n");
        exit(EXIT_FAILURE);
    }

    while (read_text_tile(pixels)) {
        build_tibtile(pixels, TRUE);
        write_tibtile(tilecount);
        tilecount++;
    }

    (void) fclose_text_file();

#if defined(_MSC_VER)
    tibheader.compiler = MSC_COMP;
#elif defined(__BORLANDC__)
    tibheader.compiler = BC_COMP;
#elif defined(__GO32__)
    tibheader.compiler = DJGPP_COMP;
#else
    tibheader.compiler = OTHER_COMP;
#endif

    strncpy(tibheader.ident, "NetHack 3.6 MSDOS Port binary tile file", 80);
    strncpy(tibheader.timestamp, asctime(newtime), 24);
    tibheader.timestamp[25] = '\0';
    tibheader.tilecount = tilecount;
    tibheader.numcolors = num_colors;
#ifdef PLANAR_FILE
    tibheader.tilestyle = PLANAR_STYLE;
    write_tibheader(tibfile1, &tibheader);
    (void) fclose(tibfile1);
#ifndef OVERVIEW_FILE
    Fprintf(stderr, "Total of %d planar tiles written to %s.\n", tilecount,
            NETHACK_PLANAR_TILEFILE);
#else
    Fprintf(stderr, "Total of %d planar tiles written to %s.\n", tilecount,
            NETHACK_OVERVIEW_TILEFILE);
#endif
#endif

#ifdef PACKED_FILE
    tibheader.tilestyle = PACKED_STYLE;
    write_tibheader(tibfile2, &tibheader);
    Fprintf(stderr, "Total of %d packed tiles written to %s.\n", tilecount,
            NETHACK_PACKED_TILEFILE);
    (void) fclose(tibfile2);
#endif

    exit(EXIT_SUCCESS);
    /*NOTREACHED*/
    return 0;
}

static void
write_tibheader(fileptr, tibhdr)
FILE *fileptr;
struct tibhdr_struct *tibhdr;
{
    if (fseek(fileptr, 0L, SEEK_SET)) {
        Fprintf(stderr, "Error writing header to tile file\n");
    }
    fwrite(tibhdr, sizeof(struct tibhdr_struct), 1, fileptr);
}

static void
build_tibtile(pixels, statues)
pixel (*pixels)[TILE_X];
boolean statues;
{
    static int graymappings[] = {
        /* .  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  */
        0, 1, 17, 18, 19, 20, 27, 22, 23, 24, 25, 26, 21, 15, 13, 14, 14
    };
    int i, j, k, co_off;
    unsigned char co_mask, tmp;

#ifdef PLANAR_FILE
#ifndef OVERVIEW_FILE
    memset((void *) &planetile, 0, sizeof(struct planar_cell_struct));
#else
    memset((void *) &planetile, 0,
           sizeof(struct overview_planar_cell_struct));
#endif
#endif
    for (j = 0; j < TILE_Y; j++) {
        for (i = 0; i < TILE_X; i++) {
            for (k = 0; k < num_colors; k++) {
                if (ColorMap[CM_RED][k] == pixels[j][i].r
                    && ColorMap[CM_GREEN][k] == pixels[j][i].g
                    && ColorMap[CM_BLUE][k] == pixels[j][i].b)
                    break;
            }
            if (k >= num_colors)
                Fprintf(stderr, "color not in colormap!\n");
            if (statues) {
                k = graymappings[k];
            } else {
                if (k == 16) {
                    k = 13;
                } else if (k == 13) {
                    k = 16;
                }
            }
#ifdef PACKED_FILE
            packtile[j][i] = k;
#endif

#ifdef PLANAR_FILE
            if (i > 7) {
                co_off = 1;
                co_mask = masktable[i - 8];
            } else {
                co_off = 0;
                co_mask = masktable[i];
            }

            if (!statues) {
                if (k == 28) {
                    k = 0;
                }
                if (k >= 16) {
                    fprintf(stderr, "Warning: pixel value %d in 16 color bitmap\n", k);
                }
            }
            tmp = planetile.plane[0].image[j][co_off];
            planetile.plane[0].image[j][co_off] =
                (k & 0x0008) ? (tmp | co_mask) : (tmp & ~co_mask);

            tmp = planetile.plane[1].image[j][co_off];
            planetile.plane[1].image[j][co_off] =
                (k & 0x0004) ? (tmp | co_mask) : (tmp & ~co_mask);

            tmp = planetile.plane[2].image[j][co_off];
            planetile.plane[2].image[j][co_off] =
                (k & 0x0002) ? (tmp | co_mask) : (tmp & ~co_mask);

            tmp = planetile.plane[3].image[j][co_off];
            planetile.plane[3].image[j][co_off] =
                (k & 0x0001) ? (tmp | co_mask) : (tmp & ~co_mask);
#endif /* PLANAR_FILE */
        }
    }
}

static void
write_tibtile(recnum)
int recnum;
{
    long fpos;

#ifdef PLANAR_FILE
#ifndef OVERVIEW_FILE
    fpos = ((long) (recnum) * (long) sizeof(struct planar_cell_struct))
           + (long) TIBHEADER_SIZE;
#else
    fpos =
        ((long) (recnum) * (long) sizeof(struct overview_planar_cell_struct))
        + (long) TIBHEADER_SIZE;
#endif
    if (fseek(tibfile1, fpos, SEEK_SET)) {
        Fprintf(stderr, "Error seeking before planar tile write %d\n",
                recnum);
    }
#ifndef OVERVIEW_FILE
    fwrite(&planetile, sizeof(struct planar_cell_struct), 1, tibfile1);
#else
    fwrite(&planetile, sizeof(struct overview_planar_cell_struct), 1,
           tibfile1);
#endif
#endif

#ifdef PACKED_FILE
    fpos =
        ((long) (recnum) * (long) sizeof(packtile)) + (long) TIBHEADER_SIZE;
    if (fseek(tibfile2, fpos, SEEK_SET)) {
        Fprintf(stderr, "Error seeking before packed tile write %d\n",
                recnum);
    }
    fwrite(&packtile, sizeof(packtile), 1, tibfile2);
#endif
}

static void
remap_colors()
{
    char swap;

    swap = ColorMap[CM_RED][13];
    ColorMap[CM_RED][13] = ColorMap[CM_RED][16];
    ColorMap[CM_RED][16] = swap;
    swap = ColorMap[CM_GREEN][13];
    ColorMap[CM_GREEN][13] = ColorMap[CM_GREEN][16];
    ColorMap[CM_GREEN][16] = swap;
    swap = ColorMap[CM_BLUE][13];
    ColorMap[CM_BLUE][13] = ColorMap[CM_BLUE][16];
    ColorMap[CM_BLUE][16] = swap;
}