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