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