|
|
1.1 root 1: /*
2: * Copyright (c) 1985 Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms are permitted
6: * provided that the above copyright notice and this paragraph are
7: * duplicated in all such forms and that any documentation,
8: * advertising materials, and other materials related to such
9: * distribution and use acknowledge that the software was developed
10: * by the University of California, Berkeley. The name of the
11: * University may not be used to endorse or promote products derived
12: * from this software without specific prior written permission.
13: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15: * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16: */
17:
18: #ifndef lint
19: static char sccsid[] = "@(#)makemaze.c 5.2 (Berkeley) 6/27/88";
20: #endif /* not lint */
21:
22: /*
23: * Hunt
24: * Copyright (c) 1985 Conrad C. Huang, Gregory S. Couch, Kenneth C.R.C. Arnold
25: * San Francisco, California
26: */
27:
28: # include "hunt.h"
29:
30: # define ISCLEAR(y,x) (Maze[y][x] == SPACE)
31: # define ODD(n) ((n) & 01)
32:
33: makemaze()
34: {
35: register char *sp;
36: register int y, x;
37:
38: /*
39: * fill maze with walls
40: */
41: sp = &Maze[0][0];
42: while (sp < &Maze[HEIGHT - 1][WIDTH])
43: *sp++ = DOOR;
44:
45: y = rand_num(DBOUND - UBOUND) + UBOUND;
46: x = rand_num(RBOUND - LBOUND) + LBOUND;
47: dig(y, x); /* Dig out the maze */
48: remap();
49: }
50:
51: # define NPERM 24
52: # define NDIR 4
53:
54: int dirs[NPERM][NDIR] = {
55: {0,1,2,3}, {3,0,1,2}, {0,2,3,1}, {0,3,2,1},
56: {1,0,2,3}, {2,3,0,1}, {0,2,1,3}, {2,3,1,0},
57: {1,0,3,2}, {1,2,0,3}, {3,1,2,0}, {2,0,3,1},
58: {1,3,0,2}, {0,3,1,2}, {1,3,2,0}, {2,0,1,3},
59: {0,1,3,2}, {3,1,0,2}, {2,1,0,3}, {1,2,3,0},
60: {2,1,3,0}, {3,0,2,1}, {3,2,0,1}, {3,2,1,0}
61: };
62:
63: int incr[NDIR][2] = {
64: {0, 1}, {1, 0}, {0, -1}, {-1, 0}
65: };
66:
67: dig(y, x)
68: int y, x;
69: {
70: register int *dp;
71: register int *ip;
72: register int ny, nx;
73: register int *endp;
74:
75: Maze[y][x] = SPACE; /* Clear this spot */
76: dp = dirs[rand_num(NPERM)];
77: endp = &dp[NDIR];
78: while (dp < endp) {
79: ip = &incr[*dp++][0];
80: ny = y + *ip++;
81: nx = x + *ip;
82: if (candig(ny, nx))
83: dig(ny, nx);
84: }
85: }
86:
87: /*
88: * candig:
89: * Is it legal to clear this spot?
90: */
91: candig(y, x)
92: register int y, x;
93: {
94: register int i;
95:
96: if (ODD(x) && ODD(y))
97: return FALSE; /* can't touch ODD spots */
98:
99: if (y < UBOUND || y >= DBOUND)
100: return FALSE; /* Beyond vertical bounds, NO */
101: if (x < LBOUND || x >= RBOUND)
102: return FALSE; /* Beyond horizontal bounds, NO */
103:
104: if (ISCLEAR(y, x))
105: return FALSE; /* Already clear, NO */
106:
107: i = ISCLEAR(y, x + 1);
108: i += ISCLEAR(y, x - 1);
109: if (i > 1)
110: return FALSE; /* Introduces cycle, NO */
111: i += ISCLEAR(y + 1, x);
112: if (i > 1)
113: return FALSE; /* Introduces cycle, NO */
114: i += ISCLEAR(y - 1, x);
115: if (i > 1)
116: return FALSE; /* Introduces cycle, NO */
117:
118: return TRUE; /* OK */
119: }
120:
121: remap()
122: {
123: register int y, x;
124: register char *sp;
125: register int stat;
126:
127: for (y = 0; y < HEIGHT; y++)
128: for (x = 0; x < WIDTH; x++) {
129: sp = &Maze[y][x];
130: if (*sp == SPACE)
131: continue;
132: stat = 0;
133: if (y - 1 >= 0 && Maze[y - 1][x] != SPACE)
134: stat |= NORTH;
135: if (y + 1 < HEIGHT && Maze[y + 1][x] != SPACE)
136: stat |= SOUTH;
137: if (x + 1 < WIDTH && Maze[y][x + 1] != SPACE)
138: stat |= EAST;
139: if (x - 1 >= 0 && Maze[y][x - 1] != SPACE)
140: stat |= WEST;
141: switch (stat) {
142: case WEST | EAST:
143: *sp = WALL1;
144: break;
145: case NORTH | SOUTH:
146: *sp = WALL2;
147: break;
148: case 0:
149: # ifdef RANDOM
150: *sp = DOOR;
151: # endif RANDOM
152: # ifdef REFLECT
153: *sp = rand_num(2) ? WALL4 : WALL5;
154: # endif REFLECT
155: break;
156: default:
157: *sp = WALL3;
158: break;
159: }
160: }
161: bcopy((char *) Maze, (char *) Orig_maze, sizeof Maze);
162: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.