Bring hunt in from OpenBSD. The best multi-player terminal game ever!
[dragonfly.git] / games / hunt / huntd / makemaze.c
1 /*
2  * Copyright (c) 1983-2003, Regents of the University of California.
3  * All rights reserved.
4  * 
5  * Redistribution and use in source and binary forms, with or without 
6  * modification, are permitted provided that the following conditions are 
7  * met:
8  * 
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 
17  *   permission.
18  * 
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.
30  *
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.1 2008/09/02 21:50:21 dillon Exp $
34  */
35
36 #include <string.h>
37
38 #include "hunt.h"
39 #include "server.h"
40 #include "conf.h"
41
42 # define        ISCLEAR(y,x)    (Maze[y][x] == SPACE)
43 # define        ODD(n)          ((n) & 01)
44
45 static  int     candig(int, int);
46 static  void    dig(int, int);
47 static  void    dig_maze(int, int);
48 static  void    remap(void);
49
50 void
51 makemaze()
52 {
53         char    *sp;
54         int     y, x;
55
56         /*
57          * fill maze with walls
58          */
59         sp = &Maze[0][0];
60         while (sp < &Maze[HEIGHT - 1][WIDTH])
61                 *sp++ = DOOR;
62
63         x = rand_num(WIDTH / 2) * 2 + 1;
64         y = rand_num(HEIGHT / 2) * 2 + 1;
65         dig_maze(x, y);
66         remap();
67 }
68
69 # define        NPERM   24
70 # define        NDIR    4
71
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}
79         };
80
81 int     incr[NDIR][2] = {
82                 {0, 1}, {1, 0}, {0, -1}, {-1, 0}
83         };
84
85 static void
86 dig(y, x)
87         int     y, x;
88 {
89         int     *dp;
90         int     *ip;
91         int     ny, nx;
92         int     *endp;
93
94         Maze[y][x] = SPACE;                     /* Clear this spot */
95         dp = dirs[rand_num(NPERM)];
96         endp = &dp[NDIR];
97         while (dp < endp) {
98                 ip = &incr[*dp++][0];
99                 ny = y + *ip++;
100                 nx = x + *ip;
101                 if (candig(ny, nx))
102                         dig(ny, nx);
103         }
104 }
105
106 /*
107  * candig:
108  *      Is it legal to clear this spot?
109  */
110 static int
111 candig(y, x)
112         int     y, x;
113 {
114         int     i;
115
116         if (ODD(x) && ODD(y))
117                 return FALSE;           /* can't touch ODD spots */
118
119         if (y < UBOUND || y >= DBOUND)
120                 return FALSE;           /* Beyond vertical bounds, NO */
121         if (x < LBOUND || x >= RBOUND)
122                 return FALSE;           /* Beyond horizontal bounds, NO */
123
124         if (ISCLEAR(y, x))
125                 return FALSE;           /* Already clear, NO */
126
127         i = ISCLEAR(y, x + 1);
128         i += ISCLEAR(y, x - 1);
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         i += ISCLEAR(y - 1, x);
135         if (i > 1)
136                 return FALSE;           /* Introduces cycle, NO */
137
138         return TRUE;                    /* OK */
139 }
140
141 static void
142 dig_maze(x, y)
143         int     x, y;
144 {
145         int     tx, ty;
146         int     i, j;
147         int     order[4];
148 #define MNORTH  0x1
149 #define MSOUTH  0x2
150 #define MEAST   0x4
151 #define MWEST   0x8
152
153         tx = ty = 0;
154         Maze[y][x] = SPACE;
155         order[0] = MNORTH;
156         for (i = 1; i < 4; i++) {
157                 j = rand_num(i + 1);
158                 order[i] = order[j];
159                 order[j] = 0x1 << i;
160         }
161         for (i = 0; i < 4; i++) {
162                 switch (order[i]) {
163                   case MNORTH:
164                         tx = x;
165                         ty = y - 2;
166                         break;
167                   case MSOUTH:
168                         tx = x;
169                         ty = y + 2;
170                         break;
171                   case MEAST:
172                         tx = x + 2;
173                         ty = y;
174                         break;
175                   case MWEST:
176                         tx = x - 2;
177                         ty = y;
178                         break;
179                 }
180                 if (tx < 0 || ty < 0 || tx >= WIDTH || ty >= HEIGHT)
181                         continue;
182                 if (Maze[ty][tx] == SPACE)
183                         continue;
184                 Maze[(y + ty) / 2][(x + tx) / 2] = SPACE;
185                 dig_maze(tx, ty);
186         }
187 }
188
189 static void
190 remap()
191 {
192         int     y, x;
193         char    *sp;
194         int     stat;
195
196         for (y = 0; y < HEIGHT; y++)
197                 for (x = 0; x < WIDTH; x++) {
198                         sp = &Maze[y][x];
199                         if (*sp == SPACE)
200                                 continue;
201                         /* Find occupied adjacent cells. */
202                         stat = 0;
203                         if (y - 1 >= 0 && Maze[y - 1][x] != SPACE)
204                                 stat |= NORTH;
205                         if (y + 1 < HEIGHT && Maze[y + 1][x] != SPACE)
206                                 stat |= SOUTH;
207                         if (x + 1 < WIDTH && Maze[y][x + 1] != SPACE)
208                                 stat |= EAST;
209                         if (x - 1 >= 0 && Maze[y][x - 1] != SPACE)
210                                 stat |= WEST;
211                         switch (stat) {
212                           case WEST | EAST:
213                           case EAST:
214                           case WEST:
215                                 *sp = WALL1;                       /* - */
216                                 break;
217                           case NORTH | SOUTH:
218                           case NORTH:
219                           case SOUTH:
220                                 *sp = WALL2;                       /* | */
221                                 break;
222                           case 0:
223                                 if (conf_random)
224                                         *sp = DOOR;
225                                 if (conf_reflect)
226                                         *sp = rand_num(2) ? WALL4 : WALL5;
227                                 break;
228                           default:
229                                 *sp = WALL3;                       /* + */
230                                 break;
231                         }
232                 }
233         memcpy(Orig_maze, Maze, sizeof Orig_maze);
234 }