makemaze.c revision 1.8 1 1.8 joerg /* $NetBSD: makemaze.c,v 1.8 2011/05/23 22:58:44 joerg 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.8 joerg __RCSID("$NetBSD: makemaze.c,v 1.8 2011/05/23 22:58:44 joerg Exp $");
36 1.2 lukem #endif /* not lint */
37 1.2 lukem
38 1.6 dholland #include "hunt.h"
39 1.1 mrg
40 1.6 dholland #define ISCLEAR(y,x) (Maze[y][x] == SPACE)
41 1.6 dholland #define ODD(n) ((n) & 01)
42 1.1 mrg
43 1.8 joerg #if 0
44 1.6 dholland static int candig(int, int);
45 1.6 dholland static void dig(int, int);
46 1.8 joerg #endif
47 1.6 dholland static void dig_maze(int, int);
48 1.6 dholland static void remap(void);
49 1.2 lukem
50 1.2 lukem void
51 1.5 dholland makemaze(void)
52 1.1 mrg {
53 1.6 dholland char *sp;
54 1.6 dholland int y, x;
55 1.1 mrg
56 1.1 mrg /*
57 1.1 mrg * fill maze with walls
58 1.1 mrg */
59 1.1 mrg sp = &Maze[0][0];
60 1.1 mrg while (sp < &Maze[HEIGHT - 1][WIDTH])
61 1.1 mrg *sp++ = DOOR;
62 1.1 mrg
63 1.1 mrg x = rand_num(WIDTH / 2) * 2 + 1;
64 1.1 mrg y = rand_num(HEIGHT / 2) * 2 + 1;
65 1.1 mrg dig_maze(x, y);
66 1.1 mrg remap();
67 1.1 mrg }
68 1.1 mrg
69 1.6 dholland #define NPERM 24
70 1.6 dholland #define NDIR 4
71 1.1 mrg
72 1.8 joerg #if 0
73 1.7 dholland static int dirs[NPERM][NDIR] = {
74 1.6 dholland {0,1,2,3}, {3,0,1,2}, {0,2,3,1}, {0,3,2,1},
75 1.6 dholland {1,0,2,3}, {2,3,0,1}, {0,2,1,3}, {2,3,1,0},
76 1.6 dholland {1,0,3,2}, {1,2,0,3}, {3,1,2,0}, {2,0,3,1},
77 1.6 dholland {1,3,0,2}, {0,3,1,2}, {1,3,2,0}, {2,0,1,3},
78 1.6 dholland {0,1,3,2}, {3,1,0,2}, {2,1,0,3}, {1,2,3,0},
79 1.6 dholland {2,1,3,0}, {3,0,2,1}, {3,2,0,1}, {3,2,1,0}
80 1.6 dholland };
81 1.6 dholland
82 1.7 dholland static int incr[NDIR][2] = {
83 1.6 dholland {0, 1}, {1, 0}, {0, -1}, {-1, 0}
84 1.6 dholland };
85 1.1 mrg
86 1.8 joerg
87 1.2 lukem static void
88 1.5 dholland dig(int y, int x)
89 1.1 mrg {
90 1.6 dholland int *dp;
91 1.6 dholland int *ip;
92 1.6 dholland int ny, nx;
93 1.6 dholland int *endp;
94 1.1 mrg
95 1.1 mrg Maze[y][x] = SPACE; /* Clear this spot */
96 1.1 mrg dp = dirs[rand_num(NPERM)];
97 1.1 mrg endp = &dp[NDIR];
98 1.1 mrg while (dp < endp) {
99 1.1 mrg ip = &incr[*dp++][0];
100 1.1 mrg ny = y + *ip++;
101 1.1 mrg nx = x + *ip;
102 1.1 mrg if (candig(ny, nx))
103 1.1 mrg dig(ny, nx);
104 1.1 mrg }
105 1.1 mrg }
106 1.1 mrg
107 1.1 mrg /*
108 1.1 mrg * candig:
109 1.1 mrg * Is it legal to clear this spot?
110 1.1 mrg */
111 1.2 lukem static int
112 1.5 dholland candig(int y, int x)
113 1.1 mrg {
114 1.6 dholland int i;
115 1.1 mrg
116 1.1 mrg if (ODD(x) && ODD(y))
117 1.1 mrg return FALSE; /* can't touch ODD spots */
118 1.1 mrg
119 1.1 mrg if (y < UBOUND || y >= DBOUND)
120 1.1 mrg return FALSE; /* Beyond vertical bounds, NO */
121 1.1 mrg if (x < LBOUND || x >= RBOUND)
122 1.1 mrg return FALSE; /* Beyond horizontal bounds, NO */
123 1.1 mrg
124 1.1 mrg if (ISCLEAR(y, x))
125 1.1 mrg return FALSE; /* Already clear, NO */
126 1.1 mrg
127 1.1 mrg i = ISCLEAR(y, x + 1);
128 1.1 mrg i += ISCLEAR(y, x - 1);
129 1.1 mrg if (i > 1)
130 1.1 mrg return FALSE; /* Introduces cycle, NO */
131 1.1 mrg i += ISCLEAR(y + 1, x);
132 1.1 mrg if (i > 1)
133 1.1 mrg return FALSE; /* Introduces cycle, NO */
134 1.1 mrg i += ISCLEAR(y - 1, x);
135 1.1 mrg if (i > 1)
136 1.1 mrg return FALSE; /* Introduces cycle, NO */
137 1.1 mrg
138 1.1 mrg return TRUE; /* OK */
139 1.1 mrg }
140 1.8 joerg #endif
141 1.1 mrg
142 1.2 lukem void
143 1.5 dholland dig_maze(int x, int y)
144 1.1 mrg {
145 1.6 dholland int tx, ty;
146 1.6 dholland int i, j;
147 1.6 dholland int order[4];
148 1.6 dholland #define MNORTH 0x1
149 1.6 dholland #define MSOUTH 0x2
150 1.6 dholland #define MEAST 0x4
151 1.6 dholland #define MWEST 0x8
152 1.1 mrg
153 1.2 lukem tx = ty = 0;
154 1.1 mrg Maze[y][x] = SPACE;
155 1.1 mrg order[0] = MNORTH;
156 1.1 mrg for (i = 1; i < 4; i++) {
157 1.1 mrg j = rand_num(i + 1);
158 1.1 mrg order[i] = order[j];
159 1.1 mrg order[j] = 0x1 << i;
160 1.1 mrg }
161 1.1 mrg for (i = 0; i < 4; i++) {
162 1.1 mrg switch (order[i]) {
163 1.1 mrg case MNORTH:
164 1.1 mrg tx = x;
165 1.1 mrg ty = y - 2;
166 1.1 mrg break;
167 1.1 mrg case MSOUTH:
168 1.1 mrg tx = x;
169 1.1 mrg ty = y + 2;
170 1.1 mrg break;
171 1.1 mrg case MEAST:
172 1.1 mrg tx = x + 2;
173 1.1 mrg ty = y;
174 1.1 mrg break;
175 1.1 mrg case MWEST:
176 1.1 mrg tx = x - 2;
177 1.1 mrg ty = y;
178 1.1 mrg break;
179 1.1 mrg }
180 1.1 mrg if (tx < 0 || ty < 0 || tx >= WIDTH || ty >= HEIGHT)
181 1.1 mrg continue;
182 1.1 mrg if (Maze[ty][tx] == SPACE)
183 1.1 mrg continue;
184 1.1 mrg Maze[(y + ty) / 2][(x + tx) / 2] = SPACE;
185 1.1 mrg dig_maze(tx, ty);
186 1.1 mrg }
187 1.1 mrg }
188 1.1 mrg
189 1.2 lukem void
190 1.5 dholland remap(void)
191 1.1 mrg {
192 1.6 dholland int y, x;
193 1.6 dholland char *sp;
194 1.6 dholland int stat;
195 1.1 mrg
196 1.1 mrg for (y = 0; y < HEIGHT; y++)
197 1.1 mrg for (x = 0; x < WIDTH; x++) {
198 1.1 mrg sp = &Maze[y][x];
199 1.1 mrg if (*sp == SPACE)
200 1.1 mrg continue;
201 1.1 mrg stat = 0;
202 1.1 mrg if (y - 1 >= 0 && Maze[y - 1][x] != SPACE)
203 1.1 mrg stat |= NORTH;
204 1.1 mrg if (y + 1 < HEIGHT && Maze[y + 1][x] != SPACE)
205 1.1 mrg stat |= SOUTH;
206 1.1 mrg if (x + 1 < WIDTH && Maze[y][x + 1] != SPACE)
207 1.1 mrg stat |= EAST;
208 1.1 mrg if (x - 1 >= 0 && Maze[y][x - 1] != SPACE)
209 1.1 mrg stat |= WEST;
210 1.1 mrg switch (stat) {
211 1.1 mrg case WEST | EAST:
212 1.1 mrg case EAST:
213 1.1 mrg case WEST:
214 1.1 mrg *sp = WALL1;
215 1.1 mrg break;
216 1.1 mrg case NORTH | SOUTH:
217 1.1 mrg case NORTH:
218 1.1 mrg case SOUTH:
219 1.1 mrg *sp = WALL2;
220 1.1 mrg break;
221 1.1 mrg case 0:
222 1.6 dholland #ifdef RANDOM
223 1.1 mrg *sp = DOOR;
224 1.6 dholland #endif
225 1.6 dholland #ifdef REFLECT
226 1.1 mrg *sp = rand_num(2) ? WALL4 : WALL5;
227 1.6 dholland #endif
228 1.1 mrg break;
229 1.1 mrg default:
230 1.1 mrg *sp = WALL3;
231 1.1 mrg break;
232 1.1 mrg }
233 1.1 mrg }
234 1.1 mrg memcpy(Orig_maze, Maze, sizeof Maze);
235 1.1 mrg }
236