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