]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hunt/huntd/makemaze.c
1 /* $NetBSD: makemaze.c,v 1.12 2021/05/02 12:50:45 rillig Exp $ */
3 * Copyright (c) 1983-2003, Regents of the University of California.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * + Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * + Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * + Neither the name of the University of California, San Francisco nor
16 * the names of its contributors may be used to endorse or promote
17 * products derived from this software without specific prior written
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: makemaze.c,v 1.12 2021/05/02 12:50:45 rillig Exp $");
40 #define ISCLEAR(y,x) (Maze[y][x] == SPACE)
41 #define ODD(n) ((n) & 01)
48 static const int dirs
[NPERM
][NDIR
] = {
49 {0,1,2,3}, {3,0,1,2}, {0,2,3,1}, {0,3,2,1},
50 {1,0,2,3}, {2,3,0,1}, {0,2,1,3}, {2,3,1,0},
51 {1,0,3,2}, {1,2,0,3}, {3,1,2,0}, {2,0,3,1},
52 {1,3,0,2}, {0,3,1,2}, {1,3,2,0}, {2,0,1,3},
53 {0,1,3,2}, {3,1,0,2}, {2,1,0,3}, {1,2,3,0},
54 {2,1,3,0}, {3,0,2,1}, {3,2,0,1}, {3,2,1,0}
57 static const int incr
[NDIR
][2] = {
58 {0, 1}, {1, 0}, {0, -1}, {-1, 0}
64 * Is it legal to clear this spot?
72 return false; /* can't touch ODD spots */
74 if (y
< UBOUND
|| y
>= DBOUND
)
75 return false; /* Beyond vertical bounds, NO */
76 if (x
< LBOUND
|| x
>= RBOUND
)
77 return false; /* Beyond horizontal bounds, NO */
80 return false; /* Already clear, NO */
82 i
= ISCLEAR(y
, x
+ 1);
83 i
+= ISCLEAR(y
, x
- 1);
85 return false; /* Introduces cycle, NO */
86 i
+= ISCLEAR(y
+ 1, x
);
88 return false; /* Introduces cycle, NO */
89 i
+= ISCLEAR(y
- 1, x
);
91 return false; /* Introduces cycle, NO */
104 Maze
[y
][x
] = SPACE
; /* Clear this spot */
105 dp
= dirs
[rand_num(NPERM
)];
108 ip
= &incr
[*dp
++][0];
118 dig_maze(int x
, int y
)
131 for (i
= 1; i
< 4; i
++) {
136 for (i
= 0; i
< 4; i
++) {
155 if (tx
< 0 || ty
< 0 || tx
>= WIDTH
|| ty
>= HEIGHT
)
157 if (Maze
[ty
][tx
] == SPACE
)
159 Maze
[(y
+ ty
) / 2][(x
+ tx
) / 2] = SPACE
;
171 for (y
= 0; y
< HEIGHT
; y
++)
172 for (x
= 0; x
< WIDTH
; x
++) {
177 if (y
- 1 >= 0 && Maze
[y
- 1][x
] != SPACE
)
179 if (y
+ 1 < HEIGHT
&& Maze
[y
+ 1][x
] != SPACE
)
181 if (x
+ 1 < WIDTH
&& Maze
[y
][x
+ 1] != SPACE
)
183 if (x
- 1 >= 0 && Maze
[y
][x
- 1] != SPACE
)
201 *sp
= rand_num(2) ? WALL4
: WALL5
;
209 memcpy(Orig_maze
, Maze
, sizeof Maze
);
219 * fill maze with walls
222 while (sp
< &Maze
[HEIGHT
- 1][WIDTH
])
225 x
= rand_num(WIDTH
/ 2) * 2 + 1;
226 y
= rand_num(HEIGHT
/ 2) * 2 + 1;