makemaze.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* $NetBSD: makemaze.c,v 1.4 2004/01/27 20:30:29 jsm Exp $ */
  2. /*
  3. * Copyright (c) 1983-2003, Regents of the University of California.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  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
  18. * permission.
  19. *
  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.
  31. */
  32. #include <sys/cdefs.h>
  33. #ifndef lint
  34. __RCSID("$NetBSD: makemaze.c,v 1.4 2004/01/27 20:30:29 jsm Exp $");
  35. #endif /* not lint */
  36. # include "hunt.h"
  37. # define ISCLEAR(y,x) (Maze[y][x] == SPACE)
  38. # define ODD(n) ((n) & 01)
  39. static int candig(int, int);
  40. static void dig(int, int);
  41. static void dig_maze(int, int);
  42. static void remap(void);
  43. void
  44. makemaze()
  45. {
  46. char *sp;
  47. int y, x;
  48. /*
  49. * fill maze with walls
  50. */
  51. sp = &Maze[0][0];
  52. while (sp < &Maze[HEIGHT - 1][WIDTH])
  53. *sp++ = DOOR;
  54. x = rand_num(WIDTH / 2) * 2 + 1;
  55. y = rand_num(HEIGHT / 2) * 2 + 1;
  56. dig_maze(x, y);
  57. remap();
  58. }
  59. # define NPERM 24
  60. # define NDIR 4
  61. int dirs[NPERM][NDIR] = {
  62. {0,1,2,3}, {3,0,1,2}, {0,2,3,1}, {0,3,2,1},
  63. {1,0,2,3}, {2,3,0,1}, {0,2,1,3}, {2,3,1,0},
  64. {1,0,3,2}, {1,2,0,3}, {3,1,2,0}, {2,0,3,1},
  65. {1,3,0,2}, {0,3,1,2}, {1,3,2,0}, {2,0,1,3},
  66. {0,1,3,2}, {3,1,0,2}, {2,1,0,3}, {1,2,3,0},
  67. {2,1,3,0}, {3,0,2,1}, {3,2,0,1}, {3,2,1,0}
  68. };
  69. int incr[NDIR][2] = {
  70. {0, 1}, {1, 0}, {0, -1}, {-1, 0}
  71. };
  72. static void
  73. dig(y, x)
  74. int y, x;
  75. {
  76. int *dp;
  77. int *ip;
  78. int ny, nx;
  79. int *endp;
  80. Maze[y][x] = SPACE; /* Clear this spot */
  81. dp = dirs[rand_num(NPERM)];
  82. endp = &dp[NDIR];
  83. while (dp < endp) {
  84. ip = &incr[*dp++][0];
  85. ny = y + *ip++;
  86. nx = x + *ip;
  87. if (candig(ny, nx))
  88. dig(ny, nx);
  89. }
  90. }
  91. /*
  92. * candig:
  93. * Is it legal to clear this spot?
  94. */
  95. static int
  96. candig(y, x)
  97. int y, x;
  98. {
  99. int i;
  100. if (ODD(x) && ODD(y))
  101. return FALSE; /* can't touch ODD spots */
  102. if (y < UBOUND || y >= DBOUND)
  103. return FALSE; /* Beyond vertical bounds, NO */
  104. if (x < LBOUND || x >= RBOUND)
  105. return FALSE; /* Beyond horizontal bounds, NO */
  106. if (ISCLEAR(y, x))
  107. return FALSE; /* Already clear, NO */
  108. i = ISCLEAR(y, x + 1);
  109. i += ISCLEAR(y, x - 1);
  110. if (i > 1)
  111. return FALSE; /* Introduces cycle, NO */
  112. i += ISCLEAR(y + 1, x);
  113. if (i > 1)
  114. return FALSE; /* Introduces cycle, NO */
  115. i += ISCLEAR(y - 1, x);
  116. if (i > 1)
  117. return FALSE; /* Introduces cycle, NO */
  118. return TRUE; /* OK */
  119. }
  120. void
  121. dig_maze(x, y)
  122. int x, y;
  123. {
  124. int tx, ty;
  125. int i, j;
  126. int order[4];
  127. #define MNORTH 0x1
  128. #define MSOUTH 0x2
  129. #define MEAST 0x4
  130. #define MWEST 0x8
  131. tx = ty = 0;
  132. Maze[y][x] = SPACE;
  133. order[0] = MNORTH;
  134. for (i = 1; i < 4; i++) {
  135. j = rand_num(i + 1);
  136. order[i] = order[j];
  137. order[j] = 0x1 << i;
  138. }
  139. for (i = 0; i < 4; i++) {
  140. switch (order[i]) {
  141. case MNORTH:
  142. tx = x;
  143. ty = y - 2;
  144. break;
  145. case MSOUTH:
  146. tx = x;
  147. ty = y + 2;
  148. break;
  149. case MEAST:
  150. tx = x + 2;
  151. ty = y;
  152. break;
  153. case MWEST:
  154. tx = x - 2;
  155. ty = y;
  156. break;
  157. }
  158. if (tx < 0 || ty < 0 || tx >= WIDTH || ty >= HEIGHT)
  159. continue;
  160. if (Maze[ty][tx] == SPACE)
  161. continue;
  162. Maze[(y + ty) / 2][(x + tx) / 2] = SPACE;
  163. dig_maze(tx, ty);
  164. }
  165. }
  166. void
  167. remap()
  168. {
  169. int y, x;
  170. char *sp;
  171. int stat;
  172. for (y = 0; y < HEIGHT; y++)
  173. for (x = 0; x < WIDTH; x++) {
  174. sp = &Maze[y][x];
  175. if (*sp == SPACE)
  176. continue;
  177. stat = 0;
  178. if (y - 1 >= 0 && Maze[y - 1][x] != SPACE)
  179. stat |= NORTH;
  180. if (y + 1 < HEIGHT && Maze[y + 1][x] != SPACE)
  181. stat |= SOUTH;
  182. if (x + 1 < WIDTH && Maze[y][x + 1] != SPACE)
  183. stat |= EAST;
  184. if (x - 1 >= 0 && Maze[y][x - 1] != SPACE)
  185. stat |= WEST;
  186. switch (stat) {
  187. case WEST | EAST:
  188. case EAST:
  189. case WEST:
  190. *sp = WALL1;
  191. break;
  192. case NORTH | SOUTH:
  193. case NORTH:
  194. case SOUTH:
  195. *sp = WALL2;
  196. break;
  197. case 0:
  198. # ifdef RANDOM
  199. *sp = DOOR;
  200. # endif
  201. # ifdef REFLECT
  202. *sp = rand_num(2) ? WALL4 : WALL5;
  203. # endif
  204. break;
  205. default:
  206. *sp = WALL3;
  207. break;
  208. }
  209. }
  210. memcpy(Orig_maze, Maze, sizeof Maze);
  211. }