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