2 * Copyright (c) 1983-2003, Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
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
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.
31 * $OpenBSD: makemaze.c,v 1.6 2003/06/11 08:45:33 pjanzen Exp $
32 * $NetBSD: makemaze.c,v 1.2 1997/10/10 16:33:43 lukem Exp $
33 * $DragonFly: src/games/hunt/huntd/makemaze.c,v 1.2 2008/09/04 16:12:51 swildner Exp $
42 # define ISCLEAR(y,x) (Maze[y][x] == SPACE)
43 # define ODD(n) ((n) & 01)
45 static int candig(int, int);
46 static void dig(int, int);
47 static void dig_maze(int, int);
48 static void remap(void);
57 * fill maze with walls
60 while (sp < &Maze[HEIGHT - 1][WIDTH])
63 x = rand_num(WIDTH / 2) * 2 + 1;
64 y = rand_num(HEIGHT / 2) * 2 + 1;
72 int dirs[NPERM][NDIR] = {
73 {0,1,2,3}, {3,0,1,2}, {0,2,3,1}, {0,3,2,1},
74 {1,0,2,3}, {2,3,0,1}, {0,2,1,3}, {2,3,1,0},
75 {1,0,3,2}, {1,2,0,3}, {3,1,2,0}, {2,0,3,1},
76 {1,3,0,2}, {0,3,1,2}, {1,3,2,0}, {2,0,1,3},
77 {0,1,3,2}, {3,1,0,2}, {2,1,0,3}, {1,2,3,0},
78 {2,1,3,0}, {3,0,2,1}, {3,2,0,1}, {3,2,1,0}
82 {0, 1}, {1, 0}, {0, -1}, {-1, 0}
93 Maze[y][x] = SPACE; /* Clear this spot */
94 dp = dirs[rand_num(NPERM)];
107 * Is it legal to clear this spot?
114 if (ODD(x) && ODD(y))
115 return FALSE; /* can't touch ODD spots */
117 if (y < UBOUND || y >= DBOUND)
118 return FALSE; /* Beyond vertical bounds, NO */
119 if (x < LBOUND || x >= RBOUND)
120 return FALSE; /* Beyond horizontal bounds, NO */
123 return FALSE; /* Already clear, NO */
125 i = ISCLEAR(y, x + 1);
126 i += ISCLEAR(y, x - 1);
128 return FALSE; /* Introduces cycle, NO */
129 i += ISCLEAR(y + 1, x);
131 return FALSE; /* Introduces cycle, NO */
132 i += ISCLEAR(y - 1, x);
134 return FALSE; /* Introduces cycle, NO */
136 return TRUE; /* OK */
140 dig_maze(int x, int y)
153 for (i = 1; i < 4; i++) {
158 for (i = 0; i < 4; i++) {
177 if (tx < 0 || ty < 0 || tx >= WIDTH || ty >= HEIGHT)
179 if (Maze[ty][tx] == SPACE)
181 Maze[(y + ty) / 2][(x + tx) / 2] = SPACE;
193 for (y = 0; y < HEIGHT; y++)
194 for (x = 0; x < WIDTH; x++) {
198 /* Find occupied adjacent cells. */
200 if (y - 1 >= 0 && Maze[y - 1][x] != SPACE)
202 if (y + 1 < HEIGHT && Maze[y + 1][x] != SPACE)
204 if (x + 1 < WIDTH && Maze[y][x + 1] != SPACE)
206 if (x - 1 >= 0 && Maze[y][x - 1] != SPACE)
223 *sp = rand_num(2) ? WALL4 : WALL5;
230 memcpy(Orig_maze, Maze, sizeof Orig_maze);