Source:NetHack 3.6.0/src/rect.c

From NetHackWiki
Jump to: navigation, search

Below is the full text to rect.c from the source code of NetHack 3.6.0. To link to a particular line, write [[Source:NetHack 3.6.0/src/rect.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	rect.c	$NHDT-Date: 1432512774 2015/05/25 00:12:54 $  $NHDT-Branch: master $:$NHDT-Revision: 1.11 $ */
  2.  /* Copyright (c) 1990 by Jean-Christophe Collet	 */
  3.  /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5.  #include "hack.h"
  6.  
  7.  int FDECL(get_rect_ind, (NhRect *));
  8.  
  9.  STATIC_DCL boolean FDECL(intersect, (NhRect *, NhRect *, NhRect *));
  10.  
  11.  /*
  12.   * In this file, we will handle the various rectangle functions we
  13.   * need for room generation.
  14.   */
  15.  
  16.  #define MAXRECT 50
  17.  #define XLIM 4
  18.  #define YLIM 3
  19.  
  20.  static NhRect rect[MAXRECT + 1];
  21.  static int rect_cnt;
  22.  

init_rect

  1.  /*
  2.   * Initialisation of internal structures. Should be called for every
  3.   * new level to be build...
  4.   */
  5.  
  6.  void
  7.  init_rect()
  8.  {
  9.      rect_cnt = 1;
  10.      rect[0].lx = rect[0].ly = 0;
  11.      rect[0].hx = COLNO - 1;
  12.      rect[0].hy = ROWNO - 1;
  13.  }
  14.  

get_rect_ind

  1.  /*
  2.   * Search Index of one precise NhRect.
  3.   *
  4.   */
  5.  
  6.  int
  7.  get_rect_ind(r)
  8.  NhRect *r;
  9.  {
  10.      register NhRect *rectp;
  11.      register int lx, ly, hx, hy;
  12.      register int i;
  13.  
  14.      lx = r->lx;
  15.      ly = r->ly;
  16.      hx = r->hx;
  17.      hy = r->hy;
  18.      for (i = 0, rectp = &rect[0]; i < rect_cnt; i++, rectp++)
  19.          if (lx == rectp->lx && ly == rectp->ly && hx == rectp->hx
  20.              && hy == rectp->hy)
  21.              return i;
  22.      return -1;
  23.  }
  24.  

get_rect

  1.  /*
  2.   * Search a free rectangle that include the one given in arg
  3.   */
  4.  
  5.  NhRect *
  6.  get_rect(r)
  7.  NhRect *r;
  8.  {
  9.      register NhRect *rectp;
  10.      register int lx, ly, hx, hy;
  11.      register int i;
  12.  
  13.      lx = r->lx;
  14.      ly = r->ly;
  15.      hx = r->hx;
  16.      hy = r->hy;
  17.      for (i = 0, rectp = &rect[0]; i < rect_cnt; i++, rectp++)
  18.          if (lx >= rectp->lx && ly >= rectp->ly && hx <= rectp->hx
  19.              && hy <= rectp->hy)
  20.              return rectp;
  21.      return 0;
  22.  }
  23.  

rnd_rect

  1.  /*
  2.   * Get some random NhRect from the list.
  3.   */
  4.  
  5.  NhRect *
  6.  rnd_rect()
  7.  {
  8.      return rect_cnt > 0 ? &rect[rn2(rect_cnt)] : 0;
  9.  }
  10.  

intersect

  1.  /*
  2.   * Search intersection between two rectangles (r1 & r2).
  3.   * return TRUE if intersection exist and put it in r3.
  4.   * otherwise returns FALSE
  5.   */
  6.  
  7.  STATIC_OVL boolean
  8.  intersect(r1, r2, r3)
  9.  NhRect *r1, *r2, *r3;
  10.  {
  11.      if (r2->lx > r1->hx || r2->ly > r1->hy || r2->hx < r1->lx
  12.          || r2->hy < r1->ly)
  13.          return FALSE;
  14.  
  15.      r3->lx = (r2->lx > r1->lx ? r2->lx : r1->lx);
  16.      r3->ly = (r2->ly > r1->ly ? r2->ly : r1->ly);
  17.      r3->hx = (r2->hx > r1->hx ? r1->hx : r2->hx);
  18.      r3->hy = (r2->hy > r1->hy ? r1->hy : r2->hy);
  19.  
  20.      if (r3->lx > r3->hx || r3->ly > r3->hy)
  21.          return FALSE;
  22.      return TRUE;
  23.  }
  24.  

remove_rect

  1.  /*
  2.   * Remove a rectangle from the list of free NhRect.
  3.   */
  4.  
  5.  void
  6.  remove_rect(r)
  7.  NhRect *r;
  8.  {
  9.      int ind;
  10.  
  11.      ind = get_rect_ind(r);
  12.      if (ind >= 0)
  13.          rect[ind] = rect[--rect_cnt];
  14.  }
  15.  

add_rect

  1.  /*
  2.   * Add a NhRect to the list.
  3.   */
  4.  
  5.  void
  6.  add_rect(r)
  7.  NhRect *r;
  8.  {
  9.      if (rect_cnt >= MAXRECT) {
  10.          if (wizard)
  11.              pline("MAXRECT may be too small.");
  12.          return;
  13.      }
  14.      /* Check that this NhRect is not included in another one */
  15.      if (get_rect(r))
  16.          return;
  17.      rect[rect_cnt] = *r;
  18.      rect_cnt++;
  19.  }
  20.  

split_rects

  1.  /*
  2.   * Okay, here we have two rectangles (r1 & r2).
  3.   * r1 was already in the list and r2 is included in r1.
  4.   * What we want is to allocate r2, that is split r1 into smaller rectangles
  5.   * then remove it.
  6.   */
  7.  
  8.  void
  9.  split_rects(r1, r2)
  10.  NhRect *r1, *r2;
  11.  {
  12.      NhRect r, old_r;
  13.      int i;
  14.  
  15.      old_r = *r1;
  16.      remove_rect(r1);
  17.  
  18.      /* Walk down since rect_cnt & rect[] will change... */
  19.      for (i = rect_cnt - 1; i >= 0; i--)
  20.          if (intersect(&rect[i], r2, &r))
  21.              split_rects(&rect[i], &r);
  22.  
  23.      if (r2->ly - old_r.ly - 1
  24.          > (old_r.hy < ROWNO - 1 ? 2 * YLIM : YLIM + 1) + 4) {
  25.          r = old_r;
  26.          r.hy = r2->ly - 2;
  27.          add_rect(&r);
  28.      }
  29.      if (r2->lx - old_r.lx - 1
  30.          > (old_r.hx < COLNO - 1 ? 2 * XLIM : XLIM + 1) + 4) {
  31.          r = old_r;
  32.          r.hx = r2->lx - 2;
  33.          add_rect(&r);
  34.      }
  35.      if (old_r.hy - r2->hy - 1 > (old_r.ly > 0 ? 2 * YLIM : YLIM + 1) + 4) {
  36.          r = old_r;
  37.          r.ly = r2->hy + 2;
  38.          add_rect(&r);
  39.      }
  40.      if (old_r.hx - r2->hx - 1 > (old_r.lx > 0 ? 2 * XLIM : XLIM + 1) + 4) {
  41.          r = old_r;
  42.          r.lx = r2->hx + 2;
  43.          add_rect(&r);
  44.      }
  45.  }
  46.  
  47.  /*rect.c*/