ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/meta/cvmaze.c
Revision: 1.1
Committed: Sat Feb 22 02:07:26 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2     static const char RCSid[] = "$Id$";
3     #endif
4     /*
5     * THE MAZE GAME definitions and global structures
6     *
7     * Written by Greg Ward 8/4/84
8     */
9    
10    
11     #include "meta.h"
12    
13    
14     #define ERROR (-1) /* something wrong */
15    
16     #define OK 0 /* everything swell */
17    
18     #define TRUE 1
19    
20     #define FALSE 0
21    
22     #define UP 1
23    
24     #define DOWN 2
25    
26     #define LEFT 3
27    
28     #define RIGHT 4
29    
30     #define min(a, b) ((a) < (b) ? (a) : (b))
31    
32     #define max(a, b) ((a) > (b) ? (a) : (b))
33    
34     #define VSEP '|' /* vertical separator */
35    
36     #define HSEP '-' /* horizontal separator */
37    
38     #define ISECT '+' /* intersection character */
39    
40     #define MARKS " ^v<>*" /* maze markings */
41    
42     #define TUP 1 /* mark for trace up */
43    
44     #define TDOWN 2 /* mark for trace down */
45    
46     #define TLEFT 3 /* mark for trace left */
47    
48     #define TRIGHT 4 /* mark for trace right */
49    
50     #define MAN 5 /* mark for player */
51    
52     #define MAXSIDE 50 /* maximum maze side */
53    
54     #define YES 1 /* edge is there */
55    
56     #define NO 0 /* edge isn't there */
57    
58     #define MAYBE 2 /* undecided */
59    
60    
61    
62    
63    
64     #define DIRECTION char
65    
66     #define EDGE char
67    
68     #define MRKINDX char
69    
70     struct MAZE {
71     EDGE r0[MAXSIDE+1][MAXSIDE],
72     c0[MAXSIDE+1][MAXSIDE];
73    
74     int nrows, ncols;
75    
76     MRKINDX mark[MAXSIDE][MAXSIDE];
77    
78     int rt, rb, cl, cr;
79    
80     };
81    
82    
83    
84     /*
85     * Convert maze to metafile
86     */
87    
88    
89    
90    
91    
92     struct MAZE amaze; /* our working maze */
93    
94     int cellsize; /* the size of a maze cell (in metacoordinates) */
95    
96     char *progname;
97    
98     #define CPMEOF 0x1a
99    
100    
101     main(argc, argv)
102    
103     int argc;
104     char **argv;
105    
106     {
107     FILE *fp;
108    
109     #ifdef CPM
110     fixargs("cvmaze", &argc, &argv);
111     #endif
112     progname = *argv++;
113     argc--;
114    
115     if (argc)
116    
117     for ( ; argc; argc--, argv++) {
118     if ((fp = fopen(*argv, "r")) == NULL) {
119     fprintf(stderr, "%s: no such file\n", *argv);
120     exit(1);
121     }
122     execute(fp);
123     fclose(fp);
124     }
125    
126     else
127    
128     execute(stdin);
129    
130     writeof(stdout);
131    
132     return(0);
133     }
134    
135    
136    
137    
138     execute(fp) /* process file fp */
139    
140     FILE *fp;
141    
142     {
143    
144     while (readmaze(fp, &amaze, MARKS) != ERROR) {
145    
146     cellsize = (XYSIZE-1)/max(amaze.nrows, amaze.ncols);
147     printmaze(stdout, &amaze, MARKS);
148     pglob(PEOP, 0200, NULL);
149     }
150    
151     }
152    
153    
154    
155    
156    
157     printmaze(fp, mzp, mrks) /* print maze to fp */
158    
159     FILE *fp;
160     struct MAZE *mzp;
161     char *mrks;
162    
163     {
164     int i;
165    
166     for (i = mzp->rt; i < mzp->rb; i++) {
167    
168     putrline(fp, mzp, i);
169    
170     putcline(fp, mzp, i, mrks);
171    
172     }
173    
174     putrline(fp, mzp, mzp->rb);
175    
176     }
177    
178    
179    
180    
181     putrline(fp, mzp, row) /* print horizontal row to fp */
182    
183     FILE *fp;
184     struct MAZE *mzp;
185     int row;
186    
187     {
188     register int j;
189    
190     for (j = mzp->cl; j < mzp->cr; j++)
191     if (mzp->r0[row][j] == YES)
192     plseg(0, j*cellsize, (XYSIZE-1)-row*cellsize, (j+1)*cellsize, (XYSIZE-1)-row*cellsize);
193    
194     }
195    
196    
197    
198    
199    
200     putcline(fp, mzp, row, mrks) /* print column line to fp */
201    
202     FILE *fp;
203     struct MAZE *mzp;
204     int row;
205     char mrks[];
206    
207     {
208     register int j;
209     char s[2];
210    
211     s[1] = '\0';
212    
213     for (j = mzp->cl; j <= mzp->cr; j++)
214     if (mzp->c0[j][row] == YES)
215     plseg(0, j*cellsize, (XYSIZE-1)-row*cellsize, j*cellsize, (XYSIZE-1)-(row+1)*cellsize);
216    
217     }
218    
219    
220    
221    
222    
223     readmaze(fp, mzp, mrks) /* read maze from fp into mzp */
224    
225     FILE *fp;
226     struct MAZE *mzp;
227     char *mrks;
228    
229     {
230     int i;
231    
232     mzp->rt = mzp->rb = mzp->cl = mzp->cr = mzp->nrows = mzp ->ncols = 0;
233    
234     if ((mzp->cr = mzp->ncols = getrline(fp, mzp, 0)) == 0)
235     return(ERROR);
236    
237     for (i = 0; i <= MAXSIDE; i++) {
238    
239     if (getcline(fp, mzp, i, mrks) != mzp->ncols)
240     break;
241    
242     if (getrline(fp, mzp, i+1) != mzp->ncols)
243     return(ERROR);
244    
245     }
246    
247     if (i > MAXSIDE)
248     return(ERROR);
249    
250     mzp->rb = mzp->nrows = i;
251     return(OK);
252     }
253    
254    
255    
256     getrline(fp, mzp, row) /* get row from fp */
257    
258     FILE *fp;
259     struct MAZE *mzp;
260     int row;
261    
262     {
263     int j, nc;
264     char *fgets(), linbuf[4 + 4*MAXSIDE];
265    
266     if (fgets(linbuf, sizeof(linbuf), fp) == NULL)
267     return(0);
268    
269     nc = (strlen(linbuf) - 2) / 4;
270    
271     for (j = 0; j < nc; j++)
272    
273     switch (linbuf[2 + 4*j]) {
274    
275     case HSEP:
276     mzp->r0[row][j] = YES;
277     break;
278    
279     case ' ':
280     mzp->r0[row][j] = NO;
281     break;
282    
283     default:
284     return(0);
285     break;
286    
287     }
288    
289     return(nc);
290     }
291    
292    
293    
294    
295    
296     getcline(fp, mzp, row, mrks) /* get column line from fp */
297    
298     FILE *fp;
299     struct MAZE *mzp;
300     int row;
301     char mrks[];
302    
303     {
304     int j, nc, k;
305     char *fgets(), linbuf[4 + 4*MAXSIDE];
306    
307     if (fgets(linbuf, sizeof(linbuf), fp) == NULL)
308     return(0);
309    
310     nc = (strlen(linbuf) - 2) / 4;
311    
312     for (j = 0; j < nc; j++) {
313    
314     switch (linbuf[4*j]) {
315    
316     case VSEP:
317     mzp->c0[j][row] = YES;
318     break;
319    
320     case ' ':
321     mzp->c0[j][row] = NO;
322     break;
323    
324     default:
325     return(0);
326     break;
327    
328     }
329    
330     for (k = 0; mrks[k]; k++)
331    
332     if (linbuf[2 + 4*j] == mrks[k]) {
333     mzp->mark[row][j] = k;
334     break;
335     }
336    
337     if (!mrks[k])
338     return(0);
339    
340     }
341    
342     switch (linbuf[4*nc]) {
343    
344     case VSEP:
345     mzp->c0[nc][row] = YES;
346     break;
347    
348     case ' ':
349     mzp->c0[nc][row] = NO;
350     break;
351    
352     default:
353     return(0);
354     break;
355    
356     }
357    
358     return(nc);
359     }