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