Source:NetHack 3.6.0/src/mkmap.c

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

Below is the full text to mkmap.c from the source code of NetHack 3.6.0. To link to a particular line, write [[Source:NetHack 3.6.0/src/mkmap.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	mkmap.c	$NHDT-Date: 1432512767 2015/05/25 00:12:47 $  $NHDT-Branch: master $:$NHDT-Revision: 1.16 $ */
  2.  /* Copyright (c) J. C. Collet, M. Stephenson and D. Cohrs, 1992   */
  3.  /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5.  #include "hack.h"
  6.  #include "sp_lev.h"
  7.  
  8.  #define HEIGHT (ROWNO - 1)
  9.  #define WIDTH (COLNO - 2)
  10.  
  11.  STATIC_DCL void FDECL(init_map, (SCHAR_P));
  12.  STATIC_DCL void FDECL(init_fill, (SCHAR_P, SCHAR_P));
  13.  STATIC_DCL schar FDECL(get_map, (int, int, SCHAR_P));
  14.  STATIC_DCL void FDECL(pass_one, (SCHAR_P, SCHAR_P));
  15.  STATIC_DCL void FDECL(pass_two, (SCHAR_P, SCHAR_P));
  16.  STATIC_DCL void FDECL(pass_three, (SCHAR_P, SCHAR_P));
  17.  STATIC_DCL void NDECL(wallify_map);
  18.  STATIC_DCL void FDECL(join_map, (SCHAR_P, SCHAR_P));
  19.  STATIC_DCL void FDECL(finish_map,
  20.                        (SCHAR_P, SCHAR_P, BOOLEAN_P, BOOLEAN_P, BOOLEAN_P));
  21.  STATIC_DCL void FDECL(remove_room, (unsigned));
  22.  void FDECL(mkmap, (lev_init *));
  23.  
  24.  static char *new_locations;
  25.  int min_rx, max_rx, min_ry, max_ry; /* rectangle bounds for regions */
  26.  static int n_loc_filled;
  27.  

init_map

  1.  STATIC_OVL void
  2.  init_map(bg_typ)
  3.  schar bg_typ;
  4.  {
  5.      register int i, j;
  6.  
  7.      for (i = 1; i < COLNO; i++)
  8.          for (j = 0; j < ROWNO; j++)
  9.              levl[i][j].typ = bg_typ;
  10.  }
  11.  

init_fill

  1.  STATIC_OVL void
  2.  init_fill(bg_typ, fg_typ)
  3.  schar bg_typ, fg_typ;
  4.  {
  5.      register int i, j;
  6.      long limit, count;
  7.  
  8.      limit = (WIDTH * HEIGHT * 2) / 5;
  9.      count = 0;
  10.      while (count < limit) {
  11.          i = rn1(WIDTH - 1, 2);
  12.          j = rnd(HEIGHT - 1);
  13.          if (levl[i][j].typ == bg_typ) {
  14.              levl[i][j].typ = fg_typ;
  15.              count++;
  16.          }
  17.      }
  18.  }
  19.  

get_map

  1.  STATIC_OVL schar
  2.  get_map(col, row, bg_typ)
  3.  int col, row;
  4.  schar bg_typ;
  5.  {
  6.      if (col <= 0 || row < 0 || col > WIDTH || row >= HEIGHT)
  7.          return bg_typ;
  8.      return levl[col][row].typ;
  9.  }
  10.  

pass_one

  1.  static int dirs[16] = { -1, -1 /**/, -1, 0 /**/,  -1, 1 /**/, 0, -1 /**/,
  2.                          0,  1 /**/,  1,  -1 /**/, 1,  0 /**/, 1, 1 };
  3.  
  4.  STATIC_OVL void
  5.  pass_one(bg_typ, fg_typ)
  6.  schar bg_typ, fg_typ;
  7.  {
  8.      register int i, j;
  9.      short count, dr;
  10.  
  11.      for (i = 2; i <= WIDTH; i++)
  12.          for (j = 1; j < HEIGHT; j++) {
  13.              for (count = 0, dr = 0; dr < 8; dr++)
  14.                  if (get_map(i + dirs[dr * 2], j + dirs[(dr * 2) + 1], bg_typ)
  15.                      == fg_typ)
  16.                      count++;
  17.  
  18.              switch (count) {
  19.              case 0: /* death */
  20.              case 1:
  21.              case 2:
  22.                  levl[i][j].typ = bg_typ;
  23.                  break;
  24.              case 5:
  25.              case 6:
  26.              case 7:
  27.              case 8:
  28.                  levl[i][j].typ = fg_typ;
  29.                  break;
  30.              default:
  31.                  break;
  32.              }
  33.          }
  34.  }
  35.  

pass_two

  1.  #define new_loc(i, j) *(new_locations + ((j) * (WIDTH + 1)) + (i))
  2.  
  3.  STATIC_OVL void
  4.  pass_two(bg_typ, fg_typ)
  5.  schar bg_typ, fg_typ;
  6.  {
  7.      register int i, j;
  8.      short count, dr;
  9.  
  10.      for (i = 2; i <= WIDTH; i++)
  11.          for (j = 1; j < HEIGHT; j++) {
  12.              for (count = 0, dr = 0; dr < 8; dr++)
  13.                  if (get_map(i + dirs[dr * 2], j + dirs[(dr * 2) + 1], bg_typ)
  14.                      == fg_typ)
  15.                      count++;
  16.              if (count == 5)
  17.                  new_loc(i, j) = bg_typ;
  18.              else
  19.                  new_loc(i, j) = get_map(i, j, bg_typ);
  20.          }
  21.  
  22.      for (i = 2; i <= WIDTH; i++)
  23.          for (j = 1; j < HEIGHT; j++)
  24.              levl[i][j].typ = new_loc(i, j);
  25.  }
  26.  

pass_three

  1.  STATIC_OVL void
  2.  pass_three(bg_typ, fg_typ)
  3.  schar bg_typ, fg_typ;
  4.  {
  5.      register int i, j;
  6.      short count, dr;
  7.  
  8.      for (i = 2; i <= WIDTH; i++)
  9.          for (j = 1; j < HEIGHT; j++) {
  10.              for (count = 0, dr = 0; dr < 8; dr++)
  11.                  if (get_map(i + dirs[dr * 2], j + dirs[(dr * 2) + 1], bg_typ)
  12.                      == fg_typ)
  13.                      count++;
  14.              if (count < 3)
  15.                  new_loc(i, j) = bg_typ;
  16.              else
  17.                  new_loc(i, j) = get_map(i, j, bg_typ);
  18.          }
  19.  
  20.      for (i = 2; i <= WIDTH; i++)
  21.          for (j = 1; j < HEIGHT; j++)
  22.              levl[i][j].typ = new_loc(i, j);
  23.  }
  24.  

flood_fill_rm

  1.  /*
  2.   * use a flooding algorithm to find all locations that should
  3.   * have the same rm number as the current location.
  4.   * if anyroom is TRUE, use IS_ROOM to check room membership instead of
  5.   * exactly matching levl[sx][sy].typ and walls are included as well.
  6.   */
  7.  void
  8.  flood_fill_rm(sx, sy, rmno, lit, anyroom)
  9.  int sx;
  10.  register int sy;
  11.  register int rmno;
  12.  boolean lit;
  13.  boolean anyroom;
  14.  {
  15.      register int i;
  16.      int nx;
  17.      schar fg_typ = levl[sx][sy].typ;
  18.  
  19.      /* back up to find leftmost uninitialized location */
  20.      while (sx > 0 && (anyroom ? IS_ROOM(levl[sx][sy].typ)
  21.                                : levl[sx][sy].typ == fg_typ)
  22.             && (int) levl[sx][sy].roomno != rmno)
  23.          sx--;
  24.      sx++; /* compensate for extra decrement */
  25.  
  26.      /* assume sx,sy is valid */
  27.      if (sx < min_rx)
  28.          min_rx = sx;
  29.      if (sy < min_ry)
  30.          min_ry = sy;
  31.  
  32.      for (i = sx; i <= WIDTH && levl[i][sy].typ == fg_typ; i++) {
  33.          levl[i][sy].roomno = rmno;
  34.          levl[i][sy].lit = lit;
  35.          if (anyroom) {
  36.              /* add walls to room as well */
  37.              register int ii, jj;
  38.              for (ii = (i == sx ? i - 1 : i); ii <= i + 1; ii++)
  39.                  for (jj = sy - 1; jj <= sy + 1; jj++)
  40.                      if (isok(ii, jj) && (IS_WALL(levl[ii][jj].typ)
  41.                                           || IS_DOOR(levl[ii][jj].typ)
  42.                                           || levl[ii][jj].typ == SDOOR)) {
  43.                          levl[ii][jj].edge = 1;
  44.                          if (lit)
  45.                              levl[ii][jj].lit = lit;
  46.                          if ((int) levl[ii][jj].roomno != rmno)
  47.                              levl[ii][jj].roomno = SHARED;
  48.                      }
  49.          }
  50.          n_loc_filled++;
  51.      }
  52.      nx = i;
  53.  
  54.      if (isok(sx, sy - 1)) {
  55.          for (i = sx; i < nx; i++)
  56.              if (levl[i][sy - 1].typ == fg_typ) {
  57.                  if ((int) levl[i][sy - 1].roomno != rmno)
  58.                      flood_fill_rm(i, sy - 1, rmno, lit, anyroom);
  59.              } else {
  60.                  if ((i > sx || isok(i - 1, sy - 1))
  61.                      && levl[i - 1][sy - 1].typ == fg_typ) {
  62.                      if ((int) levl[i - 1][sy - 1].roomno != rmno)
  63.                          flood_fill_rm(i - 1, sy - 1, rmno, lit, anyroom);
  64.                  }
  65.                  if ((i < nx - 1 || isok(i + 1, sy - 1))
  66.                      && levl[i + 1][sy - 1].typ == fg_typ) {
  67.                      if ((int) levl[i + 1][sy - 1].roomno != rmno)
  68.                          flood_fill_rm(i + 1, sy - 1, rmno, lit, anyroom);
  69.                  }
  70.              }
  71.      }
  72.      if (isok(sx, sy + 1)) {
  73.          for (i = sx; i < nx; i++)
  74.              if (levl[i][sy + 1].typ == fg_typ) {
  75.                  if ((int) levl[i][sy + 1].roomno != rmno)
  76.                      flood_fill_rm(i, sy + 1, rmno, lit, anyroom);
  77.              } else {
  78.                  if ((i > sx || isok(i - 1, sy + 1))
  79.                      && levl[i - 1][sy + 1].typ == fg_typ) {
  80.                      if ((int) levl[i - 1][sy + 1].roomno != rmno)
  81.                          flood_fill_rm(i - 1, sy + 1, rmno, lit, anyroom);
  82.                  }
  83.                  if ((i < nx - 1 || isok(i + 1, sy + 1))
  84.                      && levl[i + 1][sy + 1].typ == fg_typ) {
  85.                      if ((int) levl[i + 1][sy + 1].roomno != rmno)
  86.                          flood_fill_rm(i + 1, sy + 1, rmno, lit, anyroom);
  87.                  }
  88.              }
  89.      }
  90.  
  91.      if (nx > max_rx)
  92.          max_rx = nx - 1; /* nx is just past valid region */
  93.      if (sy > max_ry)
  94.          max_ry = sy;
  95.  }
  96.  

wallify_map

  1.  /*
  2.   *	If we have drawn a map without walls, this allows us to
  3.   *	auto-magically wallify it.  Taken from lev_main.c.
  4.   */
  5.  STATIC_OVL void
  6.  wallify_map()
  7.  {
  8.      int x, y, xx, yy;
  9.  
  10.      for (x = 1; x < COLNO; x++)
  11.          for (y = 0; y < ROWNO; y++)
  12.              if (levl[x][y].typ == STONE) {
  13.                  for (yy = y - 1; yy <= y + 1; yy++)
  14.                      for (xx = x - 1; xx <= x + 1; xx++)
  15.                          if (isok(xx, yy) && levl[xx][yy].typ == ROOM) {
  16.                              if (yy != y)
  17.                                  levl[x][y].typ = HWALL;
  18.                              else
  19.                                  levl[x][y].typ = VWALL;
  20.                          }
  21.              }
  22.  }
  23.  

join_map

  1.  STATIC_OVL void
  2.  join_map(bg_typ, fg_typ)
  3.  schar bg_typ, fg_typ;
  4.  {
  5.      register struct mkroom *croom, *croom2;
  6.  
  7.      register int i, j;
  8.      int sx, sy;
  9.      coord sm, em;
  10.  
  11.      /* first, use flood filling to find all of the regions that need joining
  12.       */
  13.      for (i = 2; i <= WIDTH; i++)
  14.          for (j = 1; j < HEIGHT; j++) {
  15.              if (levl[i][j].typ == fg_typ && levl[i][j].roomno == NO_ROOM) {
  16.                  min_rx = max_rx = i;
  17.                  min_ry = max_ry = j;
  18.                  n_loc_filled = 0;
  19.                  flood_fill_rm(i, j, nroom + ROOMOFFSET, FALSE, FALSE);
  20.                  if (n_loc_filled > 3) {
  21.                      add_room(min_rx, min_ry, max_rx, max_ry, FALSE, OROOM,
  22.                               TRUE);
  23.                      rooms[nroom - 1].irregular = TRUE;
  24.                      if (nroom >= (MAXNROFROOMS * 2))
  25.                          goto joinm;
  26.                  } else {
  27.                      /*
  28.                       * it's a tiny hole; erase it from the map to avoid
  29.                       * having the player end up here with no way out.
  30.                       */
  31.                      for (sx = min_rx; sx <= max_rx; sx++)
  32.                          for (sy = min_ry; sy <= max_ry; sy++)
  33.                              if ((int) levl[sx][sy].roomno
  34.                                  == nroom + ROOMOFFSET) {
  35.                                  levl[sx][sy].typ = bg_typ;
  36.                                  levl[sx][sy].roomno = NO_ROOM;
  37.                              }
  38.                  }
  39.              }
  40.          }
  41.  
  42.  joinm:
  43.      /*
  44.       * Ok, now we can actually join the regions with fg_typ's.
  45.       * The rooms are already sorted due to the previous loop,
  46.       * so don't call sort_rooms(), which can screw up the roomno's
  47.       * validity in the levl structure.
  48.       */
  49.      for (croom = &rooms[0], croom2 = croom + 1; croom2 < &rooms[nroom];) {
  50.          /* pick random starting and end locations for "corridor" */
  51.          if (!somexy(croom, &sm) || !somexy(croom2, &em)) {
  52.              /* ack! -- the level is going to be busted */
  53.              /* arbitrarily pick centers of both rooms and hope for the best */
  54.              impossible("No start/end room loc in join_map.");
  55.              sm.x = croom->lx + ((croom->hx - croom->lx) / 2);
  56.              sm.y = croom->ly + ((croom->hy - croom->ly) / 2);
  57.              em.x = croom2->lx + ((croom2->hx - croom2->lx) / 2);
  58.              em.y = croom2->ly + ((croom2->hy - croom2->ly) / 2);
  59.          }
  60.  
  61.          (void) dig_corridor(&sm, &em, FALSE, fg_typ, bg_typ);
  62.  
  63.          /* choose next region to join */
  64.          /* only increment croom if croom and croom2 are non-overlapping */
  65.          if (croom2->lx > croom->hx
  66.              || ((croom2->ly > croom->hy || croom2->hy < croom->ly)
  67.                  && rn2(3))) {
  68.              croom = croom2;
  69.          }
  70.          croom2++; /* always increment the next room */
  71.      }
  72.  }
  73.  

finish_map

  1.  STATIC_OVL void
  2.  finish_map(fg_typ, bg_typ, lit, walled, icedpools)
  3.  schar fg_typ, bg_typ;
  4.  boolean lit, walled, icedpools;
  5.  {
  6.      int i, j;
  7.  
  8.      if (walled)
  9.          wallify_map();
  10.  
  11.      if (lit) {
  12.          for (i = 1; i < COLNO; i++)
  13.              for (j = 0; j < ROWNO; j++)
  14.                  if ((!IS_ROCK(fg_typ) && levl[i][j].typ == fg_typ)
  15.                      || (!IS_ROCK(bg_typ) && levl[i][j].typ == bg_typ)
  16.                      || (bg_typ == TREE && levl[i][j].typ == bg_typ)
  17.                      || (walled && IS_WALL(levl[i][j].typ)))
  18.                      levl[i][j].lit = TRUE;
  19.          for (i = 0; i < nroom; i++)
  20.              rooms[i].rlit = 1;
  21.      }
  22.      /* light lava even if everything's otherwise unlit;
  23.         ice might be frozen pool rather than frozen moat */
  24.      for (i = 1; i < COLNO; i++)
  25.          for (j = 0; j < ROWNO; j++) {
  26.              if (levl[i][j].typ == LAVAPOOL)
  27.                  levl[i][j].lit = TRUE;
  28.              else if (levl[i][j].typ == ICE)
  29.                  levl[i][j].icedpool = icedpools ? ICED_POOL : ICED_MOAT;
  30.          }
  31.  }
  32.  

remove_rooms

  1.  /*
  2.   * When level processed by join_map is overlaid by a MAP, some rooms may no
  3.   * longer be valid.  All rooms in the region lx <= x < hx, ly <= y < hy are
  4.   * removed.  Rooms partially in the region are truncated.  This function
  5.   * must be called before the REGIONs or ROOMs of the map are processed, or
  6.   * those rooms will be removed as well.  Assumes roomno fields in the
  7.   * region are already cleared, and roomno and irregular fields outside the
  8.   * region are all set.
  9.   */
  10.  void
  11.  remove_rooms(lx, ly, hx, hy)
  12.  int lx, ly, hx, hy;
  13.  {
  14.      int i;
  15.      struct mkroom *croom;
  16.  
  17.      for (i = nroom - 1; i >= 0; --i) {
  18.          croom = &rooms[i];
  19.          if (croom->hx < lx || croom->lx >= hx || croom->hy < ly
  20.              || croom->ly >= hy)
  21.              continue; /* no overlap */
  22.  
  23.          if (croom->lx < lx || croom->hx >= hx || croom->ly < ly
  24.              || croom->hy >= hy) { /* partial overlap */
  25.              /* TODO: ensure remaining parts of room are still joined */
  26.  
  27.              if (!croom->irregular)
  28.                  impossible("regular room in joined map");
  29.          } else {
  30.              /* total overlap, remove the room */
  31.              remove_room((unsigned) i);
  32.          }
  33.      }
  34.  }
  35.  

remove_room

  1.  /*
  2.   * Remove roomno from the rooms array, decrementing nroom.  Also updates
  3.   * all level roomno values of affected higher numbered rooms.  Assumes
  4.   * level structure contents corresponding to roomno have already been reset.
  5.   * Currently handles only the removal of rooms that have no subrooms.
  6.   */
  7.  STATIC_OVL void
  8.  remove_room(roomno)
  9.  unsigned roomno;
  10.  {
  11.      struct mkroom *croom = &rooms[roomno];
  12.      struct mkroom *maxroom = &rooms[--nroom];
  13.      int i, j;
  14.      unsigned oroomno;
  15.  
  16.      if (croom != maxroom) {
  17.          /* since the order in the array only matters for making corridors,
  18.           * copy the last room over the one being removed on the assumption
  19.           * that corridors have already been dug. */
  20.          (void) memcpy((genericptr_t) croom, (genericptr_t) maxroom,
  21.                        sizeof(struct mkroom));
  22.  
  23.          /* since maxroom moved, update affected level roomno values */
  24.          oroomno = nroom + ROOMOFFSET;
  25.          roomno += ROOMOFFSET;
  26.          for (i = croom->lx; i <= croom->hx; ++i)
  27.              for (j = croom->ly; j <= croom->hy; ++j) {
  28.                  if (levl[i][j].roomno == oroomno)
  29.                      levl[i][j].roomno = roomno;
  30.              }
  31.      }
  32.  
  33.      maxroom->hx = -1; /* just like add_room */
  34.  }
  35.  

mkmap

  1.  #define N_P1_ITER 1 /* tune map generation via this value */
  2.  #define N_P2_ITER 1 /* tune map generation via this value */
  3.  #define N_P3_ITER 2 /* tune map smoothing via this value */
  4.  
  5.  void
  6.  mkmap(init_lev)
  7.  lev_init *init_lev;
  8.  {
  9.      schar bg_typ = init_lev->bg, fg_typ = init_lev->fg;
  10.      boolean smooth = init_lev->smoothed, join = init_lev->joined;
  11.      xchar lit = init_lev->lit, walled = init_lev->walled;
  12.      int i;
  13.  
  14.      if (lit < 0)
  15.          lit = (rnd(1 + abs(depth(&u.uz))) < 11 && rn2(77)) ? 1 : 0;
  16.  
  17.      new_locations = (char *) alloc((WIDTH + 1) * HEIGHT);
  18.  
  19.      init_map(bg_typ);
  20.      init_fill(bg_typ, fg_typ);
  21.  
  22.      for (i = 0; i < N_P1_ITER; i++)
  23.          pass_one(bg_typ, fg_typ);
  24.  
  25.      for (i = 0; i < N_P2_ITER; i++)
  26.          pass_two(bg_typ, fg_typ);
  27.  
  28.      if (smooth)
  29.          for (i = 0; i < N_P3_ITER; i++)
  30.              pass_three(bg_typ, fg_typ);
  31.  
  32.      if (join)
  33.          join_map(bg_typ, fg_typ);
  34.  
  35.      finish_map(fg_typ, bg_typ, (boolean) lit, (boolean) walled,
  36.                 init_lev->icedpools);
  37.      /* a walled, joined level is cavernous, not mazelike -dlc */
  38.      if (walled && join) {
  39.          level.flags.is_maze_lev = FALSE;
  40.          level.flags.is_cavernous_lev = TRUE;
  41.      }
  42.      free(new_locations);
  43.  }
  44.  
  45.  /*mkmap.c*/