Home | History | Annotate | Line # | Download | only in hack
hack.mklev.c revision 1.5
      1  1.5       jsm /*	$NetBSD: hack.mklev.c,v 1.5 2001/03/25 20:44:01 jsm Exp $	*/
      2  1.4  christos 
      3  1.2   mycroft /*
      4  1.2   mycroft  * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
      5  1.2   mycroft  */
      6  1.2   mycroft 
      7  1.4  christos #include <sys/cdefs.h>
      8  1.2   mycroft #ifndef lint
      9  1.5       jsm __RCSID("$NetBSD: hack.mklev.c,v 1.5 2001/03/25 20:44:01 jsm Exp $");
     10  1.4  christos #endif				/* not lint */
     11  1.1       cgd 
     12  1.4  christos #include <unistd.h>
     13  1.4  christos #include <stdlib.h>
     14  1.1       cgd #include "hack.h"
     15  1.4  christos #include "extern.h"
     16  1.1       cgd 
     17  1.1       cgd #define somex() ((random()%(croom->hx-croom->lx+1))+croom->lx)
     18  1.1       cgd #define somey() ((random()%(croom->hy-croom->ly+1))+croom->ly)
     19  1.1       cgd 
     20  1.1       cgd #include "def.mkroom.h"
     21  1.4  christos #define	XLIM	4		/* define minimum required space around a
     22  1.4  christos 				 * room */
     23  1.1       cgd #define	YLIM	3
     24  1.4  christos boolean         secret;		/* TRUE while making a vault: increase
     25  1.4  christos 				 * [XY]LIM */
     26  1.4  christos int             smeq[MAXNROFROOMS + 1];
     27  1.4  christos int             doorindex;
     28  1.4  christos struct rm       zerorm;
     29  1.4  christos schar           nxcor;
     30  1.4  christos boolean         goldseen;
     31  1.4  christos int             nroom;
     32  1.1       cgd 
     33  1.1       cgd /* Definitions used by makerooms() and addrs() */
     34  1.4  christos #define	MAXRS	50		/* max lth of temp rectangle table -
     35  1.4  christos 				 * arbitrary */
     36  1.1       cgd struct rectangle {
     37  1.4  christos 	xchar           rlx, rly, rhx, rhy;
     38  1.4  christos }               rs[MAXRS + 1];
     39  1.4  christos int             rscnt, rsmax;	/* 0..rscnt-1: currently under consideration */
     40  1.4  christos /* rscnt..rsmax: discarded */
     41  1.1       cgd 
     42  1.4  christos void
     43  1.1       cgd makelevel()
     44  1.1       cgd {
     45  1.4  christos 	struct mkroom  *croom, *troom;
     46  1.4  christos 	unsigned        tryct;
     47  1.4  christos 	int x, y;
     48  1.1       cgd 
     49  1.1       cgd 	nroom = 0;
     50  1.1       cgd 	doorindex = 0;
     51  1.1       cgd 	rooms[0].hx = -1;	/* in case we are in a maze */
     52  1.1       cgd 
     53  1.4  christos 	for (x = 0; x < COLNO; x++)
     54  1.4  christos 		for (y = 0; y < ROWNO; y++)
     55  1.4  christos 			levl[x][y] = zerorm;
     56  1.1       cgd 
     57  1.4  christos 	oinit();		/* assign level dependent obj probabilities */
     58  1.1       cgd 
     59  1.4  christos 	if (dlevel >= rn1(3, 26)) {	/* there might be several mazes */
     60  1.1       cgd 		makemaz();
     61  1.1       cgd 		return;
     62  1.1       cgd 	}
     63  1.1       cgd 	/* construct the rooms */
     64  1.1       cgd 	nroom = 0;
     65  1.1       cgd 	secret = FALSE;
     66  1.1       cgd 	(void) makerooms();
     67  1.1       cgd 
     68  1.1       cgd 	/* construct stairs (up and down in different rooms if possible) */
     69  1.1       cgd 	croom = &rooms[rn2(nroom)];
     70  1.1       cgd 	xdnstair = somex();
     71  1.1       cgd 	ydnstair = somey();
     72  1.4  christos 	levl[xdnstair][ydnstair].scrsym = '>';
     73  1.1       cgd 	levl[xdnstair][ydnstair].typ = STAIRS;
     74  1.4  christos 	if (nroom > 1) {
     75  1.1       cgd 		troom = croom;
     76  1.4  christos 		croom = &rooms[rn2(nroom - 1)];
     77  1.4  christos 		if (croom >= troom)
     78  1.4  christos 			croom++;
     79  1.1       cgd 	}
     80  1.1       cgd 	xupstair = somex();	/* %% < and > might be in the same place */
     81  1.1       cgd 	yupstair = somey();
     82  1.4  christos 	levl[xupstair][yupstair].scrsym = '<';
     83  1.1       cgd 	levl[xupstair][yupstair].typ = STAIRS;
     84  1.1       cgd 
     85  1.1       cgd 	/* for each room: put things inside */
     86  1.4  christos 	for (croom = rooms; croom->hx > 0; croom++) {
     87  1.1       cgd 
     88  1.1       cgd 		/* put a sleeping monster inside */
     89  1.4  christos 		/*
     90  1.4  christos 		 * Note: monster may be on the stairs. This cannot be
     91  1.4  christos 		 * avoided: maybe the player fell through a trapdoor while a
     92  1.4  christos 		 * monster was on the stairs. Conclusion: we have to check
     93  1.4  christos 		 * for monsters on the stairs anyway.
     94  1.4  christos 		 */
     95  1.4  christos 		if (!rn2(3))
     96  1.4  christos 			(void)
     97  1.4  christos 				makemon((struct permonst *) 0, somex(), somey());
     98  1.1       cgd 
     99  1.1       cgd 		/* put traps and mimics inside */
    100  1.1       cgd 		goldseen = FALSE;
    101  1.4  christos 		while (!rn2(8 - (dlevel / 6)))
    102  1.4  christos 			mktrap(0, 0, croom);
    103  1.4  christos 		if (!goldseen && !rn2(3))
    104  1.4  christos 			mkgold(0L, somex(), somey());
    105  1.4  christos 		if (!rn2(3)) {
    106  1.1       cgd 			(void) mkobj_at(0, somex(), somey());
    107  1.1       cgd 			tryct = 0;
    108  1.4  christos 			while (!rn2(5)) {
    109  1.4  christos 				if (++tryct > 100) {
    110  1.1       cgd 					printf("tryct overflow4\n");
    111  1.1       cgd 					break;
    112  1.1       cgd 				}
    113  1.1       cgd 				(void) mkobj_at(0, somex(), somey());
    114  1.1       cgd 			}
    115  1.1       cgd 		}
    116  1.1       cgd 	}
    117  1.1       cgd 
    118  1.1       cgd 	qsort((char *) rooms, nroom, sizeof(struct mkroom), comp);
    119  1.1       cgd 	makecorridors();
    120  1.1       cgd 	make_niches();
    121  1.1       cgd 
    122  1.1       cgd 	/* make a secret treasure vault, not connected to the rest */
    123  1.4  christos 	if (nroom <= (2 * MAXNROFROOMS / 3))
    124  1.4  christos 		if (rn2(3)) {
    125  1.4  christos 			troom = &rooms[nroom];
    126  1.4  christos 			secret = TRUE;
    127  1.4  christos 			if (makerooms()) {
    128  1.4  christos 				troom->rtype = VAULT;	/* treasure vault */
    129  1.4  christos 				for (x = troom->lx; x <= troom->hx; x++)
    130  1.4  christos 					for (y = troom->ly; y <= troom->hy; y++)
    131  1.4  christos 						mkgold((long) (rnd(dlevel * 100) + 50), x, y);
    132  1.4  christos 				if (!rn2(3))
    133  1.4  christos 					makevtele();
    134  1.4  christos 			}
    135  1.1       cgd 		}
    136  1.1       cgd #ifndef QUEST
    137  1.1       cgd #ifdef WIZARD
    138  1.4  christos 	if (wizard && getenv("SHOPTYPE"))
    139  1.4  christos 		mkshop();
    140  1.1       cgd 	else
    141  1.4  christos #endif	/* WIZARD */
    142  1.4  christos 	if (dlevel > 1 && dlevel < 20 && rn2(dlevel) < 3)
    143  1.4  christos 		mkshop();
    144  1.4  christos 	else if (dlevel > 6 && !rn2(7))
    145  1.4  christos 		mkzoo(ZOO);
    146  1.4  christos 	else if (dlevel > 9 && !rn2(5))
    147  1.4  christos 		mkzoo(BEEHIVE);
    148  1.4  christos 	else if (dlevel > 11 && !rn2(6))
    149  1.4  christos 		mkzoo(MORGUE);
    150  1.4  christos 	else if (dlevel > 18 && !rn2(6))
    151  1.4  christos 		mkswamp();
    152  1.4  christos #endif	/* QUEST */
    153  1.4  christos }
    154  1.4  christos 
    155  1.4  christos int
    156  1.4  christos makerooms()
    157  1.4  christos {
    158  1.4  christos 	struct rectangle *rsp;
    159  1.4  christos 	int             lx, ly, hx, hy, lowx, lowy, hix, hiy, dx, dy;
    160  1.4  christos 	int             tryct = 0, xlim, ylim;
    161  1.1       cgd 
    162  1.1       cgd 	/* init */
    163  1.1       cgd 	xlim = XLIM + secret;
    164  1.1       cgd 	ylim = YLIM + secret;
    165  1.4  christos 	if (nroom == 0) {
    166  1.1       cgd 		rsp = rs;
    167  1.1       cgd 		rsp->rlx = rsp->rly = 0;
    168  1.4  christos 		rsp->rhx = COLNO - 1;
    169  1.4  christos 		rsp->rhy = ROWNO - 1;
    170  1.1       cgd 		rsmax = 1;
    171  1.1       cgd 	}
    172  1.1       cgd 	rscnt = rsmax;
    173  1.1       cgd 
    174  1.1       cgd 	/* make rooms until satisfied */
    175  1.4  christos 	while (rscnt > 0 && nroom < MAXNROFROOMS - 1) {
    176  1.4  christos 		if (!secret && nroom > (MAXNROFROOMS / 3) &&
    177  1.4  christos 		    !rn2((MAXNROFROOMS - nroom) * (MAXNROFROOMS - nroom)))
    178  1.4  christos 			return (0);
    179  1.1       cgd 
    180  1.1       cgd 		/* pick a rectangle */
    181  1.1       cgd 		rsp = &rs[rn2(rscnt)];
    182  1.1       cgd 		hx = rsp->rhx;
    183  1.1       cgd 		hy = rsp->rhy;
    184  1.1       cgd 		lx = rsp->rlx;
    185  1.1       cgd 		ly = rsp->rly;
    186  1.1       cgd 
    187  1.1       cgd 		/* find size of room */
    188  1.4  christos 		if (secret)
    189  1.1       cgd 			dx = dy = 1;
    190  1.1       cgd 		else {
    191  1.4  christos 			dx = 2 + rn2((hx - lx - 8 > 20) ? 12 : 8);
    192  1.1       cgd 			dy = 2 + rn2(4);
    193  1.4  christos 			if (dx * dy > 50)
    194  1.4  christos 				dy = 50 / dx;
    195  1.1       cgd 		}
    196  1.1       cgd 
    197  1.1       cgd 		/* look whether our room will fit */
    198  1.4  christos 		if (hx - lx < dx + dx / 2 + 2 * xlim || hy - ly < dy + dy / 3 + 2 * ylim) {
    199  1.4  christos 			/* no, too small */
    200  1.4  christos 			/* maybe we throw this area out */
    201  1.4  christos 			if (secret || !rn2(MAXNROFROOMS + 1 - nroom - tryct)) {
    202  1.1       cgd 				rscnt--;
    203  1.1       cgd 				rs[rsmax] = *rsp;
    204  1.1       cgd 				*rsp = rs[rscnt];
    205  1.1       cgd 				rs[rscnt] = rs[rsmax];
    206  1.1       cgd 				tryct = 0;
    207  1.1       cgd 			} else
    208  1.1       cgd 				tryct++;
    209  1.1       cgd 			continue;
    210  1.1       cgd 		}
    211  1.4  christos 		lowx = lx + xlim + rn2(hx - lx - dx - 2 * xlim + 1);
    212  1.4  christos 		lowy = ly + ylim + rn2(hy - ly - dy - 2 * ylim + 1);
    213  1.1       cgd 		hix = lowx + dx;
    214  1.1       cgd 		hiy = lowy + dy;
    215  1.1       cgd 
    216  1.4  christos 		if (maker(lowx, dx, lowy, dy)) {
    217  1.4  christos 			if (secret)
    218  1.4  christos 				return (1);
    219  1.4  christos 			addrs(lowx - 1, lowy - 1, hix + 1, hiy + 1);
    220  1.1       cgd 			tryct = 0;
    221  1.4  christos 		} else if (tryct++ > 100)
    222  1.4  christos 			break;
    223  1.1       cgd 	}
    224  1.4  christos 	return (0);		/* failed to make vault - very strange */
    225  1.1       cgd }
    226  1.1       cgd 
    227  1.4  christos void
    228  1.4  christos addrs(lowx, lowy, hix, hiy)
    229  1.4  christos 	int             lowx, lowy, hix, hiy;
    230  1.1       cgd {
    231  1.4  christos 	struct rectangle *rsp;
    232  1.4  christos 	int             lx, ly, hx, hy, xlim, ylim;
    233  1.4  christos 	boolean         discarded;
    234  1.1       cgd 
    235  1.1       cgd 	xlim = XLIM + secret;
    236  1.1       cgd 	ylim = YLIM + secret;
    237  1.1       cgd 
    238  1.1       cgd 	/* walk down since rscnt and rsmax change */
    239  1.4  christos 	for (rsp = &rs[rsmax - 1]; rsp >= rs; rsp--) {
    240  1.4  christos 
    241  1.4  christos 		if ((lx = rsp->rlx) > hix || (ly = rsp->rly) > hiy ||
    242  1.4  christos 		    (hx = rsp->rhx) < lowx || (hy = rsp->rhy) < lowy)
    243  1.1       cgd 			continue;
    244  1.4  christos 		if ((discarded = (rsp >= &rs[rscnt]))) {
    245  1.1       cgd 			*rsp = rs[--rsmax];
    246  1.1       cgd 		} else {
    247  1.1       cgd 			rsmax--;
    248  1.1       cgd 			rscnt--;
    249  1.1       cgd 			*rsp = rs[rscnt];
    250  1.4  christos 			if (rscnt != rsmax)
    251  1.1       cgd 				rs[rscnt] = rs[rsmax];
    252  1.1       cgd 		}
    253  1.4  christos 		if (lowy - ly > 2 * ylim + 4)
    254  1.4  christos 			addrsx(lx, ly, hx, lowy - 2, discarded);
    255  1.4  christos 		if (lowx - lx > 2 * xlim + 4)
    256  1.4  christos 			addrsx(lx, ly, lowx - 2, hy, discarded);
    257  1.4  christos 		if (hy - hiy > 2 * ylim + 4)
    258  1.4  christos 			addrsx(lx, hiy + 2, hx, hy, discarded);
    259  1.4  christos 		if (hx - hix > 2 * xlim + 4)
    260  1.4  christos 			addrsx(hix + 2, ly, hx, hy, discarded);
    261  1.1       cgd 	}
    262  1.1       cgd }
    263  1.1       cgd 
    264  1.4  christos void
    265  1.4  christos addrsx(lx, ly, hx, hy, discarded)
    266  1.4  christos 	int             lx, ly, hx, hy;
    267  1.4  christos 	boolean         discarded;	/* piece of a discarded area */
    268  1.1       cgd {
    269  1.4  christos 	struct rectangle *rsp;
    270  1.1       cgd 
    271  1.1       cgd 	/* check inclusions */
    272  1.4  christos 	for (rsp = rs; rsp < &rs[rsmax]; rsp++) {
    273  1.4  christos 		if (lx >= rsp->rlx && hx <= rsp->rhx &&
    274  1.4  christos 		    ly >= rsp->rly && hy <= rsp->rhy)
    275  1.1       cgd 			return;
    276  1.1       cgd 	}
    277  1.1       cgd 
    278  1.1       cgd 	/* make a new entry */
    279  1.4  christos 	if (rsmax >= MAXRS) {
    280  1.1       cgd #ifdef WIZARD
    281  1.4  christos 		if (wizard)
    282  1.4  christos 			pline("MAXRS may be too small.");
    283  1.4  christos #endif	/* WIZARD */
    284  1.1       cgd 		return;
    285  1.1       cgd 	}
    286  1.1       cgd 	rsmax++;
    287  1.4  christos 	if (!discarded) {
    288  1.1       cgd 		*rsp = rs[rscnt];
    289  1.1       cgd 		rsp = &rs[rscnt];
    290  1.1       cgd 		rscnt++;
    291  1.1       cgd 	}
    292  1.1       cgd 	rsp->rlx = lx;
    293  1.1       cgd 	rsp->rly = ly;
    294  1.1       cgd 	rsp->rhx = hx;
    295  1.1       cgd 	rsp->rhy = hy;
    296  1.1       cgd }
    297  1.1       cgd 
    298  1.4  christos int
    299  1.4  christos comp(vx, vy)
    300  1.4  christos 	const void *vx, *vy;
    301  1.4  christos {
    302  1.4  christos 	const struct mkroom  *x = vx, *y = vy;
    303  1.4  christos 	if (x->lx < y->lx)
    304  1.4  christos 		return (-1);
    305  1.4  christos 	return (x->lx > y->lx);
    306  1.1       cgd }
    307  1.1       cgd 
    308  1.1       cgd coord
    309  1.5       jsm finddpos(int xl, int yl, int xh, int yh)
    310  1.4  christos {
    311  1.4  christos 	coord           ff;
    312  1.4  christos 	int x, y;
    313  1.4  christos 
    314  1.4  christos 	x = (xl == xh) ? xl : (xl + rn2(xh - xl + 1));
    315  1.4  christos 	y = (yl == yh) ? yl : (yl + rn2(yh - yl + 1));
    316  1.4  christos 	if (okdoor(x, y))
    317  1.1       cgd 		goto gotit;
    318  1.1       cgd 
    319  1.4  christos 	for (x = xl; x <= xh; x++)
    320  1.4  christos 		for (y = yl; y <= yh; y++)
    321  1.4  christos 			if (okdoor(x, y))
    322  1.4  christos 				goto gotit;
    323  1.4  christos 
    324  1.4  christos 	for (x = xl; x <= xh; x++)
    325  1.4  christos 		for (y = yl; y <= yh; y++)
    326  1.4  christos 			if (levl[x][y].typ == DOOR || levl[x][y].typ == SDOOR)
    327  1.4  christos 				goto gotit;
    328  1.1       cgd 	/* cannot find something reasonable -- strange */
    329  1.1       cgd 	x = xl;
    330  1.1       cgd 	y = yh;
    331  1.1       cgd gotit:
    332  1.1       cgd 	ff.x = x;
    333  1.1       cgd 	ff.y = y;
    334  1.4  christos 	return (ff);
    335  1.1       cgd }
    336  1.1       cgd 
    337  1.1       cgd /* see whether it is allowable to create a door at [x,y] */
    338  1.4  christos int
    339  1.4  christos okdoor(x, y)
    340  1.4  christos 	int x, y;
    341  1.4  christos {
    342  1.4  christos 	if (levl[x - 1][y].typ == DOOR || levl[x + 1][y].typ == DOOR ||
    343  1.4  christos 	    levl[x][y + 1].typ == DOOR || levl[x][y - 1].typ == DOOR ||
    344  1.4  christos 	    levl[x - 1][y].typ == SDOOR || levl[x + 1][y].typ == SDOOR ||
    345  1.4  christos 	    levl[x][y - 1].typ == SDOOR || levl[x][y + 1].typ == SDOOR ||
    346  1.4  christos 	    (levl[x][y].typ != HWALL && levl[x][y].typ != VWALL) ||
    347  1.4  christos 	    doorindex >= DOORMAX)
    348  1.4  christos 		return (0);
    349  1.4  christos 	return (1);
    350  1.4  christos }
    351  1.4  christos 
    352  1.4  christos void
    353  1.4  christos dodoor(x, y, aroom)
    354  1.4  christos 	int x, y;
    355  1.4  christos 	struct mkroom  *aroom;
    356  1.1       cgd {
    357  1.4  christos 	if (doorindex >= DOORMAX) {
    358  1.1       cgd 		impossible("DOORMAX exceeded?");
    359  1.1       cgd 		return;
    360  1.1       cgd 	}
    361  1.4  christos 	if (!okdoor(x, y) && nxcor)
    362  1.1       cgd 		return;
    363  1.4  christos 	dosdoor(x, y, aroom, rn2(8) ? DOOR : SDOOR);
    364  1.1       cgd }
    365  1.1       cgd 
    366  1.4  christos void
    367  1.4  christos dosdoor(x, y, aroom, type)
    368  1.4  christos 	int x, y;
    369  1.4  christos 	struct mkroom  *aroom;
    370  1.4  christos 	int type;
    371  1.1       cgd {
    372  1.4  christos 	struct mkroom  *broom;
    373  1.4  christos 	int tmp;
    374  1.1       cgd 
    375  1.4  christos 	if (!IS_WALL(levl[x][y].typ))	/* avoid SDOORs with '+' as scrsym */
    376  1.1       cgd 		type = DOOR;
    377  1.1       cgd 	levl[x][y].typ = type;
    378  1.4  christos 	if (type == DOOR)
    379  1.1       cgd 		levl[x][y].scrsym = '+';
    380  1.1       cgd 	aroom->doorct++;
    381  1.4  christos 	broom = aroom + 1;
    382  1.4  christos 	if (broom->hx < 0)
    383  1.4  christos 		tmp = doorindex;
    384  1.4  christos 	else
    385  1.4  christos 		for (tmp = doorindex; tmp > broom->fdoor; tmp--)
    386  1.4  christos 			doors[tmp] = doors[tmp - 1];
    387  1.1       cgd 	doorindex++;
    388  1.1       cgd 	doors[tmp].x = x;
    389  1.1       cgd 	doors[tmp].y = y;
    390  1.4  christos 	for (; broom->hx >= 0; broom++)
    391  1.4  christos 		broom->fdoor++;
    392  1.1       cgd }
    393  1.1       cgd 
    394  1.1       cgd /* Only called from makerooms() */
    395  1.4  christos int
    396  1.4  christos maker(lowx, ddx, lowy, ddy)
    397  1.4  christos 	schar           lowx, ddx, lowy, ddy;
    398  1.4  christos {
    399  1.4  christos 	struct mkroom  *croom;
    400  1.4  christos 	int x, y, hix = lowx + ddx, hiy = lowy + ddy;
    401  1.4  christos 	int xlim = XLIM + secret, ylim = YLIM + secret;
    402  1.4  christos 
    403  1.4  christos 	if (nroom >= MAXNROFROOMS)
    404  1.4  christos 		return (0);
    405  1.4  christos 	if (lowx < XLIM)
    406  1.4  christos 		lowx = XLIM;
    407  1.4  christos 	if (lowy < YLIM)
    408  1.4  christos 		lowy = YLIM;
    409  1.4  christos 	if (hix > COLNO - XLIM - 1)
    410  1.4  christos 		hix = COLNO - XLIM - 1;
    411  1.4  christos 	if (hiy > ROWNO - YLIM - 1)
    412  1.4  christos 		hiy = ROWNO - YLIM - 1;
    413  1.1       cgd chk:
    414  1.4  christos 	if (hix <= lowx || hiy <= lowy)
    415  1.4  christos 		return (0);
    416  1.1       cgd 
    417  1.1       cgd 	/* check area around room (and make room smaller if necessary) */
    418  1.4  christos 	for (x = lowx - xlim; x <= hix + xlim; x++) {
    419  1.4  christos 		for (y = lowy - ylim; y <= hiy + ylim; y++) {
    420  1.4  christos 			if (levl[x][y].typ) {
    421  1.1       cgd #ifdef WIZARD
    422  1.4  christos 				if (wizard && !secret)
    423  1.4  christos 					pline("Strange area [%d,%d] in maker().", x, y);
    424  1.4  christos #endif	/* WIZARD */
    425  1.4  christos 				if (!rn2(3))
    426  1.4  christos 					return (0);
    427  1.4  christos 				if (x < lowx)
    428  1.4  christos 					lowx = x + xlim + 1;
    429  1.1       cgd 				else
    430  1.4  christos 					hix = x - xlim - 1;
    431  1.4  christos 				if (y < lowy)
    432  1.4  christos 					lowy = y + ylim + 1;
    433  1.1       cgd 				else
    434  1.4  christos 					hiy = y - ylim - 1;
    435  1.1       cgd 				goto chk;
    436  1.1       cgd 			}
    437  1.1       cgd 		}
    438  1.1       cgd 	}
    439  1.1       cgd 
    440  1.1       cgd 	croom = &rooms[nroom];
    441  1.1       cgd 
    442  1.1       cgd 	/* on low levels the room is lit (usually) */
    443  1.1       cgd 	/* secret vaults are always lit */
    444  1.4  christos 	if ((rnd(dlevel) < 10 && rn2(77)) || (ddx == 1 && ddy == 1)) {
    445  1.4  christos 		for (x = lowx - 1; x <= hix + 1; x++)
    446  1.4  christos 			for (y = lowy - 1; y <= hiy + 1; y++)
    447  1.1       cgd 				levl[x][y].lit = 1;
    448  1.1       cgd 		croom->rlit = 1;
    449  1.1       cgd 	} else
    450  1.1       cgd 		croom->rlit = 0;
    451  1.1       cgd 	croom->lx = lowx;
    452  1.1       cgd 	croom->hx = hix;
    453  1.1       cgd 	croom->ly = lowy;
    454  1.1       cgd 	croom->hy = hiy;
    455  1.1       cgd 	croom->rtype = croom->doorct = croom->fdoor = 0;
    456  1.1       cgd 
    457  1.4  christos 	for (x = lowx - 1; x <= hix + 1; x++)
    458  1.4  christos 		for (y = lowy - 1; y <= hiy + 1; y += (hiy - lowy + 2)) {
    459  1.4  christos 			levl[x][y].scrsym = '-';
    460  1.4  christos 			levl[x][y].typ = HWALL;
    461  1.4  christos 		}
    462  1.4  christos 	for (x = lowx - 1; x <= hix + 1; x += (hix - lowx + 2))
    463  1.4  christos 		for (y = lowy; y <= hiy; y++) {
    464  1.4  christos 			levl[x][y].scrsym = '|';
    465  1.4  christos 			levl[x][y].typ = VWALL;
    466  1.4  christos 		}
    467  1.4  christos 	for (x = lowx; x <= hix; x++)
    468  1.4  christos 		for (y = lowy; y <= hiy; y++) {
    469  1.4  christos 			levl[x][y].scrsym = '.';
    470  1.4  christos 			levl[x][y].typ = ROOM;
    471  1.4  christos 		}
    472  1.1       cgd 
    473  1.1       cgd 	smeq[nroom] = nroom;
    474  1.1       cgd 	croom++;
    475  1.1       cgd 	croom->hx = -1;
    476  1.1       cgd 	nroom++;
    477  1.4  christos 	return (1);
    478  1.1       cgd }
    479  1.1       cgd 
    480  1.4  christos void
    481  1.4  christos makecorridors()
    482  1.4  christos {
    483  1.4  christos 	int a, b;
    484  1.1       cgd 
    485  1.1       cgd 	nxcor = 0;
    486  1.4  christos 	for (a = 0; a < nroom - 1; a++)
    487  1.4  christos 		join(a, a + 1);
    488  1.4  christos 	for (a = 0; a < nroom - 2; a++)
    489  1.4  christos 		if (smeq[a] != smeq[a + 2])
    490  1.4  christos 			join(a, a + 2);
    491  1.4  christos 	for (a = 0; a < nroom; a++)
    492  1.4  christos 		for (b = 0; b < nroom; b++)
    493  1.4  christos 			if (smeq[a] != smeq[b])
    494  1.4  christos 				join(a, b);
    495  1.4  christos 	if (nroom > 2)
    496  1.4  christos 		for (nxcor = rn2(nroom) + 4; nxcor; nxcor--) {
    497  1.4  christos 			a = rn2(nroom);
    498  1.4  christos 			b = rn2(nroom - 2);
    499  1.4  christos 			if (b >= a)
    500  1.4  christos 				b += 2;
    501  1.4  christos 			join(a, b);
    502  1.4  christos 		}
    503  1.4  christos }
    504  1.4  christos 
    505  1.4  christos void
    506  1.4  christos join(a, b)
    507  1.4  christos 	int a, b;
    508  1.4  christos {
    509  1.4  christos 	coord           cc, tt;
    510  1.4  christos 	int tx, ty, xx, yy;
    511  1.4  christos 	struct rm      *crm;
    512  1.4  christos 	struct mkroom  *croom, *troom;
    513  1.4  christos 	int dx, dy, dix, diy, cct;
    514  1.1       cgd 
    515  1.1       cgd 	croom = &rooms[a];
    516  1.1       cgd 	troom = &rooms[b];
    517  1.1       cgd 
    518  1.4  christos 	/*
    519  1.4  christos 	 * find positions cc and tt for doors in croom and troom and
    520  1.4  christos 	 * direction for a corridor between them
    521  1.4  christos 	 */
    522  1.1       cgd 
    523  1.4  christos 	if (troom->hx < 0 || croom->hx < 0 || doorindex >= DOORMAX)
    524  1.4  christos 		return;
    525  1.4  christos 	if (troom->lx > croom->hx) {
    526  1.1       cgd 		dx = 1;
    527  1.1       cgd 		dy = 0;
    528  1.4  christos 		xx = croom->hx + 1;
    529  1.4  christos 		tx = troom->lx - 1;
    530  1.4  christos 		cc = finddpos(xx, croom->ly, xx, croom->hy);
    531  1.4  christos 		tt = finddpos(tx, troom->ly, tx, troom->hy);
    532  1.4  christos 	} else if (troom->hy < croom->ly) {
    533  1.1       cgd 		dy = -1;
    534  1.1       cgd 		dx = 0;
    535  1.4  christos 		yy = croom->ly - 1;
    536  1.4  christos 		cc = finddpos(croom->lx, yy, croom->hx, yy);
    537  1.4  christos 		ty = troom->hy + 1;
    538  1.4  christos 		tt = finddpos(troom->lx, ty, troom->hx, ty);
    539  1.4  christos 	} else if (troom->hx < croom->lx) {
    540  1.1       cgd 		dx = -1;
    541  1.1       cgd 		dy = 0;
    542  1.4  christos 		xx = croom->lx - 1;
    543  1.4  christos 		tx = troom->hx + 1;
    544  1.4  christos 		cc = finddpos(xx, croom->ly, xx, croom->hy);
    545  1.4  christos 		tt = finddpos(tx, troom->ly, tx, troom->hy);
    546  1.1       cgd 	} else {
    547  1.1       cgd 		dy = 1;
    548  1.1       cgd 		dx = 0;
    549  1.4  christos 		yy = croom->hy + 1;
    550  1.4  christos 		ty = troom->ly - 1;
    551  1.4  christos 		cc = finddpos(croom->lx, yy, croom->hx, yy);
    552  1.4  christos 		tt = finddpos(troom->lx, ty, troom->hx, ty);
    553  1.1       cgd 	}
    554  1.1       cgd 	xx = cc.x;
    555  1.1       cgd 	yy = cc.y;
    556  1.1       cgd 	tx = tt.x - dx;
    557  1.1       cgd 	ty = tt.y - dy;
    558  1.4  christos 	if (nxcor && levl[xx + dx][yy + dy].typ)
    559  1.1       cgd 		return;
    560  1.4  christos 	dodoor(xx, yy, croom);
    561  1.1       cgd 
    562  1.1       cgd 	cct = 0;
    563  1.4  christos 	while (xx != tx || yy != ty) {
    564  1.4  christos 		xx += dx;
    565  1.4  christos 		yy += dy;
    566  1.1       cgd 
    567  1.4  christos 		/* loop: dig corridor at [xx,yy] and find new [xx,yy] */
    568  1.4  christos 		if (cct++ > 500 || (nxcor && !rn2(35)))
    569  1.4  christos 			return;
    570  1.1       cgd 
    571  1.4  christos 		if (xx == COLNO - 1 || xx == 0 || yy == 0 || yy == ROWNO - 1)
    572  1.4  christos 			return;	/* impossible */
    573  1.1       cgd 
    574  1.4  christos 		crm = &levl[xx][yy];
    575  1.4  christos 		if (!(crm->typ)) {
    576  1.4  christos 			if (rn2(100)) {
    577  1.4  christos 				crm->typ = CORR;
    578  1.4  christos 				crm->scrsym = CORR_SYM;
    579  1.4  christos 				if (nxcor && !rn2(50))
    580  1.4  christos 					(void) mkobj_at(ROCK_SYM, xx, yy);
    581  1.4  christos 			} else {
    582  1.4  christos 				crm->typ = SCORR;
    583  1.4  christos 				crm->scrsym = ' ';
    584  1.4  christos 			}
    585  1.4  christos 		} else if (crm->typ != CORR && crm->typ != SCORR) {
    586  1.4  christos 			/* strange ... */
    587  1.4  christos 			return;
    588  1.1       cgd 		}
    589  1.4  christos 		/* find next corridor position */
    590  1.4  christos 		dix = abs(xx - tx);
    591  1.4  christos 		diy = abs(yy - ty);
    592  1.4  christos 
    593  1.4  christos 		/* do we have to change direction ? */
    594  1.4  christos 		if (dy && dix > diy) {
    595  1.4  christos 			int ddx = (xx > tx) ? -1 : 1;
    596  1.4  christos 
    597  1.4  christos 			crm = &levl[xx + ddx][yy];
    598  1.4  christos 			if (!crm->typ || crm->typ == CORR || crm->typ == SCORR) {
    599  1.4  christos 				dx = ddx;
    600  1.4  christos 				dy = 0;
    601  1.4  christos 				continue;
    602  1.4  christos 			}
    603  1.4  christos 		} else if (dx && diy > dix) {
    604  1.4  christos 			int ddy = (yy > ty) ? -1 : 1;
    605  1.1       cgd 
    606  1.4  christos 			crm = &levl[xx][yy + ddy];
    607  1.4  christos 			if (!crm->typ || crm->typ == CORR || crm->typ == SCORR) {
    608  1.4  christos 				dy = ddy;
    609  1.4  christos 				dx = 0;
    610  1.4  christos 				continue;
    611  1.4  christos 			}
    612  1.4  christos 		}
    613  1.4  christos 		/* continue straight on? */
    614  1.4  christos 		crm = &levl[xx + dx][yy + dy];
    615  1.4  christos 		if (!crm->typ || crm->typ == CORR || crm->typ == SCORR)
    616  1.4  christos 			continue;
    617  1.1       cgd 
    618  1.4  christos 		/* no, what must we do now?? */
    619  1.4  christos 		if (dx) {
    620  1.4  christos 			dx = 0;
    621  1.4  christos 			dy = (ty < yy) ? -1 : 1;
    622  1.4  christos 			crm = &levl[xx + dx][yy + dy];
    623  1.4  christos 			if (!crm->typ || crm->typ == CORR || crm->typ == SCORR)
    624  1.4  christos 				continue;
    625  1.4  christos 			dy = -dy;
    626  1.4  christos 			continue;
    627  1.4  christos 		} else {
    628  1.4  christos 			dy = 0;
    629  1.4  christos 			dx = (tx < xx) ? -1 : 1;
    630  1.4  christos 			crm = &levl[xx + dx][yy + dy];
    631  1.4  christos 			if (!crm->typ || crm->typ == CORR || crm->typ == SCORR)
    632  1.4  christos 				continue;
    633  1.4  christos 			dx = -dx;
    634  1.4  christos 			continue;
    635  1.4  christos 		}
    636  1.1       cgd 	}
    637  1.1       cgd 
    638  1.1       cgd 	/* we succeeded in digging the corridor */
    639  1.1       cgd 	dodoor(tt.x, tt.y, troom);
    640  1.1       cgd 
    641  1.4  christos 	if (smeq[a] < smeq[b])
    642  1.1       cgd 		smeq[b] = smeq[a];
    643  1.1       cgd 	else
    644  1.1       cgd 		smeq[a] = smeq[b];
    645  1.1       cgd }
    646  1.1       cgd 
    647  1.4  christos void
    648  1.1       cgd make_niches()
    649  1.1       cgd {
    650  1.4  christos 	int             ct = rnd(nroom / 2 + 1);
    651  1.4  christos 	while (ct--)
    652  1.4  christos 		makeniche(FALSE);
    653  1.1       cgd }
    654  1.1       cgd 
    655  1.4  christos void
    656  1.1       cgd makevtele()
    657  1.1       cgd {
    658  1.1       cgd 	makeniche(TRUE);
    659  1.1       cgd }
    660  1.1       cgd 
    661  1.4  christos void
    662  1.1       cgd makeniche(with_trap)
    663  1.4  christos 	boolean         with_trap;
    664  1.1       cgd {
    665  1.4  christos 	struct mkroom  *aroom;
    666  1.4  christos 	struct rm      *rm;
    667  1.4  christos 	int             vct = 8;
    668  1.4  christos 	coord           dd;
    669  1.4  christos 	int		dy, xx, yy;
    670  1.4  christos 	struct trap    *ttmp;
    671  1.4  christos 
    672  1.4  christos 	if (doorindex < DOORMAX)
    673  1.4  christos 		while (vct--) {
    674  1.4  christos 			aroom = &rooms[rn2(nroom - 1)];
    675  1.4  christos 			if (aroom->rtype != 0)
    676  1.4  christos 				continue;	/* not an ordinary room */
    677  1.4  christos 			if (aroom->doorct == 1 && rn2(5))
    678  1.4  christos 				continue;
    679  1.4  christos 			if (rn2(2)) {
    680  1.4  christos 				dy = 1;
    681  1.4  christos 				dd = finddpos(aroom->lx, aroom->hy + 1, aroom->hx, aroom->hy + 1);
    682  1.4  christos 			} else {
    683  1.4  christos 				dy = -1;
    684  1.4  christos 				dd = finddpos(aroom->lx, aroom->ly - 1, aroom->hx, aroom->ly - 1);
    685  1.4  christos 			}
    686  1.4  christos 			xx = dd.x;
    687  1.4  christos 			yy = dd.y;
    688  1.4  christos 			if ((rm = &levl[xx][yy + dy])->typ)
    689  1.4  christos 				continue;
    690  1.4  christos 			if (with_trap || !rn2(4)) {
    691  1.4  christos 				rm->typ = SCORR;
    692  1.4  christos 				rm->scrsym = ' ';
    693  1.4  christos 				if (with_trap) {
    694  1.4  christos 					ttmp = maketrap(xx, yy + dy, TELEP_TRAP);
    695  1.4  christos 					ttmp->once = 1;
    696  1.4  christos 					make_engr_at(xx, yy - dy, "ad ae?ar um");
    697  1.4  christos 				}
    698  1.4  christos 				dosdoor(xx, yy, aroom, SDOOR);
    699  1.4  christos 			} else {
    700  1.4  christos 				rm->typ = CORR;
    701  1.4  christos 				rm->scrsym = CORR_SYM;
    702  1.4  christos 				if (rn2(7))
    703  1.4  christos 					dosdoor(xx, yy, aroom, rn2(5) ? SDOOR : DOOR);
    704  1.4  christos 				else {
    705  1.4  christos 					mksobj_at(SCR_TELEPORTATION, xx, yy + dy);
    706  1.4  christos 					if (!rn2(3))
    707  1.4  christos 						(void) mkobj_at(0, xx, yy + dy);
    708  1.4  christos 				}
    709  1.4  christos 			}
    710  1.4  christos 			return;
    711  1.1       cgd 		}
    712  1.1       cgd }
    713  1.1       cgd 
    714  1.1       cgd /* make a trap somewhere (in croom if mazeflag = 0) */
    715  1.4  christos void
    716  1.4  christos mktrap(num, mazeflag, croom)
    717  1.4  christos 	int num, mazeflag;
    718  1.4  christos 	struct mkroom  *croom;
    719  1.4  christos {
    720  1.4  christos 	struct trap    *ttmp;
    721  1.4  christos 	int             kind, nopierc, nomimic, fakedoor, fakegold, tryct = 0;
    722  1.4  christos 	xchar           mx, my;
    723  1.1       cgd 
    724  1.4  christos 	if (!num || num >= TRAPNUM) {
    725  1.1       cgd 		nopierc = (dlevel < 4) ? 1 : 0;
    726  1.4  christos 		nomimic = (dlevel < 9 || goldseen) ? 1 : 0;
    727  1.4  christos 		if (strchr(fut_geno, 'M'))
    728  1.4  christos 			nomimic = 1;
    729  1.1       cgd 		kind = rn2(TRAPNUM - nopierc - nomimic);
    730  1.1       cgd 		/* note: PIERC = 7, MIMIC = 8, TRAPNUM = 9 */
    731  1.4  christos 	} else
    732  1.4  christos 		kind = num;
    733  1.1       cgd 
    734  1.4  christos 	if (kind == MIMIC) {
    735  1.4  christos 		struct monst   *mtmp;
    736  1.1       cgd 
    737  1.1       cgd 		fakedoor = (!rn2(3) && !mazeflag);
    738  1.1       cgd 		fakegold = (!fakedoor && !rn2(2));
    739  1.4  christos 		if (fakegold)
    740  1.4  christos 			goldseen = TRUE;
    741  1.1       cgd 		do {
    742  1.4  christos 			if (++tryct > 200)
    743  1.4  christos 				return;
    744  1.4  christos 			if (fakedoor) {
    745  1.1       cgd 				/* note: fakedoor maybe on actual door */
    746  1.4  christos 				if (rn2(2)) {
    747  1.4  christos 					if (rn2(2))
    748  1.4  christos 						mx = croom->hx + 1;
    749  1.4  christos 					else
    750  1.4  christos 						mx = croom->lx - 1;
    751  1.1       cgd 					my = somey();
    752  1.1       cgd 				} else {
    753  1.4  christos 					if (rn2(2))
    754  1.4  christos 						my = croom->hy + 1;
    755  1.4  christos 					else
    756  1.4  christos 						my = croom->ly - 1;
    757  1.1       cgd 					mx = somex();
    758  1.1       cgd 				}
    759  1.4  christos 			} else if (mazeflag) {
    760  1.4  christos 				coord           mm;
    761  1.1       cgd 				mm = mazexy();
    762  1.1       cgd 				mx = mm.x;
    763  1.1       cgd 				my = mm.y;
    764  1.1       cgd 			} else {
    765  1.1       cgd 				mx = somex();
    766  1.1       cgd 				my = somey();
    767  1.1       cgd 			}
    768  1.4  christos 		} while (m_at(mx, my) || levl[mx][my].typ == STAIRS);
    769  1.4  christos 		if ((mtmp = makemon(PM_MIMIC, mx, my)) != NULL) {
    770  1.4  christos 			mtmp->mimic = 1;
    771  1.4  christos 			mtmp->mappearance =
    772  1.4  christos 				fakegold ? '$' : fakedoor ? '+' :
    773  1.4  christos 				(mazeflag && rn2(2)) ? AMULET_SYM :
    774  1.4  christos 				"=/)%?![<>"[rn2(9)];
    775  1.1       cgd 		}
    776  1.1       cgd 		return;
    777  1.1       cgd 	}
    778  1.1       cgd 	do {
    779  1.4  christos 		if (++tryct > 200)
    780  1.1       cgd 			return;
    781  1.4  christos 		if (mazeflag) {
    782  1.4  christos 			coord           mm;
    783  1.1       cgd 			mm = mazexy();
    784  1.1       cgd 			mx = mm.x;
    785  1.1       cgd 			my = mm.y;
    786  1.1       cgd 		} else {
    787  1.1       cgd 			mx = somex();
    788  1.1       cgd 			my = somey();
    789  1.1       cgd 		}
    790  1.4  christos 	} while (t_at(mx, my) || levl[mx][my].typ == STAIRS);
    791  1.1       cgd 	ttmp = maketrap(mx, my, kind);
    792  1.4  christos 	if (mazeflag && !rn2(10) && ttmp->ttyp < PIERC)
    793  1.1       cgd 		ttmp->tseen = 1;
    794  1.1       cgd }
    795