1 /* aNetHack 0.0.1 tiletext.c $ANH-Date: 1466687974 2016/06/23 13:19:34 $ $ANH-Branch: aNetHack-3.6.0 $:$ANH-Revision: 1.14 $ */
2 /* aNetHack may be freely redistributed. See license for details. */
7 pixval ColorMap
[3][MAXCOLORMAPSIZE
];
9 pixval MainColorMap
[3][MAXCOLORMAPSIZE
];
12 static short color_index
[MAXCOLORMAPSIZE
];
13 static int num_colors
;
14 static char charcolors
[MAXCOLORMAPSIZE
];
16 static int placeholder_init
= 0;
17 static pixel placeholder
[TILE_Y
][TILE_X
];
18 static FILE *tile_file
;
19 static int tile_set
, tile_set_indx
;
21 static const char *text_sets
[] = { "monthin.txt", "objthin.txt",
24 static const char *text_sets
[] = { "monsters.txt", "objects.txt",
28 extern const char *FDECL(tilename
, (int, int));
29 extern boolean
FDECL(acceptable_tilename
, (int, const char *, const char *));
30 static void FDECL(read_text_colormap
, (FILE *));
31 static boolean
FDECL(write_text_colormap
, (FILE *));
32 static boolean
FDECL(read_txttile
, (FILE *, pixel (*)[TILE_X
]));
33 static void FDECL(write_txttile
, (FILE *, pixel (*)[TILE_X
]));
35 /* Ugh. DICE doesn't like %[A-Z], so we have to spell it out... */
36 #define FORMAT_STRING \
37 "%[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.] = " \
40 static int grayscale
= 0;
41 /* grayscale color mapping */
42 static const int graymappings
[] = {
43 /* . A B C D E F G H I J K L M N O P */
44 0, 1, 17, 18, 19, 20, 27, 22, 23, 24, 25, 26, 21, 15, 13, 14, 14
55 read_text_colormap(txtfile
)
61 for (i
= 0; i
< MAXCOLORMAPSIZE
; i
++)
65 while (fscanf(txtfile
, FORMAT_STRING
, c
, &r
, &g
, &b
) == 4) {
66 color_index
[(int) c
[0]] = num_colors
;
67 ColorMap
[CM_RED
][num_colors
] = r
;
68 ColorMap
[CM_GREEN
][num_colors
] = g
;
69 ColorMap
[CM_BLUE
][num_colors
] = b
;
72 colorsinmap
= num_colors
;
78 write_text_colormap(txtfile
)
84 num_colors
= colorsinmainmap
;
85 if (num_colors
> 62) {
86 Fprintf(stderr
, "too many colors (%d)\n", num_colors
);
89 for (i
= 0; i
< num_colors
; i
++) {
99 txtfile
, "%c = (%d, %d, %d)\n", c
, (int) MainColorMap
[CM_RED
][i
],
100 (int) MainColorMap
[CM_GREEN
][i
], (int) MainColorMap
[CM_BLUE
][i
]);
106 read_txttile(txtfile
, pixels
)
108 pixel (*pixels
)[TILE_X
];
111 char buf
[BUFSZ
], ttype
[BUFSZ
];
115 if (fscanf(txtfile
, "# %s %d (%[^)])", ttype
, &i
, buf
) <= 0)
118 ph
= strcmp(ttype
, "placeholder") == 0;
120 if (!ph
&& strcmp(ttype
, "tile") != 0)
121 Fprintf(stderr
, "Keyword \"%s\" unexpected for entry %d\n", ttype
, i
);
124 /* check tile name, but not relative number, which will
125 * change when tiles are added
127 p
= tilename(tile_set
, tile_set_indx
);
128 if (p
&& strcmp(p
, buf
) && !acceptable_tilename(tile_set_indx
,buf
,p
)) {
129 Fprintf(stderr
, "warning: for tile %d (numbered %d) of %s,\n",
130 tile_set_indx
, i
, text_sets
[tile_set
- 1]);
131 Fprintf(stderr
, "\tfound '%s' while expecting '%s'\n", buf
, p
);
136 /* look for non-whitespace at each stage */
137 if (fscanf(txtfile
, "%1s", c
) < 0) {
138 Fprintf(stderr
, "unexpected EOF\n");
142 Fprintf(stderr
, "didn't find expected '{'\n");
145 for (j
= 0; j
< TILE_Y
; j
++) {
146 for (i
= 0; i
< TILE_X
; i
++) {
147 if (fscanf(txtfile
, "%1s", c
) < 0) {
148 Fprintf(stderr
, "unexpected EOF\n");
151 k
= color_index
[(int) c
[0]];
153 if (k
> (SIZE(graymappings
) - 1))
154 Fprintf(stderr
, "Gray mapping issue %d > %d.\n", k
,
155 SIZE(graymappings
) - 1);
160 Fprintf(stderr
, "color %c not in colormap!\n", c
[0]);
162 pixels
[j
][i
].r
= ColorMap
[CM_RED
][k
];
163 pixels
[j
][i
].g
= ColorMap
[CM_GREEN
][k
];
164 pixels
[j
][i
].b
= ColorMap
[CM_BLUE
][k
];
169 /* remember it for later */
170 memcpy(placeholder
, pixels
, sizeof(placeholder
));
172 if (fscanf(txtfile
, "%1s ", c
) < 0) {
173 Fprintf(stderr
, "unexpected EOF\n");
177 Fprintf(stderr
, "didn't find expected '}'\n");
181 /* DICE again... it doesn't seem to eat whitespace after the } like
182 * it should, so we have to do so manually.
184 while ((*c
= fgetc(txtfile
)) != EOF
&& isspace((uchar
) *c
))
192 write_txttile(txtfile
, pixels
)
194 pixel (*pixels
)[TILE_X
];
200 if (memcmp(placeholder
, pixels
, sizeof(placeholder
)) == 0)
201 type
= "placeholder";
206 Fprintf(txtfile
, "# %s %d (unknown)\n", type
, tile_set_indx
);
208 p
= tilename(tile_set
, tile_set_indx
);
210 Fprintf(txtfile
, "# %s %d (%s)\n", type
, tile_set_indx
, p
);
212 Fprintf(txtfile
, "# %s %d (null)\n", type
, tile_set_indx
);
216 Fprintf(txtfile
, "{\n");
217 for (j
= 0; j
< TILE_Y
; j
++) {
218 Fprintf(txtfile
, " ");
219 for (i
= 0; i
< TILE_X
; i
++) {
220 for (k
= 0; k
< num_colors
; k
++) {
221 if (ColorMap
[CM_RED
][k
] == pixels
[j
][i
].r
222 && ColorMap
[CM_GREEN
][k
] == pixels
[j
][i
].g
223 && ColorMap
[CM_BLUE
][k
] == pixels
[j
][i
].b
)
227 Fprintf(stderr
, "color not in colormap!\n");
228 (void) fputc(charcolors
[k
], txtfile
);
230 Fprintf(txtfile
, "\n");
232 Fprintf(txtfile
, "}\n");
235 /* initialize main colormap from globally accessed ColorMap */
241 colorsinmainmap
= colorsinmap
;
242 for (i
= 0; i
< colorsinmap
; i
++) {
243 MainColorMap
[CM_RED
][i
] = ColorMap
[CM_RED
][i
];
244 MainColorMap
[CM_GREEN
][i
] = ColorMap
[CM_GREEN
][i
];
245 MainColorMap
[CM_BLUE
][i
] = ColorMap
[CM_BLUE
][i
];
249 /* merge new colors from ColorMap into MainColorMap */
255 for (i
= 0; i
< colorsinmap
; i
++) {
256 for (j
= 0; j
< colorsinmainmap
; j
++) {
257 if (MainColorMap
[CM_RED
][j
] == ColorMap
[CM_RED
][i
]
258 && MainColorMap
[CM_GREEN
][j
] == ColorMap
[CM_GREEN
][i
]
259 && MainColorMap
[CM_BLUE
][j
] == ColorMap
[CM_BLUE
][i
])
262 if (j
>= colorsinmainmap
) { /* new color */
263 if (colorsinmainmap
>= MAXCOLORMAPSIZE
) {
265 "Too many colors to merge -- excess ignored.\n");
268 MainColorMap
[CM_RED
][j
] = ColorMap
[CM_RED
][i
];
269 MainColorMap
[CM_GREEN
][j
] = ColorMap
[CM_GREEN
][i
];
270 MainColorMap
[CM_BLUE
][j
] = ColorMap
[CM_BLUE
][i
];
277 fopen_text_file(filename
, type
)
278 const char *filename
;
284 if (tile_file
!= (FILE *) 0) {
285 Fprintf(stderr
, "can only open one text file at at time\n");
289 tile_file
= fopen(filename
, type
);
290 if (tile_file
== (FILE *) 0) {
291 Fprintf(stderr
, "cannot open text file %s\n", filename
);
295 p
= rindex(filename
, '/');
302 for (i
= 0; i
< SIZE(text_sets
); i
++) {
303 if (!strcmp(p
, text_sets
[i
]))
308 if (!strcmp(type
, RDTMODE
)) {
309 /* Fill placeholder with noise */
310 if (!placeholder_init
) {
312 for (i
= 0; i
< (int) sizeof placeholder
; i
++)
313 ((char *) placeholder
)[i
] = i
% 256;
316 read_text_colormap(tile_file
);
317 if (!colorsinmainmap
)
322 } else if (!strcmp(type
, WRTMODE
)) {
323 if (!colorsinmainmap
) {
324 Fprintf(stderr
, "no colormap set yet\n");
327 return (write_text_colormap(tile_file
));
329 Fprintf(stderr
, "bad mode (%s) for fopen_text_file\n", type
);
335 read_text_tile(pixels
)
336 pixel (*pixels
)[TILE_X
];
338 return (read_txttile(tile_file
, pixels
));
342 write_text_tile(pixels
)
343 pixel (*pixels
)[TILE_X
];
345 write_txttile(tile_file
, pixels
);
354 ret
= fclose(tile_file
);
355 tile_file
= (FILE *) 0;