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