1 /* NetHack 3.6 tiletext.c $NHDT-Date: 1524689272 2018/04/25 20:47:52 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.16 $ */
2 /* Copyright (c) 2016 by Pasi Kallinen */
3 /* NetHack may be freely redistributed. See license for details. */
8 pixval ColorMap[3][MAXCOLORMAPSIZE];
10 pixval MainColorMap[3][MAXCOLORMAPSIZE];
13 static short color_index[MAXCOLORMAPSIZE];
14 static int num_colors;
15 static char charcolors[MAXCOLORMAPSIZE];
17 static int placeholder_init = 0;
18 static pixel placeholder[TILE_Y][TILE_X];
19 static FILE *tile_file;
20 static int tile_set, tile_set_indx;
22 static const char *text_sets[] = { "monthin.txt", "objthin.txt",
25 static const char *text_sets[] = { "monsters.txt", "objects.txt",
29 extern const char *FDECL(tilename, (int, int));
30 extern boolean FDECL(acceptable_tilename, (int, const char *, const char *));
31 static void FDECL(read_text_colormap, (FILE *));
32 static boolean FDECL(write_text_colormap, (FILE *));
33 static boolean FDECL(read_txttile, (FILE *, pixel (*)[TILE_X]));
34 static void FDECL(write_txttile, (FILE *, pixel (*)[TILE_X]));
36 /* Ugh. DICE doesn't like %[A-Z], so we have to spell it out... */
37 #define FORMAT_STRING \
38 "%[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.] = " \
41 static int grayscale = 0;
42 /* grayscale color mapping */
43 static const int graymappings[] = {
44 /* . A B C D E F G H I J K L M N O P */
45 0, 1, 17, 18, 19, 20, 27, 22, 23, 24, 25, 26, 21, 15, 13, 14, 14
56 read_text_colormap(txtfile)
62 for (i = 0; i < MAXCOLORMAPSIZE; i++)
66 while (fscanf(txtfile, FORMAT_STRING, c, &r, &g, &b) == 4) {
67 color_index[(int) c[0]] = num_colors;
68 ColorMap[CM_RED][num_colors] = r;
69 ColorMap[CM_GREEN][num_colors] = g;
70 ColorMap[CM_BLUE][num_colors] = b;
73 colorsinmap = num_colors;
79 write_text_colormap(txtfile)
85 num_colors = colorsinmainmap;
86 if (num_colors > 62) {
87 Fprintf(stderr, "too many colors (%d)\n", num_colors);
90 for (i = 0; i < num_colors; i++) {
100 txtfile, "%c = (%d, %d, %d)\n", c, (int) MainColorMap[CM_RED][i],
101 (int) MainColorMap[CM_GREEN][i], (int) MainColorMap[CM_BLUE][i]);
107 read_txttile(txtfile, pixels)
109 pixel (*pixels)[TILE_X];
112 char buf[BUFSZ], ttype[BUFSZ];
116 if (fscanf(txtfile, "# %s %d (%[^)])", ttype, &i, buf) <= 0)
119 ph = strcmp(ttype, "placeholder") == 0;
121 if (!ph && strcmp(ttype, "tile") != 0)
122 Fprintf(stderr, "Keyword \"%s\" unexpected for entry %d\n", ttype, i);
125 /* check tile name, but not relative number, which will
126 * change when tiles are added
128 p = tilename(tile_set, tile_set_indx);
129 if (p && strcmp(p, buf) && !acceptable_tilename(tile_set_indx,buf,p)) {
130 Fprintf(stderr, "warning: for tile %d (numbered %d) of %s,\n",
131 tile_set_indx, i, text_sets[tile_set - 1]);
132 Fprintf(stderr, "\tfound '%s' while expecting '%s'\n", buf, p);
137 /* look for non-whitespace at each stage */
138 if (fscanf(txtfile, "%1s", c) < 0) {
139 Fprintf(stderr, "unexpected EOF\n");
143 Fprintf(stderr, "didn't find expected '{'\n");
146 for (j = 0; j < TILE_Y; j++) {
147 for (i = 0; i < TILE_X; i++) {
148 if (fscanf(txtfile, "%1s", c) < 0) {
149 Fprintf(stderr, "unexpected EOF\n");
152 k = color_index[(int) c[0]];
154 if (k > (SIZE(graymappings) - 1))
155 Fprintf(stderr, "Gray mapping issue %d > %d.\n", k,
156 SIZE(graymappings) - 1);
161 Fprintf(stderr, "color %c not in colormap!\n", c[0]);
163 pixels[j][i].r = ColorMap[CM_RED][k];
164 pixels[j][i].g = ColorMap[CM_GREEN][k];
165 pixels[j][i].b = ColorMap[CM_BLUE][k];
170 /* remember it for later */
171 memcpy(placeholder, pixels, sizeof(placeholder));
173 if (fscanf(txtfile, "%1s ", c) < 0) {
174 Fprintf(stderr, "unexpected EOF\n");
178 Fprintf(stderr, "didn't find expected '}'\n");
182 /* DICE again... it doesn't seem to eat whitespace after the } like
183 * it should, so we have to do so manually.
185 while ((*c = fgetc(txtfile)) != EOF && isspace((uchar) *c))
193 write_txttile(txtfile, pixels)
195 pixel (*pixels)[TILE_X];
201 if (memcmp(placeholder, pixels, sizeof(placeholder)) == 0)
202 type = "placeholder";
207 Fprintf(txtfile, "# %s %d (unknown)\n", type, tile_set_indx);
209 p = tilename(tile_set, tile_set_indx);
211 Fprintf(txtfile, "# %s %d (%s)\n", type, tile_set_indx, p);
213 Fprintf(txtfile, "# %s %d (null)\n", type, tile_set_indx);
217 Fprintf(txtfile, "{\n");
218 for (j = 0; j < TILE_Y; j++) {
219 Fprintf(txtfile, " ");
220 for (i = 0; i < TILE_X; i++) {
221 for (k = 0; k < num_colors; k++) {
222 if (ColorMap[CM_RED][k] == pixels[j][i].r
223 && ColorMap[CM_GREEN][k] == pixels[j][i].g
224 && ColorMap[CM_BLUE][k] == pixels[j][i].b)
228 Fprintf(stderr, "color not in colormap!\n");
229 (void) fputc(charcolors[k], txtfile);
231 Fprintf(txtfile, "\n");
233 Fprintf(txtfile, "}\n");
236 /* initialize main colormap from globally accessed ColorMap */
242 colorsinmainmap = colorsinmap;
243 for (i = 0; i < colorsinmap; i++) {
244 MainColorMap[CM_RED][i] = ColorMap[CM_RED][i];
245 MainColorMap[CM_GREEN][i] = ColorMap[CM_GREEN][i];
246 MainColorMap[CM_BLUE][i] = ColorMap[CM_BLUE][i];
250 /* merge new colors from ColorMap into MainColorMap */
256 for (i = 0; i < colorsinmap; i++) {
257 for (j = 0; j < colorsinmainmap; j++) {
258 if (MainColorMap[CM_RED][j] == ColorMap[CM_RED][i]
259 && MainColorMap[CM_GREEN][j] == ColorMap[CM_GREEN][i]
260 && MainColorMap[CM_BLUE][j] == ColorMap[CM_BLUE][i])
263 if (j >= colorsinmainmap) { /* new color */
264 if (colorsinmainmap >= MAXCOLORMAPSIZE) {
266 "Too many colors to merge -- excess ignored.\n");
269 MainColorMap[CM_RED][j] = ColorMap[CM_RED][i];
270 MainColorMap[CM_GREEN][j] = ColorMap[CM_GREEN][i];
271 MainColorMap[CM_BLUE][j] = ColorMap[CM_BLUE][i];
278 fopen_text_file(filename, type)
279 const char *filename;
285 if (tile_file != (FILE *) 0) {
286 Fprintf(stderr, "can only open one text file at at time\n");
290 tile_file = fopen(filename, type);
291 if (tile_file == (FILE *) 0) {
292 Fprintf(stderr, "cannot open text file %s\n", filename);
296 p = rindex(filename, '/');
303 for (i = 0; i < SIZE(text_sets); i++) {
304 if (!strcmp(p, text_sets[i]))
309 if (!strcmp(type, RDTMODE)) {
310 /* Fill placeholder with noise */
311 if (!placeholder_init) {
313 for (i = 0; i < (int) sizeof placeholder; i++)
314 ((char *) placeholder)[i] = i % 256;
317 read_text_colormap(tile_file);
318 if (!colorsinmainmap)
323 } else if (!strcmp(type, WRTMODE)) {
324 if (!colorsinmainmap) {
325 Fprintf(stderr, "no colormap set yet\n");
328 return (write_text_colormap(tile_file));
330 Fprintf(stderr, "bad mode (%s) for fopen_text_file\n", type);
336 read_text_tile(pixels)
337 pixel (*pixels)[TILE_X];
339 return (read_txttile(tile_file, pixels));
343 write_text_tile(pixels)
344 pixel (*pixels)[TILE_X];
346 write_txttile(tile_file, pixels);
355 ret = fclose(tile_file);
356 tile_file = (FILE *) 0;