# Source:NetHack 3.4.3/src/sp lev.c
Below is the full text to src/sp_lev.c from NetHack 3.4.3. To link to a particular line, write [[sp_lev.c#line123]], for example.

Contents

1 Top of file
2 set_wall_property
3 rnddoor
4 rndtrap
5 get_location
6 is_ok_location
7 sp_lev_shuffle
8 get_room_loc
9 get_free_room_loc
10 check_room
11 create_room
12 create_subroom
13 create_door
14 create_secret_door
15 create_trap
16 noncoalignment
17 create_monster
18 create_object
19 create_engraving
20 create_stairs
21 create_altar
22 create_gold
23 create_feature
24 search_door
25 dig_corridor
26 fix_stair_rooms
27 create_corridor
28 fill_room
29 free_rooms
30 build_room
31 light_region
32 load_common_data
33 load_one_monster
34 load_one_object
35 load_one_engraving
36 load_rooms
37 maze1xy
38 load_maze
39 load_special

Top of file
/*	SCCS Id: @(#)sp_lev.c	3.4	2001/09/06	*/
/*	Copyright (c) 1989 by Jean-Christophe Collet */
/* 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.

/*
* This file contains the various functions that are related to the special
* levels.
* It contains also the special level loader.
*
*/

#include "hack.h"
#include "dlb.h"
/* #define DEBUG */	/* uncomment to enable code debugging */

#ifdef DEBUG
# ifdef WIZARD
#define debugpline	if (wizard) pline
# else
#define debugpline	pline
# endif
#endif

#include "sp_lev.h"
#include "rect.h"

extern void FDECL(mkmap, (lev_init *));

STATIC_DCL void FDECL(get_room_loc, (schar *, schar *, struct mkroom *));
STATIC_DCL void FDECL(get_free_room_loc, (schar *, schar *, struct mkroom *));
STATIC_DCL void FDECL(create_trap, (trap *, struct mkroom *));
STATIC_DCL int FDECL(noncoalignment, (ALIGNTYP_P));
STATIC_DCL void FDECL(create_monster, (monster *, struct mkroom *));
STATIC_DCL void FDECL(create_object, (object *, struct mkroom *));
STATIC_DCL void FDECL(create_engraving, (engraving *,struct mkroom *));
STATIC_DCL void FDECL(create_stairs, (stair *, struct mkroom *));
STATIC_DCL void FDECL(create_altar, (altar *, struct mkroom *));
STATIC_DCL void FDECL(create_gold, (gold *, struct mkroom *));
STATIC_DCL void FDECL(create_feature, (int,int,struct mkroom *,int));
STATIC_DCL boolean FDECL(search_door, (struct mkroom *, xchar *, xchar *,
					XCHAR_P, int));
STATIC_DCL void NDECL(fix_stair_rooms);
STATIC_DCL void FDECL(create_corridor, (corridor *));

STATIC_DCL boolean FDECL(create_subroom, (struct mkroom *, XCHAR_P, XCHAR_P,
					XCHAR_P, XCHAR_P, XCHAR_P, XCHAR_P));

#define LEFT	1
#define H_LEFT	2
#define CENTER	3
#define H_RIGHT	4
#define RIGHT	5

#define TOP	1
#define BOTTOM	5

#define sq(x) ((x)*(x))

#define XLIM	4
#define YLIM	3

#define Fread	(void)dlb_fread
#define Fgetc	(schar)dlb_fgetc
#define New(type)		(type *) alloc(sizeof(type))
#define NewTab(type, size)	(type **) alloc(sizeof(type *) * (unsigned)size)
#define Free(ptr)		if(ptr) free((genericptr_t) (ptr))

static NEARDATA walk walklist[50];
extern int min_rx, max_rx, min_ry, max_ry; /* from mkmap.c */

static char Map[COLNO][ROWNO];
static char robjects[10], rloc_x[10], rloc_y[10], rmonst[10];
static aligntyp	ralign[3] = { AM_CHAOTIC, AM_NEUTRAL, AM_LAWFUL };
static NEARDATA xchar xstart, ystart;
static NEARDATA char xsize, ysize;

STATIC_DCL void FDECL(set_wall_property, (XCHAR_P,XCHAR_P,XCHAR_P,XCHAR_P,int));
STATIC_DCL int NDECL(rnddoor);
STATIC_DCL int NDECL(rndtrap);
STATIC_DCL void FDECL(get_location, (schar *,schar *,int));
STATIC_DCL void FDECL(sp_lev_shuffle, (char *,char *,int));
STATIC_DCL void FDECL(light_region, (region *));
STATIC_DCL void FDECL(load_common_data, (dlb *,int));
STATIC_DCL void FDECL(load_one_monster, (dlb *,monster *));
STATIC_DCL void FDECL(load_one_object, (dlb *,object *));
STATIC_DCL void FDECL(load_one_engraving, (dlb *,engraving *));
STATIC_DCL boolean FDECL(load_rooms, (dlb *));
STATIC_DCL void FDECL(maze1xy, (coord *,int));
STATIC_DCL boolean FDECL(load_maze, (dlb *));
STATIC_DCL void FDECL(create_door, (room_door *, struct mkroom *));
STATIC_DCL void FDECL(free_rooms,(room **, int));
STATIC_DCL void FDECL(build_room, (room *, room*));

char *lev_message = 0;
lev_region *lregions = 0;
int num_lregions = 0;
lev_init init_lev;
set_wall_property
/*
* Make walls of the area (x1, y1, x2, y2) non diggable/non passwall-able
*/

STATIC_OVL void
set_wall_property(x1,y1,x2,y2, prop)
xchar x1, y1, x2, y2;
int prop;
{
	register xchar x, y;

	for(y = y1; y &lt;= y2; y++)
	    for(x = x1; x &lt;= x2; x++)
		if(IS_STWALL(levl[x][y].typ))
		    levl[x][y].wall_info |= prop;
}
rnddoor
/*
* Choose randomly the state (nodoor, open, closed or locked) for a door
*/
STATIC_OVL int
rnddoor()
{
	int i = 1 &lt;&lt; rn2(5);
	i &gt;&gt;= 1;
	return i;
}
rndtrap
/*
* Select a random trap
*/
STATIC_OVL int
rndtrap()
{
	int rtrap;

	do {
	    rtrap = rnd(TRAPNUM-1);
	    switch (rtrap) {
	     case HOLE:		/* no random holes on special levels */
	     case MAGIC_PORTAL:	rtrap = NO_TRAP;
				break;
	     case TRAPDOOR:	if (!Can_dig_down(&amp;u.uz)) rtrap = NO_TRAP;
				break;
	     case LEVEL_TELEP:
	     case TELEP_TRAP:	if (level.flags.noteleport) rtrap = NO_TRAP;
				break;
	     case ROLLING_BOULDER_TRAP:
	     case ROCKTRAP:	if (In_endgame(&amp;u.uz)) rtrap = NO_TRAP;
				break;
	    }
	} while (rtrap == NO_TRAP);
	return rtrap;
}
get_location
/*
* Coordinates in special level files are handled specially:
*
*	if x or y is -11, we generate a random coordinate.
*	if x or y is between -1 and -10, we read one from the corresponding
*	register (x0, x1, ... x9).
*	if x or y is nonnegative, we convert it from relative to the local map
*	to global coordinates.
*	The "humidity" flag is used to insure that engravings aren't
*	created underwater, or eels on dry land.
*/
#define DRY	0x1
#define WET	0x2

STATIC_DCL boolean FDECL(is_ok_location, (SCHAR_P, SCHAR_P, int));

STATIC_OVL void
get_location(x, y, humidity)
schar *x, *y;
int humidity;
{
	int cpt = 0;

	if (*x &gt;= 0) {			/* normal locations */
		*x += xstart;
		*y += ystart;
	} else if (*x &gt; -11) {		/* special locations */
		*y = ystart + rloc_y[ - *y - 1];
		*x = xstart + rloc_x[ - *x - 1];
	} else {			/* random location */
	    do {
		*x = xstart + rn2((int)xsize);
		*y = ystart + rn2((int)ysize);
		if (is_ok_location(*x,*y,humidity)) break;
	    } while (++cpt &lt; 100);
	    if (cpt &gt;= 100) {
		register int xx, yy;
		/* last try */
		for (xx = 0; xx &lt; xsize; xx++)
		    for (yy = 0; yy &lt; ysize; yy++) {
			*x = xstart + xx;
			*y = ystart + yy;
			if (is_ok_location(*x,*y,humidity)) goto found_it;
		    }
		panic("get_location:  can't find a place!");
	    }
	}
found_it:;

	if (!isok(*x,*y)) {
	    impossible("get_location:  (%d,%d) out of bounds", *x, *y);
	    *x = x_maze_max; *y = y_maze_max;
	}
}
is_ok_location
STATIC_OVL boolean
is_ok_location(x, y, humidity)
register schar x, y;
register int humidity;
{
	register int typ;

	if (Is_waterlevel(&amp;u.uz)) return TRUE;	/* accept any spot */

	if (humidity &amp; DRY) {
	    typ = levl[x][y].typ;
	    if (typ == ROOM || typ == AIR ||
		    typ == CLOUD || typ == ICE || typ == CORR)
		return TRUE;
	}
	if (humidity &amp; WET) {
	    if (is_pool(x,y) || is_lava(x,y))
		return TRUE;
	}
	return FALSE;
}
sp_lev_shuffle
/*
* Shuffle the registers for locations, objects or monsters
*/

STATIC_OVL void
sp_lev_shuffle(list1, list2, n)
char list1[], list2[];
int n;
{
	register int i, j;
	register char k;

	for (i = n - 1; i &gt; 0; i--) {
		if ((j = rn2(i + 1)) == i) continue;
		k = list1[j];
		list1[j] = list1[i];
		list1[i] = k;
		if (list2) {
			k = list2[j];
			list2[j] = list2[i];
			list2[i] = k;
		}
	}
}
get_room_loc
/*
* Get a relative position inside a room.
* negative values for x or y means RANDOM!
*/

STATIC_OVL void
get_room_loc(x,y, croom)
schar		*x, *y;
struct mkroom	*croom;
{
	coord c;

	if (*x &lt;0 &amp;&amp; *y &lt;0) {
		if (somexy(croom, &amp;c)) {
			*x = c.x;
			*y = c.y;
		} else
		    panic("get_room_loc : can't find a place!");
	} else {
		if (*x &lt; 0)
		    *x = rn2(croom-&gt;hx - croom-&gt;lx + 1);
		if (*y &lt; 0)
		    *y = rn2(croom-&gt;hy - croom-&gt;ly + 1);
		*x += croom-&gt;lx;
		*y += croom-&gt;ly;
	}
}
get_free_room_loc
/*
* Get a relative position inside a room.
* negative values for x or y means RANDOM!
*/

STATIC_OVL void
get_free_room_loc(x,y, croom)
schar		*x, *y;
struct mkroom	*croom;
{
	schar try_x, try_y;
	register int trycnt = 0;

	do {
	    try_x = *x,  try_y = *y;
	    get_room_loc(&amp;try_x, &amp;try_y, croom);
	} while (levl[try_x][try_y].typ != ROOM &amp;&amp; ++trycnt &lt;= 100);

	if (trycnt &gt; 100)
	    panic("get_free_room_loc:  can't find a place!");
	*x = try_x,  *y = try_y;
}
check_room
boolean
check_room(lowx, ddx, lowy, ddy, vault)
xchar *lowx, *ddx, *lowy, *ddy;
boolean vault;
{
	register int x,y,hix = *lowx + *ddx, hiy = *lowy + *ddy;
	register struct rm *lev;
	int xlim, ylim, ymax;

	xlim = XLIM + (vault ? 1 : 0);
	ylim = YLIM + (vault ? 1 : 0);

	if (*lowx &lt; 3)		*lowx = 3;
	if (*lowy &lt; 2)		*lowy = 2;
	if (hix &gt; COLNO-3)	hix = COLNO-3;
	if (hiy &gt; ROWNO-3)	hiy = ROWNO-3;
chk:
	if (hix &lt;= *lowx || hiy &lt;= *lowy)	return FALSE;

	/* check area around room (and make room smaller if necessary) */
	for (x = *lowx - xlim; x&lt;= hix + xlim; x++) {
		if(x &lt;= 0 || x &gt;= COLNO) continue;
		y = *lowy - ylim;	ymax = hiy + ylim;
		if(y &lt; 0) y = 0;
		if(ymax &gt;= ROWNO) ymax = (ROWNO-1);
		lev = &amp;levl[x][y];
		for (; y &lt;= ymax; y++) {
			if (lev++-&gt;typ) {
#ifdef DEBUG
				if(!vault)
				    debugpline("strange area [%d,%d] in check_room.",x,y);
#endif
				if (!rn2(3))	return FALSE;
				if (x &lt; *lowx)
				    *lowx = x + xlim + 1;
				else
				    hix = x - xlim - 1;
				if (y &lt; *lowy)
				    *lowy = y + ylim + 1;
				else
				    hiy = y - ylim - 1;
				goto chk;
			}
		}
	}
	*ddx = hix - *lowx;
	*ddy = hiy - *lowy;
	return TRUE;
}
create_room
/*
* Create a new room.
* This is still very incomplete...
*/

boolean
create_room(x,y,w,h,xal,yal,rtype,rlit)
xchar	x,y;
xchar	w,h;
xchar	xal,yal;
xchar	rtype, rlit;
{
	xchar	xabs, yabs;
	int	wtmp, htmp, xaltmp, yaltmp, xtmp, ytmp;
	NhRect	*r1 = 0, r2;
	int	trycnt = 0;
	boolean	vault = FALSE;
	int	xlim = XLIM, ylim = YLIM;

	if (rtype == -1)	/* Is the type random ? */
	    rtype = OROOM;

	if (rtype == VAULT) {
		vault = TRUE;
		xlim++;
		ylim++;
	}

	/* on low levels the room is lit (usually) */
	/* some other rooms may require lighting */

	/* is light state random ? */
	if (rlit == -1)
	    rlit = (rnd(1+abs(depth(&amp;u.uz))) &lt; 11 &amp;&amp; rn2(77)) ? TRUE : FALSE;

	/*
	 * Here we will try to create a room. If some parameters are
	 * random we are willing to make several try before we give
	 * it up.
	 */
	do {
		xchar xborder, yborder;
		wtmp = w; htmp = h;
		xtmp = x; ytmp = y;
		xaltmp = xal; yaltmp = yal;

		/* First case : a totaly random room */

		if((xtmp &lt; 0 &amp;&amp; ytmp &lt;0 &amp;&amp; wtmp &lt; 0 &amp;&amp; xaltmp &lt; 0 &amp;&amp;
		   yaltmp &lt; 0) || vault) {
			xchar hx, hy, lx, ly, dx, dy;
			r1 = rnd_rect(); /* Get a random rectangle */

			if (!r1) { /* No more free rectangles ! */
#ifdef DEBUG
				debugpline("No more rects...");
#endif
				return FALSE;
			}
			hx = r1-&gt;hx;
			hy = r1-&gt;hy;
			lx = r1-&gt;lx;
			ly = r1-&gt;ly;
			if (vault)
			    dx = dy = 1;
			else {
				dx = 2 + rn2((hx-lx &gt; 28) ? 12 : 8);
				dy = 2 + rn2(4);
				if(dx*dy &gt; 50)
				    dy = 50/dx;
			}
			xborder = (lx &gt; 0 &amp;&amp; hx &lt; COLNO -1) ? 2*xlim : xlim+1;
			yborder = (ly &gt; 0 &amp;&amp; hy &lt; ROWNO -1) ? 2*ylim : ylim+1;
			if(hx-lx &lt; dx + 3 + xborder ||
			   hy-ly &lt; dy + 3 + yborder) {
				r1 = 0;
				continue;
			}
			xabs = lx + (lx &gt; 0 ? xlim : 3)
			    + rn2(hx - (lx&gt;0?lx : 3) - dx - xborder + 1);
			yabs = ly + (ly &gt; 0 ? ylim : 2)
			    + rn2(hy - (ly&gt;0?ly : 2) - dy - yborder + 1);
			if (ly == 0 &amp;&amp; hy &gt;= (ROWNO-1) &amp;&amp;
			    (!nroom || !rn2(nroom)) &amp;&amp; (yabs+dy &gt; ROWNO/2)) {
			    yabs = rn1(3, 2);
			    if(nroom &lt; 4 &amp;&amp; dy&gt;1) dy--;
		        }
			if (!check_room(&amp;xabs, &amp;dx, &amp;yabs, &amp;dy, vault)) {
				r1 = 0;
				continue;
			}
			wtmp = dx+1;
			htmp = dy+1;
			r2.lx = xabs-1; r2.ly = yabs-1;
			r2.hx = xabs + wtmp;
			r2.hy = yabs + htmp;
		} else {	/* Only some parameters are random */
			int rndpos = 0;
			if (xtmp &lt; 0 &amp;&amp; ytmp &lt; 0) { /* Position is RANDOM */
				xtmp = rnd(5);
				ytmp = rnd(5);
				rndpos = 1;
			}
			if (wtmp &lt; 0 || htmp &lt; 0) { /* Size is RANDOM */
				wtmp = rn1(15, 3);
				htmp = rn1(8, 2);
			}
			if (xaltmp == -1) /* Horizontal alignment is RANDOM */
			    xaltmp = rnd(3);
			if (yaltmp == -1) /* Vertical alignment is RANDOM */
			    yaltmp = rnd(3);

			/* Try to generate real (absolute) coordinates here! */

			xabs = (((xtmp-1) * COLNO) / 5) + 1;
			yabs = (((ytmp-1) * ROWNO) / 5) + 1;
			switch (xaltmp) {
			      case LEFT:
				break;
			      case RIGHT:
				xabs += (COLNO / 5) - wtmp;
				break;
			      case CENTER:
				xabs += ((COLNO / 5) - wtmp) / 2;
				break;
			}
			switch (yaltmp) {
			      case TOP:
				break;
			      case BOTTOM:
				yabs += (ROWNO / 5) - htmp;
				break;
			      case CENTER:
				yabs += ((ROWNO / 5) - htmp) / 2;
				break;
			}

			if (xabs + wtmp - 1 &gt; COLNO - 2)
			    xabs = COLNO - wtmp - 3;
			if (xabs &lt; 2)
			    xabs = 2;
			if (yabs + htmp - 1&gt; ROWNO - 2)
			    yabs = ROWNO - htmp - 3;
			if (yabs &lt; 2)
			    yabs = 2;

			/* Try to find a rectangle that fit our room ! */

			r2.lx = xabs-1; r2.ly = yabs-1;
			r2.hx = xabs + wtmp + rndpos;
			r2.hy = yabs + htmp + rndpos;
			r1 = get_rect(&amp;r2);
		}
	} while (++trycnt &lt;= 100 &amp;&amp; !r1);
	if (!r1) {	/* creation of room failed ? */
		return FALSE;
	}
	split_rects(r1, &amp;r2);

	if (!vault) {
		smeq[nroom] = nroom;
		add_room(xabs, yabs, xabs+wtmp-1, yabs+htmp-1,
			 rlit, rtype, FALSE);
	} else {
		rooms[nroom].lx = xabs;
		rooms[nroom].ly = yabs;
	}
	return TRUE;
}
create_subroom
/*
* Create a subroom in room proom at pos x,y with width w &amp; height h.
* x &amp; y are relative to the parent room.
*/

STATIC_OVL boolean
create_subroom(proom, x, y, w,  h, rtype, rlit)
struct mkroom *proom;
xchar x,y;
xchar w,h;
xchar rtype, rlit;
{
	xchar width, height;

	width = proom-&gt;hx - proom-&gt;lx + 1;
	height = proom-&gt;hy - proom-&gt;ly + 1;

	/* There is a minimum size for the parent room */
	if (width &lt; 4 || height &lt; 4)
	    return FALSE;

	/* Check for random position, size, etc... */

	if (w == -1)
	    w = rnd(width - 3);
	if (h == -1)
	    h = rnd(height - 3);
	if (x == -1)
	    x = rnd(width - w - 1) - 1;
	if (y == -1)
	    y = rnd(height - h - 1) - 1;
	if (x == 1)
	    x = 0;
	if (y == 1)
	    y = 0;
	if ((x + w + 1) == width)
	    x++;
	if ((y + h + 1) == height)
	    y++;
	if (rtype == -1)
	    rtype = OROOM;
	if (rlit == -1)
	    rlit = (rnd(1+abs(depth(&amp;u.uz))) &lt; 11 &amp;&amp; rn2(77)) ? TRUE : FALSE;
	add_subroom(proom, proom-&gt;lx + x, proom-&gt;ly + y,
		    proom-&gt;lx + x + w - 1, proom-&gt;ly + y + h - 1,
		    rlit, rtype, FALSE);
	return TRUE;
}
create_door
/*
* Create a new door in a room.
* It's placed on a wall (north, south, east or west).
*/

STATIC_OVL void
create_door(dd, broom)
room_door *dd;
struct mkroom *broom;
{
	int	x, y;
	int	trycnt = 0;

	if (dd-&gt;secret == -1)
	    dd-&gt;secret = rn2(2);

	if (dd-&gt;mask == -1) {
		/* is it a locked door, closed, or a doorway? */
		if (!dd-&gt;secret) {
			if(!rn2(3)) {
				if(!rn2(5))
				    dd-&gt;mask = D_ISOPEN;
				else if(!rn2(6))
				    dd-&gt;mask = D_LOCKED;
				else
				    dd-&gt;mask = D_CLOSED;
				if (dd-&gt;mask != D_ISOPEN &amp;&amp; !rn2(25))
				    dd-&gt;mask |= D_TRAPPED;
			} else
			    dd-&gt;mask = D_NODOOR;
		} else {
			if(!rn2(5))	dd-&gt;mask = D_LOCKED;
			else		dd-&gt;mask = D_CLOSED;

			if(!rn2(20)) dd-&gt;mask |= D_TRAPPED;
		}
	}

	do {
		register int dwall, dpos;

		dwall = dd-&gt;wall;
		if (dwall == -1)	/* The wall is RANDOM */
		    dwall = 1 &lt;&lt; rn2(4);

		dpos = dd-&gt;pos;
		if (dpos == -1)	/* The position is RANDOM */
		    dpos = rn2((dwall == W_WEST || dwall == W_EAST) ?
			    (broom-&gt;hy - broom-&gt;ly) : (broom-&gt;hx - broom-&gt;lx));

		/* Convert wall and pos into an absolute coordinate! */

		switch (dwall) {
		      case W_NORTH:
			y = broom-&gt;ly - 1;
			x = broom-&gt;lx + dpos;
			break;
		      case W_SOUTH:
			y = broom-&gt;hy + 1;
			x = broom-&gt;lx + dpos;
			break;
		      case W_WEST:
			x = broom-&gt;lx - 1;
			y = broom-&gt;ly + dpos;
			break;
		      case W_EAST:
			x = broom-&gt;hx + 1;
			y = broom-&gt;ly + dpos;
			break;
		      default:
			x = y = 0;
			panic("create_door: No wall for door!");
			break;
		}
		if (okdoor(x,y))
		    break;
	} while (++trycnt &lt;= 100);
	if (trycnt &gt; 100) {
		impossible("create_door: Can't find a proper place!");
		return;
	}
	add_door(x,y,broom);
	levl[x][y].typ = (dd-&gt;secret ? SDOOR : DOOR);
	levl[x][y].doormask = dd-&gt;mask;
}
create_secret_door
/*
* Create a secret door in croom on any one of the specified walls.
*/
void
create_secret_door(croom, walls)
struct mkroom *croom;
xchar walls; /* any of W_NORTH | W_SOUTH | W_EAST | W_WEST (or W_ANY) */
{
xchar sx, sy; /* location of the secret door */
int count;

for(count = 0; count &lt; 100; count++) {
	sx = rn1(croom-&gt;hx - croom-&gt;lx + 1, croom-&gt;lx);
	sy = rn1(croom-&gt;hy - croom-&gt;ly + 1, croom-&gt;ly);

	switch(rn2(4)) {
	case 0:  /* top */
	    if(!(walls &amp; W_NORTH)) continue;
	    sy = croom-&gt;ly-1; break;
	case 1: /* bottom */
	    if(!(walls &amp; W_SOUTH)) continue;
	    sy = croom-&gt;hy+1; break;
	case 2: /* left */
	    if(!(walls &amp; W_EAST)) continue;
	    sx = croom-&gt;lx-1; break;
	case 3: /* right */
	    if(!(walls &amp; W_WEST)) continue;
	    sx = croom-&gt;hx+1; break;
	}

	if(okdoor(sx,sy)) {
	    levl[sx][sy].typ = SDOOR;
	    levl[sx][sy].doormask = D_CLOSED;
	    add_door(sx,sy,croom);
	    return;
	}
}

impossible("couldn't create secret door on any walls 0x%x", walls);
}
create_trap
/*
* Create a trap in a room.
*/

STATIC_OVL void
create_trap(t,croom)
trap	*t;
struct mkroom	*croom;
{
schar	x,y;
coord	tm;

if (rn2(100) &lt; t-&gt;chance) {
	x = t-&gt;x;
	y = t-&gt;y;
	if (croom)
	    get_free_room_loc(&amp;x, &amp;y, croom);
	else
	    get_location(&amp;x, &amp;y, DRY);

	tm.x = x;
	tm.y = y;

	mktrap(t-&gt;type, 1, (struct mkroom*) 0, &amp;tm);
}
}
noncoalignment
/*
* Create a monster in a room.
*/

STATIC_OVL int
noncoalignment(alignment)
aligntyp alignment;
{
	int k;

	k = rn2(2);
	if (!alignment)
		return(k ? -1 : 1);
	return(k ? -alignment : 0);
}
create_monster
STATIC_OVL void
create_monster(m,croom)
monster	*m;
struct mkroom	*croom;
{
struct monst *mtmp;
schar x, y;
char class;
aligntyp amask;
coord cc;
struct permonst *pm;
unsigned g_mvflags;

if (rn2(100) &lt; m-&gt;chance) {

	if (m-&gt;class &gt;= 0)
	    class = (char) def_char_to_monclass((char)m-&gt;class);
	else if (m-&gt;class &gt; -11)
	    class = (char) def_char_to_monclass(rmonst[- m-&gt;class - 1]);
	else
	    class = 0;

	if (class == MAXMCLASSES)
	    panic("create_monster: unknown monster class '%c'", m-&gt;class);

	amask = (m-&gt;align == AM_SPLEV_CO) ?
			Align2amask(u.ualignbase[A_ORIGINAL]) :
		(m-&gt;align == AM_SPLEV_NONCO) ?
			Align2amask(noncoalignment(u.ualignbase[A_ORIGINAL])) :
		(m-&gt;align &lt;= -11) ? induced_align(80) :
		(m-&gt;align &lt; 0 ? ralign[-m-&gt;align-1] : m-&gt;align);

	if (!class)
	    pm = (struct permonst *) 0;
	else if (m-&gt;id != NON_PM) {
	    pm = &amp;mons[m-&gt;id];
	    g_mvflags = (unsigned) mvitals[monsndx(pm)].mvflags;
	    if ((pm-&gt;geno &amp; G_UNIQ) &amp;&amp; (g_mvflags &amp; G_EXTINCT))
		goto m_done;
	    else if (g_mvflags &amp; G_GONE)	/* genocided or extinct */
		pm = (struct permonst *) 0;	/* make random monster */
	} else {
	    pm = mkclass(class,G_NOGEN);
	    /* if we can't get a specific monster type (pm == 0) then the
	       class has been genocided, so settle for a random monster */
	}
	if (In_mines(&amp;u.uz) &amp;&amp; pm &amp;&amp; your_race(pm) &amp;&amp;
			(Race_if(PM_DWARF) || Race_if(PM_GNOME)) &amp;&amp; rn2(3))
	    pm = (struct permonst *) 0;

	x = m-&gt;x;
	y = m-&gt;y;
	if (croom)
	    get_room_loc(&amp;x, &amp;y, croom);
	else {
	    if (!pm || !is_swimmer(pm))
		get_location(&amp;x, &amp;y, DRY);
	    else if (pm-&gt;mlet == S_EEL)
		get_location(&amp;x, &amp;y, WET);
	    else
		get_location(&amp;x, &amp;y, DRY|WET);
	}
	/* try to find a close place if someone else is already there */
	if (MON_AT(x,y) &amp;&amp; enexto(&amp;cc, x, y, pm))
	    x = cc.x,  y = cc.y;

	if(m-&gt;align != -12)
	    mtmp = mk_roamer(pm, Amask2align(amask), x, y, m-&gt;peaceful);
	else if(PM_ARCHEOLOGIST &lt;= m-&gt;id &amp;&amp; m-&gt;id &lt;= PM_WIZARD)
	         mtmp = mk_mplayer(pm, x, y, FALSE);
	else mtmp = makemon(pm, x, y, NO_MM_FLAGS);

	if (mtmp) {
	    /* handle specific attributes for some special monsters */
	    if (m-&gt;name.str) mtmp = christen_monst(mtmp, m-&gt;name.str);

	    /*
	     * This is currently hardwired for mimics only.  It should
	     * eventually be expanded.
	     */
	    if (m-&gt;appear_as.str &amp;&amp; mtmp-&gt;data-&gt;mlet == S_MIMIC) {
		int i;

		switch (m-&gt;appear) {
		    case M_AP_NOTHING:
			impossible(
		"create_monster: mon has an appearance, \"%s\", but no type",
				m-&gt;appear_as.str);
			break;

		    case M_AP_FURNITURE:
			for (i = 0; i &lt; MAXPCHARS; i++)
			    if (!strcmp(defsyms[i].explanation,
					m-&gt;appear_as.str))
				break;
			if (i == MAXPCHARS) {
			    impossible(
				"create_monster: can't find feature \"%s\"",
				m-&gt;appear_as.str);
			} else {
			    mtmp-&gt;m_ap_type = M_AP_FURNITURE;
			    mtmp-&gt;mappearance = i;
			}
			break;

		    case M_AP_OBJECT:
			for (i = 0; i &lt; NUM_OBJECTS; i++)
			    if (OBJ_NAME(objects[i]) &amp;&amp;
				!strcmp(OBJ_NAME(objects[i]),m-&gt;appear_as.str))
				break;
			if (i == NUM_OBJECTS) {
			    impossible(
				"create_monster: can't find object \"%s\"",
				m-&gt;appear_as.str);
			} else {
			    mtmp-&gt;m_ap_type = M_AP_OBJECT;
			    mtmp-&gt;mappearance = i;
			}
			break;

		    case M_AP_MONSTER:
			/* note: mimics don't appear as monsters! */
			/*	 (but chameleons can :-)	  */
		    default:
			impossible(
		"create_monster: unimplemented mon appear type [%d,\"%s\"]",
				m-&gt;appear, m-&gt;appear_as.str);
			break;
		}
		if (does_block(x, y, &amp;levl[x][y]))
		    block_point(x, y);
	    }

	    if (m-&gt;peaceful &gt;= 0) {
		mtmp-&gt;mpeaceful = m-&gt;peaceful;
		/* changed mpeaceful again; have to reset malign */
		set_malign(mtmp);
	    }
	    if (m-&gt;asleep &gt;= 0) {
#ifdef UNIXPC
		/* optimizer bug strikes again */
		if (m-&gt;asleep)
			mtmp-&gt;msleeping = 1;
		else
			mtmp-&gt;msleeping = 0;
#else
		mtmp-&gt;msleeping = m-&gt;asleep;
#endif
	    }
	}

}		/* if (rn2(100) &lt; m-&gt;chance) */
m_done:
Free(m-&gt;name.str);
Free(m-&gt;appear_as.str);
}
create_object
/*
* Create an object in a room.
*/

STATIC_OVL void
create_object(o,croom)
object	*o;
struct mkroom	*croom;
{
struct obj *otmp;
schar x, y;
char c;
boolean named;	/* has a name been supplied in level description? */

if (rn2(100) &lt; o-&gt;chance) {
	named = o-&gt;name.str ? TRUE : FALSE;

	x = o-&gt;x; y = o-&gt;y;
	if (croom)
	    get_room_loc(&amp;x, &amp;y, croom);
	else
	    get_location(&amp;x, &amp;y, DRY);

	if (o-&gt;class &gt;= 0)
	    c = o-&gt;class;
	else if (o-&gt;class &gt; -11)
	    c = robjects[ -(o-&gt;class+1)];
	else
	    c = 0;

	if (!c)
	    otmp = mkobj_at(RANDOM_CLASS, x, y, !named);
	else if (o-&gt;id != -1)
	    otmp = mksobj_at(o-&gt;id, x, y, TRUE, !named);
	else {
	    /*
	     * The special levels are compiled with the default "text" object
	     * class characters.  We must convert them to the internal format.
	     */
	    char oclass = (char) def_char_to_objclass(c);

	    if (oclass == MAXOCLASSES)
		panic("create_object:  unexpected object class '%c'",c);

	    /* KMH -- Create piles of gold properly */
	    if (oclass == COIN_CLASS)
		otmp = mkgold(0L, x, y);
	    else
		otmp = mkobj_at(oclass, x, y, !named);
	}

	if (o-&gt;spe != -127)	/* That means NOT RANDOM! */
	    otmp-&gt;spe = (schar)o-&gt;spe;

	switch (o-&gt;curse_state) {
	      case 1:	bless(otmp); break; /* BLESSED */
	      case 2:	unbless(otmp); uncurse(otmp); break; /* uncursed */
	      case 3:	curse(otmp); break; /* CURSED */
	      default:	break;	/* Otherwise it's random and we're happy
				 * with what mkobj gave us! */
	}

	/*	corpsenm is "empty" if -1, random if -2, otherwise specific */
	if (o-&gt;corpsenm == NON_PM - 1) otmp-&gt;corpsenm = rndmonnum();
	else if (o-&gt;corpsenm != NON_PM) otmp-&gt;corpsenm = o-&gt;corpsenm;

	/* assume we wouldn't be given an egg corpsenm unless it was
	   hatchable */
	if (otmp-&gt;otyp == EGG &amp;&amp; otmp-&gt;corpsenm != NON_PM) {
	    if (dead_species(otmp-&gt;otyp, TRUE))
		kill_egg(otmp);	/* make sure nothing hatches */
	    else
		attach_egg_hatch_timeout(otmp);	/* attach new hatch timeout */
	}

	if (named)
	    otmp = oname(otmp, o-&gt;name.str);

	switch(o-&gt;containment) {
	    static struct obj *container = 0;

	    /* contents */
	    case 1:
		if (!container) {
		    impossible("create_object: no container");
		    break;
		}
		remove_object(otmp);
		(void) add_to_container(container, otmp);
		goto o_done;		/* don't stack, but do other cleanup */
	    /* container */
	    case 2:
		delete_contents(otmp);
		container = otmp;
		break;
	    /* nothing */
	    case 0: break;

	    default: impossible("containment type %d?", (int) o-&gt;containment);
	}

	/* Medusa level special case: statues are petrified monsters, so they
	 * are not stone-resistant and have monster inventory.  They also lack
	 * other contents, but that can be specified as an empty container.
	 */
	if (o-&gt;id == STATUE &amp;&amp; Is_medusa_level(&amp;u.uz) &amp;&amp;
		    o-&gt;corpsenm == NON_PM) {
	    struct monst *was;
	    struct obj *obj;
	    int wastyp;

	    /* Named random statues are of player types, and aren't stone-
	     * resistant (if they were, we'd have to reset the name as well as
	     * setting corpsenm).
	     */
	    for (wastyp = otmp-&gt;corpsenm; ; wastyp = rndmonnum()) {
		/* makemon without rndmonst() might create a group */
		was = makemon(&amp;mons[wastyp], 0, 0, NO_MM_FLAGS);
		if (!resists_ston(was)) break;
		mongone(was);
	    }
	    otmp-&gt;corpsenm = wastyp;
	    while(was-&gt;minvent) {
		obj = was-&gt;minvent;
		obj-&gt;owornmask = 0;
		obj_extract_self(obj);
		(void) add_to_container(otmp, obj);
	    }
	    otmp-&gt;owt = weight(otmp);
	    mongone(was);
	}

	stackobj(otmp);

}		/* if (rn2(100) &lt; o-&gt;chance) */
o_done:
Free(o-&gt;name.str);
}
create_engraving
/*
* Randomly place a specific engraving, then release its memory.
*/
STATIC_OVL void
create_engraving(e, croom)
engraving *e;
struct mkroom *croom;
{
	xchar x, y;

	x = e-&gt;x,  y = e-&gt;y;
	if (croom)
	    get_room_loc(&amp;x, &amp;y, croom);
	else
	    get_location(&amp;x, &amp;y, DRY);

	make_engr_at(x, y, e-&gt;engr.str, 0L, e-&gt;etype);
	free((genericptr_t) e-&gt;engr.str);
}
create_stairs
/*
* Create stairs in a room.
*
*/

STATIC_OVL void
create_stairs(s,croom)
stair	*s;
struct mkroom	*croom;
{
	schar		x,y;

	x = s-&gt;x; y = s-&gt;y;
	get_free_room_loc(&amp;x, &amp;y, croom);
	mkstairs(x,y,(char)s-&gt;up, croom);
}
create_altar
/*
* Create an altar in a room.
*/

STATIC_OVL void
create_altar(a, croom)
	altar		*a;
	struct mkroom	*croom;
{
	schar		sproom,x,y;
	aligntyp	amask;
	boolean		croom_is_temple = TRUE;
	int oldtyp; 

	x = a-&gt;x; y = a-&gt;y;

	if (croom) {
	    get_free_room_loc(&amp;x, &amp;y, croom);
	    if (croom-&gt;rtype != TEMPLE)
		croom_is_temple = FALSE;
	} else {
	    get_location(&amp;x, &amp;y, DRY);
	    if ((sproom = (schar) *in_rooms(x, y, TEMPLE)) != 0)
		croom = &amp;rooms[sproom - ROOMOFFSET];
	    else
		croom_is_temple = FALSE;
	}

	/* check for existing features */
	oldtyp = levl[x][y].typ;
	if (oldtyp == STAIRS || oldtyp == LADDER)
	    return;

	a-&gt;x = x;
	a-&gt;y = y;

	/* Is the alignment random ?
	 * If so, it's an 80% chance that the altar will be co-aligned.
	 *
	 * The alignment is encoded as amask values instead of alignment
	 * values to avoid conflicting with the rest of the encoding,
	 * shared by many other parts of the special level code.
	 */

	amask = (a-&gt;align == AM_SPLEV_CO) ?
			Align2amask(u.ualignbase[A_ORIGINAL]) :
		(a-&gt;align == AM_SPLEV_NONCO) ?
			Align2amask(noncoalignment(u.ualignbase[A_ORIGINAL])) :
		(a-&gt;align == -11) ? induced_align(80) :
		(a-&gt;align &lt; 0 ? ralign[-a-&gt;align-1] : a-&gt;align);

	levl[x][y].typ = ALTAR;
	levl[x][y].altarmask = amask;

	if (a-&gt;shrine &lt; 0) a-&gt;shrine = rn2(2);	/* handle random case */

	if (oldtyp == FOUNTAIN)
	    level.flags.nfountains--;
	else if (oldtyp == SINK)
	    level.flags.nsinks--;

	if (!croom_is_temple || !a-&gt;shrine) return;

	if (a-&gt;shrine) {	/* Is it a shrine  or sanctum? */
	    priestini(&amp;u.uz, croom, x, y, (a-&gt;shrine &gt; 1));
	    levl[x][y].altarmask |= AM_SHRINE;
	    level.flags.has_temple = TRUE;
	}
}
create_gold
/*
* Create a gold pile in a room.
*/

STATIC_OVL void
create_gold(g,croom)
gold *g;
struct mkroom	*croom;
{
	schar		x,y;

	x = g-&gt;x; y= g-&gt;y;
	if (croom)
	    get_room_loc(&amp;x, &amp;y, croom);
	else
	    get_location(&amp;x, &amp;y, DRY);

	if (g-&gt;amount == -1)
	    g-&gt;amount = rnd(200);
	(void) mkgold((long) g-&gt;amount, x, y);
}
create_feature
/*
* Create a feature (e.g a fountain) in a room.
*/

STATIC_OVL void
create_feature(fx, fy, croom, typ)
int		fx, fy;
struct mkroom	*croom;
int		typ;
{
	schar		x,y;
	int		trycnt = 0;

	x = fx;  y = fy;
	if (croom) {
	    if (x &lt; 0 &amp;&amp; y &lt; 0)
		do {
		    x = -1;  y = -1;
		    get_room_loc(&amp;x, &amp;y, croom);
		} while (++trycnt &lt;= 200 &amp;&amp; occupied(x,y));
	    else
		get_room_loc(&amp;x, &amp;y, croom);
	    if(trycnt &gt; 200)
		return;
	} else {
	    get_location(&amp;x, &amp;y, DRY);
	}
	/* Don't cover up an existing feature (particularly randomly
	   placed stairs).  However, if the _same_ feature is already
	   here, it came from the map drawing and we still need to
	   update the special counters. */
	if (IS_FURNITURE(levl[x][y].typ) &amp;&amp; levl[x][y].typ != typ)
	    return;

	levl[x][y].typ = typ;
	if (typ == FOUNTAIN)
	    level.flags.nfountains++;
	else if (typ == SINK)
	    level.flags.nsinks++;
}
search_door
/*
* Search for a door in a room on a specified wall.
*/

STATIC_OVL boolean
search_door(croom,x,y,wall,cnt)
struct mkroom *croom;
xchar *x, *y;
xchar wall;
int cnt;
{
	int dx, dy;
	int xx,yy;

	switch(wall) {
	      case W_NORTH:
		dy = 0; dx = 1;
		xx = croom-&gt;lx;
		yy = croom-&gt;hy + 1;
		break;
	      case W_SOUTH:
		dy = 0; dx = 1;
		xx = croom-&gt;lx;
		yy = croom-&gt;ly - 1;
		break;
	      case W_EAST:
		dy = 1; dx = 0;
		xx = croom-&gt;hx + 1;
		yy = croom-&gt;ly;
		break;
	      case W_WEST:
		dy = 1; dx = 0;
		xx = croom-&gt;lx - 1;
		yy = croom-&gt;ly;
		break;
	      default:
		dx = dy = xx = yy = 0;
		panic("search_door: Bad wall!");
		break;
	}
	while (xx &lt;= croom-&gt;hx+1 &amp;&amp; yy &lt;= croom-&gt;hy+1) {
		if (IS_DOOR(levl[xx][yy].typ) || levl[xx][yy].typ == SDOOR) {
			*x = xx;
			*y = yy;
			if (cnt-- &lt;= 0)
			    return TRUE;
		}
		xx += dx;
		yy += dy;
	}
	return FALSE;
}
dig_corridor
/*
* Dig a corridor between two points.
*/

boolean
dig_corridor(org,dest,nxcor,ftyp,btyp)
coord *org, *dest;
boolean nxcor;
schar ftyp, btyp;
{
	register int dx=0, dy=0, dix, diy, cct;
	register struct rm *crm;
	register int tx, ty, xx, yy;

	xx = org-&gt;x;  yy = org-&gt;y;
	tx = dest-&gt;x; ty = dest-&gt;y;
	if (xx &lt;= 0 || yy &lt;= 0 || tx &lt;= 0 || ty &lt;= 0 ||
	    xx &gt; COLNO-1 || tx &gt; COLNO-1 ||
	    yy &gt; ROWNO-1 || ty &gt; ROWNO-1) {
#ifdef DEBUG
		debugpline("dig_corridor: bad coords : (%d,%d) (%d,%d).",
			   xx,yy,tx,ty);
#endif
		return FALSE;
	}
	if (tx &gt; xx)		dx = 1;
	else if (ty &gt; yy)	dy = 1;
	else if (tx &lt; xx)	dx = -1;
	else			dy = -1;

	xx -= dx;
	yy -= dy;
	cct = 0;
	while(xx != tx || yy != ty) {
	    /* loop: dig corridor at [xx,yy] and find new [xx,yy] */
	    if(cct++ &gt; 500 || (nxcor &amp;&amp; !rn2(35)))
		return FALSE;

	    xx += dx;
	    yy += dy;

	    if(xx &gt;= COLNO-1 || xx &lt;= 0 || yy &lt;= 0 || yy &gt;= ROWNO-1)
		return FALSE;		/* impossible */

	    crm = &amp;levl[xx][yy];
	    if(crm-&gt;typ == btyp) {
		if(ftyp != CORR || rn2(100)) {
			crm-&gt;typ = ftyp;
			if(nxcor &amp;&amp; !rn2(50))
				(void) mksobj_at(BOULDER, xx, yy, TRUE, FALSE);
		} else {
			crm-&gt;typ = SCORR;
		}
	    } else
	    if(crm-&gt;typ != ftyp &amp;&amp; crm-&gt;typ != SCORR) {
		/* strange ... */
		return FALSE;
	    }

	    /* find next corridor position */
	    dix = abs(xx-tx);
	    diy = abs(yy-ty);

	    /* do we have to change direction ? */
	    if(dy &amp;&amp; dix &gt; diy) {
		register int ddx = (xx &gt; tx) ? -1 : 1;

		crm = &amp;levl[xx+ddx][yy];
		if(crm-&gt;typ == btyp || crm-&gt;typ == ftyp || crm-&gt;typ == SCORR) {
		    dx = ddx;
		    dy = 0;
		    continue;
		}
	    } else if(dx &amp;&amp; diy &gt; dix) {
		register int ddy = (yy &gt; ty) ? -1 : 1;

		crm = &amp;levl[xx][yy+ddy];
		if(crm-&gt;typ == btyp || crm-&gt;typ == ftyp || crm-&gt;typ == SCORR) {
		    dy = ddy;
		    dx = 0;
		    continue;
		}
	    }

	    /* continue straight on? */
	    crm = &amp;levl[xx+dx][yy+dy];
	    if(crm-&gt;typ == btyp || crm-&gt;typ == ftyp || crm-&gt;typ == SCORR)
		continue;

	    /* no, what must we do now?? */
	    if(dx) {
		dx = 0;
		dy = (ty &lt; yy) ? -1 : 1;
	    } else {
		dy = 0;
		dx = (tx &lt; xx) ? -1 : 1;
	    }
	    crm = &amp;levl[xx+dx][yy+dy];
	    if(crm-&gt;typ == btyp || crm-&gt;typ == ftyp || crm-&gt;typ == SCORR)
		continue;
	    dy = -dy;
	    dx = -dx;
	}
	return TRUE;
}
fix_stair_rooms
/*
* Disgusting hack: since special levels have their rooms filled before
* sorting the rooms, we have to re-arrange the speed values upstairs_room
* and dnstairs_room after the rooms have been sorted.  On normal levels,
* stairs don't get created until _after_ sorting takes place.
*/
STATIC_OVL void
fix_stair_rooms()
{
int i;
struct mkroom *croom;

if(xdnstair &amp;&amp;
!((dnstairs_room-&gt;lx &lt;= xdnstair &amp;&amp; xdnstair &lt;= dnstairs_room-&gt;hx) &amp;&amp;
	 (dnstairs_room-&gt;ly &lt;= ydnstair &amp;&amp; ydnstair &lt;= dnstairs_room-&gt;hy))) {
	for(i=0; i &lt; nroom; i++) {
	    croom = &amp;rooms[i];
	    if((croom-&gt;lx &lt;= xdnstair &amp;&amp; xdnstair &lt;= croom-&gt;hx) &amp;&amp;
	       (croom-&gt;ly &lt;= ydnstair &amp;&amp; ydnstair &lt;= croom-&gt;hy)) {
		dnstairs_room = croom;
		break;
	    }
	}
	if(i == nroom)
	    panic("Couldn't find dnstair room in fix_stair_rooms!");
}
if(xupstair &amp;&amp;
!((upstairs_room-&gt;lx &lt;= xupstair &amp;&amp; xupstair &lt;= upstairs_room-&gt;hx) &amp;&amp;
	 (upstairs_room-&gt;ly &lt;= yupstair &amp;&amp; yupstair &lt;= upstairs_room-&gt;hy))) {
	for(i=0; i &lt; nroom; i++) {
	    croom = &amp;rooms[i];
	    if((croom-&gt;lx &lt;= xupstair &amp;&amp; xupstair &lt;= croom-&gt;hx) &amp;&amp;
	       (croom-&gt;ly &lt;= yupstair &amp;&amp; yupstair &lt;= croom-&gt;hy)) {
		upstairs_room = croom;
		break;
	    }
	}
	if(i == nroom)
	    panic("Couldn't find upstair room in fix_stair_rooms!");
}
}
create_corridor
/*
* Corridors always start from a door. But it can end anywhere...
* Basically we search for door coordinates or for endpoints coordinates
* (from a distance).
*/

STATIC_OVL void
create_corridor(c)
corridor	*c;
{
	coord org, dest;

	if (c-&gt;src.room == -1) {
		sort_rooms();
		fix_stair_rooms();
		makecorridors();
		return;
	}

	if( !search_door(&amp;rooms[c-&gt;src.room], &amp;org.x, &amp;org.y, c-&gt;src.wall,
			 c-&gt;src.door))
	    return;

	if (c-&gt;dest.room != -1) {
		if(!search_door(&amp;rooms[c-&gt;dest.room], &amp;dest.x, &amp;dest.y,
				c-&gt;dest.wall, c-&gt;dest.door))
		    return;
		switch(c-&gt;src.wall) {
		      case W_NORTH: org.y--; break;
		      case W_SOUTH: org.y++; break;
		      case W_WEST:  org.x--; break;
		      case W_EAST:  org.x++; break;
		}
		switch(c-&gt;dest.wall) {
		      case W_NORTH: dest.y--; break;
		      case W_SOUTH: dest.y++; break;
		      case W_WEST:  dest.x--; break;
		      case W_EAST:  dest.x++; break;
		}
		(void) dig_corridor(&amp;org, &amp;dest, FALSE, CORR, STONE);
	}
}
fill_room
/*
* Fill a room (shop, zoo, etc...) with appropriate stuff.
*/

void
fill_room(croom, prefilled)
struct mkroom *croom;
boolean prefilled;
{
	if (!croom || croom-&gt;rtype == OROOM)
	    return;

	if (!prefilled) {
	    int x,y;

	    /* Shop ? */
	    if (croom-&gt;rtype &gt;= SHOPBASE) {
		    stock_room(croom-&gt;rtype - SHOPBASE, croom);
		    level.flags.has_shop = TRUE;
		    return;
	    }

	    switch (croom-&gt;rtype) {
		case VAULT:
		    for (x=croom-&gt;lx;x&lt;=croom-&gt;hx;x++)
			for (y=croom-&gt;ly;y&lt;=croom-&gt;hy;y++)
			    (void) mkgold((long)rn1(abs(depth(&amp;u.uz))*100, 51), x, y);
		    break;
		case COURT:
		case ZOO:
		case BEEHIVE:
		case MORGUE:
		case BARRACKS:
		    fill_zoo(croom);
		    break;
	    }
	}
	switch (croom-&gt;rtype) {
	    case VAULT:
		level.flags.has_vault = TRUE;
		break;
	    case ZOO:
		level.flags.has_zoo = TRUE;
		break;
	    case COURT:
		level.flags.has_court = TRUE;
		break;
	    case MORGUE:
		level.flags.has_morgue = TRUE;
		break;
	    case BEEHIVE:
		level.flags.has_beehive = TRUE;
		break;
	    case BARRACKS:
		level.flags.has_barracks = TRUE;
		break;
	    case TEMPLE:
		level.flags.has_temple = TRUE;
		break;
	    case SWAMP:
		level.flags.has_swamp = TRUE;
		break;
	}
}
free_rooms
STATIC_OVL void
free_rooms(ro, n)
room **ro;
int n;
{
	short j;
	room *r;

	while(n--) {
		r = ro[n];
		Free(r-&gt;name);
		Free(r-&gt;parent);
		if ((j = r-&gt;ndoor) != 0) {
			while(j--)
			    Free(r-&gt;doors[j]);
			Free(r-&gt;doors);
		}
		if ((j = r-&gt;nstair) != 0) {
			while(j--)
			    Free(r-&gt;stairs[j]);
			Free(r-&gt;stairs);
		}
		if ((j = r-&gt;naltar) != 0) {
			while (j--)
			    Free(r-&gt;altars[j]);
			Free(r-&gt;altars);
		}
		if ((j = r-&gt;nfountain) != 0) {
			while(j--)
			    Free(r-&gt;fountains[j]);
			Free(r-&gt;fountains);
		}
		if ((j = r-&gt;nsink) != 0) {
			while(j--)
			    Free(r-&gt;sinks[j]);
			Free(r-&gt;sinks);
		}
		if ((j = r-&gt;npool) != 0) {
			while(j--)
			    Free(r-&gt;pools[j]);
			Free(r-&gt;pools);
		}
		if ((j = r-&gt;ntrap) != 0) {
			while (j--)
			    Free(r-&gt;traps[j]);
			Free(r-&gt;traps);
		}
		if ((j = r-&gt;nmonster) != 0) {
			while (j--)
				Free(r-&gt;monsters[j]);
			Free(r-&gt;monsters);
		}
		if ((j = r-&gt;nobject) != 0) {
			while (j--)
				Free(r-&gt;objects[j]);
			Free(r-&gt;objects);
		}
		if ((j = r-&gt;ngold) != 0) {
			while(j--)
			    Free(r-&gt;golds[j]);
			Free(r-&gt;golds);
		}
		if ((j = r-&gt;nengraving) != 0) {
			while (j--)
				Free(r-&gt;engravings[j]);
			Free(r-&gt;engravings);
		}
		Free(r);
	}
	Free(ro);
}
build_room
STATIC_OVL void
build_room(r, pr)
room *r, *pr;
{
	boolean okroom;
	struct mkroom	*aroom;
	short i;
	xchar rtype = (!r-&gt;chance || rn2(100) &lt; r-&gt;chance) ? r-&gt;rtype : OROOM;

	if(pr) {
		aroom = &amp;subrooms[nsubroom];
		okroom = create_subroom(pr-&gt;mkr, r-&gt;x, r-&gt;y, r-&gt;w, r-&gt;h,
					rtype, r-&gt;rlit);
	} else {
		aroom = &amp;rooms[nroom];
		okroom = create_room(r-&gt;x, r-&gt;y, r-&gt;w, r-&gt;h, r-&gt;xalign,
				     r-&gt;yalign, rtype, r-&gt;rlit);
		r-&gt;mkr = aroom;
	}

	if (okroom) {
		/* Create subrooms if necessary... */
		for(i=0; i &lt; r-&gt;nsubroom; i++)
		    build_room(r-&gt;subrooms[i], r);
		/* And now we can fill the room! */

		/* Priority to the stairs */

		for(i=0; i 

<r->nstair; i++)
		    create_stairs(r-&gt;stairs[i], aroom);

		/* Then to the various elements (sinks, etc..) */
		for(i = 0; i<r->nsink; i++)
		    create_feature(r-&gt;sinks[i]-&gt;x, r-&gt;sinks[i]-&gt;y, aroom, SINK);
		for(i = 0; i<r->npool; i++)
		    create_feature(r-&gt;pools[i]-&gt;x, r-&gt;pools[i]-&gt;y, aroom, POOL);
		for(i = 0; i<r->nfountain; i++)
		    create_feature(r-&gt;fountains[i]-&gt;x, r-&gt;fountains[i]-&gt;y,
				   aroom, FOUNTAIN);
		for(i = 0; i<r->naltar; i++)
		    create_altar(r-&gt;altars[i], aroom);
		for(i = 0; i<r->ndoor; i++)
		    create_door(r-&gt;doors[i], aroom);

		/* The traps */
		for(i = 0; i<r->ntrap; i++)
		    create_trap(r-&gt;traps[i], aroom);

		/* The monsters */
		for(i = 0; i<r->nmonster; i++)
		    create_monster(r-&gt;monsters[i], aroom);

		/* The objects */
		for(i = 0; i<r->nobject; i++)
		    create_object(r-&gt;objects[i], aroom);

		/* The gold piles */
		for(i = 0; i<r->ngold; i++)
		    create_gold(r-&gt;golds[i], aroom);

		/* The engravings */
		for (i = 0; i &lt; r-&gt;nengraving; i++)
		    create_engraving(r-&gt;engravings[i], aroom);

#ifdef SPECIALIZATION
		topologize(aroom,FALSE);		/* set roomno */
#else
		topologize(aroom);			/* set roomno */
#endif
		/* MRS - 07/04/91 - This is temporary but should result
		 * in proper filling of shops, etc.
		 * DLC - this can fail if corridors are added to this room
		 * at a later point.  Currently no good way to fix this.
		 */
		if(aroom-&gt;rtype != OROOM &amp;&amp; r-&gt;filled) fill_room(aroom, FALSE);
	}
}
light_region
/*
* set lighting in a region that will not become a room.
*/
STATIC_OVL void
light_region(tmpregion)
region  *tmpregion;
{
register boolean litstate = tmpregion-&gt;rlit ? 1 : 0;
register int hiy = tmpregion-&gt;y2;
register int x, y;
register struct rm *lev;
int lowy = tmpregion-&gt;y1;
int lowx = tmpregion-&gt;x1, hix = tmpregion-&gt;x2;

if(litstate) {
	/* adjust region size for walls, but only if lighted */
	lowx = max(lowx-1,1);
	hix = min(hix+1,COLNO-1);
	lowy = max(lowy-1,0);
	hiy = min(hiy+1, ROWNO-1);
}
for(x = lowx; x &lt;= hix; x++) {
	lev = &amp;levl[x][lowy];
	for(y = lowy; y &lt;= hiy; y++) {
	    if (lev-&gt;typ != LAVAPOOL) /* this overrides normal lighting */
		lev-&gt;lit = litstate;
	    lev++;
	}
}
}
load_common_data
/* initialization common to all special levels */
STATIC_OVL void
load_common_data(fd, typ)
dlb *fd;
int typ;
{
	uchar	n;
	long	lev_flags;
	int	i;

{
	aligntyp atmp;
	/* shuffle 3 alignments; can't use sp_lev_shuffle() on aligntyp's */
	i = rn2(3);   atmp=ralign[2]; ralign[2]=ralign[i]; ralign[i]=atmp;
	if (rn2(2)) { atmp=ralign[1]; ralign[1]=ralign[0]; ralign[0]=atmp; }
}

	level.flags.is_maze_lev = typ == SP_LEV_MAZE;

	/* Read the level initialization data */
	Fread((genericptr_t) &amp;init_lev, 1, sizeof(lev_init), fd);
	if(init_lev.init_present) {
	    if(init_lev.lit &lt; 0)
		init_lev.lit = rn2(2);
	    mkmap(&amp;init_lev);
	}

	/* Read the per level flags */
	Fread((genericptr_t) &amp;lev_flags, 1, sizeof(lev_flags), fd);
	if (lev_flags &amp; NOTELEPORT)
	    level.flags.noteleport = 1;
	if (lev_flags &amp; HARDFLOOR)
	    level.flags.hardfloor = 1;
	if (lev_flags &amp; NOMMAP)
	    level.flags.nommap = 1;
	if (lev_flags &amp; SHORTSIGHTED)
	    level.flags.shortsighted = 1;
	if (lev_flags &amp; ARBOREAL)
	    level.flags.arboreal = 1;

	/* Read message */
	Fread((genericptr_t) &amp;n, 1, sizeof(n), fd);
	if (n) {
	    lev_message = (char *) alloc(n + 1);
	    Fread((genericptr_t) lev_message, 1, (int) n, fd);
	    lev_message[n] = 0;
	}
}
load_one_monster
STATIC_OVL void
load_one_monster(fd, m)
dlb *fd;
monster *m;
{
	int size;

	Fread((genericptr_t) m, 1, sizeof *m, fd);
	if ((size = m-&gt;name.len) != 0) {
	    m-&gt;name.str = (char *) alloc((unsigned)size + 1);
	    Fread((genericptr_t) m-&gt;name.str, 1, size, fd);
	    m-&gt;name.str[size] = '\0';
	} else
	    m-&gt;name.str = (char *) 0;
	if ((size = m-&gt;appear_as.len) != 0) {
	    m-&gt;appear_as.str = (char *) alloc((unsigned)size + 1);
	    Fread((genericptr_t) m-&gt;appear_as.str, 1, size, fd);
	    m-&gt;appear_as.str[size] = '\0';
	} else
	    m-&gt;appear_as.str = (char *) 0;
}
load_one_object
STATIC_OVL void
load_one_object(fd, o)
dlb *fd;
object *o;
{
	int size;

	Fread((genericptr_t) o, 1, sizeof *o, fd);
	if ((size = o-&gt;name.len) != 0) {
	    o-&gt;name.str = (char *) alloc((unsigned)size + 1);
	    Fread((genericptr_t) o-&gt;name.str, 1, size, fd);
	    o-&gt;name.str[size] = '\0';
	} else
	    o-&gt;name.str = (char *) 0;
}
load_one_engraving
STATIC_OVL void
load_one_engraving(fd, e)
dlb *fd;
engraving *e;
{
	int size;

	Fread((genericptr_t) e, 1, sizeof *e, fd);
	size = e-&gt;engr.len;
	e-&gt;engr.str = (char *) alloc((unsigned)size+1);
	Fread((genericptr_t) e-&gt;engr.str, 1, size, fd);
	e-&gt;engr.str[size] = '\0';
}
load_rooms
STATIC_OVL boolean
load_rooms(fd)
dlb *fd;
{
	xchar		nrooms, ncorr;
	char		n;
	short		size;
	corridor	tmpcor;
	room**		tmproom;
	int		i, j;

	load_common_data(fd, SP_LEV_ROOMS);

	Fread((genericptr_t) &amp;n, 1, sizeof(n), fd); /* nrobjects */
	if (n) {
		Fread((genericptr_t)robjects, sizeof(*robjects), n, fd);
		sp_lev_shuffle(robjects, (char *)0, (int)n);
	}

	Fread((genericptr_t) &amp;n, 1, sizeof(n), fd); /* nrmonst */
	if (n) {
		Fread((genericptr_t)rmonst, sizeof(*rmonst), n, fd);
		sp_lev_shuffle(rmonst, (char *)0, (int)n);
	}

	Fread((genericptr_t) &amp;nrooms, 1, sizeof(nrooms), fd);
						/* Number of rooms to read */
	tmproom = NewTab(room,nrooms);
	for (i=0;i<nrooms;i++) &size,="" (size="" *="" *r;="" 1,="" =="" a="" fd);="" fread((genericptr_t)="" has="" if="" let's="" name="" new(room);="" r="tmproom[i]" room="" see="" sizeof(size),="" this="" {=""> 0) {	/* Yup, it does! */
			r-&gt;name = (char *) alloc((unsigned)size + 1);
			Fread((genericptr_t) r-&gt;name, 1, size, fd);
			r-&gt;name[size] = 0;
		} else
		    r-&gt;name = (char *) 0;

		/* Let's see if this room has a parent */
		Fread((genericptr_t) &amp;size, 1, sizeof(size), fd);
		if (size &gt; 0) {	/* Yup, it does! */
			r-&gt;parent = (char *) alloc((unsigned)size + 1);
			Fread((genericptr_t) r-&gt;parent, 1, size, fd);
			r-&gt;parent[size] = 0;
		} else
		    r-&gt;parent = (char *) 0;

		Fread((genericptr_t) &amp;r-&gt;x, 1, sizeof(r-&gt;x), fd);
					/* x pos on the grid (1-5) */
		Fread((genericptr_t) &amp;r-&gt;y, 1, sizeof(r-&gt;y), fd);
					 /* y pos on the grid (1-5) */
		Fread((genericptr_t) &amp;r-&gt;w, 1, sizeof(r-&gt;w), fd);
					 /* width of the room */
		Fread((genericptr_t) &amp;r-&gt;h, 1, sizeof(r-&gt;h), fd);
					 /* height of the room */
		Fread((genericptr_t) &amp;r-&gt;xalign, 1, sizeof(r-&gt;xalign), fd);
					 /* horizontal alignment */
		Fread((genericptr_t) &amp;r-&gt;yalign, 1, sizeof(r-&gt;yalign), fd);
					 /* vertical alignment */
		Fread((genericptr_t) &amp;r-&gt;rtype, 1, sizeof(r-&gt;rtype), fd);
					 /* type of room (zoo, shop, etc.) */
		Fread((genericptr_t) &amp;r-&gt;chance, 1, sizeof(r-&gt;chance), fd);
					 /* chance of room being special. */
		Fread((genericptr_t) &amp;r-&gt;rlit, 1, sizeof(r-&gt;rlit), fd);
					 /* lit or not ? */
		Fread((genericptr_t) &amp;r-&gt;filled, 1, sizeof(r-&gt;filled), fd);
					 /* to be filled? */
		r-&gt;nsubroom= 0;

		/* read the doors */
		Fread((genericptr_t) &amp;r-&gt;ndoor, 1, sizeof(r-&gt;ndoor), fd);
		if ((n = r-&gt;ndoor) != 0)
		    r-&gt;doors = NewTab(room_door, n);
		while(n--) {
			r-&gt;doors[(int)n] = New(room_door);
			Fread((genericptr_t) r-&gt;doors[(int)n], 1,
				sizeof(room_door), fd);
		}

		/* read the stairs */
		Fread((genericptr_t) &amp;r-&gt;nstair, 1, sizeof(r-&gt;nstair), fd);
		if ((n = r-&gt;nstair) != 0)
		    r-&gt;stairs = NewTab(stair, n);
		while (n--) {
			r-&gt;stairs[(int)n] = New(stair);
			Fread((genericptr_t) r-&gt;stairs[(int)n], 1,
				sizeof(stair), fd);
		}

		/* read the altars */
		Fread((genericptr_t) &amp;r-&gt;naltar, 1, sizeof(r-&gt;naltar), fd);
		if ((n = r-&gt;naltar) != 0)
		    r-&gt;altars = NewTab(altar, n);
		while (n--) {
			r-&gt;altars[(int)n] = New(altar);
			Fread((genericptr_t) r-&gt;altars[(int)n], 1,
				sizeof(altar), fd);
		}

		/* read the fountains */
		Fread((genericptr_t) &amp;r-&gt;nfountain, 1,
			sizeof(r-&gt;nfountain), fd);
		if ((n = r-&gt;nfountain) != 0)
		    r-&gt;fountains = NewTab(fountain, n);
		while (n--) {
			r-&gt;fountains[(int)n] = New(fountain);
			Fread((genericptr_t) r-&gt;fountains[(int)n], 1,
				sizeof(fountain), fd);
		}

		/* read the sinks */
		Fread((genericptr_t) &amp;r-&gt;nsink, 1, sizeof(r-&gt;nsink), fd);
		if ((n = r-&gt;nsink) != 0)
		    r-&gt;sinks = NewTab(sink, n);
		while (n--) {
			r-&gt;sinks[(int)n] = New(sink);
			Fread((genericptr_t) r-&gt;sinks[(int)n], 1, sizeof(sink), fd);
		}

		/* read the pools */
		Fread((genericptr_t) &amp;r-&gt;npool, 1, sizeof(r-&gt;npool), fd);
		if ((n = r-&gt;npool) != 0)
		    r-&gt;pools = NewTab(pool,n);
		while (n--) {
			r-&gt;pools[(int)n] = New(pool);
			Fread((genericptr_t) r-&gt;pools[(int)n], 1, sizeof(pool), fd);
		}

		/* read the traps */
		Fread((genericptr_t) &amp;r-&gt;ntrap, 1, sizeof(r-&gt;ntrap), fd);
		if ((n = r-&gt;ntrap) != 0)
		    r-&gt;traps = NewTab(trap, n);
		while(n--) {
			r-&gt;traps[(int)n] = New(trap);
			Fread((genericptr_t) r-&gt;traps[(int)n], 1, sizeof(trap), fd);
		}

		/* read the monsters */
		Fread((genericptr_t) &amp;r-&gt;nmonster, 1, sizeof(r-&gt;nmonster), fd);
		if ((n = r-&gt;nmonster) != 0) {
		    r-&gt;monsters = NewTab(monster, n);
		    while(n--) {
			r-&gt;monsters[(int)n] = New(monster);
			load_one_monster(fd, r-&gt;monsters[(int)n]);
		    }
		} else
		    r-&gt;monsters = 0;

		/* read the objects, in same order as mazes */
		Fread((genericptr_t) &amp;r-&gt;nobject, 1, sizeof(r-&gt;nobject), fd);
		if ((n = r-&gt;nobject) != 0) {
		    r-&gt;objects = NewTab(object, n);
		    for (j = 0; j &lt; n; ++j) {
			r-&gt;objects[j] = New(object);
			load_one_object(fd, r-&gt;objects[j]);
		    }
		} else
		    r-&gt;objects = 0;

		/* read the gold piles */
		Fread((genericptr_t) &amp;r-&gt;ngold, 1, sizeof(r-&gt;ngold), fd);
		if ((n = r-&gt;ngold) != 0)
		    r-&gt;golds = NewTab(gold, n);
		while (n--) {
			r-&gt;golds[(int)n] = New(gold);
			Fread((genericptr_t) r-&gt;golds[(int)n], 1, sizeof(gold), fd);
		}

		/* read the engravings */
		Fread((genericptr_t) &amp;r-&gt;nengraving, 1,
			sizeof(r-&gt;nengraving), fd);
		if ((n = r-&gt;nengraving) != 0) {
		    r-&gt;engravings = NewTab(engraving,n);
		    while (n--) {
			r-&gt;engravings[(int)n] = New(engraving);
			load_one_engraving(fd, r-&gt;engravings[(int)n]);
		    }
		} else
		    r-&gt;engravings = 0;

	}

	/* Now that we have loaded all the rooms, search the
	 * subrooms and create the links.
	 */

	for (i = 0; i<nrooms; (tmproom[i]-="" i++)="" if="">parent) {
		    /* Search the parent room */
		    for(j=0; j<nrooms; (tmproom[j]-="" if="" j++)="">name &amp;&amp; !strcmp(tmproom[j]-&gt;name,
						       tmproom[i]-&gt;parent)) {
				n = tmproom[j]-&gt;nsubroom++;
				tmproom[j]-&gt;subrooms[(int)n] = tmproom[i];
				break;
			}
	    }

	/*
	 * Create the rooms now...
	 */

	for (i=0; i &lt; nrooms; i++)
	    if(!tmproom[i]-&gt;parent)
		build_room(tmproom[i], (room *) 0);

	free_rooms(tmproom, nrooms);

	/* read the corridors */

	Fread((genericptr_t) &amp;ncorr, sizeof(ncorr), 1, fd);
	for (i=0; i<ncorr; !(y="" !is_ok_location((schar)x,="" %="" &tmpcor,="" 'touched'="" (!(x="" (--tryct="" (schar)y,="" *="" *m;="" -="" 0)="" 1,="" 2)="" 3);="" 3,="" <="" `humidity'="" a="" be="" break;="" but="" by="" called,="" chances="" change="" circumstances="" coord="" coordinate="" create_corridor(&tmpcor);="" do="" drastically="" due="" every="" fd);="" fread((genericptr_t)="" give="" humidity)="" humidity));="" humidity;="" i++)="" if="" in="" int="" is,="" it="" level.="" loader.="" m-="" map[x][y]="" maze="" maze.="" maze1xy="" maze1xy(m,="" might="" more="" normally="" not="" of="" or="" outside="" part="" place="" random="" register="" return="" screening="" select="" sizeof(tmpcor),="" so="" special="" static_ovl="" take="" ten="" than="" that="" the="" to="" tries="" true;="" tryct="2000;" tryct:="" under="" up="" void="" want="" we="" we'll="" which="" while="" won't="" x="rn1(x_maze_max" x,="" y="rn1(y_maze_max" y,="" {="" ||="" }="">x = (xchar)x,  m-&gt;y = (xchar)y;
}
load_maze
/*
* The Big Thing: special maze loader
*
* Could be cleaner, but it works.
*/

STATIC_OVL boolean
load_maze(fd)
dlb *fd;
{
xchar   x, y, typ;
boolean prefilled, room_not_needed;

char    n, numpart = 0;
xchar   nwalk = 0, nwalk_sav;
schar   filling;
char    halign, valign;

int     xi, dir, size;
coord   mm;
int     mapcount, mapcountmax, mapfact;

lev_region  tmplregion;
region  tmpregion;
door    tmpdoor;
trap    tmptrap;
monster tmpmons;
object  tmpobj;
drawbridge tmpdb;
walk    tmpwalk;
digpos  tmpdig;
lad     tmplad;
stair   tmpstair, prevstair;
altar   tmpaltar;
gold    tmpgold;
fountain tmpfountain;
engraving tmpengraving;
xchar   mustfill[(MAXNROFROOMS+1)*2];
struct trap *badtrap;
boolean has_bounds;

(void) memset((genericptr_t)⤅[0][0], 0, sizeof Map);
load_common_data(fd, SP_LEV_MAZE);

/* Initialize map */
Fread((genericptr_t) &amp;filling, 1, sizeof(filling), fd);
if (!init_lev.init_present) { /* don't init if mkmap() has been called */
for(x = 2; x &lt;= x_maze_max; x++)
	for(y = 0; y &lt;= y_maze_max; y++)
	    if (filling == -1) {
#ifndef WALLIFIED_MAZE
		    levl[x][y].typ = STONE;
#else
		    levl[x][y].typ =
			(y &lt; 2 || ((x % 2) &amp;&amp; (y % 2))) ? STONE : HWALL;
#endif
	    } else {
		    levl[x][y].typ = filling;
	    }
}

/* Start reading the file */
Fread((genericptr_t) &amp;numpart, 1, sizeof(numpart), fd);
						/* Number of parts */
if (!numpart || numpart &gt; 9)
	panic("load_maze error: numpart = %d", (int) numpart);

while (numpart--) {
	Fread((genericptr_t) &amp;halign, 1, sizeof(halign), fd);
					/* Horizontal alignment */
	Fread((genericptr_t) &amp;valign, 1, sizeof(valign), fd);
					/* Vertical alignment */
	Fread((genericptr_t) &amp;xsize, 1, sizeof(xsize), fd);
					/* size in X */
	Fread((genericptr_t) &amp;ysize, 1, sizeof(ysize), fd);
					/* size in Y */
	switch((int) halign) {
	    case LEFT:	    xstart = 3;					break;
	    case H_LEFT:    xstart = 2+((x_maze_max-2-xsize)/4);	break;
	    case CENTER:    xstart = 2+((x_maze_max-2-xsize)/2);	break;
	    case H_RIGHT:   xstart = 2+((x_maze_max-2-xsize)*3/4);	break;
	    case RIGHT:     xstart = x_maze_max-xsize-1;		break;
	}
	switch((int) valign) {
	    case TOP:	    ystart = 3;					break;
	    case CENTER:    ystart = 2+((y_maze_max-2-ysize)/2);	break;
	    case BOTTOM:    ystart = y_maze_max-ysize-1;		break;
	}
	if (!(xstart % 2)) xstart++;
	if (!(ystart % 2)) ystart++;
	if ((ystart &lt; 0) || (ystart + ysize &gt; ROWNO)) {
	    /* try to move the start a bit */
	    ystart += (ystart &gt; 0) ? -2 : 2;
	    if(ysize == ROWNO) ystart = 0;
	    if(ystart &lt; 0 || ystart + ysize &gt; ROWNO)
		panic("reading special level with ysize too large");
	}

	/*
	 * If any CROSSWALLs are found, must change to ROOM after REGION's
	 * are laid out.  CROSSWALLS are used to specify "invisible"
	 * boundaries where DOOR syms look bad or aren't desirable.
	 */
	has_bounds = FALSE;

	if(init_lev.init_present &amp;&amp; xsize &lt;= 1 &amp;&amp; ysize &lt;= 1) {
	    xstart = 1;
	    ystart = 0;
	    xsize = COLNO-1;
	    ysize = ROWNO;
	} else {
	    /* Load the map */
	    for(y = ystart; y &lt; ystart+ysize; y++)
		for(x = xstart; x &lt; xstart+xsize; x++) {
		    levl[x][y].typ = Fgetc(fd);
		    levl[x][y].lit = FALSE;
		    /* clear out levl: load_common_data may set them */
		    levl[x][y].flags = 0;
		    levl[x][y].horizontal = 0;
		    levl[x][y].roomno = 0;
		    levl[x][y].edge = 0;
		    /*
		     * Note: Even though levl[x][y].typ is type schar,
		     *	 lev_comp.y saves it as type char. Since schar != char
		     *	 all the time we must make this exception or hack
		     *	 through lev_comp.y to fix.
		     */

		    /*
		     *  Set secret doors to closed (why not trapped too?).  Set
		     *  the horizontal bit.
		     */
		    if (levl[x][y].typ == SDOOR || IS_DOOR(levl[x][y].typ)) {
			if(levl[x][y].typ == SDOOR)
			    levl[x][y].doormask = D_CLOSED;
			/*
			 *  If there is a wall to the left that connects to a
			 *  (secret) door, then it is horizontal.  This does
			 *  not allow (secret) doors to be corners of rooms.
			 */
			if (x != xstart &amp;&amp; (IS_WALL(levl[x-1][y].typ) ||
					    levl[x-1][y].horizontal))
			    levl[x][y].horizontal = 1;
		    } else if(levl[x][y].typ == HWALL ||
				levl[x][y].typ == IRONBARS)
			levl[x][y].horizontal = 1;
		    else if(levl[x][y].typ == LAVAPOOL)
			levl[x][y].lit = 1;
		    else if(levl[x][y].typ == CROSSWALL)
			has_bounds = TRUE;
		    Map[x][y] = 1;
		}
	    if (init_lev.init_present &amp;&amp; init_lev.joined)
		remove_rooms(xstart, ystart, xstart+xsize, ystart+ysize);
	}

	Fread((genericptr_t) &amp;n, 1, sizeof(n), fd);
						/* Number of level regions */
	if(n) {
	    if(num_lregions) {
		/* realloc the lregion space to add the new ones */
		/* don't really free it up until the whole level is done */
		lev_region *newl = (lev_region *) alloc(sizeof(lev_region) *
						(unsigned)(n+num_lregions));
		(void) memcpy((genericptr_t)(newl+n), (genericptr_t)lregions,
					sizeof(lev_region) * num_lregions);
		Free(lregions);
		num_lregions += n;
		lregions = newl;
	    } else {
		num_lregions = n;
		lregions = (lev_region *)
				alloc(sizeof(lev_region) * (unsigned)n);
	    }
	}

	while(n--) {
	    Fread((genericptr_t) &amp;tmplregion, sizeof(tmplregion), 1, fd);
	    if ((size = tmplregion.rname.len) != 0) {
		tmplregion.rname.str = (char *) alloc((unsigned)size + 1);
		Fread((genericptr_t) tmplregion.rname.str, size, 1, fd);
		tmplregion.rname.str[size] = '\0';
	    } else
		tmplregion.rname.str = (char *) 0;
	    if(!tmplregion.in_islev) {
		get_location(&amp;tmplregion.inarea.x1, &amp;tmplregion.inarea.y1,
								DRY|WET);
		get_location(&amp;tmplregion.inarea.x2, &amp;tmplregion.inarea.y2,
								DRY|WET);
	    }
	    if(!tmplregion.del_islev) {
		get_location(&amp;tmplregion.delarea.x1, &amp;tmplregion.delarea.y1,
								DRY|WET);
		get_location(&amp;tmplregion.delarea.x2, &amp;tmplregion.delarea.y2,
								DRY|WET);
	    }
	    lregions[(int)n] = tmplregion;
	}

	Fread((genericptr_t) &amp;n, 1, sizeof(n), fd);
						/* Random objects */
	if(n) {
		Fread((genericptr_t)robjects, sizeof(*robjects), (int) n, fd);
		sp_lev_shuffle(robjects, (char *)0, (int)n);
	}

	Fread((genericptr_t) &amp;n, 1, sizeof(n), fd);
						/* Random locations */
	if(n) {
		Fread((genericptr_t)rloc_x, sizeof(*rloc_x), (int) n, fd);
		Fread((genericptr_t)rloc_y, sizeof(*rloc_y), (int) n, fd);
		sp_lev_shuffle(rloc_x, rloc_y, (int)n);
	}

	Fread((genericptr_t) &amp;n, 1, sizeof(n), fd);
						/* Random monsters */
	if(n) {
		Fread((genericptr_t)rmonst, sizeof(*rmonst), (int) n, fd);
		sp_lev_shuffle(rmonst, (char *)0, (int)n);
	}

	(void) memset((genericptr_t)mustfill, 0, sizeof(mustfill));
	Fread((genericptr_t) &amp;n, 1, sizeof(n), fd);
						/* Number of subrooms */
	while(n--) {
		register struct mkroom *troom;

		Fread((genericptr_t)&amp;tmpregion, 1, sizeof(tmpregion), fd);

		if(tmpregion.rtype &gt; MAXRTYPE) {
		    tmpregion.rtype -= MAXRTYPE+1;
		    prefilled = TRUE;
		} else
		    prefilled = FALSE;

		if(tmpregion.rlit &lt; 0)
		    tmpregion.rlit = (rnd(1+abs(depth(&amp;u.uz))) &lt; 11 &amp;&amp; rn2(77))
			? TRUE : FALSE;

		get_location(&amp;tmpregion.x1, &amp;tmpregion.y1, DRY|WET);
		get_location(&amp;tmpregion.x2, &amp;tmpregion.y2, DRY|WET);

		/* for an ordinary room, `prefilled' is a flag to force
		   an actual room to be created (such rooms are used to
		   control placement of migrating monster arrivals) */
		room_not_needed = (tmpregion.rtype == OROOM &amp;&amp;
				   !tmpregion.rirreg &amp;&amp; !prefilled);
		if (room_not_needed || nroom &gt;= MAXNROFROOMS) {
		    if (!room_not_needed)
			impossible("Too many rooms on new level!");
		    light_region(&amp;tmpregion);
		    continue;
		}

		troom = &amp;rooms[nroom];

		/* mark rooms that must be filled, but do it later */
		if (tmpregion.rtype != OROOM)
		    mustfill[nroom] = (prefilled ? 2 : 1);

		if(tmpregion.rirreg) {
		    min_rx = max_rx = tmpregion.x1;
		    min_ry = max_ry = tmpregion.y1;
		    flood_fill_rm(tmpregion.x1, tmpregion.y1,
				  nroom+ROOMOFFSET, tmpregion.rlit, TRUE);
		    add_room(min_rx, min_ry, max_rx, max_ry,
			     FALSE, tmpregion.rtype, TRUE);
		    troom-&gt;rlit = tmpregion.rlit;
		    troom-&gt;irregular = TRUE;
		} else {
		    add_room(tmpregion.x1, tmpregion.y1,
			     tmpregion.x2, tmpregion.y2,
			     tmpregion.rlit, tmpregion.rtype, TRUE);
#ifdef SPECIALIZATION
		    topologize(troom,FALSE);		/* set roomno */
#else
		    topologize(troom);			/* set roomno */
#endif
		}
	}

	Fread((genericptr_t) &amp;n, 1, sizeof(n), fd);
						/* Number of doors */
	while(n--) {
		struct mkroom *croom = &amp;rooms[0];

		Fread((genericptr_t)&amp;tmpdoor, 1, sizeof(tmpdoor), fd);

		x = tmpdoor.x;	y = tmpdoor.y;
		typ = tmpdoor.mask == -1 ? rnddoor() : tmpdoor.mask;

		get_location(&amp;x, &amp;y, DRY);
		if(levl[x][y].typ != SDOOR)
			levl[x][y].typ = DOOR;
		else {
			if(typ &lt; D_CLOSED)
			    typ = D_CLOSED; /* force it to be closed */
		}
		levl[x][y].doormask = typ;

		/* Now the complicated part, list it with each subroom */
		/* The dog move and mail daemon routines use this */
		while(croom-&gt;hx &gt;= 0 &amp;&amp; doorindex &lt; DOORMAX) {
		    if(croom-&gt;hx &gt;= x-1 &amp;&amp; croom-&gt;lx &lt;= x+1 &amp;&amp;
		       croom-&gt;hy &gt;= y-1 &amp;&amp; croom-&gt;ly &lt;= y+1) {
			/* Found it */
			add_door(x, y, croom);
		    }
		    croom++;
		}
	}

	/* now that we have rooms _and_ associated doors, fill the rooms */
	for(n = 0; n &lt; SIZE(mustfill); n++)
	    if(mustfill[(int)n])
		fill_room(&amp;rooms[(int)n], (mustfill[(int)n] == 2));

	/* if special boundary syms (CROSSWALL) in map, remove them now */
	if(has_bounds) {
	    for(x = xstart; x &lt; xstart+xsize; x++)
		for(y = ystart; y &lt; ystart+ysize; y++)
		    if(levl[x][y].typ == CROSSWALL)
			levl[x][y].typ = ROOM;
	}

	Fread((genericptr_t) &amp;n, 1, sizeof(n), fd);
						/* Number of drawbridges */
	while(n--) {
		Fread((genericptr_t)&amp;tmpdb, 1, sizeof(tmpdb), fd);

		x = tmpdb.x;  y = tmpdb.y;
		get_location(&amp;x, &amp;y, DRY|WET);

		if (!create_drawbridge(x, y, tmpdb.dir, tmpdb.db_open))
		    impossible("Cannot create drawbridge.");
	}

	Fread((genericptr_t) &amp;n, 1, sizeof(n), fd);
						/* Number of mazewalks */
	while(n--) {
Read the starting point of the maze and the direction the maze initially extends from this point (as defined in the level description). Convert the starting point to map global coordinates. Store in walklist for later use below.

		Fread((genericptr_t)&amp;tmpwalk, 1, sizeof(tmpwalk), fd);

		get_location(&amp;tmpwalk.x, &amp;tmpwalk.y, DRY|WET);

		walklist[nwalk++] = tmpwalk;
	}

	Fread((genericptr_t) &amp;n, 1, sizeof(n), fd);
						/* Number of non_diggables */
	while(n--) {
		Fread((genericptr_t)&amp;tmpdig, 1, sizeof(tmpdig), fd);

		get_location(&amp;tmpdig.x1, &amp;tmpdig.y1, DRY|WET);
		get_location(&amp;tmpdig.x2, &amp;tmpdig.y2, DRY|WET);

		set_wall_property(tmpdig.x1, tmpdig.y1,
				  tmpdig.x2, tmpdig.y2, W_NONDIGGABLE);
	}

	Fread((genericptr_t) &amp;n, 1, sizeof(n), fd);
						/* Number of non_passables */
	while(n--) {
		Fread((genericptr_t)&amp;tmpdig, 1, sizeof(tmpdig), fd);

		get_location(&amp;tmpdig.x1, &amp;tmpdig.y1, DRY|WET);
		get_location(&amp;tmpdig.x2, &amp;tmpdig.y2, DRY|WET);

		set_wall_property(tmpdig.x1, tmpdig.y1,
				  tmpdig.x2, tmpdig.y2, W_NONPASSWALL);
	}

	Fread((genericptr_t) &amp;n, 1, sizeof(n), fd);
						/* Number of ladders */
	while(n--) {
		Fread((genericptr_t)&amp;tmplad, 1, sizeof(tmplad), fd);

		x = tmplad.x;  y = tmplad.y;
		get_location(&amp;x, &amp;y, DRY);

		levl[x][y].typ = LADDER;
		if (tmplad.up == 1) {
			xupladder = x;	yupladder = y;
			levl[x][y].ladder = LA_UP;
		} else {
			xdnladder = x;	ydnladder = y;
			levl[x][y].ladder = LA_DOWN;
		}
	}

	prevstair.x = prevstair.y = 0;
	Fread((genericptr_t) &amp;n, 1, sizeof(n), fd);
						/* Number of stairs */
	while(n--) {
		Fread((genericptr_t)&amp;tmpstair, 1, sizeof(tmpstair), fd);

		xi = 0;
		do {
		    x = tmpstair.x;  y = tmpstair.y;
		    get_location(&amp;x, &amp;y, DRY);
		} while(prevstair.x &amp;&amp; xi++ &lt; 100 &amp;&amp;
			distmin(x,y,prevstair.x,prevstair.y) &lt;= 8);
		if ((badtrap = t_at(x,y)) != 0) deltrap(badtrap);
		mkstairs(x, y, (char)tmpstair.up, (struct mkroom *)0);
		prevstair.x = x;
		prevstair.y = y;
	}

	Fread((genericptr_t) &amp;n, 1, sizeof(n), fd);
						/* Number of altars */
	while(n--) {
		Fread((genericptr_t)&amp;tmpaltar, 1, sizeof(tmpaltar), fd);

		create_altar(&amp;tmpaltar, (struct mkroom *)0);
	}

	Fread((genericptr_t) &amp;n, 1, sizeof(n), fd);
						/* Number of fountains */
	while (n--) {
		Fread((genericptr_t)&amp;tmpfountain, 1, sizeof(tmpfountain), fd);

		create_feature(tmpfountain.x, tmpfountain.y,
			       (struct mkroom *)0, FOUNTAIN);
	}

	Fread((genericptr_t) &amp;n, 1, sizeof(n), fd);
						/* Number of traps */
	while(n--) {
		Fread((genericptr_t)&amp;tmptrap, 1, sizeof(tmptrap), fd);

		create_trap(&amp;tmptrap, (struct mkroom *)0);
	}

	Fread((genericptr_t) &amp;n, 1, sizeof(n), fd);
						/* Number of monsters */
	while(n--) {
		load_one_monster(fd, &amp;tmpmons);

		create_monster(&amp;tmpmons, (struct mkroom *)0);
	}

	Fread((genericptr_t) &amp;n, 1, sizeof(n), fd);
						/* Number of objects */
	while(n--) {
		load_one_object(fd, &amp;tmpobj);

		create_object(&amp;tmpobj, (struct mkroom *)0);
	}

	Fread((genericptr_t) &amp;n, 1, sizeof(n), fd);
						/* Number of gold piles */
	while (n--) {
		Fread((genericptr_t)&amp;tmpgold, 1, sizeof(tmpgold), fd);

		create_gold(&amp;tmpgold, (struct mkroom *)0);
	}

	Fread((genericptr_t) &amp;n, 1, sizeof(n), fd);
						/* Number of engravings */
	while(n--) {
		load_one_engraving(fd, &amp;tmpengraving);

		create_engraving(&amp;tmpengraving, (struct mkroom *)0);
	}

}		/* numpart loop */
Generate each maze area specified in the level:

nwalk_sav = nwalk;
while(nwalk--) {
Get the maze starting point and direction previously defined:

	    x = (xchar) walklist[nwalk].x;
	    y = (xchar) walklist[nwalk].y;
	    dir = walklist[nwalk].dir;
Carve one square in the specified direction:

	    /* don't use move() - it doesn't use W_NORTH, etc. */
	    switch (dir) {
		case W_NORTH: --y; break;
		case W_SOUTH: y++; break;
		case W_EAST:  x++; break;
		case W_WEST:  --x; break;
		default: panic("load_maze: bad MAZEWALK direction");
	    }

	    if(!IS_DOOR(levl[x][y].typ)) {
#ifndef WALLIFIED_MAZE
		levl[x][y].typ = CORR;
#else
		levl[x][y].typ = ROOM;
#endif
		levl[x][y].flags = 0;
	    }

	    /*
	     * We must be sure that the parity of the coordinates for
	     * walkfrom() is odd.  But we must also take into account
	     * what direction was chosen.
	     */
	    if(!(x % 2)) {
		if (dir == W_EAST)
		    x++;
		else
		    x--;

		/* no need for IS_DOOR check; out of map bounds */
#ifndef WALLIFIED_MAZE
		levl[x][y].typ = CORR;
#else
		levl[x][y].typ = ROOM;
#endif
		levl[x][y].flags = 0;
	    }

	    if (!(y % 2)) {
		if (dir == W_SOUTH)
		    y++;
		else
		    y--;
	    }
Why do we not need to set levl[x][y].typ and .flags again after fixing the y coordinate?
Carve random maze extending outward from this starting point. 

	    walkfrom(x, y);
}
After generating all maze areas, re-wallify the entire map - should this be wrapped in "#ifdef WALLIFIED_MAZE"...?

wallification(1, 0, COLNO-1, ROWNO-1);

/*
* If there's a significant portion of maze unused by the special level,
* we don't want it empty.
*
* Makes the number of traps, monsters, etc. proportional
* to the size of the maze.
*/
mapcountmax = mapcount = (x_maze_max - 2) * (y_maze_max - 2);

for(x = 2; x &lt; x_maze_max; x++)
	for(y = 0; y &lt; y_maze_max; y++)
	    if(Map[x][y]) mapcount--;

if (nwalk_sav &amp;&amp; (mapcount &gt; (int) (mapcountmax / 10))) {
Populate the random maze sections of the map. See similar logic in mkmaze.c#line631

	    mapfact = (int) ((mapcount * 100L) / mapcountmax);
	    for(x = rnd((int) (20 * mapfact) / 100); x; x--) {
		    maze1xy(&amp;mm, DRY);
		    (void) mkobj_at(rn2(2) ? GEM_CLASS : RANDOM_CLASS,
							mm.x, mm.y, TRUE);
	    }
	    for(x = rnd((int) (12 * mapfact) / 100); x; x--) {
		    maze1xy(&amp;mm, DRY);
		    (void) mksobj_at(BOULDER, mm.x, mm.y, TRUE, FALSE);
	    }
	    for (x = rn2(2); x; x--) {
		maze1xy(&amp;mm, DRY);
		(void) makemon(&amp;mons[PM_MINOTAUR], mm.x, mm.y, NO_MM_FLAGS);
	    }
	    for(x = rnd((int) (12 * mapfact) / 100); x; x--) {
		    maze1xy(&amp;mm, WET|DRY);
		    (void) makemon((struct permonst *) 0, mm.x, mm.y, NO_MM_FLAGS);
	    }
	    for(x = rn2((int) (15 * mapfact) / 100); x; x--) {
		    maze1xy(&amp;mm, DRY);
		    (void) mkgold(0L,mm.x,mm.y);
	    }
	    for(x = rn2((int) (15 * mapfact) / 100); x; x--) {
		    int trytrap;

		    maze1xy(&amp;mm, DRY);
		    trytrap = rndtrap();
		    if (sobj_at(BOULDER, mm.x, mm.y))
			while (trytrap == PIT || trytrap == SPIKED_PIT ||
				trytrap == TRAPDOOR || trytrap == HOLE)
			    trytrap = rndtrap();
		    (void) maketrap(mm.x, mm.y, trytrap);
	    }
}
return TRUE;
}
load_special
/*
* General loader
*/

boolean
load_special(name)
const char *name;
{
	dlb *fd;
	boolean result = FALSE;
	char c;
	struct version_info vers_info;

	fd = dlb_fopen(name, RDBMODE);
	if (!fd) return FALSE;

	Fread((genericptr_t) &amp;vers_info, sizeof vers_info, 1, fd);
	if (!check_version(&amp;vers_info, name, TRUE))
	    goto give_up;

	Fread((genericptr_t) &amp;c, sizeof c, 1, fd); /* c Header */

	switch (c) {
		case SP_LEV_ROOMS:
		    result = load_rooms(fd);
		    break;
		case SP_LEV_MAZE:
		    result = load_maze(fd);
		    break;
		default:	/* ??? */
		    result = FALSE;
	}
give_up:
	(void)dlb_fclose(fd);
	return result;
}

/*sp_lev.c*/

</ncorr;></nrooms;></nrooms;></nrooms;i++)></r-></r-></r-></r-></r-></r-></r-></r-></r-></r->