Source:NetHack 3.4.3/src/sp lev.c

From NetHackWiki
(Redirected from Source:Ref/create altar)
Jump to: navigation, search

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.

Top of file

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

  1. /*
  2. * This file contains the various functions that are related to the special
  3. * levels.
  4. * It contains also the special level loader.
  5. *
  6. */
  7.  
  8. #include "hack.h"
  9. #include "dlb.h"
  10. /* #define DEBUG */	/* uncomment to enable code debugging */
  11.  
  12. #ifdef DEBUG
  13. # ifdef WIZARD
  14. #define debugpline	if (wizard) pline
  15. # else
  16. #define debugpline	pline
  17. # endif
  18. #endif
  19.  
  20. #include "sp_lev.h"
  21. #include "rect.h"
  22.  
  23. extern void FDECL(mkmap, (lev_init *));
  24.  
  25. STATIC_DCL void FDECL(get_room_loc, (schar *, schar *, struct mkroom *));
  26. STATIC_DCL void FDECL(get_free_room_loc, (schar *, schar *, struct mkroom *));
  27. STATIC_DCL void FDECL(create_trap, (trap *, struct mkroom *));
  28. STATIC_DCL int FDECL(noncoalignment, (ALIGNTYP_P));
  29. STATIC_DCL void FDECL(create_monster, (monster *, struct mkroom *));
  30. STATIC_DCL void FDECL(create_object, (object *, struct mkroom *));
  31. STATIC_DCL void FDECL(create_engraving, (engraving *,struct mkroom *));
  32. STATIC_DCL void FDECL(create_stairs, (stair *, struct mkroom *));
  33. STATIC_DCL void FDECL(create_altar, (altar *, struct mkroom *));
  34. STATIC_DCL void FDECL(create_gold, (gold *, struct mkroom *));
  35. STATIC_DCL void FDECL(create_feature, (int,int,struct mkroom *,int));
  36. STATIC_DCL boolean FDECL(search_door, (struct mkroom *, xchar *, xchar *,
  37. 					XCHAR_P, int));
  38. STATIC_DCL void NDECL(fix_stair_rooms);
  39. STATIC_DCL void FDECL(create_corridor, (corridor *));
  40.  
  41. STATIC_DCL boolean FDECL(create_subroom, (struct mkroom *, XCHAR_P, XCHAR_P,
  42. 					XCHAR_P, XCHAR_P, XCHAR_P, XCHAR_P));
  43.  
  44. #define LEFT	1
  45. #define H_LEFT	2
  46. #define CENTER	3
  47. #define H_RIGHT	4
  48. #define RIGHT	5
  49.  
  50. #define TOP	1
  51. #define BOTTOM	5
  52.  
  53. #define sq(x) ((x)*(x))
  54.  
  55. #define XLIM	4
  56. #define YLIM	3
  57.  
  58. #define Fread	(void)dlb_fread
  59. #define Fgetc	(schar)dlb_fgetc
  60. #define New(type)		(type *) alloc(sizeof(type))
  61. #define NewTab(type, size)	(type **) alloc(sizeof(type *) * (unsigned)size)
  62. #define Free(ptr)		if(ptr) free((genericptr_t) (ptr))
  63.  
  64. static NEARDATA walk walklist[50];
  65. extern int min_rx, max_rx, min_ry, max_ry; /* from mkmap.c */
  66.  
  67. static char Map[COLNO][ROWNO];
  68. static char robjects[10], rloc_x[10], rloc_y[10], rmonst[10];
  69. static aligntyp	ralign[3] = { AM_CHAOTIC, AM_NEUTRAL, AM_LAWFUL };
  70. static NEARDATA xchar xstart, ystart;
  71. static NEARDATA char xsize, ysize;
  72.  
  73. STATIC_DCL void FDECL(set_wall_property, (XCHAR_P,XCHAR_P,XCHAR_P,XCHAR_P,int));
  74. STATIC_DCL int NDECL(rnddoor);
  75. STATIC_DCL int NDECL(rndtrap);
  76. STATIC_DCL void FDECL(get_location, (schar *,schar *,int));
  77. STATIC_DCL void FDECL(sp_lev_shuffle, (char *,char *,int));
  78. STATIC_DCL void FDECL(light_region, (region *));
  79. STATIC_DCL void FDECL(load_common_data, (dlb *,int));
  80. STATIC_DCL void FDECL(load_one_monster, (dlb *,monster *));
  81. STATIC_DCL void FDECL(load_one_object, (dlb *,object *));
  82. STATIC_DCL void FDECL(load_one_engraving, (dlb *,engraving *));
  83. STATIC_DCL boolean FDECL(load_rooms, (dlb *));
  84. STATIC_DCL void FDECL(maze1xy, (coord *,int));
  85. STATIC_DCL boolean FDECL(load_maze, (dlb *));
  86. STATIC_DCL void FDECL(create_door, (room_door *, struct mkroom *));
  87. STATIC_DCL void FDECL(free_rooms,(room **, int));
  88. STATIC_DCL void FDECL(build_room, (room *, room*));
  89.  
  90. char *lev_message = 0;
  91. lev_region *lregions = 0;
  92. int num_lregions = 0;
  93. lev_init init_lev;

set_wall_property

  1. /*
  2. * Make walls of the area (x1, y1, x2, y2) non diggable/non passwall-able
  3. */
  4.  
  5. STATIC_OVL void
  6. set_wall_property(x1,y1,x2,y2, prop)
  7. xchar x1, y1, x2, y2;
  8. int prop;
  9. {
  10. 	register xchar x, y;
  11.  
  12. 	for(y = y1; y <= y2; y++)
  13. 	    for(x = x1; x <= x2; x++)
  14. 		if(IS_STWALL(levl[x][y].typ))
  15. 		    levl[x][y].wall_info |= prop;
  16. }

rnddoor

  1. /*
  2. * Choose randomly the state (nodoor, open, closed or locked) for a door
  3. */
  4. STATIC_OVL int
  5. rnddoor()
  6. {
  7. 	int i = 1 << rn2(5);
  8. 	i >>= 1;
  9. 	return i;
  10. }

rndtrap

  1. /*
  2. * Select a random trap
  3. */
  4. STATIC_OVL int
  5. rndtrap()
  6. {
  7. 	int rtrap;
  8.  
  9. 	do {
  10. 	    rtrap = rnd(TRAPNUM-1);
  11. 	    switch (rtrap) {
  12. 	     case HOLE:		/* no random holes on special levels */
  13. 	     case MAGIC_PORTAL:	rtrap = NO_TRAP;
  14. 				break;
  15. 	     case TRAPDOOR:	if (!Can_dig_down(&u.uz)) rtrap = NO_TRAP;
  16. 				break;
  17. 	     case LEVEL_TELEP:
  18. 	     case TELEP_TRAP:	if (level.flags.noteleport) rtrap = NO_TRAP;
  19. 				break;
  20. 	     case ROLLING_BOULDER_TRAP:
  21. 	     case ROCKTRAP:	if (In_endgame(&u.uz)) rtrap = NO_TRAP;
  22. 				break;
  23. 	    }
  24. 	} while (rtrap == NO_TRAP);
  25. 	return rtrap;
  26. }

get_location

  1. /*
  2. * Coordinates in special level files are handled specially:
  3. *
  4. *	if x or y is -11, we generate a random coordinate.
  5. *	if x or y is between -1 and -10, we read one from the corresponding
  6. *	register (x0, x1, ... x9).
  7. *	if x or y is nonnegative, we convert it from relative to the local map
  8. *	to global coordinates.
  9. *	The "humidity" flag is used to insure that engravings aren't
  10. *	created underwater, or eels on dry land.
  11. */
  12. #define DRY	0x1
  13. #define WET	0x2
  14.  
  15. STATIC_DCL boolean FDECL(is_ok_location, (SCHAR_P, SCHAR_P, int));
  16.  
  17. STATIC_OVL void
  18. get_location(x, y, humidity)
  19. schar *x, *y;
  20. int humidity;
  21. {
  22. 	int cpt = 0;
  23.  
  24. 	if (*x >= 0) {			/* normal locations */
  25. 		*x += xstart;
  26. 		*y += ystart;
  27. 	} else if (*x > -11) {		/* special locations */
  28. 		*y = ystart + rloc_y[ - *y - 1];
  29. 		*x = xstart + rloc_x[ - *x - 1];
  30. 	} else {			/* random location */
  31. 	    do {
  32. 		*x = xstart + rn2((int)xsize);
  33. 		*y = ystart + rn2((int)ysize);
  34. 		if (is_ok_location(*x,*y,humidity)) break;
  35. 	    } while (++cpt < 100);
  36. 	    if (cpt >= 100) {
  37. 		register int xx, yy;
  38. 		/* last try */
  39. 		for (xx = 0; xx < xsize; xx++)
  40. 		    for (yy = 0; yy < ysize; yy++) {
  41. 			*x = xstart + xx;
  42. 			*y = ystart + yy;
  43. 			if (is_ok_location(*x,*y,humidity)) goto found_it;
  44. 		    }
  45. 		panic("get_location:  can't find a place!");
  46. 	    }
  47. 	}
  48. found_it:;
  49.  
  50. 	if (!isok(*x,*y)) {
  51. 	    impossible("get_location:  (%d,%d) out of bounds", *x, *y);
  52. 	    *x = x_maze_max; *y = y_maze_max;
  53. 	}
  54. }

is_ok_location

  1. STATIC_OVL boolean
  2. is_ok_location(x, y, humidity)
  3. register schar x, y;
  4. register int humidity;
  5. {
  6. 	register int typ;
  7.  
  8. 	if (Is_waterlevel(&u.uz)) return TRUE;	/* accept any spot */
  9.  
  10. 	if (humidity & DRY) {
  11. 	    typ = levl[x][y].typ;
  12. 	    if (typ == ROOM || typ == AIR ||
  13. 		    typ == CLOUD || typ == ICE || typ == CORR)
  14. 		return TRUE;
  15. 	}
  16. 	if (humidity & WET) {
  17. 	    if (is_pool(x,y) || is_lava(x,y))
  18. 		return TRUE;
  19. 	}
  20. 	return FALSE;
  21. }

sp_lev_shuffle

  1. /*
  2. * Shuffle the registers for locations, objects or monsters
  3. */
  4.  
  5. STATIC_OVL void
  6. sp_lev_shuffle(list1, list2, n)
  7. char list1[], list2[];
  8. int n;
  9. {
  10. 	register int i, j;
  11. 	register char k;
  12.  
  13. 	for (i = n - 1; i > 0; i--) {
  14. 		if ((j = rn2(i + 1)) == i) continue;
  15. 		k = list1[j];
  16. 		list1[j] = list1[i];
  17. 		list1[i] = k;
  18. 		if (list2) {
  19. 			k = list2[j];
  20. 			list2[j] = list2[i];
  21. 			list2[i] = k;
  22. 		}
  23. 	}
  24. }

get_room_loc

  1. /*
  2. * Get a relative position inside a room.
  3. * negative values for x or y means RANDOM!
  4. */
  5.  
  6. STATIC_OVL void
  7. get_room_loc(x,y, croom)
  8. schar		*x, *y;
  9. struct mkroom	*croom;
  10. {
  11. 	coord c;
  12.  
  13. 	if (*x <0 && *y <0) {
  14. 		if (somexy(croom, &c)) {
  15. 			*x = c.x;
  16. 			*y = c.y;
  17. 		} else
  18. 		    panic("get_room_loc : can't find a place!");
  19. 	} else {
  20. 		if (*x < 0)
  21. 		    *x = rn2(croom->hx - croom->lx + 1);
  22. 		if (*y < 0)
  23. 		    *y = rn2(croom->hy - croom->ly + 1);
  24. 		*x += croom->lx;
  25. 		*y += croom->ly;
  26. 	}
  27. }

get_free_room_loc

  1. /*
  2. * Get a relative position inside a room.
  3. * negative values for x or y means RANDOM!
  4. */
  5.  
  6. STATIC_OVL void
  7. get_free_room_loc(x,y, croom)
  8. schar		*x, *y;
  9. struct mkroom	*croom;
  10. {
  11. 	schar try_x, try_y;
  12. 	register int trycnt = 0;
  13.  
  14. 	do {
  15. 	    try_x = *x,  try_y = *y;
  16. 	    get_room_loc(&try_x, &try_y, croom);
  17. 	} while (levl[try_x][try_y].typ != ROOM && ++trycnt <= 100);
  18.  
  19. 	if (trycnt > 100)
  20. 	    panic("get_free_room_loc:  can't find a place!");
  21. 	*x = try_x,  *y = try_y;
  22. }

check_room

  1. boolean
  2. check_room(lowx, ddx, lowy, ddy, vault)
  3. xchar *lowx, *ddx, *lowy, *ddy;
  4. boolean vault;
  5. {
  6. 	register int x,y,hix = *lowx + *ddx, hiy = *lowy + *ddy;
  7. 	register struct rm *lev;
  8. 	int xlim, ylim, ymax;
  9.  
  10. 	xlim = XLIM + (vault ? 1 : 0);
  11. 	ylim = YLIM + (vault ? 1 : 0);
  12.  
  13. 	if (*lowx < 3)		*lowx = 3;
  14. 	if (*lowy < 2)		*lowy = 2;
  15. 	if (hix > COLNO-3)	hix = COLNO-3;
  16. 	if (hiy > ROWNO-3)	hiy = ROWNO-3;
  17. chk:
  18. 	if (hix <= *lowx || hiy <= *lowy)	return FALSE;
  19.  
  20. 	/* check area around room (and make room smaller if necessary) */
  21. 	for (x = *lowx - xlim; x<= hix + xlim; x++) {
  22. 		if(x <= 0 || x >= COLNO) continue;
  23. 		y = *lowy - ylim;	ymax = hiy + ylim;
  24. 		if(y < 0) y = 0;
  25. 		if(ymax >= ROWNO) ymax = (ROWNO-1);
  26. 		lev = &levl[x][y];
  27. 		for (; y <= ymax; y++) {
  28. 			if (lev++->typ) {
  29. #ifdef DEBUG
  30. 				if(!vault)
  31. 				    debugpline("strange area [%d,%d] in check_room.",x,y);
  32. #endif
  33. 				if (!rn2(3))	return FALSE;
  34. 				if (x < *lowx)
  35. 				    *lowx = x + xlim + 1;
  36. 				else
  37. 				    hix = x - xlim - 1;
  38. 				if (y < *lowy)
  39. 				    *lowy = y + ylim + 1;
  40. 				else
  41. 				    hiy = y - ylim - 1;
  42. 				goto chk;
  43. 			}
  44. 		}
  45. 	}
  46. 	*ddx = hix - *lowx;
  47. 	*ddy = hiy - *lowy;
  48. 	return TRUE;
  49. }

create_room

  1. /*
  2. * Create a new room.
  3. * This is still very incomplete...
  4. */
  5.  
  6. boolean
  7. create_room(x,y,w,h,xal,yal,rtype,rlit)
  8. xchar	x,y;
  9. xchar	w,h;
  10. xchar	xal,yal;
  11. xchar	rtype, rlit;
  12. {
  13. 	xchar	xabs, yabs;
  14. 	int	wtmp, htmp, xaltmp, yaltmp, xtmp, ytmp;
  15. 	NhRect	*r1 = 0, r2;
  16. 	int	trycnt = 0;
  17. 	boolean	vault = FALSE;
  18. 	int	xlim = XLIM, ylim = YLIM;
  19.  
  20. 	if (rtype == -1)	/* Is the type random ? */
  21. 	    rtype = OROOM;
  22.  
  23. 	if (rtype == VAULT) {
  24. 		vault = TRUE;
  25. 		xlim++;
  26. 		ylim++;
  27. 	}
  28.  
  29. 	/* on low levels the room is lit (usually) */
  30. 	/* some other rooms may require lighting */
  31.  
  32. 	/* is light state random ? */
  33. 	if (rlit == -1)
  34. 	    rlit = (rnd(1+abs(depth(&u.uz))) < 11 && rn2(77)) ? TRUE : FALSE;
  35.  
  36. 	/*
  37. 	 * Here we will try to create a room. If some parameters are
  38. 	 * random we are willing to make several try before we give
  39. 	 * it up.
  40. 	 */
  41. 	do {
  42. 		xchar xborder, yborder;
  43. 		wtmp = w; htmp = h;
  44. 		xtmp = x; ytmp = y;
  45. 		xaltmp = xal; yaltmp = yal;
  46.  
  47. 		/* First case : a totaly random room */
  48.  
  49. 		if((xtmp < 0 && ytmp <0 && wtmp < 0 && xaltmp < 0 &&
  50. 		   yaltmp < 0) || vault) {
  51. 			xchar hx, hy, lx, ly, dx, dy;
  52. 			r1 = rnd_rect(); /* Get a random rectangle */
  53.  
  54. 			if (!r1) { /* No more free rectangles ! */
  55. #ifdef DEBUG
  56. 				debugpline("No more rects...");
  57. #endif
  58. 				return FALSE;
  59. 			}
  60. 			hx = r1->hx;
  61. 			hy = r1->hy;
  62. 			lx = r1->lx;
  63. 			ly = r1->ly;
  64. 			if (vault)
  65. 			    dx = dy = 1;
  66. 			else {
  67. 				dx = 2 + rn2((hx-lx > 28) ? 12 : 8);
  68. 				dy = 2 + rn2(4);
  69. 				if(dx*dy > 50)
  70. 				    dy = 50/dx;
  71. 			}
  72. 			xborder = (lx > 0 && hx < COLNO -1) ? 2*xlim : xlim+1;
  73. 			yborder = (ly > 0 && hy < ROWNO -1) ? 2*ylim : ylim+1;
  74. 			if(hx-lx < dx + 3 + xborder ||
  75. 			   hy-ly < dy + 3 + yborder) {
  76. 				r1 = 0;
  77. 				continue;
  78. 			}
  79. 			xabs = lx + (lx > 0 ? xlim : 3)
  80. 			    + rn2(hx - (lx>0?lx : 3) - dx - xborder + 1);
  81. 			yabs = ly + (ly > 0 ? ylim : 2)
  82. 			    + rn2(hy - (ly>0?ly : 2) - dy - yborder + 1);
  83. 			if (ly == 0 && hy >= (ROWNO-1) &&
  84. 			    (!nroom || !rn2(nroom)) && (yabs+dy > ROWNO/2)) {
  85. 			    yabs = rn1(3, 2);
  86. 			    if(nroom < 4 && dy>1) dy--;
  87. 		        }
  88. 			if (!check_room(&xabs, &dx, &yabs, &dy, vault)) {
  89. 				r1 = 0;
  90. 				continue;
  91. 			}
  92. 			wtmp = dx+1;
  93. 			htmp = dy+1;
  94. 			r2.lx = xabs-1; r2.ly = yabs-1;
  95. 			r2.hx = xabs + wtmp;
  96. 			r2.hy = yabs + htmp;
  97. 		} else {	/* Only some parameters are random */
  98. 			int rndpos = 0;
  99. 			if (xtmp < 0 && ytmp < 0) { /* Position is RANDOM */
  100. 				xtmp = rnd(5);
  101. 				ytmp = rnd(5);
  102. 				rndpos = 1;
  103. 			}
  104. 			if (wtmp < 0 || htmp < 0) { /* Size is RANDOM */
  105. 				wtmp = rn1(15, 3);
  106. 				htmp = rn1(8, 2);
  107. 			}
  108. 			if (xaltmp == -1) /* Horizontal alignment is RANDOM */
  109. 			    xaltmp = rnd(3);
  110. 			if (yaltmp == -1) /* Vertical alignment is RANDOM */
  111. 			    yaltmp = rnd(3);
  112.  
  113. 			/* Try to generate real (absolute) coordinates here! */
  114.  
  115. 			xabs = (((xtmp-1) * COLNO) / 5) + 1;
  116. 			yabs = (((ytmp-1) * ROWNO) / 5) + 1;
  117. 			switch (xaltmp) {
  118. 			      case LEFT:
  119. 				break;
  120. 			      case RIGHT:
  121. 				xabs += (COLNO / 5) - wtmp;
  122. 				break;
  123. 			      case CENTER:
  124. 				xabs += ((COLNO / 5) - wtmp) / 2;
  125. 				break;
  126. 			}
  127. 			switch (yaltmp) {
  128. 			      case TOP:
  129. 				break;
  130. 			      case BOTTOM:
  131. 				yabs += (ROWNO / 5) - htmp;
  132. 				break;
  133. 			      case CENTER:
  134. 				yabs += ((ROWNO / 5) - htmp) / 2;
  135. 				break;
  136. 			}
  137.  
  138. 			if (xabs + wtmp - 1 > COLNO - 2)
  139. 			    xabs = COLNO - wtmp - 3;
  140. 			if (xabs < 2)
  141. 			    xabs = 2;
  142. 			if (yabs + htmp - 1> ROWNO - 2)
  143. 			    yabs = ROWNO - htmp - 3;
  144. 			if (yabs < 2)
  145. 			    yabs = 2;
  146.  
  147. 			/* Try to find a rectangle that fit our room ! */
  148.  
  149. 			r2.lx = xabs-1; r2.ly = yabs-1;
  150. 			r2.hx = xabs + wtmp + rndpos;
  151. 			r2.hy = yabs + htmp + rndpos;
  152. 			r1 = get_rect(&r2);
  153. 		}
  154. 	} while (++trycnt <= 100 && !r1);
  155. 	if (!r1) {	/* creation of room failed ? */
  156. 		return FALSE;
  157. 	}
  158. 	split_rects(r1, &r2);
  159.  
  160. 	if (!vault) {
  161. 		smeq[nroom] = nroom;
  162. 		add_room(xabs, yabs, xabs+wtmp-1, yabs+htmp-1,
  163. 			 rlit, rtype, FALSE);
  164. 	} else {
  165. 		rooms[nroom].lx = xabs;
  166. 		rooms[nroom].ly = yabs;
  167. 	}
  168. 	return TRUE;
  169. }

create_subroom

  1. /*
  2. * Create a subroom in room proom at pos x,y with width w & height h.
  3. * x & y are relative to the parent room.
  4. */
  5.  
  6. STATIC_OVL boolean
  7. create_subroom(proom, x, y, w,  h, rtype, rlit)
  8. struct mkroom *proom;
  9. xchar x,y;
  10. xchar w,h;
  11. xchar rtype, rlit;
  12. {
  13. 	xchar width, height;
  14.  
  15. 	width = proom->hx - proom->lx + 1;
  16. 	height = proom->hy - proom->ly + 1;
  17.  
  18. 	/* There is a minimum size for the parent room */
  19. 	if (width < 4 || height < 4)
  20. 	    return FALSE;
  21.  
  22. 	/* Check for random position, size, etc... */
  23.  
  24. 	if (w == -1)
  25. 	    w = rnd(width - 3);
  26. 	if (h == -1)
  27. 	    h = rnd(height - 3);
  28. 	if (x == -1)
  29. 	    x = rnd(width - w - 1) - 1;
  30. 	if (y == -1)
  31. 	    y = rnd(height - h - 1) - 1;
  32. 	if (x == 1)
  33. 	    x = 0;
  34. 	if (y == 1)
  35. 	    y = 0;
  36. 	if ((x + w + 1) == width)
  37. 	    x++;
  38. 	if ((y + h + 1) == height)
  39. 	    y++;
  40. 	if (rtype == -1)
  41. 	    rtype = OROOM;
  42. 	if (rlit == -1)
  43. 	    rlit = (rnd(1+abs(depth(&u.uz))) < 11 && rn2(77)) ? TRUE : FALSE;
  44. 	add_subroom(proom, proom->lx + x, proom->ly + y,
  45. 		    proom->lx + x + w - 1, proom->ly + y + h - 1,
  46. 		    rlit, rtype, FALSE);
  47. 	return TRUE;
  48. }

create_door

  1. /*
  2. * Create a new door in a room.
  3. * It's placed on a wall (north, south, east or west).
  4. */
  5.  
  6. STATIC_OVL void
  7. create_door(dd, broom)
  8. room_door *dd;
  9. struct mkroom *broom;
  10. {
  11. 	int	x, y;
  12. 	int	trycnt = 0;
  13.  
  14. 	if (dd->secret == -1)
  15. 	    dd->secret = rn2(2);
  16.  
  17. 	if (dd->mask == -1) {
  18. 		/* is it a locked door, closed, or a doorway? */
  19. 		if (!dd->secret) {
  20. 			if(!rn2(3)) {
  21. 				if(!rn2(5))
  22. 				    dd->mask = D_ISOPEN;
  23. 				else if(!rn2(6))
  24. 				    dd->mask = D_LOCKED;
  25. 				else
  26. 				    dd->mask = D_CLOSED;
  27. 				if (dd->mask != D_ISOPEN && !rn2(25))
  28. 				    dd->mask |= D_TRAPPED;
  29. 			} else
  30. 			    dd->mask = D_NODOOR;
  31. 		} else {
  32. 			if(!rn2(5))	dd->mask = D_LOCKED;
  33. 			else		dd->mask = D_CLOSED;
  34.  
  35. 			if(!rn2(20)) dd->mask |= D_TRAPPED;
  36. 		}
  37. 	}
  38.  
  39. 	do {
  40. 		register int dwall, dpos;
  41.  
  42. 		dwall = dd->wall;
  43. 		if (dwall == -1)	/* The wall is RANDOM */
  44. 		    dwall = 1 << rn2(4);
  45.  
  46. 		dpos = dd->pos;
  47. 		if (dpos == -1)	/* The position is RANDOM */
  48. 		    dpos = rn2((dwall == W_WEST || dwall == W_EAST) ?
  49. 			    (broom->hy - broom->ly) : (broom->hx - broom->lx));
  50.  
  51. 		/* Convert wall and pos into an absolute coordinate! */
  52.  
  53. 		switch (dwall) {
  54. 		      case W_NORTH:
  55. 			y = broom->ly - 1;
  56. 			x = broom->lx + dpos;
  57. 			break;
  58. 		      case W_SOUTH:
  59. 			y = broom->hy + 1;
  60. 			x = broom->lx + dpos;
  61. 			break;
  62. 		      case W_WEST:
  63. 			x = broom->lx - 1;
  64. 			y = broom->ly + dpos;
  65. 			break;
  66. 		      case W_EAST:
  67. 			x = broom->hx + 1;
  68. 			y = broom->ly + dpos;
  69. 			break;
  70. 		      default:
  71. 			x = y = 0;
  72. 			panic("create_door: No wall for door!");
  73. 			break;
  74. 		}
  75. 		if (okdoor(x,y))
  76. 		    break;
  77. 	} while (++trycnt <= 100);
  78. 	if (trycnt > 100) {
  79. 		impossible("create_door: Can't find a proper place!");
  80. 		return;
  81. 	}
  82. 	add_door(x,y,broom);
  83. 	levl[x][y].typ = (dd->secret ? SDOOR : DOOR);
  84. 	levl[x][y].doormask = dd->mask;
  85. }

create_secret_door

  1. /*
  2. * Create a secret door in croom on any one of the specified walls.
  3. */
  4. void
  5. create_secret_door(croom, walls)
  6. struct mkroom *croom;
  7. xchar walls; /* any of W_NORTH | W_SOUTH | W_EAST | W_WEST (or W_ANY) */
  8. {
  9. xchar sx, sy; /* location of the secret door */
  10. int count;
  11.  
  12. for(count = 0; count < 100; count++) {
  13. 	sx = rn1(croom->hx - croom->lx + 1, croom->lx);
  14. 	sy = rn1(croom->hy - croom->ly + 1, croom->ly);
  15.  
  16. 	switch(rn2(4)) {
  17. 	case 0:  /* top */
  18. 	    if(!(walls & W_NORTH)) continue;
  19. 	    sy = croom->ly-1; break;
  20. 	case 1: /* bottom */
  21. 	    if(!(walls & W_SOUTH)) continue;
  22. 	    sy = croom->hy+1; break;
  23. 	case 2: /* left */
  24. 	    if(!(walls & W_EAST)) continue;
  25. 	    sx = croom->lx-1; break;
  26. 	case 3: /* right */
  27. 	    if(!(walls & W_WEST)) continue;
  28. 	    sx = croom->hx+1; break;
  29. 	}
  30.  
  31. 	if(okdoor(sx,sy)) {
  32. 	    levl[sx][sy].typ = SDOOR;
  33. 	    levl[sx][sy].doormask = D_CLOSED;
  34. 	    add_door(sx,sy,croom);
  35. 	    return;
  36. 	}
  37. }
  38.  
  39. impossible("couldn't create secret door on any walls 0x%x", walls);
  40. }

create_trap

  1. /*
  2. * Create a trap in a room.
  3. */
  4.  
  5. STATIC_OVL void
  6. create_trap(t,croom)
  7. trap	*t;
  8. struct mkroom	*croom;
  9. {
  10. schar	x,y;
  11. coord	tm;
  12.  
  13. if (rn2(100) < t->chance) {
  14. 	x = t->x;
  15. 	y = t->y;
  16. 	if (croom)
  17. 	    get_free_room_loc(&x, &y, croom);
  18. 	else
  19. 	    get_location(&x, &y, DRY);
  20.  
  21. 	tm.x = x;
  22. 	tm.y = y;
  23.  
  24. 	mktrap(t->type, 1, (struct mkroom*) 0, &tm);
  25. }
  26. }

noncoalignment

  1. /*
  2. * Create a monster in a room.
  3. */
  4.  
  5. STATIC_OVL int
  6. noncoalignment(alignment)
  7. aligntyp alignment;
  8. {
  9. 	int k;
  10.  
  11. 	k = rn2(2);
  12. 	if (!alignment)
  13. 		return(k ? -1 : 1);
  14. 	return(k ? -alignment : 0);
  15. }

create_monster

  1. STATIC_OVL void
  2. create_monster(m,croom)
  3. monster	*m;
  4. struct mkroom	*croom;
  5. {
  6. struct monst *mtmp;
  7. schar x, y;
  8. char class;
  9. aligntyp amask;
  10. coord cc;
  11. struct permonst *pm;
  12. unsigned g_mvflags;
  13.  
  14. if (rn2(100) < m->chance) {
  15.  
  16. 	if (m->class >= 0)
  17. 	    class = (char) def_char_to_monclass((char)m->class);
  18. 	else if (m->class > -11)
  19. 	    class = (char) def_char_to_monclass(rmonst[- m->class - 1]);
  20. 	else
  21. 	    class = 0;
  22.  
  23. 	if (class == MAXMCLASSES)
  24. 	    panic("create_monster: unknown monster class '%c'", m->class);
  25.  
  26. 	amask = (m->align == AM_SPLEV_CO) ?
  27. 			Align2amask(u.ualignbase[A_ORIGINAL]) :
  28. 		(m->align == AM_SPLEV_NONCO) ?
  29. 			Align2amask(noncoalignment(u.ualignbase[A_ORIGINAL])) :
  30. 		(m->align <= -11) ? induced_align(80) :
  31. 		(m->align < 0 ? ralign[-m->align-1] : m->align);
  32.  
  33. 	if (!class)
  34. 	    pm = (struct permonst *) 0;
  35. 	else if (m->id != NON_PM) {
  36. 	    pm = &mons[m->id];
  37. 	    g_mvflags = (unsigned) mvitals[monsndx(pm)].mvflags;
  38. 	    if ((pm->geno & G_UNIQ) && (g_mvflags & G_EXTINCT))
  39. 		goto m_done;
  40. 	    else if (g_mvflags & G_GONE)	/* genocided or extinct */
  41. 		pm = (struct permonst *) 0;	/* make random monster */
  42. 	} else {
  43. 	    pm = mkclass(class,G_NOGEN);
  44. 	    /* if we can't get a specific monster type (pm == 0) then the
  45. 	       class has been genocided, so settle for a random monster */
  46. 	}
  47. 	if (In_mines(&u.uz) && pm && your_race(pm) &&
  48. 			(Race_if(PM_DWARF) || Race_if(PM_GNOME)) && rn2(3))
  49. 	    pm = (struct permonst *) 0;
  50.  
  51. 	x = m->x;
  52. 	y = m->y;
  53. 	if (croom)
  54. 	    get_room_loc(&x, &y, croom);
  55. 	else {
  56. 	    if (!pm || !is_swimmer(pm))
  57. 		get_location(&x, &y, DRY);
  58. 	    else if (pm->mlet == S_EEL)
  59. 		get_location(&x, &y, WET);
  60. 	    else
  61. 		get_location(&x, &y, DRY|WET);
  62. 	}
  63. 	/* try to find a close place if someone else is already there */
  64. 	if (MON_AT(x,y) && enexto(&cc, x, y, pm))
  65. 	    x = cc.x,  y = cc.y;
  66.  
  67. 	if(m->align != -12)
  68. 	    mtmp = mk_roamer(pm, Amask2align(amask), x, y, m->peaceful);
  69. 	else if(PM_ARCHEOLOGIST <= m->id && m->id <= PM_WIZARD)
  70. 	         mtmp = mk_mplayer(pm, x, y, FALSE);
  71. 	else mtmp = makemon(pm, x, y, NO_MM_FLAGS);
  72.  
  73. 	if (mtmp) {
  74. 	    /* handle specific attributes for some special monsters */
  75. 	    if (m->name.str) mtmp = christen_monst(mtmp, m->name.str);
  76.  
  77. 	    /*
  78. 	     * This is currently hardwired for mimics only.  It should
  79. 	     * eventually be expanded.
  80. 	     */
  81. 	    if (m->appear_as.str && mtmp->data->mlet == S_MIMIC) {
  82. 		int i;
  83.  
  84. 		switch (m->appear) {
  85. 		    case M_AP_NOTHING:
  86. 			impossible(
  87. 		"create_monster: mon has an appearance, \"%s\", but no type",
  88. 				m->appear_as.str);
  89. 			break;
  90.  
  91. 		    case M_AP_FURNITURE:
  92. 			for (i = 0; i < MAXPCHARS; i++)
  93. 			    if (!strcmp(defsyms[i].explanation,
  94. 					m->appear_as.str))
  95. 				break;
  96. 			if (i == MAXPCHARS) {
  97. 			    impossible(
  98. 				"create_monster: can't find feature \"%s\"",
  99. 				m->appear_as.str);
  100. 			} else {
  101. 			    mtmp->m_ap_type = M_AP_FURNITURE;
  102. 			    mtmp->mappearance = i;
  103. 			}
  104. 			break;
  105.  
  106. 		    case M_AP_OBJECT:
  107. 			for (i = 0; i < NUM_OBJECTS; i++)
  108. 			    if (OBJ_NAME(objects[i]) &&
  109. 				!strcmp(OBJ_NAME(objects[i]),m->appear_as.str))
  110. 				break;
  111. 			if (i == NUM_OBJECTS) {
  112. 			    impossible(
  113. 				"create_monster: can't find object \"%s\"",
  114. 				m->appear_as.str);
  115. 			} else {
  116. 			    mtmp->m_ap_type = M_AP_OBJECT;
  117. 			    mtmp->mappearance = i;
  118. 			}
  119. 			break;
  120.  
  121. 		    case M_AP_MONSTER:
  122. 			/* note: mimics don't appear as monsters! */
  123. 			/*	 (but chameleons can :-)	  */
  124. 		    default:
  125. 			impossible(
  126. 		"create_monster: unimplemented mon appear type [%d,\"%s\"]",
  127. 				m->appear, m->appear_as.str);
  128. 			break;
  129. 		}
  130. 		if (does_block(x, y, &levl[x][y]))
  131. 		    block_point(x, y);
  132. 	    }
  133.  
  134. 	    if (m->peaceful >= 0) {
  135. 		mtmp->mpeaceful = m->peaceful;
  136. 		/* changed mpeaceful again; have to reset malign */
  137. 		set_malign(mtmp);
  138. 	    }
  139. 	    if (m->asleep >= 0) {
  140. #ifdef UNIXPC
  141. 		/* optimizer bug strikes again */
  142. 		if (m->asleep)
  143. 			mtmp->msleeping = 1;
  144. 		else
  145. 			mtmp->msleeping = 0;
  146. #else
  147. 		mtmp->msleeping = m->asleep;
  148. #endif
  149. 	    }
  150. 	}
  151.  
  152. }		/* if (rn2(100) < m->chance) */
  153. m_done:
  154. Free(m->name.str);
  155. Free(m->appear_as.str);
  156. }

create_object

  1. /*
  2. * Create an object in a room.
  3. */
  4.  
  5. STATIC_OVL void
  6. create_object(o,croom)
  7. object	*o;
  8. struct mkroom	*croom;
  9. {
  10. struct obj *otmp;
  11. schar x, y;
  12. char c;
  13. boolean named;	/* has a name been supplied in level description? */
  14.  
  15. if (rn2(100) < o->chance) {
  16. 	named = o->name.str ? TRUE : FALSE;
  17.  
  18. 	x = o->x; y = o->y;
  19. 	if (croom)
  20. 	    get_room_loc(&x, &y, croom);
  21. 	else
  22. 	    get_location(&x, &y, DRY);
  23.  
  24. 	if (o->class >= 0)
  25. 	    c = o->class;
  26. 	else if (o->class > -11)
  27. 	    c = robjects[ -(o->class+1)];
  28. 	else
  29. 	    c = 0;
  30.  
  31. 	if (!c)
  32. 	    otmp = mkobj_at(RANDOM_CLASS, x, y, !named);
  33. 	else if (o->id != -1)
  34. 	    otmp = mksobj_at(o->id, x, y, TRUE, !named);
  35. 	else {
  36. 	    /*
  37. 	     * The special levels are compiled with the default "text" object
  38. 	     * class characters.  We must convert them to the internal format.
  39. 	     */
  40. 	    char oclass = (char) def_char_to_objclass(c);
  41.  
  42. 	    if (oclass == MAXOCLASSES)
  43. 		panic("create_object:  unexpected object class '%c'",c);
  44.  
  45. 	    /* KMH -- Create piles of gold properly */
  46. 	    if (oclass == COIN_CLASS)
  47. 		otmp = mkgold(0L, x, y);
  48. 	    else
  49. 		otmp = mkobj_at(oclass, x, y, !named);
  50. 	}
  51.  
  52. 	if (o->spe != -127)	/* That means NOT RANDOM! */
  53. 	    otmp->spe = (schar)o->spe;
  54.  
  55. 	switch (o->curse_state) {
  56. 	      case 1:	bless(otmp); break; /* BLESSED */
  57. 	      case 2:	unbless(otmp); uncurse(otmp); break; /* uncursed */
  58. 	      case 3:	curse(otmp); break; /* CURSED */
  59. 	      default:	break;	/* Otherwise it's random and we're happy
  60. 				 * with what mkobj gave us! */
  61. 	}
  62.  
  63. 	/*	corpsenm is "empty" if -1, random if -2, otherwise specific */
  64. 	if (o->corpsenm == NON_PM - 1) otmp->corpsenm = rndmonnum();
  65. 	else if (o->corpsenm != NON_PM) otmp->corpsenm = o->corpsenm;
  66.  
  67. 	/* assume we wouldn't be given an egg corpsenm unless it was
  68. 	   hatchable */
  69. 	if (otmp->otyp == EGG && otmp->corpsenm != NON_PM) {
  70. 	    if (dead_species(otmp->otyp, TRUE))
  71. 		kill_egg(otmp);	/* make sure nothing hatches */
  72. 	    else
  73. 		attach_egg_hatch_timeout(otmp);	/* attach new hatch timeout */
  74. 	}
  75.  
  76. 	if (named)
  77. 	    otmp = oname(otmp, o->name.str);
  78.  
  79. 	switch(o->containment) {
  80. 	    static struct obj *container = 0;
  81.  
  82. 	    /* contents */
  83. 	    case 1:
  84. 		if (!container) {
  85. 		    impossible("create_object: no container");
  86. 		    break;
  87. 		}
  88. 		remove_object(otmp);
  89. 		(void) add_to_container(container, otmp);
  90. 		goto o_done;		/* don't stack, but do other cleanup */
  91. 	    /* container */
  92. 	    case 2:
  93. 		delete_contents(otmp);
  94. 		container = otmp;
  95. 		break;
  96. 	    /* nothing */
  97. 	    case 0: break;
  98.  
  99. 	    default: impossible("containment type %d?", (int) o->containment);
  100. 	}
  101.  
  102. 	/* Medusa level special case: statues are petrified monsters, so they
  103. 	 * are not stone-resistant and have monster inventory.  They also lack
  104. 	 * other contents, but that can be specified as an empty container.
  105. 	 */
  106. 	if (o->id == STATUE && Is_medusa_level(&u.uz) &&
  107. 		    o->corpsenm == NON_PM) {
  108. 	    struct monst *was;
  109. 	    struct obj *obj;
  110. 	    int wastyp;
  111.  
  112. 	    /* Named random statues are of player types, and aren't stone-
  113. 	     * resistant (if they were, we'd have to reset the name as well as
  114. 	     * setting corpsenm).
  115. 	     */
  116. 	    for (wastyp = otmp->corpsenm; ; wastyp = rndmonnum()) {
  117. 		/* makemon without rndmonst() might create a group */
  118. 		was = makemon(&mons[wastyp], 0, 0, NO_MM_FLAGS);
  119. 		if (!resists_ston(was)) break;
  120. 		mongone(was);
  121. 	    }
  122. 	    otmp->corpsenm = wastyp;
  123. 	    while(was->minvent) {
  124. 		obj = was->minvent;
  125. 		obj->owornmask = 0;
  126. 		obj_extract_self(obj);
  127. 		(void) add_to_container(otmp, obj);
  128. 	    }
  129. 	    otmp->owt = weight(otmp);
  130. 	    mongone(was);
  131. 	}
  132.  
  133. 	stackobj(otmp);
  134.  
  135. }		/* if (rn2(100) < o->chance) */
  136. o_done:
  137. Free(o->name.str);
  138. }

create_engraving

  1. /*
  2. * Randomly place a specific engraving, then release its memory.
  3. */
  4. STATIC_OVL void
  5. create_engraving(e, croom)
  6. engraving *e;
  7. struct mkroom *croom;
  8. {
  9. 	xchar x, y;
  10.  
  11. 	x = e->x,  y = e->y;
  12. 	if (croom)
  13. 	    get_room_loc(&x, &y, croom);
  14. 	else
  15. 	    get_location(&x, &y, DRY);
  16.  
  17. 	make_engr_at(x, y, e->engr.str, 0L, e->etype);
  18. 	free((genericptr_t) e->engr.str);
  19. }

create_stairs

  1. /*
  2. * Create stairs in a room.
  3. *
  4. */
  5.  
  6. STATIC_OVL void
  7. create_stairs(s,croom)
  8. stair	*s;
  9. struct mkroom	*croom;
  10. {
  11. 	schar		x,y;
  12.  
  13. 	x = s->x; y = s->y;
  14. 	get_free_room_loc(&x, &y, croom);
  15. 	mkstairs(x,y,(char)s->up, croom);
  16. }

create_altar

  1. /*
  2. * Create an altar in a room.
  3. */
  4.  
  5. STATIC_OVL void
  6. create_altar(a, croom)
  7. 	altar		*a;
  8. 	struct mkroom	*croom;
  9. {
  10. 	schar		sproom,x,y;
  11. 	aligntyp	amask;
  12. 	boolean		croom_is_temple = TRUE;
  13. 	int oldtyp; 
  14.  
  15. 	x = a->x; y = a->y;
  16.  
  17. 	if (croom) {
  18. 	    get_free_room_loc(&x, &y, croom);
  19. 	    if (croom->rtype != TEMPLE)
  20. 		croom_is_temple = FALSE;
  21. 	} else {
  22. 	    get_location(&x, &y, DRY);
  23. 	    if ((sproom = (schar) *in_rooms(x, y, TEMPLE)) != 0)
  24. 		croom = &rooms[sproom - ROOMOFFSET];
  25. 	    else
  26. 		croom_is_temple = FALSE;
  27. 	}
  28.  
  29. 	/* check for existing features */
  30. 	oldtyp = levl[x][y].typ;
  31. 	if (oldtyp == STAIRS || oldtyp == LADDER)
  32. 	    return;
  33.  
  34. 	a->x = x;
  35. 	a->y = y;
  36.  
  37. 	/* Is the alignment random ?
  38. 	 * If so, it's an 80% chance that the altar will be co-aligned.
  39. 	 *
  40. 	 * The alignment is encoded as amask values instead of alignment
  41. 	 * values to avoid conflicting with the rest of the encoding,
  42. 	 * shared by many other parts of the special level code.
  43. 	 */
  44.  
  45. 	amask = (a->align == AM_SPLEV_CO) ?
  46. 			Align2amask(u.ualignbase[A_ORIGINAL]) :
  47. 		(a->align == AM_SPLEV_NONCO) ?
  48. 			Align2amask(noncoalignment(u.ualignbase[A_ORIGINAL])) :
  49. 		(a->align == -11) ? induced_align(80) :
  50. 		(a->align < 0 ? ralign[-a->align-1] : a->align);
  51.  
  52. 	levl[x][y].typ = ALTAR;
  53. 	levl[x][y].altarmask = amask;
  54.  
  55. 	if (a->shrine < 0) a->shrine = rn2(2);	/* handle random case */
  56.  
  57. 	if (oldtyp == FOUNTAIN)
  58. 	    level.flags.nfountains--;
  59. 	else if (oldtyp == SINK)
  60. 	    level.flags.nsinks--;
  61.  
  62. 	if (!croom_is_temple || !a->shrine) return;
  63.  
  64. 	if (a->shrine) {	/* Is it a shrine  or sanctum? */
  65. 	    priestini(&u.uz, croom, x, y, (a->shrine > 1));
  66. 	    levl[x][y].altarmask |= AM_SHRINE;
  67. 	    level.flags.has_temple = TRUE;
  68. 	}
  69. }

create_gold

  1. /*
  2. * Create a gold pile in a room.
  3. */
  4.  
  5. STATIC_OVL void
  6. create_gold(g,croom)
  7. gold *g;
  8. struct mkroom	*croom;
  9. {
  10. 	schar		x,y;
  11.  
  12. 	x = g->x; y= g->y;
  13. 	if (croom)
  14. 	    get_room_loc(&x, &y, croom);
  15. 	else
  16. 	    get_location(&x, &y, DRY);
  17.  
  18. 	if (g->amount == -1)
  19. 	    g->amount = rnd(200);
  20. 	(void) mkgold((long) g->amount, x, y);
  21. }

create_feature

  1. /*
  2. * Create a feature (e.g a fountain) in a room.
  3. */
  4.  
  5. STATIC_OVL void
  6. create_feature(fx, fy, croom, typ)
  7. int		fx, fy;
  8. struct mkroom	*croom;
  9. int		typ;
  10. {
  11. 	schar		x,y;
  12. 	int		trycnt = 0;
  13.  
  14. 	x = fx;  y = fy;
  15. 	if (croom) {
  16. 	    if (x < 0 && y < 0)
  17. 		do {
  18. 		    x = -1;  y = -1;
  19. 		    get_room_loc(&x, &y, croom);
  20. 		} while (++trycnt <= 200 && occupied(x,y));
  21. 	    else
  22. 		get_room_loc(&x, &y, croom);
  23. 	    if(trycnt > 200)
  24. 		return;
  25. 	} else {
  26. 	    get_location(&x, &y, DRY);
  27. 	}
  28. 	/* Don't cover up an existing feature (particularly randomly
  29. 	   placed stairs).  However, if the _same_ feature is already
  30. 	   here, it came from the map drawing and we still need to
  31. 	   update the special counters. */
  32. 	if (IS_FURNITURE(levl[x][y].typ) && levl[x][y].typ != typ)
  33. 	    return;
  34.  
  35. 	levl[x][y].typ = typ;
  36. 	if (typ == FOUNTAIN)
  37. 	    level.flags.nfountains++;
  38. 	else if (typ == SINK)
  39. 	    level.flags.nsinks++;
  40. }

search_door

  1. /*
  2. * Search for a door in a room on a specified wall.
  3. */
  4.  
  5. STATIC_OVL boolean
  6. search_door(croom,x,y,wall,cnt)
  7. struct mkroom *croom;
  8. xchar *x, *y;
  9. xchar wall;
  10. int cnt;
  11. {
  12. 	int dx, dy;
  13. 	int xx,yy;
  14.  
  15. 	switch(wall) {
  16. 	      case W_NORTH:
  17. 		dy = 0; dx = 1;
  18. 		xx = croom->lx;
  19. 		yy = croom->hy + 1;
  20. 		break;
  21. 	      case W_SOUTH:
  22. 		dy = 0; dx = 1;
  23. 		xx = croom->lx;
  24. 		yy = croom->ly - 1;
  25. 		break;
  26. 	      case W_EAST:
  27. 		dy = 1; dx = 0;
  28. 		xx = croom->hx + 1;
  29. 		yy = croom->ly;
  30. 		break;
  31. 	      case W_WEST:
  32. 		dy = 1; dx = 0;
  33. 		xx = croom->lx - 1;
  34. 		yy = croom->ly;
  35. 		break;
  36. 	      default:
  37. 		dx = dy = xx = yy = 0;
  38. 		panic("search_door: Bad wall!");
  39. 		break;
  40. 	}
  41. 	while (xx <= croom->hx+1 && yy <= croom->hy+1) {
  42. 		if (IS_DOOR(levl[xx][yy].typ) || levl[xx][yy].typ == SDOOR) {
  43. 			*x = xx;
  44. 			*y = yy;
  45. 			if (cnt-- <= 0)
  46. 			    return TRUE;
  47. 		}
  48. 		xx += dx;
  49. 		yy += dy;
  50. 	}
  51. 	return FALSE;
  52. }

dig_corridor

  1. /*
  2. * Dig a corridor between two points.
  3. */
  4.  
  5. boolean
  6. dig_corridor(org,dest,nxcor,ftyp,btyp)
  7. coord *org, *dest;
  8. boolean nxcor;
  9. schar ftyp, btyp;
  10. {
  11. 	register int dx=0, dy=0, dix, diy, cct;
  12. 	register struct rm *crm;
  13. 	register int tx, ty, xx, yy;
  14.  
  15. 	xx = org->x;  yy = org->y;
  16. 	tx = dest->x; ty = dest->y;
  17. 	if (xx <= 0 || yy <= 0 || tx <= 0 || ty <= 0 ||
  18. 	    xx > COLNO-1 || tx > COLNO-1 ||
  19. 	    yy > ROWNO-1 || ty > ROWNO-1) {
  20. #ifdef DEBUG
  21. 		debugpline("dig_corridor: bad coords : (%d,%d) (%d,%d).",
  22. 			   xx,yy,tx,ty);
  23. #endif
  24. 		return FALSE;
  25. 	}
  26. 	if (tx > xx)		dx = 1;
  27. 	else if (ty > yy)	dy = 1;
  28. 	else if (tx < xx)	dx = -1;
  29. 	else			dy = -1;
  30.  
  31. 	xx -= dx;
  32. 	yy -= dy;
  33. 	cct = 0;
  34. 	while(xx != tx || yy != ty) {
  35. 	    /* loop: dig corridor at [xx,yy] and find new [xx,yy] */
  36. 	    if(cct++ > 500 || (nxcor && !rn2(35)))
  37. 		return FALSE;
  38.  
  39. 	    xx += dx;
  40. 	    yy += dy;
  41.  
  42. 	    if(xx >= COLNO-1 || xx <= 0 || yy <= 0 || yy >= ROWNO-1)
  43. 		return FALSE;		/* impossible */
  44.  
  45. 	    crm = &levl[xx][yy];
  46. 	    if(crm->typ == btyp) {
  47. 		if(ftyp != CORR || rn2(100)) {
  48. 			crm->typ = ftyp;
  49. 			if(nxcor && !rn2(50))
  50. 				(void) mksobj_at(BOULDER, xx, yy, TRUE, FALSE);
  51. 		} else {
  52. 			crm->typ = SCORR;
  53. 		}
  54. 	    } else
  55. 	    if(crm->typ != ftyp && crm->typ != SCORR) {
  56. 		/* strange ... */
  57. 		return FALSE;
  58. 	    }
  59.  
  60. 	    /* find next corridor position */
  61. 	    dix = abs(xx-tx);
  62. 	    diy = abs(yy-ty);
  63.  
  64. 	    /* do we have to change direction ? */
  65. 	    if(dy && dix > diy) {
  66. 		register int ddx = (xx > tx) ? -1 : 1;
  67.  
  68. 		crm = &levl[xx+ddx][yy];
  69. 		if(crm->typ == btyp || crm->typ == ftyp || crm->typ == SCORR) {
  70. 		    dx = ddx;
  71. 		    dy = 0;
  72. 		    continue;
  73. 		}
  74. 	    } else if(dx && diy > dix) {
  75. 		register int ddy = (yy > ty) ? -1 : 1;
  76.  
  77. 		crm = &levl[xx][yy+ddy];
  78. 		if(crm->typ == btyp || crm->typ == ftyp || crm->typ == SCORR) {
  79. 		    dy = ddy;
  80. 		    dx = 0;
  81. 		    continue;
  82. 		}
  83. 	    }
  84.  
  85. 	    /* continue straight on? */
  86. 	    crm = &levl[xx+dx][yy+dy];
  87. 	    if(crm->typ == btyp || crm->typ == ftyp || crm->typ == SCORR)
  88. 		continue;
  89.  
  90. 	    /* no, what must we do now?? */
  91. 	    if(dx) {
  92. 		dx = 0;
  93. 		dy = (ty < yy) ? -1 : 1;
  94. 	    } else {
  95. 		dy = 0;
  96. 		dx = (tx < xx) ? -1 : 1;
  97. 	    }
  98. 	    crm = &levl[xx+dx][yy+dy];
  99. 	    if(crm->typ == btyp || crm->typ == ftyp || crm->typ == SCORR)
  100. 		continue;
  101. 	    dy = -dy;
  102. 	    dx = -dx;
  103. 	}
  104. 	return TRUE;
  105. }

fix_stair_rooms

  1. /*
  2. * Disgusting hack: since special levels have their rooms filled before
  3. * sorting the rooms, we have to re-arrange the speed values upstairs_room
  4. * and dnstairs_room after the rooms have been sorted.  On normal levels,
  5. * stairs don't get created until _after_ sorting takes place.
  6. */
  7. STATIC_OVL void
  8. fix_stair_rooms()
  9. {
  10. int i;
  11. struct mkroom *croom;
  12.  
  13. if(xdnstair &&
  14. !((dnstairs_room->lx <= xdnstair && xdnstair <= dnstairs_room->hx) &&
  15. 	 (dnstairs_room->ly <= ydnstair && ydnstair <= dnstairs_room->hy))) {
  16. 	for(i=0; i < nroom; i++) {
  17. 	    croom = &rooms[i];
  18. 	    if((croom->lx <= xdnstair && xdnstair <= croom->hx) &&
  19. 	       (croom->ly <= ydnstair && ydnstair <= croom->hy)) {
  20. 		dnstairs_room = croom;
  21. 		break;
  22. 	    }
  23. 	}
  24. 	if(i == nroom)
  25. 	    panic("Couldn't find dnstair room in fix_stair_rooms!");
  26. }
  27. if(xupstair &&
  28. !((upstairs_room->lx <= xupstair && xupstair <= upstairs_room->hx) &&
  29. 	 (upstairs_room->ly <= yupstair && yupstair <= upstairs_room->hy))) {
  30. 	for(i=0; i < nroom; i++) {
  31. 	    croom = &rooms[i];
  32. 	    if((croom->lx <= xupstair && xupstair <= croom->hx) &&
  33. 	       (croom->ly <= yupstair && yupstair <= croom->hy)) {
  34. 		upstairs_room = croom;
  35. 		break;
  36. 	    }
  37. 	}
  38. 	if(i == nroom)
  39. 	    panic("Couldn't find upstair room in fix_stair_rooms!");
  40. }
  41. }

create_corridor

  1. /*
  2. * Corridors always start from a door. But it can end anywhere...
  3. * Basically we search for door coordinates or for endpoints coordinates
  4. * (from a distance).
  5. */
  6.  
  7. STATIC_OVL void
  8. create_corridor(c)
  9. corridor	*c;
  10. {
  11. 	coord org, dest;
  12.  
  13. 	if (c->src.room == -1) {
  14. 		sort_rooms();
  15. 		fix_stair_rooms();
  16. 		makecorridors();
  17. 		return;
  18. 	}
  19.  
  20. 	if( !search_door(&rooms[c->src.room], &org.x, &org.y, c->src.wall,
  21. 			 c->src.door))
  22. 	    return;
  23.  
  24. 	if (c->dest.room != -1) {
  25. 		if(!search_door(&rooms[c->dest.room], &dest.x, &dest.y,
  26. 				c->dest.wall, c->dest.door))
  27. 		    return;
  28. 		switch(c->src.wall) {
  29. 		      case W_NORTH: org.y--; break;
  30. 		      case W_SOUTH: org.y++; break;
  31. 		      case W_WEST:  org.x--; break;
  32. 		      case W_EAST:  org.x++; break;
  33. 		}
  34. 		switch(c->dest.wall) {
  35. 		      case W_NORTH: dest.y--; break;
  36. 		      case W_SOUTH: dest.y++; break;
  37. 		      case W_WEST:  dest.x--; break;
  38. 		      case W_EAST:  dest.x++; break;
  39. 		}
  40. 		(void) dig_corridor(&org, &dest, FALSE, CORR, STONE);
  41. 	}
  42. }

fill_room

  1. /*
  2. * Fill a room (shop, zoo, etc...) with appropriate stuff.
  3. */
  4.  
  5. void
  6. fill_room(croom, prefilled)
  7. struct mkroom *croom;
  8. boolean prefilled;
  9. {
  10. 	if (!croom || croom->rtype == OROOM)
  11. 	    return;
  12.  
  13. 	if (!prefilled) {
  14. 	    int x,y;
  15.  
  16. 	    /* Shop ? */
  17. 	    if (croom->rtype >= SHOPBASE) {
  18. 		    stock_room(croom->rtype - SHOPBASE, croom);
  19. 		    level.flags.has_shop = TRUE;
  20. 		    return;
  21. 	    }
  22.  
  23. 	    switch (croom->rtype) {
  24. 		case VAULT:
  25. 		    for (x=croom->lx;x<=croom->hx;x++)
  26. 			for (y=croom->ly;y<=croom->hy;y++)
  27. 			    (void) mkgold((long)rn1(abs(depth(&u.uz))*100, 51), x, y);
  28. 		    break;
  29. 		case COURT:
  30. 		case ZOO:
  31. 		case BEEHIVE:
  32. 		case MORGUE:
  33. 		case BARRACKS:
  34. 		    fill_zoo(croom);
  35. 		    break;
  36. 	    }
  37. 	}
  38. 	switch (croom->rtype) {
  39. 	    case VAULT:
  40. 		level.flags.has_vault = TRUE;
  41. 		break;
  42. 	    case ZOO:
  43. 		level.flags.has_zoo = TRUE;
  44. 		break;
  45. 	    case COURT:
  46. 		level.flags.has_court = TRUE;
  47. 		break;
  48. 	    case MORGUE:
  49. 		level.flags.has_morgue = TRUE;
  50. 		break;
  51. 	    case BEEHIVE:
  52. 		level.flags.has_beehive = TRUE;
  53. 		break;
  54. 	    case BARRACKS:
  55. 		level.flags.has_barracks = TRUE;
  56. 		break;
  57. 	    case TEMPLE:
  58. 		level.flags.has_temple = TRUE;
  59. 		break;
  60. 	    case SWAMP:
  61. 		level.flags.has_swamp = TRUE;
  62. 		break;
  63. 	}
  64. }

free_rooms

  1. STATIC_OVL void
  2. free_rooms(ro, n)
  3. room **ro;
  4. int n;
  5. {
  6. 	short j;
  7. 	room *r;
  8.  
  9. 	while(n--) {
  10. 		r = ro[n];
  11. 		Free(r->name);
  12. 		Free(r->parent);
  13. 		if ((j = r->ndoor) != 0) {
  14. 			while(j--)
  15. 			    Free(r->doors[j]);
  16. 			Free(r->doors);
  17. 		}
  18. 		if ((j = r->nstair) != 0) {
  19. 			while(j--)
  20. 			    Free(r->stairs[j]);
  21. 			Free(r->stairs);
  22. 		}
  23. 		if ((j = r->naltar) != 0) {
  24. 			while (j--)
  25. 			    Free(r->altars[j]);
  26. 			Free(r->altars);
  27. 		}
  28. 		if ((j = r->nfountain) != 0) {
  29. 			while(j--)
  30. 			    Free(r->fountains[j]);
  31. 			Free(r->fountains);
  32. 		}
  33. 		if ((j = r->nsink) != 0) {
  34. 			while(j--)
  35. 			    Free(r->sinks[j]);
  36. 			Free(r->sinks);
  37. 		}
  38. 		if ((j = r->npool) != 0) {
  39. 			while(j--)
  40. 			    Free(r->pools[j]);
  41. 			Free(r->pools);
  42. 		}
  43. 		if ((j = r->ntrap) != 0) {
  44. 			while (j--)
  45. 			    Free(r->traps[j]);
  46. 			Free(r->traps);
  47. 		}
  48. 		if ((j = r->nmonster) != 0) {
  49. 			while (j--)
  50. 				Free(r->monsters[j]);
  51. 			Free(r->monsters);
  52. 		}
  53. 		if ((j = r->nobject) != 0) {
  54. 			while (j--)
  55. 				Free(r->objects[j]);
  56. 			Free(r->objects);
  57. 		}
  58. 		if ((j = r->ngold) != 0) {
  59. 			while(j--)
  60. 			    Free(r->golds[j]);
  61. 			Free(r->golds);
  62. 		}
  63. 		if ((j = r->nengraving) != 0) {
  64. 			while (j--)
  65. 				Free(r->engravings[j]);
  66. 			Free(r->engravings);
  67. 		}
  68. 		Free(r);
  69. 	}
  70. 	Free(ro);
  71. }

build_room

  1. STATIC_OVL void
  2. build_room(r, pr)
  3. room *r, *pr;
  4. {
  5. 	boolean okroom;
  6. 	struct mkroom	*aroom;
  7. 	short i;
  8. 	xchar rtype = (!r->chance || rn2(100) < r->chance) ? r->rtype : OROOM;
  9.  
  10. 	if(pr) {
  11. 		aroom = &subrooms[nsubroom];
  12. 		okroom = create_subroom(pr->mkr, r->x, r->y, r->w, r->h,
  13. 					rtype, r->rlit);
  14. 	} else {
  15. 		aroom = &rooms[nroom];
  16. 		okroom = create_room(r->x, r->y, r->w, r->h, r->xalign,
  17. 				     r->yalign, rtype, r->rlit);
  18. 		r->mkr = aroom;
  19. 	}
  20.  
  21. 	if (okroom) {
  22. 		/* Create subrooms if necessary... */
  23. 		for(i=0; i < r->nsubroom; i++)
  24. 		    build_room(r->subrooms[i], r);
  25. 		/* And now we can fill the room! */
  26.  
  27. 		/* Priority to the stairs */
  28.  
  29. 		for(i=0; i <r->nstair; i++)
  30. 		    create_stairs(r->stairs[i], aroom);
  31.  
  32. 		/* Then to the various elements (sinks, etc..) */
  33. 		for(i = 0; i<r->nsink; i++)
  34. 		    create_feature(r->sinks[i]->x, r->sinks[i]->y, aroom, SINK);
  35. 		for(i = 0; i<r->npool; i++)
  36. 		    create_feature(r->pools[i]->x, r->pools[i]->y, aroom, POOL);
  37. 		for(i = 0; i<r->nfountain; i++)
  38. 		    create_feature(r->fountains[i]->x, r->fountains[i]->y,
  39. 				   aroom, FOUNTAIN);
  40. 		for(i = 0; i<r->naltar; i++)
  41. 		    create_altar(r->altars[i], aroom);
  42. 		for(i = 0; i<r->ndoor; i++)
  43. 		    create_door(r->doors[i], aroom);
  44.  
  45. 		/* The traps */
  46. 		for(i = 0; i<r->ntrap; i++)
  47. 		    create_trap(r->traps[i], aroom);
  48.  
  49. 		/* The monsters */
  50. 		for(i = 0; i<r->nmonster; i++)
  51. 		    create_monster(r->monsters[i], aroom);
  52.  
  53. 		/* The objects */
  54. 		for(i = 0; i<r->nobject; i++)
  55. 		    create_object(r->objects[i], aroom);
  56.  
  57. 		/* The gold piles */
  58. 		for(i = 0; i<r->ngold; i++)
  59. 		    create_gold(r->golds[i], aroom);
  60.  
  61. 		/* The engravings */
  62. 		for (i = 0; i < r->nengraving; i++)
  63. 		    create_engraving(r->engravings[i], aroom);
  64.  
  65. #ifdef SPECIALIZATION
  66. 		topologize(aroom,FALSE);		/* set roomno */
  67. #else
  68. 		topologize(aroom);			/* set roomno */
  69. #endif
  70. 		/* MRS - 07/04/91 - This is temporary but should result
  71. 		 * in proper filling of shops, etc.
  72. 		 * DLC - this can fail if corridors are added to this room
  73. 		 * at a later point.  Currently no good way to fix this.
  74. 		 */
  75. 		if(aroom->rtype != OROOM && r->filled) fill_room(aroom, FALSE);
  76. 	}
  77. }

light_region

  1. /*
  2. * set lighting in a region that will not become a room.
  3. */
  4. STATIC_OVL void
  5. light_region(tmpregion)
  6. region  *tmpregion;
  7. {
  8. register boolean litstate = tmpregion->rlit ? 1 : 0;
  9. register int hiy = tmpregion->y2;
  10. register int x, y;
  11. register struct rm *lev;
  12. int lowy = tmpregion->y1;
  13. int lowx = tmpregion->x1, hix = tmpregion->x2;
  14.  
  15. if(litstate) {
  16. 	/* adjust region size for walls, but only if lighted */
  17. 	lowx = max(lowx-1,1);
  18. 	hix = min(hix+1,COLNO-1);
  19. 	lowy = max(lowy-1,0);
  20. 	hiy = min(hiy+1, ROWNO-1);
  21. }
  22. for(x = lowx; x <= hix; x++) {
  23. 	lev = &levl[x][lowy];
  24. 	for(y = lowy; y <= hiy; y++) {
  25. 	    if (lev->typ != LAVAPOOL) /* this overrides normal lighting */
  26. 		lev->lit = litstate;
  27. 	    lev++;
  28. 	}
  29. }
  30. }

load_common_data

  1. /* initialization common to all special levels */
  2. STATIC_OVL void
  3. load_common_data(fd, typ)
  4. dlb *fd;
  5. int typ;
  6. {
  7. 	uchar	n;
  8. 	long	lev_flags;
  9. 	int	i;
  10.  
  11. {
  12. 	aligntyp atmp;
  13. 	/* shuffle 3 alignments; can't use sp_lev_shuffle() on aligntyp's */
  14. 	i = rn2(3);   atmp=ralign[2]; ralign[2]=ralign[i]; ralign[i]=atmp;
  15. 	if (rn2(2)) { atmp=ralign[1]; ralign[1]=ralign[0]; ralign[0]=atmp; }
  16. }
  17.  
  18. 	level.flags.is_maze_lev = typ == SP_LEV_MAZE;
  19.  
  20. 	/* Read the level initialization data */
  21. 	Fread((genericptr_t) &init_lev, 1, sizeof(lev_init), fd);
  22. 	if(init_lev.init_present) {
  23. 	    if(init_lev.lit < 0)
  24. 		init_lev.lit = rn2(2);
  25. 	    mkmap(&init_lev);
  26. 	}
  27.  
  28. 	/* Read the per level flags */
  29. 	Fread((genericptr_t) &lev_flags, 1, sizeof(lev_flags), fd);
  30. 	if (lev_flags & NOTELEPORT)
  31. 	    level.flags.noteleport = 1;
  32. 	if (lev_flags & HARDFLOOR)
  33. 	    level.flags.hardfloor = 1;
  34. 	if (lev_flags & NOMMAP)
  35. 	    level.flags.nommap = 1;
  36. 	if (lev_flags & SHORTSIGHTED)
  37. 	    level.flags.shortsighted = 1;
  38. 	if (lev_flags & ARBOREAL)
  39. 	    level.flags.arboreal = 1;
  40.  
  41. 	/* Read message */
  42. 	Fread((genericptr_t) &n, 1, sizeof(n), fd);
  43. 	if (n) {
  44. 	    lev_message = (char *) alloc(n + 1);
  45. 	    Fread((genericptr_t) lev_message, 1, (int) n, fd);
  46. 	    lev_message[n] = 0;
  47. 	}
  48. }

load_one_monster

  1. STATIC_OVL void
  2. load_one_monster(fd, m)
  3. dlb *fd;
  4. monster *m;
  5. {
  6. 	int size;
  7.  
  8. 	Fread((genericptr_t) m, 1, sizeof *m, fd);
  9. 	if ((size = m->name.len) != 0) {
  10. 	    m->name.str = (char *) alloc((unsigned)size + 1);
  11. 	    Fread((genericptr_t) m->name.str, 1, size, fd);
  12. 	    m->name.str[size] = '\0';
  13. 	} else
  14. 	    m->name.str = (char *) 0;
  15. 	if ((size = m->appear_as.len) != 0) {
  16. 	    m->appear_as.str = (char *) alloc((unsigned)size + 1);
  17. 	    Fread((genericptr_t) m->appear_as.str, 1, size, fd);
  18. 	    m->appear_as.str[size] = '\0';
  19. 	} else
  20. 	    m->appear_as.str = (char *) 0;
  21. }

load_one_object

  1. STATIC_OVL void
  2. load_one_object(fd, o)
  3. dlb *fd;
  4. object *o;
  5. {
  6. 	int size;
  7.  
  8. 	Fread((genericptr_t) o, 1, sizeof *o, fd);
  9. 	if ((size = o->name.len) != 0) {
  10. 	    o->name.str = (char *) alloc((unsigned)size + 1);
  11. 	    Fread((genericptr_t) o->name.str, 1, size, fd);
  12. 	    o->name.str[size] = '\0';
  13. 	} else
  14. 	    o->name.str = (char *) 0;
  15. }

load_one_engraving

  1. STATIC_OVL void
  2. load_one_engraving(fd, e)
  3. dlb *fd;
  4. engraving *e;
  5. {
  6. 	int size;
  7.  
  8. 	Fread((genericptr_t) e, 1, sizeof *e, fd);
  9. 	size = e->engr.len;
  10. 	e->engr.str = (char *) alloc((unsigned)size+1);
  11. 	Fread((genericptr_t) e->engr.str, 1, size, fd);
  12. 	e->engr.str[size] = '\0';
  13. }

load_rooms

  1. STATIC_OVL boolean
  2. load_rooms(fd)
  3. dlb *fd;
  4. {
  5. 	xchar		nrooms, ncorr;
  6. 	char		n;
  7. 	short		size;
  8. 	corridor	tmpcor;
  9. 	room**		tmproom;
  10. 	int		i, j;
  11.  
  12. 	load_common_data(fd, SP_LEV_ROOMS);
  13.  
  14. 	Fread((genericptr_t) &n, 1, sizeof(n), fd); /* nrobjects */
  15. 	if (n) {
  16. 		Fread((genericptr_t)robjects, sizeof(*robjects), n, fd);
  17. 		sp_lev_shuffle(robjects, (char *)0, (int)n);
  18. 	}
  19.  
  20. 	Fread((genericptr_t) &n, 1, sizeof(n), fd); /* nrmonst */
  21. 	if (n) {
  22. 		Fread((genericptr_t)rmonst, sizeof(*rmonst), n, fd);
  23. 		sp_lev_shuffle(rmonst, (char *)0, (int)n);
  24. 	}
  25.  
  26. 	Fread((genericptr_t) &nrooms, 1, sizeof(nrooms), fd);
  27. 						/* Number of rooms to read */
  28. 	tmproom = NewTab(room,nrooms);
  29. 	for (i=0;i<nrooms;i++) {
  30. 		room *r;
  31.  
  32. 		r = tmproom[i] = New(room);
  33.  
  34. 		/* Let's see if this room has a name */
  35. 		Fread((genericptr_t) &size, 1, sizeof(size), fd);
  36. 		if (size > 0) {	/* Yup, it does! */
  37. 			r->name = (char *) alloc((unsigned)size + 1);
  38. 			Fread((genericptr_t) r->name, 1, size, fd);
  39. 			r->name[size] = 0;
  40. 		} else
  41. 		    r->name = (char *) 0;
  42.  
  43. 		/* Let's see if this room has a parent */
  44. 		Fread((genericptr_t) &size, 1, sizeof(size), fd);
  45. 		if (size > 0) {	/* Yup, it does! */
  46. 			r->parent = (char *) alloc((unsigned)size + 1);
  47. 			Fread((genericptr_t) r->parent, 1, size, fd);
  48. 			r->parent[size] = 0;
  49. 		} else
  50. 		    r->parent = (char *) 0;
  51.  
  52. 		Fread((genericptr_t) &r->x, 1, sizeof(r->x), fd);
  53. 					/* x pos on the grid (1-5) */
  54. 		Fread((genericptr_t) &r->y, 1, sizeof(r->y), fd);
  55. 					 /* y pos on the grid (1-5) */
  56. 		Fread((genericptr_t) &r->w, 1, sizeof(r->w), fd);
  57. 					 /* width of the room */
  58. 		Fread((genericptr_t) &r->h, 1, sizeof(r->h), fd);
  59. 					 /* height of the room */
  60. 		Fread((genericptr_t) &r->xalign, 1, sizeof(r->xalign), fd);
  61. 					 /* horizontal alignment */
  62. 		Fread((genericptr_t) &r->yalign, 1, sizeof(r->yalign), fd);
  63. 					 /* vertical alignment */
  64. 		Fread((genericptr_t) &r->rtype, 1, sizeof(r->rtype), fd);
  65. 					 /* type of room (zoo, shop, etc.) */
  66. 		Fread((genericptr_t) &r->chance, 1, sizeof(r->chance), fd);
  67. 					 /* chance of room being special. */
  68. 		Fread((genericptr_t) &r->rlit, 1, sizeof(r->rlit), fd);
  69. 					 /* lit or not ? */
  70. 		Fread((genericptr_t) &r->filled, 1, sizeof(r->filled), fd);
  71. 					 /* to be filled? */
  72. 		r->nsubroom= 0;
  73.  
  74. 		/* read the doors */
  75. 		Fread((genericptr_t) &r->ndoor, 1, sizeof(r->ndoor), fd);
  76. 		if ((n = r->ndoor) != 0)
  77. 		    r->doors = NewTab(room_door, n);
  78. 		while(n--) {
  79. 			r->doors[(int)n] = New(room_door);
  80. 			Fread((genericptr_t) r->doors[(int)n], 1,
  81. 				sizeof(room_door), fd);
  82. 		}
  83.  
  84. 		/* read the stairs */
  85. 		Fread((genericptr_t) &r->nstair, 1, sizeof(r->nstair), fd);
  86. 		if ((n = r->nstair) != 0)
  87. 		    r->stairs = NewTab(stair, n);
  88. 		while (n--) {
  89. 			r->stairs[(int)n] = New(stair);
  90. 			Fread((genericptr_t) r->stairs[(int)n], 1,
  91. 				sizeof(stair), fd);
  92. 		}
  93.  
  94. 		/* read the altars */
  95. 		Fread((genericptr_t) &r->naltar, 1, sizeof(r->naltar), fd);
  96. 		if ((n = r->naltar) != 0)
  97. 		    r->altars = NewTab(altar, n);
  98. 		while (n--) {
  99. 			r->altars[(int)n] = New(altar);
  100. 			Fread((genericptr_t) r->altars[(int)n], 1,
  101. 				sizeof(altar), fd);
  102. 		}
  103.  
  104. 		/* read the fountains */
  105. 		Fread((genericptr_t) &r->nfountain, 1,
  106. 			sizeof(r->nfountain), fd);
  107. 		if ((n = r->nfountain) != 0)
  108. 		    r->fountains = NewTab(fountain, n);
  109. 		while (n--) {
  110. 			r->fountains[(int)n] = New(fountain);
  111. 			Fread((genericptr_t) r->fountains[(int)n], 1,
  112. 				sizeof(fountain), fd);
  113. 		}
  114.  
  115. 		/* read the sinks */
  116. 		Fread((genericptr_t) &r->nsink, 1, sizeof(r->nsink), fd);
  117. 		if ((n = r->nsink) != 0)
  118. 		    r->sinks = NewTab(sink, n);
  119. 		while (n--) {
  120. 			r->sinks[(int)n] = New(sink);
  121. 			Fread((genericptr_t) r->sinks[(int)n], 1, sizeof(sink), fd);
  122. 		}
  123.  
  124. 		/* read the pools */
  125. 		Fread((genericptr_t) &r->npool, 1, sizeof(r->npool), fd);
  126. 		if ((n = r->npool) != 0)
  127. 		    r->pools = NewTab(pool,n);
  128. 		while (n--) {
  129. 			r->pools[(int)n] = New(pool);
  130. 			Fread((genericptr_t) r->pools[(int)n], 1, sizeof(pool), fd);
  131. 		}
  132.  
  133. 		/* read the traps */
  134. 		Fread((genericptr_t) &r->ntrap, 1, sizeof(r->ntrap), fd);
  135. 		if ((n = r->ntrap) != 0)
  136. 		    r->traps = NewTab(trap, n);
  137. 		while(n--) {
  138. 			r->traps[(int)n] = New(trap);
  139. 			Fread((genericptr_t) r->traps[(int)n], 1, sizeof(trap), fd);
  140. 		}
  141.  
  142. 		/* read the monsters */
  143. 		Fread((genericptr_t) &r->nmonster, 1, sizeof(r->nmonster), fd);
  144. 		if ((n = r->nmonster) != 0) {
  145. 		    r->monsters = NewTab(monster, n);
  146. 		    while(n--) {
  147. 			r->monsters[(int)n] = New(monster);
  148. 			load_one_monster(fd, r->monsters[(int)n]);
  149. 		    }
  150. 		} else
  151. 		    r->monsters = 0;
  152.  
  153. 		/* read the objects, in same order as mazes */
  154. 		Fread((genericptr_t) &r->nobject, 1, sizeof(r->nobject), fd);
  155. 		if ((n = r->nobject) != 0) {
  156. 		    r->objects = NewTab(object, n);
  157. 		    for (j = 0; j < n; ++j) {
  158. 			r->objects[j] = New(object);
  159. 			load_one_object(fd, r->objects[j]);
  160. 		    }
  161. 		} else
  162. 		    r->objects = 0;
  163.  
  164. 		/* read the gold piles */
  165. 		Fread((genericptr_t) &r->ngold, 1, sizeof(r->ngold), fd);
  166. 		if ((n = r->ngold) != 0)
  167. 		    r->golds = NewTab(gold, n);
  168. 		while (n--) {
  169. 			r->golds[(int)n] = New(gold);
  170. 			Fread((genericptr_t) r->golds[(int)n], 1, sizeof(gold), fd);
  171. 		}
  172.  
  173. 		/* read the engravings */
  174. 		Fread((genericptr_t) &r->nengraving, 1,
  175. 			sizeof(r->nengraving), fd);
  176. 		if ((n = r->nengraving) != 0) {
  177. 		    r->engravings = NewTab(engraving,n);
  178. 		    while (n--) {
  179. 			r->engravings[(int)n] = New(engraving);
  180. 			load_one_engraving(fd, r->engravings[(int)n]);
  181. 		    }
  182. 		} else
  183. 		    r->engravings = 0;
  184.  
  185. 	}
  186.  
  187. 	/* Now that we have loaded all the rooms, search the
  188. 	 * subrooms and create the links.
  189. 	 */
  190.  
  191. 	for (i = 0; i<nrooms; i++)
  192. 	    if (tmproom[i]->parent) {
  193. 		    /* Search the parent room */
  194. 		    for(j=0; j<nrooms; j++)
  195. 			if (tmproom[j]->name && !strcmp(tmproom[j]->name,
  196. 						       tmproom[i]->parent)) {
  197. 				n = tmproom[j]->nsubroom++;
  198. 				tmproom[j]->subrooms[(int)n] = tmproom[i];
  199. 				break;
  200. 			}
  201. 	    }
  202.  
  203. 	/*
  204. 	 * Create the rooms now...
  205. 	 */
  206.  
  207. 	for (i=0; i < nrooms; i++)
  208. 	    if(!tmproom[i]->parent)
  209. 		build_room(tmproom[i], (room *) 0);
  210.  
  211. 	free_rooms(tmproom, nrooms);
  212.  
  213. 	/* read the corridors */
  214.  
  215. 	Fread((genericptr_t) &ncorr, sizeof(ncorr), 1, fd);
  216. 	for (i=0; i<ncorr; i++) {
  217. 		Fread((genericptr_t) &tmpcor, 1, sizeof(tmpcor), fd);
  218. 		create_corridor(&tmpcor);
  219. 	}
  220.  
  221. 	return TRUE;
  222. }

maze1xy

  1. /*
  2. * Select a random coordinate in the maze.
  3. *
  4. * We want a place not 'touched' by the loader.  That is, a place in
  5. * the maze outside every part of the special level.
  6. */
  7.  
  8. STATIC_OVL void
  9. maze1xy(m, humidity)
  10. coord *m;
  11. int humidity;
  12. {
  13. 	register int x, y, tryct = 2000;
  14. 	/* tryct:  normally it won't take more than ten or so tries due
  15. 	   to the circumstances under which we'll be called, but the
  16. 	   `humidity' screening might drastically change the chances */
  17.  
  18. 	do {
  19. 	    x = rn1(x_maze_max - 3, 3);
  20. 	    y = rn1(y_maze_max - 3, 3);
  21. 	    if (--tryct < 0) break;	/* give up */
  22. 	} while (!(x % 2) || !(y % 2) || Map[x][y] ||
  23. 		 !is_ok_location((schar)x, (schar)y, humidity));
  24.  
  25. 	m->x = (xchar)x,  m->y = (xchar)y;
  26. }

load_maze

  1. /*
  2. * The Big Thing: special maze loader
  3. *
  4. * Could be cleaner, but it works.
  5. */
  6.  
  7. STATIC_OVL boolean
  8. load_maze(fd)
  9. dlb *fd;
  10. {
  11. xchar   x, y, typ;
  12. boolean prefilled, room_not_needed;
  13.  
  14. char    n, numpart = 0;
  15. xchar   nwalk = 0, nwalk_sav;
  16. schar   filling;
  17. char    halign, valign;
  18.  
  19. int     xi, dir, size;
  20. coord   mm;
  21. int     mapcount, mapcountmax, mapfact;
  22.  
  23. lev_region  tmplregion;
  24. region  tmpregion;
  25. door    tmpdoor;
  26. trap    tmptrap;
  27. monster tmpmons;
  28. object  tmpobj;
  29. drawbridge tmpdb;
  30. walk    tmpwalk;
  31. digpos  tmpdig;
  32. lad     tmplad;
  33. stair   tmpstair, prevstair;
  34. altar   tmpaltar;
  35. gold    tmpgold;
  36. fountain tmpfountain;
  37. engraving tmpengraving;
  38. xchar   mustfill[(MAXNROFROOMS+1)*2];
  39. struct trap *badtrap;
  40. boolean has_bounds;
  41.  
  42. (void) memset((genericptr_t)&Map[0][0], 0, sizeof Map);
  43. load_common_data(fd, SP_LEV_MAZE);
  44.  
  45. /* Initialize map */
  46. Fread((genericptr_t) &filling, 1, sizeof(filling), fd);
  47. if (!init_lev.init_present) { /* don't init if mkmap() has been called */
  48. for(x = 2; x <= x_maze_max; x++)
  49. 	for(y = 0; y <= y_maze_max; y++)
  50. 	    if (filling == -1) {
  51. #ifndef WALLIFIED_MAZE
  52. 		    levl[x][y].typ = STONE;
  53. #else
  54. 		    levl[x][y].typ =
  55. 			(y < 2 || ((x % 2) && (y % 2))) ? STONE : HWALL;
  56. #endif
  57. 	    } else {
  58. 		    levl[x][y].typ = filling;
  59. 	    }
  60. }
  61.  
  62. /* Start reading the file */
  63. Fread((genericptr_t) &numpart, 1, sizeof(numpart), fd);
  64. 						/* Number of parts */
  65. if (!numpart || numpart > 9)
  66. 	panic("load_maze error: numpart = %d", (int) numpart);
  67.  
  68. while (numpart--) {
  69. 	Fread((genericptr_t) &halign, 1, sizeof(halign), fd);
  70. 					/* Horizontal alignment */
  71. 	Fread((genericptr_t) &valign, 1, sizeof(valign), fd);
  72. 					/* Vertical alignment */
  73. 	Fread((genericptr_t) &xsize, 1, sizeof(xsize), fd);
  74. 					/* size in X */
  75. 	Fread((genericptr_t) &ysize, 1, sizeof(ysize), fd);
  76. 					/* size in Y */
  77. 	switch((int) halign) {
  78. 	    case LEFT:	    xstart = 3;					break;
  79. 	    case H_LEFT:    xstart = 2+((x_maze_max-2-xsize)/4);	break;
  80. 	    case CENTER:    xstart = 2+((x_maze_max-2-xsize)/2);	break;
  81. 	    case H_RIGHT:   xstart = 2+((x_maze_max-2-xsize)*3/4);	break;
  82. 	    case RIGHT:     xstart = x_maze_max-xsize-1;		break;
  83. 	}
  84. 	switch((int) valign) {
  85. 	    case TOP:	    ystart = 3;					break;
  86. 	    case CENTER:    ystart = 2+((y_maze_max-2-ysize)/2);	break;
  87. 	    case BOTTOM:    ystart = y_maze_max-ysize-1;		break;
  88. 	}
  89. 	if (!(xstart % 2)) xstart++;
  90. 	if (!(ystart % 2)) ystart++;
  91. 	if ((ystart < 0) || (ystart + ysize > ROWNO)) {
  92. 	    /* try to move the start a bit */
  93. 	    ystart += (ystart > 0) ? -2 : 2;
  94. 	    if(ysize == ROWNO) ystart = 0;
  95. 	    if(ystart < 0 || ystart + ysize > ROWNO)
  96. 		panic("reading special level with ysize too large");
  97. 	}
  98.  
  99. 	/*
  100. 	 * If any CROSSWALLs are found, must change to ROOM after REGION's
  101. 	 * are laid out.  CROSSWALLS are used to specify "invisible"
  102. 	 * boundaries where DOOR syms look bad or aren't desirable.
  103. 	 */
  104. 	has_bounds = FALSE;
  105.  
  106. 	if(init_lev.init_present && xsize <= 1 && ysize <= 1) {
  107. 	    xstart = 1;
  108. 	    ystart = 0;
  109. 	    xsize = COLNO-1;
  110. 	    ysize = ROWNO;
  111. 	} else {
  112. 	    /* Load the map */
  113. 	    for(y = ystart; y < ystart+ysize; y++)
  114. 		for(x = xstart; x < xstart+xsize; x++) {
  115. 		    levl[x][y].typ = Fgetc(fd);
  116. 		    levl[x][y].lit = FALSE;
  117. 		    /* clear out levl: load_common_data may set them */
  118. 		    levl[x][y].flags = 0;
  119. 		    levl[x][y].horizontal = 0;
  120. 		    levl[x][y].roomno = 0;
  121. 		    levl[x][y].edge = 0;
  122. 		    /*
  123. 		     * Note: Even though levl[x][y].typ is type schar,
  124. 		     *	 lev_comp.y saves it as type char. Since schar != char
  125. 		     *	 all the time we must make this exception or hack
  126. 		     *	 through lev_comp.y to fix.
  127. 		     */
  128.  
  129. 		    /*
  130. 		     *  Set secret doors to closed (why not trapped too?).  Set
  131. 		     *  the horizontal bit.
  132. 		     */
  133. 		    if (levl[x][y].typ == SDOOR || IS_DOOR(levl[x][y].typ)) {
  134. 			if(levl[x][y].typ == SDOOR)
  135. 			    levl[x][y].doormask = D_CLOSED;
  136. 			/*
  137. 			 *  If there is a wall to the left that connects to a
  138. 			 *  (secret) door, then it is horizontal.  This does
  139. 			 *  not allow (secret) doors to be corners of rooms.
  140. 			 */
  141. 			if (x != xstart && (IS_WALL(levl[x-1][y].typ) ||
  142. 					    levl[x-1][y].horizontal))
  143. 			    levl[x][y].horizontal = 1;
  144. 		    } else if(levl[x][y].typ == HWALL ||
  145. 				levl[x][y].typ == IRONBARS)
  146. 			levl[x][y].horizontal = 1;
  147. 		    else if(levl[x][y].typ == LAVAPOOL)
  148. 			levl[x][y].lit = 1;
  149. 		    else if(levl[x][y].typ == CROSSWALL)
  150. 			has_bounds = TRUE;
  151. 		    Map[x][y] = 1;
  152. 		}
  153. 	    if (init_lev.init_present && init_lev.joined)
  154. 		remove_rooms(xstart, ystart, xstart+xsize, ystart+ysize);
  155. 	}
  156.  
  157. 	Fread((genericptr_t) &n, 1, sizeof(n), fd);
  158. 						/* Number of level regions */
  159. 	if(n) {
  160. 	    if(num_lregions) {
  161. 		/* realloc the lregion space to add the new ones */
  162. 		/* don't really free it up until the whole level is done */
  163. 		lev_region *newl = (lev_region *) alloc(sizeof(lev_region) *
  164. 						(unsigned)(n+num_lregions));
  165. 		(void) memcpy((genericptr_t)(newl+n), (genericptr_t)lregions,
  166. 					sizeof(lev_region) * num_lregions);
  167. 		Free(lregions);
  168. 		num_lregions += n;
  169. 		lregions = newl;
  170. 	    } else {
  171. 		num_lregions = n;
  172. 		lregions = (lev_region *)
  173. 				alloc(sizeof(lev_region) * (unsigned)n);
  174. 	    }
  175. 	}
  176.  
  177. 	while(n--) {
  178. 	    Fread((genericptr_t) &tmplregion, sizeof(tmplregion), 1, fd);
  179. 	    if ((size = tmplregion.rname.len) != 0) {
  180. 		tmplregion.rname.str = (char *) alloc((unsigned)size + 1);
  181. 		Fread((genericptr_t) tmplregion.rname.str, size, 1, fd);
  182. 		tmplregion.rname.str[size] = '\0';
  183. 	    } else
  184. 		tmplregion.rname.str = (char *) 0;
  185. 	    if(!tmplregion.in_islev) {
  186. 		get_location(&tmplregion.inarea.x1, &tmplregion.inarea.y1,
  187. 								DRY|WET);
  188. 		get_location(&tmplregion.inarea.x2, &tmplregion.inarea.y2,
  189. 								DRY|WET);
  190. 	    }
  191. 	    if(!tmplregion.del_islev) {
  192. 		get_location(&tmplregion.delarea.x1, &tmplregion.delarea.y1,
  193. 								DRY|WET);
  194. 		get_location(&tmplregion.delarea.x2, &tmplregion.delarea.y2,
  195. 								DRY|WET);
  196. 	    }
  197. 	    lregions[(int)n] = tmplregion;
  198. 	}
  199.  
  200. 	Fread((genericptr_t) &n, 1, sizeof(n), fd);
  201. 						/* Random objects */
  202. 	if(n) {
  203. 		Fread((genericptr_t)robjects, sizeof(*robjects), (int) n, fd);
  204. 		sp_lev_shuffle(robjects, (char *)0, (int)n);
  205. 	}
  206.  
  207. 	Fread((genericptr_t) &n, 1, sizeof(n), fd);
  208. 						/* Random locations */
  209. 	if(n) {
  210. 		Fread((genericptr_t)rloc_x, sizeof(*rloc_x), (int) n, fd);
  211. 		Fread((genericptr_t)rloc_y, sizeof(*rloc_y), (int) n, fd);
  212. 		sp_lev_shuffle(rloc_x, rloc_y, (int)n);
  213. 	}
  214.  
  215. 	Fread((genericptr_t) &n, 1, sizeof(n), fd);
  216. 						/* Random monsters */
  217. 	if(n) {
  218. 		Fread((genericptr_t)rmonst, sizeof(*rmonst), (int) n, fd);
  219. 		sp_lev_shuffle(rmonst, (char *)0, (int)n);
  220. 	}
  221.  
  222. 	(void) memset((genericptr_t)mustfill, 0, sizeof(mustfill));
  223. 	Fread((genericptr_t) &n, 1, sizeof(n), fd);
  224. 						/* Number of subrooms */
  225. 	while(n--) {
  226. 		register struct mkroom *troom;
  227.  
  228. 		Fread((genericptr_t)&tmpregion, 1, sizeof(tmpregion), fd);
  229.  
  230. 		if(tmpregion.rtype > MAXRTYPE) {
  231. 		    tmpregion.rtype -= MAXRTYPE+1;
  232. 		    prefilled = TRUE;
  233. 		} else
  234. 		    prefilled = FALSE;
  235.  
  236. 		if(tmpregion.rlit < 0)
  237. 		    tmpregion.rlit = (rnd(1+abs(depth(&u.uz))) < 11 && rn2(77))
  238. 			? TRUE : FALSE;
  239.  
  240. 		get_location(&tmpregion.x1, &tmpregion.y1, DRY|WET);
  241. 		get_location(&tmpregion.x2, &tmpregion.y2, DRY|WET);
  242.  
  243. 		/* for an ordinary room, `prefilled' is a flag to force
  244. 		   an actual room to be created (such rooms are used to
  245. 		   control placement of migrating monster arrivals) */
  246. 		room_not_needed = (tmpregion.rtype == OROOM &&
  247. 				   !tmpregion.rirreg && !prefilled);
  248. 		if (room_not_needed || nroom >= MAXNROFROOMS) {
  249. 		    if (!room_not_needed)
  250. 			impossible("Too many rooms on new level!");
  251. 		    light_region(&tmpregion);
  252. 		    continue;
  253. 		}
  254.  
  255. 		troom = &rooms[nroom];
  256.  
  257. 		/* mark rooms that must be filled, but do it later */
  258. 		if (tmpregion.rtype != OROOM)
  259. 		    mustfill[nroom] = (prefilled ? 2 : 1);
  260.  
  261. 		if(tmpregion.rirreg) {
  262. 		    min_rx = max_rx = tmpregion.x1;
  263. 		    min_ry = max_ry = tmpregion.y1;
  264. 		    flood_fill_rm(tmpregion.x1, tmpregion.y1,
  265. 				  nroom+ROOMOFFSET, tmpregion.rlit, TRUE);
  266. 		    add_room(min_rx, min_ry, max_rx, max_ry,
  267. 			     FALSE, tmpregion.rtype, TRUE);
  268. 		    troom->rlit = tmpregion.rlit;
  269. 		    troom->irregular = TRUE;
  270. 		} else {
  271. 		    add_room(tmpregion.x1, tmpregion.y1,
  272. 			     tmpregion.x2, tmpregion.y2,
  273. 			     tmpregion.rlit, tmpregion.rtype, TRUE);
  274. #ifdef SPECIALIZATION
  275. 		    topologize(troom,FALSE);		/* set roomno */
  276. #else
  277. 		    topologize(troom);			/* set roomno */
  278. #endif
  279. 		}
  280. 	}
  281.  
  282. 	Fread((genericptr_t) &n, 1, sizeof(n), fd);
  283. 						/* Number of doors */
  284. 	while(n--) {
  285. 		struct mkroom *croom = &rooms[0];
  286.  
  287. 		Fread((genericptr_t)&tmpdoor, 1, sizeof(tmpdoor), fd);
  288.  
  289. 		x = tmpdoor.x;	y = tmpdoor.y;
  290. 		typ = tmpdoor.mask == -1 ? rnddoor() : tmpdoor.mask;
  291.  
  292. 		get_location(&x, &y, DRY);
  293. 		if(levl[x][y].typ != SDOOR)
  294. 			levl[x][y].typ = DOOR;
  295. 		else {
  296. 			if(typ < D_CLOSED)
  297. 			    typ = D_CLOSED; /* force it to be closed */
  298. 		}
  299. 		levl[x][y].doormask = typ;
  300.  
  301. 		/* Now the complicated part, list it with each subroom */
  302. 		/* The dog move and mail daemon routines use this */
  303. 		while(croom->hx >= 0 && doorindex < DOORMAX) {
  304. 		    if(croom->hx >= x-1 && croom->lx <= x+1 &&
  305. 		       croom->hy >= y-1 && croom->ly <= y+1) {
  306. 			/* Found it */
  307. 			add_door(x, y, croom);
  308. 		    }
  309. 		    croom++;
  310. 		}
  311. 	}
  312.  
  313. 	/* now that we have rooms _and_ associated doors, fill the rooms */
  314. 	for(n = 0; n < SIZE(mustfill); n++)
  315. 	    if(mustfill[(int)n])
  316. 		fill_room(&rooms[(int)n], (mustfill[(int)n] == 2));
  317.  
  318. 	/* if special boundary syms (CROSSWALL) in map, remove them now */
  319. 	if(has_bounds) {
  320. 	    for(x = xstart; x < xstart+xsize; x++)
  321. 		for(y = ystart; y < ystart+ysize; y++)
  322. 		    if(levl[x][y].typ == CROSSWALL)
  323. 			levl[x][y].typ = ROOM;
  324. 	}
  325.  
  326. 	Fread((genericptr_t) &n, 1, sizeof(n), fd);
  327. 						/* Number of drawbridges */
  328. 	while(n--) {
  329. 		Fread((genericptr_t)&tmpdb, 1, sizeof(tmpdb), fd);
  330.  
  331. 		x = tmpdb.x;  y = tmpdb.y;
  332. 		get_location(&x, &y, DRY|WET);
  333.  
  334. 		if (!create_drawbridge(x, y, tmpdb.dir, tmpdb.db_open))
  335. 		    impossible("Cannot create drawbridge.");
  336. 	}
  337.  
  338. 	Fread((genericptr_t) &n, 1, sizeof(n), fd);
  339. 						/* Number of mazewalks */
  340. 	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.

  1. 		Fread((genericptr_t)&tmpwalk, 1, sizeof(tmpwalk), fd);
  2.  
  3. 		get_location(&tmpwalk.x, &tmpwalk.y, DRY|WET);
  4.  
  5. 		walklist[nwalk++] = tmpwalk;
  6. 	}
  7.  
  8. 	Fread((genericptr_t) &n, 1, sizeof(n), fd);
  9. 						/* Number of non_diggables */
  10. 	while(n--) {
  11. 		Fread((genericptr_t)&tmpdig, 1, sizeof(tmpdig), fd);
  12.  
  13. 		get_location(&tmpdig.x1, &tmpdig.y1, DRY|WET);
  14. 		get_location(&tmpdig.x2, &tmpdig.y2, DRY|WET);
  15.  
  16. 		set_wall_property(tmpdig.x1, tmpdig.y1,
  17. 				  tmpdig.x2, tmpdig.y2, W_NONDIGGABLE);
  18. 	}
  19.  
  20. 	Fread((genericptr_t) &n, 1, sizeof(n), fd);
  21. 						/* Number of non_passables */
  22. 	while(n--) {
  23. 		Fread((genericptr_t)&tmpdig, 1, sizeof(tmpdig), fd);
  24.  
  25. 		get_location(&tmpdig.x1, &tmpdig.y1, DRY|WET);
  26. 		get_location(&tmpdig.x2, &tmpdig.y2, DRY|WET);
  27.  
  28. 		set_wall_property(tmpdig.x1, tmpdig.y1,
  29. 				  tmpdig.x2, tmpdig.y2, W_NONPASSWALL);
  30. 	}
  31.  
  32. 	Fread((genericptr_t) &n, 1, sizeof(n), fd);
  33. 						/* Number of ladders */
  34. 	while(n--) {
  35. 		Fread((genericptr_t)&tmplad, 1, sizeof(tmplad), fd);
  36.  
  37. 		x = tmplad.x;  y = tmplad.y;
  38. 		get_location(&x, &y, DRY);
  39.  
  40. 		levl[x][y].typ = LADDER;
  41. 		if (tmplad.up == 1) {
  42. 			xupladder = x;	yupladder = y;
  43. 			levl[x][y].ladder = LA_UP;
  44. 		} else {
  45. 			xdnladder = x;	ydnladder = y;
  46. 			levl[x][y].ladder = LA_DOWN;
  47. 		}
  48. 	}
  49.  
  50. 	prevstair.x = prevstair.y = 0;
  51. 	Fread((genericptr_t) &n, 1, sizeof(n), fd);
  52. 						/* Number of stairs */
  53. 	while(n--) {
  54. 		Fread((genericptr_t)&tmpstair, 1, sizeof(tmpstair), fd);
  55.  
  56. 		xi = 0;
  57. 		do {
  58. 		    x = tmpstair.x;  y = tmpstair.y;
  59. 		    get_location(&x, &y, DRY);
  60. 		} while(prevstair.x && xi++ < 100 &&
  61. 			distmin(x,y,prevstair.x,prevstair.y) <= 8);
  62. 		if ((badtrap = t_at(x,y)) != 0) deltrap(badtrap);
  63. 		mkstairs(x, y, (char)tmpstair.up, (struct mkroom *)0);
  64. 		prevstair.x = x;
  65. 		prevstair.y = y;
  66. 	}
  67.  
  68. 	Fread((genericptr_t) &n, 1, sizeof(n), fd);
  69. 						/* Number of altars */
  70. 	while(n--) {
  71. 		Fread((genericptr_t)&tmpaltar, 1, sizeof(tmpaltar), fd);
  72.  
  73. 		create_altar(&tmpaltar, (struct mkroom *)0);
  74. 	}
  75.  
  76. 	Fread((genericptr_t) &n, 1, sizeof(n), fd);
  77. 						/* Number of fountains */
  78. 	while (n--) {
  79. 		Fread((genericptr_t)&tmpfountain, 1, sizeof(tmpfountain), fd);
  80.  
  81. 		create_feature(tmpfountain.x, tmpfountain.y,
  82. 			       (struct mkroom *)0, FOUNTAIN);
  83. 	}
  84.  
  85. 	Fread((genericptr_t) &n, 1, sizeof(n), fd);
  86. 						/* Number of traps */
  87. 	while(n--) {
  88. 		Fread((genericptr_t)&tmptrap, 1, sizeof(tmptrap), fd);
  89.  
  90. 		create_trap(&tmptrap, (struct mkroom *)0);
  91. 	}
  92.  
  93. 	Fread((genericptr_t) &n, 1, sizeof(n), fd);
  94. 						/* Number of monsters */
  95. 	while(n--) {
  96. 		load_one_monster(fd, &tmpmons);
  97.  
  98. 		create_monster(&tmpmons, (struct mkroom *)0);
  99. 	}
  100.  
  101. 	Fread((genericptr_t) &n, 1, sizeof(n), fd);
  102. 						/* Number of objects */
  103. 	while(n--) {
  104. 		load_one_object(fd, &tmpobj);
  105.  
  106. 		create_object(&tmpobj, (struct mkroom *)0);
  107. 	}
  108.  
  109. 	Fread((genericptr_t) &n, 1, sizeof(n), fd);
  110. 						/* Number of gold piles */
  111. 	while (n--) {
  112. 		Fread((genericptr_t)&tmpgold, 1, sizeof(tmpgold), fd);
  113.  
  114. 		create_gold(&tmpgold, (struct mkroom *)0);
  115. 	}
  116.  
  117. 	Fread((genericptr_t) &n, 1, sizeof(n), fd);
  118. 						/* Number of engravings */
  119. 	while(n--) {
  120. 		load_one_engraving(fd, &tmpengraving);
  121.  
  122. 		create_engraving(&tmpengraving, (struct mkroom *)0);
  123. 	}
  124.  
  125. }		/* numpart loop */

Generate each maze area specified in the level:

  1. nwalk_sav = nwalk;
  2. while(nwalk--) {

Get the maze starting point and direction previously defined:

  1. 	    x = (xchar) walklist[nwalk].x;
  2. 	    y = (xchar) walklist[nwalk].y;
  3. 	    dir = walklist[nwalk].dir;

Carve one square in the specified direction:

  1. 	    /* don't use move() - it doesn't use W_NORTH, etc. */
  2. 	    switch (dir) {
  3. 		case W_NORTH: --y; break;
  4. 		case W_SOUTH: y++; break;
  5. 		case W_EAST:  x++; break;
  6. 		case W_WEST:  --x; break;
  7. 		default: panic("load_maze: bad MAZEWALK direction");
  8. 	    }
  9.  
  10. 	    if(!IS_DOOR(levl[x][y].typ)) {
  11. #ifndef WALLIFIED_MAZE
  12. 		levl[x][y].typ = CORR;
  13. #else
  14. 		levl[x][y].typ = ROOM;
  15. #endif
  16. 		levl[x][y].flags = 0;
  17. 	    }
  18.  
  19. 	    /*
  20. 	     * We must be sure that the parity of the coordinates for
  21. 	     * walkfrom() is odd.  But we must also take into account
  22. 	     * what direction was chosen.
  23. 	     */
  24. 	    if(!(x % 2)) {
  25. 		if (dir == W_EAST)
  26. 		    x++;
  27. 		else
  28. 		    x--;
  29.  
  30. 		/* no need for IS_DOOR check; out of map bounds */
  31. #ifndef WALLIFIED_MAZE
  32. 		levl[x][y].typ = CORR;
  33. #else
  34. 		levl[x][y].typ = ROOM;
  35. #endif
  36. 		levl[x][y].flags = 0;
  37. 	    }
  38.  
  39. 	    if (!(y % 2)) {
  40. 		if (dir == W_SOUTH)
  41. 		    y++;
  42. 		else
  43. 		    y--;
  44. 	    }

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.

  1. 	    walkfrom(x, y);
  2. }

After generating all maze areas, re-wallify the entire map - should this be wrapped in "#ifdef WALLIFIED_MAZE"...?

  1. wallification(1, 0, COLNO-1, ROWNO-1);
  2.  
  3. /*
  4. * If there's a significant portion of maze unused by the special level,
  5. * we don't want it empty.
  6. *
  7. * Makes the number of traps, monsters, etc. proportional
  8. * to the size of the maze.
  9. */
  10. mapcountmax = mapcount = (x_maze_max - 2) * (y_maze_max - 2);
  11.  
  12. for(x = 2; x < x_maze_max; x++)
  13. 	for(y = 0; y < y_maze_max; y++)
  14. 	    if(Map[x][y]) mapcount--;
  15.  
  16. if (nwalk_sav && (mapcount > (int) (mapcountmax / 10))) {

Populate the random maze sections of the map. See similar logic in mkmaze.c#line631

  1. 	    mapfact = (int) ((mapcount * 100L) / mapcountmax);
  2. 	    for(x = rnd((int) (20 * mapfact) / 100); x; x--) {
  3. 		    maze1xy(&mm, DRY);
  4. 		    (void) mkobj_at(rn2(2) ? GEM_CLASS : RANDOM_CLASS,
  5. 							mm.x, mm.y, TRUE);
  6. 	    }
  7. 	    for(x = rnd((int) (12 * mapfact) / 100); x; x--) {
  8. 		    maze1xy(&mm, DRY);
  9. 		    (void) mksobj_at(BOULDER, mm.x, mm.y, TRUE, FALSE);
  10. 	    }
  11. 	    for (x = rn2(2); x; x--) {
  12. 		maze1xy(&mm, DRY);
  13. 		(void) makemon(&mons[PM_MINOTAUR], mm.x, mm.y, NO_MM_FLAGS);
  14. 	    }
  15. 	    for(x = rnd((int) (12 * mapfact) / 100); x; x--) {
  16. 		    maze1xy(&mm, WET|DRY);
  17. 		    (void) makemon((struct permonst *) 0, mm.x, mm.y, NO_MM_FLAGS);
  18. 	    }
  19. 	    for(x = rn2((int) (15 * mapfact) / 100); x; x--) {
  20. 		    maze1xy(&mm, DRY);
  21. 		    (void) mkgold(0L,mm.x,mm.y);
  22. 	    }
  23. 	    for(x = rn2((int) (15 * mapfact) / 100); x; x--) {
  24. 		    int trytrap;
  25.  
  26. 		    maze1xy(&mm, DRY);
  27. 		    trytrap = rndtrap();
  28. 		    if (sobj_at(BOULDER, mm.x, mm.y))
  29. 			while (trytrap == PIT || trytrap == SPIKED_PIT ||
  30. 				trytrap == TRAPDOOR || trytrap == HOLE)
  31. 			    trytrap = rndtrap();
  32. 		    (void) maketrap(mm.x, mm.y, trytrap);
  33. 	    }
  34. }
  35. return TRUE;
  36. }

load_special

  1. /*
  2. * General loader
  3. */
  4.  
  5. boolean
  6. load_special(name)
  7. const char *name;
  8. {
  9. 	dlb *fd;
  10. 	boolean result = FALSE;
  11. 	char c;
  12. 	struct version_info vers_info;
  13.  
  14. 	fd = dlb_fopen(name, RDBMODE);
  15. 	if (!fd) return FALSE;
  16.  
  17. 	Fread((genericptr_t) &vers_info, sizeof vers_info, 1, fd);
  18. 	if (!check_version(&vers_info, name, TRUE))
  19. 	    goto give_up;
  20.  
  21. 	Fread((genericptr_t) &c, sizeof c, 1, fd); /* c Header */
  22.  
  23. 	switch (c) {
  24. 		case SP_LEV_ROOMS:
  25. 		    result = load_rooms(fd);
  26. 		    break;
  27. 		case SP_LEV_MAZE:
  28. 		    result = load_maze(fd);
  29. 		    break;
  30. 		default:	/* ??? */
  31. 		    result = FALSE;
  32. 	}
  33. give_up:
  34. 	(void)dlb_fclose(fd);
  35. 	return result;
  36. }
  37.  
  38. /*sp_lev.c*/