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