File: pathfile.c

package info (click to toggle)
xgalaga 2.0.34-41
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,568 kB
  • ctags: 1,222
  • sloc: ansic: 15,819; sh: 2,634; perl: 816; makefile: 155
file content (412 lines) | stat: -rw-r--r-- 7,968 bytes parent folder | download
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
/* $Id: pathfile.c,v 1.3 1998/04/30 05:11:56 mrogre Exp $ */
/* Copyright (c) 1998 Joe Rumsey (mrogre@mediaone.net) */
#include "copyright.h"
#include <config.h>


#include <stdio.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <sys/stat.h>
#include <ctype.h>
#include <stdlib.h>

#include "paths.h"
#include "pathfile.h"
#include "data.h"
#include "defs.h"

#define MAXFILENAME 1024

#define MAXPATHLEN 50
#define MAXLINE 1024

typedef enum {
    PF_PATHDEF,
    PF_PATHS,
    PF_SHAPES,
    PF_DELAYS
} PathfileToken;

struct path_entry
{
    int dir, duration;
};

struct path_info
{
    int startx, starty, len;
};

struct path_entry epaths[NUMPATHS][MAXPATHLEN];
struct path_info  pathinfo[NUMPATHS];

int al_shapes[MAXALIENS];
int al_delays[MAXALIENS];
int al_paths[MAXALIENS];

int
get_line(FILE* file, char* buf)
{
    int done = 0;
    char nextline[MAXLINE];

    buf[0] = 0;
    while(!done)
    {
	if(!fgets(nextline, MAXLINE, file))
	    return 0;

	/* remove newline */
	nextline[strlen(nextline) - 1] = 0;

	if(strlen(nextline) + strlen(buf) >= MAXLINE) {
	    fprintf(stderr, "get_line: Line too long\n");
	    return -1;
	}
	
	strcpy(buf + strlen(buf), nextline);

	if(nextline[strlen(nextline) -1] != '\\')
	    done = 1;
	else {
	    buf[strlen(buf) -1] = ' ';
	}
    }
    return 1;
}

PathfileToken
get_token(char* line, int* elem, char** data)
{
    PathfileToken pt;
    char *datastart;

    *elem = -1;
    
    if(strncmp(line, "paths:", 6) == 0) {	
	pt = PF_PATHS;
    } else if (strncmp(line, "path.", 5) == 0) {
	if(line[5] >= '0' && line[5] <= '9')
	    *elem = line[5] - '0';
	else if(line[5] >= 'a' && line[5] <= 'z')
	    *elem = line[5] - 'a' + 10;
	else if(line[5] >= 'A' && line[5] <= 'Z')
	    *elem = line[5] - 'A' + 36;
	else {
	    fprintf(stderr, "%c is not a valid path identifier\n", line[5]);
	    return -1;
	}
	pt = PF_PATHDEF;
    } else if (strncmp(line, "delays:", 7) == 0) {
	pt = PF_DELAYS;
    } else if (strncmp(line, "shapes:", 7) == 0) {
	pt = PF_SHAPES;
    } else {
	return -1;
    }

    datastart = strchr(line, ':');
    if(!datastart)
	return -1;
    datastart++;

    while(isspace(*datastart))
	datastart++;

    if(!(*datastart))
	return -1;

    *data = datastart;
    return pt;

}

#define iscompass(c) (((c) == 'n') || ((c) == 'e') || ((c) == 's') || ((c) == 'w'))
#define CHKDIR(x) \
  curdir++; if(strcmp(x, dir) == 0) { gotdir = curdir; }

static void
parse_path(int pnum, char* pstr)
{
    char *fnd;
    char dir[40];
    int dp;
    int curdir = -1, gotdir;
    int duration;
    int curentry;
    int sx, sy;
    char *comma;

    curentry = 0;

    comma = strchr(pstr, ',');
    sx = atoi(pstr);
    sy = atoi(comma+1);
    fnd = strchr(pstr, ':');
    while(!iscompass(*fnd) && *fnd)
	fnd++;

    if(!(*fnd))
	fprintf(stderr, "No path info for path %d\n", pnum);

    while(*fnd) {
	dp = 0;
	while(iscompass(*fnd) && dp < 3) {
	    dir[dp] = *fnd;
	    fnd++;
	    dp++;
	}
	if(!isdigit(*fnd)) {
	    fprintf(stderr, "Error parsing path: %s\n", pstr);
	    return;
	}
	dir[dp] = 0;
	curdir = -1;
	gotdir = -1;

	CHKDIR("n");
	CHKDIR("nne");
	CHKDIR("ne");
	CHKDIR("ene");
	CHKDIR("e");
	CHKDIR("ese");
	CHKDIR("se");
	CHKDIR("sse");
	CHKDIR("s");
	CHKDIR("ssw");
	CHKDIR("sw");
	CHKDIR("wsw");
	CHKDIR("w");
	CHKDIR("wnw");
	CHKDIR("nw");
	CHKDIR("nnw");

	if(gotdir < 0) {
	    fprintf(stderr, "direction %s in path %d is not valid\n",
		    dir, pnum);
	}

	duration = atoi(fnd);
	
	epaths[pnum][curentry].dir = gotdir;
	epaths[pnum][curentry].duration = duration;
	curentry++;
	while(isdigit(*fnd) || isspace(*fnd))
	    fnd++;
    }
    epaths[pnum][curentry].dir = -1;
    epaths[pnum][curentry].duration = -1;

    pathinfo[pnum].len = curentry;
    pathinfo[pnum].startx = sx;
    pathinfo[pnum].starty = sy;
}

static int
parse_shapes(char* data)
{
    char* chk;
    int i;

    for(i = 0, chk = data; i < MAXALIENS; i++)
    {
	while(isspace(*chk)) chk++;
	if(!(*chk))
	    return 0;

	if(*chk >= '0' && *chk <= '9')
	    al_shapes[i] = *chk - '0';
	else if(*chk >= 'a' && *chk <= 'z')
	    al_shapes[i] = *chk - 'a' + 10;
	else if(*chk >= 'A' && *chk <= 'Z')
	    al_shapes[i] = *chk - 'A' + 10 + 26;
	else if(*chk == '-')
	    al_shapes[i] = -1;
	else {
	    fprintf(stderr, "'%c' is not a valid shape character\n", *chk);
	    al_shapes[i] = 0;
	}

	chk++;
    }

    return 1;
}

static int
parse_paths(char* data)
{
    char* chk;
    int i;

    for(i = 0, chk = data; i < MAXALIENS; i++)
    {
	while(isspace(*chk)) chk++;
	if(!(*chk))
	    return 0;

	if(*chk >= '0' && *chk <= '9')
	    al_paths[i] = *chk - '0';
	else if(*chk >= 'a' && *chk <= 'z')
	    al_paths[i] = *chk - 'a' + 10;
	else if(*chk >= 'A' && *chk <= 'Z')
	    al_paths[i] = *chk - 'A' + 10 + 26;
	else if(*chk == '-')
	    al_paths[i] = -1;
	else {
	    fprintf(stderr, "'%c' is not a valid shape character\n", *chk);
	    al_paths[i] = 0;
	}

	chk++;
    }

    return 1;
}

static int
parse_delays(char* data)
{
    char* chk;
    int i;

    for(i = 0, chk = data; i < MAXALIENS; i++)
    {
	while(isspace(*chk)) chk++;
	if(!(*chk))
	    return 0;

	al_delays[i] = atoi(chk);
	while(!isspace(*chk) && *chk) chk++;
    }

    return 1;
}

int
read_level(int lev)
{
    char filename[MAXFILENAME];
    char readline[MAXLINE];
    char *data;
    int  gotPaths = 0, gotShapes = 0, gotDelays = 0;
    int rlen;
    int elem;
    FILE* lf;
    PathfileToken tok;
    static int maxLevel = 1;
    static char *real_level_path = 0;
    struct stat statbuf;

    if(!real_level_path)
    {
	sprintf(filename, "%s", LEVELDIR);
	if(stat(filename, &statbuf) != 0 || !(statbuf.st_mode & S_IFDIR)) {
	    sprintf(filename, "./levels");
	    if(stat(filename, &statbuf) != 0 || !(statbuf.st_mode & S_IFDIR)) {
		fprintf(stderr, "Can't find level directory %s OR ./levels\n",
			XGALAGADIR);
		return -1;
	    }
	}
	real_level_path = malloc(strlen(filename) + 1);
	strcpy(real_level_path, filename);
    }

    sprintf(filename, "%s/level%d.xgl", real_level_path, lev);
    lf = fopen(filename, "r");
    if(!lf) {
	/* Dont use recursion, to avoid stack overflow if start level is
	 * very high (or the game is played for a _very_ long time. :-) 
	 * -- JEH */
	for (lev = lev - maxLevel; lev > 0 && ! lf ;lev = lev - maxLevel) {
		metaLevel++;
		/* real_level_path is already set, so.. */
		sprintf(filename, "%s/level%d.xgl", real_level_path, lev);
		lf = fopen(filename, "r");
	}
	
        //fprintf(stderr, "Can't open level file %s\n", filename);
	//return read_level(lev - maxLevel);
    }

    if(lev > maxLevel)
	maxLevel = lev;

    while(!feof(lf)) {
	if((rlen = get_line(lf, readline)) <= 0) {
	    if(gotShapes && gotPaths && gotDelays) {
		return 1;
	    }
	    fprintf(stderr, "Error reading level file %s:\n", filename);
	    if(!gotShapes)
		fprintf(stderr, "  Missing shapes\n");
	    if(!gotPaths)
		fprintf(stderr, "  Missing paths\n");
	    if(!gotDelays)
		fprintf(stderr, "  Missing delays\n");

	    fclose(lf);
	    return 0;
	}
	
	switch((tok = get_token(readline, &elem, &data))) {
	case PF_PATHDEF:
	    parse_path(elem, data);
	    break;
	case PF_SHAPES:
	    gotShapes = parse_shapes(data);
	    break;
	case PF_PATHS:
	    gotPaths = parse_paths(data);
	    break;
	case PF_DELAYS:
	    gotDelays = parse_delays(data);
	    break;
	default:
	    break;
	}
    }

    fclose(lf);
    
    if(gotShapes && gotPaths && gotDelays) {
	return 1;
    }

    return 0;
}

int get_path(int anum)
{
    return al_paths[anum];
}

void get_xy(int anum, int* x, int* y)
{
    *x = (WINWIDTH  * pathinfo[al_paths[anum]].startx) / 400;
    *y = (WINHEIGHT * pathinfo[al_paths[anum]].starty) / 500;
}

int get_delay(int anum)
{
    return al_delays[anum];
}

int get_dir(int pnum, int pos)
{
    return epaths[pnum][pos].dir;
}

int get_duration(int pnum, int pos)
{
    return epaths[pnum][pos].duration;
}

int get_shape(int anum)
{
    return al_shapes[anum];
}