makemaze.c revision 1.1.1.2 1 /*
2 * Copyright (c) 1983-2003, Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * + Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * + Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * + Neither the name of the University of California, San Francisco nor
15 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 # include "hunt.h"
33
34 # define ISCLEAR(y,x) (Maze[y][x] == SPACE)
35 # define ODD(n) ((n) & 01)
36
37 makemaze()
38 {
39 register char *sp;
40 register int y, x;
41
42 /*
43 * fill maze with walls
44 */
45 sp = &Maze[0][0];
46 while (sp < &Maze[HEIGHT - 1][WIDTH])
47 *sp++ = DOOR;
48
49 x = rand_num(WIDTH / 2) * 2 + 1;
50 y = rand_num(HEIGHT / 2) * 2 + 1;
51 dig_maze(x, y);
52 remap();
53 }
54
55 # define NPERM 24
56 # define NDIR 4
57
58 int dirs[NPERM][NDIR] = {
59 {0,1,2,3}, {3,0,1,2}, {0,2,3,1}, {0,3,2,1},
60 {1,0,2,3}, {2,3,0,1}, {0,2,1,3}, {2,3,1,0},
61 {1,0,3,2}, {1,2,0,3}, {3,1,2,0}, {2,0,3,1},
62 {1,3,0,2}, {0,3,1,2}, {1,3,2,0}, {2,0,1,3},
63 {0,1,3,2}, {3,1,0,2}, {2,1,0,3}, {1,2,3,0},
64 {2,1,3,0}, {3,0,2,1}, {3,2,0,1}, {3,2,1,0}
65 };
66
67 int incr[NDIR][2] = {
68 {0, 1}, {1, 0}, {0, -1}, {-1, 0}
69 };
70
71 dig(y, x)
72 int y, x;
73 {
74 register int *dp;
75 register int *ip;
76 register int ny, nx;
77 register int *endp;
78
79 Maze[y][x] = SPACE; /* Clear this spot */
80 dp = dirs[rand_num(NPERM)];
81 endp = &dp[NDIR];
82 while (dp < endp) {
83 ip = &incr[*dp++][0];
84 ny = y + *ip++;
85 nx = x + *ip;
86 if (candig(ny, nx))
87 dig(ny, nx);
88 }
89 }
90
91 /*
92 * candig:
93 * Is it legal to clear this spot?
94 */
95 candig(y, x)
96 register int y, x;
97 {
98 register int i;
99
100 if (ODD(x) && ODD(y))
101 return FALSE; /* can't touch ODD spots */
102
103 if (y < UBOUND || y >= DBOUND)
104 return FALSE; /* Beyond vertical bounds, NO */
105 if (x < LBOUND || x >= RBOUND)
106 return FALSE; /* Beyond horizontal bounds, NO */
107
108 if (ISCLEAR(y, x))
109 return FALSE; /* Already clear, NO */
110
111 i = ISCLEAR(y, x + 1);
112 i += ISCLEAR(y, x - 1);
113 if (i > 1)
114 return FALSE; /* Introduces cycle, NO */
115 i += ISCLEAR(y + 1, x);
116 if (i > 1)
117 return FALSE; /* Introduces cycle, NO */
118 i += ISCLEAR(y - 1, x);
119 if (i > 1)
120 return FALSE; /* Introduces cycle, NO */
121
122 return TRUE; /* OK */
123 }
124
125 dig_maze(x, y)
126 int x, y;
127 {
128 register int tx, ty;
129 register int i, j;
130 int order[4];
131 #define MNORTH 0x1
132 #define MSOUTH 0x2
133 #define MEAST 0x4
134 #define MWEST 0x8
135
136 Maze[y][x] = SPACE;
137 order[0] = MNORTH;
138 for (i = 1; i < 4; i++) {
139 j = rand_num(i + 1);
140 order[i] = order[j];
141 order[j] = 0x1 << i;
142 }
143 for (i = 0; i < 4; i++) {
144 switch (order[i]) {
145 case MNORTH:
146 tx = x;
147 ty = y - 2;
148 break;
149 case MSOUTH:
150 tx = x;
151 ty = y + 2;
152 break;
153 case MEAST:
154 tx = x + 2;
155 ty = y;
156 break;
157 case MWEST:
158 tx = x - 2;
159 ty = y;
160 break;
161 }
162 if (tx < 0 || ty < 0 || tx >= WIDTH || ty >= HEIGHT)
163 continue;
164 if (Maze[ty][tx] == SPACE)
165 continue;
166 Maze[(y + ty) / 2][(x + tx) / 2] = SPACE;
167 dig_maze(tx, ty);
168 }
169 }
170
171 remap()
172 {
173 register int y, x;
174 register char *sp;
175 register int stat;
176
177 for (y = 0; y < HEIGHT; y++)
178 for (x = 0; x < WIDTH; x++) {
179 sp = &Maze[y][x];
180 if (*sp == SPACE)
181 continue;
182 stat = 0;
183 if (y - 1 >= 0 && Maze[y - 1][x] != SPACE)
184 stat |= NORTH;
185 if (y + 1 < HEIGHT && Maze[y + 1][x] != SPACE)
186 stat |= SOUTH;
187 if (x + 1 < WIDTH && Maze[y][x + 1] != SPACE)
188 stat |= EAST;
189 if (x - 1 >= 0 && Maze[y][x - 1] != SPACE)
190 stat |= WEST;
191 switch (stat) {
192 case WEST | EAST:
193 case EAST:
194 case WEST:
195 *sp = WALL1;
196 break;
197 case NORTH | SOUTH:
198 case NORTH:
199 case SOUTH:
200 *sp = WALL2;
201 break;
202 case 0:
203 # ifdef RANDOM
204 *sp = DOOR;
205 # endif
206 # ifdef REFLECT
207 *sp = rand_num(2) ? WALL4 : WALL5;
208 # endif
209 break;
210 default:
211 *sp = WALL3;
212 break;
213 }
214 }
215 memcpy(Orig_maze, Maze, sizeof Maze);
216 }
217