Source:NetHack 3.4.3/src/mkmaze.c

From NetHackWiki
Jump to: navigation, search

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

Top of file[edit]

  1. /*	SCCS Id: @(#)mkmaze.c	3.4	2002/04/04	*/
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  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. #include "hack.h"
  2. #include "sp_lev.h"
  3. #include "lev.h"	/* save & restore info */
  4.  
  5. /* from sp_lev.c, for fixup_special() */
  6. extern char *lev_message;
  7. extern lev_region *lregions;
  8. extern int num_lregions;
  9.  
  10. STATIC_DCL boolean FDECL(iswall,(int,int));
  11. STATIC_DCL boolean FDECL(iswall_or_stone,(int,int));
  12. STATIC_DCL boolean FDECL(is_solid,(int,int));
  13. STATIC_DCL int FDECL(extend_spine, (int [3][3], int, int, int));
  14. STATIC_DCL boolean FDECL(okay,(int,int,int));
  15. STATIC_DCL void FDECL(maze0xy,(coord *));
  16. STATIC_DCL boolean FDECL(put_lregion_here,(XCHAR_P,XCHAR_P,XCHAR_P,
  17. 	XCHAR_P,XCHAR_P,XCHAR_P,XCHAR_P,BOOLEAN_P,d_level *));
  18. STATIC_DCL void NDECL(fixup_special);
  19. STATIC_DCL void FDECL(move, (int *,int *,int));
  20. STATIC_DCL void NDECL(setup_waterlevel);
  21. STATIC_DCL void NDECL(unsetup_waterlevel);

iswall[edit]

  1. STATIC_OVL boolean
  2. iswall(x,y)
  3. int x,y;
  4. {
  5. register int type;
  6.  
  7. if (!isok(x,y)) return FALSE;
  8. type = levl[x][y].typ;
  9. return (IS_WALL(type) || IS_DOOR(type) ||
  10. 	    type == SDOOR || type == IRONBARS);
  11. }

iswall_or_stone[edit]

  1. STATIC_OVL boolean
  2. iswall_or_stone(x,y)
  3. int x,y;
  4. {
  5. register int type;
  6.  
  7. /* out of bounds = stone */
  8. if (!isok(x,y)) return TRUE;
  9.  
  10. type = levl[x][y].typ;
  11. return (type == STONE || IS_WALL(type) || IS_DOOR(type) ||
  12. 	    type == SDOOR || type == IRONBARS);
  13. }

is_solid[edit]

  1. /* return TRUE if out of bounds, wall or rock */
  2. STATIC_OVL boolean
  3. is_solid(x,y)
  4. int x, y;
  5. {
  6. return (!isok(x,y) || IS_STWALL(levl[x][y].typ));
  7. }

extend_spine[edit]

  1. /*
  2. * Return 1 (not TRUE - we're doing bit vectors here) if we want to extend
  3. * a wall spine in the (dx,dy) direction.  Return 0 otherwise.
  4. *
  5. * To extend a wall spine in that direction, first there must be a wall there.
  6. * Then, extend a spine unless the current position is surrounded by walls
  7. * in the direction given by (dx,dy).  E.g. if 'x' is our location, 'W'
  8. * a wall, '.' a room, 'a' anything (we don't care), and our direction is
  9. * (0,1) - South or down - then:
  10. *
  11. *		a a a
  12. *		W x W		This would not extend a spine from x down
  13. *		W W W		(a corridor of walls is formed).
  14. *
  15. *		a a a
  16. *		W x W		This would extend a spine from x down.
  17. *		. W W
  18. */
  19. STATIC_OVL int
  20. extend_spine(locale, wall_there, dx, dy)
  21. int locale[3][3];
  22. int wall_there, dx, dy;
  23. {
  24. int spine, nx, ny;
  25.  
  26. nx = 1 + dx;
  27. ny = 1 + dy;
  28.  
  29. if (wall_there) {	/* wall in that direction */
  30. 	if (dx) {
  31. 	    if (locale[ 1][0] && locale[ 1][2] && /* EW are wall/stone */
  32. 		locale[nx][0] && locale[nx][2]) { /* diag are wall/stone */
  33. 		spine = 0;
  34. 	    } else {
  35. 		spine = 1;
  36. 	    }
  37. 	} else {	/* dy */
  38. 	    if (locale[0][ 1] && locale[2][ 1] && /* NS are wall/stone */
  39. 		locale[0][ny] && locale[2][ny]) { /* diag are wall/stone */
  40. 		spine = 0;
  41. 	    } else {
  42. 		spine = 1;
  43. 	    }
  44. 	}
  45. } else {
  46. 	spine = 0;
  47. }
  48.  
  49. return spine;
  50. }

wallification[edit]

  1. /*
  2. * Wall cleanup.  This function has two purposes: (1) remove walls that
  3. * are totally surrounded by stone - they are redundant.  (2) correct
  4. * the types so that they extend and connect to each other.
  5. */
  6. void
  7. wallification(x1, y1, x2, y2)
  8. int x1, y1, x2, y2;
  9. {
  10. 	uchar type;
  11. 	register int x,y;
  12. 	struct rm *lev;
  13. 	int bits;
  14. 	int locale[3][3];	/* rock or wall status surrounding positions */
  15. 	/*
  16. 	 * Value 0 represents a free-standing wall.  It could be anything,
  17. 	 * so even though this table says VWALL, we actually leave whatever
  18. 	 * typ was there alone.
  19. 	 */
  20. 	static xchar spine_array[16] = {
  21. 	    VWALL,	HWALL,		HWALL,		HWALL,
  22. 	    VWALL,	TRCORNER,	TLCORNER,	TDWALL,
  23. 	    VWALL,	BRCORNER,	BLCORNER,	TUWALL,
  24. 	    VWALL,	TLWALL,		TRWALL,		CROSSWALL
  25. 	};
  26.  
  27. 	/* sanity check on incoming variables */
  28. 	if (x1<0 || x2>=COLNO || x1>x2 || y1<0 || y2>=ROWNO || y1>y2)
  29. 	    panic("wallification: bad bounds (%d,%d) to (%d,%d)",x1,y1,x2,y2);
  30.  
  31. 	/* Step 1: change walls surrounded by rock to rock. */
  32. 	for(x = x1; x <= x2; x++)
  33. 	    for(y = y1; y <= y2; y++) {
  34. 		lev = &levl[x][y];
  35. 		type = lev->typ;
  36. 		if (IS_WALL(type) && type != DBWALL) {
  37. 		    if (is_solid(x-1,y-1) &&
  38. 			is_solid(x-1,y  ) &&
  39. 			is_solid(x-1,y+1) &&
  40. 			is_solid(x,  y-1) &&
  41. 			is_solid(x,  y+1) &&
  42. 			is_solid(x+1,y-1) &&
  43. 			is_solid(x+1,y  ) &&
  44. 			is_solid(x+1,y+1))
  45. 		    lev->typ = STONE;
  46. 		}
  47. 	    }
  48.  
  49. 	/*
  50. 	 * Step 2: set the correct wall type.  We can't combine steps
  51. 	 * 1 and 2 into a single sweep because we depend on knowing if
  52. 	 * the surrounding positions are stone.
  53. 	 */
  54. 	for(x = x1; x <= x2; x++)
  55. 	    for(y = y1; y <= y2; y++) {
  56. 		lev = &levl[x][y];
  57. 		type = lev->typ;
  58. 		if ( !(IS_WALL(type) && type != DBWALL)) continue;
  59.  
  60. 		/* set the locations TRUE if rock or wall or out of bounds */
  61. 		locale[0][0] = iswall_or_stone(x-1,y-1);
  62. 		locale[1][0] = iswall_or_stone(  x,y-1);
  63. 		locale[2][0] = iswall_or_stone(x+1,y-1);
  64.  
  65. 		locale[0][1] = iswall_or_stone(x-1,  y);
  66. 		locale[2][1] = iswall_or_stone(x+1,  y);
  67.  
  68. 		locale[0][2] = iswall_or_stone(x-1,y+1);
  69. 		locale[1][2] = iswall_or_stone(  x,y+1);
  70. 		locale[2][2] = iswall_or_stone(x+1,y+1);
  71.  
  72. 		/* determine if wall should extend to each direction NSEW */
  73. 		bits =    (extend_spine(locale, iswall(x,y-1),  0, -1) << 3)
  74. 			| (extend_spine(locale, iswall(x,y+1),  0,  1) << 2)
  75. 			| (extend_spine(locale, iswall(x+1,y),  1,  0) << 1)
  76. 			|  extend_spine(locale, iswall(x-1,y), -1,  0);
  77.  
  78. 		/* don't change typ if wall is free-standing */
  79. 		if (bits) lev->typ = spine_array[bits];
  80. 	    }
  81. }

okay[edit]

Helper function for walkfrom(). Checks to see if the square two spaces from the given location in the given direction is within map bounds and is currently solid stone. Returns TRUE if so, otherwise FALSE. Note that this function does not check the contents of the square one space in the given direction.

  1. STATIC_OVL boolean
  2. okay(x,y,dir)
  3. int x,y;
  4. register int dir;
  5. {
  6. 	move(&x,&y,dir);
  7. 	move(&x,&y,dir);
  8. 	if(x<3 || y<3 || x>x_maze_max || y>y_maze_max || levl[x][y].typ != 0)
  9. 		return(FALSE);
  10. 	return(TRUE);
  11. }

maze0xy[edit]

Chooses a random starting point for maze generation. Always picks a point with odd (never even) coordinates with x in [3 .. x_maze_max-1], y in [3 .. y_maze_max-1].

  1. STATIC_OVL void
  2. maze0xy(cc)	/* find random starting point for maze generation */
  3. 	coord	*cc;
  4. {
  5. 	cc->x = 3 + 2*rn2((x_maze_max>>1) - 1);
  6. 	cc->y = 3 + 2*rn2((y_maze_max>>1) - 1);
  7. 	return;
  8. }

bad_location[edit]

  1. /*
  2. * Bad if:
  3. *	pos is occupied OR
  4. *	pos is inside restricted region (lx,ly,hx,hy) OR
  5. *	NOT (pos is corridor and a maze level OR pos is a room OR pos is air)
  6. */
  7. boolean
  8. bad_location(x, y, lx, ly, hx, hy)
  9. xchar x, y;
  10. xchar lx, ly, hx, hy;
  11. {
  12. return((boolean)(occupied(x, y) ||
  13. 	   within_bounded_area(x,y, lx,ly, hx,hy) ||
  14. 	   !((levl[x][y].typ == CORR && level.flags.is_maze_lev) ||
  15. 	       levl[x][y].typ == ROOM || levl[x][y].typ == AIR)));
  16. }

place_lregion[edit]

  1. /* pick a location in area (lx, ly, hx, hy) but not in (nlx, nly, nhx, nhy) */
  2. /* and place something (based on rtype) in that region */
  3. void
  4. place_lregion(lx, ly, hx, hy, nlx, nly, nhx, nhy, rtype, lev)
  5. xchar	lx, ly, hx, hy;
  6. xchar	nlx, nly, nhx, nhy;
  7. xchar	rtype;
  8. d_level	*lev;
  9. {
  10. int trycnt;
  11. boolean oneshot;
  12. xchar x, y;
  13.  
  14. if(!lx) { /* default to whole level */
  15. 	/*
  16. 	 * if there are rooms and this a branch, let place_branch choose
  17. 	 * the branch location (to avoid putting branches in corridors).
  18. 	 */
  19. 	if(rtype == LR_BRANCH && nroom) {
  20. 	    place_branch(Is_branchlev(&u.uz), 0, 0);
  21. 	    return;
  22. 	}
  23.  
  24. 	lx = 1; hx = COLNO-1;
  25. 	ly = 1; hy = ROWNO-1;
  26. }
  27.  
  28. /* first a probabilistic approach */
  29.  
  30. oneshot = (lx == hx && ly == hy);
  31. for (trycnt = 0; trycnt < 200; trycnt++) {
  32. 	x = rn1((hx - lx) + 1, lx);
  33. 	y = rn1((hy - ly) + 1, ly);
  34. 	if (put_lregion_here(x,y,nlx,nly,nhx,nhy,rtype,oneshot,lev))
  35. 	    return;
  36. }
  37.  
  38. /* then a deterministic one */
  39.  
  40. oneshot = TRUE;
  41. for (x = lx; x <= hx; x++)
  42. 	for (y = ly; y <= hy; y++)
  43. 	    if (put_lregion_here(x,y,nlx,nly,nhx,nhy,rtype,oneshot,lev))
  44. 		return;
  45.  
  46. impossible("Couldn't place lregion type %d!", rtype);
  47. }

put_lregion_here[edit]

  1. STATIC_OVL boolean
  2. put_lregion_here(x,y,nlx,nly,nhx,nhy,rtype,oneshot,lev)
  3. xchar x, y;
  4. xchar nlx, nly, nhx, nhy;
  5. xchar rtype;
  6. boolean oneshot;
  7. d_level *lev;
  8. {
  9. if (bad_location(x, y, nlx, nly, nhx, nhy)) {
  10. 	if (!oneshot) {
  11. 	    return FALSE;		/* caller should try again */
  12. 	} else {
  13. 	    /* Must make do with the only location possible;
  14. 	       avoid failure due to a misplaced trap.
  15. 	       It might still fail if there's a dungeon feature here. */
  16. 	    struct trap *t = t_at(x,y);
  17.  
  18. 	    if (t && t->ttyp != MAGIC_PORTAL) deltrap(t);
  19. 	    if (bad_location(x, y, nlx, nly, nhx, nhy)) return FALSE;
  20. 	}
  21. }
  22. switch (rtype) {
  23. case LR_TELE:
  24. case LR_UPTELE:
  25. case LR_DOWNTELE:
  26. 	/* "something" means the player in this case */
  27. 	if(MON_AT(x, y)) {
  28. 	    /* move the monster if no choice, or just try again */
  29. 	    if(oneshot) (void) rloc(m_at(x,y), FALSE);
  30. 	    else return(FALSE);
  31. 	}
  32. 	u_on_newpos(x, y);
  33. 	break;
  34. case LR_PORTAL:
  35. 	mkportal(x, y, lev->dnum, lev->dlevel);
  36. 	break;
  37. case LR_DOWNSTAIR:
  38. case LR_UPSTAIR:
  39. 	mkstairs(x, y, (char)rtype, (struct mkroom *)0);
  40. 	break;
  41. case LR_BRANCH:
  42. 	place_branch(Is_branchlev(&u.uz), x, y);
  43. 	break;
  44. }
  45. return(TRUE);
  46. }
  47.  
  48. static boolean was_waterlevel; /* ugh... this shouldn't be needed */

fixup_special[edit]

  1. /* this is special stuff that the level compiler cannot (yet) handle */
  2. STATIC_OVL void
  3. fixup_special()
  4. {
  5. register lev_region *r = lregions;
  6. struct d_level lev;
  7. register int x, y;
  8. struct mkroom *croom;
  9. boolean added_branch = FALSE;
  10.  
  11. if (was_waterlevel) {
  12. 	was_waterlevel = FALSE;
  13. 	u.uinwater = 0;
  14. 	unsetup_waterlevel();
  15. } else if (Is_waterlevel(&u.uz)) {
  16. 	level.flags.hero_memory = 0;
  17. 	was_waterlevel = TRUE;
  18. 	/* water level is an odd beast - it has to be set up
  19. 	   before calling place_lregions etc. */
  20. 	setup_waterlevel();
  21. }
  22. for(x = 0; x < num_lregions; x++, r++) {
  23. 	switch(r->rtype) {
  24. 	case LR_BRANCH:
  25. 	    added_branch = TRUE;
  26. 	    goto place_it;
  27.  
  28. 	case LR_PORTAL:
  29. 	    if(*r->rname.str >= '0' && *r->rname.str <= '9') {
  30. 		/* "chutes and ladders" */
  31. 		lev = u.uz;
  32. 		lev.dlevel = atoi(r->rname.str);
  33. 	    } else {
  34. 		s_level *sp = find_level(r->rname.str);
  35. 		lev = sp->dlevel;
  36. 	    }
  37. 	    /* fall into... */
  38.  
  39. 	case LR_UPSTAIR:
  40. 	case LR_DOWNSTAIR:
  41. 	place_it:
  42. 	    place_lregion(r->inarea.x1, r->inarea.y1,
  43. 			  r->inarea.x2, r->inarea.y2,
  44. 			  r->delarea.x1, r->delarea.y1,
  45. 			  r->delarea.x2, r->delarea.y2,
  46. 			  r->rtype, &lev);
  47. 	    break;
  48.  
  49. 	case LR_TELE:
  50. 	case LR_UPTELE:
  51. 	case LR_DOWNTELE:
  52. 	    /* save the region outlines for goto_level() */
  53. 	    if(r->rtype == LR_TELE || r->rtype == LR_UPTELE) {
  54. 		    updest.lx = r->inarea.x1; updest.ly = r->inarea.y1;
  55. 		    updest.hx = r->inarea.x2; updest.hy = r->inarea.y2;
  56. 		    updest.nlx = r->delarea.x1; updest.nly = r->delarea.y1;
  57. 		    updest.nhx = r->delarea.x2; updest.nhy = r->delarea.y2;
  58. 	    }
  59. 	    if(r->rtype == LR_TELE || r->rtype == LR_DOWNTELE) {
  60. 		    dndest.lx = r->inarea.x1; dndest.ly = r->inarea.y1;
  61. 		    dndest.hx = r->inarea.x2; dndest.hy = r->inarea.y2;
  62. 		    dndest.nlx = r->delarea.x1; dndest.nly = r->delarea.y1;
  63. 		    dndest.nhx = r->delarea.x2; dndest.nhy = r->delarea.y2;
  64. 	    }
  65. 	    /* place_lregion gets called from goto_level() */
  66. 	    break;
  67. 	}
  68.  
  69. 	if (r->rname.str) free((genericptr_t) r->rname.str),  r->rname.str = 0;
  70. }
  71.  
  72. /* place dungeon branch if not placed above */
  73. if (!added_branch && Is_branchlev(&u.uz)) {
  74. 	place_lregion(0,0,0,0,0,0,0,0,LR_BRANCH,(d_level *)0);
  75. }
  76.  
  77. 	/* KMH -- Sokoban levels */
  78. 	if(In_sokoban(&u.uz))
  79. 		sokoban_detect();
  80.  
  81. /* Still need to add some stuff to level file */
  82. if (Is_medusa_level(&u.uz)) {
  83. 	struct obj *otmp;
  84. 	int tryct;
  85.  
  86. 	croom = &rooms[0]; /* only one room on the medusa level */
  87. 	for (tryct = rnd(4); tryct; tryct--) {
  88. 	    x = somex(croom); y = somey(croom);
  89. 	    if (goodpos(x, y, (struct monst *)0, 0)) {
  90. 		otmp = mk_tt_object(STATUE, x, y);
  91. 		while (otmp && (poly_when_stoned(&mons[otmp->corpsenm]) ||
  92. 				pm_resistance(&mons[otmp->corpsenm],MR_STONE))) {
  93. 		    otmp->corpsenm = rndmonnum();
  94. 		    otmp->owt = weight(otmp);
  95. 		}
  96. 	    }
  97. 	}
  98.  
  99. 	if (rn2(2))
  100. 	    otmp = mk_tt_object(STATUE, somex(croom), somey(croom));
  101. 	else /* Medusa statues don't contain books */
  102. 	    otmp = mkcorpstat(STATUE, (struct monst *)0, (struct permonst *)0,
  103. 			      somex(croom), somey(croom), FALSE);
  104. 	if (otmp) {
  105. 	    while (pm_resistance(&mons[otmp->corpsenm],MR_STONE)
  106. 		   || poly_when_stoned(&mons[otmp->corpsenm])) {
  107. 		otmp->corpsenm = rndmonnum();
  108. 		otmp->owt = weight(otmp);
  109. 	    }
  110. 	}
  111. } else if(Is_wiz1_level(&u.uz)) {
  112. 	croom = search_special(MORGUE);
  113.  
  114. 	create_secret_door(croom, W_SOUTH|W_EAST|W_WEST);
  115. } else if(Is_knox(&u.uz)) {
  116. 	/* using an unfilled morgue for rm id */
  117. 	croom = search_special(MORGUE);
  118. 	/* avoid inappropriate morgue-related messages */
  119. 	level.flags.graveyard = level.flags.has_morgue = 0;
  120. 	croom->rtype = OROOM;	/* perhaps it should be set to VAULT? */
  121. 	/* stock the main vault */
  122. 	for(x = croom->lx; x <= croom->hx; x++)
  123. 	    for(y = croom->ly; y <= croom->hy; y++) {
  124. 		(void) mkgold((long) rn1(300, 600), x, y);
  125. 		if (!rn2(3) && !is_pool(x,y))
  126. 		    (void)maketrap(x, y, rn2(3) ? LANDMINE : SPIKED_PIT);
  127. 	    }
  128. } else if (Role_if(PM_PRIEST) && In_quest(&u.uz)) {
  129. 	/* less chance for undead corpses (lured from lower morgues) */
  130. 	level.flags.graveyard = 1;
  131. } else if (Is_stronghold(&u.uz)) {
  132. 	level.flags.graveyard = 1;
  133. } else if(Is_sanctum(&u.uz)) {
  134. 	croom = search_special(TEMPLE);
  135.  
  136. 	create_secret_door(croom, W_ANY);
  137. } else if(on_level(&u.uz, &orcus_level)) {
  138. 	   register struct monst *mtmp, *mtmp2;
  139.  
  140. 	   /* it's a ghost town, get rid of shopkeepers */
  141. 	    for(mtmp = fmon; mtmp; mtmp = mtmp2) {
  142. 		    mtmp2 = mtmp->nmon;
  143. 		    if(mtmp->isshk) mongone(mtmp);
  144. 	    }
  145. }
  146.  
  147. if(lev_message) {
  148. 	char *str, *nl;
  149. 	for(str = lev_message; (nl = index(str, '\n')) != 0; str = nl+1) {
  150. 	    *nl = '\0';
  151. 	    pline("%s", str);
  152. 	}
  153. 	if(*str)
  154. 	    pline("%s", str);
  155. 	free((genericptr_t)lev_message);
  156. 	lev_message = 0;
  157. }
  158.  
  159. if (lregions)
  160. 	free((genericptr_t) lregions),  lregions = 0;
  161. num_lregions = 0;
  162. }

makemaz[edit]

Function to create a special level with the given name, or else (special level not found or name not specified) a level filled entirely with maze (a la Gehennom). Called from makelevel() in mklev.c

  1. void
  2. makemaz(s)
  3. register const char *s;
  4. {
  5. 	int x,y;
  6. 	char protofile[20];
  7. 	s_level	*sp = Is_special(&u.uz);
  8. 	coord mm;
  9.  
  10. 	if(*s) {
  11. 	    if(sp && sp->rndlevs) Sprintf(protofile, "%s-%d", s,
  12. 						rnd((int) sp->rndlevs));
  13. 	    else		 Strcpy(protofile, s);
  14. 	} else if(*(dungeons[u.uz.dnum].proto)) {
  15. 	    if(dunlevs_in_dungeon(&u.uz) > 1) {
  16. 		if(sp && sp->rndlevs)
  17. 		     Sprintf(protofile, "%s%d-%d", dungeons[u.uz.dnum].proto,
  18. 						dunlev(&u.uz),
  19. 						rnd((int) sp->rndlevs));
  20. 		else Sprintf(protofile, "%s%d", dungeons[u.uz.dnum].proto,
  21. 						dunlev(&u.uz));
  22. 	    } else if(sp && sp->rndlevs) {
  23. 		     Sprintf(protofile, "%s-%d", dungeons[u.uz.dnum].proto,
  24. 						rnd((int) sp->rndlevs));
  25. 	    } else Strcpy(protofile, dungeons[u.uz.dnum].proto);
  26.  
  27. 	} else Strcpy(protofile, "");
  28.  
  29. #ifdef WIZARD
  30. 	/* SPLEVTYPE format is "level-choice,level-choice"... */
  31. 	if (wizard && *protofile && sp && sp->rndlevs) {
  32. 	    char *ep = getenv("SPLEVTYPE");	/* not nh_getenv */
  33. 	    if (ep) {
  34. 		/* rindex always succeeds due to code in prior block */
  35. 		int len = (rindex(protofile, '-') - protofile) + 1;
  36.  
  37. 		while (ep && *ep) {
  38. 		    if (!strncmp(ep, protofile, len)) {
  39. 			int pick = atoi(ep + len);
  40. 			/* use choice only if valid */
  41. 			if (pick > 0 && pick <= (int) sp->rndlevs)
  42. 			    Sprintf(protofile + len, "%d", pick);
  43. 			break;
  44. 		    } else {
  45. 			ep = index(ep, ',');
  46. 			if (ep) ++ep;
  47. 		    }
  48. 		}
  49. 	    }
  50. 	}
  51. #endif
  52.  
  53. 	if(*protofile) {

Create the specified special level and return if successful.

  1. 	    Strcat(protofile, LEV_EXT);
  2. 	    if(load_special(protofile)) {
  3. 		fixup_special();
  4. 		/* some levels can end up with monsters
  5. 		   on dead mon list, including light source monsters */
  6. 		dmonsfree();
  7. 		return;	/* no mazification right now */
  8. 	    }
  9. 	    impossible("Couldn't load \"%s\" - making a maze.", protofile);
  10. 	}

Either this isn't a special level, or special level loading/creation failed - it's maze time!

  1. 	level.flags.is_maze_lev = TRUE;

Fill the map with stone/walls

  1. #ifndef WALLIFIED_MAZE
  2. 	for(x = 2; x < x_maze_max; x++)
  3. 		for(y = 2; y < y_maze_max; y++)
  4. 			levl[x][y].typ = STONE;
  5. #else
  6. 	for(x = 2; x <= x_maze_max; x++)
  7. 		for(y = 2; y <= y_maze_max; y++)
  8. 			levl[x][y].typ = ((x % 2) && (y % 2)) ? STONE : HWALL;
  9. #endif

Choose a random starting point for the maze.

  1. 	maze0xy(&mm);

Carve out maze corridors branching out from this point.

  1. 	walkfrom((int) mm.x, (int) mm.y);
  2. 	/* put a boulder at the maze center */
  3. 	(void) mksobj_at(BOULDER, (int) mm.x, (int) mm.y, TRUE, FALSE);
  4.  
  5. #ifdef WALLIFIED_MAZE

Fix up the walls so they are nicely connected:

  1. 	wallification(2, 2, x_maze_max, y_maze_max);
  2. #endif

Choose a random valid point in the maze and place the upstairs.

  1. 	mazexy(&mm);
  2. 	mkstairs(mm.x, mm.y, 1, (struct mkroom *)0);		/* up */
  3. 	if (!Invocation_lev(&u.uz)) {

Choose another random valid point in the maze and place the downstairs.

  1. 	    mazexy(&mm);
  2. 	    mkstairs(mm.x, mm.y, 0, (struct mkroom *)0);	/* down */
  3. 	} else {	/* choose "vibrating square" location */
  4. #define x_maze_min 2
  5. #define y_maze_min 2
  6. 	    /*
  7. 	     * Pick a position where the stairs down to Moloch's Sanctum
  8. 	     * level will ultimately be created.  At that time, an area
  9. 	     * will be altered:  walls removed, moat and traps generated,
  10. 	     * boulders destroyed.  The position picked here must ensure
  11. 	     * that that invocation area won't extend off the map.
  12. 	     *
  13. 	     * We actually allow up to 2 squares around the usual edge of
  14. 	     * the area to get truncated; see mkinvokearea(mklev.c).
  15. 	     */
  16. #define INVPOS_X_MARGIN (6 - 2)
  17. #define INVPOS_Y_MARGIN (5 - 2)
  18. #define INVPOS_DISTANCE 11
  19. 	    int x_range = x_maze_max - x_maze_min - 2*INVPOS_X_MARGIN - 1,
  20. 		y_range = y_maze_max - y_maze_min - 2*INVPOS_Y_MARGIN - 1;
  21.  
  22. #ifdef DEBUG
  23. 	    if (x_range <= INVPOS_X_MARGIN || y_range <= INVPOS_Y_MARGIN ||
  24. 		   (x_range * y_range) <= (INVPOS_DISTANCE * INVPOS_DISTANCE))
  25. 		panic("inv_pos: maze is too small! (%d x %d)",
  26. 		      x_maze_max, y_maze_max);
  27. #endif
  28. 	    inv_pos.x = inv_pos.y = 0; /*{occupied() => invocation_pos()}*/
  29. 	    do {
  30. 		x = rn1(x_range, x_maze_min + INVPOS_X_MARGIN + 1);
  31. 		y = rn1(y_range, y_maze_min + INVPOS_Y_MARGIN + 1);
  32. 		/* we don't want it to be too near the stairs, nor
  33. 		   to be on a spot that's already in use (wall|trap) */
  34. 	    } while (x == xupstair || y == yupstair ||	/*(direct line)*/
  35. 		     abs(x - xupstair) == abs(y - yupstair) ||
  36. 		     distmin(x, y, xupstair, yupstair) <= INVPOS_DISTANCE ||
  37. 		     !SPACE_POS(levl[x][y].typ) || occupied(x, y));
  38. 	    inv_pos.x = x;
  39. 	    inv_pos.y = y;
  40. #undef INVPOS_X_MARGIN
  41. #undef INVPOS_Y_MARGIN
  42. #undef INVPOS_DISTANCE
  43. #undef x_maze_min
  44. #undef y_maze_min
  45. 	}
  46.  
  47. 	/* place branch stair or portal */
  48. 	place_branch(Is_branchlev(&u.uz), 0, 0);

Generate random maze-level contents:

  • 11-18 random gems
  • 2-11 boulders
  • 0-2 minotaurs
  • 7-11 other random monsters
  • 7-12 piles of gold
  • 7-12 random traps
  1. 	for(x = rn1(8,11); x; x--) {
  2. 		mazexy(&mm);
  3. 		(void) mkobj_at(rn2(2) ? GEM_CLASS : 0, mm.x, mm.y, TRUE);
  4. 	}
  5. 	for(x = rn1(10,2); x; x--) {
  6. 		mazexy(&mm);
  7. 		(void) mksobj_at(BOULDER, mm.x, mm.y, TRUE, FALSE);
  8. 	}
  9. 	for (x = rn2(3); x; x--) {
  10. 		mazexy(&mm);
  11. 		(void) makemon(&mons[PM_MINOTAUR], mm.x, mm.y, NO_MM_FLAGS);
  12. 	}
  13. 	for(x = rn1(5,7); x; x--) {
  14. 		mazexy(&mm);
  15. 		(void) makemon((struct permonst *) 0, mm.x, mm.y, NO_MM_FLAGS);
  16. 	}
  17. 	for(x = rn1(6,7); x; x--) {
  18. 		mazexy(&mm);
  19. 		(void) mkgold(0L,mm.x,mm.y);
  20. 	}
  21. 	for(x = rn1(6,7); x; x--)
  22. 		mktrap(0,1,(struct mkroom *) 0, (coord*) 0);
  23. }

walkfrom[edit]

Function that recursively/iteratively carves out maze tunnels until no more tunnels can be carved. General approach is to random-walk a single corridor from the starting point until reaching a dead-end, then back up until reaching a square where a new branch could be carved out and start a new corridor there, repeating until no new tunnels can be carved.

Exists in two versions - an iterative implementation for limited-stack systems, and a recursive implementation for others.

  1. #ifdef MICRO
  2. /* Make the mazewalk iterative by faking a stack.  This is needed to
  3. * ensure the mazewalk is successful in the limited stack space of
  4. * the program.  This iterative version uses the minimum amount of stack
  5. * that is totally safe.
  6. */
  7. void
  8. walkfrom(x,y)
  9. int x,y;
  10. {
  11. #define CELLS (ROWNO * COLNO) / 4		/* a maze cell is 4 squares */
  12. 	char mazex[CELLS + 1], mazey[CELLS + 1];	/* char's are OK */
  13. 	int q, a, dir, pos;
  14. 	int dirs[4];

"pos" is the depth of the simulated stack, and "mazex" and "mazey" store the x/y position at this level of the stack.

  1. 	pos = 1;
  2. 	mazex[pos] = (char) x;
  3. 	mazey[pos] = (char) y;
  4. 	while (pos) {
  5. 		x = (int) mazex[pos];
  6. 		y = (int) mazey[pos];
  7. 		if(!IS_DOOR(levl[x][y].typ)) {
  8. 		    /* might still be on edge of MAP, so don't overwrite */
  9. #ifndef WALLIFIED_MAZE
  10. 		    levl[x][y].typ = CORR;
  11. #else
  12. 		    levl[x][y].typ = ROOM;
  13. #endif
  14. 		    levl[x][y].flags = 0;
  15. 		}

Count and record the possible directions that a tunnel can go from this point.

  1. 		q = 0;
  2. 		for (a = 0; a < 4; a++)
  3. 			if(okay(x, y, a)) dirs[q++]= a;

If no valid directions from this point, move up one level of the stack (back along the maze toward the origin point).

  1. 		if (!q)
  2. 			pos--;
  3. 		else {

Otherwise, pick a random valid direction and carve two squares in that direction.

  1. 			dir = dirs[rn2(q)];
  2. 			move(&x, &y, dir);
  3. #ifndef WALLIFIED_MAZE
  4. 			levl[x][y].typ = CORR;
  5. #else
  6. 			levl[x][y].typ = ROOM;
  7. #endif
  8. 			move(&x, &y, dir);

Move one level deeper into the simulated stack (continue the current tunnel).

  1. 			pos++;
  2. 			if (pos > CELLS)
  3. 				panic("Overflow in walkfrom");
  4. 			mazex[pos] = (char) x;
  5. 			mazey[pos] = (char) y;
  6. 		}
  7. 	}
  8. }
  9. #else

Recursive version for systems with more reasonable stack space.

  1. void
  2. walkfrom(x,y)
  3. int x,y;
  4. {
  5. 	register int q,a,dir;
  6. 	int dirs[4];
  7.  
  8. 	if(!IS_DOOR(levl[x][y].typ)) {
  9. 	    /* might still be on edge of MAP, so don't overwrite */
  10. #ifndef WALLIFIED_MAZE
  11. 	    levl[x][y].typ = CORR;
  12. #else
  13. 	    levl[x][y].typ = ROOM;
  14. #endif
  15. 	    levl[x][y].flags = 0;
  16. 	}
  17.  
  18. 	while(1) {

Count and record the possible directions that a tunnel can go from this point.

  1. 		q = 0;
  2. 		for(a = 0; a < 4; a++)
  3. 			if(okay(x,y,a)) dirs[q++]= a;

If no valid directions, return (tunnel ends here, and we will back up to the last valid branch point if any).

  1. 		if(!q) return;

Otherwise, pick a random valid direction and carve two squares in that direction.

  1. 		dir = dirs[rn2(q)];
  2. 		move(&x,&y,dir);
  3. #ifndef WALLIFIED_MAZE
  4. 		levl[x][y].typ = CORR;
  5. #else
  6. 		levl[x][y].typ = ROOM;
  7. #endif
  8. 		move(&x,&y,dir);

Recurse at the new point.

  1. 		walkfrom(x,y);
  2. 	}
  3. }
  4. #endif /* MICRO */

move[edit]

  1. STATIC_OVL void
  2. move(x,y,dir)
  3. register int *x, *y;
  4. register int dir;
  5. {
  6. 	switch(dir){
  7. 		case 0: --(*y); break;
  8. 		case 1: (*x)++; break;
  9. 		case 2: (*y)++; break;
  10. 		case 3: --(*x); break;
  11. 		default: panic("move: bad direction");
  12. 	}
  13. }

mazexy[edit]

  1. void
  2. mazexy(cc)	/* find random point in generated corridors,
  3. 		   so we don't create items in moats, bunkers, or walls */
  4. 	coord	*cc;
  5. {
  6. 	int cpt=0;
  7.  
  8. 	do {

Pick and test a random point with odd-numbered coordinates (never even-numbered). These correspond to maze corners/intersections.

  1. 	    cc->x = 3 + 2*rn2((x_maze_max>>1) - 1);
  2. 	    cc->y = 3 + 2*rn2((y_maze_max>>1) - 1);
  3. 	    cpt++;
  4. 	} while (cpt < 100 && levl[cc->x][cc->y].typ !=
  5. #ifdef WALLIFIED_MAZE
  6. 		 ROOM
  7. #else
  8. 		 CORR
  9. #endif
  10. 		);

If we tried 100 times to pick a random point and failed, brute-force it by testing every possible odd-coordinate point in the maze.

  1. 	if (cpt >= 100) {
  2. 		register int x, y;
  3. 		/* last try */
  4. 		for (x = 0; x < (x_maze_max>>1) - 1; x++)
  5. 		    for (y = 0; y < (y_maze_max>>1) - 1; y++) {
  6. 			cc->x = 3 + 2 * x;
  7. 			cc->y = 3 + 2 * y;
  8. 			if (levl[cc->x][cc->y].typ ==
  9. #ifdef WALLIFIED_MAZE
  10. 			    ROOM
  11. #else
  12. 			    CORR
  13. #endif
  14. 			   ) return;
  15. 		    }
  16. 		panic("mazexy: can't find a place!");
  17. 	}
  18. 	return;
  19. }

bound_digging[edit]

  1. void
  2. bound_digging()
  3. /* put a non-diggable boundary around the initial portion of a level map.
  4. * assumes that no level will initially put things beyond the isok() range.
  5. *
  6. * we can't bound unconditionally on the last line with something in it,
  7. * because that something might be a niche which was already reachable,
  8. * so the boundary would be breached
  9. *
  10. * we can't bound unconditionally on one beyond the last line, because
  11. * that provides a window of abuse for WALLIFIED_MAZE special levels
  12. */
  13. {
  14. 	register int x,y;
  15. 	register unsigned typ;
  16. 	register struct rm *lev;
  17. 	boolean found, nonwall;
  18. 	int xmin,xmax,ymin,ymax;
  19.  
  20. 	if(Is_earthlevel(&u.uz)) return; /* everything diggable here */

Starting at the left edge of the level, find the first column (xmin) that contains something that isn't solid rock.

  1. 	found = nonwall = FALSE;
  2. 	for(xmin=0; !found; xmin++) {
  3. 		lev = &levl[xmin][0];
  4. 		for(y=0; y<=ROWNO-1; y++, lev++) {
  5. 			typ = lev->typ;
  6. 			if(typ != STONE) {
  7. 				found = TRUE;
  8. 				if(!IS_WALL(typ)) nonwall = TRUE;
  9. 			}
  10. 		}
  11. 	}

If the column contained anything that isn't a wall, or this is not a maze level, back up by two columns. Otherwise (maze level and found a wall) only back up by one.

  1. 	xmin -= (nonwall || !level.flags.is_maze_lev) ? 2 : 1;
  2. 	if (xmin < 0) xmin = 0;

Same as above, but for the right edge of the level.

  1. 	found = nonwall = FALSE;
  2. 	for(xmax=COLNO-1; !found; xmax--) {
  3. 		lev = &levl[xmax][0];
  4. 		for(y=0; y<=ROWNO-1; y++, lev++) {
  5. 			typ = lev->typ;
  6. 			if(typ != STONE) {
  7. 				found = TRUE;
  8. 				if(!IS_WALL(typ)) nonwall = TRUE;
  9. 			}
  10. 		}
  11. 	}
  12. 	xmax += (nonwall || !level.flags.is_maze_lev) ? 2 : 1;
  13. 	if (xmax >= COLNO) xmax = COLNO-1;

Same as above, but for the top edge of the level.

  1. 	found = nonwall = FALSE;
  2. 	for(ymin=0; !found; ymin++) {
  3. 		lev = &levl[xmin][ymin];
  4. 		for(x=xmin; x<=xmax; x++, lev += ROWNO) {
  5. 			typ = lev->typ;
  6. 			if(typ != STONE) {
  7. 				found = TRUE;
  8. 				if(!IS_WALL(typ)) nonwall = TRUE;
  9. 			}
  10. 		}
  11. 	}
  12. 	ymin -= (nonwall || !level.flags.is_maze_lev) ? 2 : 1;

(Why no check for "ymin < 0" here?)

Same as above, but for the bottom edge of the level.

  1. 	found = nonwall = FALSE;
  2. 	for(ymax=ROWNO-1; !found; ymax--) {
  3. 		lev = &levl[xmin][ymax];
  4. 		for(x=xmin; x<=xmax; x++, lev += ROWNO) {
  5. 			typ = lev->typ;
  6. 			if(typ != STONE) {
  7. 				found = TRUE;
  8. 				if(!IS_WALL(typ)) nonwall = TRUE;
  9. 			}
  10. 		}
  11. 	}
  12. 	ymax += (nonwall || !level.flags.is_maze_lev) ? 2 : 1;

(Why no check for "ymax > ROWNO" here?)

Mark everything from the bounding box outward as nondiggable.

  1. 	for (x = 0; x < COLNO; x++)
  2. 	  for (y = 0; y < ROWNO; y++)
  3. 	    if (y <= ymin || y >= ymax || x <= xmin || x >= xmax) {
  4. #ifdef DCC30_BUG
  5. 		lev = &levl[x][y];
  6. 		lev->wall_info |= W_NONDIGGABLE;
  7. #else
  8. 		levl[x][y].wall_info |= W_NONDIGGABLE;
  9. #endif
  10. 	    }
  11. }

mkportal[edit]

  1. void
  2. mkportal(x, y, todnum, todlevel)
  3. register xchar x, y, todnum, todlevel;
  4. {
  5. 	/* a portal "trap" must be matched by a */
  6. 	/* portal in the destination dungeon/dlevel */
  7. 	register struct trap *ttmp = maketrap(x, y, MAGIC_PORTAL);
  8.  
  9. 	if (!ttmp) {
  10. 		impossible("portal on top of portal??");
  11. 		return;
  12. 	}
  13. #ifdef DEBUG
  14. 	pline("mkportal: at (%d,%d), to %s, level %d",
  15. 		x, y, dungeons[todnum].dname, todlevel);
  16. #endif
  17. 	ttmp->dst.dnum = todnum;
  18. 	ttmp->dst.dlevel = todlevel;
  19. 	return;
  20. }
  21.  
  22. /*
  23. * Special waterlevel stuff in endgame (TH).
  24. *
  25. * Some of these functions would probably logically belong to some
  26. * other source files, but they are all so nicely encapsulated here.
  27. */
  28.  
  29. /* to ease the work of debuggers at this stage */
  30. #define register
  31.  
  32. #define CONS_OBJ   0
  33. #define CONS_MON   1
  34. #define CONS_HERO  2
  35. #define CONS_TRAP  3
  36.  
  37. static struct bubble *bbubbles, *ebubbles;
  38.  
  39. static struct trap *wportal;
  40. static int xmin, ymin, xmax, ymax;	/* level boundaries */
  41. /* bubble movement boundaries */
  42. #define bxmin (xmin + 1)
  43. #define bymin (ymin + 1)
  44. #define bxmax (xmax - 1)
  45. #define bymax (ymax - 1)
  46.  
  47. STATIC_DCL void NDECL(set_wportal);
  48. STATIC_DCL void FDECL(mk_bubble, (int,int,int));
  49. STATIC_DCL void FDECL(mv_bubble, (struct bubble *,int,int,BOOLEAN_P));

movebubbles[edit]

  1. void
  2. movebubbles()
  3. {
  4. 	static boolean up;
  5. 	register struct bubble *b;
  6. 	register int x, y, i, j;
  7. 	struct trap *btrap;
  8. 	static const struct rm water_pos =
  9. 		{ cmap_to_glyph(S_water), WATER, 0, 0, 0, 0, 0, 0, 0 };
  10.  
  11. 	/* set up the portal the first time bubbles are moved */
  12. 	if (!wportal) set_wportal();
  13.  
  14. 	vision_recalc(2);
  15. 	/* keep attached ball&chain separate from bubble objects */
  16. 	if (Punished) unplacebc();
  17.  
  18. 	/*
  19. 	 * Pick up everything inside of a bubble then fill all bubble
  20. 	 * locations.
  21. 	 */
  22.  
  23. 	for (b = up ? bbubbles : ebubbles; b; b = up ? b->next : b->prev) {
  24. 	    if (b->cons) panic("movebubbles: cons != null");
  25. 	    for (i = 0, x = b->x; i < (int) b->bm[0]; i++, x++)
  26. 		for (j = 0, y = b->y; j < (int) b->bm[1]; j++, y++)
  27. 		    if (b->bm[j + 2] & (1 << i)) {
  28. 			if (!isok(x,y)) {
  29. 			    impossible("movebubbles: bad pos (%d,%d)", x,y);
  30. 			    continue;
  31. 			}
  32.  
  33. 			/* pick up objects, monsters, hero, and traps */
  34. 			if (OBJ_AT(x,y)) {
  35. 			    struct obj *olist = (struct obj *) 0, *otmp;
  36. 			    struct container *cons = (struct container *)
  37. 				alloc(sizeof(struct container));
  38.  
  39. 			    while ((otmp = level.objects[x][y]) != 0) {
  40. 				remove_object(otmp);
  41. 				otmp->ox = otmp->oy = 0;
  42. 				otmp->nexthere = olist;
  43. 				olist = otmp;
  44. 			    }
  45.  
  46. 			    cons->x = x;
  47. 			    cons->y = y;
  48. 			    cons->what = CONS_OBJ;
  49. 			    cons->list = (genericptr_t) olist;
  50. 			    cons->next = b->cons;
  51. 			    b->cons = cons;
  52. 			}
  53. 			if (MON_AT(x,y)) {
  54. 			    struct monst *mon = m_at(x,y);
  55. 			    struct container *cons = (struct container *)
  56. 				alloc(sizeof(struct container));
  57.  
  58. 			    cons->x = x;
  59. 			    cons->y = y;
  60. 			    cons->what = CONS_MON;
  61. 			    cons->list = (genericptr_t) mon;
  62.  
  63. 			    cons->next = b->cons;
  64. 			    b->cons = cons;
  65.  
  66. 			    if(mon->wormno)
  67. 				remove_worm(mon);
  68. 			    else
  69. 				remove_monster(x, y);
  70.  
  71. 			    newsym(x,y);	/* clean up old position */
  72. 			    mon->mx = mon->my = 0;
  73. 			}
  74. 			if (!u.uswallow && x == u.ux && y == u.uy) {
  75. 			    struct container *cons = (struct container *)
  76. 				alloc(sizeof(struct container));
  77.  
  78. 			    cons->x = x;
  79. 			    cons->y = y;
  80. 			    cons->what = CONS_HERO;
  81. 			    cons->list = (genericptr_t) 0;
  82.  
  83. 			    cons->next = b->cons;
  84. 			    b->cons = cons;
  85. 			}
  86. 			if ((btrap = t_at(x,y)) != 0) {
  87. 			    struct container *cons = (struct container *)
  88. 				alloc(sizeof(struct container));
  89.  
  90. 			    cons->x = x;
  91. 			    cons->y = y;
  92. 			    cons->what = CONS_TRAP;
  93. 			    cons->list = (genericptr_t) btrap;
  94.  
  95. 			    cons->next = b->cons;
  96. 			    b->cons = cons;
  97. 			}
  98.  
  99. 			levl[x][y] = water_pos;
  100. 			block_point(x,y);
  101. 		    }
  102. 	}
  103.  
  104. 	/*
  105. 	 * Every second time traverse down.  This is because otherwise
  106. 	 * all the junk that changes owners when bubbles overlap
  107. 	 * would eventually end up in the last bubble in the chain.
  108. 	 */
  109.  
  110. 	up = !up;
  111. 	for (b = up ? bbubbles : ebubbles; b; b = up ? b->next : b->prev) {
  112. 		register int rx = rn2(3), ry = rn2(3);
  113.  
  114. 		mv_bubble(b,b->dx + 1 - (!b->dx ? rx : (rx ? 1 : 0)),
  115. 			    b->dy + 1 - (!b->dy ? ry : (ry ? 1 : 0)),
  116. 			    FALSE);
  117. 	}
  118.  
  119. 	/* put attached ball&chain back */
  120. 	if (Punished) placebc();
  121. 	vision_full_recalc = 1;
  122. }

water_friction[edit]

  1. /* when moving in water, possibly (1 in 3) alter the intended destination */
  2. void
  3. water_friction()
  4. {
  5. 	register int x, y, dx, dy;
  6. 	register boolean eff = FALSE;
  7.  
  8. 	if (Swimming && rn2(4))
  9. 		return;		/* natural swimmers have advantage */
  10.  
  11. 	if (u.dx && !rn2(!u.dy ? 3 : 6)) {	/* 1/3 chance or half that */
  12. 		/* cancel delta x and choose an arbitrary delta y value */
  13. 		x = u.ux;
  14. 		do {
  15. 		    dy = rn2(3) - 1;		/* -1, 0, 1 */
  16. 		    y = u.uy + dy;
  17. 		} while (dy && (!isok(x,y) || !is_pool(x,y)));
  18. 		u.dx = 0;
  19. 		u.dy = dy;
  20. 		eff = TRUE;
  21. 	} else if (u.dy && !rn2(!u.dx ? 3 : 5)) {	/* 1/3 or 1/5*(5/6) */
  22. 		/* cancel delta y and choose an arbitrary delta x value */
  23. 		y = u.uy;
  24. 		do {
  25. 		    dx = rn2(3) - 1;		/* -1 .. 1 */
  26. 		    x = u.ux + dx;
  27. 		} while (dx && (!isok(x,y) || !is_pool(x,y)));
  28. 		u.dy = 0;
  29. 		u.dx = dx;
  30. 		eff = TRUE;
  31. 	}
  32. 	if (eff) pline("Water turbulence affects your movements.");
  33. }

save_waterlevel[edit]

  1. void
  2. save_waterlevel(fd, mode)
  3. int fd, mode;
  4. {
  5. 	register struct bubble *b;
  6.  
  7. 	if (!Is_waterlevel(&u.uz)) return;
  8.  
  9. 	if (perform_bwrite(mode)) {
  10. 	    int n = 0;
  11. 	    for (b = bbubbles; b; b = b->next) ++n;
  12. 	    bwrite(fd, (genericptr_t)&n, sizeof (int));
  13. 	    bwrite(fd, (genericptr_t)&xmin, sizeof (int));
  14. 	    bwrite(fd, (genericptr_t)&ymin, sizeof (int));
  15. 	    bwrite(fd, (genericptr_t)&xmax, sizeof (int));
  16. 	    bwrite(fd, (genericptr_t)&ymax, sizeof (int));
  17. 	    for (b = bbubbles; b; b = b->next)
  18. 		bwrite(fd, (genericptr_t)b, sizeof (struct bubble));
  19. 	}
  20. 	if (release_data(mode))
  21. 	    unsetup_waterlevel();
  22. }

restore_waterlevel[edit]

  1. void
  2. restore_waterlevel(fd)
  3. register int fd;
  4. {
  5. 	register struct bubble *b = (struct bubble *)0, *btmp;
  6. 	register int i;
  7. 	int n;
  8.  
  9. 	if (!Is_waterlevel(&u.uz)) return;
  10.  
  11. 	set_wportal();
  12. 	mread(fd,(genericptr_t)&n,sizeof(int));
  13. 	mread(fd,(genericptr_t)&xmin,sizeof(int));
  14. 	mread(fd,(genericptr_t)&ymin,sizeof(int));
  15. 	mread(fd,(genericptr_t)&xmax,sizeof(int));
  16. 	mread(fd,(genericptr_t)&ymax,sizeof(int));
  17. 	for (i = 0; i < n; i++) {
  18. 		btmp = b;
  19. 		b = (struct bubble *)alloc(sizeof(struct bubble));
  20. 		mread(fd,(genericptr_t)b,sizeof(struct bubble));
  21. 		if (bbubbles) {
  22. 			btmp->next = b;
  23. 			b->prev = btmp;
  24. 		} else {
  25. 			bbubbles = b;
  26. 			b->prev = (struct bubble *)0;
  27. 		}
  28. 		mv_bubble(b,0,0,TRUE);
  29. 	}
  30. 	ebubbles = b;
  31. 	b->next = (struct bubble *)0;
  32. 	was_waterlevel = TRUE;
  33. }

waterbody_name[edit]

  1. const char *waterbody_name(x, y)
  2. xchar x,y;
  3. {
  4. 	register struct rm *lev;
  5. 	schar ltyp;
  6.  
  7. 	if (!isok(x,y))
  8. 		return "drink";		/* should never happen */
  9. 	lev = &levl[x][y];
  10. 	ltyp = lev->typ;
  11.  
  12. 	if (is_lava(x,y))
  13. 		return "lava";
  14. 	else if (ltyp == ICE ||
  15. 		 (ltyp == DRAWBRIDGE_UP &&
  16. 		  (levl[x][y].drawbridgemask & DB_UNDER) == DB_ICE))
  17. 		return "ice";
  18. 	else if (((ltyp != POOL) && (ltyp != WATER) &&
  19. 	  !Is_medusa_level(&u.uz) && !Is_waterlevel(&u.uz) && !Is_juiblex_level(&u.uz)) ||
  20. 	   (ltyp == DRAWBRIDGE_UP && (levl[x][y].drawbridgemask & DB_UNDER) == DB_MOAT))
  21. 		return "moat";
  22. 	else if ((ltyp != POOL) && (ltyp != WATER) && Is_juiblex_level(&u.uz))
  23. 		return "swamp";
  24. 	else if (ltyp == POOL)
  25. 		return "pool of water";
  26. 	else return "water";
  27. }

set_wportal[edit]

  1. STATIC_OVL void
  2. set_wportal()
  3. {
  4. 	/* there better be only one magic portal on water level... */
  5. 	for (wportal = ftrap; wportal; wportal = wportal->ntrap)
  6. 		if (wportal->ttyp == MAGIC_PORTAL) return;
  7. 	impossible("set_wportal(): no portal!");
  8. }

setup_waterlevel[edit]

  1. STATIC_OVL void
  2. setup_waterlevel()
  3. {
  4. 	register int x, y;
  5. 	register int xskip, yskip;
  6. 	register int water_glyph = cmap_to_glyph(S_water);
  7.  
  8. 	/* ouch, hardcoded... */
  9.  
  10. 	xmin = 3;
  11. 	ymin = 1;
  12. 	xmax = 78;
  13. 	ymax = 20;
  14.  
  15. 	/* set hero's memory to water */
  16.  
  17. 	for (x = xmin; x <= xmax; x++)
  18. 		for (y = ymin; y <= ymax; y++)
  19. 			levl[x][y].glyph = water_glyph;
  20.  
  21. 	/* make bubbles */
  22.  
  23. 	xskip = 10 + rn2(10);
  24. 	yskip = 4 + rn2(4);
  25. 	for (x = bxmin; x <= bxmax; x += xskip)
  26. 		for (y = bymin; y <= bymax; y += yskip)
  27. 			mk_bubble(x,y,rn2(7));
  28. }

unsetup_waterlevel[edit]

  1. STATIC_OVL void
  2. unsetup_waterlevel()
  3. {
  4. 	register struct bubble *b, *bb;
  5.  
  6. 	/* free bubbles */
  7.  
  8. 	for (b = bbubbles; b; b = bb) {
  9. 		bb = b->next;
  10. 		free((genericptr_t)b);
  11. 	}
  12. 	bbubbles = ebubbles = (struct bubble *)0;
  13. }

mk_bubble[edit]

  1. STATIC_OVL void
  2. mk_bubble(x,y,n)
  3. register int x, y, n;
  4. {
  5. 	/*
  6. 	 * These bit masks make visually pleasing bubbles on a normal aspect
  7. 	 * 25x80 terminal, which naturally results in them being mathematically
  8. 	 * anything but symmetric.  For this reason they cannot be computed
  9. 	 * in situ, either.  The first two elements tell the dimensions of
  10. 	 * the bubble's bounding box.
  11. 	 */
  12. 	static uchar
  13. 		bm2[] = {2,1,0x3},
  14. 		bm3[] = {3,2,0x7,0x7},
  15. 		bm4[] = {4,3,0x6,0xf,0x6},
  16. 		bm5[] = {5,3,0xe,0x1f,0xe},
  17. 		bm6[] = {6,4,0x1e,0x3f,0x3f,0x1e},
  18. 		bm7[] = {7,4,0x3e,0x7f,0x7f,0x3e},
  19. 		bm8[] = {8,4,0x7e,0xff,0xff,0x7e},
  20. 		*bmask[] = {bm2,bm3,bm4,bm5,bm6,bm7,bm8};
  21.  
  22. 	register struct bubble *b;
  23.  
  24. 	if (x >= bxmax || y >= bymax) return;
  25. 	if (n >= SIZE(bmask)) {
  26. 		impossible("n too large (mk_bubble)");
  27. 		n = SIZE(bmask) - 1;
  28. 	}
  29. 	b = (struct bubble *)alloc(sizeof(struct bubble));
  30. 	if ((x + (int) bmask[n][0] - 1) > bxmax) x = bxmax - bmask[n][0] + 1;
  31. 	if ((y + (int) bmask[n][1] - 1) > bymax) y = bymax - bmask[n][1] + 1;
  32. 	b->x = x;
  33. 	b->y = y;
  34. 	b->dx = 1 - rn2(3);
  35. 	b->dy = 1 - rn2(3);
  36. 	b->bm = bmask[n];
  37. 	b->cons = 0;
  38. 	if (!bbubbles) bbubbles = b;
  39. 	if (ebubbles) {
  40. 		ebubbles->next = b;
  41. 		b->prev = ebubbles;
  42. 	}
  43. 	else
  44. 		b->prev = (struct bubble *)0;
  45. 	b->next =  (struct bubble *)0;
  46. 	ebubbles = b;
  47. 	mv_bubble(b,0,0,TRUE);
  48. }

mv_bubble[edit]

  1. /*
  2. * The player, the portal and all other objects and monsters
  3. * float along with their associated bubbles.  Bubbles may overlap
  4. * freely, and the contents may get associated with other bubbles in
  5. * the process.  Bubbles are "sticky", meaning that if the player is
  6. * in the immediate neighborhood of one, he/she may get sucked inside.
  7. * This property also makes leaving a bubble slightly difficult.
  8. */
  9. STATIC_OVL void
  10. mv_bubble(b,dx,dy,ini)
  11. register struct bubble *b;
  12. register int dx, dy;
  13. register boolean ini;
  14. {
  15. 	register int x, y, i, j, colli = 0;
  16. 	struct container *cons, *ctemp;
  17.  
  18. 	/* move bubble */
  19. 	if (dx < -1 || dx > 1 || dy < -1 || dy > 1) {
  20. 	    /* pline("mv_bubble: dx = %d, dy = %d", dx, dy); */
  21. 	    dx = sgn(dx);
  22. 	    dy = sgn(dy);
  23. 	}
  24.  
  25. 	/*
  26. 	 * collision with level borders?
  27. 	 *	1 = horizontal border, 2 = vertical, 3 = corner
  28. 	 */
  29. 	if (b->x <= bxmin) colli |= 2;
  30. 	if (b->y <= bymin) colli |= 1;
  31. 	if ((int) (b->x + b->bm[0] - 1) >= bxmax) colli |= 2;
  32. 	if ((int) (b->y + b->bm[1] - 1) >= bymax) colli |= 1;
  33.  
  34. 	if (b->x < bxmin) {
  35. 	    pline("bubble xmin: x = %d, xmin = %d", b->x, bxmin);
  36. 	    b->x = bxmin;
  37. 	}
  38. 	if (b->y < bymin) {
  39. 	    pline("bubble ymin: y = %d, ymin = %d", b->y, bymin);
  40. 	    b->y = bymin;
  41. 	}
  42. 	if ((int) (b->x + b->bm[0] - 1) > bxmax) {
  43. 	    pline("bubble xmax: x = %d, xmax = %d",
  44. 			b->x + b->bm[0] - 1, bxmax);
  45. 	    b->x = bxmax - b->bm[0] + 1;
  46. 	}
  47. 	if ((int) (b->y + b->bm[1] - 1) > bymax) {
  48. 	    pline("bubble ymax: y = %d, ymax = %d",
  49. 			b->y + b->bm[1] - 1, bymax);
  50. 	    b->y = bymax - b->bm[1] + 1;
  51. 	}
  52.  
  53. 	/* bounce if we're trying to move off the border */
  54. 	if (b->x == bxmin && dx < 0) dx = -dx;
  55. 	if (b->x + b->bm[0] - 1 == bxmax && dx > 0) dx = -dx;
  56. 	if (b->y == bymin && dy < 0) dy = -dy;
  57. 	if (b->y + b->bm[1] - 1 == bymax && dy > 0) dy = -dy;
  58.  
  59. 	b->x += dx;
  60. 	b->y += dy;
  61.  
  62. 	/* void positions inside bubble */
  63.  
  64. 	for (i = 0, x = b->x; i < (int) b->bm[0]; i++, x++)
  65. 	    for (j = 0, y = b->y; j < (int) b->bm[1]; j++, y++)
  66. 		if (b->bm[j + 2] & (1 << i)) {
  67. 		    levl[x][y].typ = AIR;
  68. 		    levl[x][y].lit = 1;
  69. 		    unblock_point(x,y);
  70. 		}
  71.  
  72. 	/* replace contents of bubble */
  73. 	for (cons = b->cons; cons; cons = ctemp) {
  74. 	    ctemp = cons->next;
  75. 	    cons->x += dx;
  76. 	    cons->y += dy;
  77.  
  78. 	    switch(cons->what) {
  79. 		case CONS_OBJ: {
  80. 		    struct obj *olist, *otmp;
  81.  
  82. 		    for (olist=(struct obj *)cons->list; olist; olist=otmp) {
  83. 			otmp = olist->nexthere;
  84. 			place_object(olist, cons->x, cons->y);
  85. 		    }
  86. 		    break;
  87. 		}
  88.  
  89. 		case CONS_MON: {
  90. 		    struct monst *mon = (struct monst *) cons->list;
  91. 		    (void) mnearto(mon, cons->x, cons->y, TRUE);
  92. 		    break;
  93. 		}
  94.  
  95. 		case CONS_HERO: {
  96. 		    int ux0 = u.ux, uy0 = u.uy;
  97.  
  98. 		    /* change u.ux0 and u.uy0? */
  99. 		    u.ux = cons->x;
  100. 		    u.uy = cons->y;
  101. 		    newsym(ux0, uy0);	/* clean up old position */
  102.  
  103. 		    if (MON_AT(cons->x, cons->y)) {
  104. 				mnexto(m_at(cons->x,cons->y));
  105. 			}
  106. 		    break;
  107. 		}
  108.  
  109. 		case CONS_TRAP: {
  110. 		    struct trap *btrap = (struct trap *) cons->list;
  111. 		    btrap->tx = cons->x;
  112. 		    btrap->ty = cons->y;
  113. 		    break;
  114. 		}
  115.  
  116. 		default:
  117. 		    impossible("mv_bubble: unknown bubble contents");
  118. 		    break;
  119. 	    }
  120. 	    free((genericptr_t)cons);
  121. 	}
  122. 	b->cons = 0;
  123.  
  124. 	/* boing? */
  125.  
  126. 	switch (colli) {
  127. 	    case 1: b->dy = -b->dy;	break;
  128. 	    case 3: b->dy = -b->dy;	/* fall through */
  129. 	    case 2: b->dx = -b->dx;	break;
  130. 	    default:
  131. 		/* sometimes alter direction for fun anyway
  132. 		   (higher probability for stationary bubbles) */
  133. 		if (!ini && ((b->dx || b->dy) ? !rn2(20) : !rn2(5))) {
  134. 			b->dx = 1 - rn2(3);
  135. 			b->dy = 1 - rn2(3);
  136. 		}
  137. 	}
  138. }
  139.  
  140. /*mkmaze.c*/