Source:NetHack 3.6.1/src/mkmaze.c

From NetHackWiki
(Redirected from Mkmaze.c)
Jump to: navigation, search

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

Top of file

  1.  /* NetHack 3.6	mkmaze.c	$NHDT-Date: 1518718417 2018/02/15 18:13:37 $  $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.55 $ */
  2.  /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3.  /*-Copyright (c) Pasi Kallinen, 2018. */
  4.  /* 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.  #include "hack.h"
  3.  #include "sp_lev.h"
  4.  #include "lev.h" /* save & restore info */
  5.  
  6.  /* from sp_lev.c, for fixup_special() */
  7.  extern lev_region *lregions;
  8.  extern int num_lregions;
  9.  /* for preserving the insect legs when wallifying baalz level */
  10.  static lev_region bughack = { {COLNO, ROWNO, 0, 0}, {COLNO, ROWNO, 0, 0} };
  11.  
  12.  STATIC_DCL int FDECL(iswall, (int, int));
  13.  STATIC_DCL int FDECL(iswall_or_stone, (int, int));
  14.  STATIC_DCL boolean FDECL(is_solid, (int, int));
  15.  STATIC_DCL int FDECL(extend_spine, (int[3][3], int, int, int));
  16.  STATIC_DCL boolean FDECL(okay, (int, int, int));
  17.  STATIC_DCL void FDECL(maze0xy, (coord *));
  18.  STATIC_DCL boolean FDECL(put_lregion_here, (XCHAR_P, XCHAR_P, XCHAR_P,
  19.                                              XCHAR_P, XCHAR_P, XCHAR_P,
  20.                                              XCHAR_P, BOOLEAN_P, d_level *));
  21.  STATIC_DCL void NDECL(baalz_fixup);
  22.  STATIC_DCL void NDECL(setup_waterlevel);
  23.  STATIC_DCL void NDECL(unsetup_waterlevel);
  24.  
  25.  /* adjust a coordinate one step in the specified direction */
  26.  #define mz_move(X, Y, dir) \
  27.      do {                                                         \
  28.          switch (dir) {                                           \
  29.          case 0:  --(Y);  break;                                  \
  30.          case 1:  (X)++;  break;                                  \
  31.          case 2:  (Y)++;  break;                                  \
  32.          case 3:  --(X);  break;                                  \
  33.          default: panic("mz_move: bad direction %d", dir);        \
  34.          }                                                        \
  35.      } while (0)
  36.  

iswall

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

iswall_or_stone

  1.  STATIC_OVL int
  2.  iswall_or_stone(x, y)
  3.  int x, y;
  4.  {
  5.      /* out of bounds = stone */
  6.      if (!isok(x, y))
  7.          return 1;
  8.  
  9.      return (levl[x][y].typ == STONE || iswall(x, y));
  10.  }
  11.  

is_solid

  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 (boolean) (!isok(x, y) || IS_STWALL(levl[x][y].typ));
  7.  }
  8.  

extend_spine

  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.  }
  51.  

wall_cleanup

  1.  /* Remove walls totally surrounded by stone */
  2.  void
  3.  wall_cleanup(x1, y1, x2, y2)
  4.  int x1, y1, x2, y2;
  5.  {
  6.      uchar type;
  7.      int x, y;
  8.      struct rm *lev;
  9.  
  10.      /* sanity check on incoming variables */
  11.      if (x1 < 0 || x2 >= COLNO || x1 > x2 || y1 < 0 || y2 >= ROWNO || y1 > y2)
  12.          panic("wall_cleanup: bad bounds (%d,%d) to (%d,%d)", x1, y1, x2, y2);
  13.  
  14.      /* change walls surrounded by rock to rock. */
  15.      for (x = x1; x <= x2; x++)
  16.          for (y = y1; y <= y2; y++) {
  17.              if (within_bounded_area(x, y,
  18.                                      bughack.inarea.x1, bughack.inarea.y1,
  19.                                      bughack.inarea.x2, bughack.inarea.y2))
  20.                  continue;
  21.              lev = &levl[x][y];
  22.              type = lev->typ;
  23.              if (IS_WALL(type) && type != DBWALL) {
  24.                  if (is_solid(x - 1, y - 1) && is_solid(x - 1, y)
  25.                      && is_solid(x - 1, y + 1) && is_solid(x, y - 1)
  26.                      && is_solid(x, y + 1) && is_solid(x + 1, y - 1)
  27.                      && is_solid(x + 1, y) && is_solid(x + 1, y + 1))
  28.                      lev->typ = STONE;
  29.              }
  30.          }
  31.  }
  32.  

fix_wall_spines

  1.  /* Correct wall types so they extend and connect to each other */
  2.  void
  3.  fix_wall_spines(x1, y1, x2, y2)
  4.  int x1, y1, x2, y2;
  5.  {
  6.      uchar type;
  7.      int x, y;
  8.      struct rm *lev;
  9.      int FDECL((*loc_f), (int, int));
  10.      int bits;
  11.      int locale[3][3]; /* rock or wall status surrounding positions */
  12.  
  13.      /*
  14.       * Value 0 represents a free-standing wall.  It could be anything,
  15.       * so even though this table says VWALL, we actually leave whatever
  16.       * typ was there alone.
  17.       */
  18.      static xchar spine_array[16] = { VWALL, HWALL,    HWALL,    HWALL,
  19.                                       VWALL, TRCORNER, TLCORNER, TDWALL,
  20.                                       VWALL, BRCORNER, BLCORNER, TUWALL,
  21.                                       VWALL, TLWALL,   TRWALL,   CROSSWALL };
  22.  
  23.      /* sanity check on incoming variables */
  24.      if (x1 < 0 || x2 >= COLNO || x1 > x2 || y1 < 0 || y2 >= ROWNO || y1 > y2)
  25.          panic("wall_extends: bad bounds (%d,%d) to (%d,%d)", x1, y1, x2, y2);
  26.  
  27.      /* set the correct wall type. */
  28.      for (x = x1; x <= x2; x++)
  29.          for (y = y1; y <= y2; y++) {
  30.              lev = &levl[x][y];
  31.              type = lev->typ;
  32.              if (!(IS_WALL(type) && type != DBWALL))
  33.                  continue;
  34.  
  35.              /* set the locations TRUE if rock or wall or out of bounds */
  36.              loc_f = within_bounded_area(x, y, /* for baalz insect */
  37.                                          bughack.inarea.x1, bughack.inarea.y1,
  38.                                          bughack.inarea.x2, bughack.inarea.y2)
  39.                         ? iswall
  40.                         : iswall_or_stone;
  41.              locale[0][0] = (*loc_f)(x - 1, y - 1);
  42.              locale[1][0] = (*loc_f)(x, y - 1);
  43.              locale[2][0] = (*loc_f)(x + 1, y - 1);
  44.  
  45.              locale[0][1] = (*loc_f)(x - 1, y);
  46.              locale[2][1] = (*loc_f)(x + 1, y);
  47.  
  48.              locale[0][2] = (*loc_f)(x - 1, y + 1);
  49.              locale[1][2] = (*loc_f)(x, y + 1);
  50.              locale[2][2] = (*loc_f)(x + 1, y + 1);
  51.  
  52.              /* determine if wall should extend to each direction NSEW */
  53.              bits = (extend_spine(locale, iswall(x, y - 1), 0, -1) << 3)
  54.                     | (extend_spine(locale, iswall(x, y + 1), 0, 1) << 2)
  55.                     | (extend_spine(locale, iswall(x + 1, y), 1, 0) << 1)
  56.                     | extend_spine(locale, iswall(x - 1, y), -1, 0);
  57.  
  58.              /* don't change typ if wall is free-standing */
  59.              if (bits)
  60.                  lev->typ = spine_array[bits];
  61.          }
  62.  }
  63.  

wallification

  1.  void
  2.  wallification(x1, y1, x2, y2)
  3.  int x1, y1, x2, y2;
  4.  {
  5.      wall_cleanup(x1, y1, x2, y2);
  6.      fix_wall_spines(x1, y1, x2, y2);
  7.  }
  8.  

okay

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

maze0xy

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

bad_location

  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
  16.                             || levl[x][y].typ == AIR));
  17.  }
  18.  

place_lregion

  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; /* column 0 is not used */
  25.          hx = COLNO - 1;
  26.          ly = 0; /* 3.6.0 and earlier erroneously had 1 here */
  27.          hy = ROWNO - 1;
  28.      }
  29.  
  30.      /* first a probabilistic approach */
  31.  
  32.      oneshot = (lx == hx && ly == hy);
  33.      for (trycnt = 0; trycnt < 200; trycnt++) {
  34.          x = rn1((hx - lx) + 1, lx);
  35.          y = rn1((hy - ly) + 1, ly);
  36.          if (put_lregion_here(x, y, nlx, nly, nhx, nhy, rtype, oneshot, lev))
  37.              return;
  38.      }
  39.  
  40.      /* then a deterministic one */
  41.  
  42.      for (x = lx; x <= hx; x++)
  43.          for (y = ly; y <= hy; y++)
  44.              if (put_lregion_here(x, y, nlx, nly, nhx, nhy, rtype, TRUE, lev))
  45.                  return;
  46.  
  47.      impossible("Couldn't place lregion type %d!", rtype);
  48.  }
  49.  

put_lregion_here

  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 && t->ttyp != VIBRATING_SQUARE)
  19.                  deltrap(t);
  20.              if (bad_location(x, y, nlx, nly, nhx, nhy))
  21.                  return FALSE;
  22.          }
  23.      }
  24.      switch (rtype) {
  25.      case LR_TELE:
  26.      case LR_UPTELE:
  27.      case LR_DOWNTELE:
  28.          /* "something" means the player in this case */
  29.          if (MON_AT(x, y)) {
  30.              /* move the monster if no choice, or just try again */
  31.              if (oneshot)
  32.                  (void) rloc(m_at(x, y), FALSE);
  33.              else
  34.                  return FALSE;
  35.          }
  36.          u_on_newpos(x, y);
  37.          break;
  38.      case LR_PORTAL:
  39.          mkportal(x, y, lev->dnum, lev->dlevel);
  40.          break;
  41.      case LR_DOWNSTAIR:
  42.      case LR_UPSTAIR:
  43.          mkstairs(x, y, (char) rtype, (struct mkroom *) 0);
  44.          break;
  45.      case LR_BRANCH:
  46.          place_branch(Is_branchlev(&u.uz), x, y);
  47.          break;
  48.      }
  49.      return TRUE;
  50.  }
  51.  

baalz_fixup

  1.  /* fix up Baalzebub's lair, which depicts a level-sized beetle;
  2.     its legs are walls within solid rock--regular wallification
  3.     classifies them as superfluous and gets rid of them */
  4.  STATIC_OVL void
  5.  baalz_fixup()
  6.  {
  7.      struct monst *mtmp;
  8.      int x, y, lastx, lasty;
  9.  
  10.      /*
  11.       * baalz level's nondiggable region surrounds the "insect" and rooms.
  12.       * The outermost perimeter of that region is subject to wall cleanup
  13.       * (hence 'x + 1' and 'y + 1' for starting don't-clean column and row,
  14.       * 'lastx - 1' and 'lasty - 1' for ending don't-clean column and row)
  15.       * and the interior is protected against that (in wall_cleanup()).
  16.       *
  17.       * Assumes level.flags.corrmaze is True, otherwise the bug legs will
  18.       * have already been "cleaned" away by general wallification.
  19.       */
  20.  
  21.      /* find low and high x for to-be-wallified portion of level */
  22.      y = ROWNO / 2;
  23.      for (lastx = x = 0; x < COLNO; ++x)
  24.          if ((levl[x][y].wall_info & W_NONDIGGABLE) != 0) {
  25.              if (!lastx)
  26.                  bughack.inarea.x1 = x + 1;
  27.              lastx = x;
  28.          }
  29.      bughack.inarea.x2 = ((lastx > bughack.inarea.x1) ? lastx : x) - 1;
  30.      /* find low and high y for to-be-wallified portion of level */
  31.      x = bughack.inarea.x1;
  32.      for (lasty = y = 0; y < ROWNO; ++y)
  33.          if ((levl[x][y].wall_info & W_NONDIGGABLE) != 0) {
  34.              if (!lasty)
  35.                  bughack.inarea.y1 = y + 1;
  36.              lasty = y;
  37.          }
  38.      bughack.inarea.y2 = ((lasty > bughack.inarea.y1) ? lasty : y) - 1;
  39.      /* two pools mark where special post-wallify fix-ups are needed */
  40.      for (x = bughack.inarea.x1; x <= bughack.inarea.x2; ++x)
  41.          for (y = bughack.inarea.y1; y <= bughack.inarea.y2; ++y)
  42.              if (levl[x][y].typ == POOL) {
  43.                  levl[x][y].typ = HWALL;
  44.                  if (bughack.delarea.x1 == COLNO)
  45.                      bughack.delarea.x1 = x, bughack.delarea.y1 = y;
  46.                  else
  47.                      bughack.delarea.x2 = x, bughack.delarea.y2 = y;
  48.              } else if (levl[x][y].typ == IRONBARS) {
  49.                  /* novelty effect; allowing digging in front of 'eyes' */
  50.                  levl[x - 1][y].wall_info &= ~W_NONDIGGABLE;
  51.                  if (isok(x - 2, y))
  52.                      levl[x - 2][y].wall_info &= ~W_NONDIGGABLE;
  53.              }
  54.  
  55.      wallification(max(bughack.inarea.x1 - 2, 1),
  56.                    max(bughack.inarea.y1 - 2, 0),
  57.                    min(bughack.inarea.x2 + 2, COLNO - 1),
  58.                    min(bughack.inarea.y2 + 2, ROWNO - 1));
  59.  
  60.      /* bughack hack for rear-most legs on baalz level; first joint on
  61.         both top and bottom gets a bogus extra connection to room area,
  62.         producing unwanted rectangles; change back to separated legs */
  63.      x = bughack.delarea.x1, y = bughack.delarea.y1;
  64.      if (isok(x, y) && levl[x][y].typ == TLWALL
  65.          && isok(x, y + 1) && levl[x][y + 1].typ == TUWALL) {
  66.          levl[x][y].typ = BRCORNER;
  67.          levl[x][y + 1].typ = HWALL;
  68.          if ((mtmp = m_at(x, y)) != 0) /* something at temporary pool... */
  69.              (void) rloc(mtmp, FALSE);
  70.      }
  71.      x = bughack.delarea.x2, y = bughack.delarea.y2;
  72.      if (isok(x, y) && levl[x][y].typ == TLWALL
  73.          && isok(x, y - 1) && levl[x][y - 1].typ == TDWALL) {
  74.          levl[x][y].typ = TRCORNER;
  75.          levl[x][y - 1].typ = HWALL;
  76.          if ((mtmp = m_at(x, y)) != 0) /* something at temporary pool... */
  77.              (void) rloc(mtmp, FALSE);
  78.      }
  79.  
  80.      /* reset bughack region; set low end to <COLNO,ROWNO> so that
  81.         within_bounded_region() in fix_wall_spines() will fail
  82.         most quickly--on its first test--when loading other levels */
  83.      bughack.inarea.x1 = bughack.delarea.x1 = COLNO;
  84.      bughack.inarea.y1 = bughack.delarea.y1 = ROWNO;
  85.      bughack.inarea.x2 = bughack.delarea.x2 = 0;
  86.      bughack.inarea.y2 = bughack.delarea.y2 = 0;
  87.  }
  88.  
  89.  static boolean was_waterlevel; /* ugh... this shouldn't be needed */
  90.  

fixup_special

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

maze_inbounds

  1.  boolean
  2.  maze_inbounds(x, y)
  3.  int x, y;
  4.  {
  5.      return (x >= 2 && y >= 2
  6.              && x < x_maze_max && y < y_maze_max && isok(x, y));
  7.  }
  8.  

maze_remove_deadends

  1.  void
  2.  maze_remove_deadends(typ)
  3.  xchar typ;
  4.  {
  5.      char dirok[4];
  6.      int x, y, dir, idx, idx2, dx, dy, dx2, dy2;
  7.  
  8.      dirok[0] = 0; /* lint suppression */
  9.      for (x = 2; x < x_maze_max; x++)
  10.          for (y = 2; y < y_maze_max; y++)
  11.              if (ACCESSIBLE(levl[x][y].typ) && (x % 2) && (y % 2)) {
  12.                  idx = idx2 = 0;
  13.                  for (dir = 0; dir < 4; dir++) {
  14.                      /* note: mz_move() is a macro which modifies
  15.                         one of its first two parameters */
  16.                      dx = dx2 = x;
  17.                      dy = dy2 = y;
  18.                      mz_move(dx, dy, dir);
  19.                      if (!maze_inbounds(dx, dy)) {
  20.                          idx2++;
  21.                          continue;
  22.                      }
  23.                      mz_move(dx2, dy2, dir);
  24.                      mz_move(dx2, dy2, dir);
  25.                      if (!maze_inbounds(dx2, dy2)) {
  26.                          idx2++;
  27.                          continue;
  28.                      }
  29.                      if (!ACCESSIBLE(levl[dx][dy].typ)
  30.                          && ACCESSIBLE(levl[dx2][dy2].typ)) {
  31.                          dirok[idx++] = dir;
  32.                          idx2++;
  33.                      }
  34.                  }
  35.                  if (idx2 >= 3 && idx > 0) {
  36.                      dx = x;
  37.                      dy = y;
  38.                      dir = dirok[rn2(idx)];
  39.                      mz_move(dx, dy, dir);
  40.                      levl[dx][dy].typ = typ;
  41.                  }
  42.              }
  43.  }
  44.  

create_maze

  1.  /* Create a maze with specified corridor width and wall thickness
  2.   * TODO: rewrite walkfrom so it works on temp space, not levl
  3.   */
  4.  void
  5.  create_maze(corrwid, wallthick)
  6.  int corrwid;
  7.  int wallthick;
  8.  {
  9.      int x,y;
  10.      coord mm;
  11.      int tmp_xmax = x_maze_max;
  12.      int tmp_ymax = y_maze_max;
  13.      int rdx = 0;
  14.      int rdy = 0;
  15.      int scale;
  16.  
  17.      if (wallthick < 1)
  18.          wallthick = 1;
  19.      else if (wallthick > 5)
  20.          wallthick = 5;
  21.  
  22.      if (corrwid < 1)
  23.          corrwid = 1;
  24.      else if (corrwid > 5)
  25.          corrwid = 5;
  26.  
  27.      scale = corrwid + wallthick;
  28.      rdx = (x_maze_max / scale);
  29.      rdy = (y_maze_max / scale);
  30.  
  31.      if (level.flags.corrmaze)
  32.          for (x = 2; x < (rdx * 2); x++)
  33.              for (y = 2; y < (rdy * 2); y++)
  34.                  levl[x][y].typ = STONE;
  35.      else
  36.          for (x = 2; x <= (rdx * 2); x++)
  37.              for (y = 2; y <= (rdy * 2); y++)
  38.                  levl[x][y].typ = ((x % 2) && (y % 2)) ? STONE : HWALL;
  39.  
  40.      /* set upper bounds for maze0xy and walkfrom */
  41.      x_maze_max = (rdx * 2);
  42.      y_maze_max = (rdy * 2);
  43.  
  44.      /* create maze */
  45.      maze0xy(&mm);
  46.      walkfrom((int) mm.x, (int) mm.y, 0);
  47.  
  48.      if (!rn2(5))
  49.          maze_remove_deadends((level.flags.corrmaze) ? CORR : ROOM);
  50.  
  51.      /* restore bounds */
  52.      x_maze_max = tmp_xmax;
  53.      y_maze_max = tmp_ymax;
  54.  
  55.      /* scale maze up if needed */
  56.      if (scale > 2) {
  57.          char tmpmap[COLNO][ROWNO];
  58.          int rx = 1, ry = 1;
  59.  
  60.          /* back up the existing smaller maze */
  61.          for (x = 1; x < x_maze_max; x++)
  62.              for (y = 1; y < y_maze_max; y++) {
  63.                  tmpmap[x][y] = levl[x][y].typ;
  64.              }
  65.  
  66.          /* do the scaling */
  67.          rx = x = 2;
  68.          while (rx < x_maze_max) {
  69.              int mx = (x % 2) ? corrwid
  70.                               : ((x == 2 || x == (rdx * 2)) ? 1
  71.                                                             : wallthick);
  72.              ry = y = 2;
  73.              while (ry < y_maze_max) {
  74.                  int dx = 0, dy = 0;
  75.                  int my = (y % 2) ? corrwid
  76.                                   : ((y == 2 || y == (rdy * 2)) ? 1
  77.                                                                 : wallthick);
  78.                  for (dx = 0; dx < mx; dx++)
  79.                      for (dy = 0; dy < my; dy++) {
  80.                          if (rx+dx >= x_maze_max
  81.                              || ry+dy >= y_maze_max)
  82.                              break;
  83.                          levl[rx + dx][ry + dy].typ = tmpmap[x][y];
  84.                      }
  85.                  ry += my;
  86.                  y++;
  87.              }
  88.              rx += mx;
  89.              x++;
  90.          }
  91.  
  92.      }
  93.  }
  94.  
  95.  

makemaz

  1.  void
  2.  makemaz(s)
  3.  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)
  12.              Sprintf(protofile, "%s-%d", s, rnd((int) sp->rndlevs));
  13.          else
  14.              Strcpy(protofile, s);
  15.      } else if (*(dungeons[u.uz.dnum].proto)) {
  16.          if (dunlevs_in_dungeon(&u.uz) > 1) {
  17.              if (sp && sp->rndlevs)
  18.                  Sprintf(protofile, "%s%d-%d", dungeons[u.uz.dnum].proto,
  19.                          dunlev(&u.uz), rnd((int) sp->rndlevs));
  20.              else
  21.                  Sprintf(protofile, "%s%d", dungeons[u.uz.dnum].proto,
  22.                          dunlev(&u.uz));
  23.          } else if (sp && sp->rndlevs) {
  24.              Sprintf(protofile, "%s-%d", dungeons[u.uz.dnum].proto,
  25.                      rnd((int) sp->rndlevs));
  26.          } else
  27.              Strcpy(protofile, dungeons[u.uz.dnum].proto);
  28.  
  29.      } else
  30.          Strcpy(protofile, "");
  31.  
  32.      /* SPLEVTYPE format is "level-choice,level-choice"... */
  33.      if (wizard && *protofile && sp && sp->rndlevs) {
  34.          char *ep = getenv("SPLEVTYPE"); /* not nh_getenv */
  35.          if (ep) {
  36.              /* rindex always succeeds due to code in prior block */
  37.              int len = (int) ((rindex(protofile, '-') - protofile) + 1);
  38.  
  39.              while (ep && *ep) {
  40.                  if (!strncmp(ep, protofile, len)) {
  41.                      int pick = atoi(ep + len);
  42.                      /* use choice only if valid */
  43.                      if (pick > 0 && pick <= (int) sp->rndlevs)
  44.                          Sprintf(protofile + len, "%d", pick);
  45.                      break;
  46.                  } else {
  47.                      ep = index(ep, ',');
  48.                      if (ep)
  49.                          ++ep;
  50.                  }
  51.              }
  52.          }
  53.      }
  54.  
  55.      if (*protofile) {
  56.          Strcat(protofile, LEV_EXT);
  57.          if (load_special(protofile)) {
  58.              /* some levels can end up with monsters
  59.                 on dead mon list, including light source monsters */
  60.              dmonsfree();
  61.              return; /* no mazification right now */
  62.          }
  63.          impossible("Couldn't load \"%s\" - making a maze.", protofile);
  64.      }
  65.  
  66.      level.flags.is_maze_lev = TRUE;
  67.      level.flags.corrmaze = !rn2(3);
  68.  
  69.      if (!Invocation_lev(&u.uz) && rn2(2)) {
  70.          int corrscale = rnd(4);
  71.          create_maze(corrscale,rnd(4)-corrscale);
  72.      } else {
  73.          create_maze(1,1);
  74.      }
  75.  
  76.      if (!level.flags.corrmaze)
  77.          wallification(2, 2, x_maze_max, y_maze_max);
  78.  
  79.      mazexy(&mm);
  80.      mkstairs(mm.x, mm.y, 1, (struct mkroom *) 0); /* up */
  81.      if (!Invocation_lev(&u.uz)) {
  82.          mazexy(&mm);
  83.          mkstairs(mm.x, mm.y, 0, (struct mkroom *) 0); /* down */
  84.      } else { /* choose "vibrating square" location */
  85.  #define x_maze_min 2
  86.  #define y_maze_min 2
  87.  /*
  88.   * Pick a position where the stairs down to Moloch's Sanctum
  89.   * level will ultimately be created.  At that time, an area
  90.   * will be altered:  walls removed, moat and traps generated,
  91.   * boulders destroyed.  The position picked here must ensure
  92.   * that that invocation area won't extend off the map.
  93.   *
  94.   * We actually allow up to 2 squares around the usual edge of
  95.   * the area to get truncated; see mkinvokearea(mklev.c).
  96.   */
  97.  #define INVPOS_X_MARGIN (6 - 2)
  98.  #define INVPOS_Y_MARGIN (5 - 2)
  99.  #define INVPOS_DISTANCE 11
  100.          int x_range = x_maze_max - x_maze_min - 2 * INVPOS_X_MARGIN - 1,
  101.              y_range = y_maze_max - y_maze_min - 2 * INVPOS_Y_MARGIN - 1;
  102.  
  103.          if (x_range <= INVPOS_X_MARGIN || y_range <= INVPOS_Y_MARGIN
  104.              || (x_range * y_range) <= (INVPOS_DISTANCE * INVPOS_DISTANCE)) {
  105.              debugpline2("inv_pos: maze is too small! (%d x %d)",
  106.                          x_maze_max, y_maze_max);
  107.          }
  108.          inv_pos.x = inv_pos.y = 0; /*{occupied() => invocation_pos()}*/
  109.          do {
  110.              x = rn1(x_range, x_maze_min + INVPOS_X_MARGIN + 1);
  111.              y = rn1(y_range, y_maze_min + INVPOS_Y_MARGIN + 1);
  112.              /* we don't want it to be too near the stairs, nor
  113.                 to be on a spot that's already in use (wall|trap) */
  114.          } while (x == xupstair || y == yupstair /*(direct line)*/
  115.                   || abs(x - xupstair) == abs(y - yupstair)
  116.                   || distmin(x, y, xupstair, yupstair) <= INVPOS_DISTANCE
  117.                   || !SPACE_POS(levl[x][y].typ) || occupied(x, y));
  118.          inv_pos.x = x;
  119.          inv_pos.y = y;
  120.          maketrap(inv_pos.x, inv_pos.y, VIBRATING_SQUARE);
  121.  #undef INVPOS_X_MARGIN
  122.  #undef INVPOS_Y_MARGIN
  123.  #undef INVPOS_DISTANCE
  124.  #undef x_maze_min
  125.  #undef y_maze_min
  126.      }
  127.  
  128.      /* place branch stair or portal */
  129.      place_branch(Is_branchlev(&u.uz), 0, 0);
  130.  
  131.      for (x = rn1(8, 11); x; x--) {
  132.          mazexy(&mm);
  133.          (void) mkobj_at(rn2(2) ? GEM_CLASS : 0, mm.x, mm.y, TRUE);
  134.      }
  135.      for (x = rn1(10, 2); x; x--) {
  136.          mazexy(&mm);
  137.          (void) mksobj_at(BOULDER, mm.x, mm.y, TRUE, FALSE);
  138.      }
  139.      for (x = rn2(3); x; x--) {
  140.          mazexy(&mm);
  141.          (void) makemon(&mons[PM_MINOTAUR], mm.x, mm.y, NO_MM_FLAGS);
  142.      }
  143.      for (x = rn1(5, 7); x; x--) {
  144.          mazexy(&mm);
  145.          (void) makemon((struct permonst *) 0, mm.x, mm.y, NO_MM_FLAGS);
  146.      }
  147.      for (x = rn1(6, 7); x; x--) {
  148.          mazexy(&mm);
  149.          (void) mkgold(0L, mm.x, mm.y);
  150.      }
  151.      for (x = rn1(6, 7); x; x--)
  152.          mktrap(0, 1, (struct mkroom *) 0, (coord *) 0);
  153.  }
  154.  

walkfrom

  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, typ)
  9.  int x, y;
  10.  schar typ;
  11.  {
  12.  #define CELLS (ROWNO * COLNO) / 4            /* a maze cell is 4 squares */
  13.      char mazex[CELLS + 1], mazey[CELLS + 1]; /* char's are OK */
  14.      int q, a, dir, pos;
  15.      int dirs[4];
  16.  
  17.      if (!typ) {
  18.          if (level.flags.corrmaze)
  19.              typ = CORR;
  20.          else
  21.              typ = ROOM;
  22.      }
  23.  
  24.      pos = 1;
  25.      mazex[pos] = (char) x;
  26.      mazey[pos] = (char) y;
  27.      while (pos) {
  28.          x = (int) mazex[pos];
  29.          y = (int) mazey[pos];
  30.          if (!IS_DOOR(levl[x][y].typ)) {
  31.              /* might still be on edge of MAP, so don't overwrite */
  32.              levl[x][y].typ = typ;
  33.              levl[x][y].flags = 0;
  34.          }
  35.          q = 0;
  36.          for (a = 0; a < 4; a++)
  37.              if (okay(x, y, a))
  38.                  dirs[q++] = a;
  39.          if (!q)
  40.              pos--;
  41.          else {
  42.              dir = dirs[rn2(q)];
  43.              mz_move(x, y, dir);
  44.              levl[x][y].typ = typ;
  45.              mz_move(x, y, dir);
  46.              pos++;
  47.              if (pos > CELLS)
  48.                  panic("Overflow in walkfrom");
  49.              mazex[pos] = (char) x;
  50.              mazey[pos] = (char) y;
  51.          }
  52.      }
  53.  }
  54.  #else /* !MICRO */
  55.  
  56.  void
  57.  walkfrom(x, y, typ)
  58.  int x, y;
  59.  schar typ;
  60.  {
  61.      int q, a, dir;
  62.      int dirs[4];
  63.  
  64.      if (!typ) {
  65.          if (level.flags.corrmaze)
  66.              typ = CORR;
  67.          else
  68.              typ = ROOM;
  69.      }
  70.  
  71.      if (!IS_DOOR(levl[x][y].typ)) {
  72.          /* might still be on edge of MAP, so don't overwrite */
  73.          levl[x][y].typ = typ;
  74.          levl[x][y].flags = 0;
  75.      }
  76.  
  77.      while (1) {
  78.          q = 0;
  79.          for (a = 0; a < 4; a++)
  80.              if (okay(x, y, a))
  81.                  dirs[q++] = a;
  82.          if (!q)
  83.              return;
  84.          dir = dirs[rn2(q)];
  85.          mz_move(x, y, dir);
  86.          levl[x][y].typ = typ;
  87.          mz_move(x, y, dir);
  88.          walkfrom(x, y, typ);
  89.      }
  90.  }
  91.  #endif /* ?MICRO */
  92.  

mazexy

  1.  /* find random point in generated corridors,
  2.     so we don't create items in moats, bunkers, or walls */
  3.  void
  4.  mazexy(cc)
  5.  coord *cc;
  6.  {
  7.      int cpt = 0;
  8.  
  9.      do {
  10.          cc->x = 1 + rn2(x_maze_max);
  11.          cc->y = 1 + rn2(y_maze_max);
  12.          cpt++;
  13.      } while (cpt < 100
  14.               && levl[cc->x][cc->y].typ
  15.                      != (level.flags.corrmaze ? CORR : ROOM));
  16.      if (cpt >= 100) {
  17.          int x, y;
  18.  
  19.          /* last try */
  20.          for (x = 1; x < x_maze_max; x++)
  21.              for (y = 1; y < y_maze_max; y++) {
  22.                  cc->x = x;
  23.                  cc->y = y;
  24.                  if (levl[cc->x][cc->y].typ
  25.                      == (level.flags.corrmaze ? CORR : ROOM))
  26.                      return;
  27.              }
  28.          panic("mazexy: can't find a place!");
  29.      }
  30.      return;
  31.  }
  32.  

bound_digging

  1.  /* put a non-diggable boundary around the initial portion of a level map.
  2.   * assumes that no level will initially put things beyond the isok() range.
  3.   *
  4.   * we can't bound unconditionally on the last line with something in it,
  5.   * because that something might be a niche which was already reachable,
  6.   * so the boundary would be breached
  7.   *
  8.   * we can't bound unconditionally on one beyond the last line, because
  9.   * that provides a window of abuse for wallified special levels
  10.   */
  11.  void
  12.  bound_digging()
  13.  {
  14.      int x, y;
  15.      unsigned typ;
  16.      struct rm *lev;
  17.      boolean found, nonwall;
  18.      int xmin, xmax, ymin, ymax;
  19.  
  20.      if (Is_earthlevel(&u.uz))
  21.          return; /* everything diggable here */
  22.  
  23.      found = nonwall = FALSE;
  24.      for (xmin = 0; !found && xmin <= COLNO; xmin++) {
  25.          lev = &levl[xmin][0];
  26.          for (y = 0; y <= ROWNO - 1; y++, lev++) {
  27.              typ = lev->typ;
  28.              if (typ != STONE) {
  29.                  found = TRUE;
  30.                  if (!IS_WALL(typ))
  31.                      nonwall = TRUE;
  32.              }
  33.          }
  34.      }
  35.      xmin -= (nonwall || !level.flags.is_maze_lev) ? 2 : 1;
  36.      if (xmin < 0)
  37.          xmin = 0;
  38.  
  39.      found = nonwall = FALSE;
  40.      for (xmax = COLNO - 1; !found && xmax >= 0; xmax--) {
  41.          lev = &levl[xmax][0];
  42.          for (y = 0; y <= ROWNO - 1; y++, lev++) {
  43.              typ = lev->typ;
  44.              if (typ != STONE) {
  45.                  found = TRUE;
  46.                  if (!IS_WALL(typ))
  47.                      nonwall = TRUE;
  48.              }
  49.          }
  50.      }
  51.      xmax += (nonwall || !level.flags.is_maze_lev) ? 2 : 1;
  52.      if (xmax >= COLNO)
  53.          xmax = COLNO - 1;
  54.  
  55.      found = nonwall = FALSE;
  56.      for (ymin = 0; !found && ymin <= ROWNO; ymin++) {
  57.          lev = &levl[xmin][ymin];
  58.          for (x = xmin; x <= xmax; x++, lev += ROWNO) {
  59.              typ = lev->typ;
  60.              if (typ != STONE) {
  61.                  found = TRUE;
  62.                  if (!IS_WALL(typ))
  63.                      nonwall = TRUE;
  64.              }
  65.          }
  66.      }
  67.      ymin -= (nonwall || !level.flags.is_maze_lev) ? 2 : 1;
  68.  
  69.      found = nonwall = FALSE;
  70.      for (ymax = ROWNO - 1; !found && ymax >= 0; ymax--) {
  71.          lev = &levl[xmin][ymax];
  72.          for (x = xmin; x <= xmax; x++, lev += ROWNO) {
  73.              typ = lev->typ;
  74.              if (typ != STONE) {
  75.                  found = TRUE;
  76.                  if (!IS_WALL(typ))
  77.                      nonwall = TRUE;
  78.              }
  79.          }
  80.      }
  81.      ymax += (nonwall || !level.flags.is_maze_lev) ? 2 : 1;
  82.  
  83.      for (x = 0; x < COLNO; x++)
  84.          for (y = 0; y < ROWNO; y++)
  85.              if (y <= ymin || y >= ymax || x <= xmin || x >= xmax) {
  86.  #ifdef DCC30_BUG
  87.                  lev = &levl[x][y];
  88.                  lev->wall_info |= W_NONDIGGABLE;
  89.  #else
  90.                  levl[x][y].wall_info |= W_NONDIGGABLE;
  91.  #endif
  92.              }
  93.  }
  94.  

mkportal

  1.  void
  2.  mkportal(x, y, todnum, todlevel)
  3.  xchar x, y, todnum, todlevel;
  4.  {
  5.      /* a portal "trap" must be matched by a
  6.         portal in the destination dungeon/dlevel */
  7.      struct trap *ttmp = maketrap(x, y, MAGIC_PORTAL);
  8.  
  9.      if (!ttmp) {
  10.          impossible("portal on top of portal??");
  11.          return;
  12.      }
  13.      debugpline4("mkportal: at <%d,%d>, to %s, level %d", x, y,
  14.                  dungeons[todnum].dname, todlevel);
  15.      ttmp->dst.dnum = todnum;
  16.      ttmp->dst.dlevel = todlevel;
  17.      return;
  18.  }
  19.  

fumaroles

  1.  void
  2.  fumaroles()
  3.  {
  4.      xchar n;
  5.      boolean snd = FALSE, loud = FALSE;
  6.  
  7.      for (n = rn2(3) + 2; n; n--) {
  8.          xchar x = rn1(COLNO - 4, 3);
  9.          xchar y = rn1(ROWNO - 4, 3);
  10.  
  11.          if (levl[x][y].typ == LAVAPOOL) {
  12.              NhRegion *r = create_gas_cloud(x, y, 4 + rn2(5), rn1(10, 5));
  13.  
  14.              clear_heros_fault(r);
  15.              snd = TRUE;
  16.              if (distu(x, y) < 15)
  17.                  loud = TRUE;
  18.          }
  19.      }
  20.      if (snd && !Deaf)
  21.          Norep("You hear a %swhoosh!", loud ? "loud " : "");
  22.  }
  23.  
  24.  /*
  25.   * Special waterlevel stuff in endgame (TH).
  26.   *
  27.   * Some of these functions would probably logically belong to some
  28.   * other source files, but they are all so nicely encapsulated here.
  29.   */
  30.  
  31.  static struct bubble *bbubbles, *ebubbles;
  32.  
  33.  static struct trap *wportal;
  34.  static int xmin, ymin, xmax, ymax; /* level boundaries */
  35.  /* bubble movement boundaries */
  36.  #define bxmin (xmin + 1)
  37.  #define bymin (ymin + 1)
  38.  #define bxmax (xmax - 1)
  39.  #define bymax (ymax - 1)
  40.  
  41.  STATIC_DCL void NDECL(set_wportal);
  42.  STATIC_DCL void FDECL(mk_bubble, (int, int, int));
  43.  STATIC_DCL void FDECL(mv_bubble, (struct bubble *, int, int, BOOLEAN_P));
  44.  

movebubbles

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

water_friction

  1.  /* when moving in water, possibly (1 in 3) alter the intended destination */
  2.  void
  3.  water_friction()
  4.  {
  5.      int x, y, dx, dy;
  6.      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)
  33.          pline("Water turbulence affects your movements.");
  34.  }
  35.  

save_waterlevel

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

restore_waterlevel

  1.  void
  2.  restore_waterlevel(fd)
  3.  int fd;
  4.  {
  5.      struct bubble *b = (struct bubble *) 0, *btmp;
  6.      int i, n;
  7.  
  8.      if (!Is_waterlevel(&u.uz) && !Is_airlevel(&u.uz))
  9.          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.  }
  34.  

waterbody_name

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

set_wportal

  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)
  7.              return;
  8.      impossible("set_wportal(): no portal!");
  9.  }
  10.  

setup_waterlevel

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

unsetup_waterlevel

  1.  STATIC_OVL void
  2.  unsetup_waterlevel()
  3.  {
  4.      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.  }
  14.  

mk_bubble

  1.  STATIC_OVL void
  2.  mk_bubble(x, y, n)
  3.  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 bm2[] = { 2, 1, 0x3 },
  13.                   bm3[] = { 3, 2, 0x7, 0x7 },
  14.                   bm4[] = { 4, 3, 0x6, 0xf, 0x6 },
  15.                   bm5[] = { 5, 3, 0xe, 0x1f, 0xe },
  16.                   bm6[] = { 6, 4, 0x1e, 0x3f, 0x3f, 0x1e },
  17.                   bm7[] = { 7, 4, 0x3e, 0x7f, 0x7f, 0x3e },
  18.                   bm8[] = { 8, 4, 0x7e, 0xff, 0xff, 0x7e },
  19.                   *bmask[] = { bm2, bm3, bm4, bm5, bm6, bm7, bm8 };
  20.      struct bubble *b;
  21.  
  22.      if (x >= bxmax || y >= bymax)
  23.          return;
  24.      if (n >= SIZE(bmask)) {
  25.          impossible("n too large (mk_bubble)");
  26.          n = SIZE(bmask) - 1;
  27.      }
  28.      if (bmask[n][1] > MAX_BMASK) {
  29.          panic("bmask size is larger than MAX_BMASK");
  30.      }
  31.      b = (struct bubble *) alloc(sizeof(struct bubble));
  32.      if ((x + (int) bmask[n][0] - 1) > bxmax)
  33.          x = bxmax - bmask[n][0] + 1;
  34.      if ((y + (int) bmask[n][1] - 1) > bymax)
  35.          y = bymax - bmask[n][1] + 1;
  36.      b->x = x;
  37.      b->y = y;
  38.      b->dx = 1 - rn2(3);
  39.      b->dy = 1 - rn2(3);
  40.      /* y dimension is the length of bitmap data - see bmask above */
  41.      (void) memcpy((genericptr_t) b->bm, (genericptr_t) bmask[n],
  42.                    (bmask[n][1] + 2) * sizeof(b->bm[0]));
  43.      b->cons = 0;
  44.      if (!bbubbles)
  45.          bbubbles = b;
  46.      if (ebubbles) {
  47.          ebubbles->next = b;
  48.          b->prev = ebubbles;
  49.      } else
  50.          b->prev = (struct bubble *) 0;
  51.      b->next = (struct bubble *) 0;
  52.      ebubbles = b;
  53.      mv_bubble(b, 0, 0, TRUE);
  54.  }
  55.  

mv_bubble

  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.  struct bubble *b;
  12.  int dx, dy;
  13.  boolean ini;
  14.  {
  15.      int x, y, i, j, colli = 0;
  16.      struct container *cons, *ctemp;
  17.  
  18.      /* clouds move slowly */
  19.      if (!Is_airlevel(&u.uz) || !rn2(6)) {
  20.          /* move bubble */
  21.          if (dx < -1 || dx > 1 || dy < -1 || dy > 1) {
  22.              /* pline("mv_bubble: dx = %d, dy = %d", dx, dy); */
  23.              dx = sgn(dx);
  24.              dy = sgn(dy);
  25.          }
  26.  
  27.          /*
  28.           * collision with level borders?
  29.           *      1 = horizontal border, 2 = vertical, 3 = corner
  30.           */
  31.          if (b->x <= bxmin)
  32.              colli |= 2;
  33.          if (b->y <= bymin)
  34.              colli |= 1;
  35.          if ((int) (b->x + b->bm[0] - 1) >= bxmax)
  36.              colli |= 2;
  37.          if ((int) (b->y + b->bm[1] - 1) >= bymax)
  38.              colli |= 1;
  39.  
  40.          if (b->x < bxmin) {
  41.              pline("bubble xmin: x = %d, xmin = %d", b->x, bxmin);
  42.              b->x = bxmin;
  43.          }
  44.          if (b->y < bymin) {
  45.              pline("bubble ymin: y = %d, ymin = %d", b->y, bymin);
  46.              b->y = bymin;
  47.          }
  48.          if ((int) (b->x + b->bm[0] - 1) > bxmax) {
  49.              pline("bubble xmax: x = %d, xmax = %d", b->x + b->bm[0] - 1,
  50.                    bxmax);
  51.              b->x = bxmax - b->bm[0] + 1;
  52.          }
  53.          if ((int) (b->y + b->bm[1] - 1) > bymax) {
  54.              pline("bubble ymax: y = %d, ymax = %d", b->y + b->bm[1] - 1,
  55.                    bymax);
  56.              b->y = bymax - b->bm[1] + 1;
  57.          }
  58.  
  59.          /* bounce if we're trying to move off the border */
  60.          if (b->x == bxmin && dx < 0)
  61.              dx = -dx;
  62.          if (b->x + b->bm[0] - 1 == bxmax && dx > 0)
  63.              dx = -dx;
  64.          if (b->y == bymin && dy < 0)
  65.              dy = -dy;
  66.          if (b->y + b->bm[1] - 1 == bymax && dy > 0)
  67.              dy = -dy;
  68.  
  69.          b->x += dx;
  70.          b->y += dy;
  71.      }
  72.  
  73.      /* draw the bubbles */
  74.      for (i = 0, x = b->x; i < (int) b->bm[0]; i++, x++)
  75.          for (j = 0, y = b->y; j < (int) b->bm[1]; j++, y++)
  76.              if (b->bm[j + 2] & (1 << i)) {
  77.                  if (Is_waterlevel(&u.uz)) {
  78.                      levl[x][y].typ = AIR;
  79.                      levl[x][y].lit = 1;
  80.                      unblock_point(x, y);
  81.                  } else if (Is_airlevel(&u.uz)) {
  82.                      levl[x][y].typ = CLOUD;
  83.                      levl[x][y].lit = 1;
  84.                      block_point(x, y);
  85.                  }
  86.              }
  87.  
  88.      if (Is_waterlevel(&u.uz)) {
  89.          /* replace contents of bubble */
  90.          for (cons = b->cons; cons; cons = ctemp) {
  91.              ctemp = cons->next;
  92.              cons->x += dx;
  93.              cons->y += dy;
  94.  
  95.              switch (cons->what) {
  96.              case CONS_OBJ: {
  97.                  struct obj *olist, *otmp;
  98.  
  99.                  for (olist = (struct obj *) cons->list; olist; olist = otmp) {
  100.                      otmp = olist->nexthere;
  101.                      place_object(olist, cons->x, cons->y);
  102.                  }
  103.                  break;
  104.              }
  105.  
  106.              case CONS_MON: {
  107.                  struct monst *mon = (struct monst *) cons->list;
  108.                  (void) mnearto(mon, cons->x, cons->y, TRUE);
  109.                  break;
  110.              }
  111.  
  112.              case CONS_HERO: {
  113.                  int ux0 = u.ux, uy0 = u.uy;
  114.  
  115.                  /* change u.ux0 and u.uy0? */
  116.                  u.ux = cons->x;
  117.                  u.uy = cons->y;
  118.                  newsym(ux0, uy0); /* clean up old position */
  119.  
  120.                  if (MON_AT(cons->x, cons->y)) {
  121.                      mnexto(m_at(cons->x, cons->y));
  122.                  }
  123.                  break;
  124.              }
  125.  
  126.              case CONS_TRAP: {
  127.                  struct trap *btrap = (struct trap *) cons->list;
  128.                  btrap->tx = cons->x;
  129.                  btrap->ty = cons->y;
  130.                  break;
  131.              }
  132.  
  133.              default:
  134.                  impossible("mv_bubble: unknown bubble contents");
  135.                  break;
  136.              }
  137.              free((genericptr_t) cons);
  138.          }
  139.          b->cons = 0;
  140.      }
  141.  
  142.      /* boing? */
  143.      switch (colli) {
  144.      case 1:
  145.          b->dy = -b->dy;
  146.          break;
  147.      case 3:
  148.          b->dy = -b->dy; /* fall through */
  149.      case 2:
  150.          b->dx = -b->dx;
  151.          break;
  152.      default:
  153.          /* sometimes alter direction for fun anyway
  154.             (higher probability for stationary bubbles) */
  155.          if (!ini && ((b->dx || b->dy) ? !rn2(20) : !rn2(5))) {
  156.              b->dx = 1 - rn2(3);
  157.              b->dy = 1 - rn2(3);
  158.          }
  159.      }
  160.  }
  161.  
  162.  /*mkmaze.c*/