Source:NetHack 3.6.0/src/mkmaze.c

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

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

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.

Top of file

  1.  /* NetHack 3.6	mkmaze.c	$NHDT-Date: 1449269918 2015/12/04 22:58:38 $  $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.42 $ */
  2.  /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3.  /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5.  #include "hack.h"
  6.  #include "sp_lev.h"
  7.  #include "lev.h" /* save & restore info */
  8.  
  9.  /* from sp_lev.c, for fixup_special() */
  10.  extern lev_region *lregions;
  11.  extern int num_lregions;
  12.  
  13.  STATIC_DCL boolean FDECL(iswall, (int, int));
  14.  STATIC_DCL boolean FDECL(iswall_or_stone, (int, int));
  15.  STATIC_DCL boolean FDECL(is_solid, (int, int));
  16.  STATIC_DCL int FDECL(extend_spine, (int[3][3], int, int, int));
  17.  STATIC_DCL boolean FDECL(okay, (int, int, int));
  18.  STATIC_DCL void FDECL(maze0xy, (coord *));
  19.  STATIC_DCL boolean FDECL(put_lregion_here, (XCHAR_P, XCHAR_P, XCHAR_P,
  20.                                              XCHAR_P, XCHAR_P, XCHAR_P,
  21.                                              XCHAR_P, BOOLEAN_P, d_level *));
  22.  STATIC_DCL void NDECL(fixup_special);
  23.  STATIC_DCL void NDECL(setup_waterlevel);
  24.  STATIC_DCL void NDECL(unsetup_waterlevel);
  25.  
  26.  /* adjust a coordinate one step in the specified direction */
  27.  #define mz_move(X, Y, dir) \
  28.      do {                                                         \
  29.          switch (dir) {                                           \
  30.          case 0:  --(Y);  break;                                  \
  31.          case 1:  (X)++;  break;                                  \
  32.          case 2:  (Y)++;  break;                                  \
  33.          case 3:  --(X);  break;                                  \
  34.          default: panic("mz_move: bad direction %d", dir);        \
  35.          }                                                        \
  36.      } while (0)
  37.  

iswall

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

iswall_or_stone

  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))
  9.          return TRUE;
  10.  
  11.      type = levl[x][y].typ;
  12.      return (boolean) (type == STONE
  13.                        || IS_WALL(type) || IS_DOOR(type)
  14.                        || type == SDOOR || type == IRONBARS);
  15.  }
  16.  

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.  

wallification

  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] = { VWALL, HWALL,    HWALL,    HWALL,
  21.                                       VWALL, TRCORNER, TLCORNER, TDWALL,
  22.                                       VWALL, BRCORNER, BLCORNER, TUWALL,
  23.                                       VWALL, TLWALL,   TRWALL,   CROSSWALL };
  24.  
  25.      /* sanity check on incoming variables */
  26.      if (x1 < 0 || x2 >= COLNO || x1 > x2 || y1 < 0 || y2 >= ROWNO || y1 > y2)
  27.          panic("wallification: bad bounds (%d,%d) to (%d,%d)", x1, y1, x2, y2);
  28.  
  29.      /* Step 1: change walls surrounded by rock to rock. */
  30.      for (x = x1; x <= x2; x++)
  31.          for (y = y1; y <= y2; y++) {
  32.              lev = &levl[x][y];
  33.              type = lev->typ;
  34.              if (IS_WALL(type) && type != DBWALL) {
  35.                  if (is_solid(x - 1, y - 1) && is_solid(x - 1, y)
  36.                      && is_solid(x - 1, y + 1) && is_solid(x, y - 1)
  37.                      && is_solid(x, y + 1) && is_solid(x + 1, y - 1)
  38.                      && is_solid(x + 1, y) && is_solid(x + 1, y + 1))
  39.                      lev->typ = STONE;
  40.              }
  41.          }
  42.  
  43.      /*
  44.       * Step 2: set the correct wall type.  We can't combine steps
  45.       * 1 and 2 into a single sweep because we depend on knowing if
  46.       * the surrounding positions are stone.
  47.       */
  48.      for (x = x1; x <= x2; x++)
  49.          for (y = y1; y <= y2; y++) {
  50.              lev = &levl[x][y];
  51.              type = lev->typ;
  52.              if (!(IS_WALL(type) && type != DBWALL))
  53.                  continue;
  54.  
  55.              /* set the locations TRUE if rock or wall or out of bounds */
  56.              locale[0][0] = iswall_or_stone(x - 1, y - 1);
  57.              locale[1][0] = iswall_or_stone(x, y - 1);
  58.              locale[2][0] = iswall_or_stone(x + 1, y - 1);
  59.  
  60.              locale[0][1] = iswall_or_stone(x - 1, y);
  61.              locale[2][1] = iswall_or_stone(x + 1, y);
  62.  
  63.              locale[0][2] = iswall_or_stone(x - 1, y + 1);
  64.              locale[1][2] = iswall_or_stone(x, y + 1);
  65.              locale[2][2] = iswall_or_stone(x + 1, y + 1);
  66.  
  67.              /* determine if wall should extend to each direction NSEW */
  68.              bits = (extend_spine(locale, iswall(x, y - 1), 0, -1) << 3)
  69.                     | (extend_spine(locale, iswall(x, y + 1), 0, 1) << 2)
  70.                     | (extend_spine(locale, iswall(x + 1, y), 1, 0) << 1)
  71.                     | extend_spine(locale, iswall(x - 1, y), -1, 0);
  72.  
  73.              /* don't change typ if wall is free-standing */
  74.              if (bits)
  75.                  lev->typ = spine_array[bits];
  76.          }
  77.  }
  78.  

okay

  1.  STATIC_OVL boolean
  2.  okay(x, y, dir)
  3.  int x, y;
  4.  register 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 != 0)
  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;
  25.          hx = COLNO - 1;
  26.          ly = 1;
  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.      oneshot = TRUE;
  43.      for (x = lx; x <= hx; x++)
  44.          for (y = ly; y <= hy; y++)
  45.              if (put_lregion_here(x, y, nlx, nly, nhx, nhy, rtype, oneshot,
  46.                                   lev))
  47.                  return;
  48.  
  49.      impossible("Couldn't place lregion type %d!", rtype);
  50.  }
  51.  

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.  

fixup_special

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

makemaz

  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)
  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.              fixup_special();
  59.              /* some levels can end up with monsters
  60.                 on dead mon list, including light source monsters */
  61.              dmonsfree();
  62.              return; /* no mazification right now */
  63.          }
  64.          impossible("Couldn't load \"%s\" - making a maze.", protofile);
  65.      }
  66.  
  67.      level.flags.is_maze_lev = TRUE;
  68.      level.flags.corrmaze = !rn2(3);
  69.  
  70.      if (level.flags.corrmaze)
  71.          for (x = 2; x < x_maze_max; x++)
  72.              for (y = 2; y < y_maze_max; y++)
  73.                  levl[x][y].typ = STONE;
  74.      else
  75.          for (x = 2; x <= x_maze_max; x++)
  76.              for (y = 2; y <= y_maze_max; y++)
  77.                  levl[x][y].typ = ((x % 2) && (y % 2)) ? STONE : HWALL;
  78.  
  79.      maze0xy(&mm);
  80.      walkfrom((int) mm.x, (int) mm.y, 0);
  81.      /* put a boulder at the maze center */
  82.      (void) mksobj_at(BOULDER, (int) mm.x, (int) mm.y, TRUE, FALSE);
  83.  
  84.      if (!level.flags.corrmaze)
  85.          wallification(2, 2, x_maze_max, y_maze_max);
  86.  
  87.      mazexy(&mm);
  88.      mkstairs(mm.x, mm.y, 1, (struct mkroom *) 0); /* up */
  89.      if (!Invocation_lev(&u.uz)) {
  90.          mazexy(&mm);
  91.          mkstairs(mm.x, mm.y, 0, (struct mkroom *) 0); /* down */
  92.      } else { /* choose "vibrating square" location */
  93.  #define x_maze_min 2
  94.  #define y_maze_min 2
  95.  /*
  96.   * Pick a position where the stairs down to Moloch's Sanctum
  97.   * level will ultimately be created.  At that time, an area
  98.   * will be altered:  walls removed, moat and traps generated,
  99.   * boulders destroyed.  The position picked here must ensure
  100.   * that that invocation area won't extend off the map.
  101.   *
  102.   * We actually allow up to 2 squares around the usual edge of
  103.   * the area to get truncated; see mkinvokearea(mklev.c).
  104.   */
  105.  #define INVPOS_X_MARGIN (6 - 2)
  106.  #define INVPOS_Y_MARGIN (5 - 2)
  107.  #define INVPOS_DISTANCE 11
  108.          int x_range = x_maze_max - x_maze_min - 2 * INVPOS_X_MARGIN - 1,
  109.              y_range = y_maze_max - y_maze_min - 2 * INVPOS_Y_MARGIN - 1;
  110.  
  111.          if (x_range <= INVPOS_X_MARGIN || y_range <= INVPOS_Y_MARGIN
  112.              || (x_range * y_range) <= (INVPOS_DISTANCE * INVPOS_DISTANCE)) {
  113.              debugpline2("inv_pos: maze is too small! (%d x %d)",
  114.                          x_maze_max, y_maze_max);
  115.          }
  116.          inv_pos.x = inv_pos.y = 0; /*{occupied() => invocation_pos()}*/
  117.          do {
  118.              x = rn1(x_range, x_maze_min + INVPOS_X_MARGIN + 1);
  119.              y = rn1(y_range, y_maze_min + INVPOS_Y_MARGIN + 1);
  120.              /* we don't want it to be too near the stairs, nor
  121.                 to be on a spot that's already in use (wall|trap) */
  122.          } while (x == xupstair || y == yupstair /*(direct line)*/
  123.                   || abs(x - xupstair) == abs(y - yupstair)
  124.                   || distmin(x, y, xupstair, yupstair) <= INVPOS_DISTANCE
  125.                   || !SPACE_POS(levl[x][y].typ) || occupied(x, y));
  126.          inv_pos.x = x;
  127.          inv_pos.y = y;
  128.          maketrap(inv_pos.x, inv_pos.y, VIBRATING_SQUARE);
  129.  #undef INVPOS_X_MARGIN
  130.  #undef INVPOS_Y_MARGIN
  131.  #undef INVPOS_DISTANCE
  132.  #undef x_maze_min
  133.  #undef y_maze_min
  134.      }
  135.  
  136.      /* place branch stair or portal */
  137.      place_branch(Is_branchlev(&u.uz), 0, 0);
  138.  
  139.      for (x = rn1(8, 11); x; x--) {
  140.          mazexy(&mm);
  141.          (void) mkobj_at(rn2(2) ? GEM_CLASS : 0, mm.x, mm.y, TRUE);
  142.      }
  143.      for (x = rn1(10, 2); x; x--) {
  144.          mazexy(&mm);
  145.          (void) mksobj_at(BOULDER, mm.x, mm.y, TRUE, FALSE);
  146.      }
  147.      for (x = rn2(3); x; x--) {
  148.          mazexy(&mm);
  149.          (void) makemon(&mons[PM_MINOTAUR], mm.x, mm.y, NO_MM_FLAGS);
  150.      }
  151.      for (x = rn1(5, 7); x; x--) {
  152.          mazexy(&mm);
  153.          (void) makemon((struct permonst *) 0, mm.x, mm.y, NO_MM_FLAGS);
  154.      }
  155.      for (x = rn1(6, 7); x; x--) {
  156.          mazexy(&mm);
  157.          (void) mkgold(0L, mm.x, mm.y);
  158.      }
  159.      for (x = rn1(6, 7); x; x--)
  160.          mktrap(0, 1, (struct mkroom *) 0, (coord *) 0);
  161.  }
  162.  

walkfrom

For microcompilations

  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.  

Non-microcompilations

  1.  void
  2.  walkfrom(x, y, typ)
  3.  int x, y;
  4.  schar typ;
  5.  {
  6.      register int q, a, dir;
  7.      int dirs[4];
  8.  
  9.      if (!typ) {
  10.          if (level.flags.corrmaze)
  11.              typ = CORR;
  12.          else
  13.              typ = ROOM;
  14.      }
  15.  
  16.      if (!IS_DOOR(levl[x][y].typ)) {
  17.          /* might still be on edge of MAP, so don't overwrite */
  18.          levl[x][y].typ = typ;
  19.          levl[x][y].flags = 0;
  20.      }
  21.  
  22.      while (1) {
  23.          q = 0;
  24.          for (a = 0; a < 4; a++)
  25.              if (okay(x, y, a))
  26.                  dirs[q++] = a;
  27.          if (!q)
  28.              return;
  29.          dir = dirs[rn2(q)];
  30.          mz_move(x, y, dir);
  31.          levl[x][y].typ = typ;
  32.          mz_move(x, y, dir);
  33.          walkfrom(x, y, typ);
  34.      }
  35.  }
  36.  #endif /* ?MICRO */
  37.  

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 = 3 + 2 * rn2((x_maze_max >> 1) - 1);
  11.          cc->y = 3 + 2 * rn2((y_maze_max >> 1) - 1);
  12.          cpt++;
  13.      } while (cpt < 100
  14.               && levl[cc->x][cc->y].typ
  15.                      != (level.flags.corrmaze ? CORR : ROOM));
  16.      if (cpt >= 100) {
  17.          register int x, y;
  18.          /* last try */
  19.          for (x = 0; x < (x_maze_max >> 1) - 1; x++)
  20.              for (y = 0; y < (y_maze_max >> 1) - 1; y++) {
  21.                  cc->x = 3 + 2 * x;
  22.                  cc->y = 3 + 2 * y;
  23.                  if (levl[cc->x][cc->y].typ
  24.                      == (level.flags.corrmaze ? CORR : ROOM))
  25.                      return;
  26.              }
  27.          panic("mazexy: can't find a place!");
  28.      }
  29.      return;
  30.  }
  31.  

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.      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))
  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.          if (levl[x][y].typ == LAVAPOOL) {
  11.              NhRegion *r = create_gas_cloud(x, y, 4 + rn2(5), rn1(10, 5));
  12.              clear_heros_fault(r);
  13.              snd = TRUE;
  14.              if (distu(x, y) < 15)
  15.                  loud = TRUE;
  16.          }
  17.      }
  18.      if (snd && !Deaf)
  19.          Norep("You hear a %swhoosh!", loud ? "loud " : "");
  20.  }
  21.  

Special endgame waterlevel stuff

  1.  /*
  2.   * Special waterlevel stuff in endgame (TH).
  3.   *
  4.   * Some of these functions would probably logically belong to some
  5.   * other source files, but they are all so nicely encapsulated here.
  6.   */
  7.  
  8.  #ifdef DEBUG
  9.  /* to ease the work of debuggers at this stage */
  10.  #define register
  11.  #endif
  12.  
  13.  #define CONS_OBJ 0
  14.  #define CONS_MON 1
  15.  #define CONS_HERO 2
  16.  #define CONS_TRAP 3
  17.  
  18.  static struct bubble *bbubbles, *ebubbles;
  19.  
  20.  static struct trap *wportal;
  21.  static int xmin, ymin, xmax, ymax; /* level boundaries */
  22.  /* bubble movement boundaries */
  23.  #define bxmin (xmin + 1)
  24.  #define bymin (ymin + 1)
  25.  #define bxmax (xmax - 1)
  26.  #define bymax (ymax - 1)
  27.  
  28.  STATIC_DCL void NDECL(set_wportal);
  29.  STATIC_DCL void FDECL(mk_bubble, (int, int, int));
  30.  STATIC_DCL void FDECL(mv_bubble, (struct bubble *, int, int, BOOLEAN_P));
  31.  

movebubbles

  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 = { 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.          register 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.      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)
  33.          pline("Water turbulence affects your movements.");
  34.  }
  35.  

save_waterlevel

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

waterbody_name

  1.  const char *
  2.  waterbody_name(x, y)
  3.  xchar x, y;
  4.  {
  5.      register 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 "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 "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.      register int x, y;
  5.      register int xskip, yskip;
  6.      register int water_glyph = cmap_to_glyph(S_water);
  7.      register int 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.      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.  }
  14.  

mk_bubble

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

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.  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.      /* 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*/