File: prxytile.c

package info (click to toggle)
slashem 0.0.7E7F3-10
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 24,636 kB
  • sloc: ansic: 263,206; cpp: 7,180; yacc: 2,154; sh: 739; lex: 440; awk: 97; makefile: 63; sed: 11
file content (375 lines) | stat: -rw-r--r-- 10,052 bytes parent folder | download | duplicates (4)
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
/* $Id: prxytile.c,v 1.8 2003/10/25 18:06:01 j_ali Exp $ */
/* Copyright (c) Slash'EM Development Team 2002-2003 */
/* NetHack may be freely redistributed.  See license for details. */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "config.h"
#include "nhxdr.h"
#include "proxycom.h"
#include "prxyclnt.h"
#include "proxycb.h"

/* #define DEBUG */

/*
 * entry is a comma seperated list of descriptions.
 * '\' is treated as an escape character.
 */

static void
proxy_tilemap_add_entry(struct proxy_tilemap *map, int tn, char *entry)
{
    int i, j, k, idx;
    char buf[1024];
    if (map->no_entries >= map->max_entries) {
	if (map->max_entries) {
	    map->max_entries *= 2;
	    map->entries = realloc(map->entries,
		    map->max_entries * sizeof(struct proxy_tilemap_entry));
	} else {
	    map->max_entries = 32;
	    map->entries =
		    malloc(map->max_entries * sizeof(struct proxy_tilemap_entry));
	}
	if (!map->entries)
	    panic("Not enough memory to load tile map");
    }
    idx = map->no_entries++;
    map->entries[idx].refs = 0;
    map->entries[idx].tile = tn;
    for(i = 0, j = 1; entry[i]; i++)
	if (entry[i] == '\\' && entry[i + 1])
	    i++;
	else if (entry[i] == ',')
	    j++;
    map->entries[idx].no_descs = j;
    map->entries[idx].descs = (char **)alloc(j * sizeof(char *));
    for(i = j = k = 0; ; i++)
	if (entry[i] == '\\' && entry[i + 1])
	    buf[k++] = entry[++i];
	else if (entry[i] == ',' || !entry[i]) {
	    map->entries[idx].descs[j] = (char *)alloc(k + 1);
	    strncpy(map->entries[idx].descs[j], buf, k);
	    map->entries[idx].descs[j++][k] = '\0';
	    if (!entry[i])
		break;
	    k = 0;
	}
	else if (k || entry[i] != ' ')
	    buf[k++] = entry[i];
    if (j != map->entries[idx].no_descs)
	panic("Bad description count in proxy_tilemap_set_entry");
#ifdef DEBUG
    fprintf(stderr, "tile %d \"", tn);
    for(i = j = 0; i < map->entries[idx].no_descs; i++) {
	if (!map->entries[idx].descs[i] ||
	  !*map->entries[idx].descs[i])
	    continue;
	if (j)
	    fputs(", ", stderr);
	fputs(map->entries[idx].descs[i], stderr);
	j++;
    }
    fputs("\"\n", stderr);
#endif
}

struct proxy_tilemap *
proxy_new_tilemap(void)
{
    struct proxy_tilemap *map;
    map = (struct proxy_tilemap *)alloc(sizeof(struct proxy_tilemap));
    map->no_entries = map->max_entries = 0;
    map->no_tiles = 0;
    map->entries = NULL;
    return map;
}

int
proxy_load_tilemap_line(struct proxy_tilemap *map, const char *line)
{
    int i, j, k, tn;
    char buf[256];
    char c;
    if (!strncmp(line, "tile ", 5) &&
	    sscanf(line + 5, "%d \"%255[^\"]", &tn, buf) == 2) {
	map->no_tiles ++;
	/* The string consists of alternate descriptions seperated
	 * by '/' characters. Split these up (honouring the '\'
	 * escape) and add each description to the tilemap.
	 * Spaces are removed surrounding '/' characters.
	 */
	for(i = j = 0; ; i++)
	    if (buf[i] == '\\' && buf[i + 1])
		i++;
	    else if (buf[i] == '/' || !buf[i]) {
		for(k = i - 1; buf[k] == ' '; k--)
		    ;
		k++;
		c = buf[k];
		buf[k] = '\0';
		proxy_tilemap_add_entry(map, tn, buf + j);
		buf[k] = c;
		if (!buf[i])
		    break;
		j = i + 1;
		while(buf[j] == ' ')
		    j++;
	    }
	return 0;
    } else
	return -1;		/* unrecognized map commands */
}

struct proxy_tilemap *
proxy_load_tilemap(fh, pulse, pulse_data)
int fh;
void (*pulse)();
void *pulse_data;
{
    char buf[1024];
    struct proxy_tilemap *map;
    map = proxy_new_tilemap();
    while(proxy_cb_dlbh_fgets(buf, 1024, fh)) {
	if (pulse)
	    (*pulse)(pulse_data);
	proxy_load_tilemap_line(map, buf);
    }
    return map;
}

void
proxy_free_tilemap(map)
struct proxy_tilemap *map;
{
    int i, j;
    for(i = 0; i < map->no_entries; i++) {
	for(j = 0; j < map->entries[i].no_descs; j++)
	    free(map->entries[i].descs[j]);
	free(map->entries[i].descs);
    }
    free(map);
}

/*
 * Return the number of ordered matches between the descriptions.
 * Encoded as number of matches in top 16 bits and exact matches in lower 16.
 * This has the effect that a larger number of matches will always win but
 * where there are an equal number of matches, the number of exact matches
 * is taken into account.
 */

static int
proxy_match_descriptions(struct proxy_tilemap_entry *tile_entry,
  struct proxy_glyph_mapping *glyph_desc)
{
    int i, j;
    int no_matches = 0, no_exact_matches = 0;
    int last_match = -1;
    for(i = 0; i < tile_entry->no_descs; i++) {
	if (!tile_entry->descs[i])
	    continue;
	for(j = last_match + 1; j < glyph_desc->no_descs; j++) {
	    if (!glyph_desc->descs[j])
		continue;
	    if (!strcmp(tile_entry->descs[i], glyph_desc->descs[j])) {
		no_matches++;
		no_exact_matches++;
		last_match = j;
		break;
	    } else if (!strcmp(tile_entry->descs[i], "*")) {
		no_matches++;
		last_match = j;
		break;
	    }
	}
    }
    return no_matches << 16 | no_exact_matches;
}

static short
proxy_map_glyph(struct proxy_tilemap *tile_map,
  struct proxy_glyph_mapping *desc)
{
    int i, j;
    int best = -1;
    int best_refs;
    int best_matches;
    for(i = 0; i < tile_map->no_entries; i++) {
	j = proxy_match_descriptions(tile_map->entries + i, desc);
	if (best < 0 || j > best_matches ||
	  j == best_matches && tile_map->entries[i].refs < best_refs) {
	    best = i;
	    best_refs = tile_map->entries[i].refs;
	    best_matches = j;
	}
    }
    if (best >= 0) {
	tile_map->entries[best].refs++;
	return tile_map->entries[best].tile;
    }
    else
	return -1;
}

#ifdef DEBUG
static short
proxy_log_mapping(int glyph, int tile, struct proxy_tilemap *tile_map, struct proxy_glyph_mapping *mapping)
{
    int i, j, k;
    fprintf(stderr, "glyph %d", glyph);
    if (desc) {
	fputs(" \"", stderr);
	for(i = j = 0; i < mapping->no_descs; i++) {
	    if (!mapping->descs[i] || !*mapping->descs[i])
		continue;
	    if (j)
		fputs(", ", stderr);
	    fputs(mapping->descs[i], stderr);
	    j++;
	}
	fputc('"', stderr);
    }
    if (tile >= 0) {
	fprintf(stderr, " mapped to tile %d \"", tile);
	for(k = j = 0; k < tile_map->no_entries; k++) {
	    if (tile_map->entries[k].tile == tile) {
		if (j)
		    fputs(" / ", stderr);
		for(i = j = 0; i < tile_map->entries[k].no_descs; i++) {
		    if (!tile_map->entries[k].descs[i] ||
		      !*tile_map->entries[k].descs[i])
			continue;
		    if (j)
			fputs(", ", stderr);
		    fputs(tile_map->entries[k].descs[i], stderr);
		    j++;
		}
		fprintf(stderr, " {%d}", tile_map->entries[k].refs);
	    }
	}
	fputs("\"\n", stderr);
    } else
	fputs(" not mapped\n", stderr);
}

#define PROXY_MAP_GLYPH(glyph, tile, tile_map, mapping) \
	if (1) { \
	    int PROXY_MAP_GLYPH_gn = (glyph); \
	    glyph2tile[PROXY_MAP_GLYPH_gn] = (tile); \
	    proxy_log_mapping(PROXY_MAP_GLYPH_gn, tile, tile_map, mapping); \
	} else
#else	/* DEBUG */
#define PROXY_MAP_GLYPH(glyph, tile, tile_map, mapping) \
	(glyph2tile[(glyph)] = (tile))
#endif	/* DEBUG */

short *
proxy_map_glyph2tile(glyph_map, tile_map, pulse, pulse_data)
struct proxycb_get_glyph_mapping_res *glyph_map;
struct proxy_tilemap *tile_map;
void (*pulse)();
void *pulse_data;
{
    int i, j, k, m, glyph = 0;
    struct proxy_glyph_map_info info;
    struct proxy_glyph_mapping *mapping;
    struct forward_ref {
	int first_glyph;
	int no_glyphs;
	int ref_glyph;
    };
    int no_forward_refs = 0;
    struct forward_ref *forward_refs = NULL, *fr;
    short *glyph2tile;
    glyph2tile = (short *)alloc(glyph_map->no_glyph * sizeof(short));
    for(i = 0; i < glyph_map->no_glyph; i++)
	glyph2tile[i] = -1;
    mapping = proxy_glyph_map_first(&info, glyph_map);
    while (mapping) {
	if (pulse)
	    (*pulse)(pulse_data);
	/* TODO: Where the tileset defines tiles for a mapping, this should
	 * take precedence over the alternate glyph. Currently, we always
	 * use the alternative glyph, if set.
	 */
	if (mapping->alt_glyph != glyph_map->no_glyph) {
	    m = glyph2tile[mapping->alt_glyph];
	    if (m < 0) {
		/* Referenced glyph has not yet been mapped */
		if (no_forward_refs && fr->ref_glyph == mapping->alt_glyph &&
		  fr->first_glyph + fr->no_glyphs == glyph)
		    fr->no_glyphs++;
		else {
		    if (no_forward_refs++)
			forward_refs = realloc(forward_refs,
			  no_forward_refs * sizeof (struct forward_ref));
		    else
			forward_refs = malloc(sizeof (struct forward_ref));
		    if (!forward_refs)
			panic("Not enough memory to map glyphs");
		    fr = forward_refs + no_forward_refs - 1;
		    fr->first_glyph = glyph;
		    fr->no_glyphs = 1;
		    fr->ref_glyph = mapping->alt_glyph;
		}
		glyph++;
	    }
	    else
		PROXY_MAP_GLYPH(glyph++, m, tile_map, NULL);
	} else {
	    m = proxy_map_glyph(tile_map, mapping);
	    PROXY_MAP_GLYPH(glyph++, m, tile_map, mapping);
	}
	mapping = proxy_glyph_map_next(&info);
    }
    proxy_glyph_map_close(&info);
    /* Handle any forward references (ignoring any that reference undefined
     * glyphs - we treat these just like any other undefined glyphs).
     */
    do {
	k = 0;
	fr = forward_refs;
	for(i = 0; i < no_forward_refs; i++, fr++) {
	    if (!fr->no_glyphs)
		continue;
	    m = glyph2tile[fr->ref_glyph];
	    if (m >= 0) {
		for(j = 0; j < fr->no_glyphs; j++)
		    PROXY_MAP_GLYPH(fr->first_glyph + j, m, tile_map, NULL);
		fr->no_glyphs = 0;
		k = 1;	/* We've done some work */
	    }
	    else if (!k) {
		m = fr->ref_glyph;
		fr = forward_refs;
		for(j = 0; j < no_forward_refs; j++, fr++)
		    if (fr->no_glyphs && m >= fr->first_glyph &&
		      m < fr->first_glyph + fr->no_glyphs)
			break;
		fr = forward_refs + i;
		if (j < no_forward_refs)
		    /* There's work still to do (and we haven't done any) */
		    k = -1;
	    }
	}
	if (k < 0)
	    panic("Cyclic forward references in glyph mapping");
    } while(k);
    free(forward_refs);
    /* Make certain all glyphs map to _something_ */
#if 0	/* FIXME */
    glyph = cmap_to_glyph(S_stone);
#else
    glyph = 0;
#endif
    j = glyph2tile[glyph];
    if (j < 0)
	j = 0;
    for(i = 0; i < glyph_map->no_glyph; i++)
	if (glyph2tile[i] < 0)
	    glyph2tile[i] = j;
    return glyph2tile;
}