# Source:NetHack 3.6.1/src/mklev.c
Below is the full text of mklev.c from the source code of NetHack 3.6.1. To link to a particular line, write [[Source:NetHack 3.6.1/src/mklev.c#line123]], for example.

Contents

1 Top of file
2 do_comp
3 finddpos
4 sort_rooms
5 do_room_or_subroom
6 add_room
7 add_subroom
8 makerooms
9 join
10 makecorridors
11 add_door
12 dosdoor
13 place_niche
14 makeniche
15 make_niches
16 makevtele
17 clear_level_structures
18 makelevel
19 mineralize
20 mklev
21 topologize
22 find_branch_room
23 pos_to_room
24 place_branch
25 bydoor
26 okdoor
27 dodoor
28 occupied
29 mktrap
30 mkstairs
31 mkfount
32 mksink
33 mkaltar
34 mkgrave
35 mkinvokearea
36 mkinvpos
37 mk_knox_portal

Top of file
&nbsp;/* NetHack 3.6	mklev.c	$NHDT-Date: 1511681724 2017/11/26 07:35:24 $  $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.47 $ */
&nbsp;/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
&nbsp;/*-Copyright (c) Alex Smith, 2017. */
&nbsp;/* NetHack may be freely redistributed.  See license for details. */

The NetHack General Public License applies to screenshots, source code and other content from NetHack.
This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

&nbsp;
&nbsp;#include "hack.h"
&nbsp;
&nbsp;/* for UNIX, Rand #def'd to (long)lrand48() or (long)random() */
&nbsp;/* croom-&gt;lx etc are schar (width &lt;= int), so % arith ensures that */
&nbsp;/* conversion of result to int is reasonable */
&nbsp;
&nbsp;STATIC_DCL void FDECL(mkfount, (int, struct mkroom *));
&nbsp;STATIC_DCL void FDECL(mksink, (struct mkroom *));
&nbsp;STATIC_DCL void FDECL(mkaltar, (struct mkroom *));
&nbsp;STATIC_DCL void FDECL(mkgrave, (struct mkroom *));
&nbsp;STATIC_DCL void NDECL(makevtele);
&nbsp;STATIC_DCL void NDECL(clear_level_structures);
&nbsp;STATIC_DCL void NDECL(makelevel);
&nbsp;STATIC_DCL boolean FDECL(bydoor, (XCHAR_P, XCHAR_P));
&nbsp;STATIC_DCL struct mkroom *FDECL(find_branch_room, (coord *));
&nbsp;STATIC_DCL struct mkroom *FDECL(pos_to_room, (XCHAR_P, XCHAR_P));
&nbsp;STATIC_DCL boolean FDECL(place_niche, (struct mkroom *, int *, int *, int *));
&nbsp;STATIC_DCL void FDECL(makeniche, (int));
&nbsp;STATIC_DCL void NDECL(make_niches);
&nbsp;STATIC_PTR int FDECL(CFDECLSPEC do_comp, (const genericptr,
&nbsp;                                          const genericptr));
&nbsp;STATIC_DCL void FDECL(dosdoor, (XCHAR_P, XCHAR_P, struct mkroom *, int));
&nbsp;STATIC_DCL void FDECL(join, (int, int, BOOLEAN_P));
&nbsp;STATIC_DCL void FDECL(do_room_or_subroom, (struct mkroom *, int, int,
&nbsp;                                           int, int, BOOLEAN_P,
&nbsp;                                           SCHAR_P, BOOLEAN_P, BOOLEAN_P));
&nbsp;STATIC_DCL void NDECL(makerooms);
&nbsp;STATIC_DCL void FDECL(finddpos, (coord *, XCHAR_P, XCHAR_P,
&nbsp;                                 XCHAR_P, XCHAR_P));
&nbsp;STATIC_DCL void FDECL(mkinvpos, (XCHAR_P, XCHAR_P, int));
&nbsp;STATIC_DCL void FDECL(mk_knox_portal, (XCHAR_P, XCHAR_P));
&nbsp;
&nbsp;#define create_vault() create_room(-1, -1, 2, 2, -1, -1, VAULT, TRUE)
&nbsp;#define init_vault() vault_x = -1
&nbsp;#define do_vault() (vault_x != -1)
&nbsp;static xchar vault_x, vault_y;
&nbsp;static boolean made_branch; /* used only during level creation */
&nbsp;
&nbsp;/* Args must be (const genericptr) so that qsort will always be happy. */
&nbsp;
do_comp
&nbsp;STATIC_PTR int CFDECLSPEC
&nbsp;do_comp(vx, vy)
&nbsp;const genericptr vx;
&nbsp;const genericptr vy;
&nbsp;{
&nbsp;#ifdef LINT
&nbsp;    /* lint complains about possible pointer alignment problems, but we know
&nbsp;       that vx and vy are always properly aligned. Hence, the following
&nbsp;       bogus definition:
&nbsp;    */
&nbsp;    return (vx == vy) ? 0 : -1;
&nbsp;#else
&nbsp;    register const struct mkroom *x, *y;
&nbsp;
&nbsp;    x = (const struct mkroom *) vx;
&nbsp;    y = (const struct mkroom *) vy;
&nbsp;    if (x-&gt;lx &lt; y-&gt;lx)
&nbsp;        return -1;
&nbsp;    return (x-&gt;lx &gt; y-&gt;lx);
&nbsp;#endif /* LINT */
&nbsp;}
&nbsp;
finddpos
&nbsp;STATIC_OVL void
&nbsp;finddpos(cc, xl, yl, xh, yh)
&nbsp;coord *cc;
&nbsp;xchar xl, yl, xh, yh;
&nbsp;{
&nbsp;    register xchar x, y;
&nbsp;
&nbsp;    x = rn1(xh - xl + 1, xl);
&nbsp;    y = rn1(yh - yl + 1, yl);
&nbsp;    if (okdoor(x, y))
&nbsp;        goto gotit;
&nbsp;
&nbsp;    for (x = xl; x &lt;= xh; x++)
&nbsp;        for (y = yl; y &lt;= yh; y++)
&nbsp;            if (okdoor(x, y))
&nbsp;                goto gotit;
&nbsp;
&nbsp;    for (x = xl; x &lt;= xh; x++)
&nbsp;        for (y = yl; y &lt;= yh; y++)
&nbsp;            if (IS_DOOR(levl[x][y].typ) || levl[x][y].typ == SDOOR)
&nbsp;                goto gotit;
&nbsp;    /* cannot find something reasonable -- strange */
&nbsp;    x = xl;
&nbsp;    y = yh;
&nbsp;gotit:
&nbsp;    cc-&gt;x = x;
&nbsp;    cc-&gt;y = y;
&nbsp;    return;
&nbsp;}
&nbsp;
sort_rooms
&nbsp;void
&nbsp;sort_rooms()
&nbsp;{
&nbsp;#if defined(SYSV) || defined(DGUX)
&nbsp;    qsort((genericptr_t) rooms, (unsigned) nroom, sizeof(struct mkroom),
&nbsp;          do_comp);
&nbsp;#else
&nbsp;    qsort((genericptr_t) rooms, nroom, sizeof(struct mkroom), do_comp);
&nbsp;#endif
&nbsp;}
&nbsp;
do_room_or_subroom
&nbsp;STATIC_OVL void
&nbsp;do_room_or_subroom(croom, lowx, lowy, hix, hiy, lit, rtype, special, is_room)
&nbsp;register struct mkroom *croom;
&nbsp;int lowx, lowy;
&nbsp;register int hix, hiy;
&nbsp;boolean lit;
&nbsp;schar rtype;
&nbsp;boolean special;
&nbsp;boolean is_room;
&nbsp;{
&nbsp;    register int x, y;
&nbsp;    struct rm *lev;
&nbsp;
&nbsp;    /* locations might bump level edges in wall-less rooms */
&nbsp;    /* add/subtract 1 to allow for edge locations */
&nbsp;    if (!lowx)
&nbsp;        lowx++;
&nbsp;    if (!lowy)
&nbsp;        lowy++;
&nbsp;    if (hix &gt;= COLNO - 1)
&nbsp;        hix = COLNO - 2;
&nbsp;    if (hiy &gt;= ROWNO - 1)
&nbsp;        hiy = ROWNO - 2;
&nbsp;
&nbsp;    if (lit) {
&nbsp;        for (x = lowx - 1; x &lt;= hix + 1; x++) {
&nbsp;            lev = &amp;levl[x][max(lowy - 1, 0)];
&nbsp;            for (y = lowy - 1; y &lt;= hiy + 1; y++)
&nbsp;                lev++-&gt;lit = 1;
&nbsp;        }
&nbsp;        croom-&gt;rlit = 1;
&nbsp;    } else
&nbsp;        croom-&gt;rlit = 0;
&nbsp;
&nbsp;    croom-&gt;lx = lowx;
&nbsp;    croom-&gt;hx = hix;
&nbsp;    croom-&gt;ly = lowy;
&nbsp;    croom-&gt;hy = hiy;
&nbsp;    croom-&gt;rtype = rtype;
&nbsp;    croom-&gt;doorct = 0;
&nbsp;    /* if we're not making a vault, doorindex will still be 0
&nbsp;     * if we are, we'll have problems adding niches to the previous room
&nbsp;     * unless fdoor is at least doorindex
&nbsp;     */
&nbsp;    croom-&gt;fdoor = doorindex;
&nbsp;    croom-&gt;irregular = FALSE;
&nbsp;
&nbsp;    croom-&gt;nsubrooms = 0;
&nbsp;    croom-&gt;sbrooms[0] = (struct mkroom *) 0;
&nbsp;    if (!special) {
&nbsp;        for (x = lowx - 1; x &lt;= hix + 1; x++)
&nbsp;            for (y = lowy - 1; y &lt;= hiy + 1; y += (hiy - lowy + 2)) {
&nbsp;                levl[x][y].typ = HWALL;
&nbsp;                levl[x][y].horizontal = 1; /* For open/secret doors. */
&nbsp;            }
&nbsp;        for (x = lowx - 1; x &lt;= hix + 1; x += (hix - lowx + 2))
&nbsp;            for (y = lowy; y &lt;= hiy; y++) {
&nbsp;                levl[x][y].typ = VWALL;
&nbsp;                levl[x][y].horizontal = 0; /* For open/secret doors. */
&nbsp;            }
&nbsp;        for (x = lowx; x &lt;= hix; x++) {
&nbsp;            lev = &amp;levl[x][lowy];
&nbsp;            for (y = lowy; y &lt;= hiy; y++)
&nbsp;                lev++-&gt;typ = ROOM;
&nbsp;        }
&nbsp;        if (is_room) {
&nbsp;            levl[lowx - 1][lowy - 1].typ = TLCORNER;
&nbsp;            levl[hix + 1][lowy - 1].typ = TRCORNER;
&nbsp;            levl[lowx - 1][hiy + 1].typ = BLCORNER;
&nbsp;            levl[hix + 1][hiy + 1].typ = BRCORNER;
&nbsp;        } else { /* a subroom */
&nbsp;            wallification(lowx - 1, lowy - 1, hix + 1, hiy + 1);
&nbsp;        }
&nbsp;    }
&nbsp;}
&nbsp;
add_room
&nbsp;void
&nbsp;add_room(lowx, lowy, hix, hiy, lit, rtype, special)
&nbsp;int lowx, lowy, hix, hiy;
&nbsp;boolean lit;
&nbsp;schar rtype;
&nbsp;boolean special;
&nbsp;{
&nbsp;    register struct mkroom *croom;
&nbsp;
&nbsp;    croom = &amp;rooms[nroom];
&nbsp;    do_room_or_subroom(croom, lowx, lowy, hix, hiy, lit, rtype, special,
&nbsp;                       (boolean) TRUE);
&nbsp;    croom++;
&nbsp;    croom-&gt;hx = -1;
&nbsp;    nroom++;
&nbsp;}
&nbsp;
add_subroom
&nbsp;void
&nbsp;add_subroom(proom, lowx, lowy, hix, hiy, lit, rtype, special)
&nbsp;struct mkroom *proom;
&nbsp;int lowx, lowy, hix, hiy;
&nbsp;boolean lit;
&nbsp;schar rtype;
&nbsp;boolean special;
&nbsp;{
&nbsp;    register struct mkroom *croom;
&nbsp;
&nbsp;    croom = &amp;subrooms[nsubroom];
&nbsp;    do_room_or_subroom(croom, lowx, lowy, hix, hiy, lit, rtype, special,
&nbsp;                       (boolean) FALSE);
&nbsp;    proom-&gt;sbrooms[proom-&gt;nsubrooms++] = croom;
&nbsp;    croom++;
&nbsp;    croom-&gt;hx = -1;
&nbsp;    nsubroom++;
&nbsp;}
&nbsp;
makerooms
&nbsp;STATIC_OVL void
&nbsp;makerooms()
&nbsp;{
&nbsp;    boolean tried_vault = FALSE;
&nbsp;
&nbsp;    /* make rooms until satisfied */
&nbsp;    /* rnd_rect() will returns 0 if no more rects are available... */
&nbsp;    while (nroom &lt; MAXNROFROOMS &amp;&amp; rnd_rect()) {
&nbsp;        if (nroom &gt;= (MAXNROFROOMS / 6) &amp;&amp; rn2(2) &amp;&amp; !tried_vault) {
&nbsp;            tried_vault = TRUE;
&nbsp;            if (create_vault()) {
&nbsp;                vault_x = rooms[nroom].lx;
&nbsp;                vault_y = rooms[nroom].ly;
&nbsp;                rooms[nroom].hx = -1;
&nbsp;            }
&nbsp;        } else if (!create_room(-1, -1, -1, -1, -1, -1, OROOM, -1))
&nbsp;            return;
&nbsp;    }
&nbsp;    return;
&nbsp;}
&nbsp;
join
&nbsp;STATIC_OVL void
&nbsp;join(a, b, nxcor)
&nbsp;register int a, b;
&nbsp;boolean nxcor;
&nbsp;{
&nbsp;    coord cc, tt, org, dest;
&nbsp;    register xchar tx, ty, xx, yy;
&nbsp;    register struct mkroom *croom, *troom;
&nbsp;    register int dx, dy;
&nbsp;
&nbsp;    croom = &amp;rooms[a];
&nbsp;    troom = &amp;rooms[b];
&nbsp;
&nbsp;    /* find positions cc and tt for doors in croom and troom
&nbsp;       and direction for a corridor between them */
&nbsp;
&nbsp;    if (troom-&gt;hx &lt; 0 || croom-&gt;hx &lt; 0 || doorindex &gt;= DOORMAX)
&nbsp;        return;
&nbsp;    if (troom-&gt;lx &gt; croom-&gt;hx) {
&nbsp;        dx = 1;
&nbsp;        dy = 0;
&nbsp;        xx = croom-&gt;hx + 1;
&nbsp;        tx = troom-&gt;lx - 1;
&nbsp;        finddpos(&amp;cc, xx, croom-&gt;ly, xx, croom-&gt;hy);
&nbsp;        finddpos(&amp;tt, tx, troom-&gt;ly, tx, troom-&gt;hy);
&nbsp;    } else if (troom-&gt;hy &lt; croom-&gt;ly) {
&nbsp;        dy = -1;
&nbsp;        dx = 0;
&nbsp;        yy = croom-&gt;ly - 1;
&nbsp;        finddpos(&amp;cc, croom-&gt;lx, yy, croom-&gt;hx, yy);
&nbsp;        ty = troom-&gt;hy + 1;
&nbsp;        finddpos(&amp;tt, troom-&gt;lx, ty, troom-&gt;hx, ty);
&nbsp;    } else if (troom-&gt;hx &lt; croom-&gt;lx) {
&nbsp;        dx = -1;
&nbsp;        dy = 0;
&nbsp;        xx = croom-&gt;lx - 1;
&nbsp;        tx = troom-&gt;hx + 1;
&nbsp;        finddpos(&amp;cc, xx, croom-&gt;ly, xx, croom-&gt;hy);
&nbsp;        finddpos(&amp;tt, tx, troom-&gt;ly, tx, troom-&gt;hy);
&nbsp;    } else {
&nbsp;        dy = 1;
&nbsp;        dx = 0;
&nbsp;        yy = croom-&gt;hy + 1;
&nbsp;        ty = troom-&gt;ly - 1;
&nbsp;        finddpos(&amp;cc, croom-&gt;lx, yy, croom-&gt;hx, yy);
&nbsp;        finddpos(&amp;tt, troom-&gt;lx, ty, troom-&gt;hx, ty);
&nbsp;    }
&nbsp;    xx = cc.x;
&nbsp;    yy = cc.y;
&nbsp;    tx = tt.x - dx;
&nbsp;    ty = tt.y - dy;
&nbsp;    if (nxcor &amp;&amp; levl[xx + dx][yy + dy].typ)
&nbsp;        return;
&nbsp;    if (okdoor(xx, yy) || !nxcor)
&nbsp;        dodoor(xx, yy, croom);
&nbsp;
&nbsp;    org.x = xx + dx;
&nbsp;    org.y = yy + dy;
&nbsp;    dest.x = tx;
&nbsp;    dest.y = ty;
&nbsp;
&nbsp;    if (!dig_corridor(&amp;org, &amp;dest, nxcor, level.flags.arboreal ? ROOM : CORR,
&nbsp;                      STONE))
&nbsp;        return;
&nbsp;
&nbsp;    /* we succeeded in digging the corridor */
&nbsp;    if (okdoor(tt.x, tt.y) || !nxcor)
&nbsp;        dodoor(tt.x, tt.y, troom);
&nbsp;
&nbsp;    if (smeq[a] &lt; smeq[b])
&nbsp;        smeq[b] = smeq[a];
&nbsp;    else
&nbsp;        smeq[a] = smeq[b];
&nbsp;}
&nbsp;
makecorridors
&nbsp;void
&nbsp;makecorridors()
&nbsp;{
&nbsp;    int a, b, i;
&nbsp;    boolean any = TRUE;
&nbsp;
&nbsp;    for (a = 0; a &lt; nroom - 1; a++) {
&nbsp;        join(a, a + 1, FALSE);
&nbsp;        if (!rn2(50))
&nbsp;            break; /* allow some randomness */
&nbsp;    }
&nbsp;    for (a = 0; a &lt; nroom - 2; a++)
&nbsp;        if (smeq[a] != smeq[a + 2])
&nbsp;            join(a, a + 2, FALSE);
&nbsp;    for (a = 0; any &amp;&amp; a &lt; nroom; a++) {
&nbsp;        any = FALSE;
&nbsp;        for (b = 0; b &lt; nroom; b++)
&nbsp;            if (smeq[a] != smeq[b]) {
&nbsp;                join(a, b, FALSE);
&nbsp;                any = TRUE;
&nbsp;            }
&nbsp;    }
&nbsp;    if (nroom &gt; 2)
&nbsp;        for (i = rn2(nroom) + 4; i; i--) {
&nbsp;            a = rn2(nroom);
&nbsp;            b = rn2(nroom - 2);
&nbsp;            if (b &gt;= a)
&nbsp;                b += 2;
&nbsp;            join(a, b, TRUE);
&nbsp;        }
&nbsp;}
&nbsp;
add_door
&nbsp;void
&nbsp;add_door(x, y, aroom)
&nbsp;register int x, y;
&nbsp;register struct mkroom *aroom;
&nbsp;{
&nbsp;    register struct mkroom *broom;
&nbsp;    register int tmp;
&nbsp;    int i;
&nbsp;
&nbsp;    if (aroom-&gt;doorct == 0)
&nbsp;        aroom-&gt;fdoor = doorindex;
&nbsp;
&nbsp;    aroom-&gt;doorct++;
&nbsp;
&nbsp;    for (tmp = doorindex; tmp &gt; aroom-&gt;fdoor; tmp--)
&nbsp;        doors[tmp] = doors[tmp - 1];
&nbsp;
&nbsp;    for (i = 0; i &lt; nroom; i++) {
&nbsp;        broom = &amp;rooms[i];
&nbsp;        if (broom != aroom &amp;&amp; broom-&gt;doorct &amp;&amp; broom-&gt;fdoor &gt;= aroom-&gt;fdoor)
&nbsp;            broom-&gt;fdoor++;
&nbsp;    }
&nbsp;    for (i = 0; i &lt; nsubroom; i++) {
&nbsp;        broom = &amp;subrooms[i];
&nbsp;        if (broom != aroom &amp;&amp; broom-&gt;doorct &amp;&amp; broom-&gt;fdoor &gt;= aroom-&gt;fdoor)
&nbsp;            broom-&gt;fdoor++;
&nbsp;    }
&nbsp;
&nbsp;    doorindex++;
&nbsp;    doors[aroom-&gt;fdoor].x = x;
&nbsp;    doors[aroom-&gt;fdoor].y = y;
&nbsp;}
&nbsp;
dosdoor
&nbsp;STATIC_OVL void
&nbsp;dosdoor(x, y, aroom, type)
&nbsp;register xchar x, y;
&nbsp;struct mkroom *aroom;
&nbsp;int type;
&nbsp;{
&nbsp;    boolean shdoor = *in_rooms(x, y, SHOPBASE) ? TRUE : FALSE;
&nbsp;
&nbsp;    if (!IS_WALL(levl[x][y].typ)) /* avoid SDOORs on already made doors */
&nbsp;        type = DOOR;
&nbsp;    levl[x][y].typ = type;
&nbsp;    if (type == DOOR) {
&nbsp;        if (!rn2(3)) { /* is it a locked door, closed, or a doorway? */
&nbsp;            if (!rn2(5))
&nbsp;                levl[x][y].doormask = D_ISOPEN;
&nbsp;            else if (!rn2(6))
&nbsp;                levl[x][y].doormask = D_LOCKED;
&nbsp;            else
&nbsp;                levl[x][y].doormask = D_CLOSED;
&nbsp;
&nbsp;            if (levl[x][y].doormask != D_ISOPEN &amp;&amp; !shdoor
&nbsp;                &amp;&amp; level_difficulty() &gt;= 5 &amp;&amp; !rn2(25))
&nbsp;                levl[x][y].doormask |= D_TRAPPED;
&nbsp;        } else {
&nbsp;#ifdef STUPID
&nbsp;            if (shdoor)
&nbsp;                levl[x][y].doormask = D_ISOPEN;
&nbsp;            else
&nbsp;                levl[x][y].doormask = D_NODOOR;
&nbsp;#else
&nbsp;            levl[x][y].doormask = (shdoor ? D_ISOPEN : D_NODOOR);
&nbsp;#endif
&nbsp;        }
&nbsp;
&nbsp;        /* also done in roguecorr(); doing it here first prevents
&nbsp;           making mimics in place of trapped doors on rogue level */
&nbsp;        if (Is_rogue_level(&amp;u.uz))
&nbsp;            levl[x][y].doormask = D_NODOOR;
&nbsp;
&nbsp;        if (levl[x][y].doormask &amp; D_TRAPPED) {
&nbsp;            struct monst *mtmp;
&nbsp;
&nbsp;            if (level_difficulty() &gt;= 9 &amp;&amp; !rn2(5)
&nbsp;                &amp;&amp; !((mvitals[PM_SMALL_MIMIC].mvflags &amp; G_GONE)
&nbsp;                     &amp;&amp; (mvitals[PM_LARGE_MIMIC].mvflags &amp; G_GONE)
&nbsp;                     &amp;&amp; (mvitals[PM_GIANT_MIMIC].mvflags &amp; G_GONE))) {
&nbsp;                /* make a mimic instead */
&nbsp;                levl[x][y].doormask = D_NODOOR;
&nbsp;                mtmp = makemon(mkclass(S_MIMIC, 0), x, y, NO_MM_FLAGS);
&nbsp;                if (mtmp)
&nbsp;                    set_mimic_sym(mtmp);
&nbsp;            }
&nbsp;        }
&nbsp;        /* newsym(x,y); */
&nbsp;    } else { /* SDOOR */
&nbsp;        if (shdoor || !rn2(5))
&nbsp;            levl[x][y].doormask = D_LOCKED;
&nbsp;        else
&nbsp;            levl[x][y].doormask = D_CLOSED;
&nbsp;
&nbsp;        if (!shdoor &amp;&amp; level_difficulty() &gt;= 4 &amp;&amp; !rn2(20))
&nbsp;            levl[x][y].doormask |= D_TRAPPED;
&nbsp;    }
&nbsp;
&nbsp;    add_door(x, y, aroom);
&nbsp;}
&nbsp;
place_niche
&nbsp;STATIC_OVL boolean
&nbsp;place_niche(aroom, dy, xx, yy)
&nbsp;register struct mkroom *aroom;
&nbsp;int *dy, *xx, *yy;
&nbsp;{
&nbsp;    coord dd;
&nbsp;
&nbsp;    if (rn2(2)) {
&nbsp;        *dy = 1;
&nbsp;        finddpos(ⅆ, aroom-&gt;lx, aroom-&gt;hy + 1, aroom-&gt;hx, aroom-&gt;hy + 1);
&nbsp;    } else {
&nbsp;        *dy = -1;
&nbsp;        finddpos(ⅆ, aroom-&gt;lx, aroom-&gt;ly - 1, aroom-&gt;hx, aroom-&gt;ly - 1);
&nbsp;    }
&nbsp;    *xx = dd.x;
&nbsp;    *yy = dd.y;
&nbsp;    return (boolean) ((isok(*xx, *yy + *dy)
&nbsp;                       &amp;&amp; levl[*xx][*yy + *dy].typ == STONE)
&nbsp;                      &amp;&amp; (isok(*xx, *yy - *dy)
&nbsp;                          &amp;&amp; !IS_POOL(levl[*xx][*yy - *dy].typ)
&nbsp;                          &amp;&amp; !IS_FURNITURE(levl[*xx][*yy - *dy].typ)));
&nbsp;}
&nbsp;
&nbsp;/* there should be one of these per trap, in the same order as trap.h */
&nbsp;static NEARDATA const char *trap_engravings[TRAPNUM] = {
&nbsp;    (char *) 0,      (char *) 0,    (char *) 0,    (char *) 0, (char *) 0,
&nbsp;    (char *) 0,      (char *) 0,    (char *) 0,    (char *) 0, (char *) 0,
&nbsp;    (char *) 0,      (char *) 0,    (char *) 0,    (char *) 0,
&nbsp;    /* 14..16: trap door, teleport, level-teleport */
&nbsp;    "Vlad was here", "ad aerarium", "ad aerarium", (char *) 0, (char *) 0,
&nbsp;    (char *) 0,      (char *) 0,    (char *) 0,    (char *) 0, (char *) 0,
&nbsp;};
&nbsp;
makeniche
&nbsp;STATIC_OVL void
&nbsp;makeniche(trap_type)
&nbsp;int trap_type;
&nbsp;{
&nbsp;    register struct mkroom *aroom;
&nbsp;    struct rm *rm;
&nbsp;    int vct = 8;
&nbsp;    int dy, xx, yy;
&nbsp;    struct trap *ttmp;
&nbsp;
&nbsp;    if (doorindex &lt; DOORMAX) {
&nbsp;        while (vct--) {
&nbsp;            aroom = &amp;rooms[rn2(nroom)];
&nbsp;            if (aroom-&gt;rtype != OROOM)
&nbsp;                continue; /* not an ordinary room */
&nbsp;            if (aroom-&gt;doorct == 1 &amp;&amp; rn2(5))
&nbsp;                continue;
&nbsp;            if (!place_niche(aroom, &amp;dy, &amp;xx, &amp;yy))
&nbsp;                continue;
&nbsp;
&nbsp;            rm = &amp;levl[xx][yy + dy];
&nbsp;            if (trap_type || !rn2(4)) {
&nbsp;                rm-&gt;typ = SCORR;
&nbsp;                if (trap_type) {
&nbsp;                    if ((trap_type == HOLE || trap_type == TRAPDOOR)
&nbsp;                        &amp;&amp; !Can_fall_thru(&amp;u.uz))
&nbsp;                        trap_type = ROCKTRAP;
&nbsp;                    ttmp = maketrap(xx, yy + dy, trap_type);
&nbsp;                    if (ttmp) {
&nbsp;                        if (trap_type != ROCKTRAP)
&nbsp;                            ttmp-&gt;once = 1;
&nbsp;                        if (trap_engravings[trap_type]) {
&nbsp;                            make_engr_at(xx, yy - dy,
&nbsp;                                         trap_engravings[trap_type], 0L,
&nbsp;                                         DUST);
&nbsp;                            wipe_engr_at(xx, yy - dy, 5,
&nbsp;                                         FALSE); /* age it a little */
&nbsp;                        }
&nbsp;                    }
&nbsp;                }
&nbsp;                dosdoor(xx, yy, aroom, SDOOR);
&nbsp;            } else {
&nbsp;                rm-&gt;typ = CORR;
&nbsp;                if (rn2(7))
&nbsp;                    dosdoor(xx, yy, aroom, rn2(5) ? SDOOR : DOOR);
&nbsp;                else {
&nbsp;                    /* inaccessible niches occasionally have iron bars */
&nbsp;                    if (!rn2(5) &amp;&amp; IS_WALL(levl[xx][yy].typ)) {
&nbsp;                        levl[xx][yy].typ = IRONBARS;
&nbsp;                        if (rn2(3))
&nbsp;                            (void) mkcorpstat(CORPSE, (struct monst *) 0,
&nbsp;                                              mkclass(S_HUMAN, 0), xx,
&nbsp;                                              yy + dy, TRUE);
&nbsp;                    }
&nbsp;                    if (!level.flags.noteleport)
&nbsp;                        (void) mksobj_at(SCR_TELEPORTATION, xx, yy + dy, TRUE,
&nbsp;                                         FALSE);
&nbsp;                    if (!rn2(3))
&nbsp;                        (void) mkobj_at(0, xx, yy + dy, TRUE);
&nbsp;                }
&nbsp;            }
&nbsp;            return;
&nbsp;        }
&nbsp;    }
&nbsp;}
&nbsp;
make_niches
&nbsp;STATIC_OVL void
&nbsp;make_niches()
&nbsp;{
&nbsp;    int ct = rnd((nroom &gt;&gt; 1) + 1), dep = depth(&amp;u.uz);
&nbsp;    boolean ltptr = (!level.flags.noteleport &amp;&amp; dep &gt; 15),
&nbsp;            vamp = (dep &gt; 5 &amp;&amp; dep &lt; 25);
&nbsp;
&nbsp;    while (ct--) {
&nbsp;        if (ltptr &amp;&amp; !rn2(6)) {
&nbsp;            ltptr = FALSE;
&nbsp;            makeniche(LEVEL_TELEP);
&nbsp;        } else if (vamp &amp;&amp; !rn2(6)) {
&nbsp;            vamp = FALSE;
&nbsp;            makeniche(TRAPDOOR);
&nbsp;        } else
&nbsp;            makeniche(NO_TRAP);
&nbsp;    }
&nbsp;}
&nbsp;
makevtele
&nbsp;STATIC_OVL void
&nbsp;makevtele()
&nbsp;{
&nbsp;    makeniche(TELEP_TRAP);
&nbsp;}
&nbsp;
clear_level_structures
&nbsp;/* clear out various globals that keep information on the current level.
&nbsp; * some of this is only necessary for some types of levels (maze, normal,
&nbsp; * special) but it's easier to put it all in one place than make sure
&nbsp; * each type initializes what it needs to separately.
&nbsp; */
&nbsp;STATIC_OVL void
&nbsp;clear_level_structures()
&nbsp;{
&nbsp;    static struct rm zerorm = { cmap_to_glyph(S_stone),
&nbsp;                                0, 0, 0, 0, 0, 0, 0, 0, 0 };
&nbsp;    register int x, y;
&nbsp;    register struct rm *lev;
&nbsp;
&nbsp;    for (x = 0; x &lt; COLNO; x++) {
&nbsp;        lev = &amp;levl[x][0];
&nbsp;        for (y = 0; y &lt; ROWNO; y++) {
&nbsp;            *lev++ = zerorm;
&nbsp;            /*
&nbsp;             * These used to be '#if MICROPORT_BUG',
&nbsp;             * with use of memset(0) for '#if !MICROPORT_BUG' below,
&nbsp;             * but memset is not appropriate for initializing pointers,
&nbsp;             * so do these level.objects[][] and level.monsters[][]
&nbsp;             * initializations unconditionally.
&nbsp;             */
&nbsp;            level.objects[x][y] = (struct obj *) 0;
&nbsp;            level.monsters[x][y] = (struct monst *) 0;
&nbsp;        }
&nbsp;    }
&nbsp;    level.objlist = (struct obj *) 0;
&nbsp;    level.buriedobjlist = (struct obj *) 0;
&nbsp;    level.monlist = (struct monst *) 0;
&nbsp;    level.damagelist = (struct damage *) 0;
&nbsp;    level.bonesinfo = (struct cemetery *) 0;
&nbsp;
&nbsp;    level.flags.nfountains = 0;
&nbsp;    level.flags.nsinks = 0;
&nbsp;    level.flags.has_shop = 0;
&nbsp;    level.flags.has_vault = 0;
&nbsp;    level.flags.has_zoo = 0;
&nbsp;    level.flags.has_court = 0;
&nbsp;    level.flags.has_morgue = level.flags.graveyard = 0;
&nbsp;    level.flags.has_beehive = 0;
&nbsp;    level.flags.has_barracks = 0;
&nbsp;    level.flags.has_temple = 0;
&nbsp;    level.flags.has_swamp = 0;
&nbsp;    level.flags.noteleport = 0;
&nbsp;    level.flags.hardfloor = 0;
&nbsp;    level.flags.nommap = 0;
&nbsp;    level.flags.hero_memory = 1;
&nbsp;    level.flags.shortsighted = 0;
&nbsp;    level.flags.sokoban_rules = 0;
&nbsp;    level.flags.is_maze_lev = 0;
&nbsp;    level.flags.is_cavernous_lev = 0;
&nbsp;    level.flags.arboreal = 0;
&nbsp;    level.flags.wizard_bones = 0;
&nbsp;    level.flags.corrmaze = 0;
&nbsp;
&nbsp;    nroom = 0;
&nbsp;    rooms[0].hx = -1;
&nbsp;    nsubroom = 0;
&nbsp;    subrooms[0].hx = -1;
&nbsp;    doorindex = 0;
&nbsp;    init_rect();
&nbsp;    init_vault();
&nbsp;    xdnstair = ydnstair = xupstair = yupstair = 0;
&nbsp;    sstairs.sx = sstairs.sy = 0;
&nbsp;    xdnladder = ydnladder = xupladder = yupladder = 0;
&nbsp;    made_branch = FALSE;
&nbsp;    clear_regions();
&nbsp;}
&nbsp;
makelevel
&nbsp;STATIC_OVL void
&nbsp;makelevel()
&nbsp;{
&nbsp;    register struct mkroom *croom, *troom;
&nbsp;    register int tryct;
&nbsp;    register int x, y;
&nbsp;    struct monst *tmonst; /* always put a web with a spider */
&nbsp;    branch *branchp;
&nbsp;    int room_threshold;
&nbsp;
&nbsp;    if (wiz1_level.dlevel == 0)
&nbsp;        init_dungeons();
&nbsp;    oinit(); /* assign level dependent obj probabilities */
&nbsp;    clear_level_structures();
&nbsp;
&nbsp;    {
&nbsp;        register s_level *slev = Is_special(&amp;u.uz);
&nbsp;
&nbsp;        /* check for special levels */
&nbsp;        if (slev &amp;&amp; !Is_rogue_level(&amp;u.uz)) {
&nbsp;            makemaz(slev-&gt;proto);
&nbsp;            return;
&nbsp;        } else if (dungeons[u.uz.dnum].proto[0]) {
&nbsp;            makemaz("");
&nbsp;            return;
&nbsp;        } else if (In_mines(&amp;u.uz)) {
&nbsp;            makemaz("minefill");
&nbsp;            return;
&nbsp;        } else if (In_quest(&amp;u.uz)) {
&nbsp;            char fillname[9];
&nbsp;            s_level *loc_lev;
&nbsp;
&nbsp;            Sprintf(fillname, "%s-loca", urole.filecode);
&nbsp;            loc_lev = find_level(fillname);
&nbsp;
&nbsp;            Sprintf(fillname, "%s-fil", urole.filecode);
&nbsp;            Strcat(fillname,
&nbsp;                   (u.uz.dlevel &lt; loc_lev-&gt;dlevel.dlevel) ? "a" : "b");
&nbsp;            makemaz(fillname);
&nbsp;            return;
&nbsp;        } else if (In_hell(&amp;u.uz)
&nbsp;                   || (rn2(5) &amp;&amp; u.uz.dnum == medusa_level.dnum
&nbsp;                       &amp;&amp; depth(&amp;u.uz) &gt; depth(&amp;medusa_level))) {
&nbsp;            makemaz("");
&nbsp;            return;
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /* otherwise, fall through - it's a "regular" level. */
&nbsp;
&nbsp;    if (Is_rogue_level(&amp;u.uz)) {
&nbsp;        makeroguerooms();
&nbsp;        makerogueghost();
&nbsp;    } else
&nbsp;        makerooms();
&nbsp;    sort_rooms();
&nbsp;
&nbsp;    /* construct stairs (up and down in different rooms if possible) */
&nbsp;    croom = &amp;rooms[rn2(nroom)];
&nbsp;    if (!Is_botlevel(&amp;u.uz))
&nbsp;        mkstairs(somex(croom), somey(croom), 0, croom); /* down */
&nbsp;    if (nroom &gt; 1) {
&nbsp;        troom = croom;
&nbsp;        croom = &amp;rooms[rn2(nroom - 1)];
&nbsp;        if (croom == troom)
&nbsp;            croom++;
&nbsp;    }
&nbsp;
&nbsp;    if (u.uz.dlevel != 1) {
&nbsp;        xchar sx, sy;
&nbsp;        do {
&nbsp;            sx = somex(croom);
&nbsp;            sy = somey(croom);
&nbsp;        } while (occupied(sx, sy));
&nbsp;        mkstairs(sx, sy, 1, croom); /* up */
&nbsp;    }
&nbsp;
&nbsp;    branchp = Is_branchlev(&amp;u.uz);    /* possible dungeon branch */
&nbsp;    room_threshold = branchp ? 4 : 3; /* minimum number of rooms needed
&nbsp;                                         to allow a random special room */
&nbsp;    if (Is_rogue_level(&amp;u.uz))
&nbsp;        goto skip0;
&nbsp;    makecorridors();
&nbsp;    make_niches();
&nbsp;
&nbsp;    /* make a secret treasure vault, not connected to the rest */
&nbsp;    if (do_vault()) {
&nbsp;        xchar w, h;
&nbsp;        debugpline0("trying to make a vault...");
&nbsp;        w = 1;
&nbsp;        h = 1;
&nbsp;        if (check_room(&amp;vault_x, &amp;w, &amp;vault_y, &amp;h, TRUE)) {
&nbsp;        fill_vault:
&nbsp;            add_room(vault_x, vault_y, vault_x + w, vault_y + h, TRUE, VAULT,
&nbsp;                     FALSE);
&nbsp;            level.flags.has_vault = 1;
&nbsp;            ++room_threshold;
&nbsp;            fill_room(&amp;rooms[nroom - 1], FALSE);
&nbsp;            mk_knox_portal(vault_x + w, vault_y + h);
&nbsp;            if (!level.flags.noteleport &amp;&amp; !rn2(3))
&nbsp;                makevtele();
&nbsp;        } else if (rnd_rect() &amp;&amp; create_vault()) {
&nbsp;            vault_x = rooms[nroom].lx;
&nbsp;            vault_y = rooms[nroom].ly;
&nbsp;            if (check_room(&amp;vault_x, &amp;w, &amp;vault_y, &amp;h, TRUE))
&nbsp;                goto fill_vault;
&nbsp;            else
&nbsp;                rooms[nroom].hx = -1;
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    {
&nbsp;        register int u_depth = depth(&amp;u.uz);
&nbsp;
&nbsp;        if (wizard &amp;&amp; nh_getenv("SHOPTYPE"))
&nbsp;            mkroom(SHOPBASE);
&nbsp;        else if (u_depth &gt; 1 &amp;&amp; u_depth &lt; depth(&amp;medusa_level)
&nbsp;                 &amp;&amp; nroom &gt;= room_threshold &amp;&amp; rn2(u_depth) &lt; 3)
&nbsp;            mkroom(SHOPBASE);
&nbsp;        else if (u_depth &gt; 4 &amp;&amp; !rn2(6))
&nbsp;            mkroom(COURT);
&nbsp;        else if (u_depth &gt; 5 &amp;&amp; !rn2(8)
&nbsp;                 &amp;&amp; !(mvitals[PM_LEPRECHAUN].mvflags &amp; G_GONE))
&nbsp;            mkroom(LEPREHALL);
&nbsp;        else if (u_depth &gt; 6 &amp;&amp; !rn2(7))
&nbsp;            mkroom(ZOO);
&nbsp;        else if (u_depth &gt; 8 &amp;&amp; !rn2(5))
&nbsp;            mkroom(TEMPLE);
&nbsp;        else if (u_depth &gt; 9 &amp;&amp; !rn2(5)
&nbsp;                 &amp;&amp; !(mvitals[PM_KILLER_BEE].mvflags &amp; G_GONE))
&nbsp;            mkroom(BEEHIVE);
&nbsp;        else if (u_depth &gt; 11 &amp;&amp; !rn2(6))
&nbsp;            mkroom(MORGUE);
&nbsp;        else if (u_depth &gt; 12 &amp;&amp; !rn2(8) &amp;&amp; antholemon())
&nbsp;            mkroom(ANTHOLE);
&nbsp;        else if (u_depth &gt; 14 &amp;&amp; !rn2(4)
&nbsp;                 &amp;&amp; !(mvitals[PM_SOLDIER].mvflags &amp; G_GONE))
&nbsp;            mkroom(BARRACKS);
&nbsp;        else if (u_depth &gt; 15 &amp;&amp; !rn2(6))
&nbsp;            mkroom(SWAMP);
&nbsp;        else if (u_depth &gt; 16 &amp;&amp; !rn2(8)
&nbsp;                 &amp;&amp; !(mvitals[PM_COCKATRICE].mvflags &amp; G_GONE))
&nbsp;            mkroom(COCKNEST);
&nbsp;    }
&nbsp;
&nbsp;skip0:
&nbsp;    /* Place multi-dungeon branch. */
&nbsp;    place_branch(branchp, 0, 0);
&nbsp;
&nbsp;    /* for each room: put things inside */
&nbsp;    for (croom = rooms; croom-&gt;hx &gt; 0; croom++) {
&nbsp;        if (croom-&gt;rtype != OROOM)
&nbsp;            continue;
&nbsp;
&nbsp;        /* put a sleeping monster inside */
&nbsp;        /* Note: monster may be on the stairs. This cannot be
&nbsp;           avoided: maybe the player fell through a trap door
&nbsp;           while a monster was on the stairs. Conclusion:
&nbsp;           we have to check for monsters on the stairs anyway. */
&nbsp;
&nbsp;        if (u.uhave.amulet || !rn2(3)) {
&nbsp;            x = somex(croom);
&nbsp;            y = somey(croom);
&nbsp;            tmonst = makemon((struct permonst *) 0, x, y, NO_MM_FLAGS);
&nbsp;            if (tmonst &amp;&amp; tmonst-&gt;data == &amp;mons[PM_GIANT_SPIDER]
&nbsp;                &amp;&amp; !occupied(x, y))
&nbsp;                (void) maketrap(x, y, WEB);
&nbsp;        }
&nbsp;        /* put traps and mimics inside */
&nbsp;        x = 8 - (level_difficulty() / 6);
&nbsp;        if (x &lt;= 1)
&nbsp;            x = 2;
&nbsp;        while (!rn2(x))
&nbsp;            mktrap(0, 0, croom, (coord *) 0);
&nbsp;        if (!rn2(3))
&nbsp;            (void) mkgold(0L, somex(croom), somey(croom));
&nbsp;        if (Is_rogue_level(&amp;u.uz))
&nbsp;            goto skip_nonrogue;
&nbsp;        if (!rn2(10))
&nbsp;            mkfount(0, croom);
&nbsp;        if (!rn2(60))
&nbsp;            mksink(croom);
&nbsp;        if (!rn2(60))
&nbsp;            mkaltar(croom);
&nbsp;        x = 80 - (depth(&amp;u.uz) * 2);
&nbsp;        if (x &lt; 2)
&nbsp;            x = 2;
&nbsp;        if (!rn2(x))
&nbsp;            mkgrave(croom);
&nbsp;
&nbsp;        /* put statues inside */
&nbsp;        if (!rn2(20))
&nbsp;            (void) mkcorpstat(STATUE, (struct monst *) 0,
&nbsp;                              (struct permonst *) 0, somex(croom),
&nbsp;                              somey(croom), CORPSTAT_INIT);
&nbsp;        /* put box/chest inside;
&nbsp;         *  40% chance for at least 1 box, regardless of number
&nbsp;         *  of rooms; about 5 - 7.5% for 2 boxes, least likely
&nbsp;         *  when few rooms; chance for 3 or more is negligible.
&nbsp;         */
&nbsp;        if (!rn2(nroom * 5 / 2))
&nbsp;            (void) mksobj_at((rn2(3)) ? LARGE_BOX : CHEST, somex(croom),
&nbsp;                             somey(croom), TRUE, FALSE);
&nbsp;
&nbsp;        /* maybe make some graffiti */
&nbsp;        if (!rn2(27 + 3 * abs(depth(&amp;u.uz)))) {
&nbsp;            char buf[BUFSZ];
&nbsp;            const char *mesg = random_engraving(buf);
&nbsp;            if (mesg) {
&nbsp;                do {
&nbsp;                    x = somex(croom);
&nbsp;                    y = somey(croom);
&nbsp;                } while (levl[x][y].typ != ROOM &amp;&amp; !rn2(40));
&nbsp;                if (!(IS_POOL(levl[x][y].typ)
&nbsp;                      || IS_FURNITURE(levl[x][y].typ)))
&nbsp;                    make_engr_at(x, y, mesg, 0L, MARK);
&nbsp;            }
&nbsp;        }
&nbsp;
&nbsp;    skip_nonrogue:
&nbsp;        if (!rn2(3)) {
&nbsp;            (void) mkobj_at(0, somex(croom), somey(croom), TRUE);
&nbsp;            tryct = 0;
&nbsp;            while (!rn2(5)) {
&nbsp;                if (++tryct &gt; 100) {
&nbsp;                    impossible("tryct overflow4");
&nbsp;                    break;
&nbsp;                }
&nbsp;                (void) mkobj_at(0, somex(croom), somey(croom), TRUE);
&nbsp;            }
&nbsp;        }
&nbsp;    }
&nbsp;}
&nbsp;
mineralize
&nbsp;/*
&nbsp; *      Place deposits of minerals (gold and misc gems) in the stone
&nbsp; *      surrounding the rooms on the map.
&nbsp; *      Also place kelp in water.
&nbsp; *      mineralize(-1, -1, -1, -1, FALSE); =&gt; "default" behaviour
&nbsp; */
&nbsp;void
&nbsp;mineralize(kelp_pool, kelp_moat, goldprob, gemprob, skip_lvl_checks)
&nbsp;int kelp_pool, kelp_moat, goldprob, gemprob;
&nbsp;boolean skip_lvl_checks;
&nbsp;{
&nbsp;    s_level *sp;
&nbsp;    struct obj *otmp;
&nbsp;    int x, y, cnt;
&nbsp;
&nbsp;    if (kelp_pool &lt; 0)
&nbsp;        kelp_pool = 10;
&nbsp;    if (kelp_moat &lt; 0)
&nbsp;        kelp_moat = 30;
&nbsp;
&nbsp;    /* Place kelp, except on the plane of water */
&nbsp;    if (!skip_lvl_checks &amp;&amp; In_endgame(&amp;u.uz))
&nbsp;        return;
&nbsp;    for (x = 2; x &lt; (COLNO - 2); x++)
&nbsp;        for (y = 1; y &lt; (ROWNO - 1); y++)
&nbsp;            if ((kelp_pool &amp;&amp; levl[x][y].typ == POOL &amp;&amp; !rn2(kelp_pool))
&nbsp;                || (kelp_moat &amp;&amp; levl[x][y].typ == MOAT &amp;&amp; !rn2(kelp_moat)))
&nbsp;                (void) mksobj_at(KELP_FROND, x, y, TRUE, FALSE);
&nbsp;
&nbsp;    /* determine if it is even allowed;
&nbsp;       almost all special levels are excluded */
&nbsp;    if (!skip_lvl_checks
&nbsp;        &amp;&amp; (In_hell(&amp;u.uz) || In_V_tower(&amp;u.uz) || Is_rogue_level(&amp;u.uz)
&nbsp;            || level.flags.arboreal
&nbsp;            || ((sp = Is_special(&amp;u.uz)) != 0 &amp;&amp; !Is_oracle_level(&amp;u.uz)
&nbsp;                &amp;&amp; (!In_mines(&amp;u.uz) || sp-&gt;flags.town))))
&nbsp;        return;
&nbsp;
&nbsp;    /* basic level-related probabilities */
&nbsp;    if (goldprob &lt; 0)
&nbsp;        goldprob = 20 + depth(&amp;u.uz) / 3;
&nbsp;    if (gemprob &lt; 0)
&nbsp;        gemprob = goldprob / 4;
&nbsp;
&nbsp;    /* mines have ***MORE*** goodies - otherwise why mine? */
&nbsp;    if (!skip_lvl_checks) {
&nbsp;        if (In_mines(&amp;u.uz)) {
&nbsp;            goldprob *= 2;
&nbsp;            gemprob *= 3;
&nbsp;        } else if (In_quest(&amp;u.uz)) {
&nbsp;            goldprob /= 4;
&nbsp;            gemprob /= 6;
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /*
&nbsp;     * Seed rock areas with gold and/or gems.
&nbsp;     * We use fairly low level object handling to avoid unnecessary
&nbsp;     * overhead from placing things in the floor chain prior to burial.
&nbsp;     */
&nbsp;    for (x = 2; x &lt; (COLNO - 2); x++)
&nbsp;        for (y = 1; y &lt; (ROWNO - 1); y++)
&nbsp;            if (levl[x][y + 1].typ != STONE) { /* 

<x,y> spot not eligible */
&nbsp;                y += 2; /* next two spots aren't eligible either */
&nbsp;            } else if (levl[x][y].typ != STONE) { /* this spot not eligible */
&nbsp;                y += 1; /* next spot isn't eligible either */
&nbsp;            } else if (!(levl[x][y].wall_info &amp; W_NONDIGGABLE)
&nbsp;                       &amp;&amp; levl[x][y - 1].typ == STONE
&nbsp;                       &amp;&amp; levl[x + 1][y - 1].typ == STONE
&nbsp;                       &amp;&amp; levl[x - 1][y - 1].typ == STONE
&nbsp;                       &amp;&amp; levl[x + 1][y].typ == STONE
&nbsp;                       &amp;&amp; levl[x - 1][y].typ == STONE
&nbsp;                       &amp;&amp; levl[x + 1][y + 1].typ == STONE
&nbsp;                       &amp;&amp; levl[x - 1][y + 1].typ == STONE) {
&nbsp;                if (rn2(1000) &lt; goldprob) {
&nbsp;                    if ((otmp = mksobj(GOLD_PIECE, FALSE, FALSE)) != 0) {
&nbsp;                        otmp-&gt;ox = x, otmp-&gt;oy = y;
&nbsp;                        otmp-&gt;quan = 1L + rnd(goldprob * 3);
&nbsp;                        otmp-&gt;owt = weight(otmp);
&nbsp;                        if (!rn2(3))
&nbsp;                            add_to_buried(otmp);
&nbsp;                        else
&nbsp;                            place_object(otmp, x, y);
&nbsp;                    }
&nbsp;                }
&nbsp;                if (rn2(1000) &lt; gemprob) {
&nbsp;                    for (cnt = rnd(2 + dunlev(&amp;u.uz) / 3); cnt &gt; 0; cnt--)
&nbsp;                        if ((otmp = mkobj(GEM_CLASS, FALSE)) != 0) {
&nbsp;                            if (otmp-&gt;otyp == ROCK) {
&nbsp;                                dealloc_obj(otmp); /* discard it */
&nbsp;                            } else {
&nbsp;                                otmp-&gt;ox = x, otmp-&gt;oy = y;
&nbsp;                                if (!rn2(3))
&nbsp;                                    add_to_buried(otmp);
&nbsp;                                else
&nbsp;                                    place_object(otmp, x, y);
&nbsp;                            }
&nbsp;                        }
&nbsp;                }
&nbsp;            }
&nbsp;}
&nbsp;
mklev
&nbsp;void
&nbsp;mklev()
&nbsp;{
&nbsp;    struct mkroom *croom;
&nbsp;    int ridx;
&nbsp;
&nbsp;    init_mapseen(&amp;u.uz);
&nbsp;    if (getbones())
&nbsp;        return;
&nbsp;
&nbsp;    in_mklev = TRUE;
&nbsp;    makelevel();
&nbsp;    bound_digging();
&nbsp;    mineralize(-1, -1, -1, -1, FALSE);
&nbsp;    in_mklev = FALSE;
&nbsp;    /* has_morgue gets cleared once morgue is entered; graveyard stays
&nbsp;       set (graveyard might already be set even when has_morgue is clear
&nbsp;       [see fixup_special()], so don't update it unconditionally) */
&nbsp;    if (level.flags.has_morgue)
&nbsp;        level.flags.graveyard = 1;
&nbsp;    if (!level.flags.is_maze_lev) {
&nbsp;        for (croom = &amp;rooms[0]; croom != &amp;rooms[nroom]; croom++)
&nbsp;#ifdef SPECIALIZATION
&nbsp;            topologize(croom, FALSE);
&nbsp;#else
&nbsp;            topologize(croom);
&nbsp;#endif
&nbsp;    }
&nbsp;    set_wall_state();
&nbsp;    /* for many room types, rooms[].rtype is zeroed once the room has been
&nbsp;       entered; rooms[].orig_rtype always retains original rtype value */
&nbsp;    for (ridx = 0; ridx &lt; SIZE(rooms); ridx++)
&nbsp;        rooms[ridx].orig_rtype = rooms[ridx].rtype;
&nbsp;}
&nbsp;
topologize
&nbsp;void
&nbsp;#ifdef SPECIALIZATION
&nbsp;topologize(croom, do_ordinary)
&nbsp;struct mkroom *croom;
&nbsp;boolean do_ordinary;
&nbsp;#else
&nbsp;topologize(croom)
&nbsp;struct mkroom *croom;
&nbsp;#endif
&nbsp;{
&nbsp;    register int x, y, roomno = (int) ((croom - rooms) + ROOMOFFSET);
&nbsp;    int lowx = croom-&gt;lx, lowy = croom-&gt;ly;
&nbsp;    int hix = croom-&gt;hx, hiy = croom-&gt;hy;
&nbsp;#ifdef SPECIALIZATION
&nbsp;    schar rtype = croom-&gt;rtype;
&nbsp;#endif
&nbsp;    int subindex, nsubrooms = croom-&gt;nsubrooms;
&nbsp;
&nbsp;    /* skip the room if already done; i.e. a shop handled out of order */
&nbsp;    /* also skip if this is non-rectangular (it _must_ be done already) */
&nbsp;    if ((int) levl[lowx][lowy].roomno == roomno || croom-&gt;irregular)
&nbsp;        return;
&nbsp;#ifdef SPECIALIZATION
&nbsp;    if (Is_rogue_level(&amp;u.uz))
&nbsp;        do_ordinary = TRUE; /* vision routine helper */
&nbsp;    if ((rtype != OROOM) || do_ordinary)
&nbsp;#endif
&nbsp;        {
&nbsp;        /* do innards first */
&nbsp;        for (x = lowx; x &lt;= hix; x++)
&nbsp;            for (y = lowy; y &lt;= hiy; y++)
&nbsp;#ifdef SPECIALIZATION
&nbsp;                if (rtype == OROOM)
&nbsp;                    levl[x][y].roomno = NO_ROOM;
&nbsp;                else
&nbsp;#endif
&nbsp;                    levl[x][y].roomno = roomno;
&nbsp;        /* top and bottom edges */
&nbsp;        for (x = lowx - 1; x &lt;= hix + 1; x++)
&nbsp;            for (y = lowy - 1; y &lt;= hiy + 1; y += (hiy - lowy + 2)) {
&nbsp;                levl[x][y].edge = 1;
&nbsp;                if (levl[x][y].roomno)
&nbsp;                    levl[x][y].roomno = SHARED;
&nbsp;                else
&nbsp;                    levl[x][y].roomno = roomno;
&nbsp;            }
&nbsp;        /* sides */
&nbsp;        for (x = lowx - 1; x &lt;= hix + 1; x += (hix - lowx + 2))
&nbsp;            for (y = lowy; y &lt;= hiy; y++) {
&nbsp;                levl[x][y].edge = 1;
&nbsp;                if (levl[x][y].roomno)
&nbsp;                    levl[x][y].roomno = SHARED;
&nbsp;                else
&nbsp;                    levl[x][y].roomno = roomno;
&nbsp;            }
&nbsp;    }
&nbsp;    /* subrooms */
&nbsp;    for (subindex = 0; subindex &lt; nsubrooms; subindex++)
&nbsp;#ifdef SPECIALIZATION
&nbsp;        topologize(croom-&gt;sbrooms[subindex], (boolean) (rtype != OROOM));
&nbsp;#else
&nbsp;        topologize(croom-&gt;sbrooms[subindex]);
&nbsp;#endif
&nbsp;}
&nbsp;
find_branch_room
&nbsp;/* Find an unused room for a branch location. */
&nbsp;STATIC_OVL struct mkroom *
&nbsp;find_branch_room(mp)
&nbsp;coord *mp;
&nbsp;{
&nbsp;    struct mkroom *croom = 0;
&nbsp;
&nbsp;    if (nroom == 0) {
&nbsp;        mazexy(mp); /* already verifies location */
&nbsp;    } else {
&nbsp;        /* not perfect - there may be only one stairway */
&nbsp;        if (nroom &gt; 2) {
&nbsp;            int tryct = 0;
&nbsp;
&nbsp;            do
&nbsp;                croom = &amp;rooms[rn2(nroom)];
&nbsp;            while ((croom == dnstairs_room || croom == upstairs_room
&nbsp;                    || croom-&gt;rtype != OROOM) &amp;&amp; (++tryct &lt; 100));
&nbsp;        } else
&nbsp;            croom = &amp;rooms[rn2(nroom)];
&nbsp;
&nbsp;        do {
&nbsp;            if (!somexy(croom, mp))
&nbsp;                impossible("Can't place branch!");
&nbsp;        } while (occupied(mp-&gt;x, mp-&gt;y)
&nbsp;                 || (levl[mp-&gt;x][mp-&gt;y].typ != CORR
&nbsp;                     &amp;&amp; levl[mp-&gt;x][mp-&gt;y].typ != ROOM));
&nbsp;    }
&nbsp;    return croom;
&nbsp;}
&nbsp;
pos_to_room
&nbsp;/* Find the room for (x,y).  Return null if not in a room. */
&nbsp;STATIC_OVL struct mkroom *
&nbsp;pos_to_room(x, y)
&nbsp;xchar x, y;
&nbsp;{
&nbsp;    int i;
&nbsp;    struct mkroom *curr;
&nbsp;
&nbsp;    for (curr = rooms, i = 0; i &lt; nroom; curr++, i++)
&nbsp;        if (inside_room(curr, x, y))
&nbsp;            return curr;
&nbsp;    ;
&nbsp;    return (struct mkroom *) 0;
&nbsp;}
&nbsp;
place_branch
&nbsp;/* If given a branch, randomly place a special stair or portal. */
&nbsp;void
&nbsp;place_branch(br, x, y)
&nbsp;branch *br; /* branch to place */
&nbsp;xchar x, y; /* location */
&nbsp;{
&nbsp;    coord m;
&nbsp;    d_level *dest;
&nbsp;    boolean make_stairs;
&nbsp;    struct mkroom *br_room;
&nbsp;
&nbsp;    /*
&nbsp;     * Return immediately if there is no branch to make or we have
&nbsp;     * already made one.  This routine can be called twice when
&nbsp;     * a special level is loaded that specifies an SSTAIR location
&nbsp;     * as a favored spot for a branch.
&nbsp;     */
&nbsp;    if (!br || made_branch)
&nbsp;        return;
&nbsp;
&nbsp;    if (!x) { /* find random coordinates for branch */
&nbsp;        br_room = find_branch_room(&amp;m);
&nbsp;        x = m.x;
&nbsp;        y = m.y;
&nbsp;    } else {
&nbsp;        br_room = pos_to_room(x, y);
&nbsp;    }
&nbsp;
&nbsp;    if (on_level(&amp;br-&gt;end1, &amp;u.uz)) {
&nbsp;        /* we're on end1 */
&nbsp;        make_stairs = br-&gt;type != BR_NO_END1;
&nbsp;        dest = &amp;br-&gt;end2;
&nbsp;    } else {
&nbsp;        /* we're on end2 */
&nbsp;        make_stairs = br-&gt;type != BR_NO_END2;
&nbsp;        dest = &amp;br-&gt;end1;
&nbsp;    }
&nbsp;
&nbsp;    if (br-&gt;type == BR_PORTAL) {
&nbsp;        mkportal(x, y, dest-&gt;dnum, dest-&gt;dlevel);
&nbsp;    } else if (make_stairs) {
&nbsp;        sstairs.sx = x;
&nbsp;        sstairs.sy = y;
&nbsp;        sstairs.up =
&nbsp;            (char) on_level(&amp;br-&gt;end1, &amp;u.uz) ? br-&gt;end1_up : !br-&gt;end1_up;
&nbsp;        assign_level(&amp;sstairs.tolev, dest);
&nbsp;        sstairs_room = br_room;
&nbsp;
&nbsp;        levl[x][y].ladder = sstairs.up ? LA_UP : LA_DOWN;
&nbsp;        levl[x][y].typ = STAIRS;
&nbsp;    }
&nbsp;    /*
&nbsp;     * Set made_branch to TRUE even if we didn't make a stairwell (i.e.
&nbsp;     * make_stairs is false) since there is currently only one branch
&nbsp;     * per level, if we failed once, we're going to fail again on the
&nbsp;     * next call.
&nbsp;     */
&nbsp;    made_branch = TRUE;
&nbsp;}
&nbsp;
bydoor
&nbsp;STATIC_OVL boolean
&nbsp;bydoor(x, y)
&nbsp;register xchar x, y;
&nbsp;{
&nbsp;    register int typ;
&nbsp;
&nbsp;    if (isok(x + 1, y)) {
&nbsp;        typ = levl[x + 1][y].typ;
&nbsp;        if (IS_DOOR(typ) || typ == SDOOR)
&nbsp;            return TRUE;
&nbsp;    }
&nbsp;    if (isok(x - 1, y)) {
&nbsp;        typ = levl[x - 1][y].typ;
&nbsp;        if (IS_DOOR(typ) || typ == SDOOR)
&nbsp;            return TRUE;
&nbsp;    }
&nbsp;    if (isok(x, y + 1)) {
&nbsp;        typ = levl[x][y + 1].typ;
&nbsp;        if (IS_DOOR(typ) || typ == SDOOR)
&nbsp;            return TRUE;
&nbsp;    }
&nbsp;    if (isok(x, y - 1)) {
&nbsp;        typ = levl[x][y - 1].typ;
&nbsp;        if (IS_DOOR(typ) || typ == SDOOR)
&nbsp;            return TRUE;
&nbsp;    }
&nbsp;    return FALSE;
&nbsp;}
&nbsp;
okdoor
&nbsp;/* see whether it is allowable to create a door at [x,y] */
&nbsp;int
&nbsp;okdoor(x, y)
&nbsp;xchar x, y;
&nbsp;{
&nbsp;    boolean near_door = bydoor(x, y);
&nbsp;
&nbsp;    return ((levl[x][y].typ == HWALL || levl[x][y].typ == VWALL)
&nbsp;            &amp;&amp; doorindex &lt; DOORMAX &amp;&amp; !near_door);
&nbsp;}
&nbsp;
dodoor
&nbsp;void
&nbsp;dodoor(x, y, aroom)
&nbsp;int x, y;
&nbsp;struct mkroom *aroom;
&nbsp;{
&nbsp;    if (doorindex &gt;= DOORMAX) {
&nbsp;        impossible("DOORMAX exceeded?");
&nbsp;        return;
&nbsp;    }
&nbsp;
&nbsp;    dosdoor(x, y, aroom, rn2(8) ? DOOR : SDOOR);
&nbsp;}
&nbsp;
occupied
&nbsp;boolean
&nbsp;occupied(x, y)
&nbsp;register xchar x, y;
&nbsp;{
&nbsp;    return (boolean) (t_at(x, y) || IS_FURNITURE(levl[x][y].typ)
&nbsp;                      || is_lava(x, y) || is_pool(x, y)
&nbsp;                      || invocation_pos(x, y));
&nbsp;}
&nbsp;
mktrap
&nbsp;/* make a trap somewhere (in croom if mazeflag = 0 &amp;&amp; !tm) */
&nbsp;/* if tm != null, make trap at that location */
&nbsp;void
&nbsp;mktrap(num, mazeflag, croom, tm)
&nbsp;int num, mazeflag;
&nbsp;struct mkroom *croom;
&nbsp;coord *tm;
&nbsp;{
&nbsp;    register int kind;
&nbsp;    unsigned lvl = level_difficulty();
&nbsp;    coord m;
&nbsp;
&nbsp;    /* no traps in pools */
&nbsp;    if (tm &amp;&amp; is_pool(tm-&gt;x, tm-&gt;y))
&nbsp;        return;
&nbsp;
&nbsp;    if (num &gt; 0 &amp;&amp; num &lt; TRAPNUM) {
&nbsp;        kind = num;
&nbsp;    } else if (Is_rogue_level(&amp;u.uz)) {
&nbsp;        switch (rn2(7)) {
&nbsp;        default:
&nbsp;            kind = BEAR_TRAP;
&nbsp;            break; /* 0 */
&nbsp;        case 1:
&nbsp;            kind = ARROW_TRAP;
&nbsp;            break;
&nbsp;        case 2:
&nbsp;            kind = DART_TRAP;
&nbsp;            break;
&nbsp;        case 3:
&nbsp;            kind = TRAPDOOR;
&nbsp;            break;
&nbsp;        case 4:
&nbsp;            kind = PIT;
&nbsp;            break;
&nbsp;        case 5:
&nbsp;            kind = SLP_GAS_TRAP;
&nbsp;            break;
&nbsp;        case 6:
&nbsp;            kind = RUST_TRAP;
&nbsp;            break;
&nbsp;        }
&nbsp;    } else if (Inhell &amp;&amp; !rn2(5)) {
&nbsp;        /* bias the frequency of fire traps in Gehennom */
&nbsp;        kind = FIRE_TRAP;
&nbsp;    } else {
&nbsp;        do {
&nbsp;            kind = rnd(TRAPNUM - 1);
&nbsp;            /* reject "too hard" traps */
&nbsp;            switch (kind) {
&nbsp;            case MAGIC_PORTAL:
&nbsp;            case VIBRATING_SQUARE:
&nbsp;                kind = NO_TRAP;
&nbsp;                break;
&nbsp;            case ROLLING_BOULDER_TRAP:
&nbsp;            case SLP_GAS_TRAP:
&nbsp;                if (lvl &lt; 2)
&nbsp;                    kind = NO_TRAP;
&nbsp;                break;
&nbsp;            case LEVEL_TELEP:
&nbsp;                if (lvl &lt; 5 || level.flags.noteleport)
&nbsp;                    kind = NO_TRAP;
&nbsp;                break;
&nbsp;            case SPIKED_PIT:
&nbsp;                if (lvl &lt; 5)
&nbsp;                    kind = NO_TRAP;
&nbsp;                break;
&nbsp;            case LANDMINE:
&nbsp;                if (lvl &lt; 6)
&nbsp;                    kind = NO_TRAP;
&nbsp;                break;
&nbsp;            case WEB:
&nbsp;                if (lvl &lt; 7)
&nbsp;                    kind = NO_TRAP;
&nbsp;                break;
&nbsp;            case STATUE_TRAP:
&nbsp;            case POLY_TRAP:
&nbsp;                if (lvl &lt; 8)
&nbsp;                    kind = NO_TRAP;
&nbsp;                break;
&nbsp;            case FIRE_TRAP:
&nbsp;                if (!Inhell)
&nbsp;                    kind = NO_TRAP;
&nbsp;                break;
&nbsp;            case TELEP_TRAP:
&nbsp;                if (level.flags.noteleport)
&nbsp;                    kind = NO_TRAP;
&nbsp;                break;
&nbsp;            case HOLE:
&nbsp;                /* make these much less often than other traps */
&nbsp;                if (rn2(7))
&nbsp;                    kind = NO_TRAP;
&nbsp;                break;
&nbsp;            }
&nbsp;        } while (kind == NO_TRAP);
&nbsp;    }
&nbsp;
&nbsp;    if ((kind == TRAPDOOR || kind == HOLE) &amp;&amp; !Can_fall_thru(&amp;u.uz))
&nbsp;        kind = ROCKTRAP;
&nbsp;
&nbsp;    if (tm) {
&nbsp;        m = *tm;
&nbsp;    } else {
&nbsp;        register int tryct = 0;
&nbsp;        boolean avoid_boulder = (kind == PIT || kind == SPIKED_PIT
&nbsp;                                 || kind == TRAPDOOR || kind == HOLE);
&nbsp;
&nbsp;        do {
&nbsp;            if (++tryct &gt; 200)
&nbsp;                return;
&nbsp;            if (mazeflag)
&nbsp;                mazexy(&amp;m);
&nbsp;            else if (!somexy(croom, &amp;m))
&nbsp;                return;
&nbsp;        } while (occupied(m.x, m.y)
&nbsp;                 || (avoid_boulder &amp;&amp; sobj_at(BOULDER, m.x, m.y)));
&nbsp;    }
&nbsp;
&nbsp;    (void) maketrap(m.x, m.y, kind);
&nbsp;    if (kind == WEB)
&nbsp;        (void) makemon(&amp;mons[PM_GIANT_SPIDER], m.x, m.y, NO_MM_FLAGS);
&nbsp;
&nbsp;    /* The hero isn't the only person who's entered the dungeon in
&nbsp;       search of treasure. On the very shallowest levels, there's a
&nbsp;       chance that a created trap will have killed something already
&nbsp;       (and this is guaranteed on the first level).
&nbsp;
&nbsp;       This isn't meant to give any meaningful treasure (in fact, any
&nbsp;       items we drop here are typically cursed, other than ammo fired
&nbsp;       by the trap). Rather, it's mostly just for flavour and to give
&nbsp;       players on very early levels a sufficient chance to avoid traps
&nbsp;       that may end up killing them before they have a fair chance to
&nbsp;       build max HP. Including cursed items gives the same fair chance
&nbsp;       to the starting pet, and fits the rule that possessions of the
&nbsp;       dead are normally cursed.
&nbsp;
&nbsp;       Some types of traps are excluded because they're entirely
&nbsp;       nonlethal, even indirectly. We also exclude all of the
&nbsp;       later/fancier traps because they tend to have special
&nbsp;       considerations (e.g. webs, portals), often are indirectly
&nbsp;       lethal, and tend not to generate on shallower levels anyway.
&nbsp;       Finally, pits are excluded because it's weird to see an item
&nbsp;       in a pit and yet not be able to identify that the pit is there. */
&nbsp;    if (lvl &lt;= (unsigned) rnd(4)
&nbsp;        &amp;&amp; kind != SQKY_BOARD &amp;&amp; kind != RUST_TRAP
&nbsp;        &amp;&amp; kind != PIT &amp;&amp; kind != SPIKED_PIT &amp;&amp; kind &lt; HOLE) {
&nbsp;        /* Object generated by the trap; initially NULL, stays NULL if
&nbsp;           we fail to generate an object or if the trap doesn't
&nbsp;           generate objects. */
&nbsp;        struct obj *otmp = NULL;
&nbsp;        int victim_mnum; /* race of the victim */
&nbsp;
&nbsp;        /* Not all trap types have special handling here; only the ones
&nbsp;           that kill in a specific way that's obvious after the fact. */
&nbsp;        switch (kind) {
&nbsp;        case ARROW_TRAP:
&nbsp;            otmp = mksobj(ARROW, TRUE, FALSE);
&nbsp;            otmp-&gt;opoisoned = 0;
&nbsp;            /* don't adjust the quantity; maybe the trap shot multiple
&nbsp;               times, there was an untrapping attempt, etc... */
&nbsp;            break;
&nbsp;        case DART_TRAP:
&nbsp;            otmp = mksobj(DART, TRUE, FALSE);
&nbsp;            break;
&nbsp;        case ROCKTRAP:
&nbsp;            otmp = mksobj(ROCK, TRUE, FALSE);
&nbsp;            break;
&nbsp;        default:
&nbsp;            /* no item dropped by the trap */
&nbsp;            break;
&nbsp;        }
&nbsp;        if (otmp) {
&nbsp;            place_object(otmp, m.x, m.y);
&nbsp;        }
&nbsp;
&nbsp;        /* now otmp is reused for other items we're placing */
&nbsp;
&nbsp;        /* Place a random possession. This could be a weapon, tool,
&nbsp;           food, or gem, i.e. the item classes that are typically
&nbsp;           nonmagical and not worthless. */
&nbsp;        do {
&nbsp;            int poss_class = RANDOM_CLASS; /* init =&gt; lint suppression */
&nbsp;
&nbsp;            switch (rn2(4)) {
&nbsp;            case 0:
&nbsp;                poss_class = WEAPON_CLASS;
&nbsp;                break;
&nbsp;            case 1:
&nbsp;                poss_class = TOOL_CLASS;
&nbsp;                break;
&nbsp;            case 2:
&nbsp;                poss_class = FOOD_CLASS;
&nbsp;                break;
&nbsp;            case 3:
&nbsp;                poss_class = GEM_CLASS;
&nbsp;                break;
&nbsp;            }
&nbsp;
&nbsp;            otmp = mkobj(poss_class, FALSE);
&nbsp;            /* these items are always cursed, both for flavour (owned
&nbsp;               by a dead adventurer, bones-pile-style) and for balance
&nbsp;               (less useful to use, and encourage pets to avoid the trap) */
&nbsp;            if (otmp) {
&nbsp;                otmp-&gt;blessed = 0;
&nbsp;                otmp-&gt;cursed = 1;
&nbsp;                otmp-&gt;owt = weight(otmp);
&nbsp;                place_object(otmp, m.x, m.y);
&nbsp;            }
&nbsp;
&nbsp;            /* 20% chance of placing an additional item, recursively */
&nbsp;        } while (!rn2(5));
&nbsp;
&nbsp;        /* Place a corpse. */
&nbsp;        switch (rn2(15)) {
&nbsp;        case 0:
&nbsp;            /* elf corpses are the rarest as they're the most useful */
&nbsp;            victim_mnum = PM_ELF;
&nbsp;            /* elven adventurers get sleep resistance early; so don't
&nbsp;               generate elf corpses on sleeping gas traps unless a)
&nbsp;               we're on dlvl 2 (1 is impossible) and b) we pass a coin
&nbsp;               flip */
&nbsp;            if (kind == SLP_GAS_TRAP &amp;&amp; !(lvl &lt;= 2 &amp;&amp; rn2(2)))
&nbsp;                victim_mnum = PM_HUMAN;
&nbsp;            break;
&nbsp;        case 1: case 2:
&nbsp;            victim_mnum = PM_DWARF;
&nbsp;            break;
&nbsp;        case 3: case 4: case 5:
&nbsp;            victim_mnum = PM_ORC;
&nbsp;            break;
&nbsp;        case 6: case 7: case 8: case 9:
&nbsp;            /* more common as they could have come from the Mines */
&nbsp;            victim_mnum = PM_GNOME;
&nbsp;            /* 10% chance of a candle too */
&nbsp;            if (!rn2(10)) {
&nbsp;                otmp = mksobj(rn2(4) ? TALLOW_CANDLE : WAX_CANDLE,
&nbsp;                              TRUE, FALSE);
&nbsp;                otmp-&gt;quan = 1;
&nbsp;                otmp-&gt;blessed = 0;
&nbsp;                otmp-&gt;cursed = 1;
&nbsp;                otmp-&gt;owt = weight(otmp);
&nbsp;                place_object(otmp, m.x, m.y);
&nbsp;            }
&nbsp;            break;
&nbsp;        default:
&nbsp;            /* the most common race */
&nbsp;            victim_mnum = PM_HUMAN;
&nbsp;            break;
&nbsp;        }
&nbsp;        otmp = mkcorpstat(CORPSE, NULL, &amp;mons[victim_mnum], m.x, m.y,
&nbsp;                          CORPSTAT_INIT);
&nbsp;        if (otmp)
&nbsp;            otmp-&gt;age -= 51; /* died too long ago to eat */
&nbsp;    }
&nbsp;}
&nbsp;
mkstairs
&nbsp;void
&nbsp;mkstairs(x, y, up, croom)
&nbsp;xchar x, y;
&nbsp;char up;
&nbsp;struct mkroom *croom;
&nbsp;{
&nbsp;    if (!x) {
&nbsp;        impossible("mkstairs:  bogus stair attempt at &lt;%d,%d&gt;", x, y);
&nbsp;        return;
&nbsp;    }
&nbsp;
&nbsp;    /*
&nbsp;     * We can't make a regular stair off an end of the dungeon.  This
&nbsp;     * attempt can happen when a special level is placed at an end and
&nbsp;     * has an up or down stair specified in its description file.
&nbsp;     */
&nbsp;    if ((dunlev(&amp;u.uz) == 1 &amp;&amp; up)
&nbsp;        || (dunlev(&amp;u.uz) == dunlevs_in_dungeon(&amp;u.uz) &amp;&amp; !up))
&nbsp;        return;
&nbsp;
&nbsp;    if (up) {
&nbsp;        xupstair = x;
&nbsp;        yupstair = y;
&nbsp;        upstairs_room = croom;
&nbsp;    } else {
&nbsp;        xdnstair = x;
&nbsp;        ydnstair = y;
&nbsp;        dnstairs_room = croom;
&nbsp;    }
&nbsp;
&nbsp;    levl[x][y].typ = STAIRS;
&nbsp;    levl[x][y].ladder = up ? LA_UP : LA_DOWN;
&nbsp;}
&nbsp;
mkfount
&nbsp;STATIC_OVL void
&nbsp;mkfount(mazeflag, croom)
&nbsp;int mazeflag;
&nbsp;struct mkroom *croom;
&nbsp;{
&nbsp;    coord m;
&nbsp;    register int tryct = 0;
&nbsp;
&nbsp;    do {
&nbsp;        if (++tryct &gt; 200)
&nbsp;            return;
&nbsp;        if (mazeflag)
&nbsp;            mazexy(&amp;m);
&nbsp;        else if (!somexy(croom, &amp;m))
&nbsp;            return;
&nbsp;    } while (occupied(m.x, m.y) || bydoor(m.x, m.y));
&nbsp;
&nbsp;    /* Put a fountain at m.x, m.y */
&nbsp;    levl[m.x][m.y].typ = FOUNTAIN;
&nbsp;    /* Is it a "blessed" fountain? (affects drinking from fountain) */
&nbsp;    if (!rn2(7))
&nbsp;        levl[m.x][m.y].blessedftn = 1;
&nbsp;
&nbsp;    level.flags.nfountains++;
&nbsp;}
&nbsp;
mksink
&nbsp;STATIC_OVL void
&nbsp;mksink(croom)
&nbsp;struct mkroom *croom;
&nbsp;{
&nbsp;    coord m;
&nbsp;    register int tryct = 0;
&nbsp;
&nbsp;    do {
&nbsp;        if (++tryct &gt; 200)
&nbsp;            return;
&nbsp;        if (!somexy(croom, &amp;m))
&nbsp;            return;
&nbsp;    } while (occupied(m.x, m.y) || bydoor(m.x, m.y));
&nbsp;
&nbsp;    /* Put a sink at m.x, m.y */
&nbsp;    levl[m.x][m.y].typ = SINK;
&nbsp;
&nbsp;    level.flags.nsinks++;
&nbsp;}
&nbsp;
mkaltar
&nbsp;STATIC_OVL void
&nbsp;mkaltar(croom)
&nbsp;struct mkroom *croom;
&nbsp;{
&nbsp;    coord m;
&nbsp;    register int tryct = 0;
&nbsp;    aligntyp al;
&nbsp;
&nbsp;    if (croom-&gt;rtype != OROOM)
&nbsp;        return;
&nbsp;
&nbsp;    do {
&nbsp;        if (++tryct &gt; 200)
&nbsp;            return;
&nbsp;        if (!somexy(croom, &amp;m))
&nbsp;            return;
&nbsp;    } while (occupied(m.x, m.y) || bydoor(m.x, m.y));
&nbsp;
&nbsp;    /* Put an altar at m.x, m.y */
&nbsp;    levl[m.x][m.y].typ = ALTAR;
&nbsp;
&nbsp;    /* -1 - A_CHAOTIC, 0 - A_NEUTRAL, 1 - A_LAWFUL */
&nbsp;    al = rn2((int) A_LAWFUL + 2) - 1;
&nbsp;    levl[m.x][m.y].altarmask = Align2amask(al);
&nbsp;}
&nbsp;
mkgrave
&nbsp;static void
&nbsp;mkgrave(croom)
&nbsp;struct mkroom *croom;
&nbsp;{
&nbsp;    coord m;
&nbsp;    register int tryct = 0;
&nbsp;    register struct obj *otmp;
&nbsp;    boolean dobell = !rn2(10);
&nbsp;
&nbsp;    if (croom-&gt;rtype != OROOM)
&nbsp;        return;
&nbsp;
&nbsp;    do {
&nbsp;        if (++tryct &gt; 200)
&nbsp;            return;
&nbsp;        if (!somexy(croom, &amp;m))
&nbsp;            return;
&nbsp;    } while (occupied(m.x, m.y) || bydoor(m.x, m.y));
&nbsp;
&nbsp;    /* Put a grave at m.x, m.y */
&nbsp;    make_grave(m.x, m.y, dobell ? "Saved by the bell!" : (char *) 0);
&nbsp;
&nbsp;    /* Possibly fill it with objects */
&nbsp;    if (!rn2(3))
&nbsp;        (void) mkgold(0L, m.x, m.y);
&nbsp;    for (tryct = rn2(5); tryct; tryct--) {
&nbsp;        otmp = mkobj(RANDOM_CLASS, TRUE);
&nbsp;        if (!otmp)
&nbsp;            return;
&nbsp;        curse(otmp);
&nbsp;        otmp-&gt;ox = m.x;
&nbsp;        otmp-&gt;oy = m.y;
&nbsp;        add_to_buried(otmp);
&nbsp;    }
&nbsp;
&nbsp;    /* Leave a bell, in case we accidentally buried someone alive */
&nbsp;    if (dobell)
&nbsp;        (void) mksobj_at(BELL, m.x, m.y, TRUE, FALSE);
&nbsp;    return;
&nbsp;}
&nbsp;
&nbsp;/* maze levels have slightly different constraints from normal levels */
&nbsp;#define x_maze_min 2
&nbsp;#define y_maze_min 2
&nbsp;
mkinvokearea
&nbsp;/*
&nbsp; * Major level transmutation: add a set of stairs (to the Sanctum) after
&nbsp; * an earthquake that leaves behind a a new topology, centered at inv_pos.
&nbsp; * Assumes there are no rooms within the invocation area and that inv_pos
&nbsp; * is not too close to the edge of the map.  Also assume the hero can see,
&nbsp; * which is guaranteed for normal play due to the fact that sight is needed
&nbsp; * to read the Book of the Dead.
&nbsp; */
&nbsp;void
&nbsp;mkinvokearea()
&nbsp;{
&nbsp;    int dist;
&nbsp;    xchar xmin = inv_pos.x, xmax = inv_pos.x;
&nbsp;    xchar ymin = inv_pos.y, ymax = inv_pos.y;
&nbsp;    register xchar i;
&nbsp;
&nbsp;    pline_The("floor shakes violently under you!");
&nbsp;    pline_The("walls around you begin to bend and crumble!");
&nbsp;    display_nhwindow(WIN_MESSAGE, TRUE);
&nbsp;
&nbsp;    /* any trap hero is stuck in will be going away now */
&nbsp;    if (u.utrap) {
&nbsp;        u.utrap = 0;
&nbsp;        if (u.utraptype == TT_BURIEDBALL)
&nbsp;            buried_ball_to_punishment();
&nbsp;    }
&nbsp;    mkinvpos(xmin, ymin, 0); /* middle, before placing stairs */
&nbsp;
&nbsp;    for (dist = 1; dist &lt; 7; dist++) {
&nbsp;        xmin--;
&nbsp;        xmax++;
&nbsp;
&nbsp;        /* top and bottom */
&nbsp;        if (dist != 3) { /* the area is wider that it is high */
&nbsp;            ymin--;
&nbsp;            ymax++;
&nbsp;            for (i = xmin + 1; i &lt; xmax; i++) {
&nbsp;                mkinvpos(i, ymin, dist);
&nbsp;                mkinvpos(i, ymax, dist);
&nbsp;            }
&nbsp;        }
&nbsp;
&nbsp;        /* left and right */
&nbsp;        for (i = ymin; i &lt;= ymax; i++) {
&nbsp;            mkinvpos(xmin, i, dist);
&nbsp;            mkinvpos(xmax, i, dist);
&nbsp;        }
&nbsp;
&nbsp;        flush_screen(1); /* make sure the new glyphs shows up */
&nbsp;        delay_output();
&nbsp;    }
&nbsp;
&nbsp;    You("are standing at the top of a stairwell leading down!");
&nbsp;    mkstairs(u.ux, u.uy, 0, (struct mkroom *) 0); /* down */
&nbsp;    newsym(u.ux, u.uy);
&nbsp;    vision_full_recalc = 1; /* everything changed */
&nbsp;}
&nbsp;
mkinvpos
&nbsp;/* Change level topology.  Boulders in the vicinity are eliminated.
&nbsp; * Temporarily overrides vision in the name of a nice effect.
&nbsp; */
&nbsp;STATIC_OVL void
&nbsp;mkinvpos(x, y, dist)
&nbsp;xchar x, y;
&nbsp;int dist;
&nbsp;{
&nbsp;    struct trap *ttmp;
&nbsp;    struct obj *otmp;
&nbsp;    boolean make_rocks;
&nbsp;    register struct rm *lev = &amp;levl[x][y];
&nbsp;
&nbsp;    /* clip at existing map borders if necessary */
&nbsp;    if (!within_bounded_area(x, y, x_maze_min + 1, y_maze_min + 1,
&nbsp;                             x_maze_max - 1, y_maze_max - 1)) {
&nbsp;        /* only outermost 2 columns and/or rows may be truncated due to edge
&nbsp;         */
&nbsp;        if (dist &lt; (7 - 2))
&nbsp;            panic("mkinvpos: &lt;%d,%d&gt; (%d) off map edge!", x, y, dist);
&nbsp;        return;
&nbsp;    }
&nbsp;
&nbsp;    /* clear traps */
&nbsp;    if ((ttmp = t_at(x, y)) != 0)
&nbsp;        deltrap(ttmp);
&nbsp;
&nbsp;    /* clear boulders; leave some rocks for non-{moat|trap} locations */
&nbsp;    make_rocks = (dist != 1 &amp;&amp; dist != 4 &amp;&amp; dist != 5) ? TRUE : FALSE;
&nbsp;    while ((otmp = sobj_at(BOULDER, x, y)) != 0) {
&nbsp;        if (make_rocks) {
&nbsp;            fracture_rock(otmp);
&nbsp;            make_rocks = FALSE; /* don't bother with more rocks */
&nbsp;        } else {
&nbsp;            obj_extract_self(otmp);
&nbsp;            obfree(otmp, (struct obj *) 0);
&nbsp;        }
&nbsp;    }
&nbsp;    unblock_point(x, y); /* make sure vision knows this location is open */
&nbsp;
&nbsp;    /* fake out saved state */
&nbsp;    lev-&gt;seenv = 0;
&nbsp;    lev-&gt;doormask = 0;
&nbsp;    if (dist &lt; 6)
&nbsp;        lev-&gt;lit = TRUE;
&nbsp;    lev-&gt;waslit = TRUE;
&nbsp;    lev-&gt;horizontal = FALSE;
&nbsp;    /* short-circuit vision recalc */
&nbsp;    viz_array[y][x] = (dist &lt; 6) ? (IN_SIGHT | COULD_SEE) : COULD_SEE;
&nbsp;
&nbsp;    switch (dist) {
&nbsp;    case 1: /* fire traps */
&nbsp;        if (is_pool(x, y))
&nbsp;            break;
&nbsp;        lev-&gt;typ = ROOM;
&nbsp;        ttmp = maketrap(x, y, FIRE_TRAP);
&nbsp;        if (ttmp)
&nbsp;            ttmp-&gt;tseen = TRUE;
&nbsp;        break;
&nbsp;    case 0: /* lit room locations */
&nbsp;    case 2:
&nbsp;    case 3:
&nbsp;    case 6: /* unlit room locations */
&nbsp;        lev-&gt;typ = ROOM;
&nbsp;        break;
&nbsp;    case 4: /* pools (aka a wide moat) */
&nbsp;    case 5:
&nbsp;        lev-&gt;typ = MOAT;
&nbsp;        /* No kelp! */
&nbsp;        break;
&nbsp;    default:
&nbsp;        impossible("mkinvpos called with dist %d", dist);
&nbsp;        break;
&nbsp;    }
&nbsp;
&nbsp;    /* display new value of position; could have a monster/object on it */
&nbsp;    newsym(x, y);
&nbsp;}
&nbsp;
mk_knox_portal
&nbsp;/*
&nbsp; * The portal to Ludios is special.  The entrance can only occur within a
&nbsp; * vault in the main dungeon at a depth greater than 10.  The Ludios branch
&nbsp; * structure reflects this by having a bogus "source" dungeon:  the value
&nbsp; * of n_dgns (thus, Is_branchlev() will never find it).
&nbsp; *
&nbsp; * Ludios will remain isolated until the branch is corrected by this function.
&nbsp; */
&nbsp;STATIC_OVL void
&nbsp;mk_knox_portal(x, y)
&nbsp;xchar x, y;
&nbsp;{
&nbsp;    extern int n_dgns; /* from dungeon.c */
&nbsp;    d_level *source;
&nbsp;    branch *br;
&nbsp;    schar u_depth;
&nbsp;
&nbsp;    br = dungeon_branch("Fort Ludios");
&nbsp;    if (on_level(&amp;knox_level, &amp;br-&gt;end1)) {
&nbsp;        source = &amp;br-&gt;end2;
&nbsp;    } else {
&nbsp;        /* disallow Knox branch on a level with one branch already */
&nbsp;        if (Is_branchlev(&amp;u.uz))
&nbsp;            return;
&nbsp;        source = &amp;br-&gt;end1;
&nbsp;    }
&nbsp;
&nbsp;    /* Already set or 2/3 chance of deferring until a later level. */
&nbsp;    if (source-&gt;dnum &lt; n_dgns || (rn2(3) &amp;&amp; !wizard))
&nbsp;        return;
&nbsp;
&nbsp;    if (!(u.uz.dnum == oracle_level.dnum      /* in main dungeon */
&nbsp;          &amp;&amp; !at_dgn_entrance("The Quest")    /* but not Quest's entry */
&nbsp;          &amp;&amp; (u_depth = depth(&amp;u.uz)) &gt; 10    /* beneath 10 */
&nbsp;          &amp;&amp; u_depth &lt; depth(&amp;medusa_level))) /* and above Medusa */
&nbsp;        return;
&nbsp;
&nbsp;    /* Adjust source to be current level and re-insert branch. */
&nbsp;    *source = u.uz;
&nbsp;    insert_branch(br, TRUE);
&nbsp;
&nbsp;    debugpline0("Made knox portal.");
&nbsp;    place_branch(br, x, y);
&nbsp;}
&nbsp;
&nbsp;/*mklev.c*/

</x,y>